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

See RM #227

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/actres.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/actres.c b/common/actres.c
index 486b056edc..c58641f755 100644
--- a/common/actres.c
+++ b/common/actres.c
@@ -1066,7 +1066,7 @@ enum fc_tristate actres_possible(enum action_result result,
     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)))) {
@@ -1079,7 +1079,7 @@ enum fc_tristate actres_possible(enum action_result result,
     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))) {
@@ -1092,7 +1092,7 @@ enum fc_tristate actres_possible(enum action_result result,
     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 ea1c416aff..474efae65e 100644
--- a/common/map.c
+++ b/common/map.c
@@ -687,9 +687,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);
 
@@ -701,9 +702,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;
@@ -714,9 +716,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);
 
@@ -728,9 +731,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);
 
@@ -741,27 +745,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 294476e211..991bf952fc 100644
--- a/common/map.h
+++ b/common/map.h
@@ -302,11 +302,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 5e8e1f196b..41e79c4c8d 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 661c7d8181..ce4a086a49 100644
--- a/server/maphand.c
+++ b/server/maphand.c
@@ -134,6 +134,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)",
@@ -187,7 +189,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

