From 79206d92d5390d82a9a79cd6018b575de3aafea0 Mon Sep 17 00:00:00 2001 From: Jyrki Vesterinen Date: Sun, 23 Sep 2018 16:52:24 +0300 Subject: [PATCH] Fix: Chance to hit wasn't protected from becoming negative Manual merge of pull request #3550. Credit to @newfrenchy83. --- src/actions/attack.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/actions/attack.cpp b/src/actions/attack.cpp index fb7e4f22b304..b6f544bf95c3 100644 --- a/src/actions/attack.cpp +++ b/src/actions/attack.cpp @@ -157,21 +157,21 @@ battle_context_unit_stats::battle_context_unit_stats(const unit& u, } // Compute chance to hit. - chance_to_hit = opp.defense_modifier(resources::gameboard->map().get_terrain(opp_loc)) + weapon->accuracy() + signed int cth = opp.defense_modifier(resources::gameboard->map().get_terrain(opp_loc)) + weapon->accuracy() - (opp_weapon ? opp_weapon->parry() : 0); - if(chance_to_hit > 100) { - chance_to_hit = 100; - } + cth = utils::clamp(cth, 0, 100); unit_ability_list cth_specials = weapon->get_specials("chance_to_hit"); - unit_abilities::effect cth_effects(cth_specials, chance_to_hit, backstab_pos); - chance_to_hit = cth_effects.get_composite_value(); + unit_abilities::effect cth_effects(cth_specials, cth, backstab_pos); + cth = cth_effects.get_composite_value(); if(opp.get_state("invulnerable")) { - chance_to_hit = 0; + cth = 0; } + chance_to_hit = utils::clamp(cth, 0, 100); + // Compute base damage done with the weapon. int base_damage = weapon->modified_damage(backstab_pos); @@ -306,13 +306,13 @@ battle_context_unit_stats::battle_context_unit_stats(const unit_type* u_type, } signed int cth = 100 - opp_terrain_defense + weapon->accuracy() - (opp_weapon ? opp_weapon->parry() : 0); - cth = std::min(100, cth); - cth = std::max(0, cth); - chance_to_hit = cth; + cth = utils::clamp(cth, 0, 100); unit_ability_list cth_specials = weapon->get_specials("chance_to_hit"); - unit_abilities::effect cth_effects(cth_specials, chance_to_hit, backstab_pos); - chance_to_hit = cth_effects.get_composite_value(); + unit_abilities::effect cth_effects(cth_specials, cth, backstab_pos); + cth = cth_effects.get_composite_value(); + + chance_to_hit = utils::clamp(cth, 0, 100); int base_damage = weapon->modified_damage(backstab_pos); int damage_multiplier = 100;