fix: overflow with critical hit and leech calculations #3091
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This addresses an overflow issue in the skill level calculations for critical hit and leech stats. The problem occurred when the sum of
varSkills
andskillLevel
exceeded the maximum value allowed byuint16_t
, resulting in incorrect calculations for critical damage and leech values. These stats were recently updated to float, enabling high values that unfit theuint16_t
.Root Cause
The overflow issue was caused by using
uint16_t
for variables that could receive values exceeding its maximum limit (65,535). This was particularly problematic forSKILL_CRITICAL_HIT_DAMAGE
,SKILL_LIFE_LEECH_AMOUNT
, andSKILL_MANA_LEECH_AMOUNT
, as they are now floats.Solution
The solution involves treating these skills differently in the
getSkillLevel
function to prevent overflow and ensure accurate calculations for high values. This includes adjusting the handling of the critical hit multiplier to correctly reflect percentages over 100%.Description
This change fixes the overflow issue that was causing incorrect critical damage calculations. The handling of
SKILL_CRITICAL_HIT_DAMAGE
,SKILL_LIFE_LEECH_AMOUNT
, andSKILL_MANA_LEECH_AMOUNT
has been adjusted to treat them as floats to prevent truncation when values exceed the limit ofuint16_t
. This change ensures that critical hits and leech effects are calculated correctly even for high values.Behaviour
Actual
When the sum of
varSkills
+skillLevel
exceeded theuint16_t
limit, it caused an overflow, leading to incorrect critical damage calculations. For example, a value of70000
for critical damage was being truncated to65,535
, resulting in incorrect damage scaling.Expected
Correctly handle critical and leech stats as floats, even when their values exceed the
uint16_t
limit. For example, acritical damage
value of70000
should be treated as a700%
bonus.Type of change
How Has This Been Tested
The fix was manually tested using characters with high
varSkills
values. Logs were added to confirm that critical hits and leech calculations are now performed correctly, even when values exceed the previous limit.Tests Conducted:
Manual testing with characters having high
critical hit
andleech
stats.Adjusted logs to confirm the correct calculations for critical hits and leech amounts.
Verified that the overflow issue no longer occurs with values above
65,535
.varSkills
values.Checklist