Skip to content

Commit

Permalink
feat: min delay between conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Jan 1, 2024
1 parent 1f5b87c commit 9d33096
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ coinPacketSize = 25
coinImagesURL = "http://127.0.0.1/images/store/"
classicAttackSpeed = false
showScriptsLogInConsole = false
minDelayBetweenConditions = 0 -- time to suppress negative conditions after being affected by them (ms)
-- configure maximum value of critical imbuement
criticalChance = 10
inventoryGlowOnFiveBless = false
Expand Down
1 change: 1 addition & 0 deletions src/config/config_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ enum ConfigKey_t : uint16_t {
METRICS_ENABLE_PROMETHEUS,
METRICS_OSTREAM_INTERVAL,
METRICS_PROMETHEUS_ADDRESS,
MIN_DELAY_BETWEEN_CONDITIONS,
MIN_ELEMENTAL_RESISTANCE,
MONTH_KILLS_TO_RED,
MULTIPLIER_ATTACKONFIST,
Expand Down
1 change: 1 addition & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ bool ConfigManager::load() {
loadBoolConfig(L, CLEAN_PROTECTION_ZONES, "cleanProtectionZones", false);
loadBoolConfig(L, GLOBAL_SERVER_SAVE_SHUTDOWN, "globalServerSaveShutdown", true);
loadBoolConfig(L, PUSH_WHEN_ATTACKING, "pushWhenAttacking", false);
loadIntConfig(L, MIN_DELAY_BETWEEN_CONDITIONS, "minDelayBetweenConditions", 0);

loadBoolConfig(L, WEATHER_RAIN, "weatherRain", false);
loadBoolConfig(L, WEATHER_THUNDER, "thunderEffect", false);
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ void Combat::CombatConditionFunc(std::shared_ptr<Creature> caster, std::shared_p

// TODO: infight condition until all aggressive conditions has ended
if (target) {
target->addCombatCondition(conditionCopy);
target->addCombatCondition(conditionCopy, caster && caster->getPlayer() != nullptr);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,12 +1334,14 @@ bool Creature::setMaster(std::shared_ptr<Creature> newMaster, bool reloadCreatur
return true;
}

bool Creature::addCondition(std::shared_ptr<Condition> condition) {
bool Creature::addCondition(std::shared_ptr<Condition> condition, bool attackerPlayer /* = false*/) {
metrics::method_latency measure(__METHOD_NAME__);
if (condition == nullptr) {
return false;
}

if (isSuppress(condition->getType(), attackerPlayer)) {
return false;
}
std::shared_ptr<Condition> prevCond = getCondition(condition->getType(), condition->getId(), condition->getSubId());
if (prevCond) {
prevCond->addCondition(getCreature(), condition);
Expand All @@ -1355,11 +1357,11 @@ bool Creature::addCondition(std::shared_ptr<Condition> condition) {
return false;
}

bool Creature::addCombatCondition(std::shared_ptr<Condition> condition) {
bool Creature::addCombatCondition(std::shared_ptr<Condition> condition, bool attackerPlayer /* = false*/) {
// Caution: condition variable could be deleted after the call to addCondition
ConditionType_t type = condition->getType();

if (!addCondition(condition)) {
if (!addCondition(condition, attackerPlayer)) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/creatures/creature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ class Creature : virtual public Thing, public SharedObject {
return SPEECHBUBBLE_NONE;
}

bool addCondition(std::shared_ptr<Condition> condition);
bool addCombatCondition(std::shared_ptr<Condition> condition);
bool addCondition(std::shared_ptr<Condition> condition, bool attackerPlayer = false);
bool addCombatCondition(std::shared_ptr<Condition> condition, bool attackerPlayer = false);
void removeCondition(ConditionType_t conditionType, ConditionId_t conditionId, bool force = false);
void removeCondition(ConditionType_t type);
void removeCondition(std::shared_ptr<Condition> condition);
Expand Down
22 changes: 22 additions & 0 deletions src/creatures/creatures_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,28 @@ enum ConditionType_t : uint8_t {
CONDITION_COUNT = 39
};

// constexpr definiting suppressible conditions
constexpr bool IsConditionSuppressible(ConditionType_t condition) {
constexpr ConditionType_t suppressibleConditions[] = {
CONDITION_POISON,
CONDITION_FIRE,
CONDITION_ENERGY,
CONDITION_BLEEDING,
CONDITION_PARALYZE,
CONDITION_DROWN,
CONDITION_FREEZING,
CONDITION_CURSED,
};

for (const auto &suppressibleCondition : suppressibleConditions) {
if (condition == suppressibleCondition) {

Check warning on line 129 in src/creatures/creatures_definitions.hpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] src/creatures/creatures_definitions.hpp#L129

Consider using std::any_of algorithm instead of a raw loop.
Raw output
src/creatures/creatures_definitions.hpp:129:Consider using std::any_of algorithm instead of a raw loop.
return true;
}
}

return false;
}

enum ConditionParam_t {
CONDITION_PARAM_OWNER = 1,
CONDITION_PARAM_TICKS = 2,
Expand Down
10 changes: 9 additions & 1 deletion src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ std::shared_ptr<Item> Player::getInventoryItem(Slots_t slot) const {
return inventory[slot];
}

bool Player::isSuppress(ConditionType_t conditionType) const {
bool Player::isSuppress(ConditionType_t conditionType, bool attackerPlayer) const {
auto minDelay = g_configManager().getNumber(MIN_DELAY_BETWEEN_CONDITIONS, __FUNCTION__);
if (IsConditionSuppressible(conditionType) && checkLastConditionTimeWithin(conditionType, minDelay)) {
return true;
}

return m_conditionSuppressions[static_cast<size_t>(conditionType)];
}

Expand Down Expand Up @@ -4422,6 +4427,9 @@ void Player::onAddCondition(ConditionType_t type) {
}

void Player::onAddCombatCondition(ConditionType_t type) {
if (IsConditionSuppressible(type)) {
updateLastConditionTime(type);
}
switch (type) {
case CONDITION_POISON:
sendTextMessage(MESSAGE_FAILURE, "You are poisoned.");
Expand Down
20 changes: 20 additions & 0 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,25 @@ class Player final : public Creature, public Cylinder, public Bankable {
return lastAttackBlockType;
}

uint64_t getLastConditionTime(ConditionType_t type) const {
if (!lastConditionTime.contains(static_cast<uint8_t>(type))) {
return 0;
}
return lastConditionTime.at(static_cast<uint8_t>(type));
}

void updateLastConditionTime(ConditionType_t type) {
lastConditionTime[static_cast<uint8_t>(type)] = OTSYS_TIME();
}

bool checkLastConditionTimeWithin(ConditionType_t type, uint32_t interval) const {
if (!lastConditionTime.contains(static_cast<uint8_t>(type))) {
return false;
}
auto last = lastConditionTime.at(static_cast<uint8_t>(type));
return last > 0 && ((OTSYS_TIME() - last) < interval);
}

std::shared_ptr<Item> getWeapon(Slots_t slot, bool ignoreAmmo) const;
std::shared_ptr<Item> getWeapon(bool ignoreAmmo = false) const;
WeaponType_t getWeaponType() const;
Expand Down Expand Up @@ -2680,6 +2699,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
uint64_t experience = 0;
uint64_t manaSpent = 0;
uint64_t lastAttack = 0;
std::unordered_map<uint8_t, uint64_t> lastConditionTime;
uint64_t bankBalance = 0;
uint64_t lastQuestlogUpdate = 0;
uint64_t preyCards = 0;
Expand Down

0 comments on commit 9d33096

Please sign in to comment.