From 4164564f665ee41738074c10218f45fb4c4e9225 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 19 Apr 2024 21:27:52 +0300
Subject: [PATCH 64/64] 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      |  3 ++-
 server/unittools.c |  5 +++--
 4 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/common/city.c b/common/city.c
index a9726d14e3..e246dc411a 100644
--- a/common/city.c
+++ b/common/city.c
@@ -2075,21 +2075,23 @@ struct city *tile_non_allied_city(const struct tile *ptile,
 /**********************************************************************//**
   Return TRUE if there is a friendly city near to this unit
 **************************************************************************/
-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,
                                   int distance)
 {
-  return is_friendly_city_near(unit_owner(punit), unit_tile(punit),
+  return is_friendly_city_near(nmap, unit_owner(punit), unit_tile(punit),
                                distance);
 }
 
 /**********************************************************************//**
   Return TRUE if there is a friendly city near to this tile.
 **************************************************************************/
-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,
                            int distance)
 {
-  square_iterate(&(wld.map), ptile, distance, ptile1) {
+  square_iterate(nmap, ptile, distance, 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 c904d6db2f..37366a472a 100644
--- a/common/city.h
+++ b/common/city.h
@@ -765,9 +765,11 @@ 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_unit_near_a_friendly_city(const struct civ_map *nmap,
+                                  const struct unit *punit,
                                   int distance);
-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,
                            int distance);
 bool city_exists_within_max_city_map(const struct tile *ptile,
diff --git a/common/unit.c b/common/unit.c
index 1ef6abb2fa..fcff2be7a0 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -1553,6 +1553,7 @@ bool unit_being_aggressive(const struct unit *punit)
 {
   const struct tile *ptile = unit_tile(punit);
   int max_friendliness_range;
+  const struct civ_map *nmap = &(wld.map);
 
   if (!is_attack_unit(punit)) {
     return FALSE;
@@ -1580,7 +1581,7 @@ bool unit_being_aggressive(const struct unit *punit)
   max_friendliness_range = tile_has_not_aggressive_extra_for_unit(ptile,
                                                                   unit_type_get(punit));
   if (max_friendliness_range >= 0) {
-    return !is_unit_near_a_friendly_city(punit, max_friendliness_range);
+    return !is_unit_near_a_friendly_city(nmap, punit, max_friendliness_range);
   }
 
   return TRUE;
diff --git a/server/unittools.c b/server/unittools.c
index 020c3de100..d5dbab3fca 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -3740,6 +3740,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) {
@@ -3811,14 +3812,14 @@ static bool unit_move_consequences(struct unit *punit,
                                                                           type_end_pos);
 
       if (max_friendliness_range >= 0
-          && is_friendly_city_near(pplayer_end_pos, dst_tile, max_friendliness_range)) {
+          && is_friendly_city_near(nmap, pplayer_end_pos, dst_tile, max_friendliness_range)) {
         friendly_end = TRUE;
       } else {
         max_friendliness_range = tile_has_not_aggressive_extra_for_unit(src_tile,
                                                                         type_start_pos);
 
         if (max_friendliness_range >= 0
-            && is_friendly_city_near(pplayer_start_pos, src_tile, max_friendliness_range)) {
+            && is_friendly_city_near(nmap, pplayer_start_pos, src_tile, max_friendliness_range)) {
           friendly_end = TRUE;
         }
       }
-- 
2.43.0

