From 269658e3c3de5d0f27fccad76d01ea47bbb1c1c9 Mon Sep 17 00:00:00 2001 From: newfrenchy83 Date: Tue, 19 Nov 2024 15:09:12 +0100 Subject: [PATCH] value, add, sub, multiply and divide are rounded instead to ever truncated --- .../AbilitiesWML/special_calculations.cfg | 2 +- src/units/abilities.cpp | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/data/test/scenarios/wml_tests/UnitsWML/AbilitiesWML/special_calculations.cfg b/data/test/scenarios/wml_tests/UnitsWML/AbilitiesWML/special_calculations.cfg index 39ac073e5af79..2b48a84b49061 100644 --- a/data/test/scenarios/wml_tests/UnitsWML/AbilitiesWML/special_calculations.cfg +++ b/data/test/scenarios/wml_tests/UnitsWML/AbilitiesWML/special_calculations.cfg @@ -185,7 +185,7 @@ # API(s) being tested: [attacks]divide= ## # Expected end state: -# Bob's attack has 5 strikes. Here 2.008 is truncated to 2.00, and then 10 / 2.00 is 5. +# Bob's attack has 5 strikes. Here 2.008 is rounded up to 2.10, and then 10 / 2.10 is rounded up to 5. ##### {ONE_CALCULATION_UNIT_TEST divide_float_3dp 10 (divide=2.008) 5} ##### diff --git a/src/units/abilities.cpp b/src/units/abilities.cpp index a3180915bbb5a..aa89ab447404f 100644 --- a/src/units/abilities.cpp +++ b/src/units/abilities.cpp @@ -2450,10 +2450,10 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF if(wham != EFFECT_CUMULABLE){ if (const config::attribute_value *v = cfg.get("value")) { - int value = get_single_ability_value(*v, def, ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { - callable.add("base_value", wfl::variant(def)); - return formula.evaluate(callable).as_int(); - }); + int value = std::round(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { + callable.add("base_value", wfl::variant(def)); + return formula.evaluate(callable).as_int() / 1.0 ; + }) * 1 ); int value_cum = cfg["cumulative"].to_bool() ? std::max(def, value) : value; assert((set_effect_min.type != NOT_USED) == (set_effect_max.type != NOT_USED)); @@ -2482,27 +2482,27 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF } if (const config::attribute_value *v = cfg.get("add")) { - int add = get_single_ability_value(*v, def, ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { + int add = std::round(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { callable.add("base_value", wfl::variant(def)); - return formula.evaluate(callable).as_int(); - }); + return formula.evaluate(callable).as_int() / 1.0; + }) * 1); std::map::iterator add_effect = values_add.find(effect_id); if(add_effect == values_add.end() || add > add_effect->second.value) { values_add[effect_id].set(ADD, add, ability.ability_cfg, ability.teacher_loc); } } if (const config::attribute_value *v = cfg.get("sub")) { - int sub = - get_single_ability_value(*v, def, ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { + int sub = - std::round(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { callable.add("base_value", wfl::variant(def)); - return formula.evaluate(callable).as_int(); - }); + return formula.evaluate(callable).as_int() / 1.0; + }) * 1); std::map::iterator sub_effect = values_sub.find(effect_id); if(sub_effect == values_sub.end() || sub < sub_effect->second.value) { values_sub[effect_id].set(ADD, sub, ability.ability_cfg, ability.teacher_loc); } } if (const config::attribute_value *v = cfg.get("multiply")) { - int multiply = static_cast(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { + int multiply = std::round(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { callable.add("base_value", wfl::variant(def)); return formula.evaluate(callable).as_decimal() / 1000.0 ; }) * 100); @@ -2512,7 +2512,7 @@ effect::effect(const unit_ability_list& list, int def, const_attack_ptr att, EFF } } if (const config::attribute_value *v = cfg.get("divide")) { - int divide = static_cast(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { + int divide = std::round(get_single_ability_value(*v, static_cast(def), ability, list.loc(), att, [&](const wfl::formula& formula, wfl::map_formula_callable& callable) { callable.add("base_value", wfl::variant(def)); return formula.evaluate(callable).as_decimal() / 1000.0 ; }) * 100);