aboutsummaryrefslogtreecommitdiff
path: root/engines
diff options
context:
space:
mode:
authorD G Turner2012-06-07 14:41:04 +0100
committerD G Turner2012-06-07 14:41:04 +0100
commit380d3f000a12c4b923d7330cc88551f73abd7265 (patch)
treea2239a78817aa23a0b63657750b9a9f25bd257ec /engines
parent70f09e4ee36525025421db28a5a50b8b5dc0a963 (diff)
downloadscummvm-rg350-380d3f000a12c4b923d7330cc88551f73abd7265.tar.gz
scummvm-rg350-380d3f000a12c4b923d7330cc88551f73abd7265.tar.bz2
scummvm-rg350-380d3f000a12c4b923d7330cc88551f73abd7265.zip
TOON: Further cleanup to Pathfinding Class.
Removal of some unused variables, logical reordering of functions and minor changes to reduce minor duplication. No functional changes.
Diffstat (limited to 'engines')
-rw-r--r--engines/toon/path.cpp55
-rw-r--r--engines/toon/path.h16
2 files changed, 30 insertions, 41 deletions
diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp
index 527be13c24..b3f9b306be 100644
--- a/engines/toon/path.cpp
+++ b/engines/toon/path.cpp
@@ -174,7 +174,7 @@ void PathFinding::init(Picture *mask) {
}
bool PathFinding::isLikelyWalkable(int32 x, int32 y) {
- for (int32 i = 0; i < _numBlockingRects; i++) {
+ for (uint8 i = 0; i < _numBlockingRects; i++) {
if (_blockingRects[i][4] == 0) {
if (x >= _blockingRects[i][0] && x <= _blockingRects[i][2] && y >= _blockingRects[i][1] && y < _blockingRects[i][3])
return false;
@@ -364,11 +364,11 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
curX = destx;
curY = desty;
- int32 *retPathX = (int32 *)malloc(4096 * sizeof(int32));
- int32 *retPathY = (int32 *)malloc(4096 * sizeof(int32));
+ int32 *retPathX = new int32[4096];
+ int32 *retPathY = new int32[4096];
if (!retPathX || !retPathY) {
- free(retPathX);
- free(retPathY);
+ delete retPathX;
+ delete retPathY;
error("[PathFinding::findPath] Cannot allocate pathfinding buffers");
}
@@ -380,6 +380,7 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
numpath++;
int32 bestscore = sq[destx + desty * _width];
+ bool retVal = false;
while (true) {
int32 bestX = -1;
int32 bestY = -1;
@@ -406,12 +407,8 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
}
}
- if (bestX < 0 || bestY < 0) {
- free(retPathX);
- free(retPathY);
-
- return false;
- }
+ if (bestX < 0 || bestY < 0)
+ break;
retPathX[numpath] = bestX;
retPathY[numpath] = bestY;
@@ -423,28 +420,26 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
memcpy(_tempPathX, retPathX, sizeof(int32) * numpath);
memcpy(_tempPathY, retPathY, sizeof(int32) * numpath);
- free(retPathX);
- free(retPathY);
-
- return true;
+ retVal = true;
+ break;
}
curX = bestX;
curY = bestY;
}
- free(retPathX);
- free(retPathY);
+ delete retPathX;
+ delete retPathY;
- return false;
-}
-
-void PathFinding::resetBlockingRects() {
- _numBlockingRects = 0;
+ return retVal;
}
void PathFinding::addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2) {
debugC(1, kDebugPath, "addBlockingRect(%d, %d, %d, %d)", x1, y1, x2, y2);
+ if (_numBlockingRects >= kMaxBlockingRects) {
+ warning("Maximum number of %d Blocking Rects reached!", kMaxBlockingRects);
+ return;
+ }
_blockingRects[_numBlockingRects][0] = x1;
_blockingRects[_numBlockingRects][1] = y1;
@@ -456,6 +451,10 @@ void PathFinding::addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2) {
void PathFinding::addBlockingEllipse(int32 x1, int32 y1, int32 w, int32 h) {
debugC(1, kDebugPath, "addBlockingEllipse(%d, %d, %d, %d)", x1, y1, w, h);
+ if (_numBlockingRects >= kMaxBlockingRects) {
+ warning("Maximum number of %d Blocking Rects reached!", kMaxBlockingRects);
+ return;
+ }
_blockingRects[_numBlockingRects][0] = x1;
_blockingRects[_numBlockingRects][1] = y1;
@@ -465,16 +464,4 @@ void PathFinding::addBlockingEllipse(int32 x1, int32 y1, int32 w, int32 h) {
_numBlockingRects++;
}
-int32 PathFinding::getPathNodeCount() const {
- return _gridPathCount;
-}
-
-int32 PathFinding::getPathNodeX(int32 nodeId) const {
- return _tempPathX[ _gridPathCount - nodeId - 1];
-}
-
-int32 PathFinding::getPathNodeY(int32 nodeId) const {
- return _tempPathY[ _gridPathCount - nodeId - 1];
-}
-
} // End of namespace Toon
diff --git a/engines/toon/path.h b/engines/toon/path.h
index 30a7a53e9d..26abb411cc 100644
--- a/engines/toon/path.h
+++ b/engines/toon/path.h
@@ -66,15 +66,17 @@ public:
bool lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2);
bool walkLine(int32 x, int32 y, int32 x2, int32 y2);
- void resetBlockingRects();
+ void resetBlockingRects() { _numBlockingRects = 0; }
void addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2);
void addBlockingEllipse(int32 x1, int32 y1, int32 w, int32 h);
- int32 getPathNodeCount() const;
- int32 getPathNodeX(int32 nodeId) const;
- int32 getPathNodeY(int32 nodeId) const;
+ int32 getPathNodeCount() const { return _gridPathCount; }
+ int32 getPathNodeX(int32 nodeId) const { return _tempPathX[ _gridPathCount - nodeId - 1]; }
+ int32 getPathNodeY(int32 nodeId) const { return _tempPathY[ _gridPathCount - nodeId - 1]; }
private:
+ static const uint8 kMaxBlockingRects = 16;
+
Picture *_currentMask;
PathFindingHeap *_heap;
@@ -85,10 +87,10 @@ private:
int32 _tempPathX[4096];
int32 _tempPathY[4096];
- int32 _blockingRects[16][5];
- int32 _numBlockingRects;
- int32 _allocatedGridPathCount;
int32 _gridPathCount;
+
+ int32 _blockingRects[kMaxBlockingRects][5];
+ uint8 _numBlockingRects;
};
} // End of namespace Toon