diff options
author | Roderic Morris <roderic@ccs.neu.edu> | 2009-07-01 20:28:34 -0400 |
---|---|---|
committer | Roderic Morris <roderic@ccs.neu.edu> | 2009-07-01 20:41:47 -0400 |
commit | 2ea700fc0388d843795b71a9d259876ce975926f (patch) | |
tree | a64bdc3163f0ffb1663e34c3dae8de0c34953220 /src | |
parent | 6b794e76f8f046dca632299e443129c4f5ca3382 (diff) | |
download | manaserv-2ea700fc0388d843795b71a9d259876ce975926f.tar.gz manaserv-2ea700fc0388d843795b71a9d259876ce975926f.tar.bz2 manaserv-2ea700fc0388d843795b71a9d259876ce975926f.tar.xz manaserv-2ea700fc0388d843795b71a9d259876ce975926f.zip |
Remove complex path finding for players. Add a simple path finding algorithm to map.
Diffstat (limited to 'src')
-rw-r--r-- | src/game-server/being.cpp | 11 | ||||
-rw-r--r-- | src/game-server/being.hpp | 9 | ||||
-rw-r--r-- | src/game-server/character.cpp | 9 | ||||
-rw-r--r-- | src/game-server/character.hpp | 5 | ||||
-rw-r--r-- | src/game-server/map.cpp | 52 | ||||
-rw-r--r-- | src/game-server/map.hpp | 7 |
6 files changed, 88 insertions, 5 deletions
diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp index 069da14b..79b2e2fd 100644 --- a/src/game-server/being.cpp +++ b/src/game-server/being.cpp @@ -125,6 +125,15 @@ void Being::setDestination(const Point &dst) mPath.clear(); } +std::list<PATH_NODE> Being::findPath() +{ + mOld = getPosition(); + int startX = mOld.x / 32, startY = mOld.y / 32; + int destX = mDst.x / 32, destY = mDst.y / 32; + Map *map = getMap()->getMap(); + return map->findPath(startX, startY, destX, destY, getWalkMask()); +} + void Being::move() { mOld = getPosition(); @@ -169,7 +178,7 @@ void Being::move() { // No path exists: the walkability of cached path has changed, the // destination has changed, or a path was never set. - mPath = map->findPath(tileSX, tileSY, tileDX, tileDY, getWalkMask()); + mPath = findPath(); } if (mPath.empty()) diff --git a/src/game-server/being.hpp b/src/game-server/being.hpp index 96af1092..89134c7c 100644 --- a/src/game-server/being.hpp +++ b/src/game-server/being.hpp @@ -230,6 +230,11 @@ class Being : public Actor void move(); /** + * Returns the path to the being's current destination. + */ + virtual std::list<PATH_NODE> findPath(); + + /** * Sets an attribute. */ void setAttribute(int n, int value) @@ -295,14 +300,14 @@ class Being : public Actor Action mAction; std::vector< Attribute > mAttributes; Being *mTarget; + Point mOld; /**< Old coordinates. */ + Point mDst; /**< Target coordinates. */ private: Being(const Being &rhs); Being &operator=(const Being &rhs); std::list<PATH_NODE> mPath; - Point mOld; /**< Old coordinates. */ - Point mDst; /**< Target coordinates. */ unsigned short mSpeed; /**< Speed. */ unsigned char mDirection; /**< Facing direction. */ diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index f2b746e3..0488e4ec 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -114,6 +114,15 @@ void Character::update() Being::update(); } +std::list<PATH_NODE> Character::findPath() +{ + mOld = getPosition(); + int startX = mOld.x / 32, startY = mOld.y / 32; + int destX = mDst.x / 32, destY = mDst.y / 32; + Map *map = getMap()->getMap(); + return map->findSimplePath(startX, startY, destX, destY, getWalkMask()); +} + void Character::perform() { if (mAction != ATTACK || mTarget == NULL) return; diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp index f930c33b..a7b8dd5a 100644 --- a/src/game-server/character.hpp +++ b/src/game-server/character.hpp @@ -77,6 +77,11 @@ class Character : public Being void respawn(); /** + * Returns the path to the character's current destination. + */ + std::list<PATH_NODE> findPath(); + + /** * makes the character perform a special action * when it is allowed to do so */ diff --git a/src/game-server/map.cpp b/src/game-server/map.cpp index 3821b0a8..87321141 100644 --- a/src/game-server/map.cpp +++ b/src/game-server/map.cpp @@ -167,8 +167,56 @@ MetaTile *Map::getMetaTile(int x, int y) static int const basicCost = 100; -std::list<PATH_NODE> -Map::findPath(int startX, int startY, int destX, int destY, unsigned char walkmask, int maxCost) + +std::list<PATH_NODE> Map::findSimplePath(int startX, int startY, + int destX, int destY, + unsigned char walkmask) +{ + // Path to be built up (empty by default) + std::list<PATH_NODE> path; + int positionX = startX, positionY = startY; + int directionX, directionY; + // Checks our path up to 1 tiles, if a blocking tile is found + // We go to the last good tile, and break out of the loop + while(true) + { + // need to find a way to just get negative or positive 1 or 0 + // assume this is right for now. + directionX = destX - positionX; + directionY = destY - positionY; + + if (directionX > 0) + directionX = 1; + else if(directionX < 0) + directionX = -1; + + if (directionY > 0) + directionY = 1; + else if(directionY < 0) + directionY = -1; + + positionX += directionX; + positionY += directionY; + + if (getWalk(positionX, positionY, walkmask)) + { + path.push_back(PATH_NODE(positionX, positionY)); + + if ((positionX == destX) && (positionY == destY)) + { + return path; + } + } + else + { + return path; + } + } +} + +std::list<PATH_NODE> Map::findPath(int startX, int startY, + int destX, int destY, + unsigned char walkmask, int maxCost) { // Path to be built up (empty by default) std::list<PATH_NODE> path; diff --git a/src/game-server/map.hpp b/src/game-server/map.hpp index 059d9826..90d8ff33 100644 --- a/src/game-server/map.hpp +++ b/src/game-server/map.hpp @@ -173,6 +173,13 @@ class Map unsigned char walkmask, int maxCost = 20); + /** + * Finds a simple path from location to the next. + */ + std::list<PATH_NODE> findSimplePath(int startX, int startY, + int destX, int destY, + unsigned char walkmask); + private: /** * Blockmasks for different entities |