From a6dfac3c37070c06d74bcbc9fa380359aa674f57 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 20 Sep 2024 20:51:57 +0300
Subject: [PATCH 19/19] 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 ++++++++-------
 ai/threaded/taicity.c          | 17 ++++++++++-------
 common/actions.c               |  4 ++--
 common/actions.h               |  3 ++-
 server/advisors/autosettlers.c | 14 ++++++++------
 5 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/ai/tex/texaicity.c b/ai/tex/texaicity.c
index de885a8932..caf4037557 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,
@@ -279,7 +280,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
           struct action *taction = action_by_number(try_act);
           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,
@@ -293,7 +294,7 @@ static void texai_tile_worker_task_select(struct player *pplayer,
           struct action *taction = action_by_number(try_act);
           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,
@@ -390,7 +391,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))) {
@@ -418,7 +419,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/ai/threaded/taicity.c b/ai/threaded/taicity.c
index 815a1620ef..93cc8526b8 100644
--- a/ai/threaded/taicity.c
+++ b/ai/threaded/taicity.c
@@ -105,6 +105,7 @@ static void tai_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;
@@ -151,7 +152,7 @@ static void tai_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))) {
@@ -174,7 +175,7 @@ static void tai_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,
@@ -201,7 +202,7 @@ static void tai_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,
@@ -223,9 +224,10 @@ static void tai_tile_worker_task_select(struct player *pplayer,
       if (removing) {
         as_rmextra_action_iterate(try_act) {
           struct action *taction = action_by_number(try_act);
+
           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,
@@ -237,9 +239,10 @@ static void tai_tile_worker_task_select(struct player *pplayer,
       } else {
         as_extra_action_iterate(try_act) {
           struct action *taction = action_by_number(try_act);
+
           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,
@@ -335,7 +338,7 @@ static void tai_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))) {
@@ -363,7 +366,7 @@ static void tai_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 05cb1eb9b6..a5957273ba 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -6553,7 +6553,8 @@ action_speculate_unit_on_units(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,
@@ -6565,7 +6566,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 e8c3238cfa..400c58603d 100644
--- a/common/actions.h
+++ b/common/actions.h
@@ -881,7 +881,8 @@ action_speculate_unit_on_units(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/autosettlers.c b/server/advisors/autosettlers.c
index 5fb767ef45..ab5e0acc80 100644
--- a/server/advisors/autosettlers.c
+++ b/server/advisors/autosettlers.c
@@ -551,7 +551,7 @@ adv_want settler_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 settler_evaluate_improvements(const struct civ_map *nmap,
             if (removing) {
               as_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 settler_evaluate_improvements(const struct civ_map *nmap,
             } else {
               as_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 settler_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))) {
@@ -761,7 +763,7 @@ adv_want settler_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))) {
@@ -1314,7 +1316,7 @@ bool auto_settlers_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

