Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 2, 2023
1 parent 6dc0e9f commit 7f8a8ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
18 changes: 7 additions & 11 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,9 @@ void Creature::onCreatureMove(const std::shared_ptr<Creature> &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();
Expand Down Expand Up @@ -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<uint16_t>(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;
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -809,8 +811,12 @@ class Creature : virtual public Thing, public SharedObject {

void updateCalculatedStepSpeed() {
const auto stepSpeed = getStepSpeed();
walk.calculatedStepSpeed = std::max<uint32_t>(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<uint16_t>(std::max(formula, 1.));
}

walk.recache();
}
};

0 comments on commit 7f8a8ca

Please sign in to comment.