From ad649c9d63a30e80bb3ef02a283ec561cd8fb8bf Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Wed, 17 Apr 2024 06:49:51 +0300
Subject: [PATCH 14/14] Unhardcode wld.map from
 is_action_enabled_unit_on_self()

See RM #461

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/daicity.c               | 2 +-
 common/actions.c                   | 7 +++----
 common/actions.h                   | 3 ++-
 common/unit.c                      | 4 ++--
 server/actiontools.c               | 4 ++--
 server/scripting/api_server_edit.c | 4 +++-
 server/unithand.c                  | 2 +-
 server/unittools.c                 | 7 ++++---
 8 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/ai/default/daicity.c b/ai/default/daicity.c
index 37f2696713..ada6243dd9 100644
--- a/ai/default/daicity.c
+++ b/ai/default/daicity.c
@@ -507,7 +507,7 @@ static void unit_do_disband_trad(struct player *owner, struct unit *punit,
 
   /* Try to disband even if all shields will be wasted. */
   if (unit_can_do_action(punit, ACTION_DISBAND_UNIT)) {
-    if (is_action_enabled_unit_on_self(ACTION_DISBAND_UNIT, punit)) {
+    if (is_action_enabled_unit_on_self(nmap, ACTION_DISBAND_UNIT, punit)) {
       if (unit_perform_action(owner, punit->id, punit->id,
                               0, NULL, ACTION_DISBAND_UNIT, requester)) {
         /* All shields wasted. The unit did Disband Unit. */
diff --git a/common/actions.c b/common/actions.c
index ec88ebc236..0da07be17b 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -3288,7 +3288,7 @@ struct action *action_is_blocked_by(const struct action *act,
       }
       break;
     case ATK_SELF:
-      if (is_action_enabled_unit_on_self(blocker->id, actor_unit)) {
+      if (is_action_enabled_unit_on_self(nmap, blocker->id, actor_unit)) {
         return blocker;
       }
       break;
@@ -5142,11 +5142,10 @@ is_action_enabled_unit_on_self_full(const struct civ_map *nmap,
   See note in is_action_enabled() for why the action still may be
   disabled.
 **************************************************************************/
-bool is_action_enabled_unit_on_self(const action_id wanted_action,
+bool is_action_enabled_unit_on_self(const struct civ_map *nmap,
+                                    const action_id wanted_action,
                                     const struct unit *actor_unit)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   return is_action_enabled_unit_on_self_full(nmap, wanted_action, actor_unit,
                                              unit_home(actor_unit),
                                              unit_tile(actor_unit));
diff --git a/common/actions.h b/common/actions.h
index 41abaf75fa..f0072d28ae 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -809,7 +809,8 @@ bool is_action_enabled_unit_on_extras(const struct civ_map *nmap,
                                       const struct tile *target,
                                       const struct extra_type *tgt_extra);
 
-bool is_action_enabled_unit_on_self(const action_id wanted_action,
+bool is_action_enabled_unit_on_self(const struct civ_map *nmap,
+                                    const action_id wanted_action,
                                     const struct unit *actor_unit);
 
 struct act_prob action_prob_vs_city(const struct unit* actor,
diff --git a/common/unit.c b/common/unit.c
index ff5ce73860..f9e030b5e4 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -1014,7 +1014,7 @@ bool can_unit_do_activity_targeted_at(const struct civ_map *nmap,
     /* The call below doesn't support actor tile speculation. */
     fc_assert_msg(unit_tile(punit) == ptile,
                   "Please use action_speculate_unit_on_self()");
-    return is_action_enabled_unit_on_self(ACTION_FORTIFY,
+    return is_action_enabled_unit_on_self(nmap, ACTION_FORTIFY,
                                           punit);
 
   case ACTIVITY_FORTIFIED:
@@ -1062,7 +1062,7 @@ bool can_unit_do_activity_targeted_at(const struct civ_map *nmap,
     /* The call below doesn't support actor tile speculation. */
     fc_assert_msg(unit_tile(punit) == ptile,
                   "Please use action_speculate_unit_on_self()");
-    return is_action_enabled_unit_on_self(ACTION_CONVERT, punit);
+    return is_action_enabled_unit_on_self(nmap, ACTION_CONVERT, punit);
 
   case ACTIVITY_OLD_ROAD:
   case ACTIVITY_OLD_RAILROAD:
diff --git a/server/actiontools.c b/server/actiontools.c
index 01e06b1c75..ea4d835750 100644
--- a/server/actiontools.c
+++ b/server/actiontools.c
@@ -1065,7 +1065,7 @@ action_auto_perf_unit_do(const enum action_auto_perf_cause cause,
         break;
       case ATK_SELF:
         if (actor
-            && is_action_enabled_unit_on_self(act, actor)) {
+            && is_action_enabled_unit_on_self(nmap, act, actor)) {
           perform_action_to(act, actor, actor->id, EXTRA_NONE);
         }
         break;
@@ -1161,7 +1161,7 @@ action_auto_perf_unit_prob(const enum action_auto_perf_cause cause,
         break;
       case ATK_SELF:
         if (actor
-            && is_action_enabled_unit_on_self(act, actor)) {
+            && is_action_enabled_unit_on_self(nmap, act, actor)) {
           current = action_prob_self(actor, act);
         }
         break;
diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c
index d5839f82d1..b7dc68d6aa 100644
--- a/server/scripting/api_server_edit.c
+++ b/server/scripting/api_server_edit.c
@@ -558,6 +558,8 @@ bool api_edit_perform_action_unit_vs_tile_extra(lua_State *L, Unit *punit,
 bool api_edit_perform_action_unit_vs_self(lua_State *L, Unit *punit,
                                           Action *paction)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   LUASCRIPT_CHECK_STATE(L, FALSE);
   LUASCRIPT_CHECK_ARG_NIL(L, punit, 2, Unit, FALSE);
   LUASCRIPT_CHECK_ARG_NIL(L, paction, 3, Action, FALSE);
@@ -565,7 +567,7 @@ bool api_edit_perform_action_unit_vs_self(lua_State *L, Unit *punit,
   fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE);
   fc_assert_ret_val(action_get_target_kind(paction) == ATK_SELF, FALSE);
   fc_assert_ret_val(!action_has_result(paction, ACTRES_FOUND_CITY), FALSE);
-  if (is_action_enabled_unit_on_self(paction->id, punit)) {
+  if (is_action_enabled_unit_on_self(nmap, paction->id, punit)) {
     return unit_perform_action(unit_owner(punit), punit->id,
                                IDENTITY_NUMBER_ZERO, IDENTITY_NUMBER_ZERO,
                                "",
diff --git a/server/unithand.c b/server/unithand.c
index 5600eec27e..6f547f412f 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -3340,7 +3340,7 @@ bool unit_perform_action(struct player *pplayer,
 
 #define ACTION_PERFORM_UNIT_SELF(action, actor, action_performer)         \
   if (actor_unit                                                          \
-      && is_action_enabled_unit_on_self(action_type, actor_unit)) {       \
+      && is_action_enabled_unit_on_self(nmap, action_type, actor_unit)) { \
     bool success;                                                         \
     script_server_signal_emit("action_started_unit_self",                 \
                               action_by_number(action), actor);           \
diff --git a/server/unittools.c b/server/unittools.c
index 5e3a0e168a..da8edd6634 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -2383,6 +2383,7 @@ void kill_unit(struct unit *pkiller, struct unit *punit, bool vet)
   int ransom, unitcount = 0;
   bool escaped;
   bool collect_ransom = FALSE;
+  const struct civ_map *nmap = &(wld.map);
 
   sz_strlcpy(pkiller_link, unit_link(pkiller));
   sz_strlcpy(punit_link, unit_tile_link(punit));
@@ -2477,10 +2478,10 @@ void kill_unit(struct unit *pkiller, struct unit *punit, bool vet)
 
           fc_assert(vunit->hp > 0);
 
-          adjc_iterate(&(wld.map), deftile, ptile2) {
-            if (can_exist_at_tile(&(wld.map), vunit->utype, ptile2)
+          adjc_iterate(nmap, deftile, ptile2) {
+            if (can_exist_at_tile(nmap, vunit->utype, ptile2)
                 && NULL == tile_city(ptile2)) {
-              move_cost = map_move_cost_unit(&(wld.map), vunit, ptile2);
+              move_cost = map_move_cost_unit(nmap, vunit, ptile2);
               if (pkiller->moves_left <= vunit->moves_left - move_cost
                   && (is_allied_unit_tile(ptile2, pvictim)
                       || unit_list_size(ptile2->units)) == 0) {
-- 
2.43.0

