From 7f87611d4e07804f3b286bf4440ada4cca82ee78 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 26 Jun 2022 14:17:25 +0300 Subject: [PATCH 26/26] Move heavy parameter evaluations out from MAX() / MIN() See osdn #44611 Signed-off-by: Marko Lindqvist --- ai/default/aicity.c | 6 ++++-- ai/default/daidiplomacy.c | 12 ++++++++---- ai/default/daieffects.c | 4 +++- client/agents/cma_fec.c | 6 ++++-- client/gui-gtk-2.0/gamedlgs.c | 4 +++- client/gui-gtk-3.0/gamedlgs.c | 4 +++- client/gui-gtk-3.22/gamedlgs.c | 4 +++- client/helpdata.c | 25 ++++++++++++++++++------- client/mapview_common.c | 3 ++- client/options.c | 7 ++++--- common/aicore/path_finding.c | 5 ++++- server/advisors/advdata.c | 6 ++++-- server/generator/temperature_map.c | 10 ++++------ server/unittools.c | 9 ++++++--- 14 files changed, 70 insertions(+), 35 deletions(-) diff --git a/ai/default/aicity.c b/ai/default/aicity.c index 5019b53ea2..08c05fc457 100644 --- a/ai/default/aicity.c +++ b/ai/default/aicity.c @@ -1794,8 +1794,10 @@ static void adjust_improvement_wants_by_effects(struct ai_type *ait, /* Reduce want if building gets obsoleted soon */ requirement_vector_iterate(&pimprove->obsolete_by, pobs) { if (pobs->source.kind == VUT_ADVANCE && pobs->present) { - v -= v / MAX(1, research_goal_unknown_techs(presearch, - advance_number(pobs->source.value.advance))); + int num_tech = research_goal_unknown_techs(presearch, + advance_number(pobs->source.value.advance)); + + v -= v / MAX(1, num_tech); } } requirement_vector_iterate_end; diff --git a/ai/default/daidiplomacy.c b/ai/default/daidiplomacy.c index 5558800d6e..63f8bb1cdc 100644 --- a/ai/default/daidiplomacy.c +++ b/ai/default/daidiplomacy.c @@ -334,8 +334,10 @@ static int dai_goldequiv_clause(struct ai_type *ait, /* Make sure there's no division by zero */ if (research->techs_researched > 0) { - limit = MAX(1, player_tech_upkeep(pplayer) - / research->techs_researched); + int upk_per_tech = player_tech_upkeep(pplayer) + / research->techs_researched; + + limit = MAX(1, upk_per_tech); } else { limit = 1; fc_assert(player_tech_upkeep(pplayer) == 0); @@ -1019,6 +1021,7 @@ void dai_diplomacy_begin_new_phase(struct ai_type *ait, struct player *pplayer) players_iterate_alive(aplayer) { struct ai_dip_intel *adip = dai_diplomacy_get(ait, pplayer, aplayer); int amount = 0; + int pit; if (pplayer == aplayer) { continue; @@ -1072,8 +1075,9 @@ void dai_diplomacy_begin_new_phase(struct ai_type *ait, struct player *pplayer) /* Reduce love due to units in our territory. * AI is so naive, that we have to count it even if players are allied */ - amount -= MIN(player_in_territory(pplayer, aplayer) * (MAX_AI_LOVE / 200), - ai->diplomacy.love_incr + pit = player_in_territory(pplayer, aplayer) * (MAX_AI_LOVE / 200); + amount -= MIN(pit, + ai->diplomacy.love_incr * ((adip->is_allied_with_enemy != NULL) + 1)); pplayer->ai_common.love[player_index(aplayer)] += amount; if (amount != 0) { diff --git a/ai/default/daieffects.c b/ai/default/daieffects.c index 4d40a56382..a1b16c74f0 100644 --- a/ai/default/daieffects.c +++ b/ai/default/daieffects.c @@ -100,8 +100,10 @@ adv_want dai_content_effect_value(const struct player *pplayer, if (city_list_size(pplayer->cities) > get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE)) { if (get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE) > 0) { + int step_bonus = get_player_bonus(pplayer, EFT_EMPIRE_SIZE_STEP); + factor += city_list_size(pplayer->cities) - / MAX(get_player_bonus(pplayer, EFT_EMPIRE_SIZE_STEP), 1); + / MAX(step_bonus, 1); } factor += 2; } diff --git a/client/agents/cma_fec.c b/client/agents/cma_fec.c index 96640c8328..ed3cb8bb2a 100644 --- a/client/agents/cma_fec.c +++ b/client/agents/cma_fec.c @@ -328,6 +328,7 @@ const char *cmafec_get_result_descr(struct city *pcity, char buf[RESULT_COLUMNS][BUFFER_SIZE]; char citizen_types[BUFFER_SIZE]; static char buffer[600]; + int slen; /* TRANS: "W" is worker citizens, as opposed to specialists; * %s will represent the specialist types, for instance "E/S/T" */ @@ -356,6 +357,7 @@ const char *cmafec_get_result_descr(struct city *pcity, cmafec_get_short_descr(parameter)); } + slen = 20 - (int)get_internal_string_length(citizen_types); fc_snprintf(buffer, sizeof(buffer), _("Name: %s\n" "Food: %10s Gold: %10s\n" @@ -367,15 +369,15 @@ const char *cmafec_get_result_descr(struct city *pcity, "Production completed: %s"), buf[9], buf[O_FOOD], buf[O_GOLD], buf[O_SHIELD], buf[O_LUXURY], buf[O_TRADE], buf[O_SCIENCE], - MAX(0, 20 - (int)get_internal_string_length(citizen_types)), "", + MAX(0, slen), "", citizen_types, buf[6], buf[7], buf[8]); log_debug("\n%s", buffer); + return buffer; } - /************************************************************************** Create default cma presets for a new user (or without configuration file) **************************************************************************/ diff --git a/client/gui-gtk-2.0/gamedlgs.c b/client/gui-gtk-2.0/gamedlgs.c index 8c8a8a65bd..b3b7321997 100644 --- a/client/gui-gtk-2.0/gamedlgs.c +++ b/client/gui-gtk-2.0/gamedlgs.c @@ -340,6 +340,7 @@ static GtkWidget *create_multiplier_dialog(void) multipliers_iterate(pmul) { Multiplier_type_id multiplier = multiplier_index(pmul); + int mscale; fc_assert(multiplier < ARRAY_SIZE(multipliers_scale)); label = gtk_label_new(multiplier_name_translation(pmul)); @@ -348,8 +349,9 @@ static GtkWidget *create_multiplier_dialog(void) scale = gtk_hscale_new_with_range(mult_to_scale(pmul, pmul->start), mult_to_scale(pmul, pmul->stop), 1); multipliers_scale[multiplier] = scale; + mscale = mult_to_scale(pmul, pmul->stop) / 10; gtk_range_set_increments(GTK_RANGE(multipliers_scale[multiplier]), - 1, MAX(2, mult_to_scale(pmul, pmul->stop) / 10)); + 1, MAX(2, mscale)); g_signal_connect(multipliers_scale[multiplier], "format-value", G_CALLBACK(multiplier_value_callback), pmul); g_signal_connect(multipliers_scale[multiplier], "destroy", diff --git a/client/gui-gtk-3.0/gamedlgs.c b/client/gui-gtk-3.0/gamedlgs.c index fe6f961c68..502b741c40 100644 --- a/client/gui-gtk-3.0/gamedlgs.c +++ b/client/gui-gtk-3.0/gamedlgs.c @@ -339,6 +339,7 @@ static GtkWidget *create_multiplier_dialog(void) multipliers_iterate(pmul) { Multiplier_type_id multiplier = multiplier_index(pmul); + int mscale; fc_assert(multiplier < ARRAY_SIZE(multipliers_scale)); label = gtk_label_new(multiplier_name_translation(pmul)); @@ -348,8 +349,9 @@ static GtkWidget *create_multiplier_dialog(void) mult_to_scale(pmul, pmul->start), mult_to_scale(pmul, pmul->stop), 1); multipliers_scale[multiplier] = scale; + mscale = mult_to_scale(pmul, pmul->stop) / 10; gtk_range_set_increments(GTK_RANGE(multipliers_scale[multiplier]), - 1, MAX(2, mult_to_scale(pmul, pmul->stop) / 10)); + 1, MAX(2, mscale)); g_signal_connect(multipliers_scale[multiplier], "format-value", G_CALLBACK(multiplier_value_callback), pmul); g_signal_connect(multipliers_scale[multiplier], "destroy", diff --git a/client/gui-gtk-3.22/gamedlgs.c b/client/gui-gtk-3.22/gamedlgs.c index 60ca0b6a9e..68ba5e03b7 100644 --- a/client/gui-gtk-3.22/gamedlgs.c +++ b/client/gui-gtk-3.22/gamedlgs.c @@ -339,6 +339,7 @@ static GtkWidget *create_multiplier_dialog(void) multipliers_iterate(pmul) { Multiplier_type_id multiplier = multiplier_index(pmul); + int mscale; fc_assert(multiplier < ARRAY_SIZE(multipliers_scale)); label = gtk_label_new(multiplier_name_translation(pmul)); @@ -348,8 +349,9 @@ static GtkWidget *create_multiplier_dialog(void) mult_to_scale(pmul, pmul->start), mult_to_scale(pmul, pmul->stop), 1); multipliers_scale[multiplier] = scale; + mscale = mult_to_scale(pmul, pmul->stop) / 10; gtk_range_set_increments(GTK_RANGE(multipliers_scale[multiplier]), - 1, MAX(2, mult_to_scale(pmul, pmul->stop) / 10)); + 1, MAX(2, mscale)); g_signal_connect(multipliers_scale[multiplier], "format-value", G_CALLBACK(multiplier_value_callback), pmul); g_signal_connect(multipliers_scale[multiplier], "destroy", diff --git a/client/helpdata.c b/client/helpdata.c index 81b521b795..33b6fb95f8 100644 --- a/client/helpdata.c +++ b/client/helpdata.c @@ -177,11 +177,14 @@ static bool insert_veteran_help(char *outbuf, size_t outlen, for (i = 0; i < veteran->levels; i++) { const struct veteran_level *level = &veteran->definitions[i]; const char *name = name_translation_get(&level->name); + int slen; + /* Use get_internal_string_length() for correct alignment with * multibyte character encodings */ + slen = 25 - (int)get_internal_string_length(name); cat_snprintf(outbuf, outlen, "\n%s%*s %4d%% %12s", - name, MAX(0, 25 - (int)get_internal_string_length(name)), "", + name, MAX(0, slen), "", level->power_fact, /* e.g. "- ", "+ 1/3", "+ 1 ", "+ 2 2/3" */ move_points_text_full(level->move_bonus, TRUE, "+ ", "-", TRUE)); @@ -216,6 +219,7 @@ static bool insert_generated_text(char *outbuf, size_t outlen, const char *name) const char *terrain, *irrigation_result, *mining_result,*transform_result; struct universal for_terr = { .kind = VUT_TERRAIN, .value = { .terrain = pterrain }}; + int tslen, islen, mslen; fc_snprintf(irrigation_time, sizeof(irrigation_time), "%d", pterrain->irrigation_time); @@ -239,18 +243,22 @@ static bool insert_generated_text(char *outbuf, size_t outlen, const char *name) || pterrain->transform_result == T_NONE || effect_cumulative_max(EFT_TRANSFORM_POSSIBLE, &for_terr) <= 0) ? "" : terrain_name_translation(pterrain->transform_result); + /* Use get_internal_string_length() for correct alignment with * multibyte character encodings */ + tslen = 12 - (int)get_internal_string_length(terrain); + islen = 12 - (int)get_internal_string_length(irrigation_result); + mslen = 12 - (int)get_internal_string_length(mining_result); cat_snprintf(outbuf, outlen, "%s%*s %3s %s%*s %3s %s%*s %3s %s\n", terrain, - MAX(0, 12 - (int)get_internal_string_length(terrain)), "", + MAX(0, tslen), "", (pterrain->irrigation_result == T_NONE) ? "-" : irrigation_time, irrigation_result, - MAX(0, 12 - (int)get_internal_string_length(irrigation_result)), "", + MAX(0, islen), "", (pterrain->mining_result == T_NONE) ? "-" : mining_time, mining_result, - MAX(0, 12 - (int)get_internal_string_length(mining_result)), "", + MAX(0, mslen), "", (pterrain->transform_result == T_NONE) ? "-" : transform_time, transform_result); @@ -448,22 +456,24 @@ static bool insert_generated_text(char *outbuf, size_t outlen, const char *name) extra_type_by_cause_iterate(EC_ROAD, pextra) { if (pextra->buildable && pextra->build_time > 0) { const char *rname = extra_name_translation(pextra); + int slen = 18 - (int)get_internal_string_length(rname); cat_snprintf(outbuf, outlen, "\n%s%*s %3d", rname, - MAX(0, 18 - (int)get_internal_string_length(rname)), "", + MAX(0, slen), "", pextra->build_time); } } extra_type_by_cause_iterate_end; extra_type_by_cause_iterate(EC_BASE, pextra) { if (pextra->buildable && pextra->build_time > 0) { const char *bname = extra_name_translation(pextra); + int slen = 18 - (int)get_internal_string_length(bname); cat_snprintf(outbuf, outlen, "\n%s%*s %3d", bname, - MAX(0, 18 - (int)get_internal_string_length(bname)), "", + MAX(0, slen), "", pextra->build_time); } } extra_type_by_cause_iterate_end; @@ -3769,10 +3779,11 @@ void helptext_extra(char *buf, size_t bufsz, struct player *pplayer, = road ? helptext_road_bonus_str(t, proad) : NULL; if (turns > 0 || bonus_text) { const char *terrain = terrain_name_translation(t); + int slen = 12 - (int)get_internal_string_length(terrain); cat_snprintf(buf, bufsz, "%s%*s ", terrain, - MAX(0, 12 - (int)get_internal_string_length(terrain)), + MAX(0, slen), ""); if (do_time) { if (turns > 0) { diff --git a/client/mapview_common.c b/client/mapview_common.c index f520856ec6..0493af73d2 100644 --- a/client/mapview_common.c +++ b/client/mapview_common.c @@ -2698,8 +2698,9 @@ void move_unit_map_canvas(struct unit *punit, do { int new_x, new_y; + int asecs = timer_read_seconds(anim_timer); - mytime = MIN(timer_read_seconds(anim_timer), timing_sec); + mytime = MIN(asecs, timing_sec); new_x = start_x + canvas_dx * (mytime / timing_sec); new_y = start_y + canvas_dy * (mytime / timing_sec); diff --git a/client/options.c b/client/options.c index 0c1043db6f..0fdbd700b5 100644 --- a/client/options.c +++ b/client/options.c @@ -6067,9 +6067,10 @@ void options_init(void) case OT_INTEGER: if (option_int_def(poption) < option_int_min(poption) || option_int_def(poption) > option_int_max(poption)) { - int new_default = MAX(MIN(option_int_def(poption), - option_int_max(poption)), - option_int_min(poption)); + int int_def = option_int_def(poption); + int int_max = option_int_max(poption); + int int_min = option_int_min(poption); + int new_default = MAX(MIN(int_def, int_max), int_min); log_error("option %s has default value of %d, which is " "out of its range [%d; %d], changing to %d.", diff --git a/common/aicore/path_finding.c b/common/aicore/path_finding.c index 2aafe4d63a..686d172f57 100644 --- a/common/aicore/path_finding.c +++ b/common/aicore/path_finding.c @@ -1422,11 +1422,14 @@ static inline int pf_danger_map_adjust_cost(const struct pf_parameter *params, int cost, bool to_danger, int moves_left) { + int mr; + if (cost == PF_IMPOSSIBLE_MC) { return PF_IMPOSSIBLE_MC; } - cost = MIN(cost, pf_move_rate(params)); + mr = pf_move_rate(params); + cost = MIN(cost, mr); if (to_danger && cost >= moves_left) { /* We would have to end the turn on a dangerous tile! */ diff --git a/server/advisors/advdata.c b/server/advisors/advdata.c index 69d3413664..f4d8860103 100644 --- a/server/advisors/advdata.c +++ b/server/advisors/advdata.c @@ -963,8 +963,10 @@ void adv_best_government(struct player *pplayer) dist = 0; requirement_vector_iterate(&gov->reqs, preq) { if (VUT_ADVANCE == preq->source.kind) { - dist += MAX(1, research_goal_unknown_techs(presearch, - advance_number(preq->source.value.advance))); + int gut = research_goal_unknown_techs(presearch, + advance_number(preq->source.value.advance)); + + dist += MAX(1, gut); } } requirement_vector_iterate_end; val = amortize(val, dist); diff --git a/server/generator/temperature_map.c b/server/generator/temperature_map.c index 051eccc069..72aae2e246 100644 --- a/server/generator/temperature_map.c +++ b/server/generator/temperature_map.c @@ -132,15 +132,13 @@ void create_tmap(bool real) tmap(ptile) = t; } else { /* high land can be 30% cooler */ - float height = - 0.3 * MAX(0, hmap(ptile) - hmap_shore_level) - / (hmap_max_level - hmap_shore_level); + float height = - 0.3 * MAX(0, hmap(ptile) - hmap_shore_level) + / (hmap_max_level - hmap_shore_level); + int tcn = count_terrain_class_near_tile(ptile, FALSE, TRUE, TC_OCEAN); /* near ocean temperature can be 15% more "temperate" */ float temperate = (0.15 * (wld.map.server.temperature / 100 - t / MAX_COLATITUDE) - * 2 * MIN(50, count_terrain_class_near_tile(ptile, - FALSE, - TRUE, - TC_OCEAN)) + * 2 * MIN(50, tcn) / 100); tmap(ptile) = t * (1.0 + temperate) * (1.0 + height); diff --git a/server/unittools.c b/server/unittools.c index 49a6971107..a4c6a14c74 100644 --- a/server/unittools.c +++ b/server/unittools.c @@ -1517,13 +1517,14 @@ void transform_unit(struct unit *punit, struct unit_type *to_unit, struct unit_type *old_type = punit->utype; int old_mr = unit_move_rate(punit); int old_hp = unit_type_get(punit)->hp; + int lvls; punit->utype = to_unit; /* New type may not have the same veteran system, and we may want to * knock some levels off. */ - punit->veteran = MIN(punit->veteran, - utype_veteran_system(to_unit)->levels - 1); + lvls = utype_veteran_system(to_unit)->levels - 1; + punit->veteran = MIN(punit->veteran, lvls); /* Keeping the old behaviour, so first clip top, then reduce */ punit->veteran = MAX(punit->veteran - vet_loss, 0); @@ -1614,8 +1615,10 @@ struct unit *create_unit_full(struct player *pplayer, struct tile *ptile, } if (moves_left >= 0) { + int mr = unit_move_rate(punit); + /* Override default full MP */ - punit->moves_left = MIN(moves_left, unit_move_rate(punit)); + punit->moves_left = MIN(moves_left, mr); } if (ptrans) { -- 2.35.1