From 554fb02f912ad95afa51471f95956ad325b999af Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 23 Nov 2022 03:34:37 +0200 Subject: [PATCH 15/15] AI: Turn magic military emergency want values to macros Also fixes dai_unit_consider_bodyguard() bug that very high want is considered military emergency even when it's not. See osdn #46091 Signed-off-by: Marko Lindqvist --- ai/default/aicity.c | 7 +++---- ai/default/daimilitary.c | 36 +++++++++++++++++++----------------- ai/default/daimilitary.h | 3 +++ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/ai/default/aicity.c b/ai/default/aicity.c index d7582171a1..d2608f3542 100644 --- a/ai/default/aicity.c +++ b/ai/default/aicity.c @@ -231,8 +231,8 @@ static void dai_barbarian_choose_build(struct player *pplayer, /* If found anything, put it into the choice */ if (bestunit) { choice->value.utype = bestunit; - /* FIXME: 101 is the "overriding military emergency" indicator */ - choice->want = 101; + /* "Overriding military emergency" indicator */ + choice->want = DAI_WANT_MILITARY_EMERGENCY; choice->type = CT_ATTACKER; adv_choice_set_use(choice, "barbarian"); } else { @@ -265,8 +265,7 @@ static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer, if (is_barbarian(pplayer)) { dai_barbarian_choose_build(pplayer, pcity, &(city_data->choice)); } else { - /* FIXME: 101 is the "overriding military emergency" indicator */ - if ((city_data->choice.want <= 100 + if ((city_data->choice.want < DAI_WANT_MILITARY_EMERGENCY || city_data->urgency == 0) && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0 && pcity->id != adv->wonder_city)) { diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c index 37d11ed1b8..7b4740901d 100644 --- a/ai/default/daimilitary.c +++ b/ai/default/daimilitary.c @@ -412,14 +412,14 @@ void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer) /********************************************************************** Set (overwrite) our want for a building. Syela tries to explain: - - My first attempt to allow ng_wa >= 200 led to stupidity in cities - with no defenders and danger = 0 but danger > 0. Capping ng_wa at - 100 + urgency led to a failure to buy walls as required. Allowing - want > 100 with !urgency led to the AI spending too much gold and - falling behind on science. I'm trying again, but this will require + + My first attempt to allow ng_wa >= 200 led to stupidity in cities + with no defenders and danger = 0 but danger > 0. Capping ng_wa at + 100 + urgency led to a failure to buy walls as required. Allowing + want > 100 with !urgency led to the AI spending too much gold and + falling behind on science. I'm trying again, but this will require yet more tedious observation -- Syela - + The idea in this horrible function is that there is an enemy nearby that can whack us, so let's build something that can defend against him. If danger is urgent and overwhelming, danger is 200+, if it is @@ -1351,9 +1351,10 @@ cleanup: } /********************************************************************** -... this function should assign a value to choice and want and type, - where want is a value between 1 and 100. - if want is 0 this advisor doesn't want anything + This function should assign a value to choice and want and type, + where want is a value between 0.0 and + DAI_WANT_MILITARY_EMERGENCY (not inclusive) + if want is 0 this advisor doesn't want anything ***********************************************************************/ static void dai_unit_consider_bodyguard(struct ai_type *ait, struct city *pcity, @@ -1368,7 +1369,7 @@ static void dai_unit_consider_bodyguard(struct ai_type *ait, virtualunit = unit_virtual_create(pplayer, pcity, punittype, city_production_unit_veteran_level(pcity, punittype)); - if (choice->want < 100) { + if (choice->want < DAI_WANT_MILITARY_EMERGENCY) { const adv_want want = look_for_charge(ait, pplayer, virtualunit, &aunit, &acity); if (want > choice->want) { @@ -1505,7 +1506,7 @@ struct adv_choice *military_advisor_choose_build(struct ai_type *ait, * nobody behind them. */ if (dai_process_defender_want(ait, pplayer, pcity, danger, choice, martial_value)) { - choice->want = 100 + danger; + choice->want = DAI_WANT_BELOW_MIL_EMERGENCY + danger; adv_choice_set_use(choice, "first defender"); build_walls = FALSE; def_unit_selected = TRUE; @@ -1526,10 +1527,10 @@ struct adv_choice *military_advisor_choose_build(struct ai_type *ait, pimprove = improvement_by_number(wall_id); if (wall_id != B_LAST - && pcity->server.adv->building_want[wall_id] != 0 && our_def != 0 + && pcity->server.adv->building_want[wall_id] != 0 && our_def != 0 && can_city_build_improvement_now(pcity, pimprove) && (danger < 101 || num_defenders > 1 - || (city_data->grave_danger == 0 + || (city_data->grave_danger == 0 && pplayer->economic.gold > impr_buy_gold_cost(pcity, pimprove, pcity->shield_stock))) && ai_fuzzy(pplayer, TRUE)) { @@ -1538,8 +1539,8 @@ struct adv_choice *military_advisor_choose_build(struct ai_type *ait, choice->value.building = pimprove; /* building_want is hacked by assess_danger */ choice->want = pcity->server.adv->building_want[wall_id]; - if (urgency == 0 && choice->want > 100) { - choice->want = 100; + if (urgency == 0 && choice->want > DAI_WANT_BELOW_MIL_EMERGENCY) { + choice->want = DAI_WANT_BELOW_MIL_EMERGENCY; } choice->type = CT_BUILDING; adv_choice_set_use(choice, "defense building"); @@ -1628,7 +1629,8 @@ struct adv_choice *military_advisor_choose_build(struct ai_type *ait, /* If we are in severe danger, don't consider attackers. This is probably too general. In many cases we will want to buy attackers to counterattack. -- Per */ - if (choice->want - martial_value > 100 && city_data->grave_danger > 0) { + if (choice->want - martial_value >= DAI_WANT_MILITARY_EMERGENCY + && city_data->grave_danger > 0) { CITY_LOG(LOGLEVEL_BUILD, pcity, "severe danger (want " ADV_WANT_PRINTF "), force defender", choice->want); diff --git a/ai/default/daimilitary.h b/ai/default/daimilitary.h index a271fb88fd..70d53221e1 100644 --- a/ai/default/daimilitary.h +++ b/ai/default/daimilitary.h @@ -24,6 +24,9 @@ to finish him off. */ #define FINISH_HIM_CITY_COUNT 5 +#define DAI_WANT_BELOW_MIL_EMERGENCY 100 +#define DAI_WANT_MILITARY_EMERGENCY (DAI_WANT_BELOW_MIL_EMERGENCY + 1) + struct unit_type *dai_choose_defender_versus(struct city *pcity, struct unit *attacker); struct adv_choice *military_advisor_choose_build(struct ai_type *ait, -- 2.35.1