-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Optimize pathfinding #4637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Optimize pathfinding #4637
Changes from all commits
Commits
Show all changes
100 commits
Select commit
Hold shift + click to select a range
990ba89
Optimize pathfinding
NRH-AA 00951d2
Minor fleeing monsters fix
NRH-AA 89be1ad
Fix whitespace and else brackets
NRH-AA 82ad8c9
More cleanup
NRH-AA 632d202
Cleanup
NRH-AA e7b5725
Remove uneeded variable name change
NRH-AA bc36d30
Cleanup
NRH-AA 68841aa
Move neighbor arrays back to map.cpp to fix unused variable failure
NRH-AA edc1746
Fix some compiling warnings
NRH-AA 2476334
Fix incorrect method call in game.cpp
NRH-AA aa4153a
Update to new creature isDead method
NRH-AA 9f882ca
Update to clang format
NRH-AA 8d50780
Update to clang format
NRH-AA b89076b
Fix maxSearchDist=0 searches and delayed reaction to onFollowCreature
NRH-AA f4163e5
Fix fleeing monster randomly turning
NRH-AA cab2968
Reduce memory consumption and update pathmatching algorithm
NRH-AA 3834fd5
Clang formatting
NRH-AA 3009b52
Clang formatting
NRH-AA 57ce003
Remove uneeded check on new nodes
NRH-AA a68e1f5
Normalize pathfinding algorithm
NRH-AA b832222
Merge branch 'otland:master' into Optimize_Pathfinding
NRH-AA 9e53616
Final Changes: Move pathfinding call to onWalk
NRH-AA bf0cc40
Remove incorrect code
NRH-AA 44a300d
Move from Euclidean to standard A*
NRH-AA e69384d
Fix overlooked typos and uneeded logic
NRH-AA 8a9a093
Remove uneeded nullptr check and change iterations to int8_t
NRH-AA d0346d1
Revert int8_t
NRH-AA bf40672
Fix creature label error and update algorithm
NRH-AA 40581af
Remove uneeded resizing
NRH-AA d5e6586
Revert comment
NRH-AA 093b939
Add missing pathmatching call
NRH-AA d3fdf38
Allow force update path to be called more often
NRH-AA 1e42f12
Fix sight blocked monsters path updating
NRH-AA 1c38ce1
Additional blocked sight fix
NRH-AA 9f924e1
Allow paths to be drawn a little farther like rl tibia
NRH-AA 4f57e88
Add delay to pathfinding and optimize code
NRH-AA 1d36c7b
Fix incorrect conditional
NRH-AA 78aabb4
Move path delay to force update path
NRH-AA f6f0753
Change uint64_t to int64_t
NRH-AA e7efcfb
Add pathfinding interval and delay to config.lua
NRH-AA 14d42f8
Add viewport check
NRH-AA 920da7b
Fix memory leak
NRH-AA bebb988
Add memory management to follow list and pathmatching
NRH-AA dac4fc5
Replace std::sqrt for std::hypot
NRH-AA 8327be7
Merge branch 'otland:master' into Optimize_Pathfinding
NRH-AA a6b3f25
Update to new constexpr position methods
NRH-AA 4d891e9
Revert int declarations in pathmatching
NRH-AA 60efba9
Change std::hypot for much faster std::sqrtf
NRH-AA c1d8530
Formatting
NRH-AA 13e9d66
Add math.h
NRH-AA c3d86ca
Revert sqrtf and math.h
NRH-AA 8efe712
Remove uneeded path call and add speed check
NRH-AA 7f2f868
Merge branch 'otland:master' into Optimize_Pathfinding
NRH-AA 3c42e94
Fix rainsalt review
NRH-AA 297a1bb
Update configmanager
NRH-AA 147d896
Update configmanager
NRH-AA 7f74d9b
Merge branch 'master' into Optimize_Pathfinding
NRH-AA e1f35d5
Fix configmanager after merge
NRH-AA 89f0402
Update to configermanager namespace methods
NRH-AA 25b6a4b
Clang format
NRH-AA 1b78b10
Use getX()/getY() and remove calculateHueristic from AStarNodes
NRH-AA 87e3709
Replace AStarNodes:calculateHeuristic
NRH-AA f3cdd1a
Add a couple code optimizations
NRH-AA 4b757d8
Move logic for better performance
NRH-AA 591b5f4
Replace std::list for vector and modify datatypes
NRH-AA 089df96
Replace push_back for emplace_back and revert calculateHeuristic
NRH-AA 0d53483
Replace followedByCreature for followers and update std::array
NRH-AA b3698e4
Change addFollowedByCreature to addFollower
NRH-AA de8be87
Update method calls
NRH-AA 523821a
Add additional checks for redundant pathfinding calls
NRH-AA 7a07da2
Remove redundant check
NRH-AA fac752f
Clang Format
NRH-AA 27a16c7
Change updateFollowingCreaturePaths to updateFollowersPaths
NRH-AA 76241a5
Fix a problem in most recent path conditional
NRH-AA ef92d7a
Fix bug in pre path conditional check
NRH-AA 2abf230
Fix logic in last commit
NRH-AA 6bf2b1f
Add required nullptr check to stop crashes on creature death
NRH-AA 3fdb59f
Revert crash unrelated
NRH-AA b46ae89
Merge branch 'master' into Optimize_Pathfinding
NRH-AA 743b703
Add updateCreaturesPath
NRH-AA c7fbc3f
Convert calculate heuristic to int
NRH-AA 03d5c12
Merge branch 'master' into Optimize_Pathfinding
NRH-AA bbd357c
Convert to new code style
NRH-AA 1b8cd02
Fix memory leak in followers
NRH-AA fdc0405
Additional nodes clear for possible memory leak
NRH-AA 049a357
Fix logic for when to remove follower
NRH-AA 60a4f1f
Dynamically allocated nodes MUST be deleted.
NRH-AA 6ca6047
Remove nodes.clear()
NRH-AA 02c0b26
Revert
NRH-AA 6f2bb7a
Fix pathfinding delays.
NRH-AA f7fa383
Remove delay check in map.cpp and add in correct places.
NRH-AA 0d9fc3d
Fix typo
NRH-AA c4201c6
Check for duplicate creatures, add optimization, and remove uneeded
NRH-AA d4701a0
Modify config values for best performance while maintaining proper fo…
NRH-AA c31ebb1
Fix datatype typo
NRH-AA d0fc826
Optimize removeFollower code && update datatypes.
NRH-AA 8ef6c7f
Update CMake_Lists
NRH-AA 7aae302
Revert static const
NRH-AA a3c7744
Modify removefollower O(n2) to O(n)
NRH-AA b7d11d4
Code cleanup, Comments, remove uneeded code
NRH-AA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,27 +135,23 @@ void Creature::onThink(uint32_t interval) | |
| blockTicks = 0; | ||
| } | ||
|
|
||
| if (followCreature) { | ||
| walkUpdateTicks += interval; | ||
| if (forceUpdateFollowPath || walkUpdateTicks >= 2000) { | ||
| walkUpdateTicks = 0; | ||
| forceUpdateFollowPath = false; | ||
| isUpdatingPath = true; | ||
| } | ||
| } | ||
|
|
||
| if (isUpdatingPath) { | ||
| isUpdatingPath = false; | ||
| goToFollowCreature(); | ||
| } | ||
|
|
||
| // scripting event - onThink | ||
| const CreatureEventList& thinkEvents = getCreatureEvents(CREATURE_EVENT_THINK); | ||
| for (CreatureEvent* thinkEvent : thinkEvents) { | ||
| thinkEvent->executeOnThink(this, interval); | ||
| } | ||
| } | ||
|
|
||
| void Creature::forceUpdatePath() | ||
| { | ||
| if (!attackedCreature && !followCreature) { | ||
| return; | ||
| } | ||
|
|
||
| lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY); | ||
| g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); })); | ||
| } | ||
|
|
||
| void Creature::onAttacking(uint32_t interval) | ||
| { | ||
| if (!attackedCreature) { | ||
|
|
@@ -190,8 +186,6 @@ void Creature::onWalk() | |
| player->sendCancelMessage(ret); | ||
| player->sendCancelWalk(); | ||
| } | ||
|
|
||
| forceUpdateFollowPath = true; | ||
| } | ||
| } else { | ||
| stopEventWalk(); | ||
|
|
@@ -202,6 +196,9 @@ void Creature::onWalk() | |
| } | ||
| } | ||
|
|
||
| removeFollowers(); | ||
| updateFollowersPaths(); | ||
|
|
||
| if (cancelNextWalk) { | ||
| listWalkDir.clear(); | ||
| onWalkAborted(); | ||
|
|
@@ -212,6 +209,13 @@ void Creature::onWalk() | |
| eventWalk = 0; | ||
| addEventWalk(); | ||
| } | ||
|
|
||
| if (attackedCreature || followCreature) { | ||
| if (lastPathUpdate - OTSYS_TIME() > 0) { | ||
| g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); })); | ||
| lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void Creature::onWalk(Direction& dir) | ||
|
|
@@ -415,10 +419,6 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos | |
| } | ||
|
|
||
| if (creature == followCreature || (creature == this && followCreature)) { | ||
| if (hasFollowPath) { | ||
| isUpdatingPath = true; | ||
| } | ||
|
|
||
| if (newPos.z != oldPos.z || !canSee(followCreature->getPosition())) { | ||
| onCreatureDisappear(followCreature, false); | ||
| } | ||
|
|
@@ -733,6 +733,7 @@ void Creature::setAttackedCreature(Creature* creature) | |
| } | ||
|
|
||
| attackedCreature = creature; | ||
| creature->addFollower(this); | ||
| onAttackedCreature(attackedCreature); | ||
| attackedCreature->onAttacked(); | ||
|
|
||
|
|
@@ -763,7 +764,7 @@ void Creature::getPathSearchParams(const Creature*, FindPathParams& fpp) const | |
| { | ||
| fpp.fullPathSearch = !hasFollowPath; | ||
| fpp.clearSight = true; | ||
| fpp.maxSearchDist = 12; | ||
| fpp.maxSearchDist = Map::maxViewportX + Map::maxViewportY; | ||
| fpp.minTargetDist = 1; | ||
| fpp.maxTargetDist = 1; | ||
| } | ||
|
|
@@ -780,6 +781,8 @@ void Creature::setFollowCreature(Creature* creature) | |
| } | ||
|
|
||
| followCreature = creature; | ||
| creature->addFollower(this); | ||
| hasFollowPath = false; | ||
| onFollowCreature(creature); | ||
| } | ||
|
|
||
|
|
@@ -804,13 +807,64 @@ void Creature::onFollowCreature(const Creature*) | |
| listWalkDir.clear(); | ||
| onWalkAborted(); | ||
| } | ||
| } | ||
|
|
||
| hasFollowPath = false; | ||
| forceUpdateFollowPath = false; | ||
| isUpdatingPath = true; | ||
| void Creature::onUnfollowCreature() { hasFollowPath = false; } | ||
|
|
||
| // Pathfinding Events | ||
| bool Creature::isFollower(Creature* creature) | ||
| { | ||
| auto it = std::find(followers.begin(), followers.end(), creature); | ||
| return it != followers.end(); | ||
| } | ||
|
|
||
| void Creature::addFollower(Creature* creature) | ||
| { | ||
| if (!isFollower(creature)) { | ||
| followers.push_back(creature); | ||
| } | ||
| } | ||
|
|
||
| void Creature::onUnfollowCreature() { isUpdatingPath = false; } | ||
| void Creature::removeFollowers() | ||
| { | ||
| const Position& position = getPosition(); | ||
|
|
||
| followers.erase(std::remove_if(followers.begin(), followers.end(), | ||
| [&position](Creature* creature) { | ||
| const Position& followerPosition = creature->getPosition(); | ||
| uint16_t distance = position.getDistanceX(followerPosition) + | ||
| position.getDistanceY(followerPosition); | ||
| return distance >= Map::maxViewportX + Map::maxViewportY || | ||
| position.z != followerPosition.z; | ||
| }), | ||
| followers.end()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice change! |
||
| } | ||
|
|
||
| void Creature::updateFollowersPaths() | ||
| { | ||
| if (followers.empty()) { | ||
| return; | ||
| } | ||
|
|
||
| const Position& thisPosition = getPosition(); | ||
| for (const auto& follower : followers) { | ||
| if (follower != nullptr) { | ||
| const Position& followerPosition = follower->getPosition(); | ||
|
|
||
| if (follower->lastPathUpdate - OTSYS_TIME() > 0) { | ||
| continue; | ||
| } | ||
|
|
||
| if (thisPosition.getDistanceX(followerPosition) >= Map::maxViewportX || | ||
| thisPosition.getDistanceY(followerPosition) >= Map::maxViewportY) { | ||
| continue; | ||
| } | ||
|
|
||
| g_dispatcher.addTask(createTask([id = follower->getID()]() { g_game.updateCreatureWalk(id); })); | ||
| follower->lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| double Creature::getDamageRatio(Creature* attacker) const | ||
| { | ||
|
|
@@ -1437,7 +1491,7 @@ bool Creature::isInvisible() const | |
|
|
||
| bool Creature::getPathTo(const Position& targetPos, std::vector<Direction>& dirList, const FindPathParams& fpp) const | ||
| { | ||
| return g_game.map.getPathMatching(*this, dirList, FrozenPathingConditionCall(targetPos), fpp); | ||
| return g_game.map.getPathMatching(*this, targetPos, dirList, FrozenPathingConditionCall(targetPos), fpp); | ||
| } | ||
|
|
||
| bool Creature::getPathTo(const Position& targetPos, std::vector<Direction>& dirList, int32_t minTargetDist, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.