Project

General

Profile

Feature #2502 » 0001-Fasten-map-generation-v2.patch

Paweł Nowak, 11/27/2026 10:39 PM

View differences:

server/generator/mapgen.c
struct river_map {
struct dbv blocked;
struct dbv ok;
/* Road-like extras that block a river: all those but the one the river
* is made of. */
bv_extras blocking_extras;
/* Tiles marked in 'ok', each once, in the order they were marked.
* Lets us apply or clear a river by touching only its own tiles
* instead of scanning the whole map. */
int *ok_list;
int ok_count;
int ok_alloc;
};
static int river_test_blocked(struct river_map *privermap,
......
|| plains_count > 0 || swamps_count > 0 );
}
/**********************************************************************//**
Is the tile blocked for the river being made? That is the case if the
tile was marked blocked in make_river(), or if it has some other
road-like extra than the one the river is made of. The map doesn't change
while a river is being made, so the extras can be looked at directly.
**************************************************************************/
static bool river_is_blocked(const struct river_map *privermap,
const struct tile *ptile)
{
return dbv_isset(&privermap->blocked, tile_index(ptile))
|| BV_CHECK_MASK(*tile_extras(ptile), privermap->blocking_extras);
}
/**********************************************************************//**
Help function used in make_river(). See the help there.
**************************************************************************/
......
struct tile *ptile,
struct extra_type *priver)
{
if (dbv_isset(&privermap->blocked, tile_index(ptile))) {
if (river_is_blocked(privermap, ptile)) {
return 1;
}
/* Any un-blocked? */
cardinal_adjc_iterate(&(wld.map), ptile, ptile1) {
if (!dbv_isset(&privermap->blocked, tile_index(ptile1))) {
if (!river_is_blocked(privermap, ptile1)) {
return 0;
}
} cardinal_adjc_iterate_end;
......
} cardinal_adjc_iterate_end;
}
/**********************************************************************//**
Marks a tile as part of the river being made. Each tile is recorded
once in ok_list.
**************************************************************************/
static void river_ok_set(struct river_map *privermap, int tindex)
{
if (dbv_isset(&privermap->ok, tindex)) {
return;
}
dbv_set(&privermap->ok, tindex);
if (privermap->ok_count >= privermap->ok_alloc) {
privermap->ok_alloc = privermap->ok_alloc > 0 ? privermap->ok_alloc * 2 : 256;
privermap->ok_list = fc_realloc(privermap->ok_list,
privermap->ok_alloc * sizeof(int));
}
privermap->ok_list[privermap->ok_count++] = tindex;
}
/**********************************************************************//**
Forgets the river tiles marked so far (clears only the bits we set).
**************************************************************************/
static void river_ok_clear(struct river_map *privermap)
{
int i;
for (i = 0; i < privermap->ok_count; i++) {
dbv_clr(&privermap->ok, privermap->ok_list[i]);
}
privermap->ok_count = 0;
}
/**********************************************************************//**
qsort() comparison function for tile indices (ascending).
**************************************************************************/
static int river_index_cmp(const void *a, const void *b)
{
int ia = *(const int *) a;
int ib = *(const int *) b;
return (ia > ib) - (ia < ib);
}
struct test_func {
int (*func)(struct river_map *privermap, struct tile *ptile, struct extra_type *priver);
bool fatal;
......
while (TRUE) {
/* Mark the current tile as river. */
dbv_set(&privermap->ok, tile_index(ptile));
river_ok_set(privermap, tile_index(ptile));
log_debug("The tile at (%d, %d) has been marked as river in river_map.",
TILE_XY(ptile));
......
dbv_init(&rivermap.blocked, MAP_INDEX_SIZE);
dbv_init(&rivermap.ok, MAP_INDEX_SIZE);
rivermap.ok_list = nullptr;
rivermap.ok_count = 0;
rivermap.ok_alloc = 0;
/* The main loop in this function. */
while (current_riverlength < desirable_riverlength
......
/* Reset river map before making a new river. */
dbv_clr_all(&rivermap.blocked);
dbv_clr_all(&rivermap.ok);
river_ok_clear(&rivermap);
road_river = river_types[fc_rand(river_type_count)];
/* Tiles that have some other road-like extra are blocked. */
BV_CLR_ALL(rivermap.blocking_extras);
extra_type_by_cause_iterate(EC_ROAD, oriver) {
if (oriver != road_river) {
whole_map_iterate(&(wld.map), rtile) {
if (tile_has_extra(rtile, oriver)) {
dbv_set(&rivermap.blocked, tile_index(rtile));
}
} whole_map_iterate_end;
BV_SET(rivermap.blocking_extras, extra_index(oriver));
}
} extra_type_by_cause_iterate_end;
......
/* Try to make a river. If it is OK, apply it to the map. */
if (make_river(&rivermap, ptile, road_river)) {
whole_map_iterate(&(wld.map), ptile1) {
if (dbv_isset(&rivermap.ok, tile_index(ptile1))) {
struct terrain *river_terrain = tile_terrain(ptile1);
if (!terrain_has_flag(river_terrain, TER_CAN_HAVE_RIVER)) {
/* We have to change the terrain to put a river here. */
river_terrain = pick_terrain_by_flag(TER_CAN_HAVE_RIVER);
if (river_terrain != nullptr) {
tile_set_terrain(ptile1, river_terrain);
}
int rn;
/* Apply the river tiles in ascending tile index order. This is the
* same order as when scanning the whole map, which matters as
* pick_terrain_by_flag() consumes random numbers. */
qsort(rivermap.ok_list, rivermap.ok_count, sizeof(int),
river_index_cmp);
for (rn = 0; rn < rivermap.ok_count; rn++) {
struct tile *ptile1 = index_to_tile(&(wld.map),
rivermap.ok_list[rn]);
struct terrain *river_terrain = tile_terrain(ptile1);
if (!terrain_has_flag(river_terrain, TER_CAN_HAVE_RIVER)) {
/* We have to change the terrain to put a river here. */
river_terrain = pick_terrain_by_flag(TER_CAN_HAVE_RIVER);
if (river_terrain != nullptr) {
tile_set_terrain(ptile1, river_terrain);
}
tile_add_extra(ptile1, road_river);
current_riverlength++;
map_set_placed(ptile1);
log_debug("Applied a river to (%d, %d).", TILE_XY(ptile1));
}
} whole_map_iterate_end;
tile_add_extra(ptile1, road_river);
current_riverlength++;
map_set_placed(ptile1);
log_debug("Applied a river to (%d, %d).", TILE_XY(ptile1));
}
} else {
log_debug("mapgen.c: A river failed. It might have gotten stuck "
"in a helix.");
......
dbv_free(&rivermap.blocked);
dbv_free(&rivermap.ok);
free(rivermap.ok_list);
destroy_placed_map();
}
server/generator/mapgen_utils.c
} whole_map_iterate_end;
}
/**********************************************************************//**
Gives the tile the continent (or ocean, if 'nr' is negative) number 'nr'
and counts it in the size of that continent or ocean.
**************************************************************************/
static void assign_continent_tile(struct tile *ptile, int nr)
{
tile_set_continent(ptile, nr);
if (nr < 0) {
wld.map.ocean_sizes[-nr]++;
} else {
wld.map.continent_sizes[nr]++;
}
}
/**********************************************************************//**
Number this tile and nearby tiles with the specified continent number 'nr'.
Due to the number of recursion for large maps a non-recursive algorithm is
......
&& T_UNKNOWN != pterrain
&& XOR(is_land, terrain_type_terrain_class(pterrain) == TC_OCEAN));
/* Create tile list and insert the initial tile. */
/* Create tile list and insert the initial tile.
*
* Invariant: a tile is numbered at the moment it is added to the list,
* so a tile that already has a continent number is either done or
* waiting in the list. Either way it must not be added again, and that
* is all the check in the loop below needs to know. */
tlist = tile_list_new();
assign_continent_tile(ptile, nr);
tile_list_append(tlist, ptile);
while (tile_list_size(tlist) > 0) {
......
continue;
}
/* Add the tile to the list of tiles to check. */
if (!tile_list_search(tlist, ptile3)) {
tile_list_append(tlist, ptile3);
}
/* Number the tile and add it to the list of tiles to check. */
assign_continent_tile(ptile3, nr);
tile_list_append(tlist, ptile3);
} adjc_iterate_end;
/* Set the continent data and remove the tile from the list. */
tile_set_continent(ptile2, nr);
/* This tile is done, remove it from the list. */
tile_list_remove(tlist, ptile2);
/* Count the tile */
if (nr < 0) {
wld.map.ocean_sizes[-nr]++;
} else {
wld.map.continent_sizes[nr]++;
}
}
tile_list_destroy(tlist);
(2-2/2)