From 53f267840db6d1652b00a2f699359e588d2de11a Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 26 Aug 2023 13:36:50 +0300 Subject: [PATCH 6/6] Make player got_first_city a flag See osdn #48292 Signed-off-by: Marko Lindqvist --- common/player.h | 9 ++--- server/barbarian.c | 2 +- server/citytools.c | 9 +++-- server/edithand.c | 2 +- server/plrhand.c | 4 +- server/sanitycheck.c | 4 +- server/savegame/savecompat.c | 72 +++++++++++++++++++++++++++++++++++- server/savegame/savegame2.c | 12 ++---- server/savegame/savegame3.c | 15 +------- 9 files changed, 91 insertions(+), 38 deletions(-) diff --git a/common/player.h b/common/player.h index 681a470c18..4d06f73d6a 100644 --- a/common/player.h +++ b/common/player.h @@ -60,6 +60,9 @@ enum plrcolor_mode { #define SPECENUM_VALUE0NAME "ai" #define SPECENUM_VALUE1 PLRF_SCENARIO_RESERVED #define SPECENUM_VALUE1NAME "ScenarioReserved" +/* TRUE if player has ever had a city. */ +#define SPECENUM_VALUE2 PLRF_FIRST_CITY +#define SPECENUM_VALUE2NAME "FirstCity" #define SPECENUM_COUNT PLRF_COUNT #define SPECENUM_BITVECTOR bv_plr_flags #include "specenum_gen.h" @@ -326,10 +329,6 @@ struct player /* Only used in the server (./ai/ and ./server/). */ bv_pstatus status; - bool got_first_city; /* used to give player init_buildings in first - * city. Once set, never becomes unset. - * (Previously 'capital'.) */ - struct player_tile *private_map; /* Player can see inside their borders. */ @@ -599,4 +598,4 @@ static inline bool player_is_cpuhog(const struct player *pplayer) } #endif /* __cplusplus */ -#endif /* FC__PLAYER_H */ +#endif /* FC__PLAYER_H */ diff --git a/server/barbarian.c b/server/barbarian.c index bbe2b66997..2745ab4bff 100644 --- a/server/barbarian.c +++ b/server/barbarian.c @@ -167,7 +167,7 @@ struct player *create_barbarian_player(enum barbarian_type type) barbarians->is_connected = FALSE; barbarians->government = init_government_of_nation(nation); fc_assert(barbarians->revolution_finishes < 0); - barbarians->server.got_first_city = FALSE; + BV_CLR(barbarians->flags, PLRF_FIRST_CITY); barbarians->economic.gold = 100; barbarians->phase_done = TRUE; diff --git a/server/citytools.c b/server/citytools.c index f6ff01648f..9869cc9638 100644 --- a/server/citytools.c +++ b/server/citytools.c @@ -1275,8 +1275,9 @@ bool transfer_city(struct player *ptaker, struct city *pcity, * them a palace etc */ if (build_free) { city_build_free_buildings(pcity); - } /* else caller should probably ensure palace is built */ - ptaker->server.got_first_city = TRUE; + } /* Else caller should probably ensure palace is built */ + + BV_SET(ptaker->flags, PLRF_FIRST_CITY); } citizens_convert_conquest(pcity); @@ -1439,7 +1440,7 @@ void city_build_free_buildings(struct city *pcity) /* If this isn't the first city a player has ever had, they only get * any initial buildings with the SaveSmallWonder flag, and then only * if savepalace is enabled. */ - first_city = !pplayer->server.got_first_city; + first_city = !player_has_flag(pplayer, PLRF_FIRST_CITY); has_small_wonders = FALSE; has_great_wonders = FALSE; @@ -1549,7 +1550,7 @@ void create_city(struct player *pplayer, struct tile *ptile, /* Free initial buildings, or at least a palace if they were * previously careless enough to lose all their cities */ city_build_free_buildings(pcity); - pplayer->server.got_first_city = TRUE; + BV_SET(pplayer->flags, PLRF_FIRST_CITY); } /* Set up citizens nationality. */ diff --git a/server/edithand.c b/server/edithand.c index 71ae595b80..01376a0653 100644 --- a/server/edithand.c +++ b/server/edithand.c @@ -954,7 +954,7 @@ void handle_edit_player_create(struct connection *pc, int tag) pplayer->unassigned_user = TRUE; pplayer->is_connected = FALSE; pplayer->government = init_government_of_nation(pnation); - pplayer->server.got_first_city = FALSE; + BV_CLR(pplayer->flags, PLRF_FIRST_CITY); pplayer->economic.gold = 0; pplayer->economic.infra_points = 0; diff --git a/server/plrhand.c b/server/plrhand.c index 422245ff62..7b0ded47c8 100644 --- a/server/plrhand.c +++ b/server/plrhand.c @@ -1606,7 +1606,7 @@ void server_player_init(struct player *pplayer, bool initmap, { player_status_reset(pplayer); - pplayer->server.got_first_city = FALSE; + BV_CLR(pplayer->flags, PLRF_FIRST_CITY); BV_CLR_ALL(pplayer->server.really_gives_vision); BV_CLR_ALL(pplayer->server.debug); @@ -2775,7 +2775,7 @@ static struct player *split_player(struct player *pplayer) cplayer->government = init_government_of_nation(nation_of_player(cplayer)); fc_assert(cplayer->revolution_finishes < 0); /* No capital for the splitted player. */ - cplayer->server.got_first_city = FALSE; + BV_CLR(cplayer->flags, PLRF_FIRST_CITY); players_iterate(other_player) { struct player_diplstate *ds_co diff --git a/server/sanitycheck.c b/server/sanitycheck.c index 95bd54103b..62ff9f5434 100644 --- a/server/sanitycheck.c +++ b/server/sanitycheck.c @@ -523,8 +523,8 @@ static void check_players(const char *file, const char *function, int line) SANITY_CHECK(!pplayer->nation || pplayer->nation->player == pplayer); SANITY_CHECK(player_list_search(team_members(pplayer->team), pplayer)); - SANITY_CHECK(!(city_list_size(pplayer->cities) > 0 - && !pplayer->server.got_first_city)); + SANITY_CHECK(player_has_flag(pplayer, PLRF_FIRST_CITY) + || city_list_size(pplayer->cities) == 0); city_list_iterate(pplayer->cities, pcity) { if (pcity->capital == CAPITAL_PRIMARY) { diff --git a/server/savegame/savecompat.c b/server/savegame/savecompat.c index f598337a66..5a414e5e10 100644 --- a/server/savegame/savecompat.c +++ b/server/savegame/savecompat.c @@ -2289,8 +2289,8 @@ static void compat_load_030200(struct loaddata *loading, bool got_tech = FALSE; int bulbs = 0; bool got_tech_multi - = secfile_lookup_bool_default(loading->file, FALSE, - "research.r%d.got_tech_multi", i); + = secfile_lookup_bool_default(loading->file, FALSE, + "research.r%d.got_tech_multi", i); if (secfile_lookup_bool(loading->file, &got_tech, "research.r%d.got_tech", i) @@ -2322,11 +2322,39 @@ static void compat_load_030200(struct loaddata *loading, int ncities; int cnro; size_t wlist_max_length = 0; + bool first_city; if (secfile_section_lookup(loading->file, "player%d", plrno) == NULL) { continue; } + first_city = secfile_lookup_bool_default(loading->file, FALSE, + "player%d.got_first_city", + plrno); + if (first_city) { + const char **flag_names = fc_calloc(PLRF_COUNT, sizeof(char *)); + int flagcount = 0; + const char **flags_sg; + size_t nval; + + flag_names[flagcount++] = plr_flag_id_name(PLRF_FIRST_CITY); + + flags_sg = secfile_lookup_str_vec(loading->file, &nval, + "player%d.flags", plrno); + + for (i = 0; i < nval; i++) { + enum plr_flag_id fid = plr_flag_id_by_name(flags_sg[i], + fc_strcasecmp); + + flag_names[flagcount++] = plr_flag_id_name(fid); + } + + secfile_replace_str_vec(loading->file, flag_names, flagcount, + "player%d.flags", plrno); + + free(flag_names); + } + ncities = secfile_lookup_int_default(loading->file, 0, "player%d.ncities", plrno); @@ -3054,6 +3082,46 @@ static void compat_load_dev(struct loaddata *loading) } } + { + player_slots_iterate(pslot) { + int plrno = player_slot_index(pslot); + bool first_city; + + if (secfile_section_lookup(loading->file, "player%d", plrno) == NULL) { + continue; + } + + first_city = secfile_lookup_bool_default(loading->file, FALSE, + "player%d.got_first_city", + plrno); + + if (first_city) { + const char **flag_names = fc_calloc(PLRF_COUNT, sizeof(char *)); + int flagcount = 0; + const char **flags_sg; + size_t nval; + int i; + + flag_names[flagcount++] = plr_flag_id_name(PLRF_FIRST_CITY); + + flags_sg = secfile_lookup_str_vec(loading->file, &nval, + "player%d.flags", plrno); + + for (i = 0; i < nval; i++) { + enum plr_flag_id fid = plr_flag_id_by_name(flags_sg[i], + fc_strcasecmp); + + flag_names[flagcount++] = plr_flag_id_name(fid); + } + + secfile_replace_str_vec(loading->file, flag_names, flagcount, + "player%d.flags", plrno); + + free(flag_names); + } + } player_slots_iterate_end; + } + } /* Version < 3.1.94 */ #endif /* FREECIV_DEV_SAVE_COMPAT_3_2 */ diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index 21533d28e8..77bd0df547 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -2960,11 +2960,6 @@ static void sg_load_player_main(struct loaddata *loading, = secfile_lookup_int_default(loading->file, -1, "player%d.revolution_finishes", plrno); - sg_failure_ret(secfile_lookup_bool(loading->file, - &plr->server.got_first_city, - "player%d.got_first_city", plrno), - "%s", secfile_error()); - /* Load diplomatic data (diplstate + embassy + vision). * Shared vision is loaded in sg_load_players(). */ BV_CLR_ALL(plr->real_embassy); @@ -3408,13 +3403,14 @@ static void sg_load_player_cities(struct loaddata *loading, ncities = 0; } - if (!plr->server.got_first_city && ncities > 0) { + if (!player_has_flag(plr, PLRF_FIRST_CITY) && ncities > 0) { /* Probably barbarians in an old savegame; fix up */ - plr->server.got_first_city = TRUE; + BV_SET(plr->flags, PLRF_FIRST_CITY); } wlist_max_length = secfile_lookup_int_default(loading->file, 0, - "player%d.wl_max_length", plrno); + "player%d.wl_max_length", + plrno); /* Load all cities of the player. */ for (i = 0; i < ncities; i++) { diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 54bdaa008b..6d25099501 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -4132,11 +4132,6 @@ static void sg_load_player_main(struct loaddata *loading, = secfile_lookup_int_default(loading->file, -1, "player%d.revolution_finishes", plrno); - sg_failure_ret(secfile_lookup_bool(loading->file, - &plr->server.got_first_city, - "player%d.got_first_city", plrno), - "%s", secfile_error()); - /* Load diplomatic data (diplstate + embassy + vision). * Shared vision is loaded in sg_load_players(). */ BV_CLR_ALL(plr->real_embassy); @@ -4735,8 +4730,6 @@ static void sg_save_player_main(struct savedata *saving, "player%d.achievement_count", plrno); } - secfile_insert_bool(saving->file, plr->server.got_first_city, - "player%d.got_first_city", plrno); secfile_insert_int(saving->file, plr->revolution_finishes, "player%d.revolution_finishes", plrno); @@ -4891,13 +4884,9 @@ static void sg_load_player_cities(struct loaddata *loading, ncities = 0; } - if (!plr->server.got_first_city && ncities > 0) { - /* Probably barbarians in an old savegame; fix up */ - plr->server.got_first_city = TRUE; - } - wlist_max_length = secfile_lookup_int_default(loading->file, 0, - "player%d.wl_max_length", plrno); + "player%d.wl_max_length", + plrno); routes_max = secfile_lookup_int_default(loading->file, 0, "player%d.routes_max_length", plrno); -- 2.40.1