From db305ff71f99e2e766aa291d2d64c3926bba0ad7 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 14 Apr 2023 02:45:18 +0300 Subject: [PATCH 35/35] 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. 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 | 12 +++++++----- common/requirements.h | 3 ++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/ai/default/daicity.c b/ai/default/daicity.c index e6ecf843b2..ef4f77501a 100644 --- a/ai/default/daicity.c +++ b/ai/default/daicity.c @@ -1673,7 +1673,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; @@ -1686,8 +1686,11 @@ 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 (VUT_IMPROVEMENT == preq->source.kind - && preq->source.value.building == pimprove) { - mypreq = preq; + && preq->source.value.building == pimprove) { + if (preq->range < range) { + /* More limited range */ + range = preq->range; + } present = preq->present; continue; } @@ -1708,8 +1711,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 6b7782e6e5..aa313097b9 100644 --- a/common/requirements.h +++ b/common/requirements.h @@ -55,7 +55,8 @@ extern "C" { #define SPECENUM_VALUE8NAME "Alliance" #define SPECENUM_VALUE9 REQ_RANGE_WORLD #define SPECENUM_VALUE9NAME "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