From 51971e070dade55b8c1e05ad88bbfa0b2a692d09 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 3 May 2024 20:04:23 +0300
Subject: [PATCH 18/18] Unhardcode wld.map from city_can_be_built_here()

See RM #537

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daisettler.c              |  5 +++--
 client/gui-gtk-3.0/editprop.c        |  3 ++-
 client/gui-gtk-3.22/editprop.c       |  2 +-
 client/gui-gtk-4.0/editprop.c        |  3 ++-
 client/mapview_common.c              |  3 ++-
 client/tilespec.c                    |  2 +-
 common/city.c                        | 10 +++++-----
 common/city.h                        |  3 ++-
 common/scriptcore/api_game_methods.c |  4 +++-
 server/citytools.c                   |  4 +++-
 10 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 7e0ec39936..8c16656bd8 100644
--- a/ai/default/daisettler.c
+++ b/ai/default/daisettler.c
@@ -716,11 +716,12 @@ struct cityresult *city_desirability(struct ai_type *ait, struct player *pplayer
 {
   struct city *pcity = tile_city(ptile);
   struct cityresult *cr = NULL;
+  const struct civ_map *nmap = &(wld.map);
 
   fc_assert_ret_val(punit, NULL);
   fc_assert_ret_val(pplayer, NULL);
 
-  if (!city_can_be_built_here(ptile, punit, FALSE)
+  if (!city_can_be_built_here(nmap, ptile, punit, FALSE)
       || (has_handicap(pplayer, H_MAP)
           && !map_is_known(ptile, pplayer))) {
     return NULL;
@@ -1053,7 +1054,7 @@ BUILD_CITY:
 
     /* Check that the mission is still possible. If the tile has become
      * unavailable, call it off. */
-    if (!city_can_be_built_here(ptile, punit, FALSE)) {
+    if (!city_can_be_built_here(nmap, ptile, punit, FALSE)) {
       dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
       set_unit_activity(punit, ACTIVITY_IDLE);
       send_unit_info(NULL, punit);
diff --git a/client/gui-gtk-3.0/editprop.c b/client/gui-gtk-3.0/editprop.c
index f902f2cdba..30fbcf8916 100644
--- a/client/gui-gtk-3.0/editprop.c
+++ b/client/gui-gtk-3.0/editprop.c
@@ -5807,7 +5807,8 @@ static void property_page_create_objects(struct property_page *pp,
     if (pplayer && hint_tiles) {
       tile_list_iterate(hint_tiles, atile) {
         if (!is_enemy_unit_tile(atile, pplayer)
-            && city_can_be_built_here(atile, NULL, FALSE)) {
+            && city_can_be_built_here(&(wld.map), atile,
+                                      NULL, FALSE)) {
           ptile = atile;
           break;
         }
diff --git a/client/gui-gtk-3.22/editprop.c b/client/gui-gtk-3.22/editprop.c
index 4215572a2c..8e65754396 100644
--- a/client/gui-gtk-3.22/editprop.c
+++ b/client/gui-gtk-3.22/editprop.c
@@ -5807,7 +5807,7 @@ static void property_page_create_objects(struct property_page *pp,
     if (pplayer && hint_tiles) {
       tile_list_iterate(hint_tiles, atile) {
         if (!is_enemy_unit_tile(atile, pplayer)
-            && city_can_be_built_here(atile, NULL, FALSE)) {
+            && city_can_be_built_here(&(wld.map), atile, NULL, FALSE)) {
           ptile = atile;
           break;
         }
diff --git a/client/gui-gtk-4.0/editprop.c b/client/gui-gtk-4.0/editprop.c
index 811610cba1..07fd4c26c9 100644
--- a/client/gui-gtk-4.0/editprop.c
+++ b/client/gui-gtk-4.0/editprop.c
@@ -5832,7 +5832,8 @@ static void property_page_create_objects(struct property_page *pp,
     if (pplayer && hint_tiles) {
       tile_list_iterate(hint_tiles, atile) {
         if (!is_enemy_unit_tile(atile, pplayer)
-            && city_can_be_built_here(atile, NULL, FALSE)) {
+            && city_can_be_built_here(&(wld.map), atile,
+                                      NULL, FALSE)) {
           ptile = atile;
           break;
         }
diff --git a/client/mapview_common.c b/client/mapview_common.c
index e2ad7617f9..07dcf60a78 100644
--- a/client/mapview_common.c
+++ b/client/mapview_common.c
@@ -2864,7 +2864,8 @@ struct city *find_city_or_settler_near_tile(const struct tile *ptile,
         if ((NULL == client.conn.playing
              || unit_owner(psettler) == client.conn.playing)
             && unit_can_do_action(psettler, ACTION_FOUND_CITY)
-            && city_can_be_built_here(unit_tile(psettler), psettler, FALSE)) {
+            && city_can_be_built_here(&(wld.map), unit_tile(psettler),
+                                      psettler, FALSE)) {
           if (closest_settler == NULL) {
             closest_settler = psettler;
           }
diff --git a/client/tilespec.c b/client/tilespec.c
index a3f1d18561..7924853548 100644
--- a/client/tilespec.c
+++ b/client/tilespec.c
@@ -5277,7 +5277,7 @@ bool unit_drawn_with_city_outline(const struct unit *punit, bool check_focus)
          && unit_is_cityfounder(punit)
          && !unit_has_orders(punit)
          && (client_tile_get_known(unit_tile(punit)) != TILE_UNKNOWN
-             && city_can_be_built_here(unit_tile(punit), punit, FALSE))
+             && city_can_be_built_here(&(wld.map), unit_tile(punit), punit, FALSE))
          && (!check_focus || unit_is_in_focus(punit));
 }
 
diff --git a/common/city.c b/common/city.c
index d4bb4aca50..9f3ba7d3d4 100644
--- a/common/city.c
+++ b/common/city.c
@@ -1453,12 +1453,11 @@ bool citymindist_prevents_city_on_tile(const struct civ_map *nmap,
   punit is the founding unit. It may be NULL if a city is built out of the
   blue (e.g., through editing).
 **************************************************************************/
-bool city_can_be_built_here(const struct tile *ptile,
+bool city_can_be_built_here(const struct civ_map *nmap,
+                            const struct tile *ptile,
                             const struct unit *punit,
                             bool hut_test)
 {
-  struct civ_map *nmap = &(wld.map);
-
   if (!city_can_be_built_tile_only(nmap, ptile)) {
     return FALSE;
   }
@@ -3538,6 +3537,7 @@ void city_rally_point_receive(const struct packet_city_rally_point *packet,
                               struct city *pcity)
 {
   struct unit_order *checked_orders;
+  const struct civ_map *nmap = &(wld.map);
 
   if (pcity == NULL) {
     /* Probably lost. */
@@ -3563,8 +3563,8 @@ void city_rally_point_receive(const struct packet_city_rally_point *packet,
       pcity->rally_point.orders = NULL;
     }
   } else {
-    checked_orders = create_unit_orders(&(wld.map),
-                                        packet->length, packet->orders);
+    checked_orders = create_unit_orders(nmap, packet->length,
+                                        packet->orders);
     if (!checked_orders) {
       pcity->rally_point.length = 0;
       log_error("invalid rally point orders for city number %d.",
diff --git a/common/city.h b/common/city.h
index 9ecbddbaeb..d1f1489d9a 100644
--- a/common/city.h
+++ b/common/city.h
@@ -676,7 +676,8 @@ bool city_can_work_tile(const struct city *pcity, 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,
+bool city_can_be_built_here(const struct civ_map *nmap,
+                            const struct tile *ptile,
                             const struct unit *punit,
                             bool hut_test);
 bool city_can_be_built_tile_only(const struct civ_map *nmap,
diff --git a/common/scriptcore/api_game_methods.c b/common/scriptcore/api_game_methods.c
index 2a1b237e2b..9532cf613f 100644
--- a/common/scriptcore/api_game_methods.c
+++ b/common/scriptcore/api_game_methods.c
@@ -1174,10 +1174,12 @@ int api_methods_tile_sq_distance(lua_State *L, Tile *ptile1, Tile *ptile2)
 **************************************************************************/
 bool api_methods_unit_city_can_be_built_here(lua_State *L, Unit *punit)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   LUASCRIPT_CHECK_STATE(L, FALSE);
   LUASCRIPT_CHECK_SELF(L, punit, FALSE);
 
-  return city_can_be_built_here(unit_tile(punit), punit, TRUE);
+  return city_can_be_built_here(nmap, unit_tile(punit), punit, TRUE);
 }
 
 /**********************************************************************//**
diff --git a/server/citytools.c b/server/citytools.c
index ae8fe386e1..2b6ec055b6 100644
--- a/server/citytools.c
+++ b/server/citytools.c
@@ -1649,8 +1649,10 @@ void create_city(struct player *pplayer, struct tile *ptile,
 bool create_city_for_player(struct player *pplayer, struct tile *ptile,
                             const char *name)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (is_enemy_unit_tile(ptile, pplayer)
-      || !city_can_be_built_here(ptile, NULL, FALSE)) {
+      || !city_can_be_built_here(nmap, ptile, NULL, FALSE)) {
     return FALSE;
   }
 
-- 
2.43.0

