summaryrefslogtreecommitdiff
path: root/src/game-server/object.cpp
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-02-01 20:12:27 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-02-01 20:12:27 +0000
commit20557328544d2b81b9bb0a4bfa1bae846ac960ab (patch)
tree18b50be88d2a469b25f41842b3397c4845496f7c /src/game-server/object.cpp
parent7d55d241bd5828f673a89dd49c5470aabcf52abd (diff)
downloadmanaserv-20557328544d2b81b9bb0a4bfa1bae846ac960ab.tar.gz
manaserv-20557328544d2b81b9bb0a4bfa1bae846ac960ab.tar.bz2
manaserv-20557328544d2b81b9bb0a4bfa1bae846ac960ab.tar.xz
manaserv-20557328544d2b81b9bb0a4bfa1bae846ac960ab.zip
Added caching of paths (patch by Rogier Polak).
Diffstat (limited to 'src/game-server/object.cpp')
-rw-r--r--src/game-server/object.cpp40
1 files changed, 32 insertions, 8 deletions
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;