From 4d90b674acddc7084fb093c7c4ebe6d2f4048455 Mon Sep 17 00:00:00 2001 From: Henrique Date: Tue, 30 Aug 2011 11:37:48 -0300 Subject: [PATCH] walk changes, not done yet --- src/otclient/core/creature.cpp | 87 ++++++++++++++------------ src/otclient/core/creature.h | 1 + src/otclient/core/game.cpp | 2 +- src/otclient/core/localplayer.cpp | 6 ++ src/otclient/core/localplayer.h | 2 + src/otclient/net/protocolgameparse.cpp | 5 +- src/otclient/util/position.h | 24 +++++++ 7 files changed, 84 insertions(+), 43 deletions(-) diff --git a/src/otclient/core/creature.cpp b/src/otclient/core/creature.cpp index b9210ee1..ed85167f 100644 --- a/src/otclient/core/creature.cpp +++ b/src/otclient/core/creature.cpp @@ -136,10 +136,7 @@ void Creature::draw(int x, int y) if(((m_walkOffsetX == 0 && m_walkOffsetY == 0) && m_walkOffsetX != m_walkOffsetY) || ((m_walkOffsetX == 0 || m_walkOffsetY == 0) && m_walkOffsetX == m_walkOffsetY)) { - m_walking = false; - m_walkOffsetX = 0; - m_walkOffsetY = 0; - m_animation = 0; + cancelWalk(m_direction); } m_lastTicks = g_platform.getTicks() - remainingTime; @@ -174,50 +171,44 @@ void Creature::walk(const Position& position) m_walking = true; m_walkOffsetX = 0; m_walkOffsetY = 0; - m_walkingFromPosition = m_position; int walkTimeFactor = 1; - // update map tiles - g_map.removeThingByPtr(asThing()); - m_position = position; - g_map.addThing(asThing()); - // set new direction - if(m_walkingFromPosition + Position(0, -1, 0) == m_position) { + if(m_position + Position(0, -1, 0) == position) { m_direction = Otc::North; m_walkOffsetY = 32; } - else if(m_walkingFromPosition + Position(1, 0, 0) == m_position) { + else if(m_position + Position(1, 0, 0) == position) { m_direction = Otc::East; m_walkOffsetX = -32; } - else if(m_walkingFromPosition + Position(0, 1, 0) == m_position) { + else if(m_position + Position(0, 1, 0) == position) { m_direction = Otc::South; m_walkOffsetY = -32; } - else if(m_walkingFromPosition + Position(-1, 0, 0) == m_position) { + else if(m_position + Position(-1, 0, 0) == position) { m_direction = Otc::West; m_walkOffsetX = 32; } - else if(m_walkingFromPosition + Position(1, -1, 0) == m_position) { + else if(m_position + Position(1, -1, 0) == position) { m_direction = Otc::NorthEast; m_walkOffsetX = -32; m_walkOffsetY = 32; walkTimeFactor = 2; } - else if(m_walkingFromPosition + Position(1, 1, 0) == m_position) { + else if(m_position + Position(1, 1, 0) == position) { m_direction = Otc::SouthEast; m_walkOffsetX = -32; m_walkOffsetY = -32; walkTimeFactor = 2; } - else if(m_walkingFromPosition + Position(-1, 1, 0) == m_position) { + else if(m_position + Position(-1, 1, 0) == position) { m_direction = Otc::SouthWest; m_walkOffsetX = 32; m_walkOffsetY = -32; walkTimeFactor = 2; } - else if(m_walkingFromPosition + Position(-1, -1, 0) == m_position) { + else if(m_position + Position(-1, -1, 0) == position) { m_direction = Otc::NorthWest; m_walkOffsetX = 32; m_walkOffsetY = 32; @@ -228,30 +219,46 @@ void Creature::walk(const Position& position) m_walking = false; } - // Calculate xDiv - if(m_direction >= 4) { - if(m_direction == Otc::NorthEast || m_direction == Otc::SouthEast) - m_xDiv = Otc::East; - else if(m_direction == Otc::NorthWest || m_direction == Otc::SouthWest) - m_xDiv = Otc::West; - } - else { - m_xDiv = m_direction; + // update map tiles + g_map.removeThingByPtr(asThing()); + m_position = position; + g_map.addThing(asThing()); + + if(m_walking) { + // Calculate xDiv + if(m_direction >= 4) { + if(m_direction == Otc::NorthEast || m_direction == Otc::SouthEast) + m_xDiv = Otc::East; + else if(m_direction == Otc::NorthWest || m_direction == Otc::SouthWest) + m_xDiv = Otc::West; + } + else { + m_xDiv = m_direction; + } + + // get walk speed + int groundSpeed = 0; + + ThingPtr ground = g_map.getThing(m_position, 0); + if(ground) + groundSpeed = ground->getAttributes().speed; + + float walkTime = walkTimeFactor * 1000.0 * (float)groundSpeed / m_speed; + walkTime = walkTime == 0 ? 1000 : walkTime; + + m_walkTimePerPixel = walkTime / 32.0; + + m_lastTicks = g_platform.getTicks(); } +} - // get walk speed - int groundSpeed = 0; - - ThingPtr ground = g_map.getThing(m_position, 0); - if(ground) - groundSpeed = ground->getAttributes().speed; - - float walkTime = walkTimeFactor * 1000.0 * (float)groundSpeed / m_speed; - walkTime = walkTime == 0 ? 1000 : walkTime; - - m_walkTimePerPixel = walkTime / 32.0; - - m_lastTicks = g_platform.getTicks(); +void Creature::cancelWalk(Otc::Direction direction) +{ + m_walking = false; + m_walkOffsetX = 0; + m_walkOffsetY = 0; + m_animation = 0; + m_direction = direction; } void Creature::setHealthPercent(uint8 healthPercent) diff --git a/src/otclient/core/creature.h b/src/otclient/core/creature.h index 3217bb06..44557466 100644 --- a/src/otclient/core/creature.h +++ b/src/otclient/core/creature.h @@ -71,6 +71,7 @@ public: void onHealthPercentChange(int); void walk(const Position& position); + void cancelWalk(Otc::Direction direction); int getWalkOffsetX() { return m_walkOffsetX; } int getWalkOffsetY() { return m_walkOffsetY; } diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index c3584524..d700eb0d 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -102,7 +102,7 @@ void Game::walk(Otc::Direction direction) // TODO: check if we can walk. - //m_localPlayer->setDirection(direction); + m_localPlayer->walk(direction); switch(direction) { case Otc::North: diff --git a/src/otclient/core/localplayer.cpp b/src/otclient/core/localplayer.cpp index 2fcd6e62..c249cc5a 100644 --- a/src/otclient/core/localplayer.cpp +++ b/src/otclient/core/localplayer.cpp @@ -22,3 +22,9 @@ #include "localplayer.h" +void LocalPlayer::walk(Otc::Direction direction) +{ + //Position newPos = m_position + Position::getPositionFromDirection(direction); + + //asCreature()->walk(newPos); +} diff --git a/src/otclient/core/localplayer.h b/src/otclient/core/localplayer.h index 29f35f63..e78e7fcf 100644 --- a/src/otclient/core/localplayer.h +++ b/src/otclient/core/localplayer.h @@ -39,6 +39,8 @@ public: void setSkill(Otc::Skill skill, Otc::SkillType skillType, int value) { m_skills[skill][skillType] = value; } int getSkill(Otc::Skill skill, Otc::SkillType skillType) { return m_skills[skill][skillType]; } + void walk(Otc::Direction direction); + LocalPlayerPtr asLocalPlayer() { return std::static_pointer_cast(shared_from_this()); } private: diff --git a/src/otclient/net/protocolgameparse.cpp b/src/otclient/net/protocolgameparse.cpp index 0172172e..77a03c8b 100644 --- a/src/otclient/net/protocolgameparse.cpp +++ b/src/otclient/net/protocolgameparse.cpp @@ -34,7 +34,7 @@ void ProtocolGame::parseMessage(InputMessage& msg) { while(!msg.eof()) { uint8 opt = msg.getU8(); - //dump << "protocol id:" << std::hex << (int)opt; + dump << "protocol id:" << std::hex << (int)opt; switch(opt) { case 0x0A: @@ -763,7 +763,7 @@ void ProtocolGame::parseTextMessage(InputMessage& msg) void ProtocolGame::parseCancelWalk(InputMessage& msg) { Otc::Direction direction = (Otc::Direction)msg.getU8(); - g_game.getLocalPlayer()->setDirection(direction); + m_localPlayer->cancelWalk(direction); } void ProtocolGame::parseFloorChangeUp(InputMessage& msg) @@ -1008,6 +1008,7 @@ ThingPtr ProtocolGame::internalGetThing(InputMessage& msg) creature->setShield(shield); creature->setEmblem(emblem); creature->setImpassable(impassable); + creature->cancelWalk(direction); thing = creature; diff --git a/src/otclient/util/position.h b/src/otclient/util/position.h index 7704e677..8210cd4d 100644 --- a/src/otclient/util/position.h +++ b/src/otclient/util/position.h @@ -23,6 +23,7 @@ #ifndef POSITION_H #define POSITION_H +#include #include class Position @@ -31,6 +32,29 @@ public: Position() : x(-1), y(-1), z(-1) { } Position(int x, int y, int z) : x(x), y(y), z(z) { } + static Position getPositionFromDirection(Otc::Direction direction) { + switch(direction) { + case Otc::North: + return Position( 0, -1, 0); + case Otc::East: + return Position( 1, 0, 0); + case Otc::South: + return Position( 0, 1, 0); + case Otc::West: + return Position(-1, 0, 0); + case Otc::NorthEast: + return Position( 1, -1, 0); + case Otc::SouthEast: + return Position( 1, 1, 0); + case Otc::SouthWest: + return Position(-1, 1, 0); + case Otc::NorthWest: + return Position(-1, -1, 0); + default: + return Position(); + } + } + bool isValid() const { return x >= 0 && y >= 0 && z >= 0 && x < 65536 && y < 65536 && z < 15; } Position operator+(const Position& other) const { return Position(x + other.x, y + other.y, z + other.z); }