From 58a1826aeba5b7aa1b67aaed1a80b1e662f00bb7 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 22 Mar 2024 02:09:48 +0200
Subject: [PATCH 35/35] Replace direct wld.map.interate_outwards_indices access
 by macros

See RM #334

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/map.h                         |  8 ++++----
 common/scriptcore/api_game_methods.c | 15 ++++++++-------
 common/world_object.h                |  2 ++
 server/generator/mapgen.c            | 16 ++++++++--------
 4 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/common/map.h b/common/map.h
index 3af2a11f64..3acae9e6e1 100644
--- a/common/map.h
+++ b/common/map.h
@@ -331,13 +331,13 @@ extern struct terrain_misc terrain_control;
   int _tile##_index = 0;                                                    \
   index_to_map_pos(&_start##_x, &_start##_y, tile_index(_tile##_start));    \
   for (;                                                                    \
-       _tile##_index < wld.map.num_iterate_outwards_indices;                \
+       _tile##_index < MAP_NUM_ITERATE_OUTWARDS_INDICES;                    \
        _tile##_index++) {                                                   \
-    if (wld.map.iterate_outwards_indices[_tile##_index].dist > _tile##_max) {   \
+    if (MAP_ITERATE_OUTWARDS_INDICES[_tile##_index].dist > _tile##_max) {   \
       break;                                                                \
     }                                                                       \
-    _x = wld.map.iterate_outwards_indices[_tile##_index].dx;                \
-    _y = wld.map.iterate_outwards_indices[_tile##_index].dy;                \
+    _x = MAP_ITERATE_OUTWARDS_INDICES[_tile##_index].dx;                    \
+    _y = MAP_ITERATE_OUTWARDS_INDICES[_tile##_index].dy;                    \
     _tile##_x = _x + _start##_x;                                            \
     _tile##_y = _y + _start##_y;                                            \
     _tile = map_pos_to_tile(nmap, _tile##_x, _tile##_y);                    \
diff --git a/common/scriptcore/api_game_methods.c b/common/scriptcore/api_game_methods.c
index 4961b77206..7db0af8152 100644
--- a/common/scriptcore/api_game_methods.c
+++ b/common/scriptcore/api_game_methods.c
@@ -1284,14 +1284,15 @@ int api_methods_private_tile_next_outward_index(lua_State *L, Tile *pstart,
   index_to_map_pos(&startx, &starty, tile_index(pstart));
 
   tindex++;
-  while (tindex < wld.map.num_iterate_outwards_indices) {
-    if (wld.map.iterate_outwards_indices[tindex].dist > max_dist) {
+  while (tindex < MAP_NUM_ITERATE_OUTWARDS_INDICES) {
+    if (MAP_ITERATE_OUTWARDS_INDICES[tindex].dist > max_dist) {
       return -1;
     }
-    dx = wld.map.iterate_outwards_indices[tindex].dx;
-    dy = wld.map.iterate_outwards_indices[tindex].dy;
+    dx = MAP_ITERATE_OUTWARDS_INDICES[tindex].dx;
+    dy = MAP_ITERATE_OUTWARDS_INDICES[tindex].dy;
     newx = dx + startx;
     newy = dy + starty;
+
     if (!normalize_map_pos(&(wld.map), &newx, &newy)) {
       tindex++;
       continue;
@@ -1314,12 +1315,12 @@ Tile *api_methods_private_tile_for_outward_index(lua_State *L,
   LUASCRIPT_CHECK_STATE(L, NULL);
   LUASCRIPT_CHECK_SELF(L, pstart, NULL);
   LUASCRIPT_CHECK_ARG(L,
-                      tindex >= 0 && tindex < wld.map.num_iterate_outwards_indices,
+                      tindex >= 0 && tindex < MAP_NUM_ITERATE_OUTWARDS_INDICES,
                       3, "index out of bounds", NULL);
 
   index_to_map_pos(&newx, &newy, tile_index(pstart));
-  newx += wld.map.iterate_outwards_indices[tindex].dx;
-  newy += wld.map.iterate_outwards_indices[tindex].dy;
+  newx += MAP_ITERATE_OUTWARDS_INDICES[tindex].dx;
+  newy += MAP_ITERATE_OUTWARDS_INDICES[tindex].dy;
 
   if (!normalize_map_pos(&(wld.map), &newx, &newy)) {
     return NULL;
diff --git a/common/world_object.h b/common/world_object.h
index f02d3e1bad..a7d4ac590a 100644
--- a/common/world_object.h
+++ b/common/world_object.h
@@ -63,6 +63,8 @@ extern struct world wld; /* In game.c */
 #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
+#define MAP_ITERATE_OUTWARDS_INDICES wld.map.iterate_outwards_indices
+#define MAP_NUM_ITERATE_OUTWARDS_INDICES wld.map.num_iterate_outwards_indices
 
 #ifdef __cplusplus
 }
diff --git a/server/generator/mapgen.c b/server/generator/mapgen.c
index 7e46e5bf86..a822fc94ce 100644
--- a/server/generator/mapgen.c
+++ b/server/generator/mapgen.c
@@ -2940,8 +2940,8 @@ fair_map_place_island_team(struct fair_tile *ptarget, int tx, int ty,
 
   /* Iterate positions, beginning by a random index of the outwards
    * indices. */
-  for (i = fc_rand(wld.map.num_iterate_outwards_indices / 200);
-       i < wld.map.num_iterate_outwards_indices; i++) {
+  for (i = fc_rand(MAP_NUM_ITERATE_OUTWARDS_INDICES / 200);
+       i < MAP_NUM_ITERATE_OUTWARDS_INDICES; i++) {
     x = tx + outwards_indices[i].dx;
     y = ty + outwards_indices[i].dy;
     if (normalize_map_pos(&(wld.map), &x, &y)
@@ -3537,13 +3537,13 @@ static bool map_generate_fair_islands(void)
     if (wld.map.server.team_placement != TEAM_PLACEMENT_DISABLED
         && team_players_num > 0) {
       /* Do team placement. */
-      struct iter_index outwards_indices[wld.map.num_iterate_outwards_indices];
+      struct iter_index outwards_indices[MAP_NUM_ITERATE_OUTWARDS_INDICES];
       int start_x[teams_num], start_y[teams_num];
       int dx = 0, dy = 0;
       int j, k;
 
       /* Build outwards_indices. */
-      memcpy(outwards_indices, wld.map.iterate_outwards_indices,
+      memcpy(outwards_indices, MAP_ITERATE_OUTWARDS_INDICES,
              sizeof(outwards_indices));
       switch (wld.map.server.team_placement) {
       case TEAM_PLACEMENT_DISABLED:
@@ -3551,21 +3551,21 @@ static bool map_generate_fair_islands(void)
         break;
       case TEAM_PLACEMENT_CLOSEST:
       case TEAM_PLACEMENT_CONTINENT:
-        for (j = 0; j < wld.map.num_iterate_outwards_indices; j++) {
+        for (j = 0; j < MAP_NUM_ITERATE_OUTWARDS_INDICES; j++) {
           /* We want square distances for comparing. */
           outwards_indices[j].dist =
               map_vector_to_sq_distance(outwards_indices[j].dx,
                                         outwards_indices[j].dy);
         }
-        qsort(outwards_indices, wld.map.num_iterate_outwards_indices,
+        qsort(outwards_indices, MAP_NUM_ITERATE_OUTWARDS_INDICES,
               sizeof(outwards_indices[0]), fair_team_placement_closest);
         break;
       case TEAM_PLACEMENT_HORIZONTAL:
-        qsort(outwards_indices, wld.map.num_iterate_outwards_indices,
+        qsort(outwards_indices, MAP_NUM_ITERATE_OUTWARDS_INDICES,
               sizeof(outwards_indices[0]), fair_team_placement_horizontal);
         break;
       case TEAM_PLACEMENT_VERTICAL:
-        qsort(outwards_indices, wld.map.num_iterate_outwards_indices,
+        qsort(outwards_indices, MAP_NUM_ITERATE_OUTWARDS_INDICES,
               sizeof(outwards_indices[0]), fair_team_placement_vertical);
         break;
       }
-- 
2.43.0

