Skip to content
Open
41 changes: 22 additions & 19 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,18 @@
tfs::events::creature::onThink(asCreature(), interval);
}

void Creature::forceUpdatePath()
void Creature::updateFollowPath(bool force)
{
if (attackedCreature.expired() && followCreature.expired()) {
if (followCreature.expired()) {
return;
}

if (!force) {
if (lastPathUpdate >= OTSYS_TIME()) {
return;
}
}

lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY);
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
}
Expand Down Expand Up @@ -179,12 +185,7 @@
addEventWalk();
}

if (!attackedCreature.expired() || !followCreature.expired()) {
if (lastPathUpdate < OTSYS_TIME()) {
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY);
}
}
updateFollowPath();
}

void Creature::onWalk(Direction& dir)
Expand Down Expand Up @@ -688,15 +689,12 @@
}

attackedCreature = creature;
creature->addFollower(asCreature());
onAttackedCreature(creature);

if (const auto& player = creature->asPlayer()) {
player->addInFightTicks();
}

forceUpdatePath();

for (const auto& summon : summons | tfs::views::lock_weak_ptrs) {
summon->setAttackedCreature(creature);
}
Expand Down Expand Up @@ -740,15 +738,24 @@
return;
}

followCreature = creature;
if (const auto& oldFollow = getFollowCreature()) {
oldFollow->removeFollower(asCreature());
}
creature->addFollower(asCreature());

followCreature = creature;
hasFollowPath = false;
onFollowCreature(creature);
forceUpdatePath();

updateFollowPath(true);
}

void Creature::removeFollowCreature()
{
if (const auto& oldFollow = getFollowCreature()) {
oldFollow->removeFollower(asCreature());
}

followCreature.reset();
onUnfollowCreature();
}
Expand Down Expand Up @@ -789,13 +796,9 @@
}) |
std::ranges::to<decltype(followers)>();

const auto size = followers.size();

Check failure on line 799 in src/creature.cpp

View workflow job for this annotation

GitHub Actions / ubuntu

unused variable ‘size’ [-Werror=unused-variable]

Check failure on line 799 in src/creature.cpp

View workflow job for this annotation

GitHub Actions / test

unused variable ‘size’ [-Werror=unused-variable]
for (const auto& follower : followers | tfs::views::lock_weak_ptrs) {
if (follower->lastPathUpdate < OTSYS_TIME()) {
continue;
}

g_dispatcher.addTask(createTask([id = follower->getID()]() { g_game.updateCreatureWalk(id); }));
follower->lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY);
follower->updateFollowPath();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class Creature : public Thing
virtual void onThink(uint32_t interval);
virtual void onAttacking(uint32_t) {}

virtual void forceUpdatePath();
virtual void updateFollowPath(bool force = false);
virtual void onWalk();
virtual bool getNextStep(Direction& dir, uint32_t& flags);

Expand Down
2 changes: 1 addition & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3796,7 +3796,7 @@ void Game::updateCreaturesPath(size_t index)

for (const auto& creature : checkCreatureLists[index] | tfs::views::lock_weak_ptrs) {
if (!creature->isDead()) {
creature->forceUpdatePath();
creature->updateFollowPath(true);
}
}
}
Expand Down
Loading