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;
#endif
m_dead = false;
m_autoWalking = false;
m_serverBeat = 50;
m_canReportBugs = false;
m_fightMode = Otc::FightBalanced;
@ -255,10 +254,9 @@ 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<std::tuple<int, std::string>>& 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<Otc::Direction>());
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<Otc::Direction>(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<Otc::Direction>& 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<Otc::Direction>());
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)

View File

@ -129,7 +129,6 @@ public:
// walk related
void walk(Otc::Direction direction);
void autoWalk(const std::vector<Otc::Direction>& 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;

View File

@ -24,12 +24,14 @@
#include "map.h"
#include "game.h"
#include "tile.h"
#include <framework/core/eventdispatcher.h>
LocalPlayer::LocalPlayer()
{
m_preWalking = false;
m_walkLocked = false;
m_lastPrewalkDone = true;
m_autoWalking = false;
m_known = false;
m_states = 0;
@ -95,15 +97,26 @@ 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)
{
// 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)

View File

@ -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<LocalPlayer>(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<int, Otc::LastSkill> m_skillsLevel;
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");
// addons 1000-9999
g_modules.autoLoadModules(9999);
g_map.load();
// load otclientrc.lua
if(g_resources.fileExists("/otclientrc.lua")) {