From 244c05f782335f826a0c9449b08b1d7e595aa892 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 16 Feb 2025 03:18:27 +0200
Subject: [PATCH 39/39] AI: Pass civ_map to find_something_to_kill()

See RM #1240

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daimilitary.c |  2 +-
 ai/default/daiunit.c     | 23 ++++++++++++-----------
 ai/default/daiunit.h     |  3 ++-
 3 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c
index 778a493817..30bd4598f5 100644
--- a/ai/default/daimilitary.c
+++ b/ai/default/daimilitary.c
@@ -1557,7 +1557,7 @@ static struct adv_choice *kill_something_with(struct ai_type *ait,
     goto cleanup;
   }
 
-  best_choice->want = find_something_to_kill(ait, pplayer, myunit, &ptile, NULL,
+  best_choice->want = find_something_to_kill(ait, nmap, pplayer, myunit, &ptile, NULL,
                                              &ferry_map, &ferryboat,
                                              &boattype, &move_time);
   if (NULL == ptile
diff --git a/ai/default/daiunit.c b/ai/default/daiunit.c
index 30d5725ec0..fe7407737f 100644
--- a/ai/default/daiunit.c
+++ b/ai/default/daiunit.c
@@ -102,8 +102,8 @@ static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap
                                  struct player *pplayer, struct unit *punit);
 static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
                                 struct unit *punit);
-static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
-                                struct unit *punit);
+static void dai_military_attack(struct ai_type *ait, const struct civ_map *nmap,
+                                struct player *pplayer, struct unit *punit);
 
 static bool unit_role_defender(const struct unit_type *punittype);
 static int unit_def_rating_squared(const struct unit *punit,
@@ -244,7 +244,7 @@ static bool has_defense(struct city *pcity)
   the denom, so that def=1 units are penalized correctly."
 
   Translation (GB): build_cost_balanced is used in the denominator of
-  the want equation (see, e.g.  find_something_to_kill) instead of
+  the want equation (see, e.g. find_something_to_kill() ) instead of
   just build_cost to make AI build more balanced units (with def > 1).
 **************************************************************************/
 int build_cost_balanced(const struct unit_type *punittype)
@@ -1135,7 +1135,8 @@ bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
 
   punit->id == 0 means that the unit is virtual (considered to be built).
 **************************************************************************/
-adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
+adv_want find_something_to_kill(struct ai_type *ait, const struct civ_map *nmap,
+                                struct player *pplayer,
                                 struct unit *punit,
                                 struct tile **pdest_tile, struct pf_path **ppath,
                                 struct pf_map **pferrymap,
@@ -1176,7 +1177,6 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
   adv_want best = 0;    /* Best of all wants. */
   struct tile *goto_dest_tile = NULL;
   bool can_occupy;
-  struct civ_map *nmap = &(wld.map);
 
   /* Very preliminary checks. */
   *pdest_tile = punit_tile;
@@ -1745,8 +1745,8 @@ static void dai_military_attack_barbarian(struct ai_type *ait,
   looking for trouble elsewhere. If there is nothing to kill, sailing units 
   go home, others explore while barbs go berserk.
 **************************************************************************/
-static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
-                                struct unit *punit)
+static void dai_military_attack(struct ai_type *ait, const struct civ_map *nmap,
+                                struct player *pplayer, struct unit *punit)
 {
   struct tile *dest_tile;
   int id = punit->id;
@@ -1784,7 +1784,7 @@ static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
     struct unit *ferryboat;
 
     /* Then find enemies the hard way */
-    find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
+    find_something_to_kill(ait, nmap, pplayer, punit, &dest_tile, &path,
                            NULL, &ferryboat, NULL, NULL);
     if (!same_pos(unit_tile(punit), dest_tile)) {
       if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
@@ -2426,6 +2426,7 @@ static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
   If something is attacking our city, kill it yeahhh!!!.
 **************************************************************************/
 static void dai_manage_hitpoint_recovery(struct ai_type *ait,
+                                         const struct civ_map *nmap,
                                          struct unit *punit)
 {
   struct player *pplayer = unit_owner(punit);
@@ -2466,7 +2467,7 @@ static void dai_manage_hitpoint_recovery(struct ai_type *ait,
       /* Oops */
       UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
       dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
-      dai_military_attack(ait, pplayer, punit);
+      dai_military_attack(ait, nmap, pplayer, punit);
       return;
     }
   }
@@ -2573,7 +2574,7 @@ void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
   case AIUNIT_ATTACK:
   case AIUNIT_NONE:
     TIMING_LOG(AIT_ATTACK, TIMER_START);
-    dai_military_attack(ait, pplayer, punit);
+    dai_military_attack(ait, nmap, pplayer, punit);
     TIMING_LOG(AIT_ATTACK, TIMER_STOP);
     break;
   case AIUNIT_ESCORT: 
@@ -2597,7 +2598,7 @@ void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
     break;
   case AIUNIT_RECOVER:
     TIMING_LOG(AIT_RECOVER, TIMER_START);
-    dai_manage_hitpoint_recovery(ait, punit);
+    dai_manage_hitpoint_recovery(ait, nmap, punit);
     TIMING_LOG(AIT_RECOVER, TIMER_STOP);
     break;
   case AIUNIT_HUNTER:
diff --git a/ai/default/daiunit.h b/ai/default/daiunit.h
index 8150b6d79c..3e3569b259 100644
--- a/ai/default/daiunit.h
+++ b/ai/default/daiunit.h
@@ -108,7 +108,8 @@ bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
                     const struct unit_type *cargo_type,
                     const struct unit_type *ferry_type,
                     struct tile **ferry_dest, struct tile **beachhead_tile);
-adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
+adv_want find_something_to_kill(struct ai_type *ait, const struct civ_map *nmap,
+                                struct player *pplayer,
                                 struct unit *punit,
                                 struct tile **pdest_tile,
                                 struct pf_path **ppath,
-- 
2.47.2

