diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 33e07e41..2df6cfa6 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -49,7 +49,6 @@ void Game::resetGameStates() m_denyBotCall = false; #endif m_dead = false; - m_autoWalking = false; m_serverBeat = 50; m_canReportBugs = false; m_fightMode = Otc::FightBalanced; @@ -255,9 +254,8 @@ void Game::processCreatureTeleport(const CreaturePtr& creature) creature->stopWalk(); // locks the walk for a while when teleporting - if(creature == m_localPlayer) { + if(creature == m_localPlayer) m_localPlayer->lockWalk(); - } } void Game::processChannelList(const std::vector>& channelList) @@ -394,12 +392,10 @@ void Game::processAttackCancel() void Game::processWalkCancel(Otc::Direction direction) { - m_localPlayer->cancelWalk(direction); + if(m_localPlayer->isAutoWalking()) + m_protocolGame->sendStop(); - if(m_autoWalking) { - m_protocolGame->sendAutoWalk(std::vector()); - m_autoWalking = false; - } + m_localPlayer->cancelWalk(direction); } void Game::loginWorld(const std::string& account, const std::string& password, const std::string& worldName, const std::string& worldHost, int worldPort, const std::string& characterName) @@ -445,19 +441,20 @@ void Game::walk(Otc::Direction direction) if(!canPerformGameAction()) return; + // must cancel follow before any new walk if(isFollowing()) cancelFollow(); - if(m_autoWalking) { - m_protocolGame->sendAutoWalk(std::vector(1, direction)); - m_autoWalking = false; + // msut cancel auto walking and wait next try + if(m_localPlayer->isAutoWalking()) { + m_protocolGame->sendStop(); return; } if(!m_localPlayer->canWalk(direction)) return; - // only do prewalks to walkable tiles + // only do prewalks to walkable tiles (like grounds and not walls) TilePtr toTile = g_map.getTile(m_localPlayer->getPosition().translatedToDirection(direction)); if(toTile && toTile->isWalkable()) m_localPlayer->preWalk(direction); @@ -472,28 +469,23 @@ void Game::autoWalk(const std::vector& dirs) if(!canPerformGameAction()) return; - if(dirs.size() == 1 && !m_autoWalking) { + // protocol limits walk path up to 255 directions + if(dirs.size() > 255) { + g_logger.error("Auto walk path too great, the maximum number of directions is 255"); + return; + } + + // actually do a normal walking when dirs have size == 1 + if(dirs.size() == 1) { walk(dirs.front()); return; } - if(dirs.size() > 255) - return; - + // must cancel follow before any new walk if(isFollowing()) cancelFollow(); m_protocolGame->sendAutoWalk(dirs); - m_autoWalking = true; -} - -void Game::stopAutoWalk() -{ - if(!canPerformGameAction()) - return; - - m_protocolGame->sendAutoWalk(std::vector()); - m_autoWalking = false; } void Game::forceWalk(Otc::Direction direction) @@ -563,7 +555,6 @@ void Game::stop() cancelFollow(); m_protocolGame->sendStop(); - m_autoWalking = false; } void Game::look(const ThingPtr& thing) diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 2a246fcf..23d626fb 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -129,7 +129,6 @@ public: // walk related void walk(Otc::Direction direction); void autoWalk(const std::vector& dirs); - void stopAutoWalk(); void forceWalk(Otc::Direction direction); void turn(Otc::Direction direction); void stop(); @@ -278,7 +277,6 @@ private: bool m_denyBotCall; bool m_dead; - bool m_autoWalking; int m_serverBeat; Otc::FightModes m_fightMode; Otc::ChaseModes m_chaseMode; diff --git a/src/otclient/core/localplayer.cpp b/src/otclient/core/localplayer.cpp index 651ed75f..d145dcfa 100644 --- a/src/otclient/core/localplayer.cpp +++ b/src/otclient/core/localplayer.cpp @@ -24,12 +24,14 @@ #include "map.h" #include "game.h" #include "tile.h" +#include LocalPlayer::LocalPlayer() { m_preWalking = false; m_walkLocked = false; m_lastPrewalkDone = true; + m_autoWalking = false; m_known = false; m_states = 0; @@ -95,8 +97,15 @@ void LocalPlayer::walk(const Position& oldPos, const Position& newPos) // was to another direction, replace the walk } else Creature::walk(oldPos, newPos); - } else + } + // no prewalk was going on, this must be an server side automated walk + else { + m_autoWalking = true; + if(m_autoWalkEndEvent) + m_autoWalkEndEvent->cancel(); + Creature::walk(oldPos, newPos); + } } void LocalPlayer::preWalk(Otc::Direction direction) @@ -104,6 +113,10 @@ void LocalPlayer::preWalk(Otc::Direction direction) // start walking to direction Position newPos = m_position.translatedToDirection(direction); m_preWalking = true; + + if(m_autoWalkEndEvent) + m_autoWalkEndEvent->cancel(); + m_lastPrewalkDone = false; m_lastPrewalkDestionation = newPos; Creature::walk(m_position, newPos); @@ -124,7 +137,7 @@ void LocalPlayer::cancelWalk(Otc::Direction direction) void LocalPlayer::stopWalk() { - Creature::stopWalk(); + Creature::stopWalk(); // will call terminateWalk m_lastPrewalkDone = true; m_lastPrewalkDestionation = Position(); } @@ -166,6 +179,16 @@ void LocalPlayer::terminateWalk() { Creature::terminateWalk(); m_preWalking = false; + + auto self = asLocalPlayer(); + + if(m_autoWalking) { + if(m_autoWalkEndEvent) + m_autoWalkEndEvent->cancel(); + m_autoWalkEndEvent = g_eventDispatcher.scheduleEvent([self] { + self->m_autoWalking = false; + }, 100); + } } void LocalPlayer::setStates(int states) diff --git a/src/otclient/core/localplayer.h b/src/otclient/core/localplayer.h index 4e64fdfb..03087d96 100644 --- a/src/otclient/core/localplayer.h +++ b/src/otclient/core/localplayer.h @@ -71,6 +71,7 @@ public: bool isKnown() { return m_known; } bool isPreWalking() { return m_preWalking; } + bool isAutoWalking() { return m_autoWalking; } LocalPlayerPtr asLocalPlayer() { return std::static_pointer_cast(shared_from_this()); } @@ -92,9 +93,11 @@ private: bool m_preWalking; bool m_lastPrewalkDone; bool m_walkLocked; + bool m_autoWalking; Position m_lastPrewalkDestionation; Timer m_walkLockTimer; ItemPtr m_inventoryItems[Otc::LastInventory]; + ScheduledEventPtr m_autoWalkEndEvent; std::array m_skillsLevel; std::array m_skillsLevelPercent; diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index 6a9962ea..ebe2536a 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -98,7 +98,6 @@ void OTClient::init(const std::vector& args) g_modules.ensureModuleLoaded("game"); // addons 1000-9999 g_modules.autoLoadModules(9999); - g_map.load(); // load otclientrc.lua if(g_resources.fileExists("/otclientrc.lua")) {