From 15a7d385c1f345e64cc5bca30f911fa46771c4b9 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 7 Jan 2024 08:55:33 +0200
Subject: [PATCH 29/29] Unhardcode wld.map from unit_transform_result()

See RM #139

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/gui-gtk-3.22/menu.c           |  5 +++--
 client/gui-gtk-4.0/menu.c            |  6 +++---
 client/gui-qt/citydlg.cpp            |  2 +-
 client/gui-qt/hudwidget.cpp          |  2 +-
 client/gui-qt/menu.cpp               |  4 ++--
 client/gui-sdl2/dialogs.c            |  2 +-
 client/gui-sdl2/menu.c               |  2 +-
 client/gui-sdl3/dialogs.c            |  2 +-
 client/gui-sdl3/menu.c               |  2 +-
 client/text.c                        |  4 ++--
 common/actions.c                     |  3 ++-
 common/actres.c                      |  2 +-
 common/scriptcore/api_game_methods.c |  4 ++--
 common/unit.c                        | 24 ++++++++++++++----------
 common/unit.h                        | 11 +++++++----
 common/unitlist.c                    | 10 ++++++----
 common/unitlist.h                    |  6 ++++--
 server/scripting/api_server_edit.c   |  2 +-
 server/unithand.c                    |  4 ++--
 server/unittools.c                   |  4 ++--
 20 files changed, 57 insertions(+), 44 deletions(-)

diff --git a/client/gui-gtk-3.22/menu.c b/client/gui-gtk-3.22/menu.c
index 3afcf09119..1e09cb1daa 100644
--- a/client/gui-gtk-3.22/menu.c
+++ b/client/gui-gtk-3.22/menu.c
@@ -2597,7 +2597,7 @@ void real_menus_update(void)
                            units_can_do_action(punits, ACTION_DISBAND_UNIT,
                                                TRUE));
   menu_entry_set_sensitive("UNIT_UPGRADE",
-                           units_can_upgrade(punits));
+                           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));
@@ -2741,10 +2741,11 @@ void real_menus_update(void)
     menus_rename("UNIT_UPGRADE", _("Upgr_ade"));
   }
 
-  if (units_can_convert(punits)) {
+  if (units_can_convert(&(wld.map), punits)) {
     menu_entry_set_sensitive("UNIT_CONVERT", TRUE);
     if (units_all_same_type) {
       struct unit *punit = unit_list_get(punits, 0);
+
       /* TRANS: %s is a unit type. */
       fc_snprintf(acttext, sizeof(acttext), _("C_onvert to %s"),
                   utype_name_translation(unit_type_get(punit)->converted_to));
diff --git a/client/gui-gtk-4.0/menu.c b/client/gui-gtk-4.0/menu.c
index c6a8c84135..89db3ad506 100644
--- a/client/gui-gtk-4.0/menu.c
+++ b/client/gui-gtk-4.0/menu.c
@@ -3818,8 +3818,8 @@ void real_menus_update(void)
                                                        ACTIVITY_SENTRY));
   menu_entry_set_sensitive(map, "UNIT_HOMECITY",
                            can_units_do(punits, can_unit_change_homecity));
-  menu_entry_set_sensitive(map, "UNIT_UPGRADE", units_can_upgrade(punits));
-  menu_entry_set_sensitive(map, "UNIT_CONVERT", units_can_convert(punits));
+  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",
                            units_can_do_action(punits, ACTION_DISBAND_UNIT,
                                                TRUE));
@@ -3963,7 +3963,7 @@ void real_menus_update(void)
     menus_rename(unit_menu, 13, "UNIT_UPGRADE", _("Upgr_ade"));
   }
 
-  if (units_can_convert(punits)) {
+  if (units_can_convert(&(wld.map), punits)) {
     if (units_all_same_type && num_units > 0) {
       struct unit *punit = unit_list_get(punits, 0);
 
diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp
index 4c8753fa95..940b7640fd 100644
--- a/client/gui-qt/citydlg.cpp
+++ b/client/gui-qt/citydlg.cpp
@@ -865,7 +865,7 @@ void unit_item::create_actions()
     unload_trans = NULL;
   }
 
-  if (units_can_upgrade(qunits)) {
+  if (units_can_upgrade(&(wld.map), qunits)) {
     upgrade = new QAction(_("Upgrade Unit"), this);
     connect(upgrade, &QAction::triggered, this, &unit_item::upgrade_unit);
   } else {
diff --git a/client/gui-qt/hudwidget.cpp b/client/gui-qt/hudwidget.cpp
index 6955b3ec63..fc206de54e 100644
--- a/client/gui-qt/hudwidget.cpp
+++ b/client/gui-qt/hudwidget.cpp
@@ -1096,7 +1096,7 @@ int unit_actions::update_actions()
   }
 
   // Upgrade
-  if (UU_OK == unit_upgrade_test(current_unit, FALSE)) {
+  if (UU_OK == unit_upgrade_test(&(wld.map), current_unit, FALSE)) {
     a = new hud_action(this);
     a->action_shortcut = SC_UPGRADE_UNIT;
     a->set_pixmap(fc_icons::instance()->get_pixmap("upgrade"));
diff --git a/client/gui-qt/menu.cpp b/client/gui-qt/menu.cpp
index 79a2787dae..e1ffa1539e 100644
--- a/client/gui-qt/menu.cpp
+++ b/client/gui-qt/menu.cpp
@@ -2202,7 +2202,7 @@ void mr_menu::menus_sensitive()
         break;
 
       case CONVERT:
-        if (units_can_convert(punits)) {
+        if (units_can_convert(&(wld.map), punits)) {
           i.value()->setEnabled(true);
         }
         break;
@@ -2551,7 +2551,7 @@ void mr_menu::menus_sensitive()
         break;
 
       case UPGRADE:
-        if (units_can_upgrade(punits)) {
+        if (units_can_upgrade(&(wld.map), punits)) {
           i.value()->setEnabled(true);
         }
         break;
diff --git a/client/gui-sdl2/dialogs.c b/client/gui-sdl2/dialogs.c
index 674f8b92ae..2ec0633149 100644
--- a/client/gui-sdl2/dialogs.c
+++ b/client/gui-sdl2/dialogs.c
@@ -699,7 +699,7 @@ void popup_unit_upgrade_dlg(struct unit *punit, bool city)
 
   unit_upgrade_dlg = fc_calloc(1, sizeof(struct small_dialog));
 
-  unit_upgrade_result = unit_upgrade_info(punit, cbuf, sizeof(cbuf));
+  unit_upgrade_result = unit_upgrade_info(&(wld.map), punit, cbuf, sizeof(cbuf));
 
   pstr = create_utf8_from_char_fonto(_("Upgrade Obsolete Units"),
                                      FONTO_ATTENTION);
diff --git a/client/gui-sdl2/menu.c b/client/gui-sdl2/menu.c
index ddbcd2f04f..1f0617b057 100644
--- a/client/gui-sdl2/menu.c
+++ b/client/gui-sdl2/menu.c
@@ -1437,7 +1437,7 @@ void real_menus_update(void)
         local_hide(ID_UNIT_ORDER_UPGRADE);
       }
 
-      if (unit_can_convert(punit)) {
+      if (unit_can_convert(&(wld.map), punit)) {
         local_show(ID_UNIT_ORDER_CONVERT);
       } else {
         local_hide(ID_UNIT_ORDER_CONVERT);
diff --git a/client/gui-sdl3/dialogs.c b/client/gui-sdl3/dialogs.c
index b514e2f758..efd15332ca 100644
--- a/client/gui-sdl3/dialogs.c
+++ b/client/gui-sdl3/dialogs.c
@@ -695,7 +695,7 @@ void popup_unit_upgrade_dlg(struct unit *punit, bool city)
 
   unit_upgrade_dlg = fc_calloc(1, sizeof(struct small_dialog));
 
-  unit_upgrade_result = unit_upgrade_info(punit, cbuf, sizeof(cbuf));
+  unit_upgrade_result = unit_upgrade_info(&(wld.map), punit, cbuf, sizeof(cbuf));
 
   pstr = create_utf8_from_char_fonto(_("Upgrade Obsolete Units"),
                                      FONTO_ATTENTION);
diff --git a/client/gui-sdl3/menu.c b/client/gui-sdl3/menu.c
index 31ab77a6df..c8245baa1f 100644
--- a/client/gui-sdl3/menu.c
+++ b/client/gui-sdl3/menu.c
@@ -1433,7 +1433,7 @@ void real_menus_update(void)
         local_hide(ID_UNIT_ORDER_UPGRADE);
       }
 
-      if (unit_can_convert(punit)) {
+      if (unit_can_convert(&(wld.map), punit)) {
         local_show(ID_UNIT_ORDER_CONVERT);
       } else {
         local_hide(ID_UNIT_ORDER_CONVERT);
diff --git a/client/text.c b/client/text.c
index ebc29d412e..32cd0ede41 100644
--- a/client/text.c
+++ b/client/text.c
@@ -1261,7 +1261,7 @@ bool get_units_upgrade_info(char *buf, size_t bufsz,
     fc_snprintf(buf, bufsz, _("No units to upgrade!"));
     return FALSE;
   } else if (unit_list_size(punits) == 1) {
-    return (UU_OK == unit_upgrade_info(unit_list_front(punits), buf, bufsz));
+    return (UU_OK == unit_upgrade_info(&(wld.map), unit_list_front(punits), buf, bufsz));
   } else {
     int upgrade_cost = 0;
     int num_upgraded = 0;
@@ -1269,7 +1269,7 @@ bool get_units_upgrade_info(char *buf, size_t bufsz,
 
     unit_list_iterate(punits, punit) {
       if (unit_owner(punit) == client_player()
-          && UU_OK == unit_upgrade_test(punit, FALSE)) {
+          && UU_OK == unit_upgrade_test(&(wld.map), punit, FALSE)) {
         const struct unit_type *from_unittype = unit_type_get(punit);
         const struct unit_type *to_unittype = can_upgrade_unittype(client.conn.playing,
                                                                    from_unittype);
diff --git a/common/actions.c b/common/actions.c
index d2a0a15065..d9f18547d5 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -3345,6 +3345,7 @@ action_hard_reqs_actor(const struct action *paction,
                        const struct city *homecity)
 {
   enum action_result result = paction->result;
+  const struct civ_map *nmap = &(wld.map);
 
   if (actor == NULL) {
     actor = req_context_empty();
@@ -3408,7 +3409,7 @@ action_hard_reqs_actor(const struct action *paction,
   case ACTRES_CONVERT:
     /* Reason: Keep the old rules. */
     /* Info leak: The player knows their unit's cargo and location. */
-    if (!unit_can_convert(actor->unit)) {
+    if (!unit_can_convert(nmap, actor->unit)) {
       return TRI_NO;
     }
     break;
diff --git a/common/actres.c b/common/actres.c
index ad8918d25e..96fae9715d 100644
--- a/common/actres.c
+++ b/common/actres.c
@@ -838,7 +838,7 @@ enum fc_tristate actres_possible(enum action_result result,
      * of cargo, they can predict if there will be enough room in the unit
      * upgraded to, as long as they know what unit type their unit will end
      * up as. */
-    if (unit_upgrade_test(actor->unit, FALSE) != UU_OK) {
+    if (unit_upgrade_test(nmap, actor->unit, FALSE) != UU_OK) {
       return TRI_NO;
     }
 
diff --git a/common/scriptcore/api_game_methods.c b/common/scriptcore/api_game_methods.c
index e4b127e334..7c29c17682 100644
--- a/common/scriptcore/api_game_methods.c
+++ b/common/scriptcore/api_game_methods.c
@@ -760,7 +760,7 @@ bool api_methods_player_can_build_impr_direct(lua_State *L, Player *pplayer,
 bool api_methods_unit_can_upgrade(lua_State *L, Unit *punit, bool is_free)
 {
 
-  return UU_OK == unit_upgrade_test(punit, is_free);
+  return UU_OK == unit_upgrade_test(&(wld.map), punit, is_free);
 }
 
 /**********************************************************************//**
@@ -776,7 +776,7 @@ const char *api_methods_unit_transform_problem(lua_State *L, Unit *punit,
   LUASCRIPT_CHECK_SELF(L, punit, NULL);
   LUASCRIPT_CHECK_ARG_NIL(L, ptype, 3, Unit_Type, NULL);
 
-  uu = unit_transform_result(punit, ptype);
+  uu = unit_transform_result(&(wld.map), punit, ptype);
   switch (uu) {
   case UU_OK:
     return NULL;
diff --git a/common/unit.c b/common/unit.c
index a2616f3642..8275cd1733 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -1944,7 +1944,8 @@ static bool can_type_transport_units_cargo(const struct unit_type *utype,
   in result.
 **************************************************************************/
 enum unit_upgrade_result
-unit_transform_result(const struct unit *punit,
+unit_transform_result(const struct civ_map *nmap,
+                      const struct unit *punit,
                       const struct unit_type *to_unittype)
 {
   fc_assert_ret_val(NULL != to_unittype, UU_NO_UNITTYPE);
@@ -1958,7 +1959,7 @@ unit_transform_result(const struct unit *punit,
                                  utype_class(to_unittype))) {
       return UU_UNSUITABLE_TRANSPORT;
     }
-  } else if (!can_exist_at_tile(&(wld.map), to_unittype, unit_tile(punit))) {
+  } else if (!can_exist_at_tile(nmap, to_unittype, unit_tile(punit))) {
     /* The new unit type can't survive on this terrain. */
     return UU_NOT_TERRAIN;
   }
@@ -1975,7 +1976,8 @@ unit_transform_result(const struct unit *punit,
 
   Note that this function is strongly tied to unittools.c:transform_unit().
 **************************************************************************/
-enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
+enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap,
+                                           const struct unit *punit,
                                            bool is_free)
 {
   struct player *pplayer = unit_owner(punit);
@@ -2008,20 +2010,21 @@ enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
       return UU_NOT_IN_CITY;
     }
     if (city_owner(pcity) != pplayer) {
-      /* TODO: should upgrades in allied cities be possible? */
+      /* TODO: Should upgrades in allied cities be possible? */
       return UU_NOT_CITY_OWNER;
     }
   }
 
-  /* TODO: allow transported units to be reassigned.  Check here
+  /* TODO: Allow transported units to be reassigned. Check here
    * and make changes to upgrade_unit. */
-  return unit_transform_result(punit, to_unittype);
+  return unit_transform_result(nmap, punit, to_unittype);
 }
 
 /**********************************************************************//**
   Tests if unit can be converted to another type.
 **************************************************************************/
-bool unit_can_convert(const struct unit *punit)
+bool unit_can_convert(const struct civ_map *nmap,
+                      const struct unit *punit)
 {
   const struct unit_type *tgt = unit_type_get(punit)->converted_to;
 
@@ -2029,18 +2032,19 @@ bool unit_can_convert(const struct unit *punit)
     return FALSE;
   }
 
-  return UU_OK == unit_transform_result(punit, tgt);
+  return UU_OK == unit_transform_result(nmap, punit, tgt);
 }
 
 /**********************************************************************//**
   Find the result of trying to upgrade the unit, and a message that
   most callers can use directly.
 **************************************************************************/
-enum unit_upgrade_result unit_upgrade_info(const struct unit *punit,
+enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap,
+                                           const struct unit *punit,
                                            char *buf, size_t bufsz)
 {
   struct player *pplayer = unit_owner(punit);
-  enum unit_upgrade_result result = unit_upgrade_test(punit, FALSE);
+  enum unit_upgrade_result result = unit_upgrade_test(nmap, punit, FALSE);
   int upgrade_cost;
   const struct unit_type *from_unittype = unit_type_get(punit);
   const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
diff --git a/common/unit.h b/common/unit.h
index 681174f621..b4959ae137 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -496,13 +496,16 @@ struct unit *transporter_for_unit_at(const struct unit *pcargo,
                                      const struct tile *ptile);
 
 enum unit_upgrade_result
-unit_transform_result(const struct unit *punit,
+unit_transform_result(const struct civ_map *nmap,
+                      const struct unit *punit,
                       const struct unit_type *to_unittype);
-enum unit_upgrade_result unit_upgrade_test(const struct unit *punit,
+enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap,
+                                           const struct unit *punit,
                                            bool is_free);
-enum unit_upgrade_result unit_upgrade_info(const struct unit *punit,
+enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap,
+                                           const struct unit *punit,
                                            char *buf, size_t bufsz);
-bool unit_can_convert(const struct unit *punit);
+bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit);
 
 int unit_pays_mp_for_action(const struct action *paction,
                             const struct unit *punit);
diff --git a/common/unitlist.c b/common/unitlist.c
index c5c104685b..14e782c0a2 100644
--- a/common/unitlist.c
+++ b/common/unitlist.c
@@ -313,10 +313,11 @@ bool units_have_activity_on_tile(const struct unit_list *punits,
   Return TRUE iff any of the units can be upgraded to another unit type
   (for money)
 ****************************************************************************/
-bool units_can_upgrade(const struct unit_list *punits)
+bool units_can_upgrade(const struct civ_map *nmap,
+                       const struct unit_list *punits)
 {
   unit_list_iterate(punits, punit) {
-    if (UU_OK == unit_upgrade_test(punit, FALSE)) {
+    if (UU_OK == unit_upgrade_test(nmap, punit, FALSE)) {
       return TRUE;
     }
   } unit_list_iterate_end;
@@ -327,11 +328,12 @@ bool units_can_upgrade(const struct unit_list *punits)
 /************************************************************************//**
   Return TRUE iff any of the units can convert to another unit type
 ****************************************************************************/
-bool units_can_convert(const struct unit_list *punits)
+bool units_can_convert(const struct civ_map *nmap,
+                       const struct unit_list *punits)
 {
   unit_list_iterate(punits, punit) {
     if (utype_can_do_action(unit_type_get(punit), ACTION_CONVERT)
-        && unit_can_convert(punit)) {
+        && unit_can_convert(nmap, punit)) {
       return TRUE;
     }
   } unit_list_iterate_end;
diff --git a/common/unitlist.h b/common/unitlist.h
index 3be7b3680e..0f81d12d27 100644
--- a/common/unitlist.h
+++ b/common/unitlist.h
@@ -96,8 +96,10 @@ bool units_can_unload(const struct unit_list *punits);
 bool units_have_activity_on_tile(const struct unit_list *punits,
 				 enum unit_activity activity);
 
-bool units_can_upgrade(const struct unit_list *punits);
-bool units_can_convert(const struct unit_list *punits);
+bool units_can_upgrade(const struct civ_map *nmap,
+                       const struct unit_list *punits);
+bool units_can_convert(const struct civ_map *nmap,
+                       const struct unit_list *punits);
 
 #ifdef __cplusplus
 }
diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c
index e59d4647c7..337f0085b9 100644
--- a/server/scripting/api_server_edit.c
+++ b/server/scripting/api_server_edit.c
@@ -72,7 +72,7 @@ static bool
 ur_transform_unit(struct unit *punit, const struct unit_type *to_unit,
                   int vet_loss)
 {
-  if (UU_OK == unit_transform_result(punit, to_unit)) {
+  if (UU_OK == unit_transform_result(&(wld.map), punit, to_unit)) {
     /* Avoid getting overt veteranship if a user requests increasing it */
     if (vet_loss < 0) {
       int vl = utype_veteran_levels(to_unit);
diff --git a/server/unithand.c b/server/unithand.c
index 062a554459..d01328bb05 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -216,7 +216,7 @@ void handle_unit_type_upgrade(struct player *pplayer, Unit_type_id uti)
           && unit_perform_action(pplayer, punit->id, pcity->id, 0, "",
                                  paction->id, ACT_REQ_SS_AGENT)) {
         number_of_upgraded_units++;
-      } else if (UU_NO_MONEY == unit_upgrade_test(punit, FALSE)) {
+      } else if (UU_NO_MONEY == unit_upgrade_test(&(wld.map), punit, FALSE)) {
         break;
       }
     }
@@ -1531,7 +1531,7 @@ static struct ane_expl *expl_act_not_enabl(struct unit *punit,
   } else {
     switch (paction->result) {
     case ACTRES_UPGRADE_UNIT:
-      action_custom = unit_upgrade_test(punit, FALSE);
+      action_custom = unit_upgrade_test(nmap, punit, FALSE);
       break;
     case ACTRES_AIRLIFT:
       action_custom = test_unit_can_airlift_to(nmap, NULL, punit, target_city);
diff --git a/server/unittools.c b/server/unittools.c
index 6ea51e2d8a..571bb9d25b 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -434,7 +434,7 @@ static void do_upgrade_effects(struct player *pplayer)
   unit_list_iterate(pplayer->units, punit) {
     /* We have to be careful not to strand units at sea, for example by
      * upgrading a frigate to an ironclad while it was carrying a unit. */
-    if (UU_OK == unit_upgrade_test(punit, TRUE)) {
+    if (UU_OK == unit_upgrade_test(&(wld.map), punit, TRUE)) {
       unit_list_prepend(candidates, punit);	/* Potential candidate :) */
     }
   } unit_list_iterate_end;
@@ -803,7 +803,7 @@ static void unit_convert(struct unit *punit)
   from_type = unit_type_get(punit);
   to_type = from_type->converted_to;
 
-  if (unit_can_convert(punit)) {
+  if (unit_can_convert(&(wld.map), punit)) {
     transform_unit(punit, to_type, 0);
     notify_player(unit_owner(punit), unit_tile(punit),
                   E_UNIT_UPGRADED, ftc_server,
-- 
2.43.0

