From 54a0979c26d5c8e821ad6863780787af95e97ba7 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Sun, 22 Oct 2023 22:11:17 -0300 Subject: [PATCH] use deque instead of forward_list, it's faster. --- src/creatures/combat/combat.cpp | 2 +- src/creatures/combat/condition.cpp | 4 +- src/creatures/combat/condition.hpp | 2 +- src/creatures/creature.cpp | 12 +++--- src/creatures/creature.hpp | 8 ++-- src/creatures/players/player.cpp | 2 +- src/game/game.cpp | 38 +++++++++---------- src/game/game.hpp | 4 +- .../creatures/creature_functions.cpp | 2 +- src/lua/functions/map/position_functions.cpp | 2 +- src/map/map.cpp | 4 +- src/map/map.hpp | 6 +-- src/server/network/protocol/protocolgame.cpp | 2 +- 13 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/creatures/combat/combat.cpp b/src/creatures/combat/combat.cpp index 75cec14c30f..a66689d51ff 100644 --- a/src/creatures/combat/combat.cpp +++ b/src/creatures/combat/combat.cpp @@ -898,7 +898,7 @@ void Combat::addDistanceEffect(std::shared_ptr caster, const Position void Combat::doChainEffect(const Position &origin, const Position &dest, uint8_t effect) { if (effect > 0) { - std::forward_list dirList; + std::deque dirList; FindPathParams fpp; fpp.minTargetDist = 0; fpp.maxTargetDist = 1; diff --git a/src/creatures/combat/condition.cpp b/src/creatures/combat/condition.cpp index f69488175bd..8af39b5a1a5 100644 --- a/src/creatures/combat/condition.cpp +++ b/src/creatures/combat/condition.cpp @@ -1929,7 +1929,7 @@ bool ConditionFeared::getFleeDirection(std::shared_ptr creature) { return false; } -bool ConditionFeared::getFleePath(std::shared_ptr creature, const Position &pos, std::forward_list &dirList) { +bool ConditionFeared::getFleePath(std::shared_ptr creature, const Position &pos, std::deque &dirList) { const std::vector walkSize { 15, 9, 3, 1 }; bool found = false; std::ptrdiff_t found_size = 0; @@ -2030,7 +2030,7 @@ bool ConditionFeared::startCondition(std::shared_ptr creature) { bool ConditionFeared::executeCondition(std::shared_ptr creature, int32_t interval) { Position currentPos = creature->getPosition(); - std::forward_list listDir; + std::deque listDir; g_logger().debug("[ConditionFeared::executeCondition] Executing condition, current position is {}", currentPos.toString()); diff --git a/src/creatures/combat/condition.hpp b/src/creatures/combat/condition.hpp index f8228e074ca..aaa9a96a920 100644 --- a/src/creatures/combat/condition.hpp +++ b/src/creatures/combat/condition.hpp @@ -333,7 +333,7 @@ class ConditionFeared final : public Condition { private: bool canWalkTo(std::shared_ptr creature, Position pos, Direction moveDirection) const; bool getFleeDirection(std::shared_ptr creature); - bool getFleePath(std::shared_ptr creature, const Position &pos, std::forward_list &dirList); + bool getFleePath(std::shared_ptr creature, const Position &pos, std::deque &dirList); bool getRandomDirection(std::shared_ptr creature, Position pos); bool isStuck(std::shared_ptr creature, Position pos) const; diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index 7f42e533e03..f9149d753c3 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -224,7 +224,7 @@ bool Creature::getNextStep(Direction &dir, uint32_t &) { return true; } -void Creature::startAutoWalk(const std::forward_list &listDir, bool ignoreConditions /* = false*/) { +void Creature::startAutoWalk(const std::deque &listDir, bool ignoreConditions /* = false*/) { if (!ignoreConditions && (hasCondition(CONDITION_ROOTED) || hasCondition(CONDITION_FEARED))) { return; } @@ -988,7 +988,7 @@ void Creature::goToFollowCreature() { return; // cancel pathfinder } - std::forward_list list; + std::deque list; if (self->hasFollowPath = self->getPathTo(targetPos, list, fpp)) { g_dispatcher().addEvent([self, list = std::move(list)] { self->startAutoWalk(list); }, "Creature::goToFollowCreature"); } @@ -1014,14 +1014,14 @@ void Creature::goToFollowCreature() { if (eventId != self->pathFinderEventId.load()) { return; // cancel pathfinder } - std::forward_list list; + std::deque list; if (self->hasFollowPath = self->getPathTo(targetPos, list, fpp)) { if (eventId != self->pathFinderEventId.load()) { g_logger().info("canceled"); return; // cancel pathfinder } - std::forward_list list; + std::deque list; if (self->hasFollowPath = self->getPathTo(targetPos, list, fpp)) { g_dispatcher().addEvent([self, list = std::move(list)] { self->startAutoWalk(list); }, "Creature::goToFollowCreature"); } @@ -1659,11 +1659,11 @@ bool Creature::isInvisible() const { != conditions.end(); } -bool Creature::getPathTo(const Position &targetPos, std::forward_list &dirList, const FindPathParams &fpp) { +bool Creature::getPathTo(const Position &targetPos, std::deque &dirList, const FindPathParams &fpp) { return g_game().map.getPathMatching(getCreature(), dirList, FrozenPathingConditionCall(targetPos), fpp); } -bool Creature::getPathTo(const Position &targetPos, std::forward_list &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/, bool clearSight /*= true*/, int32_t maxSearchDist /*= 7*/) { +bool Creature::getPathTo(const Position &targetPos, std::deque &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch /*= true*/, bool clearSight /*= true*/, int32_t maxSearchDist /*= 7*/) { FindPathParams fpp; fpp.fullPathSearch = fullPathSearch; fpp.maxSearchDist = maxSearchDist; diff --git a/src/creatures/creature.hpp b/src/creatures/creature.hpp index 27a833d4fc1..53dc3cef103 100644 --- a/src/creatures/creature.hpp +++ b/src/creatures/creature.hpp @@ -270,7 +270,7 @@ class Creature : virtual public Thing, public SharedObject { phmap::flat_hash_set> getZones(); // walk functions - void startAutoWalk(const std::forward_list &listDir, bool ignoreConditions = false); + void startAutoWalk(const std::deque &listDir, bool ignoreConditions = false); void addEventWalk(bool firstStep = false); void stopEventWalk(); virtual void goToFollowCreature(); @@ -528,8 +528,8 @@ class Creature : virtual public Thing, public SharedObject { double getDamageRatio(std::shared_ptr attacker) const; - bool getPathTo(const Position &targetPos, std::forward_list &dirList, const FindPathParams &fpp); - bool getPathTo(const Position &targetPos, std::forward_list &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch = true, bool clearSight = true, int32_t maxSearchDist = 7); + bool getPathTo(const Position &targetPos, std::deque &dirList, const FindPathParams &fpp); + bool getPathTo(const Position &targetPos, std::deque &dirList, int32_t minTargetDist, int32_t maxTargetDist, bool fullPathSearch = true, bool clearSight = true, int32_t maxSearchDist = 7); struct CountBlock_t { int32_t total; @@ -664,7 +664,7 @@ class Creature : virtual public Thing, public SharedObject { CreatureEventList eventsList; ConditionList conditions; - std::forward_list listWalkDir; + std::deque listWalkDir; std::weak_ptr m_tile; std::weak_ptr m_attackedCreature; diff --git a/src/creatures/players/player.cpp b/src/creatures/players/player.cpp index 7bf8f276542..374aae01c84 100644 --- a/src/creatures/players/player.cpp +++ b/src/creatures/players/player.cpp @@ -7529,7 +7529,7 @@ SoundEffect_t Player::getAttackSoundEffect() const { bool Player::canAutoWalk(const Position &toPosition, const std::function &function, uint32_t delay /* = 500*/) { if (!Position::areInRange<1, 1>(getPosition(), toPosition)) { // Check if can walk to the toPosition and send event to use function - std::forward_list listDir; + std::deque listDir; if (getPathTo(toPosition, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, &g_game(), getID(), listDir), __FUNCTION__); diff --git a/src/game/game.cpp b/src/game/game.cpp index 00b95154c48..ac1f7eb4451 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -1142,7 +1142,7 @@ void Game::playerMoveCreature(std::shared_ptr player, std::shared_ptr(movingCreatureOrigPos, player->getPosition())) { // need to walk to the creature first before moving it - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(movingCreatureOrigPos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -1439,7 +1439,7 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo if (!Position::areInRange<1, 1>(playerPos, mapFromPos)) { // need to walk to the item first before using it - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(item->getPosition(), listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -1496,7 +1496,7 @@ void Game::playerMoveItem(std::shared_ptr player, const Position &fromPo internalGetPosition(moveItem, itemPos, itemStackPos); } - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(walkPos, listDir, 0, 0, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -3018,7 +3018,7 @@ void Game::playerMove(uint32_t playerId, Direction direction) { player->setNextWalkActionTask(nullptr); player->cancelPush(); - player->startAutoWalk(std::forward_list { direction }, false); + player->startAutoWalk(std::deque { direction }, false); } void Game::forcePlayerMove(uint32_t playerId, Direction direction) { @@ -3031,7 +3031,7 @@ void Game::forcePlayerMove(uint32_t playerId, Direction direction) { player->setNextWalkActionTask(nullptr); player->cancelPush(); - player->startAutoWalk(std::forward_list { direction }, true); + player->startAutoWalk(std::deque { direction }, true); } bool Game::playerBroadcastMessage(std::shared_ptr player, const std::string &text) const { @@ -3196,7 +3196,7 @@ void Game::playerReceivePingBack(uint32_t playerId) { player->sendPingBack(); } -void Game::playerAutoWalk(uint32_t playerId, const std::forward_list &listDir) { +void Game::playerAutoWalk(uint32_t playerId, const std::deque &listDir) { std::shared_ptr player = getPlayerByID(playerId); if (!player) { return; @@ -3207,7 +3207,7 @@ void Game::playerAutoWalk(uint32_t playerId, const std::forward_list player->startAutoWalk(listDir, false); } -void Game::forcePlayerAutoWalk(uint32_t playerId, const std::forward_list &listDir) { +void Game::forcePlayerAutoWalk(uint32_t playerId, const std::deque &listDir) { std::shared_ptr player = getPlayerByID(playerId); if (!player) { return; @@ -3296,7 +3296,7 @@ void Game::playerUseItemEx(uint32_t playerId, const Position &fromPos, uint8_t f internalGetPosition(moveItem, itemPos, itemStackPos); } - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(walkToPos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -3401,7 +3401,7 @@ void Game::playerUseItem(uint32_t playerId, const Position &pos, uint8_t stackPo ReturnValue ret = g_actions().canUse(player, pos); if (ret != RETURNVALUE_NOERROR) { if (ret == RETURNVALUE_TOOFARAWAY) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -3535,7 +3535,7 @@ void Game::playerUseWithCreature(uint32_t playerId, const Position &fromPos, uin internalGetPosition(moveItem, itemPos, itemStackPos); } - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(walkToPos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -3674,7 +3674,7 @@ void Game::playerRotateItem(uint32_t playerId, const Position &pos, uint8_t stac } if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -3715,7 +3715,7 @@ void Game::playerConfigureShowOffSocket(uint32_t playerId, const Position &pos, bool isPodiumOfRenown = itemId == ITEM_PODIUM_OF_RENOWN1 || itemId == ITEM_PODIUM_OF_RENOWN2; if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, false)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); std::shared_ptr task; @@ -3762,7 +3762,7 @@ void Game::playerSetShowOffSocket(uint32_t playerId, Outfit_t &outfit, const Pos } if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, false)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerBrowseField, this, playerId, pos), "Game::playerBrowseField"); @@ -3891,7 +3891,7 @@ void Game::playerWrapableItem(uint32_t playerId, const Position &pos, uint8_t st } if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -4061,7 +4061,7 @@ void Game::playerBrowseField(uint32_t playerId, const Position &pos) { } if (!Position::areInRange<1, 1>(playerPos, pos)) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); std::shared_ptr task = createPlayerTask(400, std::bind(&Game::playerBrowseField, this, playerId, pos), "Game::playerBrowseField"); @@ -4311,7 +4311,7 @@ void Game::playerRequestTrade(uint32_t playerId, const Position &pos, uint8_t st } if (!Position::areInRange<1, 1>(tradeItemPosition, playerPosition)) { - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); @@ -4860,7 +4860,7 @@ void Game::playerQuickLoot(uint32_t playerId, const Position &pos, uint16_t item if (!autoLoot && pos.x != 0xffff) { if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { // need to walk to the corpse first before looting it - std::forward_list listDir; + std::deque listDir; if (player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); std::shared_ptr task = createPlayerTask(0, std::bind(&Game::playerQuickLoot, this, player->getID(), pos, itemId, stackPos, defaultItem, lootAllCorpses, autoLoot), "Game::playerQuickLoot"); @@ -9003,7 +9003,7 @@ void Game::playerSetMonsterPodium(uint32_t playerId, uint32_t monsterRaceId, con } if (!Position::areInRange<1, 1, 0>(pos, player->getPosition())) { - if (std::forward_list listDir; + if (std::deque listDir; player->getPathTo(pos, listDir, 0, 1, true, false)) { g_dispatcher().addEvent(std::bind_front(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); std::shared_ptr task = createPlayerTask(400, std::bind_front(&Game::playerBrowseField, this, playerId, pos), "Game::playerBrowseField"); @@ -9094,7 +9094,7 @@ void Game::playerRotatePodium(uint32_t playerId, const Position &pos, uint8_t st } if (pos.x != 0xFFFF && !Position::areInRange<1, 1, 0>(pos, player->getPosition())) { - if (std::forward_list listDir; + if (std::deque listDir; player->getPathTo(pos, listDir, 0, 1, true, true)) { g_dispatcher().addEvent(std::bind_front(&Game::playerAutoWalk, this, player->getID(), listDir), "Game::playerAutoWalk"); diff --git a/src/game/game.hpp b/src/game/game.hpp index 24807d38574..20c8ea63317 100644 --- a/src/game/game.hpp +++ b/src/game/game.hpp @@ -316,8 +316,8 @@ class Game { void playerCloseNpcChannel(uint32_t playerId); void playerReceivePing(uint32_t playerId); void playerReceivePingBack(uint32_t playerId); - void playerAutoWalk(uint32_t playerId, const std::forward_list &listDir); - void forcePlayerAutoWalk(uint32_t playerId, const std::forward_list &listDir); + void playerAutoWalk(uint32_t playerId, const std::deque &listDir); + void forcePlayerAutoWalk(uint32_t playerId, const std::deque &listDir); void playerStopAutoWalk(uint32_t playerId); void playerUseItemEx(uint32_t playerId, const Position &fromPos, uint8_t fromStackPos, uint16_t fromItemId, const Position &toPos, uint8_t toStackPos, uint16_t toItemId); void playerUseItem(uint32_t playerId, const Position &pos, uint8_t stackPos, uint8_t index, uint16_t itemId); diff --git a/src/lua/functions/creatures/creature_functions.cpp b/src/lua/functions/creatures/creature_functions.cpp index 14d86f1e291..d0001d2c0f0 100644 --- a/src/lua/functions/creatures/creature_functions.cpp +++ b/src/lua/functions/creatures/creature_functions.cpp @@ -911,7 +911,7 @@ int CreatureFunctions::luaCreatureGetPathTo(lua_State* L) { fpp.clearSight = getBoolean(L, 6, fpp.clearSight); fpp.maxSearchDist = getNumber(L, 7, fpp.maxSearchDist); - std::forward_list dirList; + std::deque dirList; if (creature->getPathTo(position, dirList, fpp)) { lua_newtable(L); diff --git a/src/lua/functions/map/position_functions.cpp b/src/lua/functions/map/position_functions.cpp index 5cee3f2d847..ea5569ba4e4 100644 --- a/src/lua/functions/map/position_functions.cpp +++ b/src/lua/functions/map/position_functions.cpp @@ -97,7 +97,7 @@ int PositionFunctions::luaPositionGetPathTo(lua_State* L) { fpp.clearSight = getBoolean(L, 6, fpp.clearSight); fpp.maxSearchDist = getNumber(L, 7, fpp.maxSearchDist); - std::forward_list dirList; + std::deque dirList; if (g_game().map.getPathMatching(pos, dirList, FrozenPathingConditionCall(position), fpp)) { lua_newtable(L); diff --git a/src/map/map.cpp b/src/map/map.cpp index 0c168b9f6cd..4eabcd9e704 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -510,11 +510,11 @@ std::shared_ptr Map::canWalkTo(const std::shared_ptr &creature, return tile; } -bool Map::getPathMatching(const std::shared_ptr &creature, std::forward_list &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp) { +bool Map::getPathMatching(const std::shared_ptr &creature, std::deque &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp) { return getPathMatching(creature, creature->getPosition(), dirList, pathCondition, fpp); } -bool Map::getPathMatching(const std::shared_ptr &creature, const Position &startPos, std::forward_list &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp) { +bool Map::getPathMatching(const std::shared_ptr &creature, const Position &startPos, std::deque &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp) { static int_fast32_t allNeighbors[8][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 } }; diff --git a/src/map/map.hpp b/src/map/map.hpp index 0ec96922910..d9c1a469ce0 100644 --- a/src/map/map.hpp +++ b/src/map/map.hpp @@ -123,9 +123,9 @@ class Map : protected MapCache { std::shared_ptr canWalkTo(const std::shared_ptr &creature, const Position &pos); - bool getPathMatching(const std::shared_ptr &creature, std::forward_list &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp); + bool getPathMatching(const std::shared_ptr &creature, std::deque &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp); - bool getPathMatching(const Position &startPos, std::forward_list &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp) { + bool getPathMatching(const Position &startPos, std::deque &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp) { return getPathMatching(nullptr, startPos, dirList, pathCondition, fpp); } @@ -147,7 +147,7 @@ class Map : protected MapCache { Houses housesCustomMaps[50]; private: - bool getPathMatching(const std::shared_ptr &creature, const Position &startPos, std::forward_list &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp); + bool getPathMatching(const std::shared_ptr &creature, const Position &startPos, std::deque &dirList, const FrozenPathingConditionCall &pathCondition, const FindPathParams &fpp); /** * Set a single tile. diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 53aedeb1c88..a0c34200c00 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -1555,7 +1555,7 @@ void ProtocolGame::parseAutoWalk(NetworkMessage &msg) { msg.skipBytes(numdirs); - std::forward_list path; + std::deque path; for (uint8_t i = 0; i < numdirs; ++i) { uint8_t rawdir = msg.getPreviousByte(); switch (rawdir) {