From f924ff6837694db02ef5d9ac74448922d83bfd9f Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 24 Aug 2022 21:24:34 +0300 Subject: [PATCH 18/48] Savegame: Use saved city_options order See osdn #44960 Signed-off-by: Marko Lindqvist --- server/savegame/savecompat.h | 4 ++++ server/savegame/savegame2.c | 45 ++++++++++++++++++++++++++++++++++-- server/savegame/savegame3.c | 38 ++++++++++++++++++++++++++++-- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/server/savegame/savecompat.h b/server/savegame/savecompat.h index d7f33880fc..2afc4951d1 100644 --- a/server/savegame/savecompat.h +++ b/server/savegame/savecompat.h @@ -127,6 +127,10 @@ struct loaddata { enum server_side_agent *order; size_t size; } ssa; + struct { + enum city_options *order; + size_t size; + } coptions; /* loaded in sg_load_game(); needed in sg_load_random(), ... */ enum server_states server_state; diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index f11c8d035d..10e2d18e6a 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -480,6 +480,8 @@ static struct loaddata *loaddata_new(struct section_file *file) loading->specialist.size = -1; loading->ds_t.order = NULL; loading->ds_t.size = -1; + loading->coptions.order = NULL; + loading->coptions.size = -1; loading->server_state = S_S_INITIAL; loading->rstate = fc_rand_state(); @@ -537,6 +539,10 @@ static void loaddata_destroy(struct loaddata *loading) free(loading->ds_t.order); } + if (loading->coptions.order != NULL) { + free(loading->coptions.order); + } + if (loading->worked_tiles != NULL) { free(loading->worked_tiles); } @@ -1454,6 +1460,41 @@ static void sg_load_savefile(struct loaddata *loading) free(modname); } + /* Load city options order. */ + loading->coptions.size + = secfile_lookup_int_default(loading->file, 0, + "savefile.city_options_size"); + + { + const char *modname_old[] = { "Disband", "Sci_Specialists", "Tax_Specialists" }; + const char **modname; + int j; + bool compat; + + if (loading->coptions.size > 0) { + modname = secfile_lookup_str_vec(loading->file, &loading->coptions.size, + "savefile.city_options_vector"); + compat = FALSE; + } else { + modname = modname_old; + loading->coptions.size = 3; + compat = TRUE; + } + + loading->coptions.order = fc_calloc(loading->coptions.size, + sizeof(*loading->coptions.order)); + + for (j = 0; j < loading->coptions.size; j++) { + loading->coptions.order[j] = city_options_by_name(modname[j], + fc_strcasecmp); + } + + if (!compat) { + free(modname); + } + } + + /* Terrain identifiers */ terrain_type_iterate(pterr) { pterr->identifier_load = '\0'; } terrain_type_iterate_end; @@ -3620,10 +3661,10 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, /* Load city options. */ BV_CLR_ALL(pcity->city_options); - for (i = 0; i < CITYO_LAST; i++) { + for (i = 0; i < loading->coptions.size; i++) { if (secfile_lookup_bool_default(loading->file, FALSE, "%s.option%d", citystr, i)) { - BV_SET(pcity->city_options, i); + BV_SET(pcity->city_options, loading->coptions.order[i]); } } diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 2e142f0276..5d8cf08309 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -596,6 +596,8 @@ static struct loaddata *loaddata_new(struct section_file *file) loading->act_dec.size = -1; loading->ssa.order = NULL; loading->ssa.size = -1; + loading->coptions.order = NULL; + loading->coptions.size = -1; loading->server_state = S_S_INITIAL; loading->rstate = fc_rand_state(); @@ -649,6 +651,10 @@ static void loaddata_destroy(struct loaddata *loading) free(loading->ssa.order); } + if (loading->coptions.order != NULL) { + free(loading->coptions.order); + } + if (loading->worked_tiles != NULL) { free(loading->worked_tiles); } @@ -1610,6 +1616,34 @@ static void sg_load_savefile(struct loaddata *loading) free(modname); } + /* Load city options order. */ + loading->coptions.size + = secfile_lookup_int_default(loading->file, 0, + "savefile.city_options_size"); + + sg_failure_ret(loading->coptions.size > 0, + "Failed to load city options order: %s", + secfile_error()); + + if (loading->coptions.size) { + const char **modname; + int j; + + modname = secfile_lookup_str_vec(loading->file, &loading->coptions.size, + "savefile.city_options_vector"); + + loading->coptions.order = fc_calloc(loading->coptions.size, + sizeof(*loading->coptions.order)); + + for (j = 0; j < loading->coptions.size; j++) { + loading->coptions.order[j] = city_options_by_name(modname[j], + fc_strcasecmp); + } + + free(modname); + } + + /* Terrain identifiers */ terrain_type_iterate(pterr) { pterr->identifier_load = '\0'; } terrain_type_iterate_end; @@ -5135,10 +5169,10 @@ static bool sg_load_player_city(struct loaddata *loading, struct player *plr, /* Load city options. */ BV_CLR_ALL(pcity->city_options); - for (i = 0; i < CITYO_LAST; i++) { + for (i = 0; i < loading->coptions.size; i++) { if (secfile_lookup_bool_default(loading->file, FALSE, "%s.option%d", citystr, i)) { - BV_SET(pcity->city_options, i); + BV_SET(pcity->city_options, loading->coptions.order[i]); } } -- 2.35.1