Fixes and improvements in find path

This commit is contained in:
Eduardo Bart 2013-01-27 16:58:22 -02:00
parent 2bd86d0695
commit 6966221e39
5 changed files with 26 additions and 26 deletions

View File

@ -178,6 +178,12 @@ void LocalPlayer::cancelWalk(Otc::Direction direction)
bool LocalPlayer::autoWalk(const Position& destination) bool LocalPlayer::autoWalk(const Position& destination)
{ {
bool tryKnownPath = false;
if(destination != m_autoWalkDestination) {
m_knownCompletePath = false;
tryKnownPath = true;
}
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> result; std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> result;
std::vector<Otc::Direction> limitedPath; std::vector<Otc::Direction> limitedPath;
@ -185,15 +191,20 @@ bool LocalPlayer::autoWalk(const Position& destination)
return true; return true;
// try to find a path that we know // try to find a path that we know
result = g_map.findPath(m_position, destination, 1000, 0); if(tryKnownPath || m_knownCompletePath) {
result = g_map.findPath(m_position, destination, 50000, 0);
if(std::get<1>(result) == Otc::PathFindResultOk) { if(std::get<1>(result) == Otc::PathFindResultOk) {
limitedPath = std::get<0>(result); limitedPath = std::get<0>(result);
// limit to 127 steps // limit to 127 steps
if(limitedPath.size() > 127) if(limitedPath.size() > 127)
limitedPath.resize(127); limitedPath.resize(127);
} else { m_knownCompletePath = true;
}
}
// no known path found, try to discover one // no known path found, try to discover one
result = g_map.findPath(m_position, destination, 1000, Otc::PathFindAllowNotSeenTiles); if(limitedPath.empty()) {
result = g_map.findPath(m_position, destination, 50000, Otc::PathFindAllowNotSeenTiles);
if(std::get<1>(result) != Otc::PathFindResultOk) { if(std::get<1>(result) != Otc::PathFindResultOk) {
callLuaField("onAutoWalkFail", std::get<1>(result)); callLuaField("onAutoWalkFail", std::get<1>(result));
return false; return false;
@ -228,6 +239,7 @@ void LocalPlayer::stopAutoWalk()
{ {
m_autoWalkDestination = Position(); m_autoWalkDestination = Position();
m_lastAutoWalkPosition = Position(); m_lastAutoWalkPosition = Position();
m_knownCompletePath = false;
if(m_autoWalkContinueEvent) if(m_autoWalkContinueEvent)
m_autoWalkContinueEvent->cancel(); m_autoWalkContinueEvent->cancel();

View File

@ -130,6 +130,7 @@ private:
stdext::boolean<true> m_lastPrewalkDone; stdext::boolean<true> m_lastPrewalkDone;
stdext::boolean<false> m_autoWalking; stdext::boolean<false> m_autoWalking;
stdext::boolean<false> m_waitingWalkPong; stdext::boolean<false> m_waitingWalkPong;
stdext::boolean<false> m_knownCompletePath;
stdext::boolean<false> m_premium; stdext::boolean<false> m_premium;
stdext::boolean<false> m_known; stdext::boolean<false> m_known;

View File

@ -522,7 +522,7 @@ int Map::getLastAwareFloor()
return Otc::SEA_FLOOR; return Otc::SEA_FLOOR;
} }
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const Position& startPos, const Position& goalPos, int maxSteps, int flags) std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const Position& startPos, const Position& goalPos, int maxComplexity, int flags)
{ {
// pathfinding using A* search algorithm // pathfinding using A* search algorithm
// as described in http://en.wikipedia.org/wiki/A*_search_algorithm // as described in http://en.wikipedia.org/wiki/A*_search_algorithm
@ -545,8 +545,6 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
} }
}; };
const uint MAX_COMPLEXITY = 100000;
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret; std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> ret;
std::vector<Otc::Direction>& dirs = std::get<0>(ret); std::vector<Otc::Direction>& dirs = std::get<0>(ret);
Otc::PathFindResult& result = std::get<1>(ret); Otc::PathFindResult& result = std::get<1>(ret);
@ -563,11 +561,6 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
return ret; return ret;
} }
if(startPos.distance(goalPos) > maxSteps) {
result = Otc::PathFindResultTooFar;
return ret;
}
std::unordered_map<Position, Node*, PositionHasher> nodes; std::unordered_map<Position, Node*, PositionHasher> nodes;
std::priority_queue<Node*, std::vector<Node*>, LessNode> searchList; std::priority_queue<Node*, std::vector<Node*>, LessNode> searchList;
@ -576,13 +569,7 @@ std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> Map::findPath(const
nodes[startPos] = currentNode; nodes[startPos] = currentNode;
Node *foundNode = nullptr; Node *foundNode = nullptr;
while(currentNode) { while(currentNode) {
// too far if((int)nodes.size() > maxComplexity) {
if(currentNode->steps >= maxSteps) {
result = Otc::PathFindResultTooFar;
break;
}
if(nodes.size() > MAX_COMPLEXITY) {
result = Otc::PathFindResultTooFar; result = Otc::PathFindResultTooFar;
break; break;
} }

View File

@ -209,7 +209,7 @@ public:
std::vector<AnimatedTextPtr> getAnimatedTexts() { return m_animatedTexts; } std::vector<AnimatedTextPtr> getAnimatedTexts() { return m_animatedTexts; }
std::vector<StaticTextPtr> getStaticTexts() { return m_staticTexts; } std::vector<StaticTextPtr> getStaticTexts() { return m_staticTexts; }
std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> findPath(const Position& start, const Position& goal, int maxSteps, int flags = 0); std::tuple<std::vector<Otc::Direction>, Otc::PathFindResult> findPath(const Position& start, const Position& goal, int maxComplexity, int flags = 0);
private: private:
void removeUnawareThings(); void removeUnawareThings();

View File

@ -42,7 +42,7 @@ enum MinimapTileFlags {
#pragma pack(push,1) // disable memory alignment #pragma pack(push,1) // disable memory alignment
struct MinimapTile struct MinimapTile
{ {
MinimapTile() : flags(0), color(0), speed(100) { } MinimapTile() : flags(0), color(0), speed(10) { }
uint8 flags; uint8 flags;
uint8 color; uint8 color;
uint8 speed; uint8 speed;