From b957c2bec41571e5eb361c7ffd7ad5324c64a60c Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Mon, 21 Nov 2022 04:23:05 +0200 Subject: [PATCH 15/15] Split nationalities list from PACKET_CITY_INFO to a separate packet See osdn #46079 Signed-off-by: Marko Lindqvist --- client/packhand.c | 30 ++++++++++++++++++++---------- common/fc_types.h | 2 ++ common/networking/packets.def | 13 ++++++++----- server/citizenshand.h | 4 +--- server/citytools.c | 25 +++++++++++++++++-------- server/citytools.h | 21 +++++++++++---------- server/diplomats.c | 4 +++- 7 files changed, 62 insertions(+), 37 deletions(-) diff --git a/client/packhand.c b/client/packhand.c index c1385f1c05..a850f47063 100644 --- a/client/packhand.c +++ b/client/packhand.c @@ -777,16 +777,6 @@ void handle_city_info(const struct packet_city_info *packet) city_size_set(pcity, packet->size); } - /* The nationality of the citizens. */ - if (game.info.citizen_nationality) { - citizens_init(pcity); - for (i = 0; i < packet->nationalities_count; i++) { - citizens_nation_set(pcity, player_slot_by_number(packet->nation_id[i]), - packet->nation_citizens[i]); - } - fc_assert(citizens_count(pcity) == city_size_get(pcity)); - } - pcity->history = packet->history; pcity->client.culture = packet->culture; pcity->client.buy_cost = packet->buy_cost; @@ -989,6 +979,26 @@ void handle_web_city_info_addition(int id, int granary_size, { } +/************************************************************************//** + Handle city nationalities packet. +****************************************************************************/ +void handle_city_nationalities(const struct packet_city_nationalities *packet) +{ + struct city *pcity = game_city_by_number(packet->id); + + /* The nationality of the citizens. */ + if (pcity != NULL && game.info.citizen_nationality) { + int i; + + citizens_init(pcity); + for (i = 0; i < packet->nationalities_count; i++) { + citizens_nation_set(pcity, player_slot_by_number(packet->nation_id[i]), + packet->nation_citizens[i]); + } + fc_assert(citizens_count(pcity) == city_size_get(pcity)); + } +} + /************************************************************************//** A helper function for handling city-info and city-short-info packets. Naturally, both require many of the same operations to be done on the diff --git a/common/fc_types.h b/common/fc_types.h index 7e0c8308f9..26e1b6420d 100644 --- a/common/fc_types.h +++ b/common/fc_types.h @@ -80,6 +80,8 @@ extern "C" { /* Line breaks after this number of characters; be carefull and use only 70 */ #define LINE_BREAK 70 +#define MAX_CITY_NATIONALITIES (MIN(MAX_NUM_PLAYER_SLOTS, MAX_CITY_SIZE)) + /* symbol to flag missing numbers for better debugging */ #define IDENTITY_NUMBER_ZERO (0) diff --git a/common/networking/packets.def b/common/networking/packets.def index b7ccad8ff8..7da65571df 100644 --- a/common/networking/packets.def +++ b/common/networking/packets.def @@ -709,10 +709,6 @@ PACKET_CITY_INFO = 31; sc, lsend, is-game-info, force, cancel(PACKET_CITY_SHORT_ UINT8 specialists_size; CITIZENS specialists[SP_MAX:specialists_size]; - UINT8 nationalities_count; - PLAYER nation_id[MAX_NUM_PLAYER_SLOTS:nationalities_count]; - CITIZENS nation_citizens[MAX_NUM_PLAYER_SLOTS:nationalities_count]; - UINT32 history; UINT32 culture; UINT32 buy_cost; @@ -767,7 +763,14 @@ PACKET_CITY_INFO = 31; sc, lsend, is-game-info, force, cancel(PACKET_CITY_SHORT_ UNIT_ORDER rally_point_orders[MAX_LEN_ROUTE:rally_point_length]; end -PACKET_CITY_SHORT_INFO = 32; sc, lsend, is-game-info, cancel(PACKET_CITY_INFO), cancel(PACKET_WEB_CITY_INFO_ADDITION) +PACKET_CITY_NATIONALITIES = 46; sc, lsend, is-game-info, force, handle-via-packet + CITY id; key + UINT8 nationalities_count; + PLAYER nation_id[MAX_CITY_NATIONALITIES:nationalities_count]; + CITIZENS nation_citizens[MAX_CITY_NATIONALITIES:nationalities_count]; +end + +PACKET_CITY_SHORT_INFO = 32; sc, lsend, is-game-info, cancel(PACKET_CITY_INFO), cancel(PACKET_WEB_CITY_INFO_ADDITION), cancel(PACKET_CITY_NATIONALITIES) CITY id; key TILE tile; diff --git a/server/citizenshand.h b/server/citizenshand.h index 31d268cbee..b511af8d37 100644 --- a/server/citizenshand.h +++ b/server/citizenshand.h @@ -15,8 +15,6 @@ struct city; -#define MAX_CITY_NATIONALITIES MIN(MAX_NUM_PLAYER_SLOTS, MAX_CITY_SIZE) - struct citizens_reduction { struct player_slot *pslot; citizens change; @@ -34,4 +32,4 @@ void citizens_reduction_apply(struct city *pcity, void citizens_print(const struct city *pcity); -#endif /* FC__CITIZENSHAND_H */ +#endif /* FC__CITIZENSHAND_H */ diff --git a/server/citytools.c b/server/citytools.c index f3735d152d..8c0d03e28c 100644 --- a/server/citytools.c +++ b/server/citytools.c @@ -2183,6 +2183,7 @@ void refresh_dumb_city(struct city *pcity) void broadcast_city_info(struct city *pcity) { struct packet_city_info packet; + struct packet_city_nationalities nat_packet; struct packet_web_city_info_addition web_packet; struct packet_city_short_info sc_pack; struct player *powner = city_owner(pcity); @@ -2200,13 +2201,14 @@ void broadcast_city_info(struct city *pcity) } routes = traderoute_packet_list_new(); - package_city(pcity, &packet, &web_packet, routes, FALSE); + package_city(pcity, &packet, &nat_packet, &web_packet, routes, FALSE); players_iterate(pplayer) { if (!send_city_suppressed || pplayer != powner) { if (can_player_see_city_internals(pplayer, pcity)) { update_dumb_city(pplayer, pcity); lsend_packet_city_info(pplayer->connections, &packet, FALSE); + lsend_packet_city_nationalities(pplayer->connections, &nat_packet, FALSE); web_lsend_packet(city_info_addition, pplayer->connections, &web_packet, FALSE); traderoute_packet_list_iterate(routes, route_packet) { lsend_packet_traderoute_info(pplayer->connections, route_packet); @@ -2224,6 +2226,7 @@ void broadcast_city_info(struct city *pcity) conn_list_iterate(game.est_connections, pconn) { if (conn_is_global_observer(pconn)) { send_packet_city_info(pconn, &packet, FALSE); + send_packet_city_nationalities(pconn, &nat_packet, FALSE); web_send_packet(city_info_addition, pconn, &web_packet, FALSE); } } conn_list_iterate_end; @@ -2334,6 +2337,7 @@ void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, struct city *pcity, struct tile *ptile) { struct packet_city_info packet; + struct packet_city_nationalities nat_packet; struct packet_web_city_info_addition web_packet; struct packet_city_short_info sc_pack; struct player *powner = NULL; @@ -2361,8 +2365,9 @@ void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, /* Send all info to the owner */ update_dumb_city(powner, pcity); - package_city(pcity, &packet, &web_packet, routes, FALSE); + package_city(pcity, &packet, &nat_packet, &web_packet, routes, FALSE); lsend_packet_city_info(dest, &packet, FALSE); + lsend_packet_city_nationalities(dest, &nat_packet, FALSE); web_lsend_packet(city_info_addition, dest, &web_packet, FALSE); traderoute_packet_list_iterate(routes, route_packet) { lsend_packet_traderoute_info(dest, route_packet); @@ -2385,8 +2390,10 @@ void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, if (pcity) { routes = traderoute_packet_list_new(); - package_city(pcity, &packet, &web_packet, routes, FALSE); /* should be dumb_city info? */ + /* Should be dumb_city info? */ + package_city(pcity, &packet, &nat_packet, &web_packet, routes, FALSE); lsend_packet_city_info(dest, &packet, FALSE); + lsend_packet_city_nationalities(dest, &nat_packet, FALSE); web_lsend_packet(city_info_addition, dest, &web_packet, FALSE); traderoute_packet_list_iterate(routes, route_packet) { lsend_packet_traderoute_info(dest, route_packet); @@ -2421,6 +2428,7 @@ void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, Fill city info packet with information about given city. ****************************************************************************/ void package_city(struct city *pcity, struct packet_city_info *packet, + struct packet_city_nationalities *nat_packet, struct packet_web_city_info_addition *web_packet, struct traderoute_packet_list *routes, bool dipl_invest) @@ -2456,7 +2464,8 @@ void package_city(struct city *pcity, struct packet_city_info *packet, } specialist_type_iterate_end; /* The nationality of the citizens. */ - packet->nationalities_count = 0; + nat_packet->id = pcity->id; + nat_packet->nationalities_count = 0; if (game.info.citizen_nationality) { int cit = 0; @@ -2466,11 +2475,11 @@ void package_city(struct city *pcity, struct packet_city_info *packet, /* This player should exist! */ fc_assert(player_slot_get_player(pslot) != NULL); - packet->nation_id[packet->nationalities_count] + nat_packet->nation_id[nat_packet->nationalities_count] = player_slot_index(pslot); - packet->nation_citizens[packet->nationalities_count] + nat_packet->nation_citizens[nat_packet->nationalities_count] = nationality; - packet->nationalities_count++; + nat_packet->nationalities_count++; cit += nationality; } @@ -2508,7 +2517,7 @@ void package_city(struct city *pcity, struct packet_city_info *packet, /* And repackage */ recursion = TRUE; - package_city(pcity, packet, web_packet, routes, dipl_invest); + package_city(pcity, packet, nat_packet, web_packet, routes, dipl_invest); recursion = FALSE; return; diff --git a/server/citytools.h b/server/citytools.h index 8f336da6d5..6eeae3cd2e 100644 --- a/server/citytools.h +++ b/server/citytools.h @@ -14,7 +14,7 @@ #define FC__CITYTOOLS_H /* common */ -#include "events.h" /* enum event_type */ +#include "events.h" /* enum event_type */ #include "packets.h" #include "unitlist.h" @@ -30,12 +30,12 @@ int build_points_left(struct city *pcity); void transfer_city_units(struct player *pplayer, struct player *pvictim, - struct unit_list *units, struct city *pcity, - struct city *exclude_city, - int kill_outside, bool verbose); + struct unit_list *units, struct city *pcity, + struct city *exclude_city, + int kill_outside, bool verbose); bool transfer_city(struct player *ptaker, struct city *pcity, - int kill_outside, bool transfer_unit_verbose, - bool resolve_stack, bool raze, bool build_free); + int kill_outside, bool transfer_unit_verbose, + bool resolve_stack, bool raze, bool build_free); struct city *find_closest_city(const struct tile *ptile, const struct city *pexclcity, const struct player *pplayer, @@ -47,14 +47,15 @@ bool unit_conquer_city(struct unit *punit, struct city *pcity); bool send_city_suppression(bool now); void send_city_info(struct player *dest, struct city *pcity); void send_city_info_at_tile(struct player *pviewer, struct conn_list *dest, - struct city *pcity, struct tile *ptile); + struct city *pcity, struct tile *ptile); void send_all_known_cities(struct conn_list *dest); void send_player_cities(struct player *pplayer); void broadcast_city_info(struct city *pcity); void package_city(struct city *pcity, struct packet_city_info *packet, + struct packet_city_nationalities *nat_packet, struct packet_web_city_info_addition *web_packet, struct traderoute_packet_list *routes, - bool dipl_invest); + bool dipl_invest); void reality_check_city(struct player *pplayer, struct tile *ptile); bool update_dumb_city(struct player *pplayer, struct city *pcity); @@ -64,7 +65,7 @@ void remove_dumb_city(struct player *pplayer, struct tile *ptile); void city_build_free_buildings(struct city *pcity); void create_city(struct player *pplayer, struct tile *ptile, - const char *name, struct player *nationality); + const char *name, struct player *nationality); bool create_city_for_player(struct player *pplayer, struct tile *ptile, const char *name); void remove_city(struct city *pcity); @@ -93,7 +94,7 @@ void change_build_target(struct player *pplayer, struct city *pcity, enum event_type event); bool is_allowed_city_name(struct player *pplayer, const char *cityname, - char *error_buf, size_t bufsz); + char *error_buf, size_t bufsz); const char *city_name_suggestion(struct player *pplayer, struct tile *ptile); void city_freeze_workers(struct city *pcity); diff --git a/server/diplomats.c b/server/diplomats.c index 9881d5a1b9..1120914d90 100644 --- a/server/diplomats.c +++ b/server/diplomats.c @@ -328,6 +328,7 @@ bool diplomat_investigate(struct player *pplayer, struct unit *pdiplomat, struct player *cplayer; struct packet_unit_short_info unit_packet; struct packet_city_info city_packet; + struct packet_city_nationalities nat_packet; struct packet_web_city_info_addition web_packet; struct traderoute_packet_list *routes; const struct unit_type *act_utype; @@ -375,10 +376,11 @@ bool diplomat_investigate(struct player *pplayer, struct unit *pdiplomat, /* Send city info to investigator's player. As this is a special case we bypass send_city_info. */ routes = traderoute_packet_list_new(); - package_city(pcity, &city_packet, &web_packet, routes, TRUE); + package_city(pcity, &city_packet, &nat_packet, &web_packet, routes, TRUE); /* We need to force to send the packet to ensure the client will receive * something and popup the city dialog. */ lsend_packet_city_info(pplayer->connections, &city_packet, TRUE); + lsend_packet_city_nationalities(pplayer->connections, &nat_packet, TRUE); web_lsend_packet(city_info_addition, pplayer->connections, &web_packet, TRUE); traderoute_packet_list_iterate(routes, route_packet) { lsend_packet_traderoute_info(pplayer->connections, route_packet); -- 2.35.1