From 1091530342dbce4521ad66aa96ded83d5efc3f03 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 16 Dec 2023 14:36:45 +0200
Subject: [PATCH 8/8] Make terrain.c functions to take map as parameter

Replace hardcoded wld.map uses

See RM #71

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 ai/default/aiferry.c               | 12 +++--
 ai/default/aiunit.c                |  4 +-
 ai/default/daieffects.c            |  6 +--
 ai/default/daisettler.c            |  8 ++--
 client/gui-gtk-3.22/cityrep.c      |  3 +-
 client/gui-gtk-4.0/cityrep.c       |  3 +-
 client/gui-qt/cityrep.cpp          |  3 +-
 common/map.c                       | 20 ++++----
 common/requirements.c              | 12 ++---
 common/terrain.c                   | 73 +++++++++++++++++-------------
 common/terrain.h                   | 33 +++++++++-----
 server/advisors/advbuilding.c      |  2 +-
 server/citytools.c                 |  5 +-
 server/generator/fracture_map.c    |  8 ++--
 server/generator/mapgen.c          | 50 +++++++++++---------
 server/generator/temperature_map.c |  2 +-
 server/maphand.c                   |  2 +-
 17 files changed, 144 insertions(+), 102 deletions(-)

diff --git a/ai/default/aiferry.c b/ai/default/aiferry.c
index a613decd93..1ee75797fd 100644
--- a/ai/default/aiferry.c
+++ b/ai/default/aiferry.c
@@ -768,6 +768,8 @@ bool aiferry_goto_amphibious(struct ai_type *ait, struct unit *ferry,
 bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer,
                       struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
 {
+  struct civ_map *nmap = &(wld.map);
+
   if (!unit_transported(punit)) {
     /* We are not on a boat and we cannot walk */
     int boatid;
@@ -778,7 +780,7 @@ bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer,
     UNIT_LOG(LOGLEVEL_GOBYBOAT, punit, "will have to go to (%d,%d) by boat",
              TILE_XY(dest_tile));
 
-    if (!is_terrain_class_near_tile(unit_tile(punit), TC_OCEAN)) {
+    if (!is_terrain_class_near_tile(nmap, unit_tile(punit), TC_OCEAN)) {
       struct pf_path *path_to_ferry = NULL;
 
       boatid = aiferry_find_boat(ait, punit, cap, &path_to_ferry);
@@ -802,7 +804,7 @@ bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer,
       pf_path_destroy(path_to_ferry);
     }
 
-    if (!is_terrain_class_near_tile(unit_tile(punit), TC_OCEAN)) {
+    if (!is_terrain_class_near_tile(nmap, unit_tile(punit), TC_OCEAN)) {
       /* Still haven't reached the coast */
       return FALSE;
     }
@@ -939,14 +941,14 @@ bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer,
 }
 
 
-/* ===================== boat management ================================= */
+/* ===================== Boat management ================================= */
 
 /**********************************************************************//**
-  A helper for ai_manage_ferryboat.  Finds a passenger for the ferry.
+  A helper for ai_manage_ferryboat. Finds a passenger for the ferry.
   Potential passengers signal the boats by setting their ai.ferry field to
   FERRY_WANTED.
 
-  TODO: lift the path off the map
+  TODO: Lift the path off the map
 **************************************************************************/
 static bool aiferry_findcargo(struct ai_type *ait, struct unit *pferry)
 {
diff --git a/ai/default/aiunit.c b/ai/default/aiunit.c
index 1c511ca0ea..6c634f4d4a 100644
--- a/ai/default/aiunit.c
+++ b/ai/default/aiunit.c
@@ -1235,7 +1235,7 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
 
     atype = unit_type_get(aunit);
 
-    /* dealing with invasion stuff */
+    /* Dealing with invasion stuff */
     if (IS_ATTACKER(atype)) {
       if (aunit->activity == ACTIVITY_GOTO) {
         invasion_funct(ait, aunit, TRUE, 0,
@@ -1300,7 +1300,7 @@ adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
                                         aiferry_find_boat(ait, punit, 1, NULL));
     }
 
-    if (0 == punit->id && is_terrain_class_near_tile(punit_tile, TC_OCEAN)) {
+    if (punit->id == 0 && is_terrain_class_near_tile(nmap, punit_tile, TC_OCEAN)) {
       harbor = TRUE;
     }
   }
diff --git a/ai/default/daieffects.c b/ai/default/daieffects.c
index 46f093dc66..7f4f11d615 100644
--- a/ai/default/daieffects.c
+++ b/ai/default/daieffects.c
@@ -215,7 +215,7 @@ adv_want dai_effect_value(struct player *pplayer,
     v += (get_entertainers(pcity) + pcity->feel[CITIZEN_UNHAPPY][FEELING_FINAL]) * 5 * amount;
     if (city_list_size(pplayer->cities)
 	> get_player_bonus(pplayer, EFT_EMPIRE_SIZE_BASE)) {
-      v += c * amount; /* offset large empire size */
+      v += c * amount; /* Offset large empire size */
     }
     v += c * amount;
     break;
@@ -334,7 +334,7 @@ adv_want dai_effect_value(struct player *pplayer,
   case EFT_HAVE_CONTACTS:
     {
       int new_contacts = 0;
-      
+
       players_iterate_alive(theother) {
         if (player_diplstate_get(pplayer, theother)->contact_turns_left <= 0) {
           new_contacts++;
@@ -511,7 +511,7 @@ adv_want dai_effect_value(struct player *pplayer,
               /* FIXME: This ignores riverboats on some rulesets.
                         We should analyze rulesets when game starts
                         and have relevant checks here. */
-              && is_terrain_class_near_tile(pcity->tile, TC_OCEAN))) {
+              && is_terrain_class_near_tile(&(wld.map), pcity->tile, TC_OCEAN))) {
         if (place > 0 && adv->threats.continent[place]) {
           v += amount * 4 / 5;
         } else {
diff --git a/ai/default/daisettler.c b/ai/default/daisettler.c
index 6f570d1c15..f34ba2db50 100644
--- a/ai/default/daisettler.c
+++ b/ai/default/daisettler.c
@@ -614,7 +614,8 @@ static adv_want result_defense_bonus(struct player *pplayer,
 *****************************************************************************/
 static adv_want naval_bonus(const struct cityresult *result)
 {
-  bool ocean_adjacent = is_terrain_class_near_tile(result->tile, TC_OCEAN);
+  bool ocean_adjacent = is_terrain_class_near_tile(&(wld.map),
+                                                   result->tile, TC_OCEAN);
 
   /* Adjust for ocean adjacency, which is nice */
   if (ocean_adjacent) {
@@ -916,7 +917,8 @@ static struct cityresult *find_best_city_placement(struct ai_type *ait,
   }
 
   if (ferry || (use_virt_boat
-                && is_terrain_class_near_tile(unit_tile(punit), TC_OCEAN)
+                && is_terrain_class_near_tile(&(wld.map),
+                                              unit_tile(punit), TC_OCEAN)
                 && tile_city(unit_tile(punit)))) {
     if (!ferry) {
       /* No boat?  Get a virtual one! */
@@ -1333,7 +1335,7 @@ void contemplate_new_city(struct ai_type *ait, struct city *pcity)
 
   if (is_ai(pplayer)) {
     struct cityresult *result;
-    bool is_coastal = is_terrain_class_near_tile(pcenter, TC_OCEAN);
+    bool is_coastal = is_terrain_class_near_tile(&(wld.map), pcenter, TC_OCEAN);
     struct ai_city *city_data = def_ai_city_data(pcity, ait);
 
     result = find_best_city_placement(ait, virtualunit, is_coastal, is_coastal);
diff --git a/client/gui-gtk-3.22/cityrep.c b/client/gui-gtk-3.22/cityrep.c
index f18a8ae4f0..356aa41016 100644
--- a/client/gui-gtk-3.22/cityrep.c
+++ b/client/gui-gtk-3.22/cityrep.c
@@ -1283,7 +1283,8 @@ static void city_select_coastal_callback(GtkMenuItem *item, gpointer data)
   for (itree_begin(model, &it); !itree_end(&it); itree_next(&it)) {
     struct city *pcity = city_model_get(model, TREE_ITER_PTR(it));
 
-    if (NULL != pcity && is_terrain_class_near_tile(pcity->tile, TC_OCEAN)) {
+    if (pcity != NULL
+        && is_terrain_class_near_tile(&(wld.map), pcity->tile, TC_OCEAN)) {
       itree_select(city_selection, &it);
     }
   }
diff --git a/client/gui-gtk-4.0/cityrep.c b/client/gui-gtk-4.0/cityrep.c
index 4a3df57e8c..4c6160b2c4 100644
--- a/client/gui-gtk-4.0/cityrep.c
+++ b/client/gui-gtk-4.0/cityrep.c
@@ -1321,7 +1321,8 @@ static void city_select_coastal_callback(GSimpleAction *action,
   for (itree_begin(model, &it); !itree_end(&it); itree_next(&it)) {
     struct city *pcity = city_model_get(model, TREE_ITER_PTR(it));
 
-    if (NULL != pcity && is_terrain_class_near_tile(pcity->tile, TC_OCEAN)) {
+    if (pcity != NULL
+        && is_terrain_class_near_tile(&(wld.map), pcity->tile, TC_OCEAN)) {
       itree_select(city_selection, &it);
     }
   }
diff --git a/client/gui-qt/cityrep.cpp b/client/gui-qt/cityrep.cpp
index 6e213d3c3e..e0abf55ac9 100644
--- a/client/gui-qt/cityrep.cpp
+++ b/client/gui-qt/cityrep.cpp
@@ -857,7 +857,8 @@ void city_widget::select_coastal()
       continue;
     }
     pcity = reinterpret_cast<city *>(qvar.value<void *>());
-    if (NULL != pcity && is_terrain_class_near_tile(pcity->tile, TC_OCEAN)) {
+    if (pcity != nullptr
+        && is_terrain_class_near_tile(&(wld.map), pcity->tile, TC_OCEAN)) {
       selection.append(QItemSelectionRange(i));
     }
   }
diff --git a/common/map.c b/common/map.c
index d59594507c..fc5f3e2419 100644
--- a/common/map.c
+++ b/common/map.c
@@ -681,7 +681,8 @@ bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile)
 ***********************************************************************/
 bool can_reclaim_ocean(const struct tile *ptile)
 {
-  int land_tiles = 100 - count_terrain_class_near_tile(ptile, FALSE, TRUE,
+  int land_tiles = 100 - count_terrain_class_near_tile(&(wld.map), ptile,
+                                                       FALSE, TRUE,
                                                        TC_OCEAN);
 
   return land_tiles >= terrain_control.ocean_reclaim_requirement_pct;
@@ -694,19 +695,21 @@ bool can_reclaim_ocean(const struct tile *ptile)
 ***********************************************************************/
 bool can_channel_land(const struct tile *ptile)
 {
-  int ocean_tiles = count_terrain_class_near_tile(ptile, FALSE, TRUE, TC_OCEAN);
+  int ocean_tiles = count_terrain_class_near_tile(&(wld.map), ptile,
+                                                  FALSE, TRUE, TC_OCEAN);
 
   return ocean_tiles >= terrain_control.land_channel_requirement_pct;
 }
 
 /*******************************************************************//**
   Returns true if the tile at the given location can be thawed from
-  terrain with a 'Frozen' flag to terrain without. This requires a
-  sufficient number of adjacent unfrozen tiles.
+  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)
 {
-  int unfrozen_tiles = 100 - count_terrain_flag_near_tile(ptile, FALSE, TRUE,
+  int unfrozen_tiles = 100 - count_terrain_flag_near_tile(&(wld.map), ptile,
+                                                          FALSE, TRUE,
                                                           TER_FROZEN);
 
   return unfrozen_tiles >= terrain_control.terrain_thaw_requirement_pct;
@@ -714,12 +717,13 @@ bool can_thaw_terrain(const struct tile *ptile)
 
 /*******************************************************************//**
   Returns true if the tile at the given location can be turned from
-  terrain without a 'Frozen' flag to terrain with. This requires a
-  sufficient number of adjacent frozen tiles.
+  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)
 {
-  int frozen_tiles = count_terrain_flag_near_tile(ptile, FALSE, TRUE,
+  int frozen_tiles = count_terrain_flag_near_tile(&(wld.map), ptile,
+                                                  FALSE, TRUE,
                                                   TER_FROZEN);
 
   return frozen_tiles >= terrain_control.terrain_freeze_requirement_pct;
diff --git a/common/requirements.c b/common/requirements.c
index a38fcdc2b8..478c53dbf1 100644
--- a/common/requirements.c
+++ b/common/requirements.c
@@ -2462,12 +2462,12 @@ is_terrain_req_active(const struct civ_map *nmap,
     if (!context->tile) {
       return TRI_MAYBE;
     }
-    return BOOL_TO_TRISTATE(pterrain && is_terrain_card_near(context->tile, pterrain, TRUE));
+    return BOOL_TO_TRISTATE(pterrain && is_terrain_card_near(nmap, context->tile, pterrain, TRUE));
   case REQ_RANGE_ADJACENT:
     if (!context->tile) {
       return TRI_MAYBE;
     }
-    return BOOL_TO_TRISTATE(pterrain && is_terrain_near_tile(context->tile, pterrain, TRUE));
+    return BOOL_TO_TRISTATE(pterrain && is_terrain_near_tile(nmap, context->tile, pterrain, TRUE));
   case REQ_RANGE_CITY:
     if (!context->city) {
       return TRI_MAYBE;
@@ -2560,13 +2560,13 @@ is_terrainclass_req_active(const struct civ_map *nmap,
       return TRI_MAYBE;
     }
     return BOOL_TO_TRISTATE(terrain_type_terrain_class(tile_terrain(context->tile)) == pclass
-                            || is_terrain_class_card_near(context->tile, pclass));
+                            || is_terrain_class_card_near(nmap, context->tile, pclass));
   case REQ_RANGE_ADJACENT:
     if (!context->tile) {
       return TRI_MAYBE;
     }
     return BOOL_TO_TRISTATE(terrain_type_terrain_class(tile_terrain(context->tile)) == pclass
-                            || is_terrain_class_near_tile(context->tile, pclass));
+                            || is_terrain_class_near_tile(nmap, context->tile, pclass));
   case REQ_RANGE_CITY:
     if (!context->city) {
       return TRI_MAYBE;
@@ -2664,7 +2664,7 @@ is_terrainflag_req_active(const struct civ_map *nmap,
     }
     return BOOL_TO_TRISTATE(terrain_has_flag(tile_terrain(context->tile),
                                              terrflag)
-                            || is_terrain_flag_card_near(context->tile,
+                            || is_terrain_flag_card_near(nmap, context->tile,
                                                          terrflag));
   case REQ_RANGE_ADJACENT:
     if (!context->tile) {
@@ -2672,7 +2672,7 @@ is_terrainflag_req_active(const struct civ_map *nmap,
     }
     return BOOL_TO_TRISTATE(terrain_has_flag(tile_terrain(context->tile),
                                              terrflag)
-                            || is_terrain_flag_near_tile(context->tile,
+                            || is_terrain_flag_near_tile(nmap, context->tile,
                                                          terrflag));
   case REQ_RANGE_CITY:
     if (!context->city) {
diff --git a/common/terrain.c b/common/terrain.c
index b31416c2e1..01c319aac3 100644
--- a/common/terrain.c
+++ b/common/terrain.c
@@ -312,15 +312,16 @@ struct extra_type *resource_extra_get(const struct resource_type *presource)
 /**********************************************************************//**
   Returns TRUE iff any cardinally adjacent tile contains the given terrain.
 **************************************************************************/
-bool is_terrain_card_near(const struct tile *ptile,
-			  const struct terrain *pterrain,
+bool is_terrain_card_near(const struct civ_map *nmap,
+                          const struct tile *ptile,
+                          const struct terrain *pterrain,
                           bool check_self)
 {
-  if (!pterrain) {
+  if (pterrain == NULL) {
     return FALSE;
   }
 
-  cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  cardinal_adjc_iterate(nmap, ptile, adjc_tile) {
     if (tile_terrain(adjc_tile) == pterrain) {
       return TRUE;
     }
@@ -332,15 +333,16 @@ bool is_terrain_card_near(const struct tile *ptile,
 /**********************************************************************//**
   Returns TRUE iff any adjacent tile contains the given terrain.
 **************************************************************************/
-bool is_terrain_near_tile(const struct tile *ptile,
-			  const struct terrain *pterrain,
+bool is_terrain_near_tile(const struct civ_map *nmap,
+                          const struct tile *ptile,
+                          const struct terrain *pterrain,
                           bool check_self)
 {
-  if (!pterrain) {
+  if (pterrain == NULL) {
     return FALSE;
   }
 
-  adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  adjc_iterate(nmap, ptile, adjc_tile) {
     if (tile_terrain(adjc_tile) == pterrain) {
       return TRUE;
     }
@@ -352,13 +354,14 @@ bool is_terrain_near_tile(const struct tile *ptile,
 /**********************************************************************//**
   Return the number of adjacent tiles that have the given terrain property.
 **************************************************************************/
-int count_terrain_property_near_tile(const struct tile *ptile,
+int count_terrain_property_near_tile(const struct civ_map *nmap,
+                                     const struct tile *ptile,
                                      bool cardinal_only, bool percentage,
                                      enum mapgen_terrain_property prop)
 {
   int count = 0, total = 0;
 
-  variable_adjc_iterate(&(wld.map), ptile, adjc_tile, cardinal_only) {
+  variable_adjc_iterate(nmap, ptile, adjc_tile, cardinal_only) {
     struct terrain *pterrain = tile_terrain(adjc_tile);
 
     if (pterrain->property[prop] > 0) {
@@ -377,15 +380,16 @@ int count_terrain_property_near_tile(const struct tile *ptile,
 /**********************************************************************//**
   Returns TRUE iff any cardinally adjacent tile contains the given resource.
 **************************************************************************/
-bool is_resource_card_near(const struct tile *ptile,
+bool is_resource_card_near(const struct civ_map *nmap,
+                           const struct tile *ptile,
                            const struct extra_type *pres,
                            bool check_self)
 {
-  if (!pres) {
+  if (pres == NULL) {
     return FALSE;
   }
 
-  cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  cardinal_adjc_iterate(nmap, ptile, adjc_tile) {
     if (tile_resource(adjc_tile) == pres) {
       return TRUE;
     }
@@ -397,15 +401,16 @@ bool is_resource_card_near(const struct tile *ptile,
 /**********************************************************************//**
   Returns TRUE iff any adjacent tile contains the given resource.
 **************************************************************************/
-bool is_resource_near_tile(const struct tile *ptile,
+bool is_resource_near_tile(const struct civ_map *nmap,
+                           const struct tile *ptile,
                            const struct extra_type *pres,
                            bool check_self)
 {
-  if (!pres) {
+  if (pres == NULL) {
     return FALSE;
   }
 
-  adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  adjc_iterate(nmap, ptile, adjc_tile) {
     if (tile_resource(adjc_tile) == pres) {
       return TRUE;
     }
@@ -418,14 +423,15 @@ bool is_resource_near_tile(const struct tile *ptile,
   Returns TRUE iff any cardinally adjacent tile contains terrain with the
   given flag (does not check ptile itself).
 **************************************************************************/
-bool is_terrain_flag_card_near(const struct tile *ptile,
+bool is_terrain_flag_card_near(const struct civ_map *nmap,
+                               const struct tile *ptile,
                                enum terrain_flag_id flag)
 {
-  cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  cardinal_adjc_iterate(nmap, ptile, adjc_tile) {
     struct terrain* pterrain = tile_terrain(adjc_tile);
 
     if (T_UNKNOWN != pterrain
-	&& terrain_has_flag(pterrain, flag)) {
+        && terrain_has_flag(pterrain, flag)) {
       return TRUE;
     }
   } cardinal_adjc_iterate_end;
@@ -437,14 +443,15 @@ bool is_terrain_flag_card_near(const struct tile *ptile,
   Returns TRUE iff any adjacent tile contains terrain with the given flag
   (does not check ptile itself).
 **************************************************************************/
-bool is_terrain_flag_near_tile(const struct tile *ptile,
+bool is_terrain_flag_near_tile(const struct civ_map *nmap,
+                               const struct tile *ptile,
                                enum terrain_flag_id flag)
 {
-  adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  adjc_iterate(nmap, ptile, adjc_tile) {
     struct terrain* pterrain = tile_terrain(adjc_tile);
 
     if (T_UNKNOWN != pterrain
-	&& terrain_has_flag(pterrain, flag)) {
+        && terrain_has_flag(pterrain, flag)) {
       return TRUE;
     }
   } adjc_iterate_end;
@@ -456,17 +463,18 @@ bool is_terrain_flag_near_tile(const struct tile *ptile,
   Return the number of adjacent tiles that have terrain with the given flag
   (not including ptile itself).
 **************************************************************************/
-int count_terrain_flag_near_tile(const struct tile *ptile,
+int count_terrain_flag_near_tile(const struct civ_map *nmap,
+                                 const struct tile *ptile,
                                  bool cardinal_only, bool percentage,
                                  enum terrain_flag_id flag)
 {
   int count = 0, total = 0;
 
-  variable_adjc_iterate(&(wld.map), ptile, adjc_tile, cardinal_only) {
+  variable_adjc_iterate(nmap, ptile, adjc_tile, cardinal_only) {
     struct terrain *pterrain = tile_terrain(adjc_tile);
 
     if (T_UNKNOWN != pterrain
-	&& terrain_has_flag(pterrain, flag)) {
+        && terrain_has_flag(pterrain, flag)) {
       count++;
     }
     total++;
@@ -568,10 +576,11 @@ enum terrain_class terrain_type_terrain_class(const struct terrain *pterrain)
   Is there terrain of the given class cardinally near tile?
   (Does not check ptile itself.)
 **************************************************************************/
-bool is_terrain_class_card_near(const struct tile *ptile,
+bool is_terrain_class_card_near(const struct civ_map *nmap,
+                                const struct tile *ptile,
                                 enum terrain_class tclass)
 {
-  cardinal_adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  cardinal_adjc_iterate(nmap, ptile, adjc_tile) {
     struct terrain* pterrain = tile_terrain(adjc_tile);
 
     if (pterrain != T_UNKNOWN) {
@@ -588,10 +597,11 @@ bool is_terrain_class_card_near(const struct tile *ptile,
   Is there terrain of the given class near tile?
   (Does not check ptile itself.)
 **************************************************************************/
-bool is_terrain_class_near_tile(const struct tile *ptile,
+bool is_terrain_class_near_tile(const struct civ_map *nmap,
+                                const struct tile *ptile,
                                 enum terrain_class tclass)
 {
-  adjc_iterate(&(wld.map), ptile, adjc_tile) {
+  adjc_iterate(nmap, ptile, adjc_tile) {
     struct terrain* pterrain = tile_terrain(adjc_tile);
 
     if (pterrain != T_UNKNOWN) {
@@ -608,13 +618,14 @@ bool is_terrain_class_near_tile(const struct tile *ptile,
   Return the number of adjacent tiles that have given terrain class
   (not including ptile itself).
 **************************************************************************/
-int count_terrain_class_near_tile(const struct tile *ptile,
+int count_terrain_class_near_tile(const struct civ_map *nmap,
+                                  const struct tile *ptile,
                                   bool cardinal_only, bool percentage,
                                   enum terrain_class tclass)
 {
   int count = 0, total = 0;
 
-  variable_adjc_iterate(&(wld.map), ptile, adjc_tile, cardinal_only) {
+  variable_adjc_iterate(nmap, ptile, adjc_tile, cardinal_only) {
     struct terrain *pterrain = tile_terrain(adjc_tile);
 
     if (T_UNKNOWN != pterrain
diff --git a/common/terrain.h b/common/terrain.h
index 565eb6ea1a..e183b9ccb5 100644
--- a/common/terrain.h
+++ b/common/terrain.h
@@ -268,11 +268,14 @@ const char *terrain_name_translation(const struct terrain *pterrain);
 /* Functions to operate on a terrain flag. */
 #define terrain_has_flag(terr, flag) BV_ISSET((terr)->flags, flag)
 
-bool is_terrain_flag_card_near(const struct tile *ptile,
+bool is_terrain_flag_card_near(const struct civ_map *nmap,
+                               const struct tile *ptile,
 			       enum terrain_flag_id flag);
-bool is_terrain_flag_near_tile(const struct tile *ptile,
+bool is_terrain_flag_near_tile(const struct civ_map *nmap,
+                               const struct tile *ptile,
 			       enum terrain_flag_id flag);
-int count_terrain_flag_near_tile(const struct tile *ptile,
+int count_terrain_flag_near_tile(const struct civ_map *nmap,
+                                 const struct tile *ptile,
 				 bool cardinal_only, bool percentage,
 				 enum terrain_flag_id flag);
 void user_terrain_flags_init(void);
@@ -290,20 +293,25 @@ bool terrain_has_resource(const struct terrain *pterrain,
                           const struct extra_type *presource);
 
 /* Functions to operate on a general terrain type. */
-bool is_terrain_card_near(const struct tile *ptile,
+bool is_terrain_card_near(const struct civ_map *nmap,
+                          const struct tile *ptile,
 			  const struct terrain *pterrain,
                           bool check_self);
-bool is_terrain_near_tile(const struct tile *ptile,
+bool is_terrain_near_tile(const struct civ_map *nmap,
+                          const struct tile *ptile,
 			  const struct terrain *pterrain,
                           bool check_self);
-int count_terrain_property_near_tile(const struct tile *ptile,
+int count_terrain_property_near_tile(const struct civ_map *nmap,
+                                     const struct tile *ptile,
                                      bool cardinal_only, bool percentage,
                                      enum mapgen_terrain_property prop);
 
-bool is_resource_card_near(const struct tile *ptile,
+bool is_resource_card_near(const struct civ_map *nmap,
+                           const struct tile *ptile,
                            const struct extra_type *pres,
                            bool check_self);
-bool is_resource_near_tile(const struct tile *ptile,
+bool is_resource_near_tile(const struct civ_map *nmap,
+                           const struct tile *ptile,
                            const struct extra_type *pres,
                            bool check_self);
 
@@ -327,9 +335,12 @@ int terrain_extra_removal_time(const struct terrain *pterrain,
 const char *terrain_class_name_translation(enum terrain_class tclass);
 
 enum terrain_class terrain_type_terrain_class(const struct terrain *pterrain);
-bool is_terrain_class_card_near(const struct tile *ptile, enum terrain_class tclass);
-bool is_terrain_class_near_tile(const struct tile *ptile, enum terrain_class tclass);
-int count_terrain_class_near_tile(const struct tile *ptile,
+bool is_terrain_class_card_near(const struct civ_map *nmap,
+                                const struct tile *ptile, enum terrain_class tclass);
+bool is_terrain_class_near_tile(const struct civ_map *nmap,
+                                  const struct tile *ptile, enum terrain_class tclass);
+int count_terrain_class_near_tile(const struct civ_map *nmap,
+                                  const struct tile *ptile,
                                   bool cardinal_only, bool percentage,
                                   enum terrain_class tclass);
 
diff --git a/server/advisors/advbuilding.c b/server/advisors/advbuilding.c
index 98c3cc342c..a6b9c77a71 100644
--- a/server/advisors/advbuilding.c
+++ b/server/advisors/advbuilding.c
@@ -226,7 +226,7 @@ void building_advisor(struct player *pplayer)
         }
       }
 
-      if (is_terrain_class_near_tile(pcity->tile, TC_OCEAN)) {
+      if (is_terrain_class_near_tile(&(wld.map), pcity->tile, TC_OCEAN)) {
         value /= 2;
       }
 
diff --git a/server/citytools.c b/server/citytools.c
index fccbfc2dc2..2a7812da43 100644
--- a/server/citytools.c
+++ b/server/citytools.c
@@ -289,7 +289,7 @@ static int evaluate_city_name_priority(struct tile *ptile,
   terrain_type_iterate(pterrain) {
     /* Now we do the same for every available terrain. */
     goodness = nation_city_terrain_preference(pncity, pterrain);
-    if (!is_terrain_near_tile(ptile, pterrain, TRUE)) {
+    if (!is_terrain_near_tile(&(wld.map), ptile, pterrain, TRUE)) {
       goodness = nation_city_preference_revert(goodness);
     }
     switch (goodness) {
@@ -895,7 +895,8 @@ struct city *find_closest_city(const struct tile *ptile,
        * - (if required) only cities native to the class */
       if ((best_dist == -1 || city_dist < best_dist)
           && (!only_continent || con == tile_continent(pcity->tile))
-          && (!only_ocean || is_terrain_class_near_tile(city_tile(pcity), TC_OCEAN))
+          && (!only_ocean || is_terrain_class_near_tile(&(wld.map),
+                                                        city_tile(pcity), TC_OCEAN))
           && (!only_known
               || (map_is_known(city_tile(pcity), pplayer)
                   && map_get_player_site(city_tile(pcity), pplayer)->identity
diff --git a/server/generator/fracture_map.c b/server/generator/fracture_map.c
index 12db71111b..868edd5426 100644
--- a/server/generator/fracture_map.c
+++ b/server/generator/fracture_map.c
@@ -297,7 +297,7 @@ void make_fracture_relief(void)
   int total_mtns;
   int iter;
 
-  /* compute the land area */
+  /* Compute the land area */
   landarea = 0;
   whole_map_iterate(&(wld.map), ptile) {
     if (hmap(ptile) > hmap_shore_level) {
@@ -310,15 +310,15 @@ void make_fracture_relief(void)
   */
   total_mtns = 0;
   whole_map_iterate(&(wld.map), ptile) {
-    if (not_placed(ptile) && hmap(ptile) > hmap_shore_level) {  /* place on land only */
-      /* mountains */
+    if (not_placed(ptile) && hmap(ptile) > hmap_shore_level) {  /* Place on land only */
+      /* Mountains */
       choose_mountain = (hmap(ptile) > local_ave_elevation(ptile) * 1.20)
         || (area_is_too_flat(ptile, hmap_mountain_level, hmap(ptile)) && (fc_rand(10) < 4));
 
       choose_hill = (hmap(ptile) > local_ave_elevation(ptile) * 1.10)
         || (area_is_too_flat(ptile, hmap_mountain_level, hmap(ptile)) && (fc_rand(10) < 4));
       /* The following avoids hills and mountains directly along the coast. */
-      if (count_terrain_class_near_tile(ptile, TRUE, TRUE, TC_OCEAN) > 0) {
+      if (count_terrain_class_near_tile(&(wld.map), ptile, TRUE, TRUE, TC_OCEAN) > 0) {
         choose_mountain = FALSE;
         choose_hill = FALSE;
       }
diff --git a/server/generator/mapgen.c b/server/generator/mapgen.c
index fb64c0a93e..bd3938224d 100644
--- a/server/generator/mapgen.c
+++ b/server/generator/mapgen.c
@@ -172,7 +172,8 @@ static int river_pct = 0;
 #define map_pos_is_dry(ptile)						\
   (map_colatitude((ptile)) <= DRY_MAX_LEVEL				\
    && map_colatitude((ptile)) > DRY_MIN_LEVEL				\
-   && count_terrain_class_near_tile((ptile), FALSE, TRUE, TC_OCEAN) <= 35)
+   && count_terrain_class_near_tile(&(wld.map), (ptile),                \
+                                    FALSE, TRUE, TC_OCEAN) <= 35)
 typedef enum { WC_ALL = 200, WC_DRY, WC_NDRY } wetness_c;
 
 /* MISCELANEOUS (OTHER CONDITIONS) */
@@ -594,7 +595,8 @@ static int river_test_adjacent_ocean(struct river_map *privermap,
                                      struct tile *ptile,
                                      struct extra_type *priver)
 {
-  return 100 - count_terrain_class_near_tile(ptile, TRUE, TRUE, TC_OCEAN);
+  return 100 - count_terrain_class_near_tile(&(wld.map), ptile,
+                                             TRUE, TRUE, TC_OCEAN);
 }
 
 /**********************************************************************//**
@@ -803,7 +805,8 @@ static bool make_river(struct river_map *privermap, struct tile *ptile,
     /* Test if the river is done. */
     /* We arbitrarily make rivers end at the poles. */
     if (count_river_near_tile(&(wld.map), ptile, priver) > 0
-        || count_terrain_class_near_tile(ptile, TRUE, TRUE, TC_OCEAN) > 0
+        || count_terrain_class_near_tile(&(wld.map), ptile,
+                                         TRUE, TRUE, TC_OCEAN) > 0
         || (tile_terrain(ptile)->property[MG_FROZEN] > 0
             && map_colatitude(ptile) < 0.8 * COLD_LEVEL)) {
 
@@ -953,17 +956,18 @@ static void make_rivers(void)
 	/* Don't start a river on river. */
 	&& !tile_has_river(ptile)
 
-	/* Don't start a river on a tile is surrounded by > 1 river +
-	   ocean tile. */
-	&& (count_river_near_tile(&(wld.map), ptile, NULL)
-	    + count_terrain_class_near_tile(ptile, TRUE, FALSE, TC_OCEAN) <= 1)
+        /* Don't start a river on a tile is surrounded by > 1 river +
+           ocean tile. */
+        && (count_river_near_tile(&(wld.map), ptile, NULL)
+            + count_terrain_class_near_tile(&(wld.map), ptile,
+                                            TRUE, FALSE, TC_OCEAN) <= 1)
 
-	/* Don't start a river on a tile that is surrounded by hills or
-	   mountains unless it is hard to find somewhere else to start
-	   it. */
-	&& (count_terrain_property_near_tile(ptile, TRUE, TRUE,
-					     MG_MOUNTAINOUS) < 90
-	    || iteration_counter >= RIVERS_MAXTRIES / 10 * 5)
+        /* Don't start a river on a tile that is surrounded by hills or
+           mountains unless it is hard to find somewhere else to start
+           it. */
+        && (count_terrain_property_near_tile(&(wld.map), ptile, TRUE, TRUE,
+                                             MG_MOUNTAINOUS) < 90
+            || iteration_counter >= RIVERS_MAXTRIES / 10 * 5)
 
 	/* Don't start a river on hills unless it is hard to find
 	   somewhere else to start it. */
@@ -1700,12 +1704,12 @@ static void fill_island(int coast, long int *bucket,
     struct terrain *pterrain = pick_terrain(ptersel->target, ptersel->prefer,
                                             ptersel->avoid);
 
-    /* the first condition helps make terrain more contiguous,
+    /* The first condition helps make terrain more contiguous,
        the second lets it avoid the coast: */
     if ((i * 3 > k * 2
          || fc_rand(100) < 50
-         || is_terrain_near_tile(ptile, pterrain, FALSE))
-        && (!is_terrain_class_card_near(ptile, TC_OCEAN)
+         || is_terrain_near_tile(&(wld.map), ptile, pterrain, FALSE))
+        && (!is_terrain_class_card_near(&(wld.map), ptile, TC_OCEAN)
             || fc_rand(100) < coast)) {
       tile_set_terrain(ptile, pterrain);
       map_set_placed(ptile);
@@ -1728,9 +1732,11 @@ static bool island_river_mouth_suitability(const struct tile *ptile,
 {
   int num_card_ocean, pct_adj_ocean, num_adj_river;
 
-  num_card_ocean = count_terrain_class_near_tile(ptile, C_CARDINAL, C_NUMBER,
+  num_card_ocean = count_terrain_class_near_tile(&(wld.map), ptile,
+                                                 C_CARDINAL, C_NUMBER,
                                                  TC_OCEAN);
-  pct_adj_ocean = count_terrain_class_near_tile(ptile, C_ADJACENT, C_PERCENT,
+  pct_adj_ocean = count_terrain_class_near_tile(&(wld.map), ptile,
+                                                C_ADJACENT, C_PERCENT,
                                                 TC_OCEAN);
   num_adj_river = count_river_type_tile_card(&(wld.map), ptile, priver, FALSE);
 
@@ -1748,9 +1754,11 @@ static bool island_river_suitability(const struct tile *ptile,
   int pct_adj_ocean, num_card_ocean, pct_adj_river, num_card_river;
 
   num_card_river = count_river_type_tile_card(&(wld.map), ptile, priver, FALSE);
-  num_card_ocean = count_terrain_class_near_tile(ptile, C_CARDINAL, C_NUMBER,
+  num_card_ocean = count_terrain_class_near_tile(&(wld.map), ptile,
+                                                 C_CARDINAL, C_NUMBER,
                                                  TC_OCEAN);
-  pct_adj_ocean = count_terrain_class_near_tile(ptile, C_ADJACENT, C_PERCENT,
+  pct_adj_ocean = count_terrain_class_near_tile(&(wld.map), ptile,
+                                                C_ADJACENT, C_PERCENT,
                                                 TC_OCEAN);
   pct_adj_river = count_river_type_near_tile(&(wld.map), ptile, priver, TRUE);
 
@@ -1782,7 +1790,7 @@ static void fill_island_rivers(int coast, long int *bucket,
   i++;
   *bucket -= i * capac;
 
-  /* generate 75% more rivers than generator 1 */
+  /* Generate 75% more rivers than generator 1 */
   i = (i * 175) / 100;
 
   k = i;
diff --git a/server/generator/temperature_map.c b/server/generator/temperature_map.c
index acdbcd4cc3..345bf15fd4 100644
--- a/server/generator/temperature_map.c
+++ b/server/generator/temperature_map.c
@@ -136,7 +136,7 @@ void create_tmap(bool real)
       /* high land can be 30% cooler */
       float height = - 0.3 * MAX(0, hmap(ptile) - hmap_shore_level)
           / (hmap_max_level - hmap_shore_level);
-      int tcn = count_terrain_class_near_tile(ptile, FALSE, TRUE, TC_OCEAN);
+      int tcn = count_terrain_class_near_tile(&(wld.map), ptile, FALSE, TRUE, TC_OCEAN);
       /* near ocean temperature can be 15% more "temperate" */
       float temperate = (0.15 * (wld.map.server.temperature / 100 - t
                                  / MAX_COLATITUDE)
diff --git a/server/maphand.c b/server/maphand.c
index e8dabf4c70..dba5133e32 100644
--- a/server/maphand.c
+++ b/server/maphand.c
@@ -92,7 +92,7 @@ static bool is_claimable_ocean(struct tile *ptile, struct tile *source,
 **************************************************************************/
 static bool is_terrain_ecologically_wet(struct tile *ptile)
 {
-  return (is_terrain_class_near_tile(ptile, TC_OCEAN)
+  return (is_terrain_class_near_tile(&(wld.map), ptile, TC_OCEAN)
           || tile_has_river(ptile)
           || count_river_near_tile(&(wld.map), ptile, NULL) > 0);
 }
-- 
2.43.0

