From a1277ed9fb14ab92918ae022d0fab7abfdf86acf Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Thu, 22 Aug 2024 21:47:37 +0300
Subject: [PATCH 19/19] 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.22/menu.c  | 5 +++--
 client/gui-gtk-4.0/menu.c   | 5 +++--
 client/gui-gtk-5.0/menu.c   | 5 +++--
 client/gui-qt/hudwidget.cpp | 2 +-
 client/gui-qt/menu.cpp      | 5 +++--
 client/gui-sdl2/menu.c      | 4 ++--
 client/gui-sdl3/menu.c      | 4 ++--
 common/unit.c               | 4 ++--
 common/unit.h               | 3 ++-
 9 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/client/gui-gtk-3.22/menu.c b/client/gui-gtk-3.22/menu.c
index 26fe7a8f61..fc80b4bff4 100644
--- a/client/gui-gtk-3.22/menu.c
+++ b/client/gui-gtk-3.22/menu.c
@@ -1766,7 +1766,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)) {
@@ -2563,7 +2563,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 f2c4ce4c8c..df250812cf 100644
--- a/client/gui-gtk-4.0/menu.c
+++ b/client/gui-gtk-4.0/menu.c
@@ -2445,7 +2445,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)) {
@@ -3779,7 +3779,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-gtk-5.0/menu.c b/client/gui-gtk-5.0/menu.c
index 020c437d04..bd61c2f204 100644
--- a/client/gui-gtk-5.0/menu.c
+++ b/client/gui-gtk-5.0/menu.c
@@ -2449,7 +2449,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)) {
@@ -3783,7 +3783,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 323bac8dc7..9d7d8d24e1 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 028537f81d..8034b9ecea 100644
--- a/client/gui-qt/menu.cpp
+++ b/client/gui-qt/menu.cpp
@@ -2350,7 +2350,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
@@ -2639,7 +2640,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 76d882bc9f..4d320b79ca 100644
--- a/client/gui-sdl2/menu.c
+++ b/client/gui-sdl2/menu.c
@@ -1083,8 +1083,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/client/gui-sdl3/menu.c b/client/gui-sdl3/menu.c
index 323140b02c..25c2179060 100644
--- a/client/gui-sdl3/menu.c
+++ b/client/gui-sdl3/menu.c
@@ -1079,8 +1079,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 5e19ccd15a..825ba55d40 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -469,10 +469,10 @@ bool is_hiding_unit(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 872b4d1334..08362346f1 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -386,7 +386,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);
 
 struct astring; /* Forward declaration. */
 
-- 
2.43.0

