aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorD G Turner2011-07-20 23:05:24 +0100
committerD G Turner2011-07-20 23:05:24 +0100
commitd035bcae826e4212ddda531633d3cf0666d57109 (patch)
treeca5b225030317abf94c95515b13597127d7966fd
parent6a6594c7a9a60c0d3546cb2f67c89a9b57cbacea (diff)
downloadscummvm-rg350-d035bcae826e4212ddda531633d3cf0666d57109.tar.gz
scummvm-rg350-d035bcae826e4212ddda531633d3cf0666d57109.tar.bz2
scummvm-rg350-d035bcae826e4212ddda531633d3cf0666d57109.zip
TOON: Cleanup and Memory Usage Reduction fixes in PathFinding Heap.
This halves the size of the pathifnding heap, but adds warnings if push() is attempted on a full heap.
-rw-r--r--engines/toon/path.cpp50
-rw-r--r--engines/toon/path.h17
2 files changed, 36 insertions, 31 deletions
diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp
index 43a134e39b..6ffe8b3672 100644
--- a/engines/toon/path.cpp
+++ b/engines/toon/path.cpp
@@ -28,7 +28,7 @@ namespace Toon {
PathFindingHeap::PathFindingHeap() {
_count = 0;
- _alloc = 0;
+ _size = 0;
_data = NULL;
}
@@ -36,33 +36,37 @@ PathFindingHeap::~PathFindingHeap() {
delete[] _data;
}
-int32 PathFindingHeap::init(int32 size) {
+void PathFindingHeap::init(int32 size) {
debugC(1, kDebugPath, "init(%d)", size);
+ _size = size;
delete[] _data;
- _data = new HeapDataGrid[size * 2];
- memset(_data, 0, sizeof(HeapDataGrid) * size * 2);
+ _data = new HeapDataGrid[_size];
+ memset(_data, 0, sizeof(HeapDataGrid) * _size);
_count = 0;
- _alloc = size;
- return size;
}
-int32 PathFindingHeap::unload() {
+void PathFindingHeap::unload() {
+ _count = 0;
+ _size = 0;
delete[] _data;
_data = NULL;
- return 0;
}
-int32 PathFindingHeap::clear() {
- //debugC(1, kDebugPath, "clear()");
+void PathFindingHeap::clear() {
+ debugC(1, kDebugPath, "clear()");
_count = 0;
- memset(_data, 0, sizeof(HeapDataGrid) * _alloc * 2);
- return 1;
+ memset(_data, 0, sizeof(HeapDataGrid) * _size);
}
-int32 PathFindingHeap::push(int32 x, int32 y, int32 weight) {
- //debugC(6, kDebugPath, "push(%d, %d, %d)", x, y, weight);
+void PathFindingHeap::push(int32 x, int32 y, int32 weight) {
+ debugC(2, kDebugPath, "push(%d, %d, %d)", x, y, weight);
+
+ if (_count == _size - 1) {
+ warning("Aborting attempt to push onto PathFindingHeap at maximum size: %d", _count);
+ return;
+ }
_count++;
_data[_count]._x = x;
@@ -87,14 +91,15 @@ int32 PathFindingHeap::push(int32 x, int32 y, int32 weight) {
break;
}
}
- return 1;
}
-int32 PathFindingHeap::pop(int32 *x, int32 *y, int32 *weight) {
- //debugC(6, kDebugPath, "pop(x, y, weight)");
+void PathFindingHeap::pop(int32 *x, int32 *y, int32 *weight) {
+ debugC(2, kDebugPath, "pop(x, y, weight)");
- if (!_count)
- return 0;
+ if (!_count) {
+ warning("Attempt to pop empty PathFindingHeap!");
+ return;
+ }
*x = _data[1]._x;
*y = _data[1]._y;
@@ -103,7 +108,7 @@ int32 PathFindingHeap::pop(int32 *x, int32 *y, int32 *weight) {
_data[1] = _data[_count];
_count--;
if (!_count)
- return 0;
+ return;
int32 lMin = 1;
int32 lT = 1;
@@ -129,7 +134,6 @@ int32 PathFindingHeap::pop(int32 *x, int32 *y, int32 *weight) {
break;
}
}
- return 0;
}
PathFinding::PathFinding(ToonEngine *vm) : _vm(vm) {
@@ -164,7 +168,7 @@ bool PathFinding::isLikelyWalkable(int32 x, int32 y) {
}
bool PathFinding::isWalkable(int32 x, int32 y) {
- //debugC(6, kDebugPath, "isWalkable(%d, %d)", x, y);
+ debugC(2, kDebugPath, "isWalkable(%d, %d)", x, y);
bool maskWalk = (_currentMask->getData(x, y) & 0x1f) > 0;
@@ -299,7 +303,7 @@ int32 PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
_heap->push(curX, curY, abs(destx - x) + abs(desty - y));
int wei = 0;
- while (_heap->_count) {
+ while (_heap->getCount()) {
wei = 0;
_heap->pop(&curX, &curY, &curWeight);
int curNode = curX + curY * _width;
diff --git a/engines/toon/path.h b/engines/toon/path.h
index 329127c9ce..2de58064f0 100644
--- a/engines/toon/path.h
+++ b/engines/toon/path.h
@@ -38,17 +38,18 @@ public:
PathFindingHeap();
~PathFindingHeap();
- int32 _alloc;
- int32 _count;
-
- int32 push(int32 x, int32 y, int32 weight);
- int32 pop(int32 *x, int32 *y, int32 *weight);
- int32 init(int32 size);
- int32 clear();
- int32 unload();
+ void push(int32 x, int32 y, int32 weight);
+ void pop(int32 *x, int32 *y, int32 *weight);
+ void init(int32 size);
+ void clear();
+ void unload();
+ int32 getCount() { return _count; }
private:
HeapDataGrid *_data;
+
+ int32 _size;
+ int32 _count;
};
class PathFinding {