From 0f7154b0cf37f0109018e1f676172d7060e3de1a Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Thu, 16 May 2024 02:32:34 +0300
Subject: [PATCH 77/77] Unhardcode wld.map from action_prob_vs_tile()

See RM #622

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/control.c     |  3 ++-
 common/actions.c     |  7 +++----
 common/actions.h     |  3 ++-
 common/unit.c        |  2 +-
 server/actiontools.c | 11 +++++++----
 server/cityhand.c    |  3 ++-
 server/unithand.c    |  2 +-
 server/unittools.c   |  2 +-
 8 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/client/control.c b/client/control.c
index f40f48b405..084e1600f0 100644
--- a/client/control.c
+++ b/client/control.c
@@ -2020,7 +2020,8 @@ static void do_disband_alternative(void *p)
     break;
   case ATK_TILE:
     if ((ptile = unit_tile(punit))
-        && action_prob_possible(action_prob_vs_tile(punit, act, ptile, NULL))) {
+        && action_prob_possible(action_prob_vs_tile(nmap, punit, act,
+                                                    ptile, NULL))) {
       request_do_action(act, punit->id, ptile->index, 0, "");
     }
     break;
diff --git a/common/actions.c b/common/actions.c
index e09ac5ee05..7e71a3d9ba 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -4558,13 +4558,12 @@ action_prob_vs_tile_full(const struct civ_map *nmap,
   Get the actor unit's probability of successfully performing the chosen
   action on the target tile.
 **************************************************************************/
-struct act_prob action_prob_vs_tile(const struct unit *actor_unit,
+struct act_prob action_prob_vs_tile(const struct civ_map *nmap,
+                                    const struct unit *actor_unit,
                                     const action_id act_id,
                                     const struct tile *target_tile,
                                     const struct extra_type *target_extra)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   return action_prob_vs_tile_full(nmap, actor_unit,
                                   unit_home(actor_unit),
                                   unit_tile(actor_unit),
@@ -4757,7 +4756,7 @@ struct act_prob action_prob_unit_vs_tgt(const struct action *paction,
     break;
   case ATK_TILE:
     if (tgt_tile) {
-      prob = action_prob_vs_tile(act_unit, paction->id, tgt_tile, extra_tgt);
+      prob = action_prob_vs_tile(nmap, act_unit, paction->id, tgt_tile, extra_tgt);
     }
     break;
   case ATK_EXTRAS:
diff --git a/common/actions.h b/common/actions.h
index acef71a7e4..576a26b8a4 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -566,7 +566,8 @@ struct act_prob action_prob_vs_stack(const struct civ_map *nmap,
                                      const action_id act_id,
                                      const struct tile* victims);
 
-struct act_prob action_prob_vs_tile(const struct unit *actor,
+struct act_prob action_prob_vs_tile(const struct civ_map *nmap,
+                                    const struct unit *actor,
                                     const action_id act_id,
                                     const struct tile *victims,
                                     const struct extra_type *target_extra);
diff --git a/common/unit.c b/common/unit.c
index 3d0703cee7..0d9b438cdc 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -478,7 +478,7 @@ bool unit_can_add_or_build_city(const struct unit *punit)
     return action_prob_possible(action_prob_vs_city(nmap, punit,
         ACTION_JOIN_CITY, tgt_city));
   } else {
-    return action_prob_possible(action_prob_vs_tile(punit,
+    return action_prob_possible(action_prob_vs_tile(nmap, punit,
         ACTION_FOUND_CITY, unit_tile(punit), NULL));
   }
 }
diff --git a/server/actiontools.c b/server/actiontools.c
index 98b1b2778f..d158a09112 100644
--- a/server/actiontools.c
+++ b/server/actiontools.c
@@ -757,7 +757,7 @@ struct tile *action_tgt_tile(struct unit *actor,
 
     switch (action_id_get_target_kind(act)) {
     case ATK_TILE:
-      prob = action_prob_vs_tile(actor, act, target, target_extra);
+      prob = action_prob_vs_tile(nmap, actor, act, target, target_extra);
       break;
     case ATK_EXTRAS:
       prob = action_prob_vs_extras(actor, act, target, target_extra);
@@ -803,6 +803,8 @@ static bool may_unit_act_vs_tile_extra(const struct unit *actor,
                                        const struct extra_type *tgt_extra,
                                        bool accept_all_actions)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (actor == NULL || tgt_tile == NULL || tgt_extra == NULL) {
     /* Can't do any actions if actor or target are missing. */
     return FALSE;
@@ -824,7 +826,7 @@ static bool may_unit_act_vs_tile_extra(const struct unit *actor,
 
     switch (action_id_get_target_kind(act)) {
     case ATK_TILE:
-      if (action_prob_possible(action_prob_vs_tile(actor, act,
+      if (action_prob_possible(action_prob_vs_tile(nmap, actor, act,
                                                    tgt_tile, tgt_extra))) {
         /* The actor unit may be able to do this action to the target
          * extra. */
@@ -888,6 +890,7 @@ int action_sub_target_id_for_action(const struct action *paction,
                                     struct unit *actor_unit)
 {
   const struct tile *tgt_tile = unit_tile(actor_unit);
+  const struct civ_map *nmap = &(wld.map);
 
   fc_assert_ret_val(paction->target_complexity == ACT_TGT_COMPL_FLEXIBLE,
                     NO_TARGET);
@@ -922,7 +925,7 @@ int action_sub_target_id_for_action(const struct action *paction,
       }
     }
     extra_type_re_active_iterate(tgt_extra) {
-      if (action_prob_possible(action_prob_vs_tile(actor_unit, paction->id,
+      if (action_prob_possible(action_prob_vs_tile(nmap, actor_unit, paction->id,
                                                    tgt_tile, tgt_extra))) {
         /* The actor unit may be able to do this action to the target
          * extra. */
@@ -1147,7 +1150,7 @@ action_auto_perf_unit_prob(const enum action_auto_perf_cause cause,
         if (tgt_tile
             && is_action_enabled_unit_on_tile(nmap, act, actor,
                                               tgt_tile, target_extra)) {
-          current = action_prob_vs_tile(actor, act, tgt_tile, target_extra);
+          current = action_prob_vs_tile(nmap, actor, act, tgt_tile, target_extra);
         }
         break;
       case ATK_EXTRAS:
diff --git a/server/cityhand.c b/server/cityhand.c
index 96ac464149..c828ca6045 100644
--- a/server/cityhand.c
+++ b/server/cityhand.c
@@ -59,6 +59,7 @@
 void handle_city_name_suggestion_req(struct player *pplayer, int unit_id)
 {
   struct unit *punit = player_unit_by_number(pplayer, unit_id);
+  const struct civ_map *nmap = &(wld.map);
 
   if (NULL == punit) {
     /* Probably died or bribed. */
@@ -67,7 +68,7 @@ void handle_city_name_suggestion_req(struct player *pplayer, int unit_id)
     return;
   }
 
-  if (action_prob_possible(action_prob_vs_tile(punit, ACTION_FOUND_CITY,
+  if (action_prob_possible(action_prob_vs_tile(nmap, punit, ACTION_FOUND_CITY,
                                                unit_tile(punit), NULL))) {
     log_verbose("handle_city_name_suggest_req(unit_pos (%d, %d))",
                 TILE_XY(unit_tile(punit)));
diff --git a/server/unithand.c b/server/unithand.c
index 2ce63bac51..11c60eaf12 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -2393,7 +2393,7 @@ void handle_unit_get_actions(struct connection *pc,
     case ATK_TILE:
       if (target_tile) {
         /* Calculate the probabilities. */
-        probabilities[act] = action_prob_vs_tile(actor_unit, act,
+        probabilities[act] = action_prob_vs_tile(nmap, actor_unit, act,
                                                  target_tile, target_extra);
       } else {
         /* No target to act against. */
diff --git a/server/unittools.c b/server/unittools.c
index 332e06a116..01e487e1fc 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -4810,7 +4810,7 @@ bool execute_orders(struct unit *punit, const bool fresh)
         tgt_id = dst_tile->index;
         break;
       case ATK_TILE:
-        prob = action_prob_vs_tile(punit, order.action,
+        prob = action_prob_vs_tile(nmap, punit, order.action,
                                    dst_tile, pextra);
         tgt_id = dst_tile->index;
         break;
-- 
2.43.0

