From 850977055257b2e41589911d2899a5050b36a0a5 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 25 Aug 2024 23:03:01 +0300
Subject: [PATCH 8/8] Unhardcode wld.map from can_unit_change_homecity_to()

See RM #809

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-3.0/citydlg.c  |  3 ++-
 client/gui-gtk-3.0/menu.c     |  2 +-
 client/gui-gtk-3.22/citydlg.c |  3 ++-
 client/gui-gtk-3.22/menu.c    |  2 +-
 client/gui-gtk-4.0/citydlg.c  |  2 +-
 client/gui-gtk-4.0/menu.c     |  2 +-
 client/gui-qt/citydlg.cpp     |  2 +-
 client/gui-qt/hudwidget.cpp   |  2 +-
 client/gui-qt/menu.cpp        |  2 +-
 client/gui-sdl2/menu.c        |  2 +-
 common/unit.c                 | 10 +++++-----
 common/unit.h                 |  6 ++++--
 12 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/client/gui-gtk-3.0/citydlg.c b/client/gui-gtk-3.0/citydlg.c
index e457c9049b..181b4b43b7 100644
--- a/client/gui-gtk-3.0/citydlg.c
+++ b/client/gui-gtk-3.0/citydlg.c
@@ -2570,7 +2570,8 @@ static gboolean present_unit_callback(GtkWidget *w, GdkEventButton *ev,
       G_CALLBACK(unit_homecity_callback),
       GINT_TO_POINTER(punit->id));
     gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-    gtk_widget_set_sensitive(item, can_unit_change_homecity_to(punit, pcity));
+    gtk_widget_set_sensitive(item, can_unit_change_homecity_to(&(wld.map),
+                                                               punit, pcity));
 
     item = gtk_menu_item_new_with_mnemonic(_("U_pgrade unit"));
     gtk_widget_set_sensitive(item, action_ever_possible(ACTION_UPGRADE_UNIT));
diff --git a/client/gui-gtk-3.0/menu.c b/client/gui-gtk-3.0/menu.c
index ae6f34c6fb..b3c2e474af 100644
--- a/client/gui-gtk-3.0/menu.c
+++ b/client/gui-gtk-3.0/menu.c
@@ -2533,7 +2533,7 @@ void real_menus_update(void)
                            units_can_upgrade(&(wld.map), punits));
   /* "UNIT_CONVERT" dealt with below */
   menu_entry_set_sensitive("UNIT_HOMECITY",
-                           can_units_do(punits, can_unit_change_homecity));
+                           can_units_do_on_map(&(wld.map), punits, can_unit_change_homecity));
   menu_entry_set_sensitive("UNIT_UNLOAD_TRANSPORTER",
                            units_are_occupied(punits));
   menu_entry_set_sensitive("UNIT_BOARD",
diff --git a/client/gui-gtk-3.22/citydlg.c b/client/gui-gtk-3.22/citydlg.c
index 8b13f36ff5..0d2fec379e 100644
--- a/client/gui-gtk-3.22/citydlg.c
+++ b/client/gui-gtk-3.22/citydlg.c
@@ -2586,7 +2586,8 @@ static gboolean present_unit_callback(GtkWidget *w, GdkEventButton *ev,
       G_CALLBACK(unit_homecity_callback),
       GINT_TO_POINTER(punit->id));
     gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
-    gtk_widget_set_sensitive(item, can_unit_change_homecity_to(punit, pcity));
+    gtk_widget_set_sensitive(item, can_unit_change_homecity_to(&(wld.map),
+                                                               punit, pcity));
 
     item = gtk_menu_item_new_with_mnemonic(_("U_pgrade unit"));
     gtk_widget_set_sensitive(item, action_ever_possible(ACTION_UPGRADE_UNIT));
diff --git a/client/gui-gtk-3.22/menu.c b/client/gui-gtk-3.22/menu.c
index 0fcc61694f..704a83480a 100644
--- a/client/gui-gtk-3.22/menu.c
+++ b/client/gui-gtk-3.22/menu.c
@@ -2559,7 +2559,7 @@ void real_menus_update(void)
                            units_can_upgrade(&(wld.map), punits));
   /* "UNIT_CONVERT" dealt with below */
   menu_entry_set_sensitive("UNIT_HOMECITY",
-                           can_units_do(punits, can_unit_change_homecity));
+                           can_units_do_on_map(&(wld.map), punits, can_unit_change_homecity));
   menu_entry_set_sensitive("UNIT_UNLOAD_TRANSPORTER",
                            units_are_occupied(punits));
   menu_entry_set_sensitive("UNIT_BOARD",
diff --git a/client/gui-gtk-4.0/citydlg.c b/client/gui-gtk-4.0/citydlg.c
index b525d2439a..4bf8db0f4c 100644
--- a/client/gui-gtk-4.0/citydlg.c
+++ b/client/gui-gtk-4.0/citydlg.c
@@ -2586,7 +2586,7 @@ static bool create_unit_menu(struct city_dialog *pdialog, struct unit *punit,
     g_signal_connect(act, "activate", G_CALLBACK(unit_homecity_callback),
                      GINT_TO_POINTER(punit->id));
     g_simple_action_set_enabled(G_SIMPLE_ACTION(act),
-                                can_unit_change_homecity_to(punit,
+                                can_unit_change_homecity_to(&(wld.map), punit,
                                                             pdialog->pcity));
     menu_item_append_unref(menu,
                            g_menu_item_new(action_id_name_translation(ACTION_HOME_CITY),
diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c
index b9391fc47f..aa6ca8a084 100644
--- a/client/gui-gtk-4.0/menu.c
+++ b/client/gui-gtk-4.0/menu.c
@@ -3801,7 +3801,7 @@ void real_menus_update(void)
                            units_have_activity_on_tile(punits,
                                                        ACTIVITY_SENTRY));
   menu_entry_set_sensitive(map, "UNIT_HOMECITY",
-                           can_units_do(punits, can_unit_change_homecity));
+                           can_units_do_on_map(&(wld.map), punits, can_unit_change_homecity));
   menu_entry_set_sensitive(map, "UNIT_UPGRADE", units_can_upgrade(&(wld.map), punits));
   menu_entry_set_sensitive(map, "UNIT_CONVERT", units_can_convert(&(wld.map), punits));
   menu_entry_set_sensitive(map, "UNIT_DISBAND",
diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp
index a7261d03ca..1d6f26ce7c 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -836,7 +836,7 @@ void unit_item::create_actions()
     disband_action = NULL;
   }
 
-  if (can_unit_change_homecity(qunit)) {
+  if (can_unit_change_homecity(&(wld.map), qunit)) {
     change_home = new QAction(action_id_name_translation(ACTION_HOME_CITY),
                               this);
     connect(change_home, &QAction::triggered, this, &unit_item::change_homecity);
diff --git a/client/gui-qt/hudwidget.cpp b/client/gui-qt/hudwidget.cpp
index 6b814ae32d..ad9479adcd 100644
--- a/client/gui-qt/hudwidget.cpp
+++ b/client/gui-qt/hudwidget.cpp
@@ -1086,7 +1086,7 @@ int unit_actions::update_actions()
 
   // Set homecity
   if (tile_city(unit_tile(current_unit))) {
-    if (can_unit_change_homecity_to(current_unit,
+    if (can_unit_change_homecity_to(&(wld.map), current_unit,
                                     tile_city(unit_tile(current_unit)))) {
       a = new hud_action(this);
       a->action_shortcut = SC_SETHOME;
diff --git a/client/gui-qt/menu.cpp b/client/gui-qt/menu.cpp
index 98dd267889..ca571312c2 100644
--- a/client/gui-qt/menu.cpp
+++ b/client/gui-qt/menu.cpp
@@ -2444,7 +2444,7 @@ void mr_menu::menus_sensitive()
         break;
 
       case HOMECITY:
-        if (can_units_do(punits, can_unit_change_homecity)) {
+        if (can_units_do_on_map(&(wld.map), punits, can_unit_change_homecity)) {
           i.value()->setEnabled(true);
         }
         break;
diff --git a/client/gui-sdl2/menu.c b/client/gui-sdl2/menu.c
index 7fc2640b83..8f8003417a 100644
--- a/client/gui-sdl2/menu.c
+++ b/client/gui-sdl2/menu.c
@@ -1337,7 +1337,7 @@ void real_menus_update(void)
         local_hide(ID_UNIT_ORDER_PILLAGE);
       }
 
-      if (pcity && can_unit_change_homecity(punit)
+      if (pcity != NULL && can_unit_change_homecity(&(wld.map), punit)
           && pcity->id != punit->homecity) {
         local_show(ID_UNIT_ORDER_HOMECITY);
       } else {
diff --git a/common/unit.c b/common/unit.c
index 65af74d353..df5b96dee2 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -441,11 +441,10 @@ bool unit_can_add_or_build_city(const struct civ_map *nmap,
 /**********************************************************************//**
   Return TRUE iff the unit can change homecity to the given city.
 **************************************************************************/
-bool can_unit_change_homecity_to(const struct unit *punit,
+bool can_unit_change_homecity_to(const struct civ_map *nmap,
+                                 const struct unit *punit,
 				 const struct city *pcity)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   if (pcity == NULL) {
     /* Can't change home city to a non existing city. */
     return FALSE;
@@ -458,9 +457,10 @@ bool can_unit_change_homecity_to(const struct unit *punit,
 /**********************************************************************//**
   Return TRUE iff the unit can change homecity at its current location.
 **************************************************************************/
-bool can_unit_change_homecity(const struct unit *punit)
+bool can_unit_change_homecity(const struct civ_map *nmap,
+                              const struct unit *punit)
 {
-  return can_unit_change_homecity_to(punit, tile_city(unit_tile(punit)));
+  return can_unit_change_homecity_to(nmap, punit, tile_city(unit_tile(punit)));
 }
 
 /**********************************************************************//**
diff --git a/common/unit.h b/common/unit.h
index c766311c1a..18214c6f35 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -331,9 +331,11 @@ bool can_unit_alight_or_be_unloaded(const struct civ_map *nmap,
                                     const struct unit *pcargo,
                                     const struct unit *ptrans);
 bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit);
-bool can_unit_change_homecity_to(const struct unit *punit,
+bool can_unit_change_homecity_to(const struct civ_map *nmap,
+                                 const struct unit *punit,
                                  const struct city *pcity);
-bool can_unit_change_homecity(const struct unit *punit);
+bool can_unit_change_homecity(const struct civ_map *nmap,
+                              const struct unit *punit);
 const char *get_activity_text(enum unit_activity activity);
 bool can_unit_continue_current_activity(const struct civ_map *nmap,
                                         struct unit *punit);
-- 
2.45.2

