Used std::clamp directly

This commit is contained in:
Charles Dang 2021-01-17 10:07:30 +11:00
parent 4d54b2a7fd
commit c35a73f5f1
19 changed files with 40 additions and 51 deletions

View file

@ -179,7 +179,7 @@ battle_context_unit_stats::battle_context_unit_stats(nonempty_unit_const_ptr up,
signed int cth = opp.defense_modifier(resources::gameboard->map().get_terrain(opp_loc)) + weapon->accuracy()
- (opp_weapon ? opp_weapon->parry() : 0);
cth = utils::clamp(cth, 0, 100);
cth = std::clamp(cth, 0, 100);
unit_ability_list cth_specials = weapon->get_special_ability("chance_to_hit");
unit_abilities::effect cth_effects(cth_specials, cth, backstab_pos, weapon);
@ -190,7 +190,7 @@ battle_context_unit_stats::battle_context_unit_stats(nonempty_unit_const_ptr up,
cth = 0;
}
chance_to_hit = utils::clamp(cth, 0, 100);
chance_to_hit = std::clamp(cth, 0, 100);
// Compute base damage done with the weapon.
int base_damage = weapon->modified_damage(backstab_pos);
@ -325,13 +325,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 = utils::clamp(cth, 0, 100);
cth = std::clamp(cth, 0, 100);
unit_ability_list cth_specials = weapon->get_specials("chance_to_hit");
unit_abilities::effect cth_effects(cth_specials, cth, backstab_pos, weapon);
cth = cth_effects.get_composite_value();
chance_to_hit = utils::clamp(cth, 0, 100);
chance_to_hit = std::clamp(cth, 0, 100);
int base_damage = weapon->modified_damage(backstab_pos);
int damage_multiplier = 100;

View file

@ -655,12 +655,12 @@ void prob_matrix::shift_cols_in_row(unsigned dst,
// calculation easier to parse.
int col_i = static_cast<int>(cols[col_x]);
int drain_amount = col_i * drain_percent / 100 + drain_constant;
unsigned newrow = utils::clamp(row_i + drain_amount, 1, max_row);
unsigned newrow = std::clamp(row_i + drain_amount, 1, max_row);
xfer(dst, src, newrow, 0, row, cols[col_x], prob);
}
// The remaining columns use the specified drainmax.
unsigned newrow = utils::clamp(row_i + drainmax, 1, max_row);
unsigned newrow = std::clamp(row_i + drainmax, 1, max_row);
for(; col_x < cols.size(); ++col_x) {
xfer(dst, src, newrow, cols[col_x] - damage, row, cols[col_x], prob);
}
@ -728,12 +728,12 @@ void prob_matrix::shift_rows_in_col(unsigned dst,
// calculation easier to parse.
int row_i = static_cast<int>(rows[row_x]);
int drain_amount = row_i * drain_percent / 100 + drain_constant;
unsigned newcol = utils::clamp(col_i + drain_amount, 1, max_col);
unsigned newcol = std::clamp(col_i + drain_amount, 1, max_col);
xfer(dst, src, 0, newcol, rows[row_x], col, prob);
}
// The remaining rows use the specified drainmax.
unsigned newcol = utils::clamp(col_i + drainmax, 1, max_col);
unsigned newcol = std::clamp(col_i + drainmax, 1, max_col);
for(; row_x < rows.size(); ++row_x) {
xfer(dst, src, rows[row_x] - damage, newcol, rows[row_x], col, prob);
}
@ -1527,7 +1527,7 @@ void monte_carlo_combat_matrix::simulate()
b_slowed |= a_slows_;
int drain_amount = (a_drain_percent_ * static_cast<signed>(damage) / 100 + a_drain_constant_);
a_hp = utils::clamp(a_hp + drain_amount, 1u, a_max_hp_);
a_hp = std::clamp(a_hp + drain_amount, 1u, a_max_hp_);
b_hp -= damage;
@ -1547,7 +1547,7 @@ void monte_carlo_combat_matrix::simulate()
a_slowed |= b_slows_;
int drain_amount = (b_drain_percent_ * static_cast<signed>(damage) / 100 + b_drain_constant_);
b_hp = utils::clamp(b_hp + drain_amount, 1u, b_max_hp_);
b_hp = std::clamp(b_hp + drain_amount, 1u, b_max_hp_);
a_hp -= damage;
@ -1779,7 +1779,7 @@ double calculate_probability_of_debuff(double initial_prob, bool enemy_gives, do
// Prob_kill can creep a bit above 100 % if the AI simulates an unit being attacked by multiple units in a row, due to rounding error.
// Likewise, it can get slightly negative if the unit already has negative HP.
// Simply limit it to suitable range.
prob_kill = utils::clamp(prob_kill, 0.0, 1.0);
prob_kill = std::clamp(prob_kill, 0.0, 1.0);
// Probability we are already debuffed and the enemy doesn't hit us.
const double prob_already_debuffed_not_touched = initial_prob * (1.0 - prob_touched);

View file

@ -528,7 +528,7 @@ commandline_options::commandline_options(const std::vector<std::string>& args)
if(vm.count("all-translations"))
translation_percent = 0;
else if(vm.count("translations-over"))
translation_percent = utils::clamp<unsigned int>(vm["translations-over"].as<unsigned int>(), 0, 100);
translation_percent = std::clamp<unsigned int>(vm["translations-over"].as<unsigned int>(), 0, 100);
}
void commandline_options::parse_log_domains_(const std::string &domains_string, const int severity)

View file

@ -94,7 +94,7 @@ unsigned int display::last_zoom_ = SmallZoom;
// Assumption: zoom_levels is a sorted vector of ascending tile sizes
int get_zoom_levels_index(unsigned int zoom_level)
{
zoom_level = utils::clamp(zoom_level, MinZoom, MaxZoom); // ensure zoom_level is within zoom_levels bounds
zoom_level = std::clamp(zoom_level, MinZoom, MaxZoom); // ensure zoom_level is within zoom_levels bounds
auto iter = std::lower_bound(zoom_levels.begin(), zoom_levels.end(), zoom_level);
// find closest match
@ -2028,7 +2028,7 @@ bool display::zoom_at_min()
bool display::set_zoom(bool increase)
{
// Ensure we don't try to access nonexistent vector indices.
zoom_index_ = utils::clamp(increase ? zoom_index_ + 1 : zoom_index_ - 1, 0, final_zoom_index);
zoom_index_ = std::clamp(increase ? zoom_index_ + 1 : zoom_index_ - 1, 0, final_zoom_index);
// No validation check is needed in the next step since we've already set the index here and
// know the new zoom value is indeed valid.
@ -2037,7 +2037,7 @@ bool display::set_zoom(bool increase)
bool display::set_zoom(unsigned int amount, const bool validate_value_and_set_index)
{
unsigned int new_zoom = utils::clamp(amount, MinZoom, MaxZoom);
unsigned int new_zoom = std::clamp(amount, MinZoom, MaxZoom);
LOG_DP << "new_zoom = " << new_zoom << std::endl;

View file

@ -49,7 +49,7 @@ game_classification::game_classification(const config& cfg)
, abbrev(cfg["abbrev"])
, end_credits(cfg["end_credits"].to_bool(true))
, end_text(cfg["end_text"])
, end_text_duration(utils::clamp<unsigned>(cfg["end_text_duration"].to_unsigned(0), 0, 5000))
, end_text_duration(std::clamp<unsigned>(cfg["end_text_duration"].to_unsigned(0), 0, 5000))
, difficulty(cfg["difficulty"].empty() ? DEFAULT_DIFFICULTY : cfg["difficulty"].str())
, random_mode(cfg["random_mode"])
, oos_debug(cfg["oos_debug"].to_bool(false))

View file

@ -554,7 +554,7 @@ color_t red_to_green(int val, bool for_text)
{
const std::vector<color_t>& color_scale = for_text ? red_green_scale_text : red_green_scale;
val = utils::clamp(val, 0, 100);
val = std::clamp(val, 0, 100);
const int lvl = (color_scale.size() - 1) * val / 100;
return color_scale[lvl];
@ -564,7 +564,7 @@ color_t blue_to_white(int val, bool for_text)
{
const std::vector<color_t>& color_scale = for_text ? blue_white_scale_text : blue_white_scale;
val = utils::clamp(val, 0, 100);
val = std::clamp(val, 0, 100);
const int lvl = (color_scale.size() - 1) * val / 100;
return color_scale[lvl];

View file

@ -190,7 +190,7 @@ game_launcher::game_launcher(const commandline_options& cmdline_opts)
load_data_ = savegame::load_game_metadata{
savegame::save_index_class::default_saves_dir(), *cmdline_opts_.load};
if(cmdline_opts_.max_fps) {
int fps = utils::clamp(*cmdline_opts_.max_fps, 1, 1000);
int fps = std::clamp(*cmdline_opts_.max_fps, 1, 1000);
fps = 1000 / fps;
// increase the delay to avoid going above the maximum
if(1000 % fps != 0) {

View file

@ -441,7 +441,7 @@ void campaign_selection::post_show(window& window)
}
rng_mode_ = RNG_MODE(utils::clamp<unsigned>(find_widget<menu_button>(&window, "rng_menu", false).get_value(), RNG_DEFAULT, RNG_BIASED));
rng_mode_ = RNG_MODE(std::clamp<unsigned>(find_widget<menu_button>(&window, "rng_menu", false).get_value(), RNG_DEFAULT, RNG_BIASED));
preferences::set_modifications(engine_.active_mods(), false);
}

View file

@ -89,7 +89,7 @@ void disable_widget_on_toggle(window& window, widget& w, const std::string& id)
// number of pager layers (since get_layer_count returns one-past-end).
int index_in_pager_range(const int first, const stacked_widget& pager)
{
return utils::clamp<int>(first, 0, pager.get_layer_count() - 1);
return std::clamp<int>(first, 0, pager.get_layer_count() - 1);
}
// Helper to make it easier to immediately apply sound toggles immediately.

View file

@ -476,7 +476,7 @@ void story_viewer::draw_callback()
return;
}
unsigned short new_alpha = utils::clamp<short>(fade_step_ * 25.5, 0, ALPHA_OPAQUE);
unsigned short new_alpha = std::clamp<short>(fade_step_ * 25.5, 0, ALPHA_OPAQUE);
find_widget<scroll_label>(get_window(), "part_text", false).set_text_alpha(new_alpha);
// The text stack also needs to be marked dirty so the background panel redraws correctly.

View file

@ -85,7 +85,7 @@ point slider::calculate_best_size() const
void slider::set_value(int value)
{
value = utils::clamp(value, minimum_value_, get_maximum_value());
value = std::clamp(value, minimum_value_, get_maximum_value());
int old_value = get_value();
if(value == old_value) {

View file

@ -132,7 +132,7 @@ unsigned slider_base::get_state() const
void slider_base::set_slider_position(int item_position)
{
// Set the value always execute since we update a part of the state.
item_position_ = utils::clamp(item_position, 0, item_last_);
item_position_ = std::clamp(item_position, 0, item_last_);
// Determine the pixel offset of the item position.
positioner_offset_ = rounded_division(item_position_, max_offset(), item_last_) + offset_before();
@ -176,7 +176,7 @@ void slider_base::recalculate()
void slider_base::move_positioner(int new_offset)
{
int max_offset = this->max_offset();
new_offset = utils::clamp(new_offset, 0, max_offset);
new_offset = std::clamp(new_offset, 0, max_offset);
slider_base::slider_position_t final_offset = {new_offset, max_offset};
update_slider_position(final_offset);

View file

@ -139,7 +139,7 @@ void stacked_widget::select_layer_impl(std::function<bool(unsigned int i)> displ
void stacked_widget::update_selected_layer_index(const int i)
{
selected_layer_ = utils::clamp<int>(i, -1, get_layer_count() - 1);
selected_layer_ = std::clamp<int>(i, -1, get_layer_count() - 1);
}
bool stacked_widget::layer_selected(const unsigned layer)

View file

@ -34,22 +34,22 @@ int get_turns(const std::string& value)
return turns_max;
}
return utils::clamp<int>(lexical_cast_default<int>(value, turns_default), turns_min, turns_max);
return std::clamp<int>(lexical_cast_default<int>(value, turns_default), turns_min, turns_max);
}
int get_village_gold(const std::string& value, const game_classification* classification)
{
return utils::clamp<int>(lexical_cast_default<int>(value, ((classification && !classification->is_normal_mp_game()) ? 1 : 2)), 1, 5);
return std::clamp<int>(lexical_cast_default<int>(value, ((classification && !classification->is_normal_mp_game()) ? 1 : 2)), 1, 5);
}
int get_village_support(const std::string& value)
{
return utils::clamp<int>(lexical_cast_default<int>(value, 1), 0, 4);
return std::clamp<int>(lexical_cast_default<int>(value, 1), 0, 4);
}
int get_xp_modifier(const std::string& value)
{
return utils::clamp<int>(lexical_cast_default<int>(value, 70), 30, 200);
return std::clamp<int>(lexical_cast_default<int>(value, 70), 30, 200);
}
} // end namespace settings

View file

@ -622,7 +622,7 @@ void set_countdown(bool value)
int countdown_init_time()
{
return utils::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_init_time"), 270), 0, 1500);
return std::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_init_time"), 270), 0, 1500);
}
void set_countdown_init_time(int value)
@ -632,7 +632,7 @@ void set_countdown_init_time(int value)
int countdown_reservoir_time()
{
return utils::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_reservoir_time"), 330), 30, 1500);
return std::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_reservoir_time"), 330), 30, 1500);
}
void set_countdown_reservoir_time(int value)
@ -642,7 +642,7 @@ void set_countdown_reservoir_time(int value)
int countdown_turn_bonus()
{
return utils::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_turn_bonus"), 60), 0, 300);
return std::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_turn_bonus"), 60), 0, 300);
}
void set_countdown_turn_bonus(int value)
@ -652,7 +652,7 @@ void set_countdown_turn_bonus(int value)
int countdown_action_bonus()
{
return utils::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_action_bonus"), 13), 0, 30);
return std::clamp<int>(lexical_cast_default<int>(preferences::get("mp_countdown_action_bonus"), 13), 0, 30);
}
void set_countdown_action_bonus(int value)

View file

@ -443,7 +443,7 @@ int font_scaling()
void set_font_scaling(int scale)
{
prefs["font_scale"] = utils::clamp(scale, min_font_scaling, max_font_scaling);
prefs["font_scale"] = std::clamp(scale, min_font_scaling, max_font_scaling);
}
int font_scaled(int size)
@ -729,7 +729,7 @@ namespace {
int scroll_speed()
{
const int value = utils::clamp<int>(lexical_cast_default<int>(get("scroll"), 50), 1, 100);
const int value = std::clamp<int>(lexical_cast_default<int>(get("scroll"), 50), 1, 100);
scroll = value/100.0;
return value;

View file

@ -25,9 +25,9 @@ class config;
// This is a color delta, so do not replace with color_t!
struct tod_color {
explicit tod_color(int red = 0, int green = 0, int blue = 0)
: r(utils::clamp(red, -510, 510))
, g(utils::clamp(green, -510, 510))
, b(utils::clamp(blue, -510, 510))
: r(std::clamp(red, -510, 510))
, g(std::clamp(green, -510, 510))
, b(std::clamp(blue, -510, 510))
{}
bool operator==(const tod_color& o) const {
return r == o.r && g == o.g && b == o.b;

View file

@ -117,7 +117,7 @@ public:
int time = 0;
unsigned int i = 0;
const int searched_time = utils::clamp(current_time, 0, base_duration);
const int searched_time = std::clamp(current_time, 0, base_duration);
while(time < searched_time && i < base_data.size()) {
time += base_data[i].second;

View file

@ -23,17 +23,6 @@ inline bool chars_less_insensitive(char a, char b) { return tolower(a) < tolower
namespace utils {
#ifdef HAVE_CXX17
using std::clamp;
#else
// NOTE: remove once we have C++17 support and can use std::clamp
template<typename T>
constexpr const T& clamp(const T& value, const T& min, const T& max)
{
return std::max<T>(std::min<T>(value, max), min);
}
#endif
namespace detail
{
/**