Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2001:
[Freeciv-Dev] [PATCH] map_adjust_* patch
Home

[Freeciv-Dev] [PATCH] map_adjust_* patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] [PATCH] map_adjust_* patch
From: Jason Dorje Short <jshort@xxxxxxxxxxxxx>
Date: Fri, 31 Aug 2001 02:13:02 -0400
Reply-to: jdorje@xxxxxxxxxxxxxxxxxxxxx

Again in the spirit of small, focused patches:

This patch replaces a number of uses of map_adjust_x/map_adjust_y with
the corresponding normalize_map_pos/is_real_tile call.  For the most
part it only covers the simple ones; there are some replacements that
will be much more tricky (and some that will be impossible).  I've also
tried to stay away from the instances that should be replaced by a
different macro, for instance the adjc_iterate loops.  This is only a
fraction of the total uses of map_adjust_*.

It applies to current CVS.  It ran a collection of automated games
yesterday without trouble.  Each instance of replacement (or, at a
minimum, each file) should be independent and can be applied separately
if there's any doubt.  (In fact, rather than wait while we argue about
specific instances these instances might as well just be skipped.  There
are plenty of other map_adjust_* instances for them to be lumped in with
later.)

jason
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.86
diff -u -r1.86 city.h
--- common/city.h       2001/08/26 13:10:12     1.86
+++ common/city.h       2001/08/31 06:03:14
@@ -130,10 +130,10 @@
     for (MCMI_y=0; MCMI_y<CITY_MAP_SIZE; MCMI_y++) { \
       if (! ((MCMI_x == 0 || MCMI_x == (CITY_MAP_SIZE-1)) \
             && (MCMI_y == 0 || MCMI_y == (CITY_MAP_SIZE-1))) ) { \
+       x_itr = city_x + MCMI_x - CITY_MAP_SIZE/2; \
         y_itr = city_y + MCMI_y - CITY_MAP_SIZE/2; \
-        if (y_itr < 0 || y_itr >= map.ysize) \
-         continue; \
-       x_itr = map_adjust_x(city_x + MCMI_x - CITY_MAP_SIZE/2);
+        if (!normalize_map_pos(&x_itr, &y_itr)) \
+         continue;
 
 #define map_city_radius_iterate_end \
       } \
Index: common/map.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.c,v
retrieving revision 1.82
diff -u -r1.82 map.c
--- common/map.c        2001/08/26 09:28:16     1.82
+++ common/map.c        2001/08/31 06:03:15
@@ -972,7 +972,11 @@
 
   if( ( (t1->terrain==T_RIVER) && (t2->terrain==T_RIVER) ) ||
       ( (t1->special&S_RIVER) && (t2->special&S_RIVER) ) ) {
-    cardinal_move = (y1==y2 || map_adjust_x(x1)==map_adjust_x(x2));
+    assert(is_real_tile(x1, y1) && is_real_tile(x2, y2));
+    normalize_map_pos(&x1, &y1);
+    normalize_map_pos(&x2, &y2);
+    /* FIXME: this check will not work with an orthogonal map */
+    cardinal_move = (x1 == x2) || (y1 == y2);
     switch (terrain_control.river_move_mode) {
     case RMV_NORMAL:
       break;
@@ -1007,7 +1011,7 @@
 static int tile_move_cost_ai(struct tile *tile0, struct tile *tile1,
                             int x, int y, int x1, int y1, int maxcost)
 {
-  assert(y >= 0 && y <= map.ysize);
+  assert(is_real_tile(x, y));
   assert(!is_server || (tile0->terrain != T_UNKNOWN && tile1->terrain != 
T_UNKNOWN));
 
   if (tile0->terrain == T_OCEAN) {
@@ -1045,8 +1049,10 @@
   int dir, x1, y1;
   int maxcost = 72; /* should be big enough without being TOO big */
   struct tile *tile0, *tile1;
- 
-  x = map_adjust_x(x);
+
+  assert(is_real_tile(x, y));
+  normalize_map_pos(&x, &y);
+
   tile0 = map_get_tile(x, y);
   debug_log_move_costs("Resetting move costs for", x, y, tile0);
 
@@ -1087,7 +1093,7 @@
     int x1, y1;
     tile0 = map_get_tile(x, y);
     for (dir = 0; dir < 8; dir++) {
-      x1 = map_adjust_x(x + DIR_DX[dir]);
+      x1 = x + DIR_DX[dir];
       y1 = y + DIR_DY[dir];
       if (normalize_map_pos(&x1, &y1)) {
        tile1 = map_get_tile(x1, y1);
@@ -1146,10 +1152,10 @@
 ***************************************************************/
 struct tile *map_get_tile(int x, int y)
 {
-  if(y<0 || y>=map.ysize)
+  if (!normalize_map_pos(&x, &y))
     return &void_tile; /* accurate fix by Syela */
   else
-    return map.tiles+map_adjust_x(x)+y*map.xsize;
+    return map.tiles + x + y*map.xsize;
 }
 
 /***************************************************************
@@ -1157,10 +1163,10 @@
 ***************************************************************/
 signed short map_get_continent(int x, int y)
 {
-  if (y<0 || y>=map.ysize)
+  if (!normalize_map_pos(&x, &y))
     return -1;
   else
-    return (map.tiles + map_adjust_x(x) + y * map.xsize)->continent;
+    return (map.tiles + x + y*map.xsize)->continent;
 }
 
 /***************************************************************
@@ -1169,7 +1175,8 @@
 void map_set_continent(int x, int y, int val)
 {
   assert(is_real_tile(x, y));
-  (map.tiles + map_adjust_x(x) + y * map.xsize)->continent=val;
+  normalize_map_pos(&x, &y);
+  (map.tiles + x + y * map.xsize)->continent=val;
 }
 
 
@@ -1178,10 +1185,10 @@
 ***************************************************************/
 enum tile_terrain_type map_get_terrain(int x, int y)
 {
-  if(y<0 || y>=map.ysize)
+  if (!normalize_map_pos(&x, &y))
     return T_UNKNOWN;
   else
-    return (map.tiles+map_adjust_x(x)+y*map.xsize)->terrain;
+    return (map.tiles + x + y*map.xsize)->terrain;
 }
 
 /***************************************************************
@@ -1189,10 +1196,10 @@
 ***************************************************************/
 enum tile_special_type map_get_special(int x, int y)
 {
-  if(!is_real_tile(x, y))
+  if (!normalize_map_pos(&x, &y))
     return S_NO_SPECIAL;
   else
-    return (map.tiles + map_adjust_x(x) + y*map.xsize)->special;
+    return (map.tiles + x + y * map.xsize)->special;
 }
 
 /***************************************************************
@@ -1200,8 +1207,9 @@
 ***************************************************************/
 void map_set_terrain(int x, int y, enum tile_terrain_type ter)
 {
-  (map.tiles+map_adjust_x(x)+
-   map_adjust_y(y)*map.xsize)->terrain=ter;
+  assert(is_real_tile(x, y));
+  normalize_map_pos(&x, &y);
+  (map.tiles + x + y*map.xsize)->terrain=ter;
 }
 
 /***************************************************************
@@ -1209,11 +1217,11 @@
 ***************************************************************/
 void map_set_special(int x, int y, enum tile_special_type spe)
 {
-  x = map_adjust_x(x);
-  y = map_adjust_y(y);
-  
-  (map.tiles +x + y*map.xsize)->special |= spe;
+  assert(is_real_tile(x, y));
+  normalize_map_pos(&x, &y);
 
+  (map.tiles + x + y*map.xsize)->special |= spe;
+
   if (spe & (S_ROAD | S_RAILROAD))
     reset_move_costs(x, y);
 }
@@ -1223,8 +1231,8 @@
 ***************************************************************/
 void map_clear_special(int x, int y, enum tile_special_type spe)
 {
-  x = map_adjust_x(x);
-  y = map_adjust_y(y);
+  assert(is_real_tile(x, y));
+  normalize_map_pos(&x, &y);
   (map.tiles + x + y*map.xsize)->special &= ~spe;
 
   if (spe & (S_ROAD | S_RAILROAD))
@@ -1236,7 +1244,8 @@
 ***************************************************************/
 struct city *map_get_city(int x, int y)
 {
-  x = map_adjust_x(x); y = map_adjust_y(y);
+  assert(is_real_tile(x, y));
+  normalize_map_pos(&x, &y);
   return (map.tiles + x + y*map.xsize)->city;
 }
 
@@ -1246,7 +1255,9 @@
 ***************************************************************/
 void map_set_city(int x, int y, struct city *pcity)
 {
-  (map.tiles+map_adjust_x(x)+map_adjust_y(y)*map.xsize)->city=pcity;
+  assert(is_real_tile(x, y));
+  normalize_map_pos(&x, &y);
+  (map.tiles + x + y*map.xsize)->city=pcity;
 }
 
 
@@ -1255,8 +1266,11 @@
 ***************************************************************/
 enum known_type tile_is_known(int x, int y)
 {
-  return (enum known_type) (((map.tiles+map_adjust_x(x)+
-          map_adjust_y(y)*map.xsize)->known));
+  if (!normalize_map_pos(&x, &y))
+    return TILE_UNKNOWN;
+  else
+    return (enum known_type) (((map.tiles + x +
+                               y*map.xsize)->known));
 }
 
 /***************************************************************
@@ -1265,8 +1279,10 @@
 ***************************************************************/
 int same_pos(int x1, int y1, int x2, int y2)
 {
-  return (map_adjust_x(x1) == map_adjust_x(x2)
-         && map_adjust_y(y1) == map_adjust_y(y2)); 
+  assert(is_real_tile(x1, y1) && is_real_tile(x2, y2));
+  normalize_map_pos(&x1, &y1);
+  normalize_map_pos(&x2, &y2);
+  return (x1 == x2 && y1 == y2);
 }
 
 /**************************************************************************
Index: common/map.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/map.h,v
retrieving revision 1.87
diff -u -r1.87 map.h
--- common/map.h        2001/08/26 09:28:17     1.87
+++ common/map.h        2001/08/31 06:03:15
@@ -312,17 +312,13 @@
        else                                                                  \
          ARG_x_itr = (ARG_start_x) - MACRO_dxy;                              \
       }                                                                       \
-      if (ARG_y_itr<0 || ARG_y_itr >= map.ysize)                              \
-       continue;                                                             \
       {                                                                       \
        int MACRO_dx = (ARG_start_x) - ARG_x_itr;                             \
        if (MACRO_dx > MACRO_max_dx || MACRO_dx < MACRO_min_dx)               \
          continue;                                                           \
       }                                                                       \
-      if (ARG_x_itr >= map.xsize)                                             \
-       ARG_x_itr -= map.xsize;                                               \
-      else if (ARG_x_itr < 0)                                                 \
-       ARG_x_itr += map.xsize;
+      if (!normalize_map_pos(&ARG_x_itr, &ARG_y_itr))                         \
+       continue;
 
 #define iterate_outward_end                                                   \
     }                                                                         \
@@ -342,14 +338,14 @@
 #define square_iterate(SI_center_x, SI_center_y, radius, SI_x_itr, SI_y_itr)  \
 {                                                                             \
   int SI_x_itr, SI_y_itr;                                                     \
-  int SI_x_itr1;                                                              \
-  for (SI_y_itr = (SI_center_y) - (radius);                                   \
-       SI_y_itr <= (SI_center_y) + (radius); SI_y_itr++) {                    \
-    if (SI_y_itr < 0 || SI_y_itr >= map.ysize)                                \
-      continue;                                                               \
+  int SI_x_itr1, SI_y_itr1;                                                   \
+  for (SI_y_itr1 = (SI_center_y) - (radius);                                  \
+       SI_y_itr1 <= (SI_center_y) + (radius); SI_y_itr1++) {                  \
     for (SI_x_itr1 = (SI_center_x) - (radius);                                \
         SI_x_itr1 <= (SI_center_x) + (radius); SI_x_itr1++) {                \
-      SI_x_itr = map_adjust_x(SI_x_itr1);
+      SI_x_itr = SI_x_itr1;                                                   \
+      SI_y_itr = SI_y_itr1;                                                   \
+      if (!normalize_map_pos(&SI_x_itr, &SI_y_itr)) continue;
 
 #define square_iterate_end                                                    \
     }                                                                         \
@@ -360,14 +356,15 @@
 #define adjc_iterate(RI_center_x, RI_center_y, RI_x_itr, RI_y_itr)            \
 {                                                                             \
   int RI_x_itr, RI_y_itr;                                                     \
-  int RI_x_itr1;                                                              \
-  for (RI_y_itr = RI_center_y - 1;                                            \
-       RI_y_itr <= RI_center_y + 1; RI_y_itr++) {                             \
-    if (RI_y_itr < 0 || RI_y_itr >= map.ysize)                                \
-      continue;                                                               \
+  int RI_x_itr1, RI_y_itr1;                                                   \
+  for (RI_y_itr1 = RI_center_y - 1;                                           \
+       RI_y_itr1 <= RI_center_y + 1; RI_y_itr1++) {                           \
     for (RI_x_itr1 = RI_center_x - 1;                                         \
         RI_x_itr1 <= RI_center_x + 1; RI_x_itr1++) {                         \
-      RI_x_itr = map_adjust_x(RI_x_itr1);                                     \
+      RI_x_itr = RI_x_itr1;                                                   \
+      RI_y_itr = RI_y_itr1;                                                   \
+      if (!normalize_map_pos(&RI_x_itr, &RI_y_itr))                           \
+        continue;                                                             \
       if (RI_x_itr == RI_center_x && RI_y_itr == RI_center_y)                 \
         continue; 
 
@@ -394,12 +391,8 @@
     y_itr = MACRO_center_y + DIR_DY[dir_itr];                                 \
     x_itr = MACRO_center_x + DIR_DX[dir_itr];                                 \
     if (MACRO_border) {                                                       \
-      if (y_itr < 0 || y_itr >= map.ysize)                                    \
-        continue;                                                             \
-      if (x_itr < 0)                                                          \
-        x_itr += map.xsize;                                                   \
-      else if (x_itr >= map.xsize)                                            \
-        x_itr -= map.xsize;                                                   \
+      if (!normalize_map_pos(&x_itr, &y_itr))                                  
\
+       continue;                                                             \
     }
 
 #define adjc_dir_iterate_end                                                  \
Index: server/gamehand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/gamehand.c,v
retrieving revision 1.94
diff -u -r1.94 gamehand.c
--- server/gamehand.c   2001/08/29 10:32:29     1.94
+++ server/gamehand.c   2001/08/31 06:03:16
@@ -111,7 +111,7 @@
        do {
          dx = x + myrand(2 * game.dispersion + 1) - game.dispersion;
          dy = y + myrand(2 * game.dispersion + 1) - game.dispersion;
-         dx = map_adjust_x(dx);
+         normalize_map_pos(&dx, &dy);
        } while (!(is_real_tile(dx, dy) &&
                   map_same_continent(x, y, dx, dy) &&
                   (map_get_terrain(dx, dy) != T_OCEAN) &&

[Prev in Thread] Current Thread [Next in Thread]