Skip to content

Commit

Permalink
cleanup: Game::checkCreatures (#3020)
Browse files Browse the repository at this point in the history
The loop by iterator has been replaced by erase_if, as it is visually
cleaner and more intuitive. In terms of performance, there was not much
gain, as both use object reallocation, perhaps a slight improvement, as
erase_if uses std::move to reallocate the last object.

A dead code that is never called has also been removed, as this code is
already called by Creature::changeHealth on line 892
  • Loading branch information
mehah authored Oct 29, 2024
1 parent e68cb87 commit 9c4b134
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ class Creature : virtual public Thing, public SharedObject {
int32_t getHealth() const {
return health;
}

bool isAlive() const {
return !isDead();
}

virtual int32_t getMaxHealth() const {
return healthMax;
}
Expand Down
31 changes: 10 additions & 21 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6395,28 +6395,17 @@ void Game::checkCreatures() {
metrics::method_latency measure(__METHOD_NAME__);
static size_t index = 0;

auto &checkCreatureList = checkCreatureLists[index];
size_t it = 0, end = checkCreatureList.size();
while (it < end) {
auto creature = checkCreatureList[it];
if (creature && creature->creatureCheck) {
if (creature->getHealth() > 0) {
creature->onThink(EVENT_CREATURE_THINK_INTERVAL);
creature->onAttacking(EVENT_CREATURE_THINK_INTERVAL);
creature->executeConditions(EVENT_CREATURE_THINK_INTERVAL);
} else {
afterCreatureZoneChange(creature, creature->getZones(), {});
creature->onDeath();
}
++it;
} else {
creature->inCheckCreaturesVector = false;

checkCreatureList[it] = checkCreatureList.back();
checkCreatureList.pop_back();
--end;
std::erase_if(checkCreatureLists[index], [this](const std::shared_ptr<Creature> &creature) {
if (creature->creatureCheck && creature->isAlive()) {
creature->onThink(EVENT_CREATURE_THINK_INTERVAL);
creature->onAttacking(EVENT_CREATURE_THINK_INTERVAL);
creature->executeConditions(EVENT_CREATURE_THINK_INTERVAL);
return false;
}
}

creature->inCheckCreaturesVector = false;
return true;
});

index = (index + 1) % EVENT_CREATURECOUNT;
}
Expand Down

0 comments on commit 9c4b134

Please sign in to comment.