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: Mon, 30 Aug 2004 20:01:44 -0700
Reply-to: rt@xxxxxxxxxxx

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

> [jdorje - Fri Aug 27 06:12:41 2004]:
> 
> 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.

This patch fixes some bad bugs in variable_adjacent_iterate, and adds a
function-like block comment.

Marcelo, you may want to take a look at it.

jason

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    31 Aug 2004 03:01:30 -0000
@@ -131,6 +131,31 @@
 }
 
 /****************************************************************************
+  This iterator behaves like adjc_iterate or cardinal_adjc_iterate depending
+  on the value of card_only.
+****************************************************************************/
+#define variable_adjc_iterate(center_x, center_y, x_itr, y_itr, card_only)  \
+{                                                                          \
+  enum direction8 *_dirlist;                                               \
+  int _total;                                                              \
+                                                                           \
+  if (card_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.
 ****************************************************************************/
 bool is_terrain_near_tile(int map_x, int map_y, Terrain_type_id t)
@@ -147,36 +172,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 +204,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    31 Aug 2004 03:01:30 -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.149
diff -u -r1.149 mapgen.c
--- server/mapgen.c     28 Aug 2004 16:50:49 -0000      1.149
+++ server/mapgen.c     31 Aug 2004 03:01:31 -0000
@@ -625,7 +625,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);
 }
 
 /*********************************************************************
@@ -641,9 +641,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));
 }
 
 /*********************************************************************
@@ -659,7 +658,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);
 }
 
 /*********************************************************************
@@ -817,7 +816,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)) { 
 
@@ -961,13 +960,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
@@ -1973,7 +1972,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]