From 810abf7b592214abbc2bc703f53eb801b5b10962 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 25 Aug 2024 22:58:35 +0300
Subject: [PATCH 19/19] Unhardcode wld.map from can_unit_change_homecity_to()

See RM #809

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 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-gtk-5.0/citydlg.c  |  2 +-
 client/gui-gtk-5.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 +-
 client/gui-sdl3/menu.c        |  2 +-
 common/unit.c                 | 10 +++++-----
 common/unit.h                 |  6 ++++--
 13 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/client/gui-gtk-3.22/citydlg.c b/client/gui-gtk-3.22/citydlg.c
index cce8847384..e44059b661 100644
--- a/client/gui-gtk-3.22/citydlg.c
+++ b/client/gui-gtk-3.22/citydlg.c
@@ -2688,7 +2688,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 fc80b4bff4..c24d791d33 100644
--- a/client/gui-gtk-3.22/menu.c
+++ b/client/gui-gtk-3.22/menu.c
@@ -2605,7 +2605,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 e474c992d8..9d152770bc 100644
--- a/client/gui-gtk-4.0/citydlg.c
+++ b/client/gui-gtk-4.0/citydlg.c
@@ -2674,7 +2674,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 df250812cf..821ac80cfe 100644
--- a/client/gui-gtk-4.0/menu.c
+++ b/client/gui-gtk-4.0/menu.c
@@ -3823,7 +3823,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-gtk-5.0/citydlg.c b/client/gui-gtk-5.0/citydlg.c
index 0e5796a79d..865e524f9b 100644
--- a/client/gui-gtk-5.0/citydlg.c
+++ b/client/gui-gtk-5.0/citydlg.c
@@ -2674,7 +2674,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-5.0/menu.c b/client/gui-gtk-5.0/menu.c
index bd61c2f204..f2d6cbfb97 100644
--- a/client/gui-gtk-5.0/menu.c
+++ b/client/gui-gtk-5.0/menu.c
@@ -3827,7 +3827,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 9472db4ed0..7f75f080ab 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -825,7 +825,7 @@ void unit_item::create_actions()
     disband_action = nullptr;
   }
 
-  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 9d7d8d24e1..6536217e0c 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 8034b9ecea..ea26cd7127 100644
--- a/client/gui-qt/menu.cpp
+++ b/client/gui-qt/menu.cpp
@@ -2436,7 +2436,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 4d320b79ca..f1957b3376 100644
--- a/client/gui-sdl2/menu.c
+++ b/client/gui-sdl2/menu.c
@@ -1306,7 +1306,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/client/gui-sdl3/menu.c b/client/gui-sdl3/menu.c
index 25c2179060..1c0ac0384a 100644
--- a/client/gui-sdl3/menu.c
+++ b/client/gui-sdl3/menu.c
@@ -1302,7 +1302,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 1e00a5822e..748e0250cd 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -486,11 +486,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;
@@ -503,9 +502,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 08362346f1..f7c0953cbc 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -332,9 +332,11 @@ bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap,
                                      const struct unit *ptrans);
 bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit);
 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

