From 32b31d207e7810da440613ab3acdca42d377b163 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 3 May 2024 20:04:23 +0300
Subject: [PATCH 37/37] 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.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 +++-
 9 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 0c98679ea2..1432d322d5 100644
--- a/ai/default/daisettler.c
+++ b/ai/default/daisettler.c
@@ -715,11 +715,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;
@@ -1054,7 +1055,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.22/editprop.c b/client/gui-gtk-3.22/editprop.c
index 99b8a63425..2e563f8ad6 100644
--- a/client/gui-gtk-3.22/editprop.c
+++ b/client/gui-gtk-3.22/editprop.c
@@ -5826,7 +5826,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 25b98f3864..02a27fc86c 100644
--- a/client/gui-gtk-4.0/editprop.c
+++ b/client/gui-gtk-4.0/editprop.c
@@ -5847,7 +5847,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 c01a0b31e3..c3cbb975b5 100644
--- a/client/mapview_common.c
+++ b/client/mapview_common.c
@@ -2879,7 +2879,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 9c1dec647c..de3a4a6360 100644
--- a/client/tilespec.c
+++ b/client/tilespec.c
@@ -5522,7 +5522,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 e969005c72..4810f2acd6 100644
--- a/common/city.c
+++ b/common/city.c
@@ -1480,12 +1480,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;
   }
@@ -3639,6 +3638,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 (NULL == pcity) {
     /* Probably lost. */
@@ -3664,8 +3664,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 %s.",
diff --git a/common/city.h b/common/city.h
index bd980e01fd..a5014da2b8 100644
--- a/common/city.h
+++ b/common/city.h
@@ -698,7 +698,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 23338c14aa..a8530ae663 100644
--- a/common/scriptcore/api_game_methods.c
+++ b/common/scriptcore/api_game_methods.c
@@ -1365,10 +1365,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 8c7995250c..2c2177d43e 100644
--- a/server/citytools.c
+++ b/server/citytools.c
@@ -1673,8 +1673,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

