Add a function to wrap percentage calculations

This should help avoid issues from integer overflow (which is technically undefined behaviour)
while also allowing such issues to be addressed centrally if they still exist.
This commit is contained in:
2023-01-21 17:41:33 -05:00
parent 7b4df6edf8
commit 4c6296612d
7 changed files with 18 additions and 19 deletions

View File

@@ -2736,7 +2736,7 @@ static void run_waterfalls(short mode){ // mode 0 - town, 1 - outdoors
add_string_to_buf(" (No supplies lost.)");
else {
cTerrain& ter = univ.scenario.ter_types[coord_to_ter(x, y)];
int lost = univ.party.food * ter.flag2 / 100;
int lost = percent(univ.party.food, ter.flag2);
if(lost >= ter.flag3) {
lost = ter.flag3;
add_string_to_buf(" (Many supplies lost.)");

View File

@@ -3265,7 +3265,7 @@ void monst_basic_abil(short m_num, std::pair<eMonstAbil,uAbility> abil, iLiving*
}
break;
case eMonstAbil::PETRIFY:
i = univ.town.monst[m_num].level * abil.second.gen.strength / 100;
i = percent(univ.town.monst[m_num].level, abil.second.gen.strength);
if(pc_target != nullptr)
petrify_pc(*pc_target, i);
else if(m_target != nullptr)
@@ -3274,20 +3274,18 @@ void monst_basic_abil(short m_num, std::pair<eMonstAbil,uAbility> abil, iLiving*
case eMonstAbil::DRAIN_SP:
if(pc_target != nullptr) {
add_string_to_buf(" Drains " + pc_target->name + '.');
pc_target->cur_sp *= abil.second.gen.strength;
pc_target->cur_sp /= 100;
pc_target->cur_sp = percent(pc_target->cur_sp, abil.second.gen.strength);
} else {
m_target->spell_note(11);
// TODO: If mp < 4 it used to set monster's skill to 1. Should that be restored?
m_target->mp *= abil.second.gen.strength;
m_target->mp /= 100;
m_target->mp = percent(m_target->mp, abil.second.gen.strength);
}
break;
case eMonstAbil::DRAIN_XP:
if(pc_target != nullptr) {
i = univ.get_target_i(*target);
if(pc_target->has_abil_equip(eItemAbil::LIFE_SAVING)) break;
drain_pc(*pc_target, univ.town.monst[m_num].level * abil.second.gen.strength / 100);
drain_pc(*pc_target, percent(univ.town.monst[m_num].level, abil.second.gen.strength));
}
break;
case eMonstAbil::KILL:

View File

@@ -1429,8 +1429,7 @@ short damage_monst(cCreature& victim, short who_hit, short how_much, eDamageType
}
if(dam_type < eDamageType::SPECIAL) {
how_much *= victim.resist[dam_type];
how_much /= 100;
how_much = percent(how_much, victim.resist[dam_type]);
}
// Absorb damage?