From 7df8d2115876252d2eee79a12ca2a91d50e996cd Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 4 Jun 2024 00:16:45 +0300
Subject: [PATCH 67/67] Unhardcode wld.map from can_city_build_unit_later()

See RM #699

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daimilitary.c |  2 +-
 ai/default/daitech.c     |  4 ++--
 client/climisc.c         | 10 ++++++----
 client/gui-sdl2/wldlg.c  |  5 +++--
 client/gui-sdl3/wldlg.c  |  5 +++--
 common/city.c            |  9 +++++----
 common/city.h            |  3 ++-
 server/cityturn.c        |  3 ++-
 8 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/ai/default/daimilitary.c b/ai/default/daimilitary.c
index 0d3f1491d5..7505593dfd 100644
--- a/ai/default/daimilitary.c
+++ b/ai/default/daimilitary.c
@@ -1130,7 +1130,7 @@ bool dai_process_defender_want(struct ai_type *ait, struct player *pplayer,
           best_unit_type = punittype;
           best_unit_cost = build_cost;
         }
-      } else if (can_city_build_unit_later(pcity, punittype)) {
+      } else if (can_city_build_unit_later(nmap, pcity, punittype)) {
         /* We first need to develop the tech required by the unit... */
 
         /* Cost (shield equivalent) of gaining these techs. */
diff --git a/ai/default/daitech.c b/ai/default/daitech.c
index 5a1d53814a..09db00dac7 100644
--- a/ai/default/daitech.c
+++ b/ai/default/daitech.c
@@ -429,7 +429,7 @@ struct unit_type *dai_wants_defender_against(struct ai_type *ait,
   unit_type_iterate(deftype) {
     if (def_values[utype_index(deftype)] > best_avl_def
         && !can_city_build_unit_now(nmap, pcity, deftype)
-        && can_city_build_unit_later(pcity, deftype)) {
+        && can_city_build_unit_later(nmap, pcity, deftype)) {
       /* It would be better than current best. Consider researching tech */
       const struct impr_type *building;
       int cost = 0;
@@ -537,7 +537,7 @@ struct unit_type *dai_wants_role_unit(struct ai_type *ait, struct player *pplaye
     if (can_city_build_unit_now(nmap, pcity, iunit)) {
       build_unit = iunit;
       break;
-    } else if (can_city_build_unit_later(pcity, iunit)) {
+    } else if (can_city_build_unit_later(nmap, pcity, iunit)) {
       const struct impr_type *building;
       struct advance *itech = A_NEVER;
       int cost = 0;
diff --git a/client/climisc.c b/client/climisc.c
index 620972c85e..359c0f1392 100644
--- a/client/climisc.c
+++ b/client/climisc.c
@@ -431,6 +431,7 @@ void center_on_something(void)
 {
   struct city *pcity;
   struct unit *punit;
+  const struct civ_map *nmap = &(wld.map);
 
   if (!can_client_change_view()) {
     return;
@@ -456,7 +457,7 @@ void center_on_something(void)
     fc_assert_ret(punit != NULL);
     center_tile_mapcanvas(unit_tile(punit));
   } else {
-    struct tile *ctile = native_pos_to_tile(&(wld.map), MAP_NATIVE_WIDTH / 2,
+    struct tile *ctile = native_pos_to_tile(nmap, MAP_NATIVE_WIDTH / 2,
                                             MAP_NATIVE_HEIGHT / 2);
 
     /* Just any known tile will do; search near the middle first. */
@@ -464,7 +465,7 @@ void center_on_something(void)
      * is guaranteed to be larger than the map will be. Although this is
      * a misuse of map.xsize and map.ysize (which are native dimensions),
      * it should give a sufficiently large radius. */
-    iterate_outward(&(wld.map), ctile, MAP_NATIVE_WIDTH + MAP_NATIVE_HEIGHT, ptile) {
+    iterate_outward(nmap, ctile, MAP_NATIVE_WIDTH + MAP_NATIVE_HEIGHT, ptile) {
       if (client_tile_get_known(ptile) != TILE_UNKNOWN) {
         ctile = ptile;
         break;
@@ -812,6 +813,7 @@ int collect_eventually_buildable_targets(struct universal *targets,
 {
   struct player *pplayer = client_player();
   int cids_used = 0;
+  const struct civ_map *nmap = &(wld.map);
 
   improvement_iterate(pimprove) {
     bool can_build;
@@ -860,8 +862,8 @@ int collect_eventually_buildable_targets(struct universal *targets,
 
     if (NULL != pcity) {
       /* Can the city build? */
-      can_build = can_city_build_unit_now(&(wld.map), pcity, punittype);
-      can_eventually_build = can_city_build_unit_later(pcity, punittype);
+      can_build = can_city_build_unit_now(nmap, pcity, punittype);
+      can_eventually_build = can_city_build_unit_later(nmap, pcity, punittype);
     } else if (NULL != pplayer) {
       /* Can our player build? */
       can_build = can_player_build_unit_now(pplayer, punittype);
diff --git a/client/gui-sdl2/wldlg.c b/client/gui-sdl2/wldlg.c
index 603fd64fc0..eb6993ddd7 100644
--- a/client/gui-sdl2/wldlg.c
+++ b/client/gui-sdl2/wldlg.c
@@ -1077,6 +1077,7 @@ void popup_worklist_editor(struct city *pcity, struct global_worklist *gwl)
   bool can_build, can_eventually_build;
   SDL_Rect area;
   int len;
+  const struct civ_map *nmap = &(wld.map);
 
   if (editor) {
     return;
@@ -1592,9 +1593,9 @@ void popup_worklist_editor(struct city *pcity, struct global_worklist *gwl)
 
     /* If there's a city, can the city build the unit? */
     if (pcity) {
-      can_build = can_build && can_city_build_unit_now(&(wld.map), pcity, un);
+      can_build = can_build && can_city_build_unit_now(nmap, pcity, un);
       can_eventually_build = can_eventually_build
-        && can_city_build_unit_later(pcity, un);
+        && can_city_build_unit_later(nmap, pcity, un);
     }
 
     if ((advanced_tech && can_eventually_build)
diff --git a/client/gui-sdl3/wldlg.c b/client/gui-sdl3/wldlg.c
index 06374a13ac..1672e6b7b6 100644
--- a/client/gui-sdl3/wldlg.c
+++ b/client/gui-sdl3/wldlg.c
@@ -1073,6 +1073,7 @@ void popup_worklist_editor(struct city *pcity, struct global_worklist *gwl)
   bool can_build, can_eventually_build;
   SDL_Rect area;
   int len;
+  const struct civ_map *nmap = &(wld.map);
 
   if (editor) {
     return;
@@ -1588,9 +1589,9 @@ void popup_worklist_editor(struct city *pcity, struct global_worklist *gwl)
 
     /* If there's a city, can the city build the unit? */
     if (pcity) {
-      can_build = can_build && can_city_build_unit_now(&(wld.map), pcity, un);
+      can_build = can_build && can_city_build_unit_now(nmap, pcity, un);
       can_eventually_build = can_eventually_build
-        && can_city_build_unit_later(pcity, un);
+        && can_city_build_unit_later(nmap, pcity, un);
     }
 
     if ((advanced_tech && can_eventually_build)
diff --git a/common/city.c b/common/city.c
index d50c2abc95..4bebce890e 100644
--- a/common/city.c
+++ b/common/city.c
@@ -967,11 +967,10 @@ bool can_city_build_unit_now(const struct civ_map *nmap,
   Returns whether player can eventually build given unit in the city;
   returns FALSE if unit can never possibly be built in this city.
 **************************************************************************/
-bool can_city_build_unit_later(const struct city *pcity,
+bool can_city_build_unit_later(const struct civ_map *nmap,
+                               const struct city *pcity,
 			       const struct unit_type *punittype)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   /* Can the _player_ ever build this unit? */
   if (!can_player_build_unit_later(city_owner(pcity), punittype)) {
     return FALSE;
@@ -1034,9 +1033,11 @@ bool can_city_build_now(const struct city *pcity,
 bool can_city_build_later(const struct city *pcity,
                           const struct universal *target)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   switch (target->kind) {
   case VUT_UTYPE:
-    return can_city_build_unit_later(pcity, target->value.utype);
+    return can_city_build_unit_later(nmap, pcity, target->value.utype);
   case VUT_IMPROVEMENT:
     return can_city_build_improvement_later(pcity, target->value.building);
   default:
diff --git a/common/city.h b/common/city.h
index b2066dfd5c..b744b69391 100644
--- a/common/city.h
+++ b/common/city.h
@@ -607,7 +607,8 @@ bool can_city_build_improvement_now(const struct city *pcity,
 bool can_city_build_unit_direct(const struct civ_map *nmap,
                                 const struct city *pcity,
                                 const struct unit_type *punittype);
-bool can_city_build_unit_later(const struct city *pcity,
+bool can_city_build_unit_later(const struct civ_map *nmap,
+                               const struct city *pcity,
                                const struct unit_type *punittype);
 bool can_city_build_unit_now(const struct civ_map *nmap,
                              const struct city *pcity,
diff --git a/server/cityturn.c b/server/cityturn.c
index 44581d3368..543102ce2d 100644
--- a/server/cityturn.c
+++ b/server/cityturn.c
@@ -2191,6 +2191,7 @@ static bool worklist_change_build_target(struct player *pplayer,
   int saved_id = pcity->id;
   bool city_checked = TRUE; /* This is used to avoid spurious city_exist() calls */
   struct worklist *pwl = &pcity->worklist;
+  const struct civ_map *nmap = &(wld.map);
 
   if (worklist_is_empty(pwl)) {
     /* Nothing in the worklist; bail now. */
@@ -2271,7 +2272,7 @@ static bool worklist_change_build_target(struct player *pplayer,
         city_checked = FALSE;
 	break;
       } else {
-        purge = !can_city_build_unit_later(pcity, pupdate);
+        purge = !can_city_build_unit_later(nmap, pcity, pupdate);
       }
       if (purge) {
 	/* If the city can never build this unit or its descendants,
-- 
2.43.0

