[Freeciv-Dev] (PR#9921) percentages for count_xxx_near_tile
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: |
undisclosed-recipients: ; |
Subject: |
[Freeciv-Dev] (PR#9921) percentages for count_xxx_near_tile |
From: |
"Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx> |
Date: |
Tue, 7 Sep 2004 20:51:20 -0700 |
Reply-to: |
rt@xxxxxxxxxxx |
<URL: http://rt.freeciv.org/Ticket/Display.html?id=9921 >
> [jdorje - Fri Sep 03 15:35:07 2004]:
>
> This patch changes count_xxx_near_tile to optionally return percentage
> values rather than the number of tiles. This is controlled by a
> parameter to the function.
Here's an updated patch.
jason
Index: common/terrain.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.c,v
retrieving revision 1.14
diff -u -r1.14 terrain.c
--- common/terrain.c 3 Sep 2004 15:12:26 -0000 1.14
+++ common/terrain.c 8 Sep 2004 03:50:45 -0000
@@ -172,17 +172,22 @@
/****************************************************************************
Return the number of adjacent tiles that have the given terrain.
****************************************************************************/
-int count_terrain_near_tile(int map_x, int map_y, bool cardinal_only,
+int count_terrain_near_tile(int map_x, int map_y,
+ bool cardinal_only, bool percentage,
Terrain_type_id t)
{
- int count = 0;
+ int count = 0, total = 0;
variable_adjc_iterate(map_x, map_y, adjc_x, adjc_y, cardinal_only) {
if (map_get_terrain(adjc_x, adjc_y) == t) {
count++;
}
+ total++;
} variable_adjc_iterate_end;
+ if (percentage) {
+ count = count * 100 / total;
+ }
return count;
}
@@ -203,17 +208,22 @@
/****************************************************************************
Returns the number of adjacent tiles that have the given map special.
****************************************************************************/
-int count_special_near_tile(int map_x, int map_y, bool cardinal_only,
+int count_special_near_tile(int map_x, int map_y,
+ bool cardinal_only, bool percentage,
enum tile_special_type spe)
{
- int count = 0;
+ int count = 0, total = 0;
variable_adjc_iterate(map_x, map_y, adjc_x, adjc_y, cardinal_only) {
if (map_has_special(adjc_x, adjc_y, spe)) {
count++;
}
+ total++;
} variable_adjc_iterate_end;
+ if (percentage) {
+ count = count * 100 / total;
+ }
return count;
}
@@ -235,16 +245,21 @@
/****************************************************************************
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, bool cardinal_only,
+int count_terrain_flag_near_tile(int map_x, int map_y,
+ bool cardinal_only, bool percentage,
enum terrain_flag_id flag)
{
- int count = 0;
+ int count = 0, total = 0;
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++;
}
+ total++;
} variable_adjc_iterate_end;
+ if (percentage) {
+ count = count * 100 / total;
+ }
return count;
}
Index: common/terrain.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/terrain.h,v
retrieving revision 1.27
diff -u -r1.27 terrain.h
--- common/terrain.h 7 Sep 2004 23:28:47 -0000 1.27
+++ common/terrain.h 8 Sep 2004 03:50:45 -0000
@@ -109,24 +109,27 @@
/* 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, bool cardinal_only,
+int count_terrain_near_tile(int map_x, int map_y,
+ bool cardinal_only, bool percentage,
Terrain_type_id t);
/* Functions to operate on a terrain special. */
bool is_special_near_tile(int map_x, int map_y, enum tile_special_type spe);
-int count_special_near_tile(int map_x, int map_y, bool cardinal_only,
+int count_special_near_tile(int map_x, int map_y,
+ bool cardinal_only, bool percentage,
enum tile_special_type spe);
/* 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, bool cardinal_only,
+int count_terrain_flag_near_tile(int x, int y,
+ bool cardinal_only, bool percentage,
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 count_ocean_near_tile(x, y, cardinal_only) \
- count_terrain_flag_near_tile(x, y, cardinal_only, TER_OCEANIC)
+#define count_ocean_near_tile(x, y, cardinal_only, percentage) \
+ count_terrain_flag_near_tile(x, y, cardinal_only, percentage, 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.159
diff -u -r1.159 mapgen.c
--- server/mapgen.c 8 Sep 2004 02:38:14 -0000 1.159
+++ server/mapgen.c 8 Sep 2004 03:50:46 -0000
@@ -315,9 +315,10 @@
/* WETNESS */
/* necessary condition of deserts placement */
-#define map_pos_is_dry(x, y) ((map_temperature((x), (y)) <= DRY_MAX_LEVEL) &&\
- (map_temperature((x), (y)) > DRY_MIN_LEVEL) && \
- (count_ocean_near_tile((x),(y),FALSE) <= 2))
+#define map_pos_is_dry(x, y) \
+ (map_temperature((x), (y)) <= DRY_MAX_LEVEL \
+ && map_temperature((x), (y)) > DRY_MIN_LEVEL
\
+ && count_ocean_near_tile((x), (y), FALSE, TRUE) <= 50)
typedef enum { WC_ALL = 200, WC_DRY, WC_NDRY } wetness_c;
/* TEMPERATURE */
@@ -755,7 +756,7 @@
*********************************************************************/
static int river_test_rivergrid(int x, int y)
{
- return (count_special_near_tile(x, y, TRUE, S_RIVER) > 1) ? 1 : 0;
+ return (count_special_near_tile(x, y, TRUE, FALSE, S_RIVER) > 1) ? 1 : 0;
}
/*********************************************************************
@@ -772,9 +773,7 @@
*********************************************************************/
static int river_test_adjacent_ocean(int x, int y)
{
- /* This number must always be >= 0. 6 is the maximum number of
- * cardinal directions. */
- return 6 - count_ocean_near_tile(x, y, TRUE);
+ return 100 - count_ocean_near_tile(x, y, TRUE, TRUE);
}
/*********************************************************************
@@ -782,9 +781,7 @@
*********************************************************************/
static int river_test_adjacent_river(int x, int y)
{
- /* This number must always be >= 0. 6 is the maximum number of
- * cardinal directions. */
- return 6 - count_special_near_tile(x, y, TRUE, S_RIVER);
+ return 100 - count_special_near_tile(x, y, TRUE, TRUE, S_RIVER);
}
/*********************************************************************
@@ -792,8 +789,8 @@
*********************************************************************/
static int river_test_adjacent_highlands(int x, int y)
{
- return (count_terrain_near_tile(x, y, TRUE, T_HILLS)
- + 2 * count_terrain_near_tile(x, y, TRUE, T_MOUNTAINS));
+ return (count_terrain_near_tile(x, y, TRUE, TRUE, T_HILLS)
+ + 2 * count_terrain_near_tile(x, y, TRUE, TRUE, T_MOUNTAINS));
}
/*********************************************************************
@@ -809,9 +806,7 @@
*********************************************************************/
static int river_test_adjacent_swamp(int x, int y)
{
- /* This number must always be >= 0. 6 is the maximum number of
- * cardinal directions. */
- return 6 - count_terrain_near_tile(x, y, TRUE, T_SWAMP);
+ return 100 - count_terrain_near_tile(x, y, TRUE, TRUE, T_SWAMP);
}
/*********************************************************************
@@ -910,16 +905,13 @@
(river_test_adjacent_ocean)
Rivers must flow down to coastal areas when possible:
- Possible values:
- n: 4 - adjacent_terrain_tiles4(...)
+ Possible values: 0-100
* Adjacent river:
(river_test_adjacent_river)
Rivers must flow down to areas near other rivers when possible:
- Possible values:
- n: 6 - count_river_near_tile(...) (should be small after the
- river-grid test)
+ Possible values: 0-100
* Adjacent highlands:
(river_test_adjacent_highlands)
@@ -968,8 +960,8 @@
/* Test if the river is done. */
/* We arbitrarily make rivers end at the poles. */
- if (count_special_near_tile(x, y, TRUE, S_RIVER) != 0
- || count_ocean_near_tile(x, y, TRUE) != 0
+ if (count_special_near_tile(x, y, TRUE, TRUE, S_RIVER) > 0
+ || count_ocean_near_tile(x, y, TRUE, TRUE) > 0
|| (map_get_terrain(x, y) == T_ARCTIC
&& map_temperature(x, y) < 8 * MAX_TEMP / 100)) {
@@ -1113,14 +1105,14 @@
/* Don't start a river on a tile is surrounded by > 1 river +
ocean tile. */
- && (count_special_near_tile(x, y, TRUE, S_RIVER)
- + count_ocean_near_tile(x, y, TRUE) <= 1)
+ && (count_special_near_tile(x, y, TRUE, FALSE, S_RIVER)
+ + count_ocean_near_tile(x, y, TRUE, FALSE) <= 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. */
- && (count_terrain_near_tile(x, y, TRUE, T_HILLS)
- + count_terrain_near_tile(x, y, TRUE, T_MOUNTAINS) < 4
+ && (count_terrain_near_tile(x, y, TRUE, TRUE, T_HILLS)
+ + count_terrain_near_tile(x, y, TRUE, TRUE, T_MOUNTAINS) < 90
|| iteration_counter == RIVERS_MAXTRIES / 10 * 5)
/* Don't start a river on hills unless it is hard to find
@@ -2138,12 +2130,12 @@
/* the first condition helps make terrain more contiguous,
the second lets it avoid the coast: */
if ((i * 3 > k * 2
- || count_special_near_tile(x, y, FALSE, S_RIVER) > 0
+ || count_special_near_tile(x, y, FALSE, TRUE, S_RIVER) > 0
|| myrand(100) < 50)
&& (!is_cardinally_adj_to_ocean(x, y) || myrand(100) < coast)) {
if (is_water_adjacent_to_tile(x, y)
- && count_ocean_near_tile(x, y, FALSE) < 4
- && count_special_near_tile(x, y, FALSE, S_RIVER) < 3) {
+ && count_ocean_near_tile(x, y, FALSE, TRUE) < 50
+ && count_special_near_tile(x, y, FALSE, TRUE, S_RIVER) < 35) {
map_set_special(x, y, S_RIVER);
i--;
}
|
|