Skip to content
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

improve: removed cache map dead code #3086

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 0 additions & 206 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -290,125 +286,18 @@ void Creature::stopEventWalk() {
}
}

void Creature::updateMapCache() {
if (!useCacheMap()) {
return;
}

metrics::method_latency measure(__METHOD_NAME__);
std::shared_ptr<Tile> 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<Tile> &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<Tile> &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<Tile> &tileItem, const Position &pos) {
if (isMapLoaded && pos.z == getPosition().z) {
updateTileCache(tileItem, pos);
}
}

void Creature::onUpdateTileItem(const std::shared_ptr<Tile> &updateTile, const Position &pos, const std::shared_ptr<Item> &, const ItemType &oldType, const std::shared_ptr<Item> &, 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<Tile> &updateTile, const Position &pos, const ItemType &iType, const std::shared_ptr<Item> &) {
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> &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> &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) {
Expand Down Expand Up @@ -526,101 +415,6 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &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<uint16_t>(myPos.getX() + x), static_cast<uint16_t>(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<uint16_t>(myPos.getX() + x), static_cast<uint16_t>(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<uint16_t>(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<uint16_t>(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();
Expand Down
22 changes: 3 additions & 19 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ class Creature : virtual public Thing, public SharedObject {

virtual void turnToCreature(const std::shared_ptr<Creature> &creature);

void onAddTileItem(const std::shared_ptr<Tile> &tile, const Position &pos);
virtual void onUpdateTileItem(const std::shared_ptr<Tile> &tile, const Position &pos, const std::shared_ptr<Item> &oldItem, const ItemType &oldType, const std::shared_ptr<Item> &newItem, const ItemType &newType);
virtual void onRemoveTileItem(const std::shared_ptr<Tile> &tile, const Position &pos, const ItemType &iType, const std::shared_ptr<Item> &item);
void onAddTileItem(const std::shared_ptr<Tile> & /*tile*/, const Position & /*pos*/) { }
virtual void onUpdateTileItem(const std::shared_ptr<Tile> &tile, const Position &pos, const std::shared_ptr<Item> &oldItem, const ItemType &oldType, const std::shared_ptr<Item> &newItem, const ItemType &newType) { }
virtual void onRemoveTileItem(const std::shared_ptr<Tile> &tile, const Position &pos, const ItemType &iType, const std::shared_ptr<Item> &item) { }

virtual void onCreatureAppear(const std::shared_ptr<Creature> &creature, bool isLogin);
virtual void onRemoveCreature(const std::shared_ptr<Creature> &creature, bool isLogout);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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> &tile, int32_t dx, int32_t dy);
void updateTileCache(const std::shared_ptr<Tile> &tile, const Position &pos);
void onCreatureDisappear(const std::shared_ptr<Creature> &creature, bool isLogout);
virtual void doAttacking(uint32_t) { }
virtual bool hasExtraSwing() {
Expand Down
13 changes: 1 addition & 12 deletions src/creatures/monsters/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,6 @@ void Monster::setIdle(bool idle) {

if (!isIdle) {
g_game().addCreatureCheck(getMonster());

} else {
onIdleStatus();
clearTargetList();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<int32_t>(Position::getDistanceX(position, masterPos), Position::getDistanceY(position, masterPos));
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2406,7 +2396,6 @@ void Monster::drainHealth(const std::shared_ptr<Creature> &attacker, int32_t dam

if (damage > 0 && randomStepping) {
ignoreFieldDamage = true;
updateMapCache();
}

if (isInvisible()) {
Expand Down
7 changes: 0 additions & 7 deletions src/creatures/monsters/monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,6 @@ class Monster final : public Creature {
uint16_t getLookCorpse() const override;
void dropLoot(const std::shared_ptr<Container> &corpse, const std::shared_ptr<Creature> &lastHitCreature) override;
void getPathSearchParams(const std::shared_ptr<Creature> &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;
Expand Down
1 change: 0 additions & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7429,7 +7429,6 @@ bool Game::combatChangeHealth(const std::shared_ptr<Creature> &attacker, const s

if (targetMonster->israndomStepping()) {
targetMonster->setIgnoreFieldDamage(true);
targetMonster->updateMapCache();
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,6 @@ std::shared_ptr<Tile> Map::canWalkTo(const std::shared_ptr<Creature> &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) {
Expand Down
Loading