From a16c28ea03d09af26045ed980255d9834037ac4a Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Sat, 18 May 2024 17:29:55 +0200 Subject: [PATCH 1/6] Move continent/ocean sizes and lake surrounders to common code See RM #641 Signed-off-by: Alina Lenk --- ai/default/aiparatrooper.c | 4 +- common/map.c | 49 +++++++++++++++ common/map.h | 3 + common/map_types.h | 15 +++++ server/generator/mapgen_utils.c | 106 +++++++++----------------------- server/generator/mapgen_utils.h | 3 - 6 files changed, 96 insertions(+), 84 deletions(-) diff --git a/ai/default/aiparatrooper.c b/ai/default/aiparatrooper.c index 379438285a..ee4f80b79d 100644 --- a/ai/default/aiparatrooper.c +++ b/ai/default/aiparatrooper.c @@ -21,6 +21,7 @@ /* common */ #include "city.h" #include "game.h" +#include "map.h" #include "player.h" #include "research.h" #include "unit.h" @@ -39,9 +40,6 @@ /* server/advisors */ #include "advdata.h" -/* server/generator */ -#include "mapgen_utils.h" - /* ai */ #include "handicaps.h" diff --git a/common/map.c b/common/map.c index 55aac29595..15c6f583c5 100644 --- a/common/map.c +++ b/common/map.c @@ -173,6 +173,10 @@ void map_init(struct civ_map *imap, bool server_side) imap->north_latitude = MAP_DEFAULT_NORTH_LATITUDE; imap->south_latitude = MAP_DEFAULT_SOUTH_LATITUDE; + imap->continent_sizes = nullptr; + imap->ocean_sizes = nullptr; + imap->lake_surrounders = nullptr; + if (server_side) { imap->server.mapsize = MAP_DEFAULT_MAPSIZE; imap->server.size = MAP_DEFAULT_SIZE; @@ -552,6 +556,18 @@ void map_free(struct civ_map *fmap) FC_FREE(fmap->iterate_outwards_indices); } + if (fmap->continent_sizes) { + FC_FREE(fmap->continent_sizes); + fmap->num_continents = 0; + } + if (fmap->ocean_sizes) { + FC_FREE(fmap->ocean_sizes); + fmap->num_oceans = 0; + } + if (fmap->lake_surrounders) { + FC_FREE(fmap->lake_surrounders); + fmap->num_oceans = 0; + } } /*******************************************************************//** @@ -774,6 +790,39 @@ bool terrain_surroundings_allow_change(const struct civ_map *nmap, return ret; } +/**********************************************************************//** + Return size in tiles of the given continent (not ocean) +**************************************************************************/ +int get_continent_size(Continent_id id) +{ + fc_assert_ret_val(id > 0, -1); + fc_assert_ret_val(id <= wld.map.num_continents, -1); + /* Client updates num_continents, but not continent_sizes */ + fc_assert_ret_val(is_server(), -1); + return wld.map.continent_sizes[id]; +} + +/**********************************************************************//** + Return size in tiles of the given ocean. You should use positive ocean + number. +**************************************************************************/ +int get_ocean_size(Continent_id id) +{ + fc_assert_ret_val(id > 0, -1); + fc_assert_ret_val(id <= wld.map.num_oceans, -1); + return wld.map.ocean_sizes[id]; +} + +/**********************************************************************//** + Get continent surrounding lake, or -1 if there is multiple continents. +**************************************************************************/ +int get_lake_surrounders(Continent_id id) +{ + fc_assert_ret_val(id < 0, -1); + fc_assert_ret_val(id >= -wld.map.num_oceans, -1); + return wld.map.lake_surrounders[-id]; +} + /*******************************************************************//** The basic cost to move punit from tile t1 to tile t2. That is, tile_move_cost(), with pre-calculated tile pointers; diff --git a/common/map.h b/common/map.h index 3acae9e6e1..d3f285727e 100644 --- a/common/map.h +++ b/common/map.h @@ -70,6 +70,9 @@ int get_direction_for_step(const struct civ_map *nmap, const struct tile *src_tile, const struct tile *dst_tile); +int get_continent_size(Continent_id id); +int get_ocean_size(Continent_id id); +int get_lake_surrounders(Continent_id id); /* Specific functions for start positions. */ struct startpos *map_startpos_by_number(int id); diff --git a/common/map_types.h b/common/map_types.h index 3f5672316c..0452bdc7ec 100644 --- a/common/map_types.h +++ b/common/map_types.h @@ -79,8 +79,23 @@ struct civ_map { int xsize, ysize; /* Native dimensions */ int north_latitude; int south_latitude; + int num_continents; int num_oceans; /* Not updated at the client */ + /* These arrays are indexed by continent number (or negative of the + * ocean number) so the 0th element is unused and the array is 1 element + * larger than you'd expect. + * + * The _sizes arrays give the sizes (in tiles) of each continent and + * ocean. + * + * The lake_surrounders array tells which single continent surrounds each + * ocean; or -1 if there's more than one adjacent continent. + */ + int *continent_sizes; /* Not updated at the client */ + int *ocean_sizes; /* Not updated at the client */ + Continent_id *lake_surrounders; /* Not updated at the client */ + struct tile *tiles; struct startpos_hash *startpos_table; diff --git a/server/generator/mapgen_utils.c b/server/generator/mapgen_utils.c index 1c1dc3522b..39904b7fcd 100644 --- a/server/generator/mapgen_utils.c +++ b/server/generator/mapgen_utils.c @@ -231,29 +231,16 @@ void smooth_int_map(int *int_map, bool zeroes_at_edges) FC_FREE(alt_int_map); } -/* These arrays are indexed by continent number (or negative of the - * ocean number) so the 0th element is unused and the array is 1 element - * larger than you'd expect. - * - * The lake surrounders array tells how many land continents surround each - * ocean (or -1 if the ocean touches more than one continent). - * - * The _sizes arrays give the sizes (in tiles) of each continent and - * ocean. - */ -static Continent_id *lake_surrounders = NULL; -static int *continent_sizes = NULL; -static int *ocean_sizes = NULL; - /**********************************************************************//** - Calculate lake_surrounders[] array + Calculate wld.map.lake_surrounders[] array **************************************************************************/ static void recalculate_lake_surrounders(void) { - const size_t size = (wld.map.num_oceans + 1) * sizeof(*lake_surrounders); + size_t size; - lake_surrounders = fc_realloc(lake_surrounders, size); - memset(lake_surrounders, 0, size); + size = (wld.map.num_oceans + 1) * sizeof(*wld.map.lake_surrounders); + wld.map.lake_surrounders = fc_realloc(wld.map.lake_surrounders, size); + memset(wld.map.lake_surrounders, 0, size); whole_map_iterate(&(wld.map), ptile) { const struct terrain *pterrain = tile_terrain(ptile); @@ -263,17 +250,19 @@ static void recalculate_lake_surrounders(void) continue; } - if (terrain_type_terrain_class(pterrain) != TC_OCEAN) { - adjc_iterate(&(wld.map), ptile, tile2) { - Continent_id cont2 = tile_continent(tile2); - - if (is_ocean_tile(tile2)) { - if (lake_surrounders[-cont2] == 0) { - lake_surrounders[-cont2] = cont; - } else if (lake_surrounders[-cont2] != cont) { - lake_surrounders[-cont2] = -1; - } - } + if (!is_ocean(pterrain)) { + fc_assert_action(cont > 0, continue); + adjc_iterate(&(wld.map), ptile, adj_tile) { + Continent_id adj_cont = tile_continent(adj_tile); + + if (is_ocean_tile(adj_tile)) { + fc_assert_action(adj_cont < 0, continue); + if (wld.map.lake_surrounders[-adj_cont] == 0) { + wld.map.lake_surrounders[-adj_cont] = cont; + } else if (wld.map.lake_surrounders[-adj_cont] != cont) { + wld.map.lake_surrounders[-adj_cont] = -1; + } + } } adjc_iterate_end; } } whole_map_iterate_end; @@ -332,9 +321,9 @@ static void assign_continent_flood(struct tile *ptile, bool is_land, int nr) /* Count the tile */ if (nr < 0) { - ocean_sizes[-nr]++; + wld.map.ocean_sizes[-nr]++; } else { - continent_sizes[nr]++; + wld.map.continent_sizes[nr]++; } } @@ -405,8 +394,8 @@ void regenerate_lakes(void) if (terrain_type_terrain_class(pterrain) != TC_OCEAN) { continue; } - if (0 < lake_surrounders[-here]) { - if (terrain_control.lake_max_size >= ocean_sizes[-here]) { + if (0 < wld.map.lake_surrounders[-here]) { + if (terrain_control.lake_max_size >= wld.map.ocean_sizes[-here]) { int frozen = terrain_has_flag(pterrain, TER_FROZEN); tile_change_terrain(ptile, lake_for_ocean[frozen][-here-1]); } @@ -414,33 +403,6 @@ void regenerate_lakes(void) } whole_map_iterate_end; } -/**********************************************************************//** - Get continent surrounding lake, or -1 if there is multiple continents. -**************************************************************************/ -int get_lake_surrounders(Continent_id cont) -{ - return lake_surrounders[-cont]; -} - -/**********************************************************************//** - Return size in tiles of the given continent (not ocean) -**************************************************************************/ -int get_continent_size(Continent_id id) -{ - fc_assert_ret_val(id > 0, -1); - return continent_sizes[id]; -} - -/**********************************************************************//** - Return size in tiles of the given ocean. You should use positive ocean - number. -**************************************************************************/ -int get_ocean_size(Continent_id id) -{ - fc_assert_ret_val(id > 0, -1); - return ocean_sizes[id]; -} - /**********************************************************************//** Assigns continent and ocean numbers to all tiles, and set map.num_continents and map.num_oceans. Recalculates continent and @@ -474,15 +436,15 @@ void assign_continent_numbers(void) if (terrain_type_terrain_class(pterrain) != TC_OCEAN) { wld.map.num_continents++; - continent_sizes = fc_realloc(continent_sizes, - (wld.map.num_continents + 1) * sizeof(*continent_sizes)); - continent_sizes[wld.map.num_continents] = 0; + wld.map.continent_sizes = fc_realloc(wld.map.continent_sizes, + (wld.map.num_continents + 1) * sizeof(*wld.map.continent_sizes)); + wld.map.continent_sizes[wld.map.num_continents] = 0; assign_continent_flood(ptile, TRUE, wld.map.num_continents); } else { wld.map.num_oceans++; - ocean_sizes = fc_realloc(ocean_sizes, - (wld.map.num_oceans + 1) * sizeof(*ocean_sizes)); - ocean_sizes[wld.map.num_oceans] = 0; + wld.map.ocean_sizes = fc_realloc(wld.map.ocean_sizes, + (wld.map.num_oceans + 1) * sizeof(*wld.map.ocean_sizes)); + wld.map.ocean_sizes[wld.map.num_oceans] = 0; assign_continent_flood(ptile, FALSE, -wld.map.num_oceans); } } whole_map_iterate_end; @@ -661,18 +623,6 @@ void smooth_water_depth(void) **************************************************************************/ void generator_free(void) { - if (lake_surrounders != NULL) { - free(lake_surrounders); - lake_surrounders = NULL; - } - if (continent_sizes != NULL) { - free(continent_sizes); - continent_sizes = NULL; - } - if (ocean_sizes != NULL) { - free(ocean_sizes); - ocean_sizes = NULL; - } } /**********************************************************************//** diff --git a/server/generator/mapgen_utils.h b/server/generator/mapgen_utils.h index df2867fee4..8258f86c7e 100644 --- a/server/generator/mapgen_utils.h +++ b/server/generator/mapgen_utils.h @@ -22,9 +22,6 @@ void generator_free(void); void regenerate_lakes(void); void smooth_water_depth(void); void assign_continent_numbers(void); -int get_lake_surrounders(Continent_id cont); -int get_continent_size(Continent_id id); -int get_ocean_size(Continent_id id); struct terrain *most_shallow_ocean(bool frozen); struct terrain *pick_ocean(int depth, bool frozen); -- 2.34.1