diff --git a/src/actions/attack.cpp b/src/actions/attack.cpp index d4db8c36a95..ded138ea3c3 100644 --- a/src/actions/attack.cpp +++ b/src/actions/attack.cpp @@ -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; diff --git a/src/attack_prediction.cpp b/src/attack_prediction.cpp index 0fdff25f788..0a67e9aaa9b 100644 --- a/src/attack_prediction.cpp +++ b/src/attack_prediction.cpp @@ -655,12 +655,12 @@ void prob_matrix::shift_cols_in_row(unsigned dst, // calculation easier to parse. int col_i = static_cast(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(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(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(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); diff --git a/src/commandline_options.cpp b/src/commandline_options.cpp index 2f4e96ef4f2..a72cd21e4c3 100644 --- a/src/commandline_options.cpp +++ b/src/commandline_options.cpp @@ -528,7 +528,7 @@ commandline_options::commandline_options(const std::vector& args) if(vm.count("all-translations")) translation_percent = 0; else if(vm.count("translations-over")) - translation_percent = utils::clamp(vm["translations-over"].as(), 0, 100); + translation_percent = std::clamp(vm["translations-over"].as(), 0, 100); } void commandline_options::parse_log_domains_(const std::string &domains_string, const int severity) diff --git a/src/display.cpp b/src/display.cpp index 6c1ec40bf32..e9c53786088 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -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; diff --git a/src/game_classification.cpp b/src/game_classification.cpp index 2f0f377d471..a925d0e7f70 100644 --- a/src/game_classification.cpp +++ b/src/game_classification.cpp @@ -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(cfg["end_text_duration"].to_unsigned(0), 0, 5000)) + , end_text_duration(std::clamp(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)) diff --git a/src/game_config.cpp b/src/game_config.cpp index 4f721b9b480..476c1bb23cb 100644 --- a/src/game_config.cpp +++ b/src/game_config.cpp @@ -554,7 +554,7 @@ color_t red_to_green(int val, bool for_text) { const std::vector& 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_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]; diff --git a/src/game_launcher.cpp b/src/game_launcher.cpp index fdb7fe95d64..d7ca5eaf901 100644 --- a/src/game_launcher.cpp +++ b/src/game_launcher.cpp @@ -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) { diff --git a/src/gui/dialogs/campaign_selection.cpp b/src/gui/dialogs/campaign_selection.cpp index 24b32d508bb..875ad24c8b7 100644 --- a/src/gui/dialogs/campaign_selection.cpp +++ b/src/gui/dialogs/campaign_selection.cpp @@ -441,7 +441,7 @@ void campaign_selection::post_show(window& window) } - rng_mode_ = RNG_MODE(utils::clamp(find_widget(&window, "rng_menu", false).get_value(), RNG_DEFAULT, RNG_BIASED)); + rng_mode_ = RNG_MODE(std::clamp(find_widget(&window, "rng_menu", false).get_value(), RNG_DEFAULT, RNG_BIASED)); preferences::set_modifications(engine_.active_mods(), false); } diff --git a/src/gui/dialogs/preferences_dialog.cpp b/src/gui/dialogs/preferences_dialog.cpp index ccdf3189603..c9bce89d521 100644 --- a/src/gui/dialogs/preferences_dialog.cpp +++ b/src/gui/dialogs/preferences_dialog.cpp @@ -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(first, 0, pager.get_layer_count() - 1); + return std::clamp(first, 0, pager.get_layer_count() - 1); } // Helper to make it easier to immediately apply sound toggles immediately. diff --git a/src/gui/dialogs/story_viewer.cpp b/src/gui/dialogs/story_viewer.cpp index 160a900e72a..9f728cf24d3 100644 --- a/src/gui/dialogs/story_viewer.cpp +++ b/src/gui/dialogs/story_viewer.cpp @@ -476,7 +476,7 @@ void story_viewer::draw_callback() return; } - unsigned short new_alpha = utils::clamp(fade_step_ * 25.5, 0, ALPHA_OPAQUE); + unsigned short new_alpha = std::clamp(fade_step_ * 25.5, 0, ALPHA_OPAQUE); find_widget(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. diff --git a/src/gui/widgets/slider.cpp b/src/gui/widgets/slider.cpp index 756104e0335..38fcccada40 100644 --- a/src/gui/widgets/slider.cpp +++ b/src/gui/widgets/slider.cpp @@ -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) { diff --git a/src/gui/widgets/slider_base.cpp b/src/gui/widgets/slider_base.cpp index 8e1a5c7c001..37918bfbcef 100644 --- a/src/gui/widgets/slider_base.cpp +++ b/src/gui/widgets/slider_base.cpp @@ -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); diff --git a/src/gui/widgets/stacked_widget.cpp b/src/gui/widgets/stacked_widget.cpp index 7661ddbca80..2b0a790ffe6 100644 --- a/src/gui/widgets/stacked_widget.cpp +++ b/src/gui/widgets/stacked_widget.cpp @@ -139,7 +139,7 @@ void stacked_widget::select_layer_impl(std::function displ void stacked_widget::update_selected_layer_index(const int i) { - selected_layer_ = utils::clamp(i, -1, get_layer_count() - 1); + selected_layer_ = std::clamp(i, -1, get_layer_count() - 1); } bool stacked_widget::layer_selected(const unsigned layer) diff --git a/src/map_settings.cpp b/src/map_settings.cpp index 6953bf36ec3..f72bfe88be7 100644 --- a/src/map_settings.cpp +++ b/src/map_settings.cpp @@ -34,22 +34,22 @@ int get_turns(const std::string& value) return turns_max; } - return utils::clamp(lexical_cast_default(value, turns_default), turns_min, turns_max); + return std::clamp(lexical_cast_default(value, turns_default), turns_min, turns_max); } int get_village_gold(const std::string& value, const game_classification* classification) { - return utils::clamp(lexical_cast_default(value, ((classification && !classification->is_normal_mp_game()) ? 1 : 2)), 1, 5); + return std::clamp(lexical_cast_default(value, ((classification && !classification->is_normal_mp_game()) ? 1 : 2)), 1, 5); } int get_village_support(const std::string& value) { - return utils::clamp(lexical_cast_default(value, 1), 0, 4); + return std::clamp(lexical_cast_default(value, 1), 0, 4); } int get_xp_modifier(const std::string& value) { - return utils::clamp(lexical_cast_default(value, 70), 30, 200); + return std::clamp(lexical_cast_default(value, 70), 30, 200); } } // end namespace settings diff --git a/src/preferences/game.cpp b/src/preferences/game.cpp index f627c56efa2..9ed886f2411 100644 --- a/src/preferences/game.cpp +++ b/src/preferences/game.cpp @@ -622,7 +622,7 @@ void set_countdown(bool value) int countdown_init_time() { - return utils::clamp(lexical_cast_default(preferences::get("mp_countdown_init_time"), 270), 0, 1500); + return std::clamp(lexical_cast_default(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(lexical_cast_default(preferences::get("mp_countdown_reservoir_time"), 330), 30, 1500); + return std::clamp(lexical_cast_default(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(lexical_cast_default(preferences::get("mp_countdown_turn_bonus"), 60), 0, 300); + return std::clamp(lexical_cast_default(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(lexical_cast_default(preferences::get("mp_countdown_action_bonus"), 13), 0, 30); + return std::clamp(lexical_cast_default(preferences::get("mp_countdown_action_bonus"), 13), 0, 30); } void set_countdown_action_bonus(int value) diff --git a/src/preferences/general.cpp b/src/preferences/general.cpp index 6731012b0e9..a9182a2aa99 100644 --- a/src/preferences/general.cpp +++ b/src/preferences/general.cpp @@ -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(lexical_cast_default(get("scroll"), 50), 1, 100); + const int value = std::clamp(lexical_cast_default(get("scroll"), 50), 1, 100); scroll = value/100.0; return value; diff --git a/src/time_of_day.hpp b/src/time_of_day.hpp index f5373a465c6..1004a5030dc 100644 --- a/src/time_of_day.hpp +++ b/src/time_of_day.hpp @@ -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; diff --git a/src/units/frame_private.hpp b/src/units/frame_private.hpp index 9ea1193fd89..d5ea9ccea3d 100644 --- a/src/units/frame_private.hpp +++ b/src/units/frame_private.hpp @@ -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; diff --git a/src/utils/general.hpp b/src/utils/general.hpp index 90cf0e05707..5ca30319af2 100644 --- a/src/utils/general.hpp +++ b/src/utils/general.hpp @@ -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 -constexpr const T& clamp(const T& value, const T& min, const T& max) -{ - return std::max(std::min(value, max), min); -} -#endif - namespace detail { /**