From e31f3f9e764e8c1361c815a59b4a8ab64e00b100 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 26 Mar 2024 00:04:11 +0200
Subject: [PATCH 25/25] Unhardcode wld.map from
 is_action_enabled_unit_on_units()

See RM #362

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

diff --git a/ai/default/aitools.c b/ai/default/aitools.c
index e2d10dd1bf..7b5d5eee9c 100644
--- a/ai/default/aitools.c
+++ b/ai/default/aitools.c
@@ -830,27 +830,27 @@ bool dai_unit_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile
   /* FIXME: try the next action if the unit tried to do an illegal action.
    * That would allow the AI to stop using the omniscient
    * is_action_enabled_unit_on_*() functions. */
-  if (is_action_enabled_unit_on_units(ACTION_CAPTURE_UNITS,
+  if (is_action_enabled_unit_on_units(nmap, ACTION_CAPTURE_UNITS,
                                       punit, ptile)) {
     /* Choose capture. */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
                    0, "", ACTION_CAPTURE_UNITS);
-  } else if (is_action_enabled_unit_on_units(ACTION_BOMBARD,
+  } else if (is_action_enabled_unit_on_units(nmap, ACTION_BOMBARD,
                                              punit, ptile)) {
     /* Choose "Bombard". */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
                    0, "", ACTION_BOMBARD);
-  } else if (is_action_enabled_unit_on_units(ACTION_BOMBARD2,
+  } else if (is_action_enabled_unit_on_units(nmap, ACTION_BOMBARD2,
                                              punit, ptile)) {
     /* Choose "Bombard 2". */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
                    0, "", ACTION_BOMBARD2);
-  } else if (is_action_enabled_unit_on_units(ACTION_BOMBARD3,
+  } else if (is_action_enabled_unit_on_units(nmap, ACTION_BOMBARD3,
                                              punit, ptile)) {
     /* Choose "Bombard 3". */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
                    0, "", ACTION_BOMBARD3);
-  } else if (is_action_enabled_unit_on_units(ACTION_NUKE_UNITS,
+  } else if (is_action_enabled_unit_on_units(nmap, ACTION_NUKE_UNITS,
                                              punit, ptile)) {
     /* Choose "Nuke Units". */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
@@ -881,12 +881,12 @@ bool dai_unit_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile
     /* Choose "Nuke City". */
     unit_do_action(unit_owner(punit), punit->id, tcity->id,
                    0, "", ACTION_NUKE_CITY);
-  } else if (is_action_enabled_unit_on_units(ACTION_ATTACK,
+  } else if (is_action_enabled_unit_on_units(nmap, ACTION_ATTACK,
                                              punit, ptile)) {
     /* Choose regular attack. */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
                    0, "", ACTION_ATTACK);
-  } else if (is_action_enabled_unit_on_units(ACTION_SUICIDE_ATTACK,
+  } else if (is_action_enabled_unit_on_units(nmap, ACTION_SUICIDE_ATTACK,
                                              punit, ptile)) {
     /* Choose suicide attack (explode missile). */
     unit_do_action(unit_owner(punit), punit->id, tile_index(ptile),
diff --git a/common/actions.c b/common/actions.c
index a597734fae..302a0d0cb4 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -3262,7 +3262,7 @@ struct action *action_is_blocked_by(const struct action *act,
         /* Can't be enabled. No target. */
         continue;
       }
-      if (is_action_enabled_unit_on_units(blocker->id,
+      if (is_action_enabled_unit_on_units(nmap, blocker->id,
                                           actor_unit, target_tile)) {
         return blocker;
       }
@@ -4917,12 +4917,11 @@ is_action_enabled_unit_on_units_full(const struct civ_map *nmap,
 
   See note in is_action_enabled() for why the action may still be disabled.
 **************************************************************************/
-bool is_action_enabled_unit_on_units(const action_id wanted_action,
+bool is_action_enabled_unit_on_units(const struct civ_map *nmap,
+                                     const action_id wanted_action,
                                      const struct unit *actor_unit,
                                      const struct tile *target_tile)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   return is_action_enabled_unit_on_units_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 375ab1d11d..7b3dda4c35 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -792,7 +792,8 @@ bool is_action_enabled_unit_on_unit(const struct civ_map *nmap,
                                     const struct unit *actor_unit,
                                     const struct unit *target_unit);
 
-bool is_action_enabled_unit_on_units(const action_id wanted_action,
+bool is_action_enabled_unit_on_units(const struct civ_map *nmap,
+                                     const action_id wanted_action,
                                      const struct unit *actor_unit,
                                      const struct tile *target_tile);
 
diff --git a/common/unit.c b/common/unit.c
index 299658bbd4..d26580609a 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -839,7 +839,7 @@ bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
 /**********************************************************************//**
   Check if the unit's current activity is actually legal.
 **************************************************************************/
-bool can_unit_continue_current_activity(struct civ_map *nmap,
+bool can_unit_continue_current_activity(const struct civ_map *nmap,
                                         struct unit *punit)
 {
   enum unit_activity current = punit->activity;
diff --git a/common/unit.h b/common/unit.h
index 18c4aa503a..398cfa4958 100644
--- a/common/unit.h
+++ b/common/unit.h
@@ -331,7 +331,7 @@ bool can_unit_change_homecity_to(const struct unit *punit,
                                  const struct city *pcity);
 bool can_unit_change_homecity(const struct unit *punit);
 const char *get_activity_text(enum unit_activity activity);
-bool can_unit_continue_current_activity(struct civ_map *nmap,
+bool can_unit_continue_current_activity(const struct civ_map *nmap,
                                         struct unit *punit);
 bool can_unit_do_activity(const struct civ_map *nmap,
                           const struct unit *punit,
diff --git a/server/actiontools.c b/server/actiontools.c
index cd02eab9bd..f5003bf572 100644
--- a/server/actiontools.c
+++ b/server/actiontools.c
@@ -1031,7 +1031,7 @@ action_auto_perf_unit_do(const enum action_auto_perf_cause cause,
       switch (action_id_get_target_kind(act)) {
       case ATK_UNITS:
         if (tgt_tile
-            && is_action_enabled_unit_on_units(act, actor, tgt_tile)) {
+            && is_action_enabled_unit_on_units(nmap, act, actor, tgt_tile)) {
           perform_action_to(act, actor, tgt_tile->index, EXTRA_NONE);
         }
         break;
@@ -1128,7 +1128,7 @@ action_auto_perf_unit_prob(const enum action_auto_perf_cause cause,
       switch (action_id_get_target_kind(act)) {
       case ATK_UNITS:
         if (tgt_tile
-            && is_action_enabled_unit_on_units(act, actor, tgt_tile)) {
+            && is_action_enabled_unit_on_units(nmap, act, actor, tgt_tile)) {
           current = action_prob_vs_units(actor, act, tgt_tile);
         }
         break;
diff --git a/server/scripting/api_server_edit.c b/server/scripting/api_server_edit.c
index c97743a69b..eaf0d7fdbb 100644
--- a/server/scripting/api_server_edit.c
+++ b/server/scripting/api_server_edit.c
@@ -441,6 +441,7 @@ bool api_edit_perform_action_unit_vs_tile(lua_State *L, Unit *punit,
                                           Action *paction, Tile *tgt)
 {
   bool enabled = FALSE;
+  const struct civ_map *nmap = &(wld.map);
 
   LUASCRIPT_CHECK_STATE(L, FALSE);
   LUASCRIPT_CHECK_ARG_NIL(L, punit, 2, Unit, FALSE);
@@ -450,7 +451,7 @@ bool api_edit_perform_action_unit_vs_tile(lua_State *L, Unit *punit,
   fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE);
   switch (action_get_target_kind(paction)) {
   case ATK_UNITS:
-    enabled = is_action_enabled_unit_on_units(paction->id, punit, tgt);
+    enabled = is_action_enabled_unit_on_units(nmap, paction->id, punit, tgt);
     break;
   case ATK_TILE:
     enabled = is_action_enabled_unit_on_tile(paction->id, punit,
@@ -498,6 +499,7 @@ bool api_edit_perform_action_unit_vs_tile_extra(lua_State *L, Unit *punit,
 {
   struct extra_type *sub_target;
   bool enabled = FALSE;
+  const struct civ_map *nmap = &(wld.map);
 
   LUASCRIPT_CHECK_STATE(L, FALSE);
   LUASCRIPT_CHECK_ARG_NIL(L, punit, 2, Unit, FALSE);
@@ -511,7 +513,7 @@ bool api_edit_perform_action_unit_vs_tile_extra(lua_State *L, Unit *punit,
   fc_assert_ret_val(action_get_actor_kind(paction) == AAK_UNIT, FALSE);
   switch (action_get_target_kind(paction)) {
   case ATK_UNITS:
-    enabled = is_action_enabled_unit_on_units(paction->id, punit, tgt);
+    enabled = is_action_enabled_unit_on_units(nmap, paction->id, punit, tgt);
     break;
   case ATK_TILE:
     enabled = is_action_enabled_unit_on_tile(paction->id, punit,
diff --git a/server/unithand.c b/server/unithand.c
index d0bb39eb8a..4574fbca91 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -2358,6 +2358,7 @@ void illegal_action_msg(struct player *pplayer,
                         const struct unit *target_unit)
 {
   struct ane_expl *explnat;
+  const struct civ_map *nmap = &(wld.map);
 
   /* Explain why the action was illegal. */
   explnat = expl_act_not_enabl(actor, stopped_action,
@@ -2411,7 +2412,7 @@ void illegal_action_msg(struct player *pplayer,
       if (!utype_can_do_act_when_ustate(unit_type_get(actor),
                                         stopped_action, USP_LIVABLE_TILE,
                                         FALSE)
-          && !can_unit_exist_at_tile(&(wld.map), actor, unit_tile(actor))) {
+          && !can_unit_exist_at_tile(nmap, actor, unit_tile(actor))) {
         unit_type_iterate(utype) {
           if (utype_can_do_act_when_ustate(utype, stopped_action,
                                            USP_LIVABLE_TILE, FALSE)) {
@@ -3393,7 +3394,7 @@ bool unit_perform_action(struct player *pplayer,
 
 #define ACTION_PERFORM_UNIT_UNITS(action, actor, target, action_performer)\
   if (target_tile                                                         \
-      && is_action_enabled_unit_on_units(action_type,                     \
+      && is_action_enabled_unit_on_units(nmap, action_type,               \
                                          actor_unit, target_tile)) {      \
     bool success;                                                         \
     script_server_signal_emit("action_started_unit_units",                \
@@ -3856,6 +3857,7 @@ void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity,
   struct player *old_owner = unit_owner(punit);
   struct player *new_owner = (new_pcity == NULL ? old_owner
                                                 : city_owner(new_pcity));
+  const struct civ_map *nmap = &(wld.map);
 
   /* Calling this function when new_pcity is same as old_pcity should
    * be safe with current implementation, but it is not meant to
@@ -3920,7 +3922,7 @@ void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity,
     }
   }
 
-  if (!can_unit_continue_current_activity(&(wld.map), punit)) {
+  if (!can_unit_continue_current_activity(nmap, punit)) {
     /* This is mainly for cases where unit owner changes to one not knowing
      * Railroad tech when unit is already building railroad.
      * Does also send_unit_info() */
@@ -4331,6 +4333,7 @@ static bool unit_bombard(struct unit *punit, struct tile *ptile,
   struct player *pplayer = unit_owner(punit);
   struct city *pcity = tile_city(ptile);
   const struct unit_type *act_utype;
+  const struct civ_map *nmap = &(wld.map);
 
   /* Sanity check: The actor still exists. */
   fc_assert_ret_val(pplayer, FALSE);
@@ -4348,7 +4351,7 @@ static bool unit_bombard(struct unit *punit, struct tile *ptile,
       enum direction8 facing;
       int att_hp, def_hp;
 
-      adj = base_get_direction_for_step(&(wld.map),
+      adj = base_get_direction_for_step(nmap,
                                         punit->tile, pdefender->tile, &facing);
 
       if (adj) {
-- 
2.43.0

