From 5f3bb2a9799c6660adddb09fcdeb3b2e50240199 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Thu, 22 Aug 2024 21:51:50 +0300
Subject: [PATCH 8/8] Unhardcode wld.map from unit_can_add_or_build_city()

See RM #799

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-3.0/menu.c   | 5 +++--
 client/gui-gtk-3.22/menu.c  | 5 +++--
 client/gui-gtk-4.0/menu.c   | 5 +++--
 client/gui-qt/hudwidget.cpp | 2 +-
 client/gui-qt/menu.cpp      | 5 +++--
 client/gui-sdl2/menu.c      | 4 ++--
 common/unit.c               | 4 ++--
 common/unit.h               | 3 ++-
 8 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/client/gui-gtk-3.0/menu.c b/client/gui-gtk-3.0/menu.c
index f90ab5dd19..ae6f34c6fb 100644
--- a/client/gui-gtk-3.0/menu.c
+++ b/client/gui-gtk-3.0/menu.c
@@ -1728,7 +1728,7 @@ static void build_city_callback(GtkMenuItem *item, gpointer data)
      * not good! */
     /* Enable the button for adding to a city in all cases, so we
        get an eventual error message from the server if we try. */
-    if (unit_can_add_or_build_city(punit)) {
+    if (unit_can_add_or_build_city(&(wld.map), punit)) {
       request_unit_build_city(punit);
     } else if (utype_can_do_action(unit_type_get(punit),
                                    ACTION_HELP_WONDER)) {
@@ -2489,7 +2489,8 @@ void real_menus_update(void)
   /* Enable the button for adding to a city in all cases, so we
    * get an eventual error message from the server if we try. */
   menu_entry_set_sensitive("BUILD_CITY",
-            (can_units_do(punits, unit_can_add_or_build_city)
+                           (can_units_do_on_map(&(wld.map), punits,
+                                                unit_can_add_or_build_city)
              || can_units_do_on_map(&(wld.map), punits,
                                     unit_can_help_build_wonder_here)));
   menu_entry_set_sensitive("BUILD_ROAD",
diff --git a/client/gui-gtk-3.22/menu.c b/client/gui-gtk-3.22/menu.c
index 969194aeb9..0fcc61694f 100644
--- a/client/gui-gtk-3.22/menu.c
+++ b/client/gui-gtk-3.22/menu.c
@@ -1753,7 +1753,7 @@ static void build_city_callback(GtkMenuItem *item, gpointer data)
      * not good! */
     /* Enable the button for adding to a city in all cases, so we
        get an eventual error message from the server if we try. */
-    if (unit_can_add_or_build_city(punit)) {
+    if (unit_can_add_or_build_city(&(wld.map), punit)) {
       request_unit_build_city(punit);
     } else if (utype_can_do_action(unit_type_get(punit),
                                    ACTION_HELP_WONDER)) {
@@ -2516,7 +2516,8 @@ void real_menus_update(void)
   /* Enable the button for adding to a city in all cases, so we
    * get an eventual error message from the server if we try. */
   menu_entry_set_sensitive("BUILD_CITY",
-            (can_units_do(punits, unit_can_add_or_build_city)
+                           (can_units_do_on_map(&(wld.map), punits,
+                                                unit_can_add_or_build_city)
              || can_units_do_on_map(&(wld.map), punits, unit_can_help_build_wonder_here)));
   menu_entry_set_sensitive("BUILD_ROAD",
                            (can_units_do_any_road(&(wld.map), punits)
diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c
index 9d2657c607..b9391fc47f 100644
--- a/client/gui-gtk-4.0/menu.c
+++ b/client/gui-gtk-4.0/menu.c
@@ -2408,7 +2408,7 @@ static void build_city_callback(GSimpleAction *action,
      * not good! */
     /* Enable the button for adding to a city in all cases, so we
        get an eventual error message from the server if we try. */
-    if (unit_can_add_or_build_city(punit)) {
+    if (unit_can_add_or_build_city(&(wld.map), punit)) {
       request_unit_build_city(punit);
     } else if (utype_can_do_action(unit_type_get(punit),
                                    ACTION_HELP_WONDER)) {
@@ -3759,7 +3759,8 @@ void real_menus_update(void)
   /* Enable the button for adding to a city in all cases, so we
    * get an eventual error message from the server if we try. */
   menu_entry_set_sensitive(map, "BUILD_CITY",
-                           (can_units_do(punits, unit_can_add_or_build_city)
+                           (can_units_do_on_map(&(wld.map), punits,
+                                                unit_can_add_or_build_city)
                             || can_units_do_on_map(&(wld.map), punits,
                                                    unit_can_help_build_wonder_here)));
 
diff --git a/client/gui-qt/hudwidget.cpp b/client/gui-qt/hudwidget.cpp
index d36eb4a7d2..6b814ae32d 100644
--- a/client/gui-qt/hudwidget.cpp
+++ b/client/gui-qt/hudwidget.cpp
@@ -993,7 +993,7 @@ int unit_actions::update_actions()
 
   /* Create possible actions */
 
-  if (unit_can_add_or_build_city(current_unit)) {
+  if (unit_can_add_or_build_city(&(wld.map), current_unit)) {
     a = new hud_action(this);
     a->action_shortcut = SC_BUILDCITY;
     a->set_pixmap(fc_icons::instance()->get_pixmap("home"));
diff --git a/client/gui-qt/menu.cpp b/client/gui-qt/menu.cpp
index f6e6648fe7..98dd267889 100644
--- a/client/gui-qt/menu.cpp
+++ b/client/gui-qt/menu.cpp
@@ -2348,7 +2348,8 @@ void mr_menu::menus_sensitive()
         break;
 
       case BUILD:
-        if (can_units_do(punits, unit_can_add_or_build_city)) {
+        if (can_units_do_on_map(&(wld.map), punits,
+                                unit_can_add_or_build_city)) {
           i.value()->setEnabled(true);
         }
         if (city_on_tile
@@ -2645,7 +2646,7 @@ void mr_menu::slot_build_city()
      * not good! */
     /* Enable the button for adding to a city in all cases, so we
        get an eventual error message from the server if we try. */
-    if (unit_can_add_or_build_city(punit)) {
+    if (unit_can_add_or_build_city(&(wld.map), punit)) {
       request_unit_build_city(punit);
     } else if (utype_can_do_action(unit_type_get(punit), ACTION_HELP_WONDER)) {
       request_unit_caravan_action(punit, ACTION_HELP_WONDER);
diff --git a/client/gui-sdl2/menu.c b/client/gui-sdl2/menu.c
index 42dc8fa7bb..7fc2640b83 100644
--- a/client/gui-sdl2/menu.c
+++ b/client/gui-sdl2/menu.c
@@ -1101,8 +1101,8 @@ void real_menus_update(void)
       /* Enable the button for adding to a city in all cases, so we
        * get an eventual error message from the server if we try. */
 
-      if (unit_can_add_or_build_city(punit)) {
-        if (pcity) {
+      if (unit_can_add_or_build_city(&(wld.map), punit)) {
+        if (pcity != NULL) {
           fc_snprintf(cbuf, sizeof(cbuf), "%s (%s)",
                       action_id_name_translation(ACTION_JOIN_CITY), "B");
         } else {
diff --git a/common/unit.c b/common/unit.c
index d25fdbd5e0..65af74d353 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -424,10 +424,10 @@ bool kills_citizen_after_attack(const struct unit *punit)
   Return TRUE iff this unit can add to a current city or build a new city
   at its current location.
 **************************************************************************/
-bool unit_can_add_or_build_city(const struct unit *punit)
+bool unit_can_add_or_build_city(const struct civ_map *nmap,
+                                const struct unit *punit)
 {
   struct city *tgt_city;
-  const struct civ_map *nmap = &(wld.map);
 
   if ((tgt_city = tile_city(unit_tile(punit)))) {
     return action_prob_possible(action_prob_vs_city(nmap, punit,
diff --git a/common/unit.h b/common/unit.h
index c4aff64656..c766311c1a 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -377,7 +377,8 @@ bool is_square_threatened(const struct civ_map *nmap,
                           const struct tile *ptile, bool omniscient);
 bool is_field_unit(const struct unit *punit);
 bool is_hiding_unit(const struct unit *punit);
-bool unit_can_add_or_build_city(const struct unit *punit);
+bool unit_can_add_or_build_city(const struct civ_map *nmap,
+                                const struct unit *punit);
 
 bool kills_citizen_after_attack(const struct unit *punit);
 
-- 
2.43.0

