summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/being.cpp20
-rw-r--r--src/being.h8
-rw-r--r--src/map.cpp44
-rw-r--r--src/map.h7
-rw-r--r--src/net/tmwserv/beinghandler.cpp5
-rw-r--r--src/player.cpp14
-rw-r--r--src/player.h4
7 files changed, 92 insertions, 10 deletions
diff --git a/src/being.cpp b/src/being.cpp
index 8d05c81b..ea587afb 100644
--- a/src/being.cpp
+++ b/src/being.cpp
@@ -159,18 +159,26 @@ void Being::setDestination(int dstX, int dstY)
setDestination(mPos.x, mPos.y, dstX, dstY);
}
+Path Being::findPath()
+{
+ Path path;
+
+ if (mMap)
+ {
+ path = mMap->findPath(mPos.x / 32, mPos.y / 32,
+ mDest.x / 32, mDest.y / 32, getWalkMask());
+ }
+
+ return path;
+}
+
void Being::setDestination(int srcX, int srcY, int dstX, int dstY)
{
mDest.x = dstX;
mDest.y = dstY;
- Path thisPath;
+ Path thisPath = findPath();
- if (mMap)
- {
- thisPath = mMap->findPath((int) srcX / 32, (int) srcY / 32,
- dstX / 32, dstY / 32, getWalkMask());
- }
if (thisPath.empty())
{
setPath(Path());
diff --git a/src/being.h b/src/being.h
index 6eef83c6..9213a2dc 100644
--- a/src/being.h
+++ b/src/being.h
@@ -184,12 +184,17 @@ class Being : public Sprite, public ConfigListener
virtual void setDestination(Uint16 destX, Uint16 destY);
#else
/**
+ * Returns the path to the being's current destination
+ */
+ virtual Path findPath();
+
+ /**
* Creates a path for the being from sx,sy to ex,ey
*/
void setDestination(int sx, int sy, int ex, int ey);
/**
- * Creates a path for the being from currect position to ex and ey
+ * Creates a path for the being from current position to ex and ey
*/
void setDestination(int ex, int ey);
@@ -410,7 +415,6 @@ class Being : public Sprite, public ConfigListener
*/
const Vector &getPosition() const { return mPos; }
-
/**
* Returns the horizontal size of the current base sprite of the being.
*/
diff --git a/src/map.cpp b/src/map.cpp
index 4e12ac97..61fcdfe8 100644
--- a/src/map.cpp
+++ b/src/map.cpp
@@ -490,6 +490,50 @@ const std::string &Map::getName() const
return getProperty("mapname");
}
+Path Map::findSimplePath(int startX, int startY,
+ int destX, int destY,
+ unsigned char walkmask)
+{
+ // Path to be built up (empty by default)
+ Path 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)
+ {
+ 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(Position(positionX, positionY));
+
+ if ((positionX == destX) && (positionY == destY))
+ {
+ return path;
+ }
+ }
+ else
+ {
+ return path;
+ }
+ }
+}
+
static int const basicCost = 100;
Path Map::findPath(int startX, int startY, int destX, int destY,
diff --git a/src/map.h b/src/map.h
index 6baf7411..1cf3c922 100644
--- a/src/map.h
+++ b/src/map.h
@@ -258,6 +258,13 @@ class Map : public Properties
const std::string &getName() const;
/**
+ * Find a simple path from one location to the next.
+ */
+ Path findSimplePath(int startX, int startY,
+ int destX, int destY,
+ unsigned char walkmask);
+
+ /**
* Find a path from one location to the next.
*/
Path findPath(int startX, int startY, int destX, int destY,
diff --git a/src/net/tmwserv/beinghandler.cpp b/src/net/tmwserv/beinghandler.cpp
index 99491203..7076ff8e 100644
--- a/src/net/tmwserv/beinghandler.cpp
+++ b/src/net/tmwserv/beinghandler.cpp
@@ -231,13 +231,14 @@ void BeingHandler::handleBeingsMoveMessage(MessageIn &msg)
// If being is a player, and he only moves a little, its ok to be a little out of sync
if (being->getType() == Being::PLAYER && abs(being->getPixelX() - dx) +
abs(being->getPixelY() - dy) < 16 &&
- (dx != being->getDestination().x && dy != being->getDestination().y))
+ (dx != being->getDestination().x &&
+ dy != being->getDestination().y))
{
being->setDestination(being->getPixelX(),being->getPixelY());
continue;
}
if (abs(being->getPixelX() - sx) +
- abs(being->getPixelY() - sy) > 10 * 32)
+ abs(being->getPixelY() - sy) > 10 * 32)
{
// Too large a desynchronization.
being->setPosition(sx, sy);
diff --git a/src/player.cpp b/src/player.cpp
index 520342e3..597fb14b 100644
--- a/src/player.cpp
+++ b/src/player.cpp
@@ -149,6 +149,20 @@ void Player::logic()
}
#endif
+Path Player::findPath()
+{
+ Path path;
+
+ if (mMap)
+ {
+ path = mMap->findSimplePath(getPosition().x / 32, getPosition().y / 32,
+ getDestination().x / 32, getDestination().y / 32,
+ getWalkMask());
+ }
+
+ return path;
+}
+
Being::Type Player::getType() const
{
return PLAYER;
diff --git a/src/player.h b/src/player.h
index 9a5c6c94..83cb4ec0 100644
--- a/src/player.h
+++ b/src/player.h
@@ -54,6 +54,10 @@ class Player : public Being
#ifdef EATHENA_SUPPORT
virtual void logic();
#endif
+ /**
+ * Returns the path to the player's current destination
+ */
+ Path findPath();
virtual Type getType() const;