From 7f8a8ca73ac7d6222a77918cf5a47b2461d78497 Mon Sep 17 00:00:00 2001 From: Renato Machado Date: Thu, 2 Nov 2023 17:17:10 -0300 Subject: [PATCH] improve --- src/creatures/creature.cpp | 18 +++++++----------- src/creatures/creature.hpp | 14 ++++++++++---- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/creatures/creature.cpp b/src/creatures/creature.cpp index c58b7bc54ca..60c0c802832 100644 --- a/src/creatures/creature.cpp +++ b/src/creatures/creature.cpp @@ -464,11 +464,9 @@ void Creature::onCreatureMove(const std::shared_ptr &creature, const s if (!teleport) { if (oldPos.z != newPos.z) { - // floor change extra cost - lastStepCost = 2; + lastStepCost = WALK_FLOOR_CHANGE_EXTRA_COST; } else if (Position::getDistanceX(newPos, oldPos) >= 1 && Position::getDistanceY(newPos, oldPos) >= 1) { - // diagonal extra cost - lastStepCost = 3; + lastStepCost = WALK_DIAGONAL_EXTRA_COST; } } else { stopEventWalk(); @@ -1415,18 +1413,16 @@ uint16_t Creature::getStepDuration(Direction dir) { } if (walk.needRecache()) { - double duration = std::floor(1000 * walk.groundSpeed / walk.calculatedStepSpeed); - walk.duration = std::ceil(duration / SERVER_BEAT) * SERVER_BEAT; + auto duration = std::floor(1000 * walk.groundSpeed / walk.calculatedStepSpeed); + walk.duration = static_cast(std::ceil(duration / SERVER_BEAT) * SERVER_BEAT); } auto duration = walk.duration; if ((dir & DIRECTION_DIAGONAL_MASK) != 0) { - duration *= 3; - } - - if (const auto &monster = getMonster()) { + duration *= WALK_DIAGONAL_EXTRA_COST; + }else if (const auto &monster = getMonster()) { if (monster->isTargetNearby() && !monster->isFleeing() && !monster->getMaster()) { - duration *= 2; + duration *= WALK_TARGET_NEARBY_EXTRA_COST; } } diff --git a/src/creatures/creature.hpp b/src/creatures/creature.hpp index 5e65bb6c870..06e6e171c9a 100644 --- a/src/creatures/creature.hpp +++ b/src/creatures/creature.hpp @@ -30,7 +30,9 @@ class Item; class Tile; class Zone; -static constexpr uint8_t FLOOR_CHANGE_EXTRA_COST = 2; +static constexpr uint8_t WALK_TARGET_NEARBY_EXTRA_COST = 2; +static constexpr uint8_t WALK_FLOOR_CHANGE_EXTRA_COST = 2; +static constexpr uint8_t WALK_DIAGONAL_EXTRA_COST = 3; static constexpr int32_t EVENT_CREATURECOUNT = 10; static constexpr int32_t EVENT_CREATURE_THINK_INTERVAL = 1000; static constexpr int32_t EVENT_CHECK_CREATURE_INTERVAL = (EVENT_CREATURE_THINK_INTERVAL / EVENT_CREATURECOUNT); @@ -156,7 +158,7 @@ class Creature : virtual public Thing, public SharedObject { int64_t getTimeSinceLastMove() const; int64_t getEventStepTicks(bool onlyDelay = false); - uint16_t getStepDuration(); + uint16_t getStepDuration(Direction dir = DIRECTION_NONE); virtual uint16_t getStepSpeed() const { return getSpeed(); } @@ -809,8 +811,12 @@ class Creature : virtual public Thing, public SharedObject { void updateCalculatedStepSpeed() { const auto stepSpeed = getStepSpeed(); - walk.calculatedStepSpeed = std::max(floor((Creature::speedA * log(stepSpeed + Creature::speedB) + Creature::speedC) + .5f), 1); - walk.calculatedStepSpeed = (stepSpeed > -Creature::speedB) ? walk.calculatedStepSpeed : 1; + walk.calculatedStepSpeed = 1; + if (stepSpeed > -Creature::speedB) { + const auto formula = std::floor((Creature::speedA * log(stepSpeed + Creature::speedB) + Creature::speedC) + .5); + walk.calculatedStepSpeed = static_cast(std::max(formula, 1.)); + } + walk.recache(); } };