From 5f703ee4f00b68c62f0545ff142c8d32e42af7f9 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Sat, 18 May 2024 20:12:52 +0200 Subject: [PATCH 2/7] Client: Keep track of continent and ocean sizes See RM #642 Signed-off-by: Alina Lenk --- client/packhand.c | 41 ++++++++++++++++++++++++++++++++++++++++- client/text.c | 8 ++++++++ common/map.c | 4 ++-- common/map_types.h | 6 +++--- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/client/packhand.c b/client/packhand.c index a16675a78e..4942abae62 100644 --- a/client/packhand.c +++ b/client/packhand.c @@ -3326,8 +3326,47 @@ void handle_tile_info(const struct packet_tile_info *packet) unit_list_clear(ptile->units); } + if (ptile->continent != packet->continent) { + /* Update known continents and sizes */ + if (packet->continent > wld.map.num_continents) { + int i; + + /* Reallocate sizes array and fill new spots with zeros */ + wld.map.continent_sizes = fc_realloc(wld.map.continent_sizes, + (packet->continent + 1) * sizeof(*wld.map.continent_sizes)); + for (i = wld.map.num_continents + 1; i <= packet->continent; i++) { + wld.map.continent_sizes[i] = 0; + } + + wld.map.num_continents = packet->continent; + } else if (packet->continent < -wld.map.num_oceans) { + int i; + + /* Reallocate sizes array and fill new spots with zeros */ + wld.map.ocean_sizes = fc_realloc(wld.map.ocean_sizes, + (-packet->continent + 1) * sizeof(*wld.map.ocean_sizes)); + for (i = wld.map.num_oceans + 1; i <= -packet->continent; i++) { + wld.map.ocean_sizes[i] = 0; + } + + wld.map.num_oceans = -packet->continent; + } + + /* Decrement old continent/ocean size */ + if (ptile->continent > 0) { + wld.map.continent_sizes[ptile->continent]--; + } else if (ptile->continent < 0) { + wld.map.ocean_sizes[-ptile->continent]--; + } + + /* Increment new continent/ocean size */ + if (packet->continent > 0) { + wld.map.continent_sizes[packet->continent]++; + } else if (packet->continent < 0) { + wld.map.ocean_sizes[-packet->continent]++; + } + } ptile->continent = packet->continent; - wld.map.num_continents = MAX(ptile->continent, wld.map.num_continents); if (packet->label[0] == '\0') { if (ptile->label != NULL) { diff --git a/client/text.c b/client/text.c index 467cd28114..9886a67df8 100644 --- a/client/text.c +++ b/client/text.c @@ -175,6 +175,14 @@ const char *popup_info_text(struct tile *ptile) index_to_native_pos(&nat_x, &nat_y, tile_index(ptile)); astr_add_line(&str, _("Native coordinates: (%d, %d)"), nat_x, nat_y); + + if (tile_continent(ptile) > 0) { + astr_add_line(&str, _("Continent size: at least %d"), + get_continent_size(tile_continent(ptile))); + } else if (tile_continent(ptile) < 0) { + astr_add_line(&str, _("Ocean size: at least %d"), + get_ocean_size(-tile_continent(ptile))); + } astr_add_line(&str, _("Latitude: %d"), map_signed_latitude(ptile)); diff --git a/common/map.c b/common/map.c index 15c6f583c5..0236d400f9 100644 --- a/common/map.c +++ b/common/map.c @@ -797,8 +797,6 @@ 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]; } @@ -820,6 +818,8 @@ int get_lake_surrounders(Continent_id id) { fc_assert_ret_val(id < 0, -1); fc_assert_ret_val(id >= -wld.map.num_oceans, -1); + /* Client updates num_oceans, but not lake_surrounders */ + fc_assert_ret_val(is_server(), -1); return wld.map.lake_surrounders[-id]; } diff --git a/common/map_types.h b/common/map_types.h index 0452bdc7ec..22892d7420 100644 --- a/common/map_types.h +++ b/common/map_types.h @@ -81,7 +81,7 @@ struct civ_map { int south_latitude; int num_continents; - int num_oceans; /* Not updated at the client */ + int num_oceans; /* 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. @@ -92,8 +92,8 @@ struct civ_map { * 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 */ + int *continent_sizes; + int *ocean_sizes; Continent_id *lake_surrounders; /* Not updated at the client */ struct tile *tiles; -- 2.34.1