From a10ccf0c08e9b41d60e3f36586da61c6f3acf20f Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Tue, 2 Jan 2024 03:54:47 +0200
Subject: [PATCH 19/19] Add MAP_*_DIRS macros to world_object.h

Hide global access to wld.map.*_dirs to macros,
so that callers work independently from the implementation
detail that all maps share these values with wld.map,
and it's always consulted, instead of individual maps.

See RM #110

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/map.c                    |  2 +-
 common/terrain.c                | 26 +++++++++----------
 common/world_object.h           |  4 +++
 server/advisors/autoexplorer.c  |  2 +-
 server/generator/mapgen.c       | 44 ++++++++++++++++-----------------
 server/generator/mapgen_utils.c |  2 +-
 6 files changed, 42 insertions(+), 38 deletions(-)

diff --git a/common/map.c b/common/map.c
index 967152cb22..1b0a6ca3e4 100644
--- a/common/map.c
+++ b/common/map.c
@@ -1948,7 +1948,7 @@ struct iterator *map_startpos_iter_init(struct map_startpos_iter *iter)
 ***********************************************************************/
 enum direction8 rand_direction(void)
 {
-  return wld.map.valid_dirs[fc_rand(wld.map.num_valid_dirs)];
+  return MAP_VALID_DIRS[fc_rand(MAP_NUM_VALID_DIRS)];
 }
 
 /*******************************************************************//**
diff --git a/common/terrain.c b/common/terrain.c
index 42f1881410..21c75dc7c4 100644
--- a/common/terrain.c
+++ b/common/terrain.c
@@ -303,22 +303,22 @@ struct extra_type *resource_extra_get(const struct resource_type *presource)
   on the value of card_only.
 **************************************************************************/
 #define variable_adjc_iterate(nmap, center_tile, _tile, card_only)      \
-{									\
-  enum direction8 *_tile##_list;					\
-  int _tile##_count;							\
-									\
-  if (card_only) {							\
-    _tile##_list = wld.map.cardinal_dirs;				\
-    _tile##_count = wld.map.num_cardinal_dirs;				\
-  } else {								\
-    _tile##_list = wld.map.valid_dirs;					\
-    _tile##_count = wld.map.num_valid_dirs;				\
-  }									\
+{                                                                       \
+  enum direction8 *_tile##_list;                                        \
+  int _tile##_count;                                                    \
+                                                                        \
+  if (card_only) {                                                      \
+    _tile##_list = MAP_CARDINAL_DIRS;                                   \
+    _tile##_count = MAP_NUM_CARDINAL_DIRS;                              \
+  } else {                                                              \
+    _tile##_list = MAP_VALID_DIRS;                                      \
+    _tile##_count = MAP_NUM_VALID_DIRS;                                 \
+  }                                                                     \
   adjc_dirlist_iterate(nmap, center_tile, _tile, _tile##_dir,           \
                        _tile##_list, _tile##_count) {
 
-#define variable_adjc_iterate_end					\
-  } adjc_dirlist_iterate_end;						\
+#define variable_adjc_iterate_end                                       \
+  } adjc_dirlist_iterate_end;                                           \
 }
 
 /**********************************************************************//**
diff --git a/common/world_object.h b/common/world_object.h
index 3fd7a29d92..f02d3e1bad 100644
--- a/common/world_object.h
+++ b/common/world_object.h
@@ -59,6 +59,10 @@ extern struct world wld; /* In game.c */
 #define MAP_NATURAL_WIDTH (MAP_IS_ISOMETRIC ? 2 * wld.map.xsize : wld.map.xsize)
 #define MAP_NATURAL_HEIGHT wld.map.ysize
 
+#define MAP_CARDINAL_DIRS wld.map.cardinal_dirs
+#define MAP_NUM_CARDINAL_DIRS wld.map.num_cardinal_dirs
+#define MAP_VALID_DIRS wld.map.valid_dirs
+#define MAP_NUM_VALID_DIRS wld.map.num_valid_dirs
 
 #ifdef __cplusplus
 }
diff --git a/server/advisors/autoexplorer.c b/server/advisors/autoexplorer.c
index a40a0eddae..accff592df 100644
--- a/server/advisors/autoexplorer.c
+++ b/server/advisors/autoexplorer.c
@@ -75,7 +75,7 @@ static int likely_native(struct tile *ptile,
     }
   } adjc_dir_iterate_end;
 
-  return 50 + (50 / wld.map.num_valid_dirs * (native - foreign));
+  return 50 + (50 / MAP_NUM_VALID_DIRS * (native - foreign));
 }
 
 /**********************************************************************//**
diff --git a/server/generator/mapgen.c b/server/generator/mapgen.c
index 63f20a0e13..73d5665d85 100644
--- a/server/generator/mapgen.c
+++ b/server/generator/mapgen.c
@@ -2970,8 +2970,8 @@ static void fair_map_make_resources(struct fair_tile *pmap)
     if (pftile->flags & FTF_OCEAN) {
       bool land_around = FALSE;
 
-      for (j = 0; j < wld.map.num_valid_dirs; j++) {
-        pftile2 = fair_map_tile_step(pmap, pftile, wld.map.valid_dirs[j]);
+      for (j = 0; j < MAP_NUM_VALID_DIRS; j++) {
+        pftile2 = fair_map_tile_step(pmap, pftile, MAP_VALID_DIRS[j]);
         if (pftile2 != NULL
             && pftile2->flags & FTF_ASSIGNED
             && !(pftile2->flags & FTF_OCEAN)) {
@@ -2989,8 +2989,8 @@ static void fair_map_make_resources(struct fair_tile *pmap)
      * resource for the terrain. */
     if (pftile->presource != NULL) {
       pftile->flags |= FTF_NO_RESOURCE;
-      for (j = 0; j < wld.map.num_valid_dirs; j++) {
-        pftile2 = fair_map_tile_step(pmap, pftile, wld.map.valid_dirs[j]);
+      for (j = 0; j < MAP_NUM_VALID_DIRS; j++) {
+        pftile2 = fair_map_tile_step(pmap, pftile, MAP_VALID_DIRS[j]);
         if (pftile2 != NULL) {
           pftile2->flags |= FTF_NO_RESOURCE;
         }
@@ -3107,8 +3107,8 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
   while (i < fantasy) {
     pftile = land_tiles[fc_rand(i)];
 
-    for (j = 0; j < wld.map.num_valid_dirs; j++) {
-      pftile2 = fair_map_tile_step(pisland, pftile, wld.map.valid_dirs[j]);
+    for (j = 0; j < MAP_NUM_VALID_DIRS; j++) {
+      pftile2 = fair_map_tile_step(pisland, pftile, MAP_VALID_DIRS[j]);
       fc_assert(pftile2 != NULL);
       if (fair_map_tile_border(pisland, pftile2, sea_around_island)) {
         continue;
@@ -3125,8 +3125,8 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
   }
   while (i < size) {
     pftile = land_tiles[i - fc_rand(fantasy) - 1];
-    pftile2 = fair_map_tile_step(pisland, pftile, wld.map.cardinal_dirs
-                                     [fc_rand(wld.map.num_cardinal_dirs)]);
+    pftile2 = fair_map_tile_step(pisland, pftile,
+                                 MAP_CARDINAL_DIRS[fc_rand(MAP_NUM_CARDINAL_DIRS)]);
     fc_assert(pftile2 != NULL);
     if (fair_map_tile_border(pisland, pftile2, sea_around_island)) {
       continue;
@@ -3199,8 +3199,8 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
   if (river_type_count > 0) {
     struct extra_type *priver;
     struct fair_tile *pend;
-    int n = ((river_pct * size * wld.map.num_cardinal_dirs
-              * wld.map.num_cardinal_dirs) / 200);
+    int n = ((river_pct * size * MAP_NUM_CARDINAL_DIRS
+              * MAP_NUM_CARDINAL_DIRS) / 200);
     int length_max = 3, length, l;
     enum direction8 dir;
     int extra_idx;
@@ -3226,8 +3226,8 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
       river_around = 0;
       connectable_river_around = FALSE;
       ocean_around = FALSE;
-      for (j = 0; j < wld.map.num_valid_dirs; j++) {
-        pftile2 = fair_map_tile_step(pisland, pftile, wld.map.valid_dirs[j]);
+      for (j = 0; j < MAP_NUM_VALID_DIRS; j++) {
+        pftile2 = fair_map_tile_step(pisland, pftile, MAP_VALID_DIRS[j]);
         if (pftile2 == NULL) {
           continue;
         }
@@ -3237,7 +3237,7 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
           break;
         } else if (BV_ISSET(pftile2->extras, extra_idx)) {
           river_around++;
-          if (!cardinal_only || is_cardinal_dir(wld.map.valid_dirs[j])) {
+          if (!cardinal_only || is_cardinal_dir(MAP_VALID_DIRS[j])) {
             connectable_river_around = TRUE;
           }
         }
@@ -3261,15 +3261,15 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
       length = -1;
       dir = direction8_invalid();
       dirs_num = 0;
-      for (j = 0; j < wld.map.num_valid_dirs; j++) {
-        if (cardinal_only && !is_cardinal_dir(wld.map.valid_dirs[j])) {
+      for (j = 0; j < MAP_NUM_VALID_DIRS; j++) {
+        if (cardinal_only && !is_cardinal_dir(MAP_VALID_DIRS[j])) {
           continue;
         }
 
         finished = FALSE;
         pftile2 = pftile;
         for (l = 2; l < length_max; l++) {
-          pftile2 = fair_map_tile_step(pisland, pftile2, wld.map.valid_dirs[j]);
+          pftile2 = fair_map_tile_step(pisland, pftile2, MAP_VALID_DIRS[j]);
           if (pftile2 == NULL
               || !terrain_has_flag(pftile2->pterrain, TER_CAN_HAVE_RIVER)) {
             break;
@@ -3278,24 +3278,24 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
           river_around = 0;
           connectable_river_around = FALSE;
           ocean_around = FALSE;
-          for (k = 0; k < wld.map.num_valid_dirs; k++) {
-            if (wld.map.valid_dirs[k] == DIR_REVERSE(wld.map.valid_dirs[j])) {
+          for (k = 0; k < MAP_NUM_VALID_DIRS; k++) {
+            if (wld.map.valid_dirs[k] == DIR_REVERSE(MAP_VALID_DIRS[j])) {
               continue;
             }
 
             pftile3 = fair_map_tile_step(pisland, pftile2,
-                                         wld.map.valid_dirs[k]);
+                                         MAP_VALID_DIRS[k]);
             if (pftile3 == NULL) {
               continue;
             }
 
             if (pftile3->flags & FTF_OCEAN) {
-              if (!cardinal_only || is_cardinal_dir(wld.map.valid_dirs[k])) {
+              if (!cardinal_only || is_cardinal_dir(MAP_VALID_DIRS[k])) {
                 ocean_around = TRUE;
               }
             } else if (BV_ISSET(pftile3->extras, extra_idx)) {
               river_around++;
-              if (!cardinal_only || is_cardinal_dir(wld.map.valid_dirs[k])) {
+              if (!cardinal_only || is_cardinal_dir(MAP_VALID_DIRS[k])) {
                 connectable_river_around = TRUE;
               }
             }
@@ -3308,7 +3308,7 @@ static struct fair_tile *fair_map_island_new(int size, int startpos_num)
           }
         }
         if (finished && fc_rand(++dirs_num) == 0) {
-          dir = wld.map.valid_dirs[j];
+          dir = MAP_VALID_DIRS[j];
           pend = pftile2;
           length = l;
         }
diff --git a/server/generator/mapgen_utils.c b/server/generator/mapgen_utils.c
index 5bf8cab196..521f7e1a81 100644
--- a/server/generator/mapgen_utils.c
+++ b/server/generator/mapgen_utils.c
@@ -585,7 +585,7 @@ static int real_distance_to_land(const struct tile *ptile, int max)
 **************************************************************************/
 static struct terrain *most_adjacent_ocean_type(const struct tile *ptile)
 {
-  const int need = 2 * wld.map.num_valid_dirs / 3;
+  const int need = 2 * MAP_NUM_VALID_DIRS / 3;
   int count;
 
   terrain_type_iterate(pterrain) {
-- 
2.43.0

