DRY overflow-aware short addition
This commit is contained in:
@@ -1483,9 +1483,7 @@ short damage_monst(cCreature& victim, short who_hit, short how_much, eDamageType
|
||||
// Absorb damage?
|
||||
if((dam_type == eDamageType::FIRE || dam_type == eDamageType::MAGIC || dam_type == eDamageType::COLD)
|
||||
&& victim.abil[eMonstAbil::ABSORB_SPELLS].active && get_ran(1,1,1000) <= victim.abil[eMonstAbil::ABSORB_SPELLS].special.extra1) {
|
||||
if(32767 - victim.health > how_much)
|
||||
victim.health = 32767;
|
||||
else victim.health += how_much;
|
||||
add_check_overflow(victim.health, how_much);
|
||||
ASB(" Magic absorbed.");
|
||||
return false;
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
#include <SFML/System/Time.hpp>
|
||||
#include <random>
|
||||
|
||||
@@ -46,3 +47,9 @@ template<typename T>
|
||||
inline T sgn(T val) {
|
||||
return val == 0 ? 0 : std::copysign(1, val);
|
||||
}
|
||||
|
||||
inline void add_check_overflow(short& val, int how_much) {
|
||||
if(SHRT_MAX - val > how_much)
|
||||
val = SHRT_MAX;
|
||||
else val += how_much;
|
||||
}
|
@@ -302,9 +302,7 @@ int cCreature::magic_adjust(int how_much) {
|
||||
if(how_much <= 0) return how_much;
|
||||
if(abil[eMonstAbil::ABSORB_SPELLS].active && get_ran(1,1,1000) <= abil[eMonstAbil::ABSORB_SPELLS].special.extra1) {
|
||||
int gain = abil[eMonstAbil::ABSORB_SPELLS].special.extra2;
|
||||
if(32767 - health > gain)
|
||||
health = 32767;
|
||||
else health += gain;
|
||||
add_check_overflow(health, gain);
|
||||
return 0;
|
||||
}
|
||||
// TODO: Magic resistance status effect?
|
||||
|
Reference in New Issue
Block a user