Skip to content
Open
7 changes: 0 additions & 7 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ replaceKickOnLogin = true
maxPacketsPerSecond = 25
enableTwoFactorAuth = true

-- Pathfinding
-- pathfindingInterval handles how often paths are force drawn
-- pathfindingDelay delays any recently drawn paths from drawing again
-- pathfindingDelay does not delay pathfindingInterval
pathfindingInterval = 200
pathfindingDelay = 300

-- Deaths
-- NOTE: Leave deathLosePercent as -1 if you want to use the default
-- death penalty formula. For the old formula, set it to 10. For
Expand Down
2 changes: 0 additions & 2 deletions src/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ bool ConfigManager::load()
integer[QUEST_TRACKER_PREMIUM_LIMIT] = getGlobalNumber(L, "questTrackerPremiumLimit", 15);
integer[STAMINA_REGEN_MINUTE] = getGlobalNumber(L, "timeToRegenMinuteStamina", 3 * 60);
integer[STAMINA_REGEN_PREMIUM] = getGlobalNumber(L, "timeToRegenMinutePremiumStamina", 6 * 60);
integer[PATHFINDING_INTERVAL] = getGlobalNumber(L, "pathfindingInterval", 200);
integer[PATHFINDING_DELAY] = getGlobalNumber(L, "pathfindingDelay", 300);

expStages = loadXMLStages();
if (expStages.empty()) {
Expand Down
2 changes: 0 additions & 2 deletions src/configmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ enum integer_config_t
QUEST_TRACKER_PREMIUM_LIMIT,
STAMINA_REGEN_MINUTE,
STAMINA_REGEN_PREMIUM,
PATHFINDING_INTERVAL,
PATHFINDING_DELAY,

LAST_INTEGER_CONFIG /* this must be the last one */
};
Expand Down
58 changes: 34 additions & 24 deletions src/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ void Creature::onThink(uint32_t interval)
tfs::events::creature::onThink(asCreature(), interval);
}

void Creature::forceUpdatePath()
void Creature::updateFollowPath()
{
if (attackedCreature.expired() && followCreature.expired()) {
if (eventFollowPath != 0) {
g_scheduler.stopEvent(eventFollowPath);
eventFollowPath = 0;
}

if (followCreature.expired()) {
return;
}

lastPathUpdate = OTSYS_TIME() + getNumber(ConfigManager::PATHFINDING_DELAY);
g_dispatcher.addTask(createTask([id = getID()]() { g_game.updateCreatureWalk(id); }));
eventFollowPath = g_scheduler.addEvent(
createSchedulerTask(EVENT_CHECK_CREATURE_INTERVAL, [id = getID()]() { g_game.updateCreatureWalk(id); }));
}

void Creature::onIdleStatus()
Expand Down Expand Up @@ -166,8 +171,6 @@ void Creature::onWalk()
}
}

updateFollowersPaths();

if (cancelNextWalk) {
listWalkDir.clear();
onWalkAborted();
Expand All @@ -179,12 +182,7 @@ void Creature::onWalk()
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);
}
}
updateFollowersPaths();
}

void Creature::onWalk(Direction& dir)
Expand Down Expand Up @@ -315,7 +313,8 @@ void Creature::updateFollowCreaturePath(FindPathParams& fpp)
{
listWalkDir.clear();

if (const auto& followCreature = getFollowCreature(); getPathTo(followCreature->getPosition(), listWalkDir, fpp)) {
if (const auto& followCreature = getFollowCreature();
followCreature && getPathTo(followCreature->getPosition(), listWalkDir, fpp)) {
hasFollowPath = true;
startAutoWalk();
} else {
Expand All @@ -341,6 +340,11 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature>& creature, const s
const Position& newPos, const std::shared_ptr<const Tile>& oldTile,
const Position& oldPos, bool teleport)
{
if (const auto& followCreature = getFollowCreature();
(creature == followCreature || (creature.get() == this && followCreature))) {
updateFollowPath();
}

if (creature.get() == this) {
lastStep = OTSYS_TIME();
lastStepCost = 1;
Expand Down Expand Up @@ -688,15 +692,12 @@ void Creature::setAttackedCreature(const std::shared_ptr<Creature>& creature)
}

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 +741,29 @@ void Creature::setFollowCreature(const std::shared_ptr<Creature>& creature)
return;
}

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

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

updateFollowPath();
}

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

if (eventFollowPath != 0) {
g_scheduler.stopEvent(eventFollowPath);
eventFollowPath = 0;
}

followCreature.reset();
onUnfollowCreature();
}
Expand Down Expand Up @@ -790,12 +805,7 @@ void Creature::updateFollowersPaths()
std::ranges::to<decltype(followers)>();

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
5 changes: 3 additions & 2 deletions src/creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ class Creature : public Thing
{
return tfs::owner_equal(followCreature, creature);
}
void completeEventFollowWalk() { eventFollowPath = 0; }

// follow events
virtual void onFollowCreature(const std::shared_ptr<const Creature>&);
Expand Down Expand Up @@ -317,7 +318,7 @@ class Creature : public Thing
virtual void onThink(uint32_t interval);
virtual void onAttacking(uint32_t) {}

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

Expand Down Expand Up @@ -393,7 +394,7 @@ class Creature : public Thing
std::vector<Direction> listWalkDir;

uint64_t lastStep = 0;
int64_t lastPathUpdate = 0;
uint32_t eventFollowPath = 0;
uint32_t id = 0;
uint32_t scriptEventsBitField = 0;
uint32_t eventWalk = 0;
Expand Down
19 changes: 1 addition & 18 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ void Game::start(ServiceManager* manager)
serviceManager = manager;

g_scheduler.addEvent(createSchedulerTask(EVENT_CREATURE_THINK_INTERVAL, [this]() { checkCreatures(0); }));
g_scheduler.addEvent(
createSchedulerTask(getNumber(ConfigManager::PATHFINDING_INTERVAL), [this]() { updateCreaturesPath(0); }));
g_scheduler.addEvent(createSchedulerTask(EVENT_DECAYINTERVAL, [this]() { checkDecay(); }));
}

Expand Down Expand Up @@ -3182,8 +3180,6 @@ void Game::playerSetAttackedCreature(uint32_t playerId, uint32_t creatureId)
}

player->setAttackedCreature(attackCreature);

g_dispatcher.addTask([this, id = player->getID()]() { updateCreatureWalk(id); });
}

void Game::playerFollowCreature(uint32_t playerId, uint32_t creatureId)
Expand All @@ -3200,8 +3196,6 @@ void Game::playerFollowCreature(uint32_t playerId, uint32_t creatureId)
} else {
player->removeFollowCreature();
}

g_dispatcher.addTask([this, id = player->getID()]() { updateCreatureWalk(id); });
}

void Game::playerSetFightModes(uint32_t playerId, fightMode_t fightMode, bool chaseMode, bool secureMode)
Expand Down Expand Up @@ -3726,6 +3720,7 @@ void Game::updateCreatureWalk(uint32_t creatureId)
if (const auto& creature = getCreatureByID(creatureId)) {
if (!creature->isDead()) {
creature->goToFollowCreature();
creature->completeEventFollowWalk();
}
}
}
Expand Down Expand Up @@ -3789,18 +3784,6 @@ void Game::checkCreatures(size_t index)
cleanup();
}

void Game::updateCreaturesPath(size_t index)
{
g_scheduler.addEvent(createSchedulerTask(getNumber(ConfigManager::PATHFINDING_INTERVAL),
[=, this]() { updateCreaturesPath((index + 1) % EVENT_CREATURECOUNT); }));

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

void Game::changeSpeed(const std::shared_ptr<Creature>& creature, int32_t varSpeedDelta)
{
int32_t varSpeed = creature->getSpeed() - creature->getBaseSpeed();
Expand Down
1 change: 0 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ class Game
void updateCreatureWalk(uint32_t creatureId);
void checkCreatureAttack(uint32_t creatureId);
void checkCreatures(size_t index);
void updateCreaturesPath(size_t index);

bool combatBlockHit(CombatDamage& damage, const std::shared_ptr<Creature>& attacker,
const std::shared_ptr<Creature>& target, bool checkDefense, bool checkArmor, bool field,
Expand Down
5 changes: 0 additions & 5 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,11 +1267,6 @@ void Player::onCreatureMove(const std::shared_ptr<Creature>& creature, const std
{
Creature::onCreatureMove(creature, newTile, newPos, oldTile, oldPos, teleport);

if (const auto& followCreature = getFollowCreature();
hasFollowPath && (creature == followCreature || (creature.get() == this && followCreature))) {
g_dispatcher.addTask([id = getID()]() { g_game.updateCreatureWalk(id); });
}

if (creature.get() != this) {
return;
}
Expand Down
Loading