From 2f55dd130c886376a45c9d0054dbd987cc055155 Mon Sep 17 00:00:00 2001 From: Bertram Date: Thu, 15 Oct 2009 01:38:18 +0200 Subject: Speed code unification part 3: Made the client handle the speed in tiles per second in TMWserv. While I was on it, I tweaked the default speed value to its final 6 tiles per second value, which seems to be nice to me. Another notice, the server does already send speed value to the player. The keyboard movement protocol is one step ahead Release Candidate, enjoy ;) --- src/being.cpp | 42 +++++++++++++++++++++++++++------------- src/being.h | 20 +++++++++++++++---- src/game.h | 1 + src/map.cpp | 5 +++++ src/map.h | 2 ++ src/net/tmwserv/beinghandler.cpp | 11 ++++------- 6 files changed, 57 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/being.cpp b/src/being.cpp index ef71b748..c13b52f7 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -58,10 +58,12 @@ #define BEING_EFFECTS_FILE "effects.xml" #define HAIR_FILE "hair.xml" -int Being::mNumberOfHairstyles = 1; +static const int DEFAULT_BEING_WIDTH = 32; +static const int DEFAULT_BEING_HEIGHT = 32; +extern const int MILLISECONDS_IN_A_TICK; + -static const int DEFAULT_WIDTH = 32; -static const int DEFAULT_HEIGHT = 32; +int Being::mNumberOfHairstyles = 1; Being::Being(int id, int job, Map *map): #ifdef EATHENA_SUPPORT @@ -87,7 +89,7 @@ Being::Being(int id, int job, Map *map): mChildParticleEffects(&mStatusParticleEffects, false), mMustResetParticles(false), #ifdef TMWSERV_SUPPORT - mWalkSpeed(100), + mWalkSpeed(6.0f), // default speed in tile per second #else mWalkSpeed(150), #endif @@ -525,16 +527,30 @@ void Being::logic() mDest : Vector(mPath.front().x, mPath.front().y); + // The Vector representing the difference between current position + // and the next destination path node. Vector dir = dest - mPos; - const float length = dir.length(); + + const float nominalLength = dir.length(); // When we've not reached our destination, move to it. - if (length > 0.0f) + if (nominalLength > 0.0f && mWalkSpeed > 0.0f) { - const float speed = mWalkSpeed / 100.0f; + + // The private mWalkSpeed member is the speed in tiles per second. + // We translate it into pixels per tick, + // because the logic is called every ticks. + const float speedInTicks = ((float)DEFAULT_TILE_SIDE_LENGTH * mWalkSpeed) + / 1000 * (float)MILLISECONDS_IN_A_TICK; + + // The deplacement of a point along a vector is calculated + // using the Unit Vector (â) multiplied by the point speed. + // â = a / ||a|| (||a|| is the a length.) + // Then, diff = (dir/||dir||)*speed, or (dir / ||dir|| / 1/speed). + Vector diff = (dir / (nominalLength / speedInTicks)); // Test if we don't miss the destination by a move too far: - if ((dir / (length / speed)).length() > dir.length()) + if (diff.length() > nominalLength) { setPosition(mPos + dir); @@ -545,7 +561,7 @@ void Being::logic() } // Otherwise, go to it using the nominal speed. else - setPosition(mPos + (dir / (length / speed))); + setPosition(mPos + diff); if (mAction != WALK) setAction(WALK); @@ -780,9 +796,9 @@ int Being::getWidth() const break; if (base) - return std::max(base->getWidth(), DEFAULT_WIDTH); + return std::max(base->getWidth(), DEFAULT_BEING_WIDTH); else - return DEFAULT_WIDTH; + return DEFAULT_BEING_WIDTH; } int Being::getHeight() const @@ -794,9 +810,9 @@ int Being::getHeight() const break; if (base) - return std::max(base->getHeight(), DEFAULT_HEIGHT); + return std::max(base->getHeight(), DEFAULT_BEING_HEIGHT); else - return DEFAULT_HEIGHT; + return DEFAULT_BEING_HEIGHT; } void Being::setTargetAnimation(SimpleAnimation* animation) diff --git a/src/being.h b/src/being.h index 5869b879..c6c49f5e 100644 --- a/src/being.h +++ b/src/being.h @@ -259,11 +259,18 @@ class Being : public Sprite, public ConfigListener virtual Type getType() const { return UNKNOWN; } /** - * Sets the walk speed (in pixels per second). + * Sets the walk speed. + * in pixels per second for eAthena, + * in tiles per second for TMWserv. */ - void setWalkSpeed(int speed) { mWalkSpeed = speed; } + void setWalkSpeed(float speed) { mWalkSpeed = speed; } - int getWalkSpeed() const { return mWalkSpeed; } + /** + * Gets the walk speed. + * in pixels per second for eAthena, + * in tiles per second for TMWserv (0.1 precision). + */ + float getWalkSpeed() const { return mWalkSpeed; } /** * Sets the sprite id. @@ -568,7 +575,12 @@ class Being : public Sprite, public ConfigListener /** Speech Bubble components */ SpeechBubble *mSpeechBubble; - int mWalkSpeed; /**< Walking speed (pixels/sec) */ + /** + * Walk speed. + * In pixels per second for eAthena, + * In tiles per second (0.1 precision) for TMWserv. + */ + float mWalkSpeed; Vector mPos; Vector mDest; diff --git a/src/game.h b/src/game.h index 1b9373a9..628c710b 100644 --- a/src/game.h +++ b/src/game.h @@ -29,6 +29,7 @@ extern std::string map_path; extern volatile int fps; extern volatile int tick_time; +extern const int MILLISECONDS_IN_A_TICK; class WindowMenu; diff --git a/src/map.cpp b/src/map.cpp index 56e13697..843b8308 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -40,6 +40,11 @@ extern volatile int tick_time; +/** + * The used side-length for tiles + */ +const int DEFAULT_TILE_SIDE_LENGTH = 32; + /** * A location on a tile map. Used for pathfinding, open list. */ diff --git a/src/map.h b/src/map.h index 9558f989..75c4bda4 100644 --- a/src/map.h +++ b/src/map.h @@ -43,6 +43,8 @@ typedef std::list MapSprites; typedef MapSprites::iterator MapSprite; typedef std::vector Layers; +extern const int DEFAULT_TILE_SIDE_LENGTH; + /** * A meta tile stores additional information about a location on a tile map. * This is information that doesn't need to be repeated for each tile in each diff --git a/src/net/tmwserv/beinghandler.cpp b/src/net/tmwserv/beinghandler.cpp index d5092782..2cf9274a 100644 --- a/src/net/tmwserv/beinghandler.cpp +++ b/src/net/tmwserv/beinghandler.cpp @@ -203,13 +203,10 @@ void BeingHandler::handleBeingsMoveMessage(MessageIn &msg) } if (speed) { - /* The speed on the server is the cost of moving from one tile to - * the next. Beings get 1000 cost units per second. The speed is - * transferred as devided by 10, so that slower speeds fit in a - * byte. Here we convert the speed to pixels per second. - */ - const float tilesPerSecond = 100.0f / speed; - being->setWalkSpeed((int) (tilesPerSecond * 32)); + // The being's speed is transfered in tiles per second * 10 + // to keep it transferable in a Byte. + // We set it back to tiles per second and in a float. + being->setWalkSpeed((float) speed / 10); } // Ignore messages from the server for the local player -- cgit v1.2.3-70-g09d2