From 7e0ca4b86b94afbd28390729bedce7a0bfbf9dc5 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 14 Apr 2023 02:40:23 +0300 Subject: [PATCH 51/51] AI: Be prepared for building providing multiple requirements for effect adjust_improvement_wants_by_effects() assumed that building under consideration would provide exactly one of the requirements for the effect it was checking. It's entirely feasible that the some building provides, e.g., multiple building flags that each are requirements for the same effect. This minor code rework also gets rid of one clang analyzer warning, as clang wasn't able to rule out the possibility of building providing none of the requirements. See osdn #47831 Signed-off-by: Marko Lindqvist --- ai/default/daicity.c | 10 ++++++---- common/requirements.h | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ai/default/daicity.c b/ai/default/daicity.c index 527e2625f6..f77ba7f930 100644 --- a/ai/default/daicity.c +++ b/ai/default/daicity.c @@ -1682,7 +1682,7 @@ static void adjust_improvement_wants_by_effects(struct ai_type *ait, } players_iterate_end; effect_list_iterate(get_req_source_effects(&source), peffect) { - struct requirement *mypreq = NULL; + enum req_range range = REQ_RANGE_MAX; bool active = TRUE; int n_needed_techs = 0; struct tech_vector needed_techs; @@ -1695,7 +1695,10 @@ static void adjust_improvement_wants_by_effects(struct ai_type *ait, /* Check if all the requirements for the currently evaluated effect * are met, except for having the building that we are evaluating. */ if (universal_fulfills_requirement(preq, &source) == ITF_YES) { - mypreq = preq; + if (preq->range < range) { + /* More limited range */ + range = preq->range; + } present = preq->present; continue; } @@ -1717,8 +1720,7 @@ static void adjust_improvement_wants_by_effects(struct ai_type *ait, n_needed_techs = tech_vector_size(&needed_techs); if ((active || n_needed_techs) && !impossible_to_get) { adv_want v1 = dai_effect_value(pplayer, ai, pcity, capital, - turns, peffect, cities[mypreq->range], - nplayers); + turns, peffect, range, nplayers); /* v1 could be negative (the effect could be undesirable), * although it is usually positive. * For example, in the default ruleset, Communism decreases the diff --git a/common/requirements.h b/common/requirements.h index fd97c1a82e..1161b1e014 100644 --- a/common/requirements.h +++ b/common/requirements.h @@ -56,7 +56,8 @@ struct astring; #define SPECENUM_VALUE9NAME "Alliance" #define SPECENUM_VALUE10 REQ_RANGE_WORLD #define SPECENUM_VALUE10NAME "World" -#define SPECENUM_COUNT REQ_RANGE_COUNT /* keep this last */ +#define SPECENUM_COUNT REQ_RANGE_COUNT /* Keep this last */ +#define REQ_RANGE_MAX (REQ_RANGE_COUNT - 1) /* REQ_RANGE_WORLD */ #include "specenum_gen.h" #define req_range_iterate(_range_) \ -- 2.39.2