diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index 8214928dd1a..2081b0572fc 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -96,10 +96,6 @@ int32_t Creature::getWalkSize() { void Creature::onThink(uint32_t interval) { metrics::method_latency measure(__METHOD_NAME__); - if (!isMapLoaded && useCacheMap()) { - isMapLoaded = true; - updateMapCache(); - } const auto &followCreature = getFollowCreature(); const auto &master = getMaster(); @@ -290,125 +286,18 @@ void Creature::stopEventWalk() { } } -void Creature::updateMapCache() { - if (!useCacheMap()) { - return; - } - - metrics::method_latency measure(__METHOD_NAME__); - std::shared_ptr newTile; - const Position &myPos = getPosition(); - Position pos(0, 0, myPos.z); - - for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) { - for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) { - pos.x = myPos.getX() + x; - pos.y = myPos.getY() + y; - newTile = g_game().map.getTile(pos); - updateTileCache(newTile, pos); - } - } -} - -void Creature::updateTileCache(const std::shared_ptr &newTile, int32_t dx, int32_t dy) { - metrics::method_latency measure(__METHOD_NAME__); - if (std::abs(dx) <= maxWalkCacheWidth && std::abs(dy) <= maxWalkCacheHeight) { - localMapCache[maxWalkCacheHeight + dy][maxWalkCacheWidth + dx] = newTile && newTile->queryAdd(0, getCreature(), 1, FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) == RETURNVALUE_NOERROR; - } -} - -void Creature::updateTileCache(const std::shared_ptr &upTile, const Position &pos) { - const Position &myPos = getPosition(); - if (pos.z == myPos.z) { - int32_t dx = Position::getOffsetX(pos, myPos); - int32_t dy = Position::getOffsetY(pos, myPos); - updateTileCache(upTile, dx, dy); - } -} - -int32_t Creature::getWalkCache(const Position &pos) { - metrics::method_latency measure(__METHOD_NAME__); - if (!useCacheMap()) { - return 2; - } - - const Position &myPos = getPosition(); - if (myPos.z != pos.z) { - return 0; - } - - if (pos == myPos) { - return 1; - } - - int32_t dx = Position::getOffsetX(pos, myPos); - if (std::abs(dx) <= maxWalkCacheWidth) { - int32_t dy = Position::getOffsetY(pos, myPos); - if (std::abs(dy) <= maxWalkCacheHeight) { - return localMapCache[maxWalkCacheHeight + dy][maxWalkCacheWidth + dx]; - } - } - - // out of range - return 2; -} - -void Creature::onAddTileItem(const std::shared_ptr &tileItem, const Position &pos) { - if (isMapLoaded && pos.z == getPosition().z) { - updateTileCache(tileItem, pos); - } -} - -void Creature::onUpdateTileItem(const std::shared_ptr &updateTile, const Position &pos, const std::shared_ptr &, const ItemType &oldType, const std::shared_ptr &, const ItemType &newType) { - if (!isMapLoaded) { - return; - } - - if (oldType.blockSolid || oldType.blockPathFind || newType.blockPathFind || newType.blockSolid) { - if (pos.z == getPosition().z) { - updateTileCache(updateTile, pos); - } - } -} - -void Creature::onRemoveTileItem(const std::shared_ptr &updateTile, const Position &pos, const ItemType &iType, const std::shared_ptr &) { - if (!isMapLoaded) { - return; - } - - if (iType.blockSolid || iType.blockPathFind || iType.isGroundTile()) { - if (pos.z == getPosition().z) { - updateTileCache(updateTile, pos); - } - } -} - void Creature::onCreatureAppear(const std::shared_ptr &creature, bool isLogin) { metrics::method_latency measure(__METHOD_NAME__); if (creature.get() == this) { - if (useCacheMap()) { - isMapLoaded = true; - updateMapCache(); - } - if (isLogin) { setLastPosition(getPosition()); } - } else if (isMapLoaded) { - if (creature->getPosition().z == getPosition().z) { - updateTileCache(creature->getTile(), creature->getPosition()); - } } } void Creature::onRemoveCreature(const std::shared_ptr &creature, bool) { metrics::method_latency measure(__METHOD_NAME__); onCreatureDisappear(creature, true); - if (creature && creature != getCreature() && isMapLoaded) { - if (creature->getPosition().z == getPosition().z) { - updateTileCache(creature->getTile(), creature->getPosition()); - } - } // Update player from monster target list (avoid memory usage after clean) if (const auto &monster = getMonster(); monster && monster->getAttackedCreature() == creature) { @@ -526,101 +415,6 @@ void Creature::onCreatureMove(const std::shared_ptr &creature, const s if (newTile->getZoneType() != oldTile->getZoneType()) { onChangeZone(getZoneType()); } - - // update map cache - if (isMapLoaded) { - if (teleport || oldPos.z != newPos.z) { - updateMapCache(); - } else { - const Position &myPos = getPosition(); - - if (oldPos.y > newPos.y) { // north - // shift y south - for (int32_t y = mapWalkHeight - 1; --y >= 0;) { - std::ranges::copy(std::span(localMapCache[y]), localMapCache[y + 1]); - } - - // update 0 - for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) { - const auto &cacheTile = g_game().map.getTile(static_cast(myPos.getX() + x), static_cast(myPos.getY() - maxWalkCacheHeight), myPos.z); - updateTileCache(cacheTile, x, -maxWalkCacheHeight); - } - } else if (oldPos.y < newPos.y) { // south - // shift y north - for (int32_t y = 0; y <= mapWalkHeight - 2; ++y) { - std::ranges::copy(std::span(localMapCache[y + 1]), localMapCache[y]); - } - - // update mapWalkHeight - 1 - for (int32_t x = -maxWalkCacheWidth; x <= maxWalkCacheWidth; ++x) { - const auto &cacheTile = g_game().map.getTile(static_cast(myPos.getX() + x), static_cast(myPos.getY() + maxWalkCacheHeight), myPos.z); - updateTileCache(cacheTile, x, maxWalkCacheHeight); - } - } - - if (oldPos.x < newPos.x) { // east - // shift y west - int32_t starty = 0; - int32_t endy = mapWalkHeight - 1; - int32_t dy = Position::getDistanceY(oldPos, newPos); - - if (dy < 0) { - endy += dy; - } else if (dy > 0) { - starty = dy; - } - - for (int32_t y = starty; y <= endy; ++y) { - for (int32_t x = 0; x <= mapWalkWidth - 2; ++x) { - localMapCache[y][x] = localMapCache[y][x + 1]; - } - } - - // update mapWalkWidth - 1 - for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) { - const auto &cacheTile = g_game().map.getTile(myPos.x + maxWalkCacheWidth, static_cast(myPos.y + y), myPos.z); - updateTileCache(cacheTile, maxWalkCacheWidth, y); - } - } else if (oldPos.x > newPos.x) { // west - // shift y east - int32_t starty = 0; - int32_t endy = mapWalkHeight - 1; - int32_t dy = Position::getDistanceY(oldPos, newPos); - - if (dy < 0) { - endy += dy; - } else if (dy > 0) { - starty = dy; - } - - for (int32_t y = starty; y <= endy; ++y) { - for (int32_t x = mapWalkWidth - 1; --x >= 0;) { - localMapCache[y][x + 1] = localMapCache[y][x]; - } - } - - // update 0 - for (int32_t y = -maxWalkCacheHeight; y <= maxWalkCacheHeight; ++y) { - const auto &cacheTile = g_game().map.getTile(myPos.x - maxWalkCacheWidth, static_cast(myPos.y + y), myPos.z); - updateTileCache(cacheTile, -maxWalkCacheWidth, y); - } - } - - updateTileCache(oldTile, oldPos); - } - } - } else { - if (isMapLoaded) { - const Position &myPos = getPosition(); - - if (newPos.z == myPos.z) { - updateTileCache(newTile, newPos); - } - - if (oldPos.z == myPos.z) { - updateTileCache(oldTile, oldPos); - } - } } const auto &followCreature = getFollowCreature(); diff --git a/src/creatures/creature.hpp b/src/creatures/creature.hpp index 9cdd70f9fbe..fcea0a6b0f2 100644 --- a/src/creatures/creature.hpp +++ b/src/creatures/creature.hpp @@ -488,9 +488,9 @@ class Creature : virtual public Thing, public SharedObject { virtual void turnToCreature(const std::shared_ptr &creature); - void onAddTileItem(const std::shared_ptr &tile, const Position &pos); - virtual void onUpdateTileItem(const std::shared_ptr &tile, const Position &pos, const std::shared_ptr &oldItem, const ItemType &oldType, const std::shared_ptr &newItem, const ItemType &newType); - virtual void onRemoveTileItem(const std::shared_ptr &tile, const Position &pos, const ItemType &iType, const std::shared_ptr &item); + void onAddTileItem(const std::shared_ptr & /*tile*/, const Position & /*pos*/) { } + virtual void onUpdateTileItem(const std::shared_ptr &tile, const Position &pos, const std::shared_ptr &oldItem, const ItemType &oldType, const std::shared_ptr &newItem, const ItemType &newType) { } + virtual void onRemoveTileItem(const std::shared_ptr &tile, const Position &pos, const ItemType &iType, const std::shared_ptr &item) { } virtual void onCreatureAppear(const std::shared_ptr &creature, bool isLogin); virtual void onRemoveCreature(const std::shared_ptr &creature, bool isLogout); @@ -560,8 +560,6 @@ class Creature : virtual public Thing, public SharedObject { return m_tile.lock(); } - int32_t getWalkCache(const Position &pos); - const Position &getLastPosition() const { return lastPosition; } @@ -699,19 +697,10 @@ class Creature : virtual public Thing, public SharedObject { Pathfinder = 1 << 3 }; - virtual bool useCacheMap() const { - return false; - } - virtual bool isDead() const { return false; } - static constexpr int32_t mapWalkWidth = MAP_MAX_VIEW_PORT_X * 2 + 1; - static constexpr int32_t mapWalkHeight = MAP_MAX_VIEW_PORT_Y * 2 + 1; - static constexpr int32_t maxWalkCacheWidth = (mapWalkWidth - 1) / 2; - static constexpr int32_t maxWalkCacheHeight = (mapWalkHeight - 1) / 2; - Position position; CountMap damageMap; @@ -775,9 +764,7 @@ class Creature : virtual public Thing, public SharedObject { std::atomic_bool creatureCheck = false; std::atomic_bool inCheckCreaturesVector = false; - bool localMapCache[mapWalkHeight][mapWalkWidth] = { { false } }; bool isInternalRemoved = false; - bool isMapLoaded = false; bool isUpdatingPath = false; bool skillLoss = true; bool lootDrop = true; @@ -801,9 +788,6 @@ class Creature : virtual public Thing, public SharedObject { bool hasEventRegistered(CreatureEventType_t event) const; CreatureEventList getCreatureEvents(CreatureEventType_t type) const; - void updateMapCache(); - void updateTileCache(const std::shared_ptr &tile, int32_t dx, int32_t dy); - void updateTileCache(const std::shared_ptr &tile, const Position &pos); void onCreatureDisappear(const std::shared_ptr &creature, bool isLogout); virtual void doAttacking(uint32_t) { } virtual bool hasExtraSwing() { diff --git a/src/creatures/monsters/monster.cpp b/src/creatures/monsters/monster.cpp index 2e9140dcc18..7276bea19d1 100644 --- a/src/creatures/monsters/monster.cpp +++ b/src/creatures/monsters/monster.cpp @@ -967,7 +967,6 @@ void Monster::setIdle(bool idle) { if (!isIdle) { g_game().addCreatureCheck(getMonster()); - } else { onIdleStatus(); clearTargetList(); @@ -1015,10 +1014,7 @@ void Monster::onAddCondition(ConditionType_t type) { onConditionStatusChange(type); } -void Monster::onConditionStatusChange(ConditionType_t type) { - if (type == CONDITION_FIRE || type == CONDITION_ENERGY || type == CONDITION_POISON) { - updateMapCache(); - } +void Monster::onConditionStatusChange(ConditionType_t /*type*/) { updateIdleStatus(); } @@ -1518,7 +1514,6 @@ void Monster::doWalkBack(uint32_t &flags, Direction &nextDirection, bool &result } else { if (ignoreFieldDamage) { ignoreFieldDamage = false; - updateMapCache(); } int32_t distance = std::max(Position::getDistanceX(position, masterPos), Position::getDistanceY(position, masterPos)); @@ -1544,7 +1539,6 @@ void Monster::doFollowCreature(uint32_t &flags, Direction &nextDirection, bool & } else { if (ignoreFieldDamage) { ignoreFieldDamage = false; - updateMapCache(); } // target dancing const auto &attackedCreature = getAttackedCreature(); @@ -2226,10 +2220,6 @@ void Monster::setHazardSystemDefenseBoost(bool value) { bool Monster::canWalkTo(Position pos, Direction moveDirection) { pos = getNextPosition(moveDirection, pos); if (isInSpawnRange(pos)) { - if (getWalkCache(pos) == 0) { - return false; - } - const auto &tile = g_game().map.getTile(pos); if (tile && tile->getTopVisibleCreature(getMonster()) == nullptr && tile->queryAdd(0, getMonster(), 1, FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) == RETURNVALUE_NOERROR) { return true; @@ -2406,7 +2396,6 @@ void Monster::drainHealth(const std::shared_ptr &attacker, int32_t dam if (damage > 0 && randomStepping) { ignoreFieldDamage = true; - updateMapCache(); } if (isInvisible()) { diff --git a/src/creatures/monsters/monster.hpp b/src/creatures/monsters/monster.hpp index ed9a628159f..b8a6087acc1 100644 --- a/src/creatures/monsters/monster.hpp +++ b/src/creatures/monsters/monster.hpp @@ -332,13 +332,6 @@ class Monster final : public Creature { uint16_t getLookCorpse() const override; void dropLoot(const std::shared_ptr &corpse, const std::shared_ptr &lastHitCreature) override; void getPathSearchParams(const std::shared_ptr &creature, FindPathParams &fpp) override; - bool useCacheMap() const override { - // return !randomStepping; - // As the map cache is done synchronously for each movement that a monster makes, it is better to disable it, - // as the pathfinder, which is one of the resources that uses this cache the most, - // is multithreding and thus the processing cost is divided between the threads. - return false; - } friend class MonsterFunctions; friend class Map; diff --git a/src/game/game.cpp b/src/game/game.cpp index 188bfb83d4f..1f6ce9951f5 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -7430,7 +7430,6 @@ bool Game::combatChangeHealth(const std::shared_ptr &attacker, const s if (targetMonster->israndomStepping()) { targetMonster->setIgnoreFieldDamage(true); - targetMonster->updateMapCache(); } } diff --git a/src/map/map.cpp b/src/map/map.cpp index 12770ad8b34..8cb17c01752 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -640,17 +640,6 @@ std::shared_ptr Map::canWalkTo(const std::shared_ptr &creature, return nullptr; } - const int32_t walkCache = creature->getWalkCache(pos); - - if (walkCache == 0) { - return nullptr; - } - - if (walkCache == 1) { - return getTile(pos.x, pos.y, pos.z); - } - - // used for non-cached tiles const auto &tile = getTile(pos.x, pos.y, pos.z); if (creature->getTile() != tile) { if (!tile || tile->queryAdd(0, creature, 1, FLAG_PATHFINDING | FLAG_IGNOREFIELDDAMAGE) != RETURNVALUE_NOERROR) {