From 226f5aaf3427e04168d8ea88a3c1c99821b85544 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 12 Jan 2024 07:20:59 +0200
Subject: [PATCH 43/43] Unhardcode wld.map from action_hard_reqs_actor()

See RM #173

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/actions.c  | 22 +++++++++++++---------
 common/actions.h  |  4 +++-
 common/unit.c     | 10 +++++++---
 server/unithand.c |  5 +++--
 4 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/common/actions.c b/common/actions.c
index 8a691b15b2..8a9f8243a4 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -2547,13 +2547,13 @@ action_actor_utype_hard_reqs_ok(const struct action *paction,
   may or may not be legal depending on the action.
 **************************************************************************/
 static enum fc_tristate
-action_hard_reqs_actor(const struct action *paction,
+action_hard_reqs_actor(const struct civ_map *nmap,
+                       const struct action *paction,
                        const struct req_context *actor,
                        const bool omniscient,
                        const struct city *homecity)
 {
   enum action_result result = paction->result;
-  const struct civ_map *nmap = &(wld.map);
 
   if (actor == NULL) {
     actor = req_context_empty();
@@ -2814,7 +2814,7 @@ is_action_possible(const action_id wanted_action,
   }
 
   /* Actor specific hard requirements. */
-  out = action_hard_reqs_actor(paction, actor, omniscient, homecity);
+  out = action_hard_reqs_actor(nmap, paction, actor, omniscient, homecity);
 
   if (out == TRI_NO) {
     /* Illegal because of a hard actor requirement. */
@@ -3623,10 +3623,11 @@ static struct act_prob ap_diplomat_battle(const struct unit *pattacker,
 /**********************************************************************//**
   Returns the action probability for when a target is unseen.
 **************************************************************************/
-static struct act_prob act_prob_unseen_target(action_id act_id,
+static struct act_prob act_prob_unseen_target(const struct civ_map *nmap,
+                                              action_id act_id,
                                               const struct unit *actor_unit)
 {
-  if (action_maybe_possible_actor_unit(act_id, actor_unit)) {
+  if (action_maybe_possible_actor_unit(nmap, act_id, actor_unit)) {
     /* Unknown because the target is unseen. */
     return ACTPROB_NOT_KNOWN;
   } else {
@@ -4066,6 +4067,7 @@ action_prob_vs_city_full(const struct unit* actor_unit,
   const struct impr_type *target_building;
   const struct unit_type *target_utype;
   const struct action *act = action_by_number(act_id);
+  const struct civ_map *nmap = &(wld.map);
 
   if (actor_unit == NULL || target_city == NULL) {
     /* Can't do an action when actor or target are missing. */
@@ -4115,7 +4117,7 @@ action_prob_vs_city_full(const struct unit* actor_unit,
   if (!player_can_see_city_externals(unit_owner(actor_unit), target_city)) {
     /* The invisible city at this tile may, as far as the player knows, not
      * exist anymore. */
-    return act_prob_unseen_target(act_id, actor_unit);
+    return act_prob_unseen_target(nmap, act_id, actor_unit);
   }
 
   target_building = tgt_city_local_building(target_city);
@@ -4249,6 +4251,7 @@ action_prob_vs_units_full(const struct unit* actor_unit,
   struct act_prob prob_all;
   const struct req_context *actor_ctxt;
   const struct action *act = action_by_number(act_id);
+  const struct civ_map *nmap = &(wld.map);
 
   if (actor_unit == NULL || target_tile == NULL) {
     /* Can't do an action when actor or target are missing. */
@@ -4321,7 +4324,7 @@ action_prob_vs_units_full(const struct unit* actor_unit,
       return ACTPROB_IMPOSSIBLE;
     } else {
       /* The player doesn't know that the tile is empty. */
-      return act_prob_unseen_target(act_id, actor_unit);
+      return act_prob_unseen_target(nmap, act_id, actor_unit);
     }
   }
 
@@ -5378,7 +5381,8 @@ bool is_action_possible_on_city(action_id act_id,
   performed right now by the specified actor unit if an approriate target
   is provided.
 **************************************************************************/
-bool action_maybe_possible_actor_unit(const action_id act_id,
+bool action_maybe_possible_actor_unit(const struct civ_map *nmap,
+                                      const action_id act_id,
                                       const struct unit *actor_unit)
 {
   const struct player *actor_player = unit_owner(actor_unit);
@@ -5400,7 +5404,7 @@ bool action_maybe_possible_actor_unit(const action_id act_id,
     return FALSE;
   }
 
-  result = action_hard_reqs_actor(paction, &actor_ctxt, FALSE,
+  result = action_hard_reqs_actor(nmap, paction, &actor_ctxt, FALSE,
                                   unit_home(actor_unit));
 
   if (result == TRI_NO) {
diff --git a/common/actions.h b/common/actions.h
index 09de098a7e..a10c53098a 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -17,6 +17,7 @@
 /* common */
 #include "actres.h"
 #include "fc_types.h"
+#include "map_types.h"
 #include "requirements.h"
 
 #ifdef __cplusplus
@@ -687,7 +688,8 @@ bool is_action_possible_on_city(action_id act_id,
                                 const struct player *actor_player,
                                 const struct city *target_city);
 
-bool action_maybe_possible_actor_unit(const action_id wanted_action,
+bool action_maybe_possible_actor_unit(const struct civ_map *nmap,
+                                      const action_id wanted_action,
                                       const struct unit *actor_unit);
 
 bool action_mp_full_makes_legal(const struct unit *actor,
diff --git a/common/unit.c b/common/unit.c
index edc8dabf8f..0666aacf94 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -822,8 +822,10 @@ bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo,
 **************************************************************************/
 bool can_unit_teleport(const struct unit *punit)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   action_by_result_iterate(paction, ACTRES_TELEPORT) {
-    if (action_maybe_possible_actor_unit(action_id(paction), punit)) {
+    if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
       return TRUE;
     }
   } action_by_result_iterate_end;
@@ -838,13 +840,15 @@ bool can_unit_teleport(const struct unit *punit)
 **************************************************************************/
 bool can_unit_paradrop(const struct unit *punit)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   action_by_result_iterate(paction, ACTRES_PARADROP) {
-    if (action_maybe_possible_actor_unit(action_id(paction), punit)) {
+    if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
       return TRUE;
     }
   } action_by_result_iterate_end;
   action_by_result_iterate(paction, ACTRES_PARADROP_CONQUER) {
-    if (action_maybe_possible_actor_unit(action_id(paction), punit)) {
+    if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
       return TRUE;
     }
   } action_by_result_iterate_end;
diff --git a/server/unithand.c b/server/unithand.c
index 959f99804f..efb5ccacc3 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -2256,6 +2256,7 @@ void handle_unit_get_actions(struct connection *pc,
   int actor_target_distance;
   const struct player_tile *plrtile;
   int target_extra_id = target_extra_id_client;
+  const struct civ_map *nmap = &(wld.map);
 
   /* No potentially legal action is known yet. If none is found the player
    * should get an explanation. */
@@ -2267,7 +2268,7 @@ void handle_unit_get_actions(struct connection *pc,
 
   actor_player = pc->playing;
   actor_unit = game_unit_by_number(actor_unit_id);
-  target_tile = index_to_tile(&(wld.map), target_tile_id);
+  target_tile = index_to_tile(nmap, target_tile_id);
 
   /* Initialize the action probabilities. */
   action_iterate(act) {
@@ -2346,7 +2347,7 @@ void handle_unit_get_actions(struct connection *pc,
           probabilities[act] = action_prob_vs_city(actor_unit, act,
                                                    target_city);
         } else if (!tile_is_seen(target_tile, actor_player)
-                   && action_maybe_possible_actor_unit(act, actor_unit)
+                   && action_maybe_possible_actor_unit(nmap, act, actor_unit)
                    && action_id_distance_accepted(act,
                                                   actor_target_distance)) {
           /* The target city is non existing. The player isn't aware of this
-- 
2.43.0

