From 9a6ae2afebe66d136a7cdd4b3996008f58731bd5 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 2 Mar 2024 05:55:12 +0200
Subject: [PATCH 20/20] Unhardcode wld.map from caravan_search_from()

See RM #281

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/aicore/caravan.c | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/common/aicore/caravan.c b/common/aicore/caravan.c
index 7c874666d0..fcfd98ee44 100644
--- a/common/aicore/caravan.c
+++ b/common/aicore/caravan.c
@@ -204,7 +204,8 @@ int caravan_result_compare(const struct caravan_result *a,
 typedef bool (*search_callback) (void *data, const struct city *pcity,
                                  int arrival_turn, int arrival_moves_left);
 
-static void caravan_search_from(const struct unit *caravan,
+static void caravan_search_from(const struct civ_map *nmap,
+                                const struct unit *caravan,
                                 const struct caravan_parameter *param,
                                 struct tile *start_tile,
                                 int turns_before, int moves_left_before,
@@ -215,7 +216,6 @@ static void caravan_search_from(const struct unit *caravan,
   struct pf_map *pfm;
   struct pf_parameter pfparam;
   int end_time;
-  const struct civ_map *nmap = &(wld.map);
 
   end_time = param->horizon - turns_before;
 
@@ -629,7 +629,8 @@ static bool cewt_callback(void *vdata, const struct city *dest,
   Using the caravan_search function to take transit time into account,
   evaluate the benefit of sending the caravan to dest.
 ****************************************************************************/
-static void caravan_evaluate_withtransit(const struct unit *caravan,
+static void caravan_evaluate_withtransit(const struct civ_map *nmap,
+                                         const struct unit *caravan,
                                          const struct city *dest,
                                          const struct caravan_parameter *param,
                                          struct caravan_result *result,
@@ -642,7 +643,7 @@ static void caravan_evaluate_withtransit(const struct unit *caravan,
   caravan_result_init(result, game_city_by_number(caravan->homecity),
                       dest, 0);
   data.result = result;
-  caravan_search_from(caravan, param, unit_tile(caravan), 0,
+  caravan_search_from(nmap, caravan, param, unit_tile(caravan), 0,
                       caravan->moves_left, omniscient, cewt_callback, &data);
 }
 
@@ -654,10 +655,12 @@ void caravan_evaluate(const struct unit *caravan,
                       const struct caravan_parameter *param,
                       struct caravan_result *result, bool omniscient)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (param->ignore_transit_time) {
     caravan_evaluate_notransit(caravan, dest, param, result);
   } else {
-    caravan_evaluate_withtransit(caravan, dest, param, result, omniscient);
+    caravan_evaluate_withtransit(nmap, caravan, dest, param, result, omniscient);
   }
 }
 
@@ -720,6 +723,7 @@ static bool cfbdw_callback(void *vdata, const struct city *dest,
   Using caravan_search, find the best destination.
 ****************************************************************************/
 static void caravan_find_best_destination_withtransit(
+    const struct civ_map *nmap,
     const struct unit *caravan,
     const struct caravan_parameter *param,
     const struct city *src,
@@ -742,7 +746,7 @@ static void caravan_find_best_destination_withtransit(
     start_tile = unit_tile(caravan);
   }
 
-  caravan_search_from(caravan, param, start_tile, turns_before,
+  caravan_search_from(nmap, caravan, param, start_tile, turns_before,
                       caravan->moves_left, omniscient, cfbdw_callback, &data);
 }
 
@@ -755,6 +759,8 @@ void caravan_find_best_destination(const struct unit *caravan,
                                    const struct caravan_parameter *parameter,
                                    struct caravan_result *result, bool omniscient)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (parameter->ignore_transit_time) {
     caravan_find_best_destination_notransit(caravan, parameter, result);
   } else {
@@ -762,7 +768,7 @@ void caravan_find_best_destination(const struct unit *caravan,
 
     fc_assert(src != NULL);
 
-    caravan_find_best_destination_withtransit(caravan, parameter, src, 0,
+    caravan_find_best_destination_withtransit(nmap, caravan, parameter, src, 0,
                                               caravan->moves_left, omniscient, result);
   }
 }
@@ -809,10 +815,10 @@ struct cowt_data {
 
 /************************************************************************//**
   Callback for the caravan_search invocation in
-  caravan_optimize_withtransit.
+  caravan_optimize_withtransit().
 
-  For every city we can reach, use caravan_find_best_destination as a
-  subroutine.
+  For every city we can reach, use caravan_find_best_destination_withtransit()
+  as a subroutine.
 ****************************************************************************/
 static bool cowt_callback(void *vdata, const struct city *pcity,
                           int arrival_time, int moves_left)
@@ -820,6 +826,7 @@ static bool cowt_callback(void *vdata, const struct city *pcity,
   struct cowt_data *data = vdata;
   const struct unit *caravan = data->caravan;
   struct caravan_result current;
+  const struct civ_map *nmap = &(wld.map);
 
   caravan_result_init(&current, game_city_by_number(caravan->homecity),
                       pcity, arrival_time);
@@ -833,7 +840,7 @@ static bool cowt_callback(void *vdata, const struct city *pcity,
   /* Next, try changing home city (if we're allowed to) */
   if (city_owner(pcity) == unit_owner(caravan)) {
     caravan_find_best_destination_withtransit(
-                caravan, data->param, pcity, arrival_time, moves_left, data->omniscient,
+                nmap, caravan, data->param, pcity, arrival_time, moves_left, data->omniscient,
                 &current);
     if (caravan_result_compare(&current, data->best) > 0) {
       *data->best = current;
@@ -847,7 +854,8 @@ static bool cowt_callback(void *vdata, const struct city *pcity,
   Find the best src/dest pair (including possibly changing home city), taking
   account of the trip time.
 ****************************************************************************/
-static void caravan_optimize_withtransit(const struct unit *caravan,
+static void caravan_optimize_withtransit(const struct civ_map *nmap,
+                                         const struct unit *caravan,
                                          const struct caravan_parameter *param,
                                          struct caravan_result *result,
                                          bool omniscient)
@@ -859,7 +867,7 @@ static void caravan_optimize_withtransit(const struct unit *caravan,
   data.best = result;
   data.omniscient = omniscient;
   caravan_result_init_zero(data.best);
-  caravan_search_from(caravan, param, unit_tile(caravan), 0,
+  caravan_search_from(nmap, caravan, param, unit_tile(caravan), 0,
                       caravan->moves_left, omniscient, cowt_callback, &data);
 }
 
@@ -872,9 +880,11 @@ void caravan_optimize_allpairs(const struct unit *caravan,
                                const struct caravan_parameter *param,
                                struct caravan_result *result, bool omniscient)
 {
+  const struct civ_map *nmap = &(wld.map);
+
   if (param->ignore_transit_time) {
     caravan_optimize_notransit(caravan, param, result);
   } else {
-    caravan_optimize_withtransit(caravan, param, result, omniscient);
+    caravan_optimize_withtransit(nmap, caravan, param, result, omniscient);
   }
 }
-- 
2.43.0

