From dd558510dc60b61d9f960c7f49e8bc0327d8115b Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 7 Jun 2012 08:36:12 +0100 Subject: TOON: Move PathFindingHeap API to use int16 for x,y coordinates. The internal x,y point representation was already changed to int16 anyway, so this just harmonises this with the external API (and with Common::Point which uses int16). --- engines/toon/path.cpp | 12 +++++++----- engines/toon/path.h | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 2dd5fc45e2..540290d823 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -60,7 +60,7 @@ void PathFindingHeap::clear() { memset(_data, 0, sizeof(HeapDataGrid) * _size); } -void PathFindingHeap::push(int32 x, int32 y, int32 weight) { +void PathFindingHeap::push(int16 x, int16 y, int32 weight) { debugC(2, kDebugPath, "push(%d, %d, %d)", x, y, weight); if (_count == _size) { @@ -87,7 +87,7 @@ void PathFindingHeap::push(int32 x, int32 y, int32 weight) { int32 lMax = _count-1; int32 lT = 0; - while (1) { + while (true) { if (lMax <= 0) break; lT = (lMax-1) / 2; @@ -104,7 +104,7 @@ void PathFindingHeap::push(int32 x, int32 y, int32 weight) { } } -void PathFindingHeap::pop(int32 *x, int32 *y, int32 *weight) { +void PathFindingHeap::pop(int16 *x, int16 *y, int32 *weight) { debugC(2, kDebugPath, "pop(x, y, weight)"); if (!_count) { @@ -123,7 +123,7 @@ void PathFindingHeap::pop(int32 *x, int32 *y, int32 *weight) { int32 lMin = 0; int32 lT = 0; - while (1) { + while (true) { lT = (lMin << 1) + 1; if (lT < _count) { if (lT < _count-1) { @@ -315,7 +315,9 @@ int32 PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { while (_heap->getCount()) { wei = 0; - _heap->pop(&curX, &curY, &curWeight); + int16 tempCurX, tempCurY; + _heap->pop(&tempCurX, &tempCurY, &curWeight); + curX = tempCurX, curY = tempCurY; // FIXME - Bodge to match heap->pop types int curNode = curX + curY * _width; int32 endX = MIN(curX + 1, _width - 1); diff --git a/engines/toon/path.h b/engines/toon/path.h index 2de58064f0..df2b2e94be 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -38,8 +38,8 @@ public: PathFindingHeap(); ~PathFindingHeap(); - void push(int32 x, int32 y, int32 weight); - void pop(int32 *x, int32 *y, int32 *weight); + void push(int16 x, int16 y, int32 weight); + void pop(int16 *x, int16 *y, int32 *weight); void init(int32 size); void clear(); void unload(); -- cgit v1.2.3 From 8deb8b3d42927d1d6e73350f96353eda1b10a1fc Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 7 Jun 2012 12:33:13 +0100 Subject: TOON: Minor cleanup and formatting fixes to Pathfinding class. --- engines/toon/path.cpp | 51 +++++++++++++++++++++++---------------------------- engines/toon/path.h | 7 ++++--- 2 files changed, 27 insertions(+), 31 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 540290d823..101778d4b4 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -161,6 +161,18 @@ PathFinding::~PathFinding(void) { delete[] _gridTemp; } +void PathFinding::init(Picture *mask) { + debugC(1, kDebugPath, "init(mask)"); + + _width = mask->getWidth(); + _height = mask->getHeight(); + _currentMask = mask; + _heap->unload(); + _heap->init(500); + delete[] _gridTemp; + _gridTemp = new int32[_width * _height]; +} + bool PathFinding::isLikelyWalkable(int32 x, int32 y) { for (int32 i = 0; i < _numBlockingRects; i++) { if (_blockingRects[i][4] == 0) { @@ -180,12 +192,10 @@ bool PathFinding::isLikelyWalkable(int32 x, int32 y) { bool PathFinding::isWalkable(int32 x, int32 y) { debugC(2, kDebugPath, "isWalkable(%d, %d)", x, y); - bool maskWalk = (_currentMask->getData(x, y) & 0x1f) > 0; - - return maskWalk; + return (_currentMask->getData(x, y) & 0x1f) > 0; } -int32 PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 *fyy, int origX, int origY) { +bool PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 *fyy, int origX, int origY) { debugC(1, kDebugPath, "findClosestWalkingPoint(%d, %d, fxx, fyy, %d, %d)", xx, yy, origX, origY); int32 currentFound = -1; @@ -214,11 +224,11 @@ int32 PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 if (currentFound != -1) { *fxx = currentFound % _width; *fyy = currentFound / _width; - return 1; + return true; } else { *fxx = 0; *fyy = 0; - return 0; + return false; } } @@ -238,15 +248,13 @@ bool PathFinding::walkLine(int32 x, int32 y, int32 x2, int32 y2) { int32 cdx = (dx << 16) / t; int32 cdy = (dy << 16) / t; - int32 i = t; _gridPathCount = 0; - while (i) { + for (int32 i = t; i > 0; i--) { _tempPathX[i] = bx >> 16; _tempPathY[i] = by >> 16; _gridPathCount++; bx += cdx; by += cdy; - i--; } _tempPathX[0] = x2; @@ -271,17 +279,16 @@ bool PathFinding::lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2) { int32 cdx = (dx << 16) / t; int32 cdy = (dy << 16) / t; - int32 i = t; - while (i) { + for (int32 i = t; i > 0; 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) { + +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) { @@ -373,7 +380,7 @@ int32 PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { numpath++; int32 bestscore = sq[destx + desty * _width]; - while (1) { + while (true) { int32 bestX = -1; int32 bestY = -1; @@ -403,7 +410,7 @@ int32 PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { free(retPathX); free(retPathY); - return 0; + return false; } retPathX[numpath] = bestX; @@ -432,18 +439,6 @@ int32 PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { return false; } -void PathFinding::init(Picture *mask) { - debugC(1, kDebugPath, "init(mask)"); - - _width = mask->getWidth(); - _height = mask->getHeight(); - _currentMask = mask; - _heap->unload(); - _heap->init(500); - delete[] _gridTemp; - _gridTemp = new int32[_width*_height]; -} - void PathFinding::resetBlockingRects() { _numBlockingRects = 0; } @@ -460,7 +455,7 @@ 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, "addBlockingRect(%d, %d, %d, %d)", x1, y1, w, h); + debugC(1, kDebugPath, "addBlockingEllipse(%d, %d, %d, %d)", x1, y1, w, h); _blockingRects[_numBlockingRects][0] = x1; _blockingRects[_numBlockingRects][1] = y1; diff --git a/engines/toon/path.h b/engines/toon/path.h index df2b2e94be..7709dfe2a0 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -57,13 +57,14 @@ public: PathFinding(ToonEngine *vm); ~PathFinding(); - int32 findPath(int32 x, int32 y, int32 destX, int32 destY); - int32 findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 *fyy, int origX = -1, int origY = -1); + void init(Picture *mask); + + bool findPath(int32 x, int32 y, int32 destX, int32 destY); + bool findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 *fyy, int origX = -1, int origY = -1); bool isWalkable(int32 x, int32 y); bool isLikelyWalkable(int32 x, int32 y); bool lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2); bool walkLine(int32 x, int32 y, int32 x2, int32 y2); - void init(Picture *mask); void resetBlockingRects(); void addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2); -- cgit v1.2.3 From 6cda28adc9e1adb5b05bdb58baf449f2e24b9945 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 7 Jun 2012 13:20:53 +0100 Subject: TOON: Remove unecessary usages of g_system. --- engines/toon/toon.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp index 657e18635f..9e7aa65265 100644 --- a/engines/toon/toon.cpp +++ b/engines/toon/toon.cpp @@ -168,7 +168,7 @@ void ToonEngine::waitForScriptStep() { // Wait after a specified number of script steps when executing a script // to lower CPU usage if (++_scriptStep >= 40) { - g_system->delayMillis(1); + _system->delayMillis(1); _scriptStep = 0; } } @@ -2979,8 +2979,7 @@ bool ToonEngine::saveGame(int32 slot, const Common::String &saveGameDesc) { return false; // dialog aborted Common::String savegameFile = getSavegameName(savegameId); - Common::SaveFileManager *saveMan = g_system->getSavefileManager(); - Common::OutSaveFile *saveFile = saveMan->openForSaving(savegameFile); + Common::OutSaveFile *saveFile = _saveFileMan->openForSaving(savegameFile); if (!saveFile) return false; @@ -3068,8 +3067,7 @@ bool ToonEngine::loadGame(int32 slot) { return false; // dialog aborted Common::String savegameFile = getSavegameName(savegameId); - Common::SaveFileManager *saveMan = g_system->getSavefileManager(); - Common::InSaveFile *loadFile = saveMan->openForLoading(savegameFile); + Common::InSaveFile *loadFile = _saveFileMan->openForLoading(savegameFile); if (!loadFile) return false; -- cgit v1.2.3 From 70f09e4ee36525025421db28a5a50b8b5dc0a963 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 7 Jun 2012 13:24:54 +0100 Subject: TOON: Reduce unecessary linkages in Pathfinding class. --- engines/toon/path.cpp | 2 +- engines/toon/path.h | 17 ++++++++--------- engines/toon/toon.cpp | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 101778d4b4..527be13c24 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -146,7 +146,7 @@ void PathFindingHeap::pop(int16 *x, int16 *y, int32 *weight) { } } -PathFinding::PathFinding(ToonEngine *vm) : _vm(vm) { +PathFinding::PathFinding() { _width = 0; _height = 0; _heap = new PathFindingHeap(); diff --git a/engines/toon/path.h b/engines/toon/path.h index 7709dfe2a0..30a7a53e9d 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -28,11 +28,6 @@ namespace Toon { // binary heap system for fast A* -struct HeapDataGrid { - int16 _x, _y; - int16 _weight; -}; - class PathFindingHeap { public: PathFindingHeap(); @@ -46,6 +41,11 @@ public: int32 getCount() { return _count; } private: + struct HeapDataGrid { + int16 _x, _y; + int16 _weight; + }; + HeapDataGrid *_data; int32 _size; @@ -54,7 +54,7 @@ private: class PathFinding { public: - PathFinding(ToonEngine *vm); + PathFinding(); ~PathFinding(); void init(Picture *mask); @@ -73,7 +73,8 @@ public: int32 getPathNodeCount() const; int32 getPathNodeX(int32 nodeId) const; int32 getPathNodeY(int32 nodeId) const; -protected: + +private: Picture *_currentMask; PathFindingHeap *_heap; @@ -88,8 +89,6 @@ protected: int32 _numBlockingRects; int32 _allocatedGridPathCount; int32 _gridPathCount; - - ToonEngine *_vm; }; } // End of namespace Toon diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp index 9e7aa65265..0b39432b53 100644 --- a/engines/toon/toon.cpp +++ b/engines/toon/toon.cpp @@ -100,7 +100,7 @@ void ToonEngine::init() { syncSoundSettings(); - _pathFinding = new PathFinding(this); + _pathFinding = new PathFinding(); resources()->openPackage("LOCAL.PAK"); resources()->openPackage("ONETIME.PAK"); -- cgit v1.2.3 From 380d3f000a12c4b923d7330cc88551f73abd7265 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 7 Jun 2012 14:41:04 +0100 Subject: 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. --- engines/toon/path.cpp | 55 ++++++++++++++++++++------------------------------- engines/toon/path.h | 16 ++++++++------- 2 files changed, 30 insertions(+), 41 deletions(-) (limited to 'engines/toon') 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 -- cgit v1.2.3 From a693603e1ee1a58f2d4c72f726a9f92a3ddf1627 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Sat, 9 Jun 2012 11:58:26 +0100 Subject: TOON: Replace Pathfinding _tempPath static buffers with Common::Array. --- engines/toon/path.cpp | 31 ++++++++++++++++++------------- engines/toon/path.h | 16 ++++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index b3f9b306be..b4fe412144 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -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; diff --git a/engines/toon/path.h b/engines/toon/path.h index 26abb411cc..6a22096054 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -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 _tempPath; int32 _blockingRects[kMaxBlockingRects][5]; uint8 _numBlockingRects; -- cgit v1.2.3 From fae9f4d5baf258ecb4f9e25fe1364e42442385e2 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Sat, 9 Jun 2012 12:33:30 +0100 Subject: TOON: Replace Pathfinding retPath static buffers with Common::Array. --- engines/toon/path.cpp | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index b4fe412144..2b41995a98 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -366,20 +366,13 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { curX = destx; curY = desty; - int32 *retPathX = new int32[4096]; - int32 *retPathY = new int32[4096]; - if (!retPathX || !retPathY) { - delete retPathX; - delete retPathY; + Common::Array retPath; - error("[PathFinding::findPath] Cannot allocate pathfinding buffers"); - } - - int32 numpath = 0; + i32Point p; + p.x = curX; + p.y = curY; + retPath.push_back(p); - retPathX[numpath] = curX; - retPathY[numpath] = curY; - numpath++; int32 bestscore = sq[destx + desty * _width]; bool retVal = false; @@ -412,18 +405,15 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { if (bestX < 0 || bestY < 0) break; - retPathX[numpath] = bestX; - retPathY[numpath] = bestY; - numpath++; + i32Point pp; + pp.x = bestX; + pp.y = bestY; + retPath.push_back(pp); if ((bestX == x && bestY == y)) { _tempPath.clear(); - i32Point p; - for (int32 i = 0; i < numpath; i++) { - p.x = retPathX[i]; - p.y = retPathY[i]; - _tempPath.push_back(p); - } + for (uint32 i = 0; i < retPath.size(); i++) + _tempPath.push_back(retPath[i]); retVal = true; break; @@ -433,9 +423,6 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { curY = bestY; } - delete retPathX; - delete retPathY; - return retVal; } -- cgit v1.2.3 From 2d0cedab1ff3984d1d269c0fb735f9179ca8e7d9 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Sat, 9 Jun 2012 13:43:42 +0100 Subject: TOON: Minor cleanups in Pathfinding class. No functional change. --- engines/toon/path.cpp | 41 ++++++++++++++++++++--------------------- engines/toon/path.h | 2 +- 2 files changed, 21 insertions(+), 22 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 2b41995a98..dfdf095af9 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -150,7 +150,7 @@ PathFinding::PathFinding() { _width = 0; _height = 0; _heap = new PathFindingHeap(); - _gridTemp = NULL; + _sq = NULL; _numBlockingRects = 0; } @@ -158,7 +158,7 @@ PathFinding::~PathFinding(void) { if (_heap) _heap->unload(); delete _heap; - delete[] _gridTemp; + delete[] _sq; } void PathFinding::init(Picture *mask) { @@ -169,8 +169,8 @@ void PathFinding::init(Picture *mask) { _currentMask = mask; _heap->unload(); _heap->init(500); - delete[] _gridTemp; - _gridTemp = new int32[_width * _height]; + delete[] _sq; + _sq = new int32[_width * _height]; } bool PathFinding::isLikelyWalkable(int32 x, int32 y) { @@ -311,23 +311,22 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { } // no direct line, we use the standard A* algorithm - memset(_gridTemp , 0, _width * _height * sizeof(int32)); + memset(_sq , 0, _width * _height * sizeof(int32)); _heap->clear(); int32 curX = x; int32 curY = y; int32 curWeight = 0; - int32 *sq = _gridTemp; - sq[curX + curY *_width] = 1; + _sq[curX + curY *_width] = 1; _heap->push(curX, curY, abs(destx - x) + abs(desty - y)); - int wei = 0; + int32 wei = 0; while (_heap->getCount()) { wei = 0; int16 tempCurX, tempCurY; _heap->pop(&tempCurX, &tempCurY, &curWeight); curX = tempCurX, curY = tempCurY; // FIXME - Bodge to match heap->pop types - int curNode = curX + curY * _width; + int32 curNode = curX + curY * _width; int32 endX = MIN(curX + 1, _width - 1); int32 endY = MIN(curY + 1, _height - 1); @@ -336,17 +335,17 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { bool next = false; for (int32 px = startX; px <= endX && !next; px++) { - for (int py = startY; py <= endY && !next; py++) { + for (int32 py = startY; py <= endY && !next; py++) { if (px != curX || py != curY) { wei = ((abs(px - curX) + abs(py - curY))); int32 curPNode = px + py * _width; if (isWalkable(px, py)) { // walkable ? - int sum = sq[curNode] + wei * (1 + (isLikelyWalkable(px, py) ? 5 : 0)); - if (sq[curPNode] > sum || !sq[curPNode]) { - int newWeight = abs(destx - px) + abs(desty - py); - sq[curPNode] = sum; - _heap->push(px, py, sq[curPNode] + newWeight); + int32 sum = _sq[curNode] + wei * (1 + (isLikelyWalkable(px, py) ? 5 : 0)); + if (_sq[curPNode] > sum || !_sq[curPNode]) { + int32 newWeight = abs(destx - px) + abs(desty - py); + _sq[curPNode] = sum; + _heap->push(px, py, _sq[curPNode] + newWeight); if (!newWeight) next = true; // we found it ! } @@ -357,7 +356,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]) { + if (!_sq[destx + desty * _width]) { // didn't find anything _tempPath.clear(); return false; @@ -373,7 +372,7 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { p.y = curY; retPath.push_back(p); - int32 bestscore = sq[destx + desty * _width]; + int32 bestscore = _sq[destx + desty * _width]; bool retVal = false; while (true) { @@ -390,10 +389,10 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { if (px != curX || py != curY) { wei = abs(px - curX) + abs(py - curY); - int PNode = px + py * _width; - if (sq[PNode] && (isWalkable(px, py))) { - if (sq[PNode] < bestscore) { - bestscore = sq[PNode]; + int32 PNode = px + py * _width; + if (_sq[PNode] && (isWalkable(px, py))) { + if (_sq[PNode] < bestscore) { + bestscore = _sq[PNode]; bestX = px; bestY = py; } diff --git a/engines/toon/path.h b/engines/toon/path.h index 6a22096054..8e2c61d482 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -83,7 +83,7 @@ private: PathFindingHeap *_heap; - int32 *_gridTemp; + int32 *_sq; int32 _width; int32 _height; -- cgit v1.2.3 From 15046a7529e3c755de1f00b4a2666786eb2bd4f8 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 10 Jun 2012 04:14:17 +0200 Subject: GUI: Get rid of SaveLoadChooser::setSaveMode. We already pass the title and process button name to the constructor of SaveLoadChooser and then do not offer any way of changing it, thus changing the edit mode of the chooser is kind of pointless and was never actually used. Instead we pass the mode on SaveLoadChooser construction now. --- engines/toon/toon.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp index 657e18635f..9da06ce5be 100644 --- a/engines/toon/toon.cpp +++ b/engines/toon/toon.cpp @@ -2961,8 +2961,7 @@ bool ToonEngine::saveGame(int32 slot, const Common::String &saveGameDesc) { EngineMan.findGame(_gameDescription->gameid, &plugin); if (slot == -1) { - GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Save game:", "Save"); - dialog->setSaveMode(true); + GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Save game:", "Save", true); savegameId = dialog->runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName()); savegameDescription = dialog->getResultString(); delete dialog; @@ -3057,8 +3056,7 @@ bool ToonEngine::loadGame(int32 slot) { EngineMan.findGame(_gameDescription->gameid, &plugin); if (slot == -1) { - GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Restore game:", "Restore"); - dialog->setSaveMode(false); + GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Restore game:", "Restore", false); savegameId = dialog->runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName()); delete dialog; } else { -- cgit v1.2.3 From 7c5cf1b400808865a5f601f70d624ad6704a0c8c Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 10 Jun 2012 04:49:42 +0200 Subject: GUI: Add helper to SaveLoadChooser, which uses the currently active target. This reduces the code duplication in all client code, which formerly duplicated the querying of the plugin, game id etc. and now simply calls the newly added method runModalWithCurrentTarget() on a SaveLoadChooser object. --- engines/toon/toon.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp index 9da06ce5be..be298e1236 100644 --- a/engines/toon/toon.cpp +++ b/engines/toon/toon.cpp @@ -2955,14 +2955,12 @@ Common::String ToonEngine::getSavegameName(int nr) { } bool ToonEngine::saveGame(int32 slot, const Common::String &saveGameDesc) { - const EnginePlugin *plugin = NULL; int16 savegameId; Common::String savegameDescription; - EngineMan.findGame(_gameDescription->gameid, &plugin); if (slot == -1) { GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Save game:", "Save", true); - savegameId = dialog->runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName()); + savegameId = dialog->runModalWithCurrentTarget(); savegameDescription = dialog->getResultString(); delete dialog; } else { @@ -3051,13 +3049,11 @@ bool ToonEngine::saveGame(int32 slot, const Common::String &saveGameDesc) { } bool ToonEngine::loadGame(int32 slot) { - const EnginePlugin *plugin = NULL; int16 savegameId; - EngineMan.findGame(_gameDescription->gameid, &plugin); if (slot == -1) { GUI::SaveLoadChooser *dialog = new GUI::SaveLoadChooser("Restore game:", "Restore", false); - savegameId = dialog->runModalWithPluginAndTarget(plugin, ConfMan.getActiveDomainName()); + savegameId = dialog->runModalWithCurrentTarget(); delete dialog; } else { savegameId = slot; -- cgit v1.2.3 From 5458127d9706d2c124d907edc5be06e58384d562 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Sun, 10 Jun 2012 16:00:26 +0100 Subject: TOON: Migrate Pathfinding API x,y coordinates to int16. This harmonises the usage with Common::Point. --- engines/toon/character.cpp | 4 ++- engines/toon/path.cpp | 66 ++++++++++++++++++++++------------------------ engines/toon/path.h | 26 +++++++++--------- engines/toon/toon.cpp | 2 +- 4 files changed, 49 insertions(+), 49 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/character.cpp b/engines/toon/character.cpp index 0e5189957b..3ac454983d 100644 --- a/engines/toon/character.cpp +++ b/engines/toon/character.cpp @@ -173,7 +173,9 @@ bool Character::walkTo(int32 newPosX, int32 newPosY) { _vm->getPathFinding()->addBlockingEllipse(_vm->getDrew()->getFinalX(), _vm->getDrew()->getFinalY(), sizeX, sizeY); } - _vm->getPathFinding()->findClosestWalkingPoint(newPosX, newPosY, &_finalX, &_finalY, _x, _y); + int16 tempFinalX, tempFinalY; + _vm->getPathFinding()->findClosestWalkingPoint(newPosX, newPosY, &tempFinalX, &tempFinalY, _x, _y); + _finalX = tempFinalX, _finalY = tempFinalY; // FIXME - Bodge to match types... if (_x == _finalX && _y == _finalY) return true; diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index dfdf095af9..5aae523455 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -60,7 +60,7 @@ void PathFindingHeap::clear() { memset(_data, 0, sizeof(HeapDataGrid) * _size); } -void PathFindingHeap::push(int16 x, int16 y, int32 weight) { +void PathFindingHeap::push(int16 x, int16 y, int16 weight) { debugC(2, kDebugPath, "push(%d, %d, %d)", x, y, weight); if (_count == _size) { @@ -104,7 +104,7 @@ void PathFindingHeap::push(int16 x, int16 y, int32 weight) { } } -void PathFindingHeap::pop(int16 *x, int16 *y, int32 *weight) { +void PathFindingHeap::pop(int16 *x, int16 *y, int16 *weight) { debugC(2, kDebugPath, "pop(x, y, weight)"); if (!_count) { @@ -173,14 +173,14 @@ void PathFinding::init(Picture *mask) { _sq = new int32[_width * _height]; } -bool PathFinding::isLikelyWalkable(int32 x, int32 y) { +bool PathFinding::isLikelyWalkable(int16 x, int16 y) { 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; } else { - int32 dx = abs(_blockingRects[i][0] - x); - int32 dy = abs(_blockingRects[i][1] - y); + int16 dx = abs(_blockingRects[i][0] - x); + int16 dy = abs(_blockingRects[i][1] - y); if ((dx << 8) / _blockingRects[i][2] < (1 << 8) && (dy << 8) / _blockingRects[i][3] < (1 << 8)) { return false; } @@ -189,13 +189,13 @@ bool PathFinding::isLikelyWalkable(int32 x, int32 y) { return true; } -bool PathFinding::isWalkable(int32 x, int32 y) { +bool PathFinding::isWalkable(int16 x, int16 y) { debugC(2, kDebugPath, "isWalkable(%d, %d)", x, y); return (_currentMask->getData(x, y) & 0x1f) > 0; } -bool PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 *fyy, int origX, int origY) { +bool PathFinding::findClosestWalkingPoint(int16 xx, int16 yy, int16 *fxx, int16 *fyy, int16 origX, int16 origY) { debugC(1, kDebugPath, "findClosestWalkingPoint(%d, %d, fxx, fyy, %d, %d)", xx, yy, origX, origY); int32 currentFound = -1; @@ -207,8 +207,8 @@ bool PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 if (origY == -1) origY = yy; - for (int y = 0; y < _height; y++) { - for (int x = 0; x < _width; x++) { + for (int16 y = 0; y < _height; y++) { + for (int16 x = 0; x < _width; x++) { if (isWalkable(x, y) && isLikelyWalkable(x, y)) { int32 ndist = (x - xx) * (x - xx) + (y - yy) * (y - yy); int32 ndist2 = (x - origX) * (x - origX) + (y - origY) * (y - origY); @@ -232,7 +232,7 @@ bool PathFinding::findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 } } -bool PathFinding::walkLine(int32 x, int32 y, int32 x2, int32 y2) { +bool PathFinding::walkLine(int16 x, int16 y, int16 x2, int16 y2) { uint32 bx = x << 16; int32 dx = x2 - x; uint32 by = y << 16; @@ -265,7 +265,7 @@ bool PathFinding::walkLine(int32 x, int32 y, int32 x2, int32 y2) { return true; } -bool PathFinding::lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2) { +bool PathFinding::lineIsWalkable(int16 x, int16 y, int16 x2, int16 y2) { uint32 bx = x << 16; int32 dx = x2 - x; uint32 by = y << 16; @@ -290,7 +290,7 @@ bool PathFinding::lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2) { return true; } -bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { +bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { debugC(1, kDebugPath, "findPath(%d, %d, %d, %d)", x, y, destx, desty); if (x == destx && y == desty) { @@ -313,9 +313,9 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { // no direct line, we use the standard A* algorithm memset(_sq , 0, _width * _height * sizeof(int32)); _heap->clear(); - int32 curX = x; - int32 curY = y; - int32 curWeight = 0; + int16 curX = x; + int16 curY = y; + int16 curWeight = 0; _sq[curX + curY *_width] = 1; _heap->push(curX, curY, abs(destx - x) + abs(desty - y)); @@ -323,19 +323,17 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { while (_heap->getCount()) { wei = 0; - int16 tempCurX, tempCurY; - _heap->pop(&tempCurX, &tempCurY, &curWeight); - curX = tempCurX, curY = tempCurY; // FIXME - Bodge to match heap->pop types + _heap->pop(&curX, &curY, &curWeight); int32 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); + int16 endX = MIN(curX + 1, _width - 1); + int16 endY = MIN(curY + 1, _height - 1); + int16 startX = MAX(curX - 1, 0); + int16 startY = MAX(curY - 1, 0); bool next = false; - for (int32 px = startX; px <= endX && !next; px++) { - for (int32 py = startY; py <= endY && !next; py++) { + for (int16 px = startX; px <= endX && !next; px++) { + for (int16 py = startY; py <= endY && !next; py++) { if (px != curX || py != curY) { wei = ((abs(px - curX) + abs(py - curY))); @@ -376,16 +374,16 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { bool retVal = false; while (true) { - int32 bestX = -1; - int32 bestY = -1; + int16 bestX = -1; + int16 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); + int16 endX = MIN(curX + 1, _width - 1); + int16 endY = MIN(curY + 1, _height - 1); + int16 startX = MAX(curX - 1, 0); + int16 startY = MAX(curY - 1, 0); - for (int32 px = startX; px <= endX; px++) { - for (int32 py = startY; py <= endY; py++) { + for (int16 px = startX; px <= endX; px++) { + for (int16 py = startY; py <= endY; py++) { if (px != curX || py != curY) { wei = abs(px - curX) + abs(py - curY); @@ -425,7 +423,7 @@ bool PathFinding::findPath(int32 x, int32 y, int32 destx, int32 desty) { return retVal; } -void PathFinding::addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2) { +void PathFinding::addBlockingRect(int16 x1, int16 y1, int16 x2, int16 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); @@ -440,7 +438,7 @@ void PathFinding::addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2) { _numBlockingRects++; } -void PathFinding::addBlockingEllipse(int32 x1, int32 y1, int32 w, int32 h) { +void PathFinding::addBlockingEllipse(int16 x1, int16 y1, int16 w, int16 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); diff --git a/engines/toon/path.h b/engines/toon/path.h index 8e2c61d482..f73415adc5 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -35,8 +35,8 @@ public: PathFindingHeap(); ~PathFindingHeap(); - void push(int16 x, int16 y, int32 weight); - void pop(int16 *x, int16 *y, int32 *weight); + void push(int16 x, int16 y, int16 weight); + void pop(int16 *x, int16 *y, int16 *weight); void init(int32 size); void clear(); void unload(); @@ -61,16 +61,16 @@ public: void init(Picture *mask); - bool findPath(int32 x, int32 y, int32 destX, int32 destY); - bool findClosestWalkingPoint(int32 xx, int32 yy, int32 *fxx, int32 *fyy, int origX = -1, int origY = -1); - bool isWalkable(int32 x, int32 y); - bool isLikelyWalkable(int32 x, int32 y); - bool lineIsWalkable(int32 x, int32 y, int32 x2, int32 y2); - bool walkLine(int32 x, int32 y, int32 x2, int32 y2); + bool findPath(int16 x, int16 y, int16 destX, int16 destY); + bool findClosestWalkingPoint(int16 xx, int16 yy, int16 *fxx, int16 *fyy, int16 origX = -1, int16 origY = -1); + bool isWalkable(int16 x, int16 y); + bool isLikelyWalkable(int16 x, int16 y); + bool lineIsWalkable(int16 x, int16 y, int16 x2, int16 y2); + bool walkLine(int16 x, int16 y, int16 x2, int16 y2); void resetBlockingRects() { _numBlockingRects = 0; } - void addBlockingRect(int32 x1, int32 y1, int32 x2, int32 y2); - void addBlockingEllipse(int32 x1, int32 y1, int32 w, int32 h); + void addBlockingRect(int16 x1, int16 y1, int16 x2, int16 y2); + void addBlockingEllipse(int16 x1, int16 y1, int16 w, int16 h); int32 getPathNodeCount() const { return _tempPath.size(); } int32 getPathNodeX(int32 nodeId) const { return _tempPath[ _tempPath.size() - nodeId - 1].x; } @@ -84,8 +84,8 @@ private: PathFindingHeap *_heap; int32 *_sq; - int32 _width; - int32 _height; + int16 _width; + int16 _height; struct i32Point { int32 x, y; @@ -93,7 +93,7 @@ private: Common::Array _tempPath; - int32 _blockingRects[kMaxBlockingRects][5]; + int16 _blockingRects[kMaxBlockingRects][5]; uint8 _numBlockingRects; }; diff --git a/engines/toon/toon.cpp b/engines/toon/toon.cpp index 0b39432b53..dd3c32b5bf 100644 --- a/engines/toon/toon.cpp +++ b/engines/toon/toon.cpp @@ -1488,7 +1488,7 @@ void ToonEngine::clickEvent() { } if (!currentHot) { - int32 xx, yy; + int16 xx, yy; if (_gameState->_inCutaway || _gameState->_inInventory || _gameState->_inCloseUp) return; -- cgit v1.2.3 From 13832580002a0a902a23fa893784aa61f7b3faaa Mon Sep 17 00:00:00 2001 From: D G Turner Date: Sun, 10 Jun 2012 20:45:37 +0100 Subject: TOON: Migrate Pathfinding Path Buffers to Common::Point. This removes the need for i32Point, which used int32, instead of the int16 of Common::Point. Since the co-ordinates passed in are in int16, this is safe. Tested with no regressions. Also, removed return value from walkLine function as it always returned true. --- engines/toon/path.cpp | 26 ++++++-------------------- engines/toon/path.h | 9 +++------ 2 files changed, 9 insertions(+), 26 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 5aae523455..63dbf1a442 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -232,7 +232,7 @@ bool PathFinding::findClosestWalkingPoint(int16 xx, int16 yy, int16 *fxx, int16 } } -bool PathFinding::walkLine(int16 x, int16 y, int16 x2, int16 y2) { +void PathFinding::walkLine(int16 x, int16 y, int16 x2, int16 y2) { uint32 bx = x << 16; int32 dx = x2 - x; uint32 by = y << 16; @@ -249,20 +249,13 @@ bool PathFinding::walkLine(int16 x, int16 y, int16 x2, int16 y2) { int32 cdy = (dy << 16) / t; _tempPath.clear(); - i32Point p; for (int32 i = t; i > 0; i--) { - p.x = bx >> 16; - p.y = by >> 16; - _tempPath.insert_at(0, p); + _tempPath.insert_at(0, Common::Point(bx >> 16, by >> 16)); bx += cdx; by += cdy; } - p.x = x2; - p.y = y2; - _tempPath.insert_at(0, p); - - return true; + _tempPath.insert_at(0, Common::Point(x2, y2)); } bool PathFinding::lineIsWalkable(int16 x, int16 y, int16 x2, int16 y2) { @@ -363,12 +356,8 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { curX = destx; curY = desty; - Common::Array retPath; - - i32Point p; - p.x = curX; - p.y = curY; - retPath.push_back(p); + Common::Array retPath; + retPath.push_back(Common::Point(curX, curY)); int32 bestscore = _sq[destx + desty * _width]; @@ -402,10 +391,7 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { if (bestX < 0 || bestY < 0) break; - i32Point pp; - pp.x = bestX; - pp.y = bestY; - retPath.push_back(pp); + retPath.push_back(Common::Point(bestX, bestY)); if ((bestX == x && bestY == y)) { _tempPath.clear(); diff --git a/engines/toon/path.h b/engines/toon/path.h index f73415adc5..2a583e5bff 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -24,6 +24,7 @@ #define TOON_PATH_H #include "common/array.h" +#include "common/rect.h" #include "toon/toon.h" @@ -66,7 +67,7 @@ public: bool isWalkable(int16 x, int16 y); bool isLikelyWalkable(int16 x, int16 y); bool lineIsWalkable(int16 x, int16 y, int16 x2, int16 y2); - bool walkLine(int16 x, int16 y, int16 x2, int16 y2); + void walkLine(int16 x, int16 y, int16 x2, int16 y2); void resetBlockingRects() { _numBlockingRects = 0; } void addBlockingRect(int16 x1, int16 y1, int16 x2, int16 y2); @@ -87,11 +88,7 @@ private: int16 _width; int16 _height; - struct i32Point { - int32 x, y; - }; - - Common::Array _tempPath; + Common::Array _tempPath; int16 _blockingRects[kMaxBlockingRects][5]; uint8 _numBlockingRects; -- cgit v1.2.3 From fcfff28c5f3cabf193d83846cfe9d90f0153d187 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Sun, 10 Jun 2012 21:12:01 +0100 Subject: TOON: Minor type fixes and cleanups in Pathfinding class. --- engines/toon/character.cpp | 2 +- engines/toon/path.cpp | 20 ++++++++++---------- engines/toon/path.h | 12 ++++++------ 3 files changed, 17 insertions(+), 17 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/character.cpp b/engines/toon/character.cpp index 3ac454983d..260296cc9f 100644 --- a/engines/toon/character.cpp +++ b/engines/toon/character.cpp @@ -186,7 +186,7 @@ bool Character::walkTo(int32 newPosX, int32 newPosY) { int32 smoothDx = 0; int32 smoothDy = 0; - for (int32 a = 0; a < _vm->getPathFinding()->getPathNodeCount(); a++) { + for (uint32 a = 0; a < _vm->getPathFinding()->getPathNodeCount(); a++) { _currentPathX[a] = _vm->getPathFinding()->getPathNodeX(a); _currentPathY[a] = _vm->getPathFinding()->getPathNodeY(a); } diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 63dbf1a442..735801eebc 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -65,7 +65,7 @@ void PathFindingHeap::push(int16 x, int16 y, int16 weight) { if (_count == _size) { // Increase size by 50% - int newSize = _size + (_size >> 1) + 1; + uint32 newSize = _size + (_size / 2) + 1; HeapDataGrid *newData; newData = (HeapDataGrid *)realloc(_data, sizeof(HeapDataGrid) * newSize); @@ -84,13 +84,13 @@ void PathFindingHeap::push(int16 x, int16 y, int16 weight) { _data[_count]._weight = weight; _count++; - int32 lMax = _count-1; - int32 lT = 0; + uint32 lMax = _count - 1; + uint32 lT = 0; while (true) { if (lMax <= 0) break; - lT = (lMax-1) / 2; + lT = (lMax - 1) / 2; if (_data[lT]._weight > _data[lMax]._weight) { HeapDataGrid temp; @@ -120,13 +120,13 @@ void PathFindingHeap::pop(int16 *x, int16 *y, int16 *weight) { if (!_count) return; - int32 lMin = 0; - int32 lT = 0; + uint32 lMin = 0; + uint32 lT = 0; while (true) { - lT = (lMin << 1) + 1; + lT = (lMin * 2) + 1; if (lT < _count) { - if (lT < _count-1) { + if (lT < _count - 1) { if (_data[lT + 1]._weight < _data[lT]._weight) lT++; } @@ -312,8 +312,8 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { _sq[curX + curY *_width] = 1; _heap->push(curX, curY, abs(destx - x) + abs(desty - y)); - int32 wei = 0; + int16 wei; while (_heap->getCount()) { wei = 0; _heap->pop(&curX, &curY, &curWeight); @@ -328,7 +328,7 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { for (int16 px = startX; px <= endX && !next; px++) { for (int16 py = startY; py <= endY && !next; py++) { if (px != curX || py != curY) { - wei = ((abs(px - curX) + abs(py - curY))); + wei = abs(px - curX) + abs(py - curY); int32 curPNode = px + py * _width; if (isWalkable(px, py)) { // walkable ? diff --git a/engines/toon/path.h b/engines/toon/path.h index 2a583e5bff..2476dc3b8a 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -41,7 +41,7 @@ public: void init(int32 size); void clear(); void unload(); - int32 getCount() { return _count; } + uint32 getCount() { return _count; } private: struct HeapDataGrid { @@ -51,8 +51,8 @@ private: HeapDataGrid *_data; - int32 _size; - int32 _count; + uint32 _size; + uint32 _count; }; class PathFinding { @@ -73,9 +73,9 @@ public: void addBlockingRect(int16 x1, int16 y1, int16 x2, int16 y2); void addBlockingEllipse(int16 x1, int16 y1, int16 w, int16 h); - 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; } + uint32 getPathNodeCount() const { return _tempPath.size(); } + int16 getPathNodeX(uint32 nodeId) const { return _tempPath[(_tempPath.size() - 1) - nodeId].x; } + int16 getPathNodeY(uint32 nodeId) const { return _tempPath[(_tempPath.size() - 1) - nodeId].y; } private: static const uint8 kMaxBlockingRects = 16; -- cgit v1.2.3 From 87eb651886ef911d8026d777abec72c88ffdf32f Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 14 Jun 2012 00:19:34 +0100 Subject: TOON: Migrate Character API x,y coordinates and subclasses to int16. This harmonises the usage with Common::Point. --- engines/toon/character.cpp | 34 ++++++++++++++++------------------ engines/toon/character.h | 28 ++++++++++++++-------------- engines/toon/drew.cpp | 2 +- engines/toon/drew.h | 2 +- engines/toon/flux.cpp | 4 ++-- engines/toon/flux.h | 2 +- 6 files changed, 35 insertions(+), 37 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/character.cpp b/engines/toon/character.cpp index 260296cc9f..2a28642f6a 100644 --- a/engines/toon/character.cpp +++ b/engines/toon/character.cpp @@ -81,7 +81,7 @@ Character::~Character(void) { void Character::init() { } -void Character::forceFacing( int32 facing ) { +void Character::forceFacing(int32 facing) { debugC(4, kDebugCharacter, "forceFacing(%d)", facing); _facing = facing; } @@ -136,8 +136,7 @@ void Character::setFacing(int32 facing) { _facing = facing; } -void Character::forcePosition(int32 x, int32 y) { - +void Character::forcePosition(int16 x, int16 y) { debugC(5, kDebugCharacter, "forcePosition(%d, %d)", x, y); setPosition(x, y); @@ -145,7 +144,7 @@ void Character::forcePosition(int32 x, int32 y) { _finalY = y; } -void Character::setPosition(int32 x, int32 y) { +void Character::setPosition(int16 x, int16 y) { debugC(5, kDebugCharacter, "setPosition(%d, %d)", x, y); _x = x; @@ -155,7 +154,7 @@ void Character::setPosition(int32 x, int32 y) { return; } -bool Character::walkTo(int32 newPosX, int32 newPosY) { +bool Character::walkTo(int16 newPosX, int16 newPosY) { debugC(1, kDebugCharacter, "walkTo(%d, %d)", newPosX, newPosY); if (!_visible) @@ -168,21 +167,19 @@ bool Character::walkTo(int32 newPosX, int32 newPosY) { // don't allow flux to go at the same position as drew if (_id == 1 ) { - int32 sizeX = MAX(5, 30 * _vm->getDrew()->getScale() / 1024); - int32 sizeY = MAX(2, 20 * _vm->getDrew()->getScale() / 1024); + int16 sizeX = MAX(5, 30 * _vm->getDrew()->getScale() / 1024); + int16 sizeY = MAX(2, 20 * _vm->getDrew()->getScale() / 1024); _vm->getPathFinding()->addBlockingEllipse(_vm->getDrew()->getFinalX(), _vm->getDrew()->getFinalY(), sizeX, sizeY); } - int16 tempFinalX, tempFinalY; - _vm->getPathFinding()->findClosestWalkingPoint(newPosX, newPosY, &tempFinalX, &tempFinalY, _x, _y); - _finalX = tempFinalX, _finalY = tempFinalY; // FIXME - Bodge to match types... + _vm->getPathFinding()->findClosestWalkingPoint(newPosX, newPosY, &_finalX, &_finalY, _x, _y); if (_x == _finalX && _y == _finalY) return true; if (_vm->getPathFinding()->findPath(_x, _y, _finalX, _finalY)) { - int32 localFinalX = _finalX; - int32 localFinalY = _finalY; + int16 localFinalX = _finalX; + int16 localFinalY = _finalY; int32 smoothDx = 0; int32 smoothDy = 0; @@ -266,10 +263,11 @@ int32 Character::getFlag() { return _flags; } -int32 Character::getX() { +int16 Character::getX() { return _x; } -int32 Character::getY() { + +int16 Character::getY() { return _y; } @@ -529,7 +527,7 @@ void Character::update(int32 timeIncrement) { } // adapted from Kyra -int32 Character::getFacingFromDirection(int32 dx, int32 dy) { +int32 Character::getFacingFromDirection(int16 dx, int16 dy) { debugC(4, kDebugCharacter, "getFacingFromDirection(%d, %d)", dx, dy); static const int facingTable[] = { @@ -640,7 +638,7 @@ void Character::load(Common::ReadStream *stream) { // "not visible" flag. if (_flags & 0x100) { _flags &= ~0x100; - setVisible(false); + setVisible(false); } } @@ -1080,11 +1078,11 @@ void Character::setDefaultSpecialAnimationId(int32 defaultAnimationId) { _animSpecialDefaultId = defaultAnimationId; } -int32 Character::getFinalX() { +int16 Character::getFinalX() { return _finalX; } -int32 Character::getFinalY() { +int16 Character::getFinalY() { return _finalY; } diff --git a/engines/toon/character.h b/engines/toon/character.h index d06a6c060c..d2d84a8c9b 100644 --- a/engines/toon/character.h +++ b/engines/toon/character.h @@ -65,13 +65,13 @@ public: virtual int32 getFlag(); virtual int32 getAnimFlag(); virtual void setAnimFlag(int32 flag); - virtual void setPosition(int32 x, int32 y); - virtual void forcePosition(int32 x, int32 y); - virtual int32 getX(); - virtual int32 getY(); - virtual int32 getFinalX(); - virtual int32 getFinalY(); - virtual bool walkTo(int32 newPosX, int32 newPosY); + virtual void setPosition(int16 x, int16 y); + virtual void forcePosition(int16 x, int16 y); + virtual int16 getX(); + virtual int16 getY(); + virtual int16 getFinalX(); + virtual int16 getFinalY(); + virtual bool walkTo(int16 newPosX, int16 newPosY); virtual bool getVisible(); virtual void setVisible(bool visible); virtual bool loadWalkAnimation(const Common::String &animName); @@ -99,7 +99,7 @@ public: virtual void resetScale() {} virtual void plotPath(Graphics::Surface& surface); - int32 getFacingFromDirection(int32 dx, int32 dy); + int32 getFacingFromDirection(int16 dx, int16 dy); static const SpecialCharacterAnimation *getSpecialAnimation(int32 characterId, int32 animationId); protected: @@ -112,11 +112,11 @@ protected: int32 _sceneAnimationId; int32 _lineToSayId; int32 _time; - int32 _x; - int32 _y; + int16 _x; + int16 _y; int32 _z; - int32 _finalX; - int32 _finalY; + int16 _finalX; + int16 _finalY; int32 _facing; int32 _flags; int32 _animFlags; @@ -137,8 +137,8 @@ protected: Animation *_shadowAnim; Animation *_specialAnim; - int32 _currentPathX[4096]; - int32 _currentPathY[4096]; + int16 _currentPathX[4096]; + int16 _currentPathY[4096]; int32 _currentPathNodeCount; int32 _currentPathNode; int32 _currentWalkStamp; diff --git a/engines/toon/drew.cpp b/engines/toon/drew.cpp index df5cfcfa03..dfd3f515fa 100644 --- a/engines/toon/drew.cpp +++ b/engines/toon/drew.cpp @@ -48,7 +48,7 @@ bool CharacterDrew::setupPalette() { return false; } -void CharacterDrew::setPosition(int32 x, int32 y) { +void CharacterDrew::setPosition(int16 x, int16 y) { debugC(5, kDebugCharacter, "setPosition(%d, %d)", x, y); _z = _vm->getLayerAtPoint(x, y); diff --git a/engines/toon/drew.h b/engines/toon/drew.h index 3357b99846..ff1b619125 100644 --- a/engines/toon/drew.h +++ b/engines/toon/drew.h @@ -35,7 +35,7 @@ public: virtual ~CharacterDrew(); bool setupPalette(); void playStandingAnim(); - void setPosition(int32 x, int32 y); + void setPosition(int16 x, int16 y); void resetScale(); void update(int32 timeIncrement); void playWalkAnim(int32 start, int32 end); diff --git a/engines/toon/flux.cpp b/engines/toon/flux.cpp index b752e65c82..70aa40fb36 100644 --- a/engines/toon/flux.cpp +++ b/engines/toon/flux.cpp @@ -45,7 +45,7 @@ void CharacterFlux::playStandingAnim() { _animationInstance->stopAnimation(); _animationInstance->setLooping(true); - //s/etVisible(true); + //setVisible(true); } void CharacterFlux::setVisible(bool visible) { @@ -99,7 +99,7 @@ int32 CharacterFlux::fixFacingForAnimation(int32 originalFacing, int32 animation return finalFacing; } -void CharacterFlux::setPosition(int32 x, int32 y) { +void CharacterFlux::setPosition(int16 x, int16 y) { debugC(5, kDebugCharacter, "setPosition(%d, %d)", x, y); _z = _vm->getLayerAtPoint(x, y); diff --git a/engines/toon/flux.h b/engines/toon/flux.h index c208bc5bda..1dc0d9c55f 100644 --- a/engines/toon/flux.h +++ b/engines/toon/flux.h @@ -34,7 +34,7 @@ public: CharacterFlux(ToonEngine *vm); virtual ~CharacterFlux(); - void setPosition(int32 x, int32 y); + void setPosition(int16 x, int16 y); void playStandingAnim(); void playWalkAnim(int32 start, int32 end); void update(int32 timeIncrement); -- cgit v1.2.3 From d2eab05e7d08bd5e4e615ee786e62be64293060a Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 14 Jun 2012 13:38:45 +0100 Subject: TOON: Replace Character _currentPath static buffers with Common::Array. --- engines/toon/character.cpp | 57 ++++++++++++++++++++++------------------------ engines/toon/character.h | 9 ++++---- 2 files changed, 32 insertions(+), 34 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/character.cpp b/engines/toon/character.cpp index 2a28642f6a..c9073de587 100644 --- a/engines/toon/character.cpp +++ b/engines/toon/character.cpp @@ -56,7 +56,6 @@ Character::Character(ToonEngine *vm) : _vm(vm) { _animScriptId = -1; _animSpecialId = -1; _animSpecialDefaultId = 0; - _currentPathNodeCount = 0; _currentPathNode = 0; _currentWalkStamp = 0; _visible = true; @@ -123,7 +122,7 @@ void Character::setFacing(int32 facing) { _lastWalkTime = _vm->getOldMilli(); } - if (_currentPathNode == 0) + if (_currentPathNode == 0) playStandingAnim(); else playWalkAnim(0, 0); @@ -166,7 +165,7 @@ bool Character::walkTo(int16 newPosX, int16 newPosY) { _vm->getPathFinding()->resetBlockingRects(); // don't allow flux to go at the same position as drew - if (_id == 1 ) { + if (_id == 1) { int16 sizeX = MAX(5, 30 * _vm->getDrew()->getScale() / 1024); int16 sizeY = MAX(2, 20 * _vm->getDrew()->getScale() / 1024); _vm->getPathFinding()->addBlockingEllipse(_vm->getDrew()->getFinalX(), _vm->getDrew()->getFinalY(), sizeX, sizeY); @@ -183,11 +182,9 @@ bool Character::walkTo(int16 newPosX, int16 newPosY) { int32 smoothDx = 0; int32 smoothDy = 0; - for (uint32 a = 0; a < _vm->getPathFinding()->getPathNodeCount(); a++) { - _currentPathX[a] = _vm->getPathFinding()->getPathNodeX(a); - _currentPathY[a] = _vm->getPathFinding()->getPathNodeY(a); - } - _currentPathNodeCount = _vm->getPathFinding()->getPathNodeCount(); + _currentPath.clear(); + for (uint32 a = 0; a < _vm->getPathFinding()->getPathNodeCount(); a++) + _currentPath.push_back(Common::Point(_vm->getPathFinding()->getPathNodeX(a), _vm->getPathFinding()->getPathNodeY(a))); _currentPathNode = 0; stopSpecialAnim(); @@ -202,12 +199,12 @@ bool Character::walkTo(int16 newPosX, int16 newPosY) { int32 localWalkStamp = _currentWalkStamp; if (_blockingWalk) { - while ((_x != newPosX || _y != newPosY) && _currentPathNode < _currentPathNodeCount && !_vm->shouldQuitGame()) { - if (_currentPathNode < _currentPathNodeCount - 4) { - int32 delta = MIN(4, _currentPathNodeCount - _currentPathNode); + while ((_x != newPosX || _y != newPosY) && _currentPathNode < _currentPath.size() && !_vm->shouldQuitGame()) { + if (_currentPathNode < _currentPath.size() - 4) { + int32 delta = MIN(4, _currentPath.size() - _currentPathNode); - int32 dx = _currentPathX[_currentPathNode+delta] - _x; - int32 dy = _currentPathY[_currentPathNode+delta] - _y; + int16 dx = _currentPath[_currentPathNode+delta].x - _x; + int16 dy = _currentPath[_currentPathNode+delta].y - _y; // smooth the facing computation. It prevents some ugly flickering from happening if (!smoothDx && !smoothDy) { @@ -226,9 +223,9 @@ bool Character::walkTo(int16 newPosX, int16 newPosY) { _numPixelToWalk += _speed * (_vm->getSystem()->getMillis() - _lastWalkTime) * _scale / 1024; _lastWalkTime = _vm->getSystem()->getMillis(); - while (_numPixelToWalk >= 1000 && _currentPathNode < _currentPathNodeCount) { - _x = _currentPathX[_currentPathNode]; - _y = _currentPathY[_currentPathNode]; + while (_numPixelToWalk >= 1000 && _currentPathNode < _currentPath.size()) { + _x = _currentPath[_currentPathNode].x; + _y = _currentPath[_currentPathNode].y; _currentPathNode += 1; _numPixelToWalk -= 1000; } @@ -244,7 +241,7 @@ bool Character::walkTo(int16 newPosX, int16 newPosY) { playStandingAnim(); _flags &= ~0x1; _currentPathNode = 0; - _currentPathNodeCount = 0; + _currentPath.clear(); if (_x != localFinalX || _y != localFinalY) { return false; @@ -348,12 +345,12 @@ void Character::stopSpecialAnim() { void Character::update(int32 timeIncrement) { debugC(5, kDebugCharacter, "update(%d)", timeIncrement); - if ((_flags & 0x1) && _currentPathNodeCount > 0) { - if (_currentPathNode < _currentPathNodeCount) { - if (_currentPathNode < _currentPathNodeCount - 10) { - int32 delta = MIN(10, _currentPathNodeCount - _currentPathNode); - int32 dx = _currentPathX[_currentPathNode+delta] - _x; - int32 dy = _currentPathY[_currentPathNode+delta] - _y; + if ((_flags & 0x1) && _currentPath.size() > 0) { + if (_currentPathNode < _currentPath.size()) { + if (_currentPathNode < _currentPath.size() - 10) { + int32 delta = MIN(10, _currentPath.size() - _currentPathNode); + int16 dx = _currentPath[_currentPathNode+delta].x - _x; + int16 dy = _currentPath[_currentPathNode+delta].y - _y; setFacing(getFacingFromDirection(dx, dy)); playWalkAnim(0, 0); } @@ -362,9 +359,9 @@ void Character::update(int32 timeIncrement) { _numPixelToWalk += _speed * (_vm->getSystem()->getMillis() - _lastWalkTime) * _scale / 1024; _lastWalkTime = _vm->getSystem()->getMillis(); - while (_numPixelToWalk > 1000 && _currentPathNode < _currentPathNodeCount) { - _x = _currentPathX[_currentPathNode]; - _y = _currentPathY[_currentPathNode]; + while (_numPixelToWalk > 1000 && _currentPathNode < _currentPath.size()) { + _x = _currentPath[_currentPathNode].x; + _y = _currentPath[_currentPathNode].y; _currentPathNode += 1; _numPixelToWalk -= 1000; } @@ -372,7 +369,7 @@ void Character::update(int32 timeIncrement) { } else { playStandingAnim(); _flags &= ~0x1; - _currentPathNodeCount = 0; + _currentPath.clear(); } } @@ -664,7 +661,7 @@ void Character::stopWalk() { _finalY = _y; _flags &= ~0x1; _currentPathNode = 0; - _currentPathNodeCount = 0; + _currentPath.clear(); } const SpecialCharacterAnimation *Character::getSpecialAnimation(int32 characterId, int32 animationId) { @@ -996,8 +993,8 @@ bool Character::loadShadowAnimation(const Common::String &animName) { } void Character::plotPath(Graphics::Surface& surface) { - for (int i = 0; i < _currentPathNodeCount; i++) { - *(byte *)surface.getBasePtr(_currentPathX[i], _currentPathY[i]) = ( i < _currentPathNode); + for (uint32 i = 0; i < _currentPath.size(); i++) { + *(byte *)surface.getBasePtr(_currentPath[i].x, _currentPath[i].y) = (i < _currentPathNode); } } diff --git a/engines/toon/character.h b/engines/toon/character.h index d2d84a8c9b..d33c314bf7 100644 --- a/engines/toon/character.h +++ b/engines/toon/character.h @@ -23,6 +23,9 @@ #ifndef TOON_CHARACTER_H #define TOON_CHARACTER_H +#include "common/array.h" +#include "common/rect.h" + #include "toon/toon.h" namespace Toon { @@ -137,10 +140,8 @@ protected: Animation *_shadowAnim; Animation *_specialAnim; - int16 _currentPathX[4096]; - int16 _currentPathY[4096]; - int32 _currentPathNodeCount; - int32 _currentPathNode; + Common::Array _currentPath; + uint32 _currentPathNode; int32 _currentWalkStamp; }; -- cgit v1.2.3 From 4aa0ec7fc4eae0941efb15affe664544c33822ea Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 14 Jun 2012 17:23:01 +0100 Subject: TOON: Change Pathfinding weight buffers to uint16. This should result in a significant saving in RAM usage. Have added warning outputs if the weights exceed the maximum limit. --- engines/toon/path.cpp | 34 +++++++++++++++++++--------------- engines/toon/path.h | 8 ++++---- 2 files changed, 23 insertions(+), 19 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/path.cpp b/engines/toon/path.cpp index 735801eebc..7914aed595 100644 --- a/engines/toon/path.cpp +++ b/engines/toon/path.cpp @@ -60,7 +60,7 @@ void PathFindingHeap::clear() { memset(_data, 0, sizeof(HeapDataGrid) * _size); } -void PathFindingHeap::push(int16 x, int16 y, int16 weight) { +void PathFindingHeap::push(int16 x, int16 y, uint16 weight) { debugC(2, kDebugPath, "push(%d, %d, %d)", x, y, weight); if (_count == _size) { @@ -104,7 +104,7 @@ void PathFindingHeap::push(int16 x, int16 y, int16 weight) { } } -void PathFindingHeap::pop(int16 *x, int16 *y, int16 *weight) { +void PathFindingHeap::pop(int16 *x, int16 *y, uint16 *weight) { debugC(2, kDebugPath, "pop(x, y, weight)"); if (!_count) { @@ -170,7 +170,7 @@ void PathFinding::init(Picture *mask) { _heap->unload(); _heap->init(500); delete[] _sq; - _sq = new int32[_width * _height]; + _sq = new uint16[_width * _height]; } bool PathFinding::isLikelyWalkable(int16 x, int16 y) { @@ -304,18 +304,16 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { } // no direct line, we use the standard A* algorithm - memset(_sq , 0, _width * _height * sizeof(int32)); + memset(_sq , 0, _width * _height * sizeof(uint16)); _heap->clear(); int16 curX = x; int16 curY = y; - int16 curWeight = 0; + uint16 curWeight = 0; _sq[curX + curY *_width] = 1; _heap->push(curX, curY, abs(destx - x) + abs(desty - y)); - int16 wei; while (_heap->getCount()) { - wei = 0; _heap->pop(&curX, &curY, &curWeight); int32 curNode = curX + curY * _width; @@ -328,15 +326,23 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { for (int16 px = startX; px <= endX && !next; px++) { for (int16 py = startY; py <= endY && !next; py++) { if (px != curX || py != curY) { - wei = abs(px - curX) + abs(py - curY); + uint16 wei = abs(px - curX) + abs(py - curY); - int32 curPNode = px + py * _width; if (isWalkable(px, py)) { // walkable ? - int32 sum = _sq[curNode] + wei * (1 + (isLikelyWalkable(px, py) ? 5 : 0)); + int32 curPNode = px + py * _width; + uint32 sum = _sq[curNode] + wei * (1 + (isLikelyWalkable(px, py) ? 5 : 0)); + if (sum > (uint32)0xFFFF) { + warning("PathFinding::findPath sum exceeds maximum representable!"); + sum = (uint32)0xFFFF; + } if (_sq[curPNode] > sum || !_sq[curPNode]) { - int32 newWeight = abs(destx - px) + abs(desty - py); _sq[curPNode] = sum; - _heap->push(px, py, _sq[curPNode] + newWeight); + uint32 newWeight = _sq[curPNode] + abs(destx - px) + abs(desty - py); + if (newWeight > (uint32)0xFFFF) { + warning("PathFinding::findPath newWeight exceeds maximum representable!"); + newWeight = (uint16)0xFFFF; + } + _heap->push(px, py, newWeight); if (!newWeight) next = true; // we found it ! } @@ -359,7 +365,7 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { Common::Array retPath; retPath.push_back(Common::Point(curX, curY)); - int32 bestscore = _sq[destx + desty * _width]; + uint16 bestscore = _sq[destx + desty * _width]; bool retVal = false; while (true) { @@ -374,8 +380,6 @@ bool PathFinding::findPath(int16 x, int16 y, int16 destx, int16 desty) { for (int16 px = startX; px <= endX; px++) { for (int16 py = startY; py <= endY; py++) { if (px != curX || py != curY) { - wei = abs(px - curX) + abs(py - curY); - int32 PNode = px + py * _width; if (_sq[PNode] && (isWalkable(px, py))) { if (_sq[PNode] < bestscore) { diff --git a/engines/toon/path.h b/engines/toon/path.h index 2476dc3b8a..59f74ef286 100644 --- a/engines/toon/path.h +++ b/engines/toon/path.h @@ -36,8 +36,8 @@ public: PathFindingHeap(); ~PathFindingHeap(); - void push(int16 x, int16 y, int16 weight); - void pop(int16 *x, int16 *y, int16 *weight); + void push(int16 x, int16 y, uint16 weight); + void pop(int16 *x, int16 *y, uint16 *weight); void init(int32 size); void clear(); void unload(); @@ -46,7 +46,7 @@ public: private: struct HeapDataGrid { int16 _x, _y; - int16 _weight; + uint16 _weight; }; HeapDataGrid *_data; @@ -84,7 +84,7 @@ private: PathFindingHeap *_heap; - int32 *_sq; + uint16 *_sq; int16 _width; int16 _height; -- cgit v1.2.3 From acaf8f762382f1aa886d4a3f5b0b8b662863f01c Mon Sep 17 00:00:00 2001 From: D G Turner Date: Thu, 14 Jun 2012 21:02:44 +0100 Subject: TOON: Fix two latent off-by-one errors in Character Class. These were exposed by assertions from Common::Array usage. --- engines/toon/character.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/character.cpp b/engines/toon/character.cpp index c9073de587..09730626f2 100644 --- a/engines/toon/character.cpp +++ b/engines/toon/character.cpp @@ -201,7 +201,7 @@ bool Character::walkTo(int16 newPosX, int16 newPosY) { if (_blockingWalk) { while ((_x != newPosX || _y != newPosY) && _currentPathNode < _currentPath.size() && !_vm->shouldQuitGame()) { if (_currentPathNode < _currentPath.size() - 4) { - int32 delta = MIN(4, _currentPath.size() - _currentPathNode); + int32 delta = MIN(4, _currentPath.size() - 1 - _currentPathNode); int16 dx = _currentPath[_currentPathNode+delta].x - _x; int16 dy = _currentPath[_currentPathNode+delta].y - _y; @@ -348,7 +348,7 @@ void Character::update(int32 timeIncrement) { if ((_flags & 0x1) && _currentPath.size() > 0) { if (_currentPathNode < _currentPath.size()) { if (_currentPathNode < _currentPath.size() - 10) { - int32 delta = MIN(10, _currentPath.size() - _currentPathNode); + int32 delta = MIN(10, _currentPath.size() - 1 - _currentPathNode); int16 dx = _currentPath[_currentPathNode+delta].x - _x; int16 dy = _currentPath[_currentPathNode+delta].y - _y; setFacing(getFacingFromDirection(dx, dy)); -- cgit v1.2.3 From c668431522d834b24c00baf964d2b6b3f3a2bbb8 Mon Sep 17 00:00:00 2001 From: D G Turner Date: Fri, 15 Jun 2012 12:11:01 +0100 Subject: TOON: Simplify code in Animation::drawFrameWithMaskAndScale(). --- engines/toon/anim.cpp | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-) (limited to 'engines/toon') diff --git a/engines/toon/anim.cpp b/engines/toon/anim.cpp index 98c667c303..84f6fa375c 100644 --- a/engines/toon/anim.cpp +++ b/engines/toon/anim.cpp @@ -229,37 +229,26 @@ void Animation::drawFrameWithMaskAndScale(Graphics::Surface &surface, int32 fram uint8 *curRow = (uint8 *)surface.pixels; uint8 *curRowMask = mask->getDataPtr(); - if (strstr(_name, "SHADOW")) { - for (int y = yy1; y < yy2; y++) { - for (int x = xx1; x < xx2; x++) { - if (x < 0 || x >= 1280 || y < 0 || y >= 400) - continue; - - uint8 *cur = curRow + x + y * destPitch; - uint8 *curMask = curRowMask + x + y * destPitchMask; - - // find the good c - int32 xs = (x - xx1) * 1024 / scale; - int32 ys = (y - yy1) * 1024 / scale; - uint8 *cc = &c[ys * w + xs]; - if (*cc && ((*curMask) >= zz)) + bool shadowFlag = false; + if (strstr(_name, "SHADOW")) + shadowFlag = true; + + for (int32 y = yy1; y < yy2; y++) { + for (int32 x = xx1; x < xx2; x++) { + if (x < 0 || x >= 1280 || y < 0 || y >= 400) + continue; + + uint8 *cur = curRow + x + y * destPitch; + uint8 *curMask = curRowMask + x + y * destPitchMask; + + // find the good c + int32 xs = (x - xx1) * 1024 / scale; + int32 ys = (y - yy1) * 1024 / scale; + uint8 *cc = &c[ys * w + xs]; + if (*cc && ((*curMask) >= zz)) { + if (shadowFlag) *cur = _vm->getShadowLUT()[*cur]; - } - } - } else { - for (int y = yy1; y < yy2; y++) { - for (int x = xx1; x < xx2; x++) { - if (x < 0 || x >= 1280 || y < 0 || y >= 400) - continue; - - uint8 *cur = curRow + x + y * destPitch; - uint8 *curMask = curRowMask + x + y * destPitchMask; - - // find the good c - int32 xs = (x - xx1) * 1024 / scale; - int32 ys = (y - yy1) * 1024 / scale; - uint8 *cc = &c[ys * w + xs]; - if (*cc && ((*curMask) >= zz)) + else *cur = *cc; } } -- cgit v1.2.3