Skip to content

Commit

Permalink
fix: division by zero in spell cooldown calculation (#1988)
Browse files Browse the repository at this point in the history
Fixes potential division by zero error in the spell cooldown calculation. We have introduced a safety check to ensure rateCooldown is not zero or extremely close to zero before performing the division.
In cases where rateCooldown is too small; it is reset to a minimal safe value to maintain functionality and avoid arithmetic exceptions. This change ensures stable and error-free operation even with small rateCooldown values.

Resolves #1920 
Resolves #1977
  • Loading branch information
dudantas authored Dec 7, 2023
1 parent 953742f commit 3838427
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/creatures/combat/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,19 @@ void Spell::setWheelOfDestinyBoost(WheelSpellBoost_t boost, WheelSpellGrade_t gr
void Spell::applyCooldownConditions(std::shared_ptr<Player> player) const {
WheelSpellGrade_t spellGrade = player->wheel()->getSpellUpgrade(getName());
bool isUpgraded = getWheelOfDestinyUpgraded() && static_cast<uint8_t>(spellGrade) > 0;
auto rate_cooldown = (int32_t)g_configManager().getFloat(RATE_SPELL_COOLDOWN, __FUNCTION__);
// Safety check to prevent division by zero
auto rateCooldown = g_configManager().getFloat(RATE_SPELL_COOLDOWN, __FUNCTION__);
if (std::abs(rateCooldown) < std::numeric_limits<float>::epsilon()) {
rateCooldown = 0.1; // Safe minimum value
}

if (cooldown > 0) {
int32_t spellCooldown = cooldown;
if (isUpgraded) {
spellCooldown -= getWheelOfDestinyBoost(WheelSpellBoost_t::COOLDOWN, spellGrade);
}
if (spellCooldown > 0) {
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, spellCooldown / rate_cooldown, 0, false, spellId);
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLCOOLDOWN, spellCooldown / rateCooldown, 0, false, spellId);
player->addCondition(condition);
}
}
Expand All @@ -627,7 +632,7 @@ void Spell::applyCooldownConditions(std::shared_ptr<Player> player) const {
spellGroupCooldown -= getWheelOfDestinyBoost(WheelSpellBoost_t::GROUP_COOLDOWN, spellGrade);
}
if (spellGroupCooldown > 0) {
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellGroupCooldown / rate_cooldown, 0, false, group);
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellGroupCooldown / rateCooldown, 0, false, group);
player->addCondition(condition);
}
}
Expand All @@ -638,7 +643,7 @@ void Spell::applyCooldownConditions(std::shared_ptr<Player> player) const {
spellSecondaryGroupCooldown -= getWheelOfDestinyBoost(WheelSpellBoost_t::SECONDARY_GROUP_COOLDOWN, spellGrade);
}
if (spellSecondaryGroupCooldown > 0) {
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellSecondaryGroupCooldown / rate_cooldown, 0, false, secondaryGroup);
std::shared_ptr<Condition> condition = Condition::createCondition(CONDITIONID_DEFAULT, CONDITION_SPELLGROUPCOOLDOWN, spellSecondaryGroupCooldown / rateCooldown, 0, false, secondaryGroup);
player->addCondition(condition);
}
}
Expand Down

0 comments on commit 3838427

Please sign in to comment.