more autowalk rework

This commit is contained in:
Eduardo Bart 2012-06-03 16:28:17 -03:00
parent 05d6e8c3e1
commit 250e9ac238
5 changed files with 46 additions and 32 deletions

View File

@ -49,7 +49,6 @@ void Game::resetGameStates()
m_denyBotCall = false; m_denyBotCall = false;
#endif #endif
m_dead = false; m_dead = false;
m_autoWalking = false;
m_serverBeat = 50; m_serverBeat = 50;
m_canReportBugs = false; m_canReportBugs = false;
m_fightMode = Otc::FightBalanced; m_fightMode = Otc::FightBalanced;
@ -255,9 +254,8 @@ void Game::processCreatureTeleport(const CreaturePtr& creature)
creature->stopWalk(); creature->stopWalk();
// locks the walk for a while when teleporting // locks the walk for a while when teleporting
if(creature == m_localPlayer) { if(creature == m_localPlayer)
m_localPlayer->lockWalk(); m_localPlayer->lockWalk();
}
} }
void Game::processChannelList(const std::vector<std::tuple<int, std::string>>& channelList) void Game::processChannelList(const std::vector<std::tuple<int, std::string>>& channelList)
@ -394,12 +392,10 @@ void Game::processAttackCancel()
void Game::processWalkCancel(Otc::Direction direction) void Game::processWalkCancel(Otc::Direction direction)
{ {
m_localPlayer->cancelWalk(direction); if(m_localPlayer->isAutoWalking())
m_protocolGame->sendStop();
if(m_autoWalking) { m_localPlayer->cancelWalk(direction);
m_protocolGame->sendAutoWalk(std::vector<Otc::Direction>());
m_autoWalking = false;
}
} }
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) 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()) if(!canPerformGameAction())
return; return;
// must cancel follow before any new walk
if(isFollowing()) if(isFollowing())
cancelFollow(); cancelFollow();
if(m_autoWalking) { // msut cancel auto walking and wait next try
m_protocolGame->sendAutoWalk(std::vector<Otc::Direction>(1, direction)); if(m_localPlayer->isAutoWalking()) {
m_autoWalking = false; m_protocolGame->sendStop();
return; return;
} }
if(!m_localPlayer->canWalk(direction)) if(!m_localPlayer->canWalk(direction))
return; 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)); TilePtr toTile = g_map.getTile(m_localPlayer->getPosition().translatedToDirection(direction));
if(toTile && toTile->isWalkable()) if(toTile && toTile->isWalkable())
m_localPlayer->preWalk(direction); m_localPlayer->preWalk(direction);
@ -472,28 +469,23 @@ void Game::autoWalk(const std::vector<Otc::Direction>& dirs)
if(!canPerformGameAction()) if(!canPerformGameAction())
return; 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()); walk(dirs.front());
return; return;
} }
if(dirs.size() > 255) // must cancel follow before any new walk
return;
if(isFollowing()) if(isFollowing())
cancelFollow(); cancelFollow();
m_protocolGame->sendAutoWalk(dirs); m_protocolGame->sendAutoWalk(dirs);
m_autoWalking = true;
}
void Game::stopAutoWalk()
{
if(!canPerformGameAction())
return;
m_protocolGame->sendAutoWalk(std::vector<Otc::Direction>());
m_autoWalking = false;
} }
void Game::forceWalk(Otc::Direction direction) void Game::forceWalk(Otc::Direction direction)
@ -563,7 +555,6 @@ void Game::stop()
cancelFollow(); cancelFollow();
m_protocolGame->sendStop(); m_protocolGame->sendStop();
m_autoWalking = false;
} }
void Game::look(const ThingPtr& thing) void Game::look(const ThingPtr& thing)

View File

@ -129,7 +129,6 @@ public:
// walk related // walk related
void walk(Otc::Direction direction); void walk(Otc::Direction direction);
void autoWalk(const std::vector<Otc::Direction>& dirs); void autoWalk(const std::vector<Otc::Direction>& dirs);
void stopAutoWalk();
void forceWalk(Otc::Direction direction); void forceWalk(Otc::Direction direction);
void turn(Otc::Direction direction); void turn(Otc::Direction direction);
void stop(); void stop();
@ -278,7 +277,6 @@ private:
bool m_denyBotCall; bool m_denyBotCall;
bool m_dead; bool m_dead;
bool m_autoWalking;
int m_serverBeat; int m_serverBeat;
Otc::FightModes m_fightMode; Otc::FightModes m_fightMode;
Otc::ChaseModes m_chaseMode; Otc::ChaseModes m_chaseMode;

View File

@ -24,12 +24,14 @@
#include "map.h" #include "map.h"
#include "game.h" #include "game.h"
#include "tile.h" #include "tile.h"
#include <framework/core/eventdispatcher.h>
LocalPlayer::LocalPlayer() LocalPlayer::LocalPlayer()
{ {
m_preWalking = false; m_preWalking = false;
m_walkLocked = false; m_walkLocked = false;
m_lastPrewalkDone = true; m_lastPrewalkDone = true;
m_autoWalking = false;
m_known = false; m_known = false;
m_states = 0; m_states = 0;
@ -95,8 +97,15 @@ void LocalPlayer::walk(const Position& oldPos, const Position& newPos)
// was to another direction, replace the walk // was to another direction, replace the walk
} else } else
Creature::walk(oldPos, newPos); 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); Creature::walk(oldPos, newPos);
}
} }
void LocalPlayer::preWalk(Otc::Direction direction) void LocalPlayer::preWalk(Otc::Direction direction)
@ -104,6 +113,10 @@ void LocalPlayer::preWalk(Otc::Direction direction)
// start walking to direction // start walking to direction
Position newPos = m_position.translatedToDirection(direction); Position newPos = m_position.translatedToDirection(direction);
m_preWalking = true; m_preWalking = true;
if(m_autoWalkEndEvent)
m_autoWalkEndEvent->cancel();
m_lastPrewalkDone = false; m_lastPrewalkDone = false;
m_lastPrewalkDestionation = newPos; m_lastPrewalkDestionation = newPos;
Creature::walk(m_position, newPos); Creature::walk(m_position, newPos);
@ -124,7 +137,7 @@ void LocalPlayer::cancelWalk(Otc::Direction direction)
void LocalPlayer::stopWalk() void LocalPlayer::stopWalk()
{ {
Creature::stopWalk(); Creature::stopWalk(); // will call terminateWalk
m_lastPrewalkDone = true; m_lastPrewalkDone = true;
m_lastPrewalkDestionation = Position(); m_lastPrewalkDestionation = Position();
} }
@ -166,6 +179,16 @@ void LocalPlayer::terminateWalk()
{ {
Creature::terminateWalk(); Creature::terminateWalk();
m_preWalking = false; 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) void LocalPlayer::setStates(int states)

View File

@ -71,6 +71,7 @@ public:
bool isKnown() { return m_known; } bool isKnown() { return m_known; }
bool isPreWalking() { return m_preWalking; } bool isPreWalking() { return m_preWalking; }
bool isAutoWalking() { return m_autoWalking; }
LocalPlayerPtr asLocalPlayer() { return std::static_pointer_cast<LocalPlayer>(shared_from_this()); } LocalPlayerPtr asLocalPlayer() { return std::static_pointer_cast<LocalPlayer>(shared_from_this()); }
@ -92,9 +93,11 @@ private:
bool m_preWalking; bool m_preWalking;
bool m_lastPrewalkDone; bool m_lastPrewalkDone;
bool m_walkLocked; bool m_walkLocked;
bool m_autoWalking;
Position m_lastPrewalkDestionation; Position m_lastPrewalkDestionation;
Timer m_walkLockTimer; Timer m_walkLockTimer;
ItemPtr m_inventoryItems[Otc::LastInventory]; ItemPtr m_inventoryItems[Otc::LastInventory];
ScheduledEventPtr m_autoWalkEndEvent;
std::array<int, Otc::LastSkill> m_skillsLevel; std::array<int, Otc::LastSkill> m_skillsLevel;
std::array<int, Otc::LastSkill> m_skillsLevelPercent; std::array<int, Otc::LastSkill> m_skillsLevelPercent;

View File

@ -98,7 +98,6 @@ void OTClient::init(const std::vector<std::string>& args)
g_modules.ensureModuleLoaded("game"); g_modules.ensureModuleLoaded("game");
// addons 1000-9999 // addons 1000-9999
g_modules.autoLoadModules(9999); g_modules.autoLoadModules(9999);
g_map.load();
// load otclientrc.lua // load otclientrc.lua
if(g_resources.fileExists("/otclientrc.lua")) { if(g_resources.fileExists("/otclientrc.lua")) {