From 518b8c627878ba0eb9f9de9a76333047daf8e7ac Mon Sep 17 00:00:00 2001 From: newfrenchy83 Date: Fri, 18 Oct 2024 15:23:52 +0200 Subject: [PATCH] modify code for what one final value was rounded. --- src/actions/attack.cpp | 14 +++++++------- src/reports.cpp | 4 ++-- src/units/abilities.cpp | 17 ++++++++--------- src/units/abilities.hpp | 4 ++-- src/units/attack_type.hpp | 2 +- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/actions/attack.cpp b/src/actions/attack.cpp index 81eb4c5f6c2e0..1e0119a93ae3f 100644 --- a/src/actions/attack.cpp +++ b/src/actions/attack.cpp @@ -181,7 +181,7 @@ battle_context_unit_stats::battle_context_unit_stats(nonempty_unit_const_ptr up, chance_to_hit = std::clamp(cth, 0, 100); // Compute base damage done with the weapon. - int base_damage = weapon->modified_damage(); + double base_damage = weapon->modified_damage(); // Get the damage multiplier applied to the base damage of the weapon. int damage_multiplier = 100; @@ -200,8 +200,8 @@ battle_context_unit_stats::battle_context_unit_stats(nonempty_unit_const_ptr up, damage_multiplier *= opp.damage_from(*weapon, !attacking, opp_loc, opp_weapon); // Compute both the normal and slowed damage. - damage = round_damage(base_damage, damage_multiplier, 10000); - slow_damage = round_damage(base_damage, damage_multiplier, 20000); + damage = static_cast(std::round(round_damage(base_damage, damage_multiplier, 10000))); + slow_damage = static_cast(std::round(round_damage(base_damage, damage_multiplier, 20000))); if(is_slowed) { damage = slow_damage; @@ -314,14 +314,14 @@ battle_context_unit_stats::battle_context_unit_stats(const unit_type* u_type, chance_to_hit = std::clamp(cth, 0, 100); - int base_damage = weapon->modified_damage(); + double base_damage = weapon->modified_damage(); int damage_multiplier = 100; damage_multiplier += generic_combat_modifier(lawful_bonus, weapon->alignment(), u_type->musthave_status("fearless"), 0); damage_multiplier *= opp_type->resistance_against(weapon->type(), !attacking); - damage = round_damage(base_damage, damage_multiplier, 10000); - slow_damage = round_damage(base_damage, damage_multiplier, 20000); + damage = static_cast(std::round(round_damage(base_damage, damage_multiplier, 10000))); + slow_damage = static_cast(std::round(round_damage(base_damage, damage_multiplier, 20000))); if(drains) { // Compute the drain percent (with 50% as the base for backward compatibility) @@ -1590,7 +1590,7 @@ int under_leadership(const unit &u, const map_location& loc, const_attack_ptr we { unit_ability_list abil = u.get_abilities_weapons("leadership", loc, weapon, opp_weapon); unit_abilities::effect leader_effect(abil, 0, nullptr, unit_abilities::EFFECT_CUMULABLE); - return leader_effect.get_composite_value(); + return static_cast(std::round(leader_effect.get_composite_value())); } int combat_modifier(const unit_map& units, diff --git a/src/reports.cpp b/src/reports.cpp index 2f71ced1c23b7..434b8490b1803 100644 --- a/src/reports.cpp +++ b/src/reports.cpp @@ -777,7 +777,7 @@ static int attack_info(const reports::context& rc, const attack_type &at, config { auto ctx = at.specials_context(u.shared_from_this(), hex, u.side() == rc.screen().playing_team().side()); int base_damage = at.damage(); - int specials_damage = at.modified_damage(); + double specials_damage = at.modified_damage(); int damage_multiplier = 100; const_attack_ptr weapon = at.shared_from_this(); unit_alignments::type attack_alignment = weapon->alignment(); @@ -790,7 +790,7 @@ static int attack_info(const reports::context& rc, const attack_type &at, config bool slowed = u.get_state(unit::STATE_SLOWED); int damage_divisor = slowed ? 20000 : 10000; // Assume no specific resistance (i.e. multiply by 100). - damage = round_damage(specials_damage, damage_multiplier * 100, damage_divisor); + damage = static_cast(std::round(round_damage(specials_damage, damage_multiplier * 100, damage_divisor))); // Hit points are used to calculate swarm, so they need to be bounded. unsigned max_hp = u.max_hitpoints(); diff --git a/src/units/abilities.cpp b/src/units/abilities.cpp index cd02f3ae2f1bc..f1a4e7ed5c971 100644 --- a/src/units/abilities.cpp +++ b/src/units/abilities.cpp @@ -1365,9 +1365,9 @@ std::set attack_type::alternative_damage_types() const /** * Returns the damage per attack of this weapon, considering specials. */ -int attack_type::modified_damage() const +double attack_type::modified_damage() const { - int damage_value = composite_value(get_specials_and_abilities("damage"), damage()); + double damage_value = unit_abilities::effect(get_specials_and_abilities("damage"), damage(), shared_from_this()).get_composite_value(); return damage_value; } @@ -1579,7 +1579,7 @@ unit_ability_list attack_type::get_specials_and_abilities(const std::string& spe int attack_type::composite_value(const unit_ability_list& abil_list, int base_value) const { - return unit_abilities::effect(abil_list, base_value, shared_from_this()).get_composite_value(); + return static_cast(std::round(unit_abilities::effect(abil_list, base_value, shared_from_this()).get_composite_value())); } static bool overwrite_special_affects(const config& special) @@ -2338,8 +2338,8 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF individual_effect set_effect_max; individual_effect set_effect_min; - utils::optional max_value = utils::nullopt; - utils::optional min_value = utils::nullopt; + utils::optional max_value = utils::nullopt; + utils::optional min_value = utils::nullopt; for (const unit_ability & ability : list) { const config& cfg = *ability.ability_cfg; @@ -2374,10 +2374,10 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF if(wham == EFFECT_DEFAULT || wham == EFFECT_CUMULABLE){ if(cfg.has_attribute("max_value")){ - max_value = max_value ? std::min(*max_value, cfg["max_value"].to_int()) : cfg["max_value"].to_int(); + max_value = max_value ? std::min(*max_value, cfg["max_value"].to_double()) : cfg["max_value"].to_double(); } if(cfg.has_attribute("min_value")){ - min_value = min_value ? std::max(*min_value, cfg["min_value"].to_int()) : cfg["min_value"].to_int(); + min_value = min_value ? std::max(*min_value, cfg["min_value"].to_double()) : cfg["min_value"].to_double(); } } @@ -2474,8 +2474,7 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF effect_list_.push_back(val.second); } - double temp_value = (value_set + addition + substraction) * multiplier / divisor; - composite_value_ = static_cast(std::round(temp_value)); + composite_value_ = (value_set + addition + substraction) * multiplier / divisor; //clamp what if min_value < max_value or one attribute only used. if(max_value && min_value && *min_value < *max_value) { composite_value_ = std::clamp(*min_value, *max_value, composite_value_); diff --git a/src/units/abilities.hpp b/src/units/abilities.hpp index 9c5f5ea2b6e1e..c08e10c89d86f 100644 --- a/src/units/abilities.hpp +++ b/src/units/abilities.hpp @@ -46,7 +46,7 @@ class effect typedef std::vector::const_iterator iterator; typedef std::vector::const_iterator const_iterator; - int get_composite_value() const + double get_composite_value() const { return composite_value_; } const_iterator begin() const { return effect_list_.begin(); } @@ -54,7 +54,7 @@ class effect { return effect_list_.end(); } private: std::vector effect_list_; - int composite_value_; + double composite_value_; }; diff --git a/src/units/attack_type.hpp b/src/units/attack_type.hpp index 5a3027989ac19..2fc645bf4de33 100644 --- a/src/units/attack_type.hpp +++ b/src/units/attack_type.hpp @@ -112,7 +112,7 @@ class attack_type : public std::enable_shared_from_this std::set alternative_damage_types() const; /** Returns the damage per attack of this weapon, considering specials. */ - int modified_damage() const; + double modified_damage() const; /** Return the special weapon value, considering specials. * @param abil_list The list of special checked.