[Freeciv-Dev] (PR#6182) remove some static map-sized arrays
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
There are two remaining MAP_MAX_WIDTH * MAP_MAX_HEIGHT arrays: in
settlers.c.
These present problems when increasing MAP_MAX_WIDTH and MAP_MAX_HEIGHT.
They also break under gen-topologies. And of course they waste memory
in general.
The attached patch replaces them with dynamic arrays, as is used
everywhere else. Autogames are identical and slightly faster (probably
because of the direct memset to territory).
jason
? rc
Index: server/cityturn.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/cityturn.c,v
retrieving revision 1.222
diff -u -r1.222 cityturn.c
--- server/cityturn.c 2003/09/09 15:55:52 1.222
+++ server/cityturn.c 2003/09/17 17:29:21
@@ -183,7 +183,7 @@
/* better than nothing, not as good as a global worker allocation -- Syela */
memset(conflict, 0, sizeof(conflict));
city_map_checked_iterate(pcity->x, pcity->y, cx, cy, mx, my) {
- conflict[cx][cy] = -1 - minimap[mx][my];
+ conflict[cx][cy] = -1 - MINIMAP(mx, my);
} city_map_checked_iterate_end;
do {
Index: server/settlers.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/settlers.c,v
retrieving revision 1.171
diff -u -r1.171 settlers.c
--- server/settlers.c 2003/08/09 15:34:19 1.171
+++ server/settlers.c 2003/09/17 17:29:21
@@ -24,6 +24,7 @@
#include "government.h"
#include "log.h"
#include "map.h"
+#include "mem.h"
#include "packets.h"
#include "support.h"
#include "timing.h"
@@ -44,10 +45,12 @@
#include "settlers.h"
/* negative: in_city_radius, 0: unassigned, positive: city_des */
-signed int minimap[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
+signed int *minimap;
BV_DEFINE(nearness, MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS);
-static nearness territory[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
+static nearness *territory;
+#define TERRITORY(map_x, map_y) territory[map_pos_to_index(map_x, map_y)]
+
BV_DEFINE(enemy_mask, MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS);
static enemy_mask enemies[MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS];
@@ -133,11 +136,12 @@
**************************************************************************/
void generate_minimap(void)
{
- memset(minimap, 0, sizeof(minimap));
+ minimap = fc_realloc(minimap, map.xsize * map.ysize * sizeof(*minimap));
+ memset(minimap, 0, map.xsize * map.ysize * sizeof(*minimap));
players_iterate(pplayer) {
city_list_iterate(pplayer->cities, pcity) {
map_city_radius_iterate(pcity->x, pcity->y, x_itr, y_itr) {
- minimap[x_itr][y_itr]--;
+ MINIMAP(x_itr, y_itr)--;
} map_city_radius_iterate_end;
} city_list_iterate_end;
} players_iterate_end;
@@ -154,10 +158,15 @@
square_iterate(x, y, CITY_MAP_SIZE-1, x1, y1) {
int dist = sq_map_distance(x, y, x1, y1);
if (dist <= CITY_MAP_SIZE) {
- if (minimap[x1][y1] < 0) minimap[x1][y1]++;
- else minimap[x1][y1] = 0;
+ if (MINIMAP(x1, y1) < 0) {
+ MINIMAP(x1, y1)++;
+ } else {
+ MINIMAP(x1, y1) = 0;
+ }
} else if (dist <= 20) {
- if (minimap[x1][y1] > 0) minimap[x1][y1] = 0;
+ if (MINIMAP(x1, y1) > 0) {
+ MINIMAP(x1, y1) = 0;
+ }
}
} square_iterate_end;
}
@@ -175,10 +184,15 @@
square_iterate(x, y, CITY_MAP_SIZE-1, x1, y1) {
int dist = sq_map_distance(x, y, x1, y1);
if (dist <= CITY_MAP_SIZE) {
- if (minimap[x1][y1] < 0) minimap[x1][y1]--;
- else minimap[x1][y1] = -1;
+ if (MINIMAP(x1, y1) < 0) {
+ MINIMAP(x1, y1)--;
+ } else {
+ MINIMAP(x1, y1) = -1;
+ }
} else if (dist <= 20) {
- if (minimap[x1][y1] > 0) minimap[x1][y1] = 0;
+ if (MINIMAP(x1, y1) > 0) {
+ MINIMAP(x1, y1) = 0;
+ }
}
} square_iterate_end;
}
@@ -222,8 +236,12 @@
}
pcity = map_get_city(x, y);
if (pcity && pcity->size >= game.add_to_size_limit) return 0;
- if (!pcity && minimap[x][y] < 0) return 0;
- if (!pcity && minimap[x][y] > 0) return minimap[x][y];
+ if (!pcity && MINIMAP(x, y) < 0) {
+ return 0;
+ }
+ if (!pcity && MINIMAP(x, y) > 0) {
+ return MINIMAP(x, y);
+ }
harbour = is_ocean_near_tile(x, y);
@@ -241,7 +259,7 @@
const int cshields = SHIELD_WEIGHTING * MORT;
const int ctrade = TRADE_WEIGHTING * MORT;
- if ((!pcity && minimap[map_x][map_y] >= 0)
+ if ((!pcity && MINIMAP(map_x, map_y) >= 0)
|| (pcity && get_worker_city(pcity, i, j) == C_TILE_EMPTY)) {
ptile = map_get_tile(map_x, map_y);
continent2 = ptile->continent;
@@ -426,7 +444,7 @@
else val = tmp;
}
val -= 110 * SHIELD_WEIGHTING; /* WAG: walls, defenders */
- minimap[x][y] = val;
+ MINIMAP(x, y) = val;
if (debug) {
freelog(LOG_DEBUG, "Total value of (%d, %d) [%d workers] = %d",
@@ -935,7 +953,7 @@
if (!is_already_assigned(punit, pplayer, x, y)
&& city_can_be_built_here(x, y, punit)
- && !BV_CHECK_MASK(territory[x][y], my_enemies)
+ && !BV_CHECK_MASK(TERRITORY(x, y), my_enemies)
/* pretty good, hope it's enough! -- Syela */
&& (near < 8 || map_get_continent(x, y) != ucont)
&& !city_exists_within_city_radius(x, y, FALSE)) {
@@ -1079,7 +1097,7 @@
in_use = (get_worker_city(pcity, i, j) == C_TILE_WORKER);
if (map_get_continent(x, y) == ucont
&& WARMAP_COST(x, y) <= THRESHOLD * mv_rate
- && !BV_CHECK_MASK(territory[x][y], my_enemies)
+ && !BV_CHECK_MASK(TERRITORY(x, y), my_enemies)
/* pretty good, hope it's enough! -- Syela */
&& !is_already_assigned(punit, pplayer, x, y)) {
/* calling is_already_assigned once instead of four times
@@ -1460,8 +1478,9 @@
static void assign_region(int x, int y, int player_no, int distance, int s)
{
square_iterate(x, y, distance, x1, y1) {
- if (s == 0 || is_ocean_near_tile(x1, y1))
- BV_SET(territory[x1][y1], player_no);
+ if (s == 0 || is_ocean_near_tile(x1, y1)) {
+ BV_SET(TERRITORY(x1, y1), player_no);
+ }
} square_iterate_end;
}
@@ -1508,9 +1527,9 @@
**************************************************************************/
static void assign_territory(void)
{
- whole_map_iterate(x, y) {
- BV_CLR_ALL(territory[x][y]);
- } whole_map_iterate_end;
+ territory = fc_realloc(territory,
+ map.xsize * map.ysize * sizeof(*territory));
+ memset(territory, 0, map.xsize * map.ysize * sizeof(*territory));
players_iterate(pplayer) {
assign_territory_player(pplayer);
Index: server/settlers.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/settlers.h,v
retrieving revision 1.22
diff -u -r1.22 settlers.h
--- server/settlers.h 2002/12/18 17:36:20 1.22
+++ server/settlers.h 2003/09/17 17:29:21
@@ -37,6 +37,7 @@
struct unit *other_passengers(struct unit *punit);
-extern signed int minimap[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
+extern signed int *minimap;
+#define MINIMAP(map_x, map_y) minimap[map_pos_to_index(map_x, map_y)]
#endif /* FC__SETTLERS_H */
- [Freeciv-Dev] (PR#6182) remove some static map-sized arrays,
Jason Short <=
|
|