From 7d8a3261c2e3e2f51fd61bead73c164f80fb6a98 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Mon, 17 Jun 2024 00:08:53 +0300
Subject: [PATCH 28/28] AI: Improve military_advisor_choose_build() parameter
 order

Make consistent with other functions:
1) If there's an AI type parameter, it must be first
2) civ_map parameter is second, or first if there's no AI type

See RM #724

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/classic/classicai.c   |  2 +-
 ai/default/aihand.c      |  8 ++++----
 ai/default/daicity.c     |  2 +-
 ai/default/daimilitary.c | 27 +++++++++++++++------------
 ai/default/daimilitary.h |  6 +++---
 ai/default/daiplayer.c   |  2 +-
 ai/tex/texai.c           |  2 +-
 ai/tex/texaiplayer.c     |  4 ++--
 8 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/ai/classic/classicai.c b/ai/classic/classicai.c
index 6e47c6dded..8caf0bd34d 100644
--- a/ai/classic/classicai.c
+++ b/ai/classic/classicai.c
@@ -144,7 +144,7 @@ static void cai_split_by_civil_war(struct player *original,
 {
   struct ai_type *deftype = classic_ai_get_self();
 
-  dai_assess_danger_player(deftype, original, &(wld.map));
+  dai_assess_danger_player(deftype, &(wld.map), original);
 }
 
 /**********************************************************************//**
diff --git a/ai/default/aihand.c b/ai/default/aihand.c
index c2c49d34a9..974b833fd3 100644
--- a/ai/default/aihand.c
+++ b/ai/default/aihand.c
@@ -741,13 +741,13 @@ static void dai_manage_government(struct ai_type *ait, struct player *pplayer)
 }
 
 /*************************************************************************//**
-  Activities to be done by AI _before_ human turn.  Here we just move the
+  Activities to be done by AI _before_ human turn. Here we just move the
   units intelligently.
 *****************************************************************************/
 void dai_do_first_activities(struct ai_type *ait, struct player *pplayer)
 {
   TIMING_LOG(AIT_ALL, TIMER_START);
-  dai_assess_danger_player(ait, pplayer, &(wld.map));
+  dai_assess_danger_player(ait, &(wld.map), pplayer);
   /* TODO: Make assess_danger save information on what is threatening
    * us and make dai_manage_units and Co act upon this information, trying
    * to eliminate the source of danger */
@@ -763,9 +763,9 @@ void dai_do_first_activities(struct ai_type *ait, struct player *pplayer)
 }
 
 /*************************************************************************//**
-  Activities to be done by AI _after_ human turn.  Here we respond to
+  Activities to be done by AI _after_ human turn. Here we respond to
   dangers created by human and AI opposition by ordering defenders in
-  cities and setting taxes accordingly.  We also do other duties.
+  cities and setting taxes accordingly. We also do other duties.
 
   We do _not_ move units here, otherwise humans complain that AI moves
   twice.
diff --git a/ai/default/daicity.c b/ai/default/daicity.c
index 605103fadb..ad341679de 100644
--- a/ai/default/daicity.c
+++ b/ai/default/daicity.c
@@ -908,7 +908,7 @@ void dai_manage_cities(struct ai_type *ait, struct player *pplayer)
     if (city_data->choice.want <= 0) {
       /* Note that this function mungs the seamap, but we don't care */
       TIMING_LOG(AIT_CITY_MILITARY, TIMER_START);
-      choice = military_advisor_choose_build(ait, pplayer, pcity, &(wld.map), NULL);
+      choice = military_advisor_choose_build(ait, &(wld.map), pplayer, pcity, NULL);
       adv_choice_copy(&(city_data->choice), choice);
       adv_free_choice(choice);
       TIMING_LOG(AIT_CITY_MILITARY, TIMER_STOP);
diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c
index 9500f7584d..fc548c6592 100644
--- a/ai/default/daimilitary.c
+++ b/ai/default/daimilitary.c
@@ -74,8 +74,9 @@
 #define CITY_CONQUEST_WORTH(_city_, _data_) \
   (_data_->worth * 0.9 + (city_size_get(_city_) - 0.5) * 10)
 
-static unsigned int assess_danger(struct ai_type *ait, struct city *pcity,
-                                  const struct civ_map *dmap,
+static unsigned int assess_danger(struct ai_type *ait,
+                                  const struct civ_map *nmap,
+                                  struct city *pcity,
                                   player_unit_list_getter ul_cb);
 
 static adv_want dai_unit_attack_desirability(struct ai_type *ait,
@@ -633,13 +634,14 @@ static unsigned int assess_danger_unit(const struct city *pcity,
 
   This is necessary to initialize some ai data before some ai calculations.
 **************************************************************************/
-void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer,
-                              const struct civ_map *dmap)
+void dai_assess_danger_player(struct ai_type *ait,
+                              const struct civ_map *nmap,
+                              struct player *pplayer)
 {
   /* Do nothing if game is not running */
   if (S_S_RUNNING == server_state()) {
     city_list_iterate(pplayer->cities, pcity) {
-      (void) assess_danger(ait, pcity, dmap, NULL);
+      (void) assess_danger(ait, nmap, pcity, NULL);
     } city_list_iterate_end;
   }
 }
@@ -698,8 +700,9 @@ static void dai_reevaluate_building(struct city *pcity, adv_want *value,
   afraid of a boat laden with enemies if it stands on the coast (i.e.
   is directly reachable by this boat).
 **************************************************************************/
-static unsigned int assess_danger(struct ai_type *ait, struct city *pcity,
-                                  const struct civ_map *dmap,
+static unsigned int assess_danger(struct ai_type *ait,
+                                  const struct civ_map *nmap,
+                                  struct city *pcity,
                                   player_unit_list_getter ul_cb)
 {
   struct player *pplayer = city_owner(pcity);
@@ -826,7 +829,7 @@ static unsigned int assess_danger(struct ai_type *ait, struct city *pcity,
      * at war with. */
 
     pcity_map = pf_reverse_map_new_for_city(pcity, aplayer, assess_turns,
-                                            omnimap, dmap);
+                                            omnimap, nmap);
 
     if (ul_cb != NULL) {
       units = ul_cb(aplayer);
@@ -1775,9 +1778,9 @@ static void adjust_ai_unit_choice(struct city *pcity,
   If 'choice->want' is 0 this advisor doesn't want anything.
 **************************************************************************/
 struct adv_choice *military_advisor_choose_build(struct ai_type *ait,
+                                                 const struct civ_map *nmap,
                                                  struct player *pplayer,
                                                  struct city *pcity,
-                                                 const struct civ_map *mamap,
                                                  player_unit_list_getter ul_cb)
 {
   struct adv_data *ai = adv_data_get(pplayer, NULL);
@@ -1791,10 +1794,10 @@ struct adv_choice *military_advisor_choose_build(struct ai_type *ait,
   struct adv_choice *choice = adv_new_choice();
   bool allow_gold_upkeep;
 
-  urgency = assess_danger(ait, pcity, mamap, ul_cb);
+  urgency = assess_danger(ait, nmap, pcity, ul_cb);
   /* Changing to quadratic to stop AI from building piles 
    * of small units -- Syela */
-  /* It has to be AFTER assess_danger thanks to wallvalue. */
+  /* It has to be AFTER assess_danger() thanks to wallvalue. */
   our_def = assess_defense_quadratic(ait, pcity); 
 
   dai_choose_diplomat_defensive(ait, pplayer, pcity, choice, our_def);
@@ -1895,7 +1898,7 @@ struct adv_choice *military_advisor_choose_build(struct ai_type *ait,
         if (pcity->server.adv->building_want[wall_id] > 0) {
           /* NB: great wall is under domestic */
           choice->value.building = pimprove;
-          /* building_want is hacked by assess_danger */
+          /* building_want is hacked by assess_danger() */
           choice->want = pcity->server.adv->building_want[wall_id];
           choice->want = choice->want * (0.5 + (ai_trait_get_value(TRAIT_BUILDER, pplayer)
                                                 / TRAIT_DEFAULT_VALUE / 2));
diff --git a/ai/default/daimilitary.h b/ai/default/daimilitary.h
index a64eef220c..16a63289eb 100644
--- a/ai/default/daimilitary.h
+++ b/ai/default/daimilitary.h
@@ -36,12 +36,12 @@ typedef struct unit_list *(player_unit_list_getter)(struct player *pplayer);
 struct unit_type *dai_choose_defender_versus(struct city *pcity,
                                              struct unit *attacker);
 struct adv_choice *military_advisor_choose_build(struct ai_type *ait,
+                                                 const struct civ_map *nmap,
                                                  struct player *pplayer,
                                                  struct city *pcity,
-                                                 const struct civ_map *mamap,
                                                  player_unit_list_getter ul_cb);
-void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer,
-                              const struct civ_map *dmap);
+void dai_assess_danger_player(struct ai_type *ait,
+                              const struct civ_map *nmap, struct player *pplayer);
 int assess_defense_quadratic(struct ai_type *ait, struct city *pcity);
 int assess_defense_unit(struct ai_type *ait, struct city *pcity,
                         struct unit *punit, bool igwall);
diff --git a/ai/default/daiplayer.c b/ai/default/daiplayer.c
index 8f8ae8e7f3..a9de9e0e3d 100644
--- a/ai/default/daiplayer.c
+++ b/ai/default/daiplayer.c
@@ -184,5 +184,5 @@ void dai_gained_control(struct ai_type *ait, struct player *pplayer)
     } city_list_iterate_end;
   }
 
-  dai_assess_danger_player(ait, pplayer, &(wld.map));
+  dai_assess_danger_player(ait, &(wld.map), pplayer);
 }
diff --git a/ai/tex/texai.c b/ai/tex/texai.c
index e7aded9e8e..56151afd4e 100644
--- a/ai/tex/texai.c
+++ b/ai/tex/texai.c
@@ -144,7 +144,7 @@ static void texwai_split_by_civil_war(struct player *original,
                                       struct player *created)
 {
   TEXAI_AIT;
-  TEXAI_DFUNC(dai_assess_danger_player, original, &(wld.map));
+  TEXAI_DFUNC(dai_assess_danger_player, &(wld.map), original);
 }
 
 /**********************************************************************//**
diff --git a/ai/tex/texaiplayer.c b/ai/tex/texaiplayer.c
index 468117b41f..c75d726ae3 100644
--- a/ai/tex/texaiplayer.c
+++ b/ai/tex/texaiplayer.c
@@ -196,8 +196,8 @@ static enum texai_abort_msg_class texai_check_messages(struct ai_type *ait)
         texai_city_worker_wants(ait, msg->plr, pcity);
 
         if (tex_city != NULL) {
-          choice = military_advisor_choose_build(ait, msg->plr, tex_city,
-                                                 texai_map_get(),
+          choice = military_advisor_choose_build(ait, texai_map_get(),
+                                                 msg->plr, tex_city,
                                                  texai_player_units);
           choice_req->city_id = tex_city->id;
           adv_choice_copy(&(choice_req->choice), choice);
-- 
2.43.0

