From ed0afec803d954148bc7d4b271724706a546edcd Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 14 Jan 2024 17:28:37 +0200
Subject: [PATCH 42/42] Unhardcode wld.map from
 citymindist_prevents_city_on_tile()

See RM #181

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/actres.c   |  2 +-
 common/city.c     | 13 ++++++++-----
 common/city.h     |  6 ++++--
 server/unithand.c |  5 +++--
 4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/common/actres.c b/common/actres.c
index ee9797ff93..864e8255ac 100644
--- a/common/actres.c
+++ b/common/actres.c
@@ -754,7 +754,7 @@ enum fc_tristate actres_possible(enum action_result result,
       return TRI_NO;
     }
 
-    if (citymindist_prevents_city_on_tile(target->tile)) {
+    if (citymindist_prevents_city_on_tile(nmap, target->tile)) {
       if (omniscient) {
         /* No need to check again. */
         return TRI_NO;
diff --git a/common/city.c b/common/city.c
index 4613b79a10..37bffa9e37 100644
--- a/common/city.c
+++ b/common/city.c
@@ -1441,11 +1441,13 @@ bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
   Returns TRUE iff it is illegal to found a city on the specified tile
   because of citymindist.
 **************************************************************************/
-bool citymindist_prevents_city_on_tile(const struct tile *ptile)
+bool citymindist_prevents_city_on_tile(const struct civ_map *nmap,
+                                       const struct tile *ptile)
 {
   /* citymindist minimum is 1, meaning adjacent is okay */
   int citymindist = game.info.citymindist;
-  square_iterate(&(wld.map), ptile, citymindist - 1, ptile1) {
+
+  square_iterate(nmap, ptile, citymindist - 1, ptile1) {
     if (tile_city(ptile1)) {
       return TRUE;
     }
@@ -1467,7 +1469,7 @@ bool city_can_be_built_here(const struct tile *ptile,
 {
   struct civ_map *nmap = &(wld.map);
 
-  if (!city_can_be_built_tile_only(ptile)) {
+  if (!city_can_be_built_tile_only(nmap, ptile)) {
     return FALSE;
   }
 
@@ -1535,14 +1537,15 @@ bool city_can_be_built_here(const struct tile *ptile,
   It may still be illegal for any unit to build a city at the specified
   tile.
 **************************************************************************/
-bool city_can_be_built_tile_only(const struct tile *ptile)
+bool city_can_be_built_tile_only(const struct civ_map *nmap,
+                                 const struct tile *ptile)
 {
   if (terrain_has_flag(tile_terrain(ptile), TER_NO_CITIES)) {
     /* No cities on this terrain. */
     return FALSE;
   }
 
-  if (citymindist_prevents_city_on_tile(ptile)) {
+  if (citymindist_prevents_city_on_tile(nmap, ptile)) {
     return FALSE;
   }
 
diff --git a/common/city.h b/common/city.h
index 7cd8f2ef7b..dc3e78c053 100644
--- a/common/city.h
+++ b/common/city.h
@@ -694,12 +694,14 @@ bool base_city_can_work_tile(const struct player *restriction,
                              const struct tile *ptile);
 bool city_can_work_tile(const struct city *pcity, const struct tile *ptile);
 
-bool citymindist_prevents_city_on_tile(const struct tile *ptile);
+bool citymindist_prevents_city_on_tile(const struct civ_map *nmap,
+                                       const struct tile *ptile);
 
 bool city_can_be_built_here(const struct tile *ptile,
                             const struct unit *punit,
                             bool hut_test);
-bool city_can_be_built_tile_only(const struct tile *ptile);
+bool city_can_be_built_tile_only(const struct civ_map *nmap,
+                                 const struct tile *ptile);
 
 /* List functions */
 struct city *city_list_find_number(struct city_list *This, int id);
diff --git a/server/unithand.c b/server/unithand.c
index efb5ccacc3..3178b10533 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -1857,7 +1857,7 @@ static struct ane_expl *expl_act_not_enabl(struct unit *punit,
     explnat->kind = ANEK_CITY_NO_CAPACITY;
     explnat->capacity_city = game_city_by_number(target_city->id);
   } else if (action_has_result_safe(paction, ACTRES_FOUND_CITY)
-             && citymindist_prevents_city_on_tile(target_tile)) {
+             && citymindist_prevents_city_on_tile(nmap, target_tile)) {
     explnat->kind = ANEK_CITY_TOO_CLOSE_TGT;
   } else if ((action_has_result_safe(paction, ACTRES_PARADROP_CONQUER)
               || action_has_result_safe(paction, ACTRES_PARADROP))
@@ -1954,6 +1954,7 @@ static void explain_why_no_action_enabled(struct unit *punit,
   struct ane_expl *explnat = expl_act_not_enabl(punit, ACTION_ANY,
                                                 target_tile,
                                                 target_city, target_unit);
+  const struct civ_map *nmap = &(wld.map);
 
   switch (explnat->kind) {
   case ANEK_ACTOR_UNIT:
@@ -1988,7 +1989,7 @@ static void explain_why_no_action_enabled(struct unit *punit,
       if (!utype_can_do_act_when_ustate(unit_type_get(punit),
                                         ACTION_ANY, USP_LIVABLE_TILE,
                                         FALSE)
-          && !can_unit_exist_at_tile(&(wld.map), punit, unit_tile(punit))) {
+          && !can_unit_exist_at_tile(nmap, punit, unit_tile(punit))) {
         unit_type_iterate(utype) {
           if (utype_can_do_act_when_ustate(utype, ACTION_ANY,
                                            USP_LIVABLE_TILE, FALSE)) {
-- 
2.43.0

