aboutsummaryrefslogtreecommitdiff
path: root/engines/toon/path.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'engines/toon/path.cpp')
-rw-r--r--engines/toon/path.cpp83
1 files changed, 60 insertions, 23 deletions
diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp
index 484e621a64..1d3b32b804 100644
--- a/engines/toon/path.cpp
+++ b/engines/toon/path.cpp
@@ -27,9 +27,20 @@
namespace Toon {
+PathFindingHeap::PathFindingHeap() {
+ _count = 0;
+ _alloc = 0;
+ _data = NULL;
+}
+
+PathFindingHeap::~PathFindingHeap() {
+ delete[] _data;
+}
+
int32 PathFindingHeap::init(int32 size) {
debugC(1, kDebugPath, "init(%d)", size);
+ delete[] _data;
_data = new HeapDataGrid[size * 2];
memset(_data, 0, sizeof(HeapDataGrid) * size * 2);
_count = 0;
@@ -37,13 +48,13 @@ int32 PathFindingHeap::init(int32 size) {
return size;
}
-int PathFindingHeap::unload() {
- if (_data)
- delete[] _data;
+int32 PathFindingHeap::unload() {
+ delete[] _data;
+ _data = NULL;
return 0;
}
-int PathFindingHeap::clear() {
+int32 PathFindingHeap::clear() {
//debugC(1, kDebugPath, "clear()");
_count = 0;
@@ -51,7 +62,7 @@ int PathFindingHeap::clear() {
return 1;
}
-int PathFindingHeap::push(int x, int y, int weight) {
+int32 PathFindingHeap::push(int32 x, int32 y, int32 weight) {
//debugC(6, kDebugPath, "push(%d, %d, %d)", x, y, weight);
_count++;
@@ -126,15 +137,15 @@ PathFinding::PathFinding(ToonEngine *vm) : _vm(vm) {
_width = 0;
_height = 0;
_heap = new PathFindingHeap();
- _gridTemp = 0;
+ _gridTemp = NULL;
_numBlockingRects = 0;
}
PathFinding::~PathFinding(void) {
- if (_heap) {
+ if (_heap)
_heap->unload();
- delete _heap;
- }
+ delete _heap;
+ delete[] _gridTemp;
}
bool PathFinding::isWalkable(int32 x, int32 y) {
@@ -193,7 +204,34 @@ int32 PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32
}
}
-int PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
+bool PathFinding::lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2) {
+
+ uint32 bx = x << 16;
+ int32 dx = x2 - x;
+ uint32 by = y << 16;
+ int32 dy = y2 - y;
+ uint32 adx = abs(dx);
+ uint32 ady = abs(dy);
+ int32 t = 0;
+ if (adx <= ady)
+ t = ady;
+ else
+ t = adx;
+
+ int32 cdx = (dx << 16) / t;
+ int32 cdy = (dy << 16) / t;
+
+ int32 i = t;
+ while (i) {
+ if(!isWalkable(bx >> 16, by >> 16))
+ return false;
+ bx += cdx;
+ by += cdy;
+ i--;
+ }
+ return true;
+}
+int32 PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
debugC(1, kDebugPath, "findPath(%d, %d, %d, %d)", x, y, destx, desty);
if (x == destx && y == desty) {
@@ -201,6 +239,9 @@ int PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
return true;
}
+ // first test direct line
+ //if(lineIsWalkable(x,y,destx,desty))
+
memset(_gridTemp , 0, _width * _height * sizeof(int32));
_heap->clear();
int32 curX = x;
@@ -212,18 +253,15 @@ int PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
_heap->push(curX, curY, abs(destx - x) + abs(desty - y));
int wei = 0;
-// Strangerke - Commented (not used)
-// byte *mask = _currentMask->getDataPtr();
-
while (_heap->_count) {
wei = 0;
_heap->pop(&curX, &curY, &curWeight);
int curNode = curX + curY * _width;
- int32 endX = MIN(curX + 1, _width - 1);
- int32 endY = MIN(curY + 1, _height - 1);
- int32 startX = MAX(curX - 1, 0);
- int32 startY = MAX(curY - 1, 0);
+ int32 endX = MIN<int32>(curX + 1, _width - 1);
+ int32 endY = MIN<int32>(curY + 1, _height - 1);
+ int32 startX = MAX<int32>(curX - 1, 0);
+ int32 startY = MAX<int32>(curY - 1, 0);
for (int32 px = startX; px <= endX; px++) {
for (int py = startY; py <= endY; py++) {
@@ -271,10 +309,10 @@ next:
int32 bestX = -1;
int32 bestY = -1;
- int32 endX = MIN(curX + 1, _width - 1);
- int32 endY = MIN(curY + 1, _height - 1);
- int32 startX = MAX(curX - 1, 0);
- int32 startY = MAX(curY - 1, 0);
+ int32 endX = MIN<int32>(curX + 1, _width - 1);
+ int32 endY = MIN<int32>(curY + 1, _height - 1);
+ int32 startX = MAX<int32>(curX - 1, 0);
+ int32 startY = MAX<int32>(curY - 1, 0);
for (int32 px = startX; px <= endX; px++) {
for (int32 py = startY; py <= endY; py++) {
@@ -323,8 +361,7 @@ void PathFinding::init(Picture *mask) {
_currentMask = mask;
_heap->unload();
_heap->init(_width * _height);
- if (_gridTemp)
- delete[] _gridTemp;
+ delete[] _gridTemp;
_gridTemp = new int32[_width*_height];
}