From 82bb0975c78188a05e8483b1608cc37bb75f233b Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 19 Apr 2024 21:32:24 +0300
Subject: [PATCH 17/17] Unhardcode wld.map from is_friendly_city_near()

See RM #480

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/city.c      | 10 ++++++----
 common/city.h      |  6 ++++--
 common/unit.c      |  4 +++-
 server/unittools.c |  7 ++++---
 4 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/common/city.c b/common/city.c
index 5a1fa68aa9..d4bb4aca50 100644
--- a/common/city.c
+++ b/common/city.c
@@ -2049,19 +2049,21 @@ struct city *tile_non_allied_city(const struct tile *ptile,
   Return TRUE if there is a friendly city near to this unit (within 3
   steps).
 **************************************************************************/
-bool is_unit_near_a_friendly_city(const struct unit *punit)
+bool is_unit_near_a_friendly_city(const struct civ_map *nmap,
+                                  const struct unit *punit)
 {
-  return is_friendly_city_near(unit_owner(punit), unit_tile(punit));
+  return is_friendly_city_near(nmap, unit_owner(punit), unit_tile(punit));
 }
 
 /**********************************************************************//**
   Return TRUE if there is a friendly city near to this tile (within 3
   steps).
 **************************************************************************/
-bool is_friendly_city_near(const struct player *owner,
+bool is_friendly_city_near(const struct civ_map *nmap,
+                           const struct player *owner,
                            const struct tile *ptile)
 {
-  square_iterate(&(wld.map), ptile, 3, ptile1) {
+  square_iterate(nmap, ptile, 3, ptile1) {
     struct city *pcity = tile_city(ptile1);
 
     if (pcity && pplayers_allied(owner, city_owner(pcity))) {
diff --git a/common/city.h b/common/city.h
index da62b9e6b8..9ecbddbaeb 100644
--- a/common/city.h
+++ b/common/city.h
@@ -743,8 +743,10 @@ static inline bool is_non_allied_city_tile(const struct tile *ptile,
   return NULL != tile_non_allied_city(ptile, pplayer);
 }
 
-bool is_unit_near_a_friendly_city(const struct unit *punit);
-bool is_friendly_city_near(const struct player *owner,
+bool is_unit_near_a_friendly_city(const struct civ_map *nmap,
+                                  const struct unit *punit);
+bool is_friendly_city_near(const struct civ_map *nmap,
+                           const struct player *owner,
                            const struct tile *ptile);
 bool city_exists_within_max_city_map(const struct tile *ptile,
                                      bool may_be_on_center);
diff --git a/common/unit.c b/common/unit.c
index f9e030b5e4..8f25cb2cd4 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -1520,6 +1520,8 @@ bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
 **************************************************************************/
 bool unit_being_aggressive(const struct unit *punit)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (!is_attack_unit(punit)) {
     return FALSE;
   }
@@ -1544,7 +1546,7 @@ bool unit_being_aggressive(const struct unit *punit)
   }
   if (tile_has_not_aggressive_extra_for_unit(unit_tile(punit),
                                              unit_type_get(punit))) {
-    return !is_unit_near_a_friendly_city(punit);
+    return !is_unit_near_a_friendly_city(nmap, punit);
   }
 
   return TRUE;
diff --git a/server/unittools.c b/server/unittools.c
index da8edd6634..62e132327c 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -3634,6 +3634,7 @@ static bool unit_move_consequences(struct unit *punit,
   bool refresh_homecity_end_pos = FALSE;
   int saved_id = punit->id;
   bool alive = TRUE;
+  const struct civ_map *nmap = &(wld.map);
 
   if (tocity && conquer_city_allowed) {
     if (!passenger) {
@@ -3694,15 +3695,15 @@ static bool unit_move_consequences(struct unit *punit,
     }
   }
 
-  /* entering/leaving a fortress or friendly territory */
+  /* Entering/leaving a fortress or friendly territory */
   if (homecity_start_pos || homecity_end_pos) {
     if ((game.info.happyborders != HB_DISABLED && tile_owner(src_tile) != tile_owner(dst_tile))
         || (tile_has_not_aggressive_extra_for_unit(dst_tile,
                                                    type_end_pos)
-            && is_friendly_city_near(pplayer_end_pos, dst_tile))
+            && is_friendly_city_near(nmap, pplayer_end_pos, dst_tile))
         || (tile_has_not_aggressive_extra_for_unit(src_tile,
                                                    type_start_pos)
-            && is_friendly_city_near(pplayer_start_pos, src_tile))) {
+            && is_friendly_city_near(nmap, pplayer_start_pos, src_tile))) {
       refresh_homecity_start_pos = TRUE;
       refresh_homecity_end_pos = TRUE;
     }
-- 
2.43.0

