From 7a94bc631a0c3db3367f600d7e54da4b1882174c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Lach?= Date: Fri, 7 Jan 2022 13:20:47 +0100 Subject: [PATCH] - Implement loading counters data from savegame diff --git a/server/savegame/savecompat.h b/server/savegame/savecompat.h index 7d87ff1640..0fd4aefa71 100644 --- a/server/savegame/savecompat.h +++ b/server/savegame/savecompat.h @@ -49,6 +49,10 @@ struct loaddata { const char *secfile_options; int version; + struct { + const char **order; + size_t size; + } counter; /* loaded in sg_load_savefile(); needed in sg_load_player() */ struct { const char **order; diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 6678113954..69660ce080 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -337,6 +337,7 @@ static void sg_save_scenario(struct savedata *saving); static void sg_load_settings(struct loaddata *loading); static void sg_save_settings(struct savedata *saving); +static void sg_load_counters (struct loaddata * loading); static void sg_save_counters (struct savedata * saving); static void sg_load_map(struct loaddata *loading); @@ -477,6 +478,8 @@ void savegame3_load(struct section_file *file) sg_load_researches(loading); /* [player] */ sg_load_players(loading); + /* [counters] */ + sg_load_counters(loading); /* [event_cache] */ sg_load_event_cache(loading); /* [treaties] */ @@ -2573,6 +2576,57 @@ static void sg_save_settings(struct savedata *saving) /* Add all compatibility settings here. */ } + +/************************************************************************//** +Load '[counters]'. +****************************************************************************/ +static void sg_load_counters(struct loaddata *loading) +{ + struct city *pcity; + int i, j; + size_t length, length2; + int *city_count; + length = secfile_lookup_int_default(loading->file, 0, + "savefile.city_counters_order_size"); + + if (length) { + loading->counter.order = secfile_lookup_str_vec(loading->file, &loading->counter.size, "savefile.city_counters_order_vector"); + + sg_failure_ret(loading->counter.order != 0, + "Failed to load counter's ruleset order: %s", + secfile_error()); + sg_failure_ret(loading->counter.size = length, + "Counter vector in savegame have bad size: %s", + secfile_error()); + } + + int corder[length]; + + for (i = 0; i < length; i++) { + + struct counter *ctg = counter_by_rule_name(loading->counter.order[i]); + corder[i] = counter_index(ctg); + } + + i = 0; + while (NULL != (city_count = + secfile_lookup_int_vec(loading->file, &length2, + "counters.c%d", i))) { + + sg_failure_ret((length2 -1 == (size_t) length), ( "Bad city counters vector size. Should be " SIZE_T_PRINTF ". Is " SIZE_T_PRINTF "." ), length, length2 - 1); + + pcity = game_city_by_number(city_count[0]); + + sg_failure_ret(NULL != pcity, "City with id %d not found. Is savegame malformed? Abort loading.", city_count[0]); + + for (j = 0; j < length; j++) { + + pcity->counter_values[corder[j]] = city_count[j+1]; + } + i++; + } +} + /************************************************************************//** Save [counters]. ****************************************************************************/ -- 2.35.1