From 44797503143f7a1c0461e718f77100f9553da1fe Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 28 Apr 2024 18:40:38 +0300
Subject: [PATCH 47/47] Unhardcode wld.map from action_prob_vs_city()

See RM #513

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/aidiplomat.c |  5 +++--
 client/control.c        |  3 ++-
 common/actions.c        |  8 ++++----
 common/actions.h        |  5 +++--
 common/unit.c           | 14 +++++++++-----
 server/actiontools.c    |  6 ++++--
 server/unithand.c       |  2 +-
 server/unittools.c      |  2 +-
 8 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/ai/default/aidiplomat.c b/ai/default/aidiplomat.c
index 9604952bbd..a9abb25d80 100644
--- a/ai/default/aidiplomat.c
+++ b/ai/default/aidiplomat.c
@@ -333,6 +333,7 @@ static void dai_diplomat_city(struct ai_type *ait, struct unit *punit,
   struct player *pplayer = unit_owner(punit);
   struct player *tplayer = city_owner(ctarget);
   int count_tech = count_stealable_techs(pplayer, tplayer);
+  const struct civ_map *nmap = &(wld.map);
 
   fc_assert_ret(is_ai(pplayer));
 
@@ -359,7 +360,7 @@ static void dai_diplomat_city(struct ai_type *ait, struct unit *punit,
     }
 
     if (!action_prob_possible(
-            action_prob_vs_city(punit, action_number(paction),ctarget))) {
+            action_prob_vs_city(nmap, punit, action_number(paction),ctarget))) {
       /* Not possible. */
       continue;
     }
@@ -388,7 +389,7 @@ static void dai_diplomat_city(struct ai_type *ait, struct unit *punit,
     /* An action has been selected. */
 
     if (action_prob_possible(
-          action_prob_vs_city(punit, action_number(chosen_action),
+          action_prob_vs_city(nmap, punit, action_number(chosen_action),
                               ctarget))) {
       log_base(LOG_DIPLOMAT, "%s %s[%d] does %s at %s",
                nation_rule_name(nation_of_unit(punit)),
diff --git a/client/control.c b/client/control.c
index f163b11d34..2e2b61e75d 100644
--- a/client/control.c
+++ b/client/control.c
@@ -1983,7 +1983,8 @@ static void do_disband_alternative(void *p)
   switch (action_id_get_target_kind(act)) {
   case ATK_CITY:
     if ((pcity = tile_city(unit_tile(punit)))
-        && action_prob_possible(action_prob_vs_city(punit, act, pcity))) {
+        && action_prob_possible(action_prob_vs_city(&(wld.map), punit,
+                                                    act, pcity))) {
       request_do_action(act, punit->id, pcity->id, 0, "");
     }
     break;
diff --git a/common/actions.c b/common/actions.c
index e8e5c4beab..c24beeda18 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -4188,12 +4188,11 @@ action_prob_vs_city_full(const struct civ_map *nmap,
   Get the actor unit's probability of successfully performing the chosen
   action on the target city.
 **************************************************************************/
-struct act_prob action_prob_vs_city(const struct unit *actor_unit,
+struct act_prob action_prob_vs_city(const struct civ_map *nmap,
+                                    const struct unit *actor_unit,
                                     const action_id act_id,
                                     const struct city *target_city)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   return action_prob_vs_city_full(nmap, actor_unit,
                                   unit_home(actor_unit),
                                   unit_tile(actor_unit),
@@ -4735,6 +4734,7 @@ struct act_prob action_prob_unit_vs_tgt(const struct action *paction,
 {
   /* Assume impossible until told otherwise. */
   struct act_prob prob = ACTPROB_IMPOSSIBLE;
+  const struct civ_map *nmap = &(wld.map);
 
   fc_assert_ret_val(paction, ACTPROB_IMPOSSIBLE);
   fc_assert_ret_val(act_unit, ACTPROB_IMPOSSIBLE);
@@ -4758,7 +4758,7 @@ struct act_prob action_prob_unit_vs_tgt(const struct action *paction,
     break;
   case ATK_CITY:
     if (tgt_city) {
-      prob = action_prob_vs_city(act_unit, paction->id, tgt_city);
+      prob = action_prob_vs_city(nmap, act_unit, paction->id, tgt_city);
     }
     break;
   case ATK_UNIT:
diff --git a/common/actions.h b/common/actions.h
index 20d21b1cf7..8cae4ee395 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -551,9 +551,10 @@ 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,
+struct act_prob action_prob_vs_city(const struct civ_map *nmap,
+                                    const struct unit *actor,
                                     const action_id act_id,
-                                    const struct city* victim);
+                                    const struct city *victim);
 
 struct act_prob action_prob_vs_unit(const struct unit* actor,
                                     const action_id act_id,
diff --git a/common/unit.c b/common/unit.c
index 2cfc6d1272..16e5a1b926 100644
--- a/common/unit.c
+++ b/common/unit.c
@@ -194,7 +194,7 @@ bool unit_can_airlift_to(const struct unit *punit,
     return is_action_enabled_unit_on_city(nmap, ACTION_AIRLIFT,
                                           punit, pdest_city);
   } else {
-    return action_prob_possible(action_prob_vs_city(punit, ACTION_AIRLIFT,
+    return action_prob_possible(action_prob_vs_city(nmap, punit, ACTION_AIRLIFT,
                                                     pdest_city));
   }
 }
@@ -261,8 +261,9 @@ int unit_shield_value(const struct unit *punit,
 bool unit_can_help_build_wonder_here(const struct unit *punit)
 {
   struct city *pcity = tile_city(unit_tile(punit));
+  const struct civ_map *nmap = &(wld.map);
 
-  if (!pcity) {
+  if (pcity == NULL) {
     /* No city to help at this tile. */
     return FALSE;
   }
@@ -274,7 +275,7 @@ bool unit_can_help_build_wonder_here(const struct unit *punit)
 
   /* Evaluate all action enablers for extra accuracy. */
   /* TODO: Is it worth it? */
-  return action_prob_possible(action_prob_vs_city(punit,
+  return action_prob_possible(action_prob_vs_city(nmap, punit,
                                                   ACTION_HELP_WONDER,
                                                   pcity));
 }
@@ -474,9 +475,10 @@ bool is_hiding_unit(const struct unit *punit)
 bool unit_can_add_or_build_city(const struct unit *punit)
 {
   struct city *tgt_city;
+  const struct civ_map *nmap = &(wld.map);
 
   if ((tgt_city = tile_city(unit_tile(punit)))) {
-    return action_prob_possible(action_prob_vs_city(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,
@@ -490,12 +492,14 @@ bool unit_can_add_or_build_city(const struct unit *punit)
 bool can_unit_change_homecity_to(const struct unit *punit,
 				 const struct city *pcity)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (pcity == NULL) {
     /* Can't change home city to a non existing city. */
     return FALSE;
   }
 
-  return action_prob_possible(action_prob_vs_city(punit, ACTION_HOME_CITY,
+  return action_prob_possible(action_prob_vs_city(nmap, punit, ACTION_HOME_CITY,
                                                   pcity));
 }
 
diff --git a/server/actiontools.c b/server/actiontools.c
index b12031960a..2860b933b5 100644
--- a/server/actiontools.c
+++ b/server/actiontools.c
@@ -599,6 +599,8 @@ void action_consequence_complete(const struct action *paction,
 static bool may_unit_act_vs_city(struct unit *actor, struct city *target,
                                  bool accept_all_actions)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (actor == NULL || target == NULL) {
     /* Can't do any actions if actor or target are missing. */
     return FALSE;
@@ -616,7 +618,7 @@ static bool may_unit_act_vs_city(struct unit *actor, struct city *target,
       continue;
     }
 
-    if (action_prob_possible(action_prob_vs_city(actor, act, target))) {
+    if (action_prob_possible(action_prob_vs_city(nmap, actor, act, target))) {
       /* The actor unit may be able to do this action to the target
        * city. */
       return TRUE;
@@ -1152,7 +1154,7 @@ action_auto_perf_unit_prob(const enum action_auto_perf_cause cause,
       case ATK_CITY:
         if (tgt_city
             && is_action_enabled_unit_on_city(nmap, act, actor, tgt_city)) {
-          current = action_prob_vs_city(actor, act, tgt_city);
+          current = action_prob_vs_city(nmap, actor, act, tgt_city);
         }
         break;
       case ATK_UNIT:
diff --git a/server/unithand.c b/server/unithand.c
index bbfeb3bf05..785c2121c4 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -2346,7 +2346,7 @@ void handle_unit_get_actions(struct connection *pc,
         /* Only a known city may be targeted. */
         if (target_city) {
           /* Calculate the probabilities. */
-          probabilities[act] = action_prob_vs_city(actor_unit, act,
+          probabilities[act] = action_prob_vs_city(nmap, actor_unit, act,
                                                    target_city);
         } else if (!tile_is_seen(target_tile, actor_player)
                    && action_maybe_possible_actor_unit(nmap, act, actor_unit)
diff --git a/server/unittools.c b/server/unittools.c
index 5a00fd27d7..48aef44f9e 100644
--- a/server/unittools.c
+++ b/server/unittools.c
@@ -4816,7 +4816,7 @@ bool execute_orders(struct unit *punit, const bool fresh)
         tgt_id = dst_tile->index;
         break;
       case ATK_CITY:
-        prob = action_prob_vs_city(punit, order.action,
+        prob = action_prob_vs_city(nmap, punit, order.action,
                                    tgt_city);
         tgt_id = tgt_city->id;
         break;
-- 
2.43.0

