Improve autowalk

This commit is contained in:
Eduardo Bart 2013-01-22 16:04:36 -02:00
parent 01e48fbcc8
commit e900a7679a
6 changed files with 55 additions and 22 deletions

View File

@ -469,9 +469,6 @@ void Game::processAttackCancel(uint seq)
void Game::processWalkCancel(Otc::Direction direction)
{
if(m_localPlayer->isAutoWalking())
m_protocolGame->sendStop();
m_localPlayer->cancelWalk(direction);
}

View File

@ -158,6 +158,17 @@ void LocalPlayer::cancelWalk(Otc::Direction direction)
m_walkPingTimer.restart();
m_idleTimer.restart();
if(m_autoWalkDestination.isValid()) {
g_game.stop();
auto self = asLocalPlayer();
if(m_autoWalkContinueEvent)
m_autoWalkContinueEvent->cancel();
m_autoWalkContinueEvent = g_dispatcher.scheduleEvent([self]() {
if(self->m_autoWalkDestination.isValid())
self->autoWalk(self->m_autoWalkDestination);
}, 500);
}
// turn to the cancel direction
if(direction != Otc::InvalidDirection)
setDirection(direction);
@ -167,25 +178,43 @@ void LocalPlayer::cancelWalk(Otc::Direction direction)
bool LocalPlayer::autoWalk(const Position& destination)
{
m_autoWalkDestination = destination;
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> result = g_map.findPath(m_position, destination, 1000, Otc::PathFindAllowNullTiles);
if(std::get<1>(result) != Otc::PathFindResultOk)
return false;
Position currentPos = m_position;
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> result;
std::vector<Otc::Direction> limitedPath;
for(auto dir : std::get<0>(result)) {
currentPos = currentPos.translatedToDirection(dir);
if(!hasSight(currentPos))
break;
else
limitedPath.push_back(dir);
if(destination == m_position)
return true;
// try to find a path that we know
result = g_map.findPath(m_position, destination, 1000, 0);
if(std::get<1>(result) == Otc::PathFindResultOk) {
limitedPath = std::get<0>(result);
// limit to 127 steps
if(limitedPath.size() > 127)
limitedPath.resize(127);
} else {
// no known path found, try to discover one
result = g_map.findPath(m_position, destination, 1000, Otc::PathFindAllowNullTiles);
if(std::get<1>(result) != Otc::PathFindResultOk)
return false;
Position currentPos = m_position;
for(auto dir : std::get<0>(result)) {
currentPos = currentPos.translatedToDirection(dir);
if(!hasSight(currentPos))
break;
else
limitedPath.push_back(dir);
}
}
m_autoWalkDestination = destination;
m_lastAutoWalkPosition = m_position.translatedToDirections(limitedPath).back();
for(auto pos : m_position.translatedToDirections(limitedPath)) {
g_map.getOrCreateTile(pos)->overwriteMinimapColor(16);
g_map.notificateTileUpdate(pos);
}
g_game.autoWalk(limitedPath);
return true;
}
@ -194,6 +223,9 @@ void LocalPlayer::stopAutoWalk()
{
m_autoWalkDestination = Position();
m_lastAutoWalkPosition = Position();
if(m_autoWalkContinueEvent)
m_autoWalkContinueEvent->cancel();
}
void LocalPlayer::stopWalk()

View File

@ -123,6 +123,7 @@ private:
Position m_autoWalkDestination;
Position m_lastAutoWalkPosition;
ScheduledEventPtr m_autoWalkEndEvent;
ScheduledEventPtr m_autoWalkContinueEvent;
ticks_t m_walkLockExpiration;
int m_lastWalkPing;
stdext::boolean<false> m_preWalking;

View File

@ -611,8 +611,8 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
tiles, need to rework this for "fly servers" and blank map click,
but it is breaking normal path finding.
*/
if(!(flags & Otc::PathFindAllowNullTiles) && !tile)
walkFactor = 3.0f;
if(!(flags & Otc::PathFindAllowNullTiles) && (!tile || tile->isEmpty()))
continue;
if(tile) {
if(!(flags & Otc::PathFindAllowCreatures) && tile->hasCreature())
continue;

View File

@ -128,13 +128,16 @@ public:
Position lastPos = *this;
std::vector<Position> positions;
if(!lastPos.isValid())
return positions;
positions.push_back(lastPos);
for(auto dir : dirs) {
if(lastPos.isValid()) {
positions.push_back(lastPos);
}
lastPos = lastPos.translatedToDirection(dir);
if(!lastPos.isValid())
break;
positions.push_back(lastPos);
}
return positions;

View File

@ -612,7 +612,7 @@ bool Tile::limitsFloorsView()
bool Tile::canErase()
{
return m_walkingCreatures.empty() && m_effects.empty() && m_things.empty() && m_flags == 0;
return m_walkingCreatures.empty() && m_effects.empty() && m_things.empty() && m_flags == 0 && m_minimapColor == 0;
}
bool Tile::hasElevation(int elevation)