diff options
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | src/game-server/object.cpp | 40 | ||||
-rw-r--r-- | src/game-server/object.hpp | 4 |
3 files changed, 37 insertions, 9 deletions
@@ -6,6 +6,8 @@ Added graceful server shutdown, chose some more sane variables names for SQL queries and fixed a possible character collision error (patch applied by Bjørn Lindeijer). + * src/game-server/object.cpp, src/game-server/object.hpp: Added + caching of paths (patch applied by Bjørn Lindeijer). 2007-01-14 Björn Steinbrink <B.Steinbrink@gmx.de> diff --git a/src/game-server/object.cpp b/src/game-server/object.cpp index 9921182d..a6939e86 100644 --- a/src/game-server/object.cpp +++ b/src/game-server/object.cpp @@ -29,7 +29,7 @@ void MovingObject::move() mOld = getPosition(); if (mActionTime > 100) { - // current move has not yet ended + // Current move has not yet ended mActionTime -= 100; return; } @@ -38,16 +38,40 @@ void MovingObject::move() int tileDX = mDst.x / 32, tileDY = mDst.y / 32; if (tileSX == tileDX && tileSY == tileDY) { - // moving while staying on the same tile is free + // Moving while staying on the same tile is free setPosition(mDst); mActionTime = 0; return; } Map *map = mapManager->getMap(getMapId()); - // TODO: cache pathfinding results - std::list<PATH_NODE> path = map->findPath(tileSX, tileSY, tileDX, tileDY); - if (path.empty()) + + /* If no path exists, the for-loop won't be entered. Else a path for the + * current destination has already been calculated. + * The tiles in this path have to be checked for walkability, + * in case there have been changes. The 'getWalk' method of the Map + * class has been used, because that seems to be the most logical + * place extra functionality will be added. + */ + for (std::list<PATH_NODE>::iterator pathIterator = mPath.begin(); + pathIterator != mPath.end(); pathIterator++) + { + if (!map->getWalk(pathIterator->x, pathIterator->y)) + { + mPath.clear(); + break; + } + } + + if (mPath.empty()) + { + // No path exists: the walkability of cached path has changed, the + // destination has changed, or a path was never set. + std::list<PATH_NODE> mPath = map->findPath(tileSX, tileSY, + tileDX, tileDY); + } + + if (mPath.empty()) { // no path was found mDst = mOld; @@ -59,11 +83,11 @@ void MovingObject::move() Point pos; do { - PATH_NODE next = path.front(); - path.pop_front(); + PATH_NODE next = mPath.front(); + mPath.pop_front(); mActionTime += (prev.x != next.x && prev.y != next.y) ? mSpeed * 362 / 256 : mSpeed; - if (path.empty()) + if (mPath.empty()) { // skip last tile center pos = mDst; diff --git a/src/game-server/object.hpp b/src/game-server/object.hpp index 498680e4..47d9a7d0 100644 --- a/src/game-server/object.hpp +++ b/src/game-server/object.hpp @@ -27,6 +27,7 @@ #include <vector> #include "point.h" +#include "game-server/map.hpp" // Object type enumeration enum @@ -197,7 +198,7 @@ class MovingObject: public Object * Sets the destination coordinates of the object. */ void setDestination(Point dst) - { mDst = dst; raiseUpdateFlags(NEW_DESTINATION); } + { mDst = dst; raiseUpdateFlags(NEW_DESTINATION); mPath.clear(); } /** * Gets the old coordinates of the object. @@ -249,6 +250,7 @@ class MovingObject: public Object Point mDst; /**< target coordinates */ Point mOld; /**< old coordinates */ unsigned short mSpeed; /**< speed */ + std::list<PATH_NODE> mPath; protected: unsigned char mDirection; /**< Facing direction */ |