Skip to content

Commit

Permalink
modify code for what one final value was rounded.
Browse files Browse the repository at this point in the history
  • Loading branch information
newfrenchy83 committed Oct 18, 2024
1 parent c648ada commit 4ea2409
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/actions/attack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<int>(std::round(round_damage(base_damage, damage_multiplier, 10000)));
slow_damage = static_cast<int>(std::round(round_damage(base_damage, damage_multiplier, 20000)));

if(is_slowed) {
damage = slow_damage;
Expand Down Expand Up @@ -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<int>(std::round(round_damage(base_damage, damage_multiplier, 10000)));
slow_damage = static_cast<int>(std::round(round_damage(base_damage, damage_multiplier, 20000)));

if(drains) {
// Compute the drain percent (with 50% as the base for backward compatibility)
Expand Down
4 changes: 2 additions & 2 deletions src/reports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<int>(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();
Expand Down
22 changes: 11 additions & 11 deletions src/units/abilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1365,9 +1365,9 @@ std::set<std::string> 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_double_value();
return damage_value;
}

Expand Down Expand Up @@ -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<int> max_value = utils::nullopt;
utils::optional<int> min_value = utils::nullopt;
utils::optional<double> max_value = utils::nullopt;
utils::optional<double> min_value = utils::nullopt;

for (const unit_ability & ability : list) {
const config& cfg = *ability.ability_cfg;
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -2474,16 +2474,16 @@ 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<int>(std::round(temp_value));
composite_double_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_);
composite_double_value_ = std::clamp(*min_value, *max_value, composite_double_value_);
} else if(max_value && !min_value) {
composite_value_ = std::min(*max_value, composite_value_);
composite_double_value_ = std::min(*max_value, composite_double_value_);
} else if(min_value && !max_value) {
composite_value_ = std::max(*min_value, composite_value_);
composite_double_value_ = std::max(*min_value, composite_double_value_);
}
composite_value_ = static_cast<int>(std::round(composite_double_value_));
}

} // end namespace unit_abilities
3 changes: 3 additions & 0 deletions src/units/abilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ class effect

int get_composite_value() const
{ return composite_value_; }
double get_composite_double_value() const
{ return composite_double_value_; }
const_iterator begin() const
{ return effect_list_.begin(); }
const_iterator end() const
{ return effect_list_.end(); }
private:
std::vector<individual_effect> effect_list_;
int composite_value_;
double composite_double_value_;
};


Expand Down
2 changes: 1 addition & 1 deletion src/units/attack_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class attack_type : public std::enable_shared_from_this<attack_type>
std::set<std::string> 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.
Expand Down

0 comments on commit 4ea2409

Please sign in to comment.