Use std::numeric_limits instead of C macros

This commit is contained in:
Charles Dang 2024-07-12 00:35:26 -04:00
parent bcd97b209a
commit 156630d314
16 changed files with 38 additions and 38 deletions

View file

@ -591,7 +591,7 @@ namespace { // Helpers for place_recruit()
const unit_map & units = resources::gameboard->units();
unit_map::const_iterator unit_itor;
map_location min_loc;
int min_dist = INT_MAX;
int min_dist = std::numeric_limits<int>::max();
for ( unit_itor = units.begin(); unit_itor != units.end(); ++unit_itor ) {
if (resources::gameboard->get_team(unit_itor->side()).is_enemy(new_unit.side()) &&
@ -603,7 +603,7 @@ namespace { // Helpers for place_recruit()
}
}
}
if (min_dist < INT_MAX) {
if (min_dist < std::numeric_limits<int>::max()) {
// Face towards closest enemy
new_unit_itor->set_facing(recruit_loc.get_relative_dir(min_loc));
} else if (leader_loc != map_location::null_location()) {

View file

@ -251,7 +251,7 @@ namespace {
while ( !unit_list.empty() )
{
std::list<heal_unit>::iterator nearest;
int min_dist = INT_MAX;
int min_dist = std::numeric_limits<int>::max();
// Next unit to be healed is the entry in list nearest to last_loc.
for ( std::list<heal_unit>::iterator check_it = unit_list.begin();

View file

@ -651,7 +651,7 @@ game_events::pump_result_t actor_sighted(const unit & target, const std::vector<
// Look for units that can be used as the second unit in sighted events.
std::vector<const unit *> second_units(teams_size, nullptr);
std::vector<std::size_t> distances(teams_size, UINT_MAX);
std::vector<std::size_t> distances(teams_size, std::numeric_limits<unsigned>::max());
for (const unit & viewer : resources::gameboard->units()) {
const std::size_t index = viewer.side() - 1;
// Does viewer belong to a team for which we still need a unit?

View file

@ -80,7 +80,7 @@ config_attribute_value& config_attribute_value::operator=(long long v)
return *this = static_cast<unsigned long long>(v);
}
if(v >= INT_MIN) {
if(v >= std::numeric_limits<int>::min()) {
// We can store this as an int.
return *this = static_cast<int>(v);
}
@ -97,7 +97,7 @@ config_attribute_value& config_attribute_value::operator=(long long v)
config_attribute_value& config_attribute_value::operator=(unsigned long long v)
{
// Use int for smaller numbers.
if(v <= INT_MAX) {
if(v <= std::numeric_limits<int>::max()) {
return *this = static_cast<int>(v);
}

View file

@ -515,7 +515,7 @@ DEFINE_WFL_FUNCTION(tan, 1, 1)
{
const double angle = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = std::tan(angle * pi<double>() / 180.0);
if(std::isnan(result) || result <= INT_MIN || result >= INT_MAX) {
if(std::isnan(result) || result <= std::numeric_limits<int>::min() || result >= std::numeric_limits<int>::max()) {
return variant();
}
@ -613,7 +613,7 @@ DEFINE_WFL_FUNCTION(exp, 1, 1)
{
const double num = args()[0]->evaluate(variables, fdb).as_decimal() / 1000.0;
const double result = std::exp(num);
if(result == 0 || result >= INT_MAX) {
if(result == 0 || result >= std::numeric_limits<int>::max()) {
// These are range errors rather than NaNs,
// but I figure it's better than returning INT_MIN.
return variant();

View file

@ -94,7 +94,7 @@ std::string get_base_filename()
}
/***** ***** ***** ***** FLAGS ***** ***** ***** *****/
const unsigned ALL = UINT_MAX; /**< All levels/domains */
const unsigned ALL = std::numeric_limits<unsigned>::max(); /**< All levels/domains */
const unsigned SIZE_INFO = 1 << 0; /**<
* Shows the size info of

View file

@ -173,7 +173,7 @@ void saved_game::set_random_seed()
}
std::stringstream stream;
stream << std::setfill('0') << std::setw(8) << std::hex << randomness::generator->get_random_int(0, INT_MAX);
stream << std::setfill('0') << std::setw(8) << std::hex << randomness::generator->get_random_int(0, std::numeric_limits<int>::max());
carryover_["random_seed"] = stream.str();
carryover_["random_calls"] = 0;
}

View file

@ -43,10 +43,10 @@ wml_tag::wml_tag(const config& cfg)
, any_tag_(cfg["any_tag"].to_bool())
{
if(max_ < 0) {
max_ = INT_MAX;
max_ = std::numeric_limits<int>::max();
}
if(max_children_ < 0) {
max_children_ = INT_MAX;
max_children_ = std::numeric_limits<int>::max();
}
if(cfg.has_attribute("super")) {

View file

@ -115,7 +115,7 @@ public:
, min_(0)
, max_(0)
, min_children_(0)
, max_children_(INT_MAX)
, max_children_(std::numeric_limits<int>::max())
, super_("")
, tags_()
, keys_()
@ -130,7 +130,7 @@ public:
, min_(min)
, max_(max)
, min_children_(0)
, max_children_(INT_MAX)
, max_children_(std::numeric_limits<int>::max())
, super_(super)
, tags_()
, keys_()

View file

@ -64,7 +64,7 @@ std::shared_ptr<wml_type> wml_type::from_config(const config& cfg)
const config& list_cfg = cfg.mandatory_child("list");
int list_min = list_cfg["min"].to_int();
int list_max = list_cfg["max"].str() == "infinite" ? -1 : list_cfg["max"].to_int(-1);
if(list_max < 0) list_max = INT_MAX;
if(list_max < 0) list_max = std::numeric_limits<int>::max();
type = std::make_shared<wml_type_list>(cfg["name"], list_cfg["split"].str("\\s*,\\s*"), list_min, list_max);
composite_range.emplace(list_cfg.child_range("element"));
} else if(cfg.has_attribute("value")) {

View file

@ -637,8 +637,8 @@ void terrain_builder::rotate_rule(building_rule& ret, int angle, const std::vect
}
// Normalize the rotation, so that it starts on a positive location
int minx = INT_MAX;
int miny = INT_MAX;
int minx = std::numeric_limits<int>::max();
int miny = std::numeric_limits<int>::max();
for(const terrain_constraint& cons : ret.constraints) {
minx = std::min<int>(cons.loc.x, minx);
@ -1154,7 +1154,7 @@ void terrain_builder::build_terrains()
// Find the constraint that contains the less terrain of all terrain rules.
// We will keep a track of the matching terrains of this constraint
// and later try to apply the rule only on them
std::size_t min_size = INT_MAX;
std::size_t min_size = std::numeric_limits<int>::max();
t_translation::ter_list min_types = t_translation::ter_list(); // <-- This must be explicitly initialized, just
// as min_constraint is, at start of loop, or we
// get a null pointer dereference when we go

View file

@ -1197,7 +1197,7 @@ static std::string select_replacement_type(const unit_ability_list& damage_type_
static std::string select_alternative_type(const unit_ability_list& damage_type_list, unit_ability_list resistance_list, const unit& u)
{
std::map<std::string, int> type_res;
int max_res = INT_MIN;
int max_res = std::numeric_limits<int>::min();
for(auto& i : damage_type_list) {
const config& c = *i.ability_cfg;
if(c.has_attribute("alternative_type")) {

View file

@ -932,7 +932,7 @@ unit_animation::particle::particle(const config& cfg, const std::string& frame_s
, last_frame_begin_time_(0)
, cycles_(false)
{
starting_frame_time_ = INT_MAX;
starting_frame_time_ = std::numeric_limits<int>::max();
config::const_child_itors range = cfg.child_range(frame_string + "frame");
if(!range.empty() && cfg[frame_string + "start_time"].empty()) {
@ -1394,7 +1394,7 @@ void unit_animator::replace_anim_if_invalid(unit_const_ptr animated_unit
void unit_animator::start_animations()
{
int begin_time = INT_MAX;
int begin_time = std::numeric_limits<int>::max();
for(const auto& anim : animated_units_) {
if(anim.my_unit->anim_comp().get_animation()) {
@ -1492,7 +1492,7 @@ int unit_animator::get_animation_time_potential() const
int unit_animator::get_end_time() const
{
int end_time = INT_MIN;
int end_time = std::numeric_limits<int>::min();
for(const auto& anim : animated_units_) {
if(anim.my_unit->anim_comp().get_animation()) {
end_time = std::max<int>(end_time, anim.my_unit->anim_comp().get_animation()->get_end_time());

View file

@ -192,7 +192,7 @@ class unit_animator
public:
unit_animator() :
animated_units_(),
start_time_(INT_MIN)
start_time_(std::numeric_limits<int>::min())
{}
void add_animation(unit_const_ptr animated_unit
@ -255,7 +255,7 @@ public:
void clear()
{
start_time_ = INT_MIN;
start_time_ = std::numeric_limits<int>::min();
animated_units_.clear();
}

View file

@ -30,7 +30,7 @@ namespace
int get_next_idle_time()
{
if(!prefs::get().idle_anim()) {
return INT_MAX;
return std::numeric_limits<int>::max();
}
const double rate = std::pow(2.0, -prefs::get().idle_anim_rate() / 10.0);
@ -66,40 +66,40 @@ const unit_animation* unit_animation_component::choose_animation(const map_locat
void unit_animation_component::set_standing(bool with_bars)
{
if (prefs::get().show_standing_animations()&& !u_.incapacitated()) {
start_animation(INT_MAX, choose_animation(u_.loc_, "standing"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "standing"),
with_bars, "", {0,0,0}, STATE_STANDING);
} else {
start_animation(INT_MAX, choose_animation(u_.loc_, "_disabled_"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "_disabled_"),
with_bars, "", {0,0,0}, STATE_STANDING);
}
}
void unit_animation_component::set_ghosted(bool with_bars)
{
start_animation(INT_MAX, choose_animation(u_.loc_, "_ghosted_"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "_ghosted_"),
with_bars);
anim_->pause_animation();
}
void unit_animation_component::set_disabled_ghosted(bool with_bars)
{
start_animation(INT_MAX, choose_animation(u_.loc_, "_disabled_ghosted_"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "_disabled_ghosted_"),
with_bars);
}
void unit_animation_component::set_idling()
{
start_animation(INT_MAX, choose_animation(u_.loc_, "idling"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "idling"),
true, "", {0,0,0}, STATE_FORGET);
}
void unit_animation_component::set_selecting()
{
if (prefs::get().show_standing_animations() && !u_.incapacitated()) {
start_animation(INT_MAX, choose_animation(u_.loc_, "selected"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "selected"),
true, "", {0,0,0}, STATE_FORGET);
} else {
start_animation(INT_MAX, choose_animation(u_.loc_, "_disabled_selected_"),
start_animation(std::numeric_limits<int>::max(), choose_animation(u_.loc_, "_disabled_selected_"),
true, "", {0,0,0}, STATE_FORGET);
}
}
@ -119,7 +119,7 @@ void unit_animation_component::start_animation (int start_time, const unit_anima
bool accelerate = (state != STATE_FORGET && state != STATE_STANDING);
draw_bars_ = with_bars;
anim_.reset(new unit_animation(*animation));
const int real_start_time = start_time == INT_MAX ? anim_->get_begin_time() : start_time;
const int real_start_time = start_time == std::numeric_limits<int>::max() ? anim_->get_begin_time() : start_time;
anim_->start_animation(real_start_time, u_.loc_, u_.loc_.get_direction(u_.facing_),
text, text_color, accelerate);
frame_begin_time_ = anim_->get_begin_time() -1;

View file

@ -136,7 +136,7 @@ int move_unit_between(const map_location& a,
display& disp)
{
if ( disp.fogged(a) && disp.fogged(b) ) {
return INT_MIN;
return std::numeric_limits<int>::min();
}
temp_unit->set_location(a);
@ -178,7 +178,7 @@ unit_mover::unit_mover(const std::vector<map_location>& path, bool animate, bool
animate_(animate),
force_scroll_(force_scroll),
animator_(),
wait_until_(INT_MIN),
wait_until_(std::numeric_limits<int>::min()),
shown_unit_(),
path_(path),
current_(0),
@ -393,10 +393,10 @@ void unit_mover::proceed_to(unit_ptr u, std::size_t path_index, bool update, boo
*/
void unit_mover::wait_for_anims()
{
if ( wait_until_ == INT_MAX )
if ( wait_until_ == std::numeric_limits<int>::max() )
// Wait for end (not currently used, but still supported).
animator_.wait_for_end();
else if ( wait_until_ != INT_MIN ) {
else if ( wait_until_ != std::numeric_limits<int>::min() ) {
// Wait until the specified time (used for normal movement).
animator_.wait_until(wait_until_);
// debug code, see unit_frame::redraw()
@ -417,7 +417,7 @@ void unit_mover::wait_for_anims()
}
// Reset data.
wait_until_ = INT_MIN;
wait_until_ = std::numeric_limits<int>::min();
animator_.clear();
update_shown_unit();