From 1f1d3a06f76bf23231688690698fc99129e9ba8c Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 20 Sep 2024 20:51:57 +0300
Subject: [PATCH 43/43] Unhardcode wld.map from action_speculate_unit_on_tile()

See RM #893

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/tex/texaicity.c            | 15 ++++++++-------
 common/actions.c              |  4 ++--
 common/actions.h              |  3 ++-
 server/advisors/autoworkers.c | 14 ++++++++------
 4 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/ai/tex/texaicity.c b/ai/tex/texaicity.c
index 7e5c1dfeb8..461194e225 100644
--- a/ai/tex/texaicity.c
+++ b/ai/tex/texaicity.c
@@ -160,6 +160,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
 {
   int orig_value;
   bool potential_worst_worked = FALSE;
+  const struct civ_map *nmap = &(wld.map);
 
   if (!city_can_work_tile(pcity, ptile)) {
     return;
@@ -206,7 +207,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
       }
 
       if (action_prob_possible(
-            action_speculate_unit_on_tile(act,
+            action_speculate_unit_on_tile(nmap, act,
                                           punit, unit_home(punit), ptile,
                                           TRUE,
                                           ptile, tgt))) {
@@ -228,7 +229,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
           if (limit == TWTL_BUILDABLE_UNITS) {
             unit_list_iterate(units, punit) {
               if (action_prob_possible(
-                    action_speculate_unit_on_tile(act,
+                    action_speculate_unit_on_tile(nmap, act,
                                                   punit, unit_home(punit),
                                                   ptile,
                                                   TRUE,
@@ -255,7 +256,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
           if (limit == TWTL_BUILDABLE_UNITS) {
             unit_list_iterate(units, punit) {
               if (action_prob_possible(
-                    action_speculate_unit_on_tile(act,
+                    action_speculate_unit_on_tile(nmap, act,
                                                   punit, unit_home(punit),
                                                   ptile,
                                                   TRUE,
@@ -280,7 +281,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
 
           if (is_extra_removed_by_action(tgt, taction)
               && action_prob_possible(
-                action_speculate_unit_on_tile(try_act,
+                action_speculate_unit_on_tile(nmap, try_act,
                                               punit,
                                               unit_home(punit), ptile,
                                               TRUE,
@@ -295,7 +296,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
 
           if (is_extra_caused_by_action(tgt, taction)
               && action_prob_possible(
-                action_speculate_unit_on_tile(try_act,
+                action_speculate_unit_on_tile(nmap, try_act,
                                               punit,
                                               unit_home(punit), ptile,
                                               TRUE,
@@ -392,7 +393,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
               fc_assert_action(action_get_target_kind(paction) == ATK_TILE,
                                break);
               if (action_prob_possible(action_speculate_unit_on_tile(
-                                           paction->id,
+                                           nmap, paction->id,
                                            punit, unit_home(punit), ptile,
                                            TRUE,
                                            ptile, tgt))) {
@@ -420,7 +421,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
               fc_assert_action(action_get_target_kind(paction) == ATK_TILE,
                                break);
               if (action_prob_possible(action_speculate_unit_on_tile(
-                                         paction->id,
+                                         nmap, paction->id,
                                          punit, unit_home(punit), ptile,
                                          TRUE,
                                          ptile, tgt))) {
diff --git a/common/actions.c b/common/actions.c
index 7aaff41d8b..b3e1fc8dfa 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -4894,7 +4894,8 @@ action_speculate_unit_on_stack(const struct civ_map *nmap,
   extra) given the specified game state changes.
 **************************************************************************/
 struct act_prob
-action_speculate_unit_on_tile(action_id act_id,
+action_speculate_unit_on_tile(const struct civ_map *nmap,
+                              action_id act_id,
                               const struct unit *actor,
                               const struct city *actor_home,
                               const struct tile *actor_tile,
@@ -4906,7 +4907,6 @@ action_speculate_unit_on_tile(action_id act_id,
    * current position rather than on actor_tile. Maybe this function should
    * return ACTPROB_NOT_IMPLEMENTED when one of those is detected and no
    * other requirement makes the action ACTPROB_IMPOSSIBLE? */
-  const struct civ_map *nmap = &(wld.map);
 
   if (omniscient_cheat) {
     if (is_action_enabled_unit_on_tile_full(nmap, act_id,
diff --git a/common/actions.h b/common/actions.h
index ed79289079..e03c29cffd 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -620,7 +620,8 @@ action_speculate_unit_on_stack(const struct civ_map *nmap,
                                const struct tile *target);
 
 struct act_prob
-action_speculate_unit_on_tile(action_id act_id,
+action_speculate_unit_on_tile(const struct civ_map *nmap,
+                              action_id act_id,
                               const struct unit *actor,
                               const struct city *actor_home,
                               const struct tile *actor_tile,
diff --git a/server/advisors/autoworkers.c b/server/advisors/autoworkers.c
index 51dcbaec31..9e80ae47aa 100644
--- a/server/advisors/autoworkers.c
+++ b/server/advisors/autoworkers.c
@@ -551,7 +551,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
             if (adv_city_worker_act_get(pcity, cindex,
                                         action_id_get_activity(act)) >= 0
                 && action_prob_possible(
-                  action_speculate_unit_on_tile(act,
+                  action_speculate_unit_on_tile(nmap, act,
                                                 punit, unit_home(punit),
                                                 ptile,
                                                 parameter.omniscience,
@@ -590,11 +590,12 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
             if (removing) {
               aw_rmextra_action_iterate(try_act) {
                 struct action *taction = action_by_number(try_act);
+
                 if (is_extra_removed_by_action(pextra, taction)) {
                   /* We do not even evaluate actions we can't do.
                    * Removal is not considered prerequisite for anything */
                   if (action_prob_possible(
-                        action_speculate_unit_on_tile(try_act,
+                        action_speculate_unit_on_tile(nmap, try_act,
                                                       punit,
                                                       unit_home(punit),
                                                       ptile,
@@ -609,10 +610,11 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
             } else {
               aw_extra_action_iterate(try_act) {
                 struct action *taction = action_by_number(try_act);
+
                 if (is_extra_caused_by_action(pextra, taction)) {
                   eval_act = action_id_get_activity(try_act);
                   if (action_prob_possible(
-                        action_speculate_unit_on_tile(try_act,
+                        action_speculate_unit_on_tile(nmap, try_act,
                                                       punit,
                                                       unit_home(punit),
                                                       ptile,
@@ -716,7 +718,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
                   dep_tgt = road_extra_get(pdep);
 
                   if (action_prob_possible(
-                        action_speculate_unit_on_tile(ACTION_ROAD,
+                        action_speculate_unit_on_tile(nmap, ACTION_ROAD,
                                                       punit, unit_home(punit), ptile,
                                                       parameter.omniscience,
                                                       ptile, dep_tgt))) {
@@ -762,7 +764,7 @@ adv_want worker_evaluate_improvements(const struct civ_map *nmap,
 
                     if (eval_dep_act != ACTIVITY_LAST) {
                       if (action_prob_possible(
-                            action_speculate_unit_on_tile(eval_dep_action,
+                            action_speculate_unit_on_tile(nmap, eval_dep_action,
                                                           punit, unit_home(punit), ptile,
                                                           parameter.omniscience,
                                                           ptile, pdep))) {
@@ -1323,7 +1325,7 @@ bool auto_workers_speculate_can_act_at(const struct unit *punit,
                                     ptile));
     case ATK_TILE:
       return action_prob_possible(action_speculate_unit_on_tile(
-                                    paction->id,
+                                    nmap, paction->id,
                                     punit, unit_home(punit), ptile,
                                     omniscient_cheat,
                                     ptile, target));
-- 
2.45.2

