From 63158fc2cb446a0f6081665f04baff506d93ed55 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 8 Jun 2024 20:14:33 +0300
Subject: [PATCH 54/54] map_init_topology(): Operate on any map

Take map as parameter, instead of assuming main map.

Requested by Alina Lenk

See RM #707

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 client/packhand.c                  |  2 +-
 common/map.c                       | 20 ++++++++++----------
 common/map.h                       |  2 +-
 server/generator/mapgen_topology.c | 10 +++++-----
 server/savegame/savegame2.c        |  2 +-
 server/savegame/savegame3.c        |  2 +-
 6 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/client/packhand.c b/client/packhand.c
index 879f2b77e9..1a6f847a96 100644
--- a/client/packhand.c
+++ b/client/packhand.c
@@ -2259,7 +2259,7 @@ void handle_map_info(const struct packet_map_info *packet)
   wld.map.topology_id = packet->topology_id;
   wld.map.wrap_id = packet->wrap_id;
 
-  map_init_topology();
+  map_init_topology(&(wld.map));
   main_map_allocate();
   client_player_maps_reset();
   init_client_goto();
diff --git a/common/map.c b/common/map.c
index 963faf509e..664b2b66ca 100644
--- a/common/map.c
+++ b/common/map.c
@@ -164,7 +164,7 @@ void map_init(struct civ_map *imap, bool server_side)
   imap->startpos_table = nullptr;
   imap->iterate_outwards_indices = nullptr;
 
-  /* The [xy]size values are set in map_init_topology. It is initialized
+  /* The [xy]size values are set in map_init_topology(). It is initialized
    * to a non-zero value because some places erroneously use these values
    * before they're initialized. */
   imap->xsize = MAP_DEFAULT_LINEAR_SIZE;
@@ -312,7 +312,7 @@ static void generate_map_indices(void)
   This is done by the map generator code (server), when loading a savegame
   or a scenario with map (server), and packhand code (client).
 ***********************************************************************/
-void map_init_topology(void)
+void map_init_topology(struct civ_map *nmap)
 {
   enum direction8 dir;
 
@@ -328,7 +328,7 @@ void map_init_topology(void)
   fc_assert(map_num_tiles() >= MAP_MIN_SIZE * 1000);
   fc_assert(map_num_tiles() <= MAP_MAX_SIZE * 1000);
 
-  wld.map.num_valid_dirs = wld.map.num_cardinal_dirs = 0;
+  nmap->num_valid_dirs = nmap->num_cardinal_dirs = 0;
 
   /* Values for direction8_invalid() */
   fc_assert(direction8_invalid() == 8);
@@ -338,23 +338,23 @@ void map_init_topology(void)
   /* Values for actual directions */
   for (dir = 0; dir < 8; dir++) {
     if (is_valid_dir_calculate(dir)) {
-      wld.map.valid_dirs[wld.map.num_valid_dirs] = dir;
-      wld.map.num_valid_dirs++;
+      nmap->valid_dirs[nmap->num_valid_dirs] = dir;
+      nmap->num_valid_dirs++;
       dir_validity[dir] = TRUE;
     } else {
       dir_validity[dir] = FALSE;
     }
     if (is_cardinal_dir_calculate(dir)) {
-      wld.map.cardinal_dirs[wld.map.num_cardinal_dirs] = dir;
-      wld.map.num_cardinal_dirs++;
+      nmap->cardinal_dirs[nmap->num_cardinal_dirs] = dir;
+      nmap->num_cardinal_dirs++;
       dir_cardinality[dir] = TRUE;
     } else {
       dir_cardinality[dir] = FALSE;
     }
   }
-  fc_assert(wld.map.num_valid_dirs > 0 && wld.map.num_valid_dirs <= 8);
-  fc_assert(wld.map.num_cardinal_dirs > 0
-            && wld.map.num_cardinal_dirs <= wld.map.num_valid_dirs);
+  fc_assert(nmap->num_valid_dirs > 0 && nmap->num_valid_dirs <= 8);
+  fc_assert(nmap->num_cardinal_dirs > 0
+            && nmap->num_cardinal_dirs <= nmap->num_valid_dirs);
 }
 
 /*******************************************************************//**
diff --git a/common/map.h b/common/map.h
index 338dbcb36f..18804f1c15 100644
--- a/common/map.h
+++ b/common/map.h
@@ -49,7 +49,7 @@ static const bool C_PERCENT = TRUE;
 
 bool map_is_empty(void);
 void map_init(struct civ_map *imap, bool server_side);
-void map_init_topology(void);
+void map_init_topology(struct civ_map *nmap);
 void map_allocate(struct civ_map *amap);
 void main_map_allocate(void);
 void map_free(struct civ_map *fmap, bool server_side);
diff --git a/server/generator/mapgen_topology.c b/server/generator/mapgen_topology.c
index 8ba29db74b..38b904abc0 100644
--- a/server/generator/mapgen_topology.c
+++ b/server/generator/mapgen_topology.c
@@ -226,20 +226,20 @@ void generator_init_topology(bool autosize)
 
   sqsize = get_sqsize();
 
-  /* initialize the ICE_BASE_LEVEL */
+  /* Initialize the ICE_BASE_LEVEL */
 
-  /* if maps has strip like poles we get smaller poles
+  /* If maps has strip like poles we get smaller poles
    * (less playables than island poles)
    *   5% for little maps
    *   2% for big ones, if map.server.temperature == 50
    * except if separate poles is set */
   if (wld.map.server.separatepoles) {
-    /* with separatepoles option strip poles are useless */
+    /* With separatepoles option strip poles are useless */
     ice_base_colatitude =
         (MAX(0, 100 * COLD_LEVEL / 3 - 1 *  MAX_COLATITUDE)
          + 1 *  MAX_COLATITUDE * sqsize) / (100 * sqsize);
   } else {
-    /* any way strip poles are not so playable as isle poles */
+    /* Any way strip poles are not so playable as isle poles */
     ice_base_colatitude =
         (MAX(0, 100 * COLD_LEVEL / 3 - 2 * MAX_COLATITUDE)
          + 2 * MAX_COLATITUDE * sqsize) / (100 * sqsize);
@@ -262,7 +262,7 @@ void generator_init_topology(bool autosize)
     wld.map.server.huts_absolute = -1;
   }
 
-  map_init_topology();
+  map_init_topology(&(wld.map));
 }
 
 /************************************************************************//**
diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c
index e752b790ca..97792a6a92 100644
--- a/server/savegame/savegame2.c
+++ b/server/savegame/savegame2.c
@@ -2261,7 +2261,7 @@ static void sg_load_map_tiles(struct loaddata *loading)
 
   /* Initialize the map for the current topology. 'map.xsize' and
    * 'map.ysize' must be set. */
-  map_init_topology();
+  map_init_topology(&(wld.map));
 
   /* Allocate map. */
   main_map_allocate();
diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c
index ee1d62cff3..00cfbaffe6 100644
--- a/server/savegame/savegame3.c
+++ b/server/savegame/savegame3.c
@@ -2922,7 +2922,7 @@ static void sg_load_map_tiles(struct loaddata *loading)
 
   /* Initialize the map for the current topology. 'map.xsize' and
    * 'map.ysize' must be set. */
-  map_init_topology();
+  map_init_topology(&(wld.map));
 
   /* Allocate map. */
   main_map_allocate();
-- 
2.43.0

