From 2a002230b6bab0fdac3b88c6e4891e95b669d3f0 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sun, 4 Feb 2024 17:59:26 +0200
Subject: [PATCH 13/13] Unhardcode wld.map from
 terrain_surroundings_allow_change()

See RM #227

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/actions.c             |  6 +++---
 common/map.c                 | 31 ++++++++++++++++++-------------
 common/map.h                 | 15 ++++++++++-----
 server/advisors/infracache.c |  3 ++-
 server/maphand.c             |  4 +++-
 5 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/common/actions.c b/common/actions.c
index 1c0f66fb64..bbc92a7fae 100644
--- a/common/actions.c
+++ b/common/actions.c
@@ -4152,7 +4152,7 @@ is_action_possible(const action_id wanted_action,
     pterrain = tile_terrain(target->tile);
     if (pterrain->transform_result == T_NONE
         || pterrain == pterrain->transform_result
-        || !terrain_surroundings_allow_change(target->tile,
+        || !terrain_surroundings_allow_change(nmap, target->tile,
                                               pterrain->transform_result)
         || (terrain_has_flag(pterrain->transform_result, TER_NO_CITIES)
             && (tile_city(target->tile)))) {
@@ -4165,7 +4165,7 @@ is_action_possible(const action_id wanted_action,
     if (pterrain->cultivate_result == NULL) {
       return TRI_NO;
     }
-    if (!terrain_surroundings_allow_change(target->tile,
+    if (!terrain_surroundings_allow_change(nmap, target->tile,
                                            pterrain->cultivate_result)
         || (terrain_has_flag(pterrain->cultivate_result, TER_NO_CITIES)
             && tile_city(target->tile))) {
@@ -4178,7 +4178,7 @@ is_action_possible(const action_id wanted_action,
     if (pterrain->plant_result == NULL) {
       return TRI_NO;
     }
-    if (!terrain_surroundings_allow_change(target->tile,
+    if (!terrain_surroundings_allow_change(nmap, target->tile,
                                            pterrain->plant_result)
         || (terrain_has_flag(pterrain->plant_result, TER_NO_CITIES)
             && tile_city(target->tile))) {
diff --git a/common/map.c b/common/map.c
index 2ad942e181..8f8a752d09 100644
--- a/common/map.c
+++ b/common/map.c
@@ -679,9 +679,10 @@ bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile)
   "reclaimed" from ocean into land.  This is the case only when there are
   a sufficient number of adjacent tiles that are not ocean.
 ***********************************************************************/
-bool can_reclaim_ocean(const struct tile *ptile)
+bool can_reclaim_ocean(const struct civ_map *nmap,
+                       const struct tile *ptile)
 {
-  int land_tiles = 100 - count_terrain_class_near_tile(&(wld.map), ptile,
+  int land_tiles = 100 - count_terrain_class_near_tile(nmap, ptile,
                                                        FALSE, TRUE,
                                                        TC_OCEAN);
 
@@ -693,9 +694,10 @@ bool can_reclaim_ocean(const struct tile *ptile)
   "channeled" from land into ocean.  This is the case only when there are
   a sufficient number of adjacent tiles that are ocean.
 ***********************************************************************/
-bool can_channel_land(const struct tile *ptile)
+bool can_channel_land(const struct civ_map *nmap,
+                      const struct tile *ptile)
 {
-  int ocean_tiles = count_terrain_class_near_tile(&(wld.map), ptile,
+  int ocean_tiles = count_terrain_class_near_tile(nmap, ptile,
                                                   FALSE, TRUE, TC_OCEAN);
 
   return ocean_tiles >= terrain_control.land_channel_requirement_pct;
@@ -706,9 +708,10 @@ bool can_channel_land(const struct tile *ptile)
   terrain with a 'Frozen' flag to terrain without. This requires
   a sufficient number of adjacent unfrozen tiles.
 ***********************************************************************/
-bool can_thaw_terrain(const struct tile *ptile)
+bool can_thaw_terrain(const struct civ_map *nmap,
+                      const struct tile *ptile)
 {
-  int unfrozen_tiles = 100 - count_terrain_flag_near_tile(&(wld.map), ptile,
+  int unfrozen_tiles = 100 - count_terrain_flag_near_tile(nmap, ptile,
                                                           FALSE, TRUE,
                                                           TER_FROZEN);
 
@@ -720,9 +723,10 @@ bool can_thaw_terrain(const struct tile *ptile)
   terrain without a 'Frozen' flag to terrain with. This requires
   a sufficient number of adjacent frozen tiles.
 ***********************************************************************/
-bool can_freeze_terrain(const struct tile *ptile)
+bool can_freeze_terrain(const struct civ_map *nmap,
+                        const struct tile *ptile)
 {
-  int frozen_tiles = count_terrain_flag_near_tile(&(wld.map), ptile,
+  int frozen_tiles = count_terrain_flag_near_tile(nmap, ptile,
                                                   FALSE, TRUE,
                                                   TER_FROZEN);
 
@@ -733,27 +737,28 @@ bool can_freeze_terrain(const struct tile *ptile)
   Returns FALSE if a terrain change to 'pterrain' would be prevented by not
   having enough similar terrain surrounding ptile.
 ***********************************************************************/
-bool terrain_surroundings_allow_change(const struct tile *ptile,
+bool terrain_surroundings_allow_change(const struct civ_map *nmap,
+                                       const struct tile *ptile,
                                        const struct terrain *pterrain)
 {
   bool ret = TRUE;
 
   if (is_ocean(tile_terrain(ptile))
-      && !is_ocean(pterrain) && !can_reclaim_ocean(ptile)) {
+      && !is_ocean(pterrain) && !can_reclaim_ocean(nmap, ptile)) {
     ret = FALSE;
   } else if (!is_ocean(tile_terrain(ptile))
-             && is_ocean(pterrain) && !can_channel_land(ptile)) {
+             && is_ocean(pterrain) && !can_channel_land(nmap, ptile)) {
     ret = FALSE;
   }
 
   if (ret) {
     if (terrain_has_flag(tile_terrain(ptile), TER_FROZEN)
         && !terrain_has_flag(pterrain, TER_FROZEN)
-        && !can_thaw_terrain(ptile)) {
+        && !can_thaw_terrain(nmap, ptile)) {
       ret = FALSE;
     } else if (!terrain_has_flag(tile_terrain(ptile), TER_FROZEN)
                && terrain_has_flag(pterrain, TER_FROZEN)
-               && !can_freeze_terrain(ptile)) {
+               && !can_freeze_terrain(nmap, ptile)) {
       ret = FALSE;
     }
   }
diff --git a/common/map.h b/common/map.h
index 82149a70fe..86e5c84ca8 100644
--- a/common/map.h
+++ b/common/map.h
@@ -310,11 +310,16 @@ bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile);
 bv_extras get_tile_infrastructure_set(const struct tile *ptile,
                                       int *count);
 
-bool can_channel_land(const struct tile *ptile);
-bool can_reclaim_ocean(const struct tile *ptile);
-bool can_thaw_terrain(const struct tile *ptile);
-bool can_freeze_terrain(const struct tile *ptile);
-bool terrain_surroundings_allow_change(const struct tile *ptile,
+bool can_channel_land(const struct civ_map *nmap,
+                      const struct tile *ptile);
+bool can_reclaim_ocean(const struct civ_map *nmap,
+                       const struct tile *ptile);
+bool can_thaw_terrain(const struct civ_map *nmap,
+                      const struct tile *ptile);
+bool can_freeze_terrain(const struct civ_map *nmap,
+                        const struct tile *ptile);
+bool terrain_surroundings_allow_change(const struct civ_map *nmap,
+                                       const struct tile *ptile,
                                        const struct terrain *pterrain);
 
 extern struct terrain_misc terrain_control;
diff --git a/server/advisors/infracache.c b/server/advisors/infracache.c
index 9c6d5745ba..87278ccb2d 100644
--- a/server/advisors/infracache.c
+++ b/server/advisors/infracache.c
@@ -145,6 +145,7 @@ static adv_want adv_calc_transform(const struct city *pcity,
   adv_want goodness;
   struct tile *vtile;
   struct terrain *old_terrain, *new_terrain;
+  const struct civ_map *nmap = &(wld.map);
 
   fc_assert_ret_val(ptile != NULL, -1);
 
@@ -155,7 +156,7 @@ static adv_want adv_calc_transform(const struct city *pcity,
     return -1;
   }
 
-  if (!terrain_surroundings_allow_change(ptile, new_terrain)) {
+  if (!terrain_surroundings_allow_change(nmap, ptile, new_terrain)) {
     /* Can't do this terrain conversion here. */
     return -1;
   }
diff --git a/server/maphand.c b/server/maphand.c
index dba5133e32..c50fdb6e4a 100644
--- a/server/maphand.c
+++ b/server/maphand.c
@@ -131,6 +131,8 @@ void climate_change(bool warming, int effect)
 {
   int k = map_num_tiles();
   bool used[k];
+  const struct civ_map *nmap = &(wld.map);
+
   memset(used, 0, sizeof(used));
 
   log_verbose("Climate change: %s (%d)",
@@ -184,7 +186,7 @@ void climate_change(bool warming, int effect)
 
       /* Only change between water and land at coastlines, and between
        * frozen and unfrozen at ice margin */
-      if (!terrain_surroundings_allow_change(ptile, new)) {
+      if (!terrain_surroundings_allow_change(nmap, ptile, new)) {
         continue;
       }
 
-- 
2.43.0

