From 9033a39757690d1a5ce8f984b68aa844938f3d61 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Wed, 13 Nov 2024 05:44:21 +0200
Subject: [PATCH 81/81] Unhardcode wld.map from city_refresh_from_main_map()

See RM #1106

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/aihand.c  |  5 +++--
 common/aicore/cm.c   | 11 +++++++----
 common/city.c        | 13 ++++++-------
 common/city.h        |  9 +++++----
 server/cityturn.c    |  5 +++--
 server/sanitycheck.c |  2 +-
 6 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/ai/default/aihand.c b/ai/default/aihand.c
index 974b833fd3..cf501ef90d 100644
--- a/ai/default/aihand.c
+++ b/ai/default/aihand.c
@@ -229,6 +229,7 @@ static void dai_manage_taxes(struct ai_type *ait, struct player *pplayer)
   int rate_sci_balance = RATE_NOT_SET;
   int rate_lux_min_celebrate = maxrate;
   int delta_tax = 0, delta_sci = 0;
+  const struct civ_map *nmap = &(wld.map);
 
 #ifdef DEBUG_TIMERS
   struct timer *taxtimer = NULL;
@@ -667,7 +668,7 @@ static void dai_manage_taxes(struct ai_type *ait, struct player *pplayer)
         cm_query_result(pcity, &cmp, cmr, FALSE);
         if (cmr->found_a_valid) {
           apply_cmresult_to_city(pcity, cmr);
-          city_refresh_from_main_map(pcity, NULL);
+          city_refresh_from_main_map(nmap, pcity, NULL);
           if (!city_happy(pcity)) {
             CITY_LOG(LOG_ERROR, pcity, "is NOT happy when it should be!");
           }
@@ -682,7 +683,7 @@ static void dai_manage_taxes(struct ai_type *ait, struct player *pplayer)
       /* KLUDGE: Must refresh to restore the original values which
        * were clobbered in cm_query_result(), after the tax rates
        * were changed. */
-      city_refresh_from_main_map(pcity, NULL);
+      city_refresh_from_main_map(nmap, pcity, NULL);
     } city_list_iterate_end;
   }
 
diff --git a/common/aicore/cm.c b/common/aicore/cm.c
index 2475a314a3..3d503164f6 100644
--- a/common/aicore/cm.c
+++ b/common/aicore/cm.c
@@ -702,6 +702,7 @@ static void apply_solution(struct cm_state *state,
 {
   struct city *pcity = state->pcity;
   int i, city_radius_sq = city_map_radius_sq_get(pcity);
+  const struct civ_map *nmap = &(wld.map);
 #ifndef FREECIV_NDEBUG
   int citizen_count = 0;
 #endif
@@ -757,7 +758,8 @@ static void apply_solution(struct cm_state *state,
   }
 
   /* Finally we must refresh the city to reset all the precomputed fields. */
-  city_refresh_from_main_map(pcity, state->workers_map);
+  city_refresh_from_main_map(nmap, pcity, state->workers_map);
+
   fc_assert_ret(citizen_count == city_size_get(pcity));
 }
 
@@ -2122,11 +2124,12 @@ void cm_query_result(struct city *pcity,
                      struct cm_result *result, bool negative_ok)
 {
   struct cm_state *state = cm_state_init(pcity, negative_ok);
+  const struct civ_map *nmap = &(wld.map);
 
-  /* Refresh the city.  Otherwise the CM can give wrong results or just be
-   * slower than necessary.  Note that cities are often passed in in an
+  /* Refresh the city. Otherwise the CM can give wrong results or just be
+   * slower than necessary. Note that cities are often passed in in an
    * unrefreshed state (which should probably be fixed). */
-  city_refresh_from_main_map(pcity, NULL);
+  city_refresh_from_main_map(nmap, pcity, NULL);
 
   cm_find_best_solution(state, param, result, negative_ok);
   cm_state_free(state);
diff --git a/common/city.c b/common/city.c
index a1e28cb6d2..dee17c02e2 100644
--- a/common/city.c
+++ b/common/city.c
@@ -2321,7 +2321,8 @@ static const struct city *nearest_gov_center(const struct city *pcity,
   workers within the city map. It uses the tile index and its size is
   defined by city_map_tiles_from_city(_pcity). See also cm_state_init().
 **************************************************************************/
-static inline void get_worked_tile_output(const struct city *pcity,
+static inline void get_worked_tile_output(const struct civ_map *nmap,
+                                          const struct city *pcity,
                                           int *output, bool *workers_map)
 {
   bool is_worked;
@@ -2329,7 +2330,6 @@ static inline void get_worked_tile_output(const struct city *pcity,
   bool is_celebrating = base_city_celebrating(pcity);
 #endif
   struct tile *pcenter = city_tile(pcity);
-  const struct civ_map *nmap = &(wld.map);
 
   memset(output, 0, O_LAST * sizeof(*output));
 
@@ -3197,10 +3197,9 @@ static inline void city_support(struct city *pcity)
 
   If 'workers_map' is set, only basic updates are needed.
 **************************************************************************/
-void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
+void city_refresh_from_main_map(const struct civ_map *nmap,
+                                struct city *pcity, bool *workers_map)
 {
-  const struct civ_map *nmap = &(wld.map);
-
   if (workers_map == NULL) {
     /* do a full refresh */
 
@@ -3213,7 +3212,7 @@ void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
   }
 
   /* Calculate output from citizens (uses city_tile_cache_get_output()). */
-  get_worked_tile_output(pcity, pcity->citizen_base, workers_map);
+  get_worked_tile_output(nmap, pcity, pcity->citizen_base, workers_map);
   add_specialist_output(pcity, pcity->citizen_base);
 
   set_city_production(pcity);
@@ -3223,7 +3222,7 @@ void city_refresh_from_main_map(struct city *pcity, bool *workers_map)
   pcity->pollution = city_pollution(pcity, pcity->prod[O_SHIELD]);
 
   happy_copy(pcity, FEELING_LUXURY);
-  citizen_happy_luxury(pcity);	/* with our new found luxuries */
+  citizen_happy_luxury(pcity);	/* With our new found luxuries */
 
   happy_copy(pcity, FEELING_EFFECT);
   citizen_content_buildings(pcity);
diff --git a/common/city.h b/common/city.h
index 8e7a0f0d7a..a7f2da2cf4 100644
--- a/common/city.h
+++ b/common/city.h
@@ -788,8 +788,9 @@ void city_add_improvement(struct city *pcity,
 void city_remove_improvement(struct city *pcity,
 			     const struct impr_type *pimprove);
 
-/* city update functions */
-void city_refresh_from_main_map(struct city *pcity, bool *workers_map);
+/* City update functions */
+void city_refresh_from_main_map(const struct civ_map *nmap,
+                                struct city *pcity, bool *workers_map);
 
 int city_waste(const struct city *pcity, Output_type_id otype, int total,
                int *breakdown);
@@ -798,13 +799,13 @@ Specialist_type_id best_specialist(Output_type_id otype,
 int get_final_city_output_bonus(const struct city *pcity, Output_type_id otype);
 bool city_built_last_turn(const struct city *pcity);
 
-/* city creation / destruction */
+/* City creation / destruction */
 struct city *create_city_virtual(struct player *pplayer,
 				 struct tile *ptile, const char *name);
 void destroy_city_virtual(struct city *pcity);
 bool city_is_virtual(const struct city *pcity);
 
-/* misc */
+/* Misc */
 bool is_city_option_set(const struct city *pcity, enum city_options option);
 void city_styles_alloc(int num);
 void city_styles_free(void);
diff --git a/server/cityturn.c b/server/cityturn.c
index 8c90249d70..42bf989d62 100644
--- a/server/cityturn.c
+++ b/server/cityturn.c
@@ -158,12 +158,13 @@ static bool check_city_migrations_player(const struct player *pplayer);
 bool city_refresh(struct city *pcity)
 {
   bool retval;
+  const struct civ_map *nmap = &(wld.map);
 
   pcity->server.needs_refresh = FALSE;
 
   retval = city_map_update_radius_sq(pcity);
-  city_units_upkeep(pcity); /* update unit upkeep */
-  city_refresh_from_main_map(pcity, NULL);
+  city_units_upkeep(pcity); /* Update unit upkeep */
+  city_refresh_from_main_map(nmap, pcity, NULL);
   city_style_refresh(pcity);
 
   if (retval) {
diff --git a/server/sanitycheck.c b/server/sanitycheck.c
index 58113bc9fd..f8c9fd3afb 100644
--- a/server/sanitycheck.c
+++ b/server/sanitycheck.c
@@ -351,7 +351,7 @@ static void check_city_size(struct city *pcity, const char *file,
               city_specialists(pcity));
 
     city_repair_size(pcity, delta);
-    city_refresh_from_main_map(pcity, NULL);
+    city_refresh_from_main_map(nmap, pcity, NULL);
   }
 }
 
-- 
2.45.2

