Restore bounded_add()

Partial revert of commit ecbb15e1c6.

Replacing bounded_add() with std::clamp() in these contexts disables
time-of-day bonuses, since ToD bonuses (the base value in these functions)
are almost always outside the range set in terrain properties.
This commit is contained in:
Jyrki Vesterinen 2018-01-21 17:27:51 +02:00
parent ecbb15e1c6
commit 0db553ef36
3 changed files with 16 additions and 2 deletions

View file

@ -55,7 +55,7 @@ public:
/// gives a @a base bonus.
int light_bonus(int base) const
{
return utils::clamp(base + light_modification_, min_light_, max_light_);
return bounded_add(base, light_modification_, max_light_, min_light_);
}
int unit_height_adjust() const { return height_adjust_; }

View file

@ -250,7 +250,7 @@ const time_of_day tod_manager::get_illuminated_time_of_day(const unit_map & unit
int best_result = terrain_light;
const int base_light = terrain_light + (net_darker ? most_add : most_sub);
for ( size_t i = 0; i != mod_list.size(); ++i ) {
int result = utils::clamp(base_light + mod_list[i], min_list[i], max_list[i]);
int result = bounded_add( base_light, mod_list[i], max_list[i], min_list[i] );
if ( net_darker && result < best_result )
best_result = result;

View file

@ -39,6 +39,20 @@ inline int div100rounded(int num) {
return (num < 0) ? -(((-num) + 50) / 100) : (num + 50) / 100;
}
/**
* Returns base + increment, but will not increase base above max_sum, nor
* decrease it below min_sum.
* (If base is already below the applicable limit, base will be returned.)
*/
inline int bounded_add(int base, int increment, int max_sum, int min_sum = 0)
{
if(increment >= 0) {
return std::min(base + increment, std::max(base, max_sum));
} else {
return std::max(base + increment, std::min(base, min_sum));
}
}
/**
* round (base_damage * bonus / divisor) to the closest integer,
* but up or down towards base_damage