Complete.Org: Mailing Lists: Archives: freeciv-dev: August 2004:
[Freeciv-Dev] (PR#9832) simplify count_xxx_near_tile interface
Home

[Freeciv-Dev] (PR#9832) simplify count_xxx_near_tile interface

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#9832) simplify count_xxx_near_tile interface
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 26 Aug 2004 23:12:43 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9832 >

Currently there are functions like

    count_xxx_near_tile(...)
    adjc_xxx_tiles4(...)

where the latter considers only cardinal tiles.  The latter is misnamed 
though, not just because it doesn't include "count" but because there 
are not necessarily 4 cardinal directions.

In this patch these two forms are merged.  This is only done in 
terrain.h.  count_special_near_tile, in map.h, is still in the old form. 
  Later I'd like to move this function into terrain.h and make the same 
change to it.

Note that it _appears_ there are not very many users of these functions. 
  However there are some other potential users out there that do their 
checks manually.  I will also be trying to hunt these down.

jason

? diff
Index: common/terrain.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.c,v
retrieving revision 1.11
diff -u -r1.11 terrain.c
--- common/terrain.c    25 Aug 2004 18:57:07 -0000      1.11
+++ common/terrain.c    27 Aug 2004 05:55:21 -0000
@@ -130,6 +130,27 @@
   } terrain_type_iterate_end;
 }
 
+#define variable_adjc_iterate(center_x, center_y, x_itr, y_itr, card_only)  \
+{                                                                          \
+  enum direction8 *dirlist;                                                \
+  int total, count = 0;                                                        
    \
+                                                                           \
+  if (cardinal_only) {                                                     \
+    dirlist = map.cardinal_dirs;                                           \
+    total = map.num_cardinal_dirs;                                         \
+  } else {                                                                 \
+    dirlist = map.valid_dirs;                                              \
+    total = map.num_valid_dirs;                                                
    \
+  }                                                                        \
+                                                                           \
+  adjc_dirlist_iterate(center_x, center_y, x_itr, y_itr,                   \
+                      _dir, dirlist, total) {
+
+#define variable_adjc_iterate_end                                          \
+  } adjc_dirlist_iterate_end;                                              \
+}
+
+
 /****************************************************************************
   Returns TRUE iff any adjacent tile contains the given terrain.
 ****************************************************************************/
@@ -147,36 +168,21 @@
 /****************************************************************************
   Return the number of adjacent tiles that have the given terrain.
 ****************************************************************************/
-int count_terrain_near_tile(int map_x, int map_y, Terrain_type_id t)
+int count_terrain_near_tile(int map_x, int map_y, bool cardinal_only,
+                           Terrain_type_id t)
 {
   int count = 0;
 
-  adjc_iterate(map_x, map_y, adjc_x, adjc_y) {
+  variable_adjc_iterate(map_x, map_y, adjc_x, adjc_y, cardinal_only) {
     if (map_get_terrain(adjc_x, adjc_y) == t) {
       count++;
     }
-  } adjc_iterate_end;
+  } variable_adjc_iterate_end;
 
   return count;
 }
 
 /****************************************************************************
-  Return the number of cardinally adjacent tiles that have the given terrain.
-****************************************************************************/
-int adjacent_terrain_tiles4(int map_x, int map_y, Terrain_type_id t)
-{
-  int num_adjacent = 0;
-
-  cardinal_adjc_iterate(map_x, map_y, adjc_x, adjc_y) {
-    if (map_get_terrain(adjc_x, adjc_y) == t) {
-      num_adjacent++;
-    }
-  } cardinal_adjc_iterate_end;
-
-  return num_adjacent;
-}
-
-/****************************************************************************
   Returns TRUE iff any adjacent tile contains terrain with the given flag.
 ****************************************************************************/
 bool is_terrain_flag_near_tile(int map_x, int map_y,
@@ -194,34 +200,16 @@
 /****************************************************************************
   Return the number of adjacent tiles that have terrain with the given flag.
 ****************************************************************************/
-int count_terrain_flag_near_tile(int map_x, int map_y,
+int count_terrain_flag_near_tile(int map_x, int map_y, bool cardinal_only,
                                 enum terrain_flag_id flag)
 {
   int count = 0;
 
-  adjc_iterate(map_x, map_y, adjc_x, adjc_y) {
+  variable_adjc_iterate(map_x, map_y, adjc_x, adjc_y, cardinal_only) {
     if (terrain_has_flag(map_get_terrain(adjc_x, adjc_y), flag)) {
       count++;
     }
-  } adjc_iterate_end;
+  } variable_adjc_iterate_end;
 
   return count;
 }
-
-/****************************************************************************
-  Return the number of cardinally adjacent tiles that have terrain with
-  the given flag.
-****************************************************************************/
-int adjacent_terrain_flag_tiles4(int map_x, int map_y,
-                                enum terrain_flag_id flag)
-{
-  int num_adjacent = 0;
-
-  cardinal_adjc_iterate(map_x, map_y, adjc_x, adjc_y) {
-    if (terrain_has_flag(map_get_terrain(adjc_x, adjc_y), flag)) {
-      num_adjacent++;
-    }
-  } cardinal_adjc_iterate_end;
-
-  return num_adjacent;
-}
Index: common/terrain.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.h,v
retrieving revision 1.20
diff -u -r1.20 terrain.h
--- common/terrain.h    25 Aug 2004 18:57:07 -0000      1.20
+++ common/terrain.h    27 Aug 2004 05:55:21 -0000
@@ -113,21 +113,19 @@
 
 /* Functions to operate on a general terrain type. */
 bool is_terrain_near_tile(int map_x, int map_y, Terrain_type_id t);
-int count_terrain_near_tile(int map_x, int map_y, Terrain_type_id t);
-int adjacent_terrain_tiles4(int map_x, int map_y, Terrain_type_id t);
+int count_terrain_near_tile(int map_x, int map_y, bool cardinal_only,
+                           Terrain_type_id t);
 
 /* Functions to operate on a terrain flag. */
 bool is_terrain_flag_near_tile(int x, int y, enum terrain_flag_id flag);
-int count_terrain_flag_near_tile(int x, int y, enum terrain_flag_id flag);
-int adjacent_terrain_flag_tiles4(int x, int y, enum terrain_flag_id flag);
+int count_terrain_flag_near_tile(int x, int y, bool cardinal_only,
+                                enum terrain_flag_id flag);
 
 /* Terrain-specific functions. */
 #define is_ocean(x) (terrain_has_flag((x), TER_OCEANIC))
 #define is_ocean_near_tile(x, y) is_terrain_flag_near_tile(x, y, TER_OCEANIC)
-#define adjacent_ocean_tiles4(x, y) \
-         adjacent_terrain_flag_tiles4(x, y, TER_OCEANIC)
-#define count_ocean_near_tile(x, y) \
-         count_terrain_flag_near_tile(x, y, TER_OCEANIC)
+#define count_ocean_near_tile(x, y, cardinal_only)             \
+  count_terrain_flag_near_tile(x, y, cardinal_only, TER_OCEANIC)
 
 /* This iterator iterates over all terrain types. */
 #define terrain_type_iterate(id)                                            \
Index: server/mapgen.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/mapgen.c,v
retrieving revision 1.148
diff -u -r1.148 mapgen.c
--- server/mapgen.c     25 Aug 2004 18:24:20 -0000      1.148
+++ server/mapgen.c     27 Aug 2004 05:55:22 -0000
@@ -618,7 +618,7 @@
 *********************************************************************/
 static int river_test_adjacent_ocean(int x, int y)
 {
-  return 4 - adjacent_ocean_tiles4(x, y);
+  return 4 - count_ocean_near_tile(x, y, TRUE);
 }
 
 /*********************************************************************
@@ -634,9 +634,8 @@
 *********************************************************************/
 static int river_test_adjacent_highlands(int x, int y)
 {
-  return
-    adjacent_terrain_tiles4(x, y, T_HILLS) +
-    2 * adjacent_terrain_tiles4(x, y , T_MOUNTAINS);
+  return (count_terrain_near_tile(x, y, TRUE, T_HILLS)
+         + 2 * count_terrain_near_tile(x, y, TRUE, T_MOUNTAINS));
 }
 
 /*********************************************************************
@@ -652,7 +651,7 @@
 *********************************************************************/
 static int river_test_adjacent_swamp(int x, int y)
 {
-  return 4 - adjacent_terrain_tiles4(x, y, T_SWAMP);
+  return 4 - count_terrain_near_tile(x, y, TRUE, T_SWAMP);
 }
 
 /*********************************************************************
@@ -810,7 +809,7 @@
     /* Test if the river is done. */
     /* We arbitrarily make rivers end at the poles. */
     if (adjacent_river_tiles4(x, y) != 0
-       || adjacent_ocean_tiles4(x, y) != 0
+       || count_ocean_near_tile(x, y, TRUE) != 0
         || (map_get_terrain(x, y) == T_ARCTIC 
            && map_temperature(x, y) < 8 * MAX_TEMP / 100)) { 
 
@@ -954,13 +953,13 @@
        /* Don't start a river on a tile is surrounded by > 1 river +
           ocean tile. */
        adjacent_river_tiles4(x, y) +
-       adjacent_ocean_tiles4(x, y) <= 1 &&
+       count_ocean_near_tile(x, y, TRUE) <= 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. */
-       (adjacent_terrain_tiles4(x, y, T_HILLS) +
-        adjacent_terrain_tiles4(x, y, T_MOUNTAINS) < 4 ||
+       (count_terrain_near_tile(x, y, TRUE, T_HILLS)
+        + count_terrain_near_tile(x, y, TRUE, T_MOUNTAINS) < 4 ||
         iteration_counter == RIVERS_MAXTRIES/10 * 5) &&
 
        /* Don't start a river on hills unless it is hard to find
@@ -1960,7 +1959,7 @@
             )
           &&( !is_cardinally_adj_to_ocean(x, y) || myrand(100) < coast )) {
        if (is_water_adjacent_to_tile(x, y) &&
-           count_ocean_near_tile(x, y) < 4 &&
+           count_ocean_near_tile(x, y, FALSE) < 4 &&
             count_special_near_tile(x, y, S_RIVER) < 3) {
          map_set_special(x, y, S_RIVER);
          i--;

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