TOON: Replace Pathfinding _tempPath static buffers with Common::Array.

This commit is contained in:
D G Turner 2012-06-09 11:58:26 +01:00
parent 380d3f000a
commit a693603e1e
2 changed files with 28 additions and 19 deletions

View File

@ -248,17 +248,19 @@ bool PathFinding::walkLine(int32 x, int32 y, int32 x2, int32 y2) {
int32 cdx = (dx << 16) / t;
int32 cdy = (dy << 16) / t;
_gridPathCount = 0;
_tempPath.clear();
i32Point p;
for (int32 i = t; i > 0; i--) {
_tempPathX[i] = bx >> 16;
_tempPathY[i] = by >> 16;
_gridPathCount++;
p.x = bx >> 16;
p.y = by >> 16;
_tempPath.insert_at(0, p);
bx += cdx;
by += cdy;
}
_tempPathX[0] = x2;
_tempPathY[0] = y2;
p.x = x2;
p.y = y2;
_tempPath.insert_at(0, p);
return true;
}
@ -292,13 +294,13 @@ bool 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) {
_gridPathCount = 0;
_tempPath.clear();
return true;
}
// ignore path finding if the character is outside the screen
if (x < 0 || x > 1280 || y < 0 || y > 400 || destx < 0 || destx > 1280 || desty < 0 || desty > 400) {
_gridPathCount = 0;
_tempPath.clear();
return true;
}
@ -357,7 +359,7 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
// let's see if we found a result !
if (!_gridTemp[destx + desty * _width]) {
// didn't find anything
_gridPathCount = 0;
_tempPath.clear();
return false;
}
@ -415,10 +417,13 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) {
numpath++;
if ((bestX == x && bestY == y)) {
_gridPathCount = numpath;
memcpy(_tempPathX, retPathX, sizeof(int32) * numpath);
memcpy(_tempPathY, retPathY, sizeof(int32) * numpath);
_tempPath.clear();
i32Point p;
for (int32 i = 0; i < numpath; i++) {
p.x = retPathX[i];
p.y = retPathY[i];
_tempPath.push_back(p);
}
retVal = true;
break;

View File

@ -23,6 +23,8 @@
#ifndef TOON_PATH_H
#define TOON_PATH_H
#include "common/array.h"
#include "toon/toon.h"
namespace Toon {
@ -70,9 +72,9 @@ public:
void addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2);
void addBlockingEllipse(int32 x1, int32 y1, int32 w, int32 h);
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]; }
int32 getPathNodeCount() const { return _tempPath.size(); }
int32 getPathNodeX(int32 nodeId) const { return _tempPath[ _tempPath.size() - nodeId - 1].x; }
int32 getPathNodeY(int32 nodeId) const { return _tempPath[ _tempPath.size() - nodeId - 1].y; }
private:
static const uint8 kMaxBlockingRects = 16;
@ -85,9 +87,11 @@ private:
int32 _width;
int32 _height;
int32 _tempPathX[4096];
int32 _tempPathY[4096];
int32 _gridPathCount;
struct i32Point {
int32 x, y;
};
Common::Array<i32Point> _tempPath;
int32 _blockingRects[kMaxBlockingRects][5];
uint8 _numBlockingRects;