From d6f575ec5d9ce67e10e8293a2c7402146cee5b71 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 19 Mar 2023 22:54:24 +0200 Subject: [PATCH 23/23] Savegame: Fix unused entry warnings about unit orders vectors See osdn #47498 Signed-off-by: Marko Lindqvist --- server/savegame/savecompat.c | 49 +++++++++++++++++++++++++++++------- server/savegame/savegame2.c | 6 +++-- server/savegame/savegame3.c | 44 ++++++++++++++++++++++++++++---- 3 files changed, 83 insertions(+), 16 deletions(-) diff --git a/server/savegame/savecompat.c b/server/savegame/savecompat.c index a9f02ded05..fe3f41da12 100644 --- a/server/savegame/savecompat.c +++ b/server/savegame/savecompat.c @@ -2277,16 +2277,17 @@ static void compat_load_030200(struct loaddata *loading, { player_slots_iterate(pslot) { int plrno = player_slot_index(pslot); - int ncities; - int cnro; - int wlist_max_length = 0; + int ncities, nunits; + int cnro, unro; + size_t wlist_max_length = 0; + size_t olist_max_length = 0; if (secfile_section_lookup(loading->file, "player%d", plrno) == NULL) { continue; } ncities = secfile_lookup_int_default(loading->file, 0, - "player%d.ncities", plrno); + "player%d.ncities", plrno); for (cnro = 0; cnro < ncities; cnro++) { int wl_length = secfile_lookup_int_default(loading->file, 0, @@ -2299,6 +2300,20 @@ static void compat_load_030200(struct loaddata *loading, secfile_insert_int(loading->file, wlist_max_length, "player%d.wl_max_length", plrno); + nunits = secfile_lookup_int_default(loading->file, 0, + "player%d.nunits", plrno); + + for (unro = 0; unro < nunits; unro++) { + int ol_length = secfile_lookup_int_default(loading->file, 0, + "player%d.u%d.orders_length", + plrno, unro); + + olist_max_length = MAX(olist_max_length, ol_length); + } + + secfile_insert_int(loading->file, olist_max_length, + "player%d.orders_max_length", plrno); + if (format_class == SAVEGAME_3) { secfile_insert_int(loading->file, MAX_TRADE_ROUTES_OLD, "player%d.routes_max_length", plrno); @@ -2638,13 +2653,14 @@ static void compat_load_dev(struct loaddata *loading) (void) secfile_entry_lookup(loading->file, "game.hardcoded_counters"); - /* Add wl_max_length entries for players */ + /* Add wl_max_length and orders_max_length entries for players */ { player_slots_iterate(pslot) { int plrno = player_slot_index(pslot); - int ncities; - int cnro; - int wlist_max_length = 0; + int ncities, nunits; + int cnro, unro; + size_t wlist_max_length = 0; + size_t olist_max_length = 0; if (secfile_section_lookup(loading->file, "player%d", plrno) == NULL) { continue; @@ -2663,9 +2679,24 @@ static void compat_load_dev(struct loaddata *loading) secfile_insert_int(loading->file, wlist_max_length, "player%d.wl_max_length", plrno); + + nunits = secfile_lookup_int_default(loading->file, 0, + "player%d.nunits", plrno); + + for (unro = 0; unro < nunits; unro++) { + int ol_length + = secfile_lookup_int_default(loading->file, 0, + "player%d.u%d.orders_length", + plrno, unro); + + olist_max_length = MAX(olist_max_length, ol_length); + } + + secfile_insert_int(loading->file, olist_max_length, + "player%d.orders_max_length", plrno); + secfile_insert_int(loading->file, MAX_TRADE_ROUTES_OLD, "player%d.routes_max_length", plrno); - } player_slots_iterate_end; } diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index c06ded2e11..e7463c2267 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -3918,7 +3918,6 @@ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, struct unit *punit, const char *unitstr) { - int j; int activity; int nat_x, nat_y; enum tile_special_type target; @@ -4363,10 +4362,11 @@ static bool sg_load_player_unit(struct loaddata *loading, punit->action_decision_tile = NULL; } - /* load the unit orders */ + /* Load the unit orders */ { int len = secfile_lookup_int_default(loading->file, 0, "%s.orders_length", unitstr); + if (len > 0) { const char *orders_unitstr, *dir_unitstr, *act_unitstr; const char *tgt_unitstr; @@ -4374,6 +4374,7 @@ static bool sg_load_player_unit(struct loaddata *loading, const char *road_unitstr = NULL; int road_idx = road_number(road_by_compat_special(ROCO_ROAD)); int rail_idx = road_number(road_by_compat_special(ROCO_RAILROAD)); + int j; punit->orders.list = fc_malloc(len * sizeof(*(punit->orders.list))); punit->orders.length = len; @@ -4407,6 +4408,7 @@ static bool sg_load_player_unit(struct loaddata *loading, } punit->has_orders = TRUE; + for (j = 0; j < len; j++) { struct unit_order *order = &punit->orders.list[j]; diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index 06737e86e9..0b1856349c 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -373,6 +373,7 @@ static void sg_load_player_units(struct loaddata *loading, struct player *plr); static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, struct unit *punit, + int orders_max_length, const char *unitstr); static void sg_load_player_units_transport(struct loaddata *loading, struct player *plr); @@ -5841,6 +5842,7 @@ static void sg_load_player_units(struct loaddata *loading, struct player *plr) { int nunits, i, plrno = player_number(plr); + size_t orders_max_length; /* Check status and return if not OK (sg_success FALSE). */ sg_check_ret(); @@ -5853,6 +5855,10 @@ static void sg_load_player_units(struct loaddata *loading, nunits = 0; /* Some old savegames may be buggy. */ } + orders_max_length = secfile_lookup_int_default(loading->file, 0, + "player%d.orders_max_length", + plrno); + for (i = 0; i < nunits; i++) { struct unit *punit; struct city *pcity; @@ -5869,7 +5875,7 @@ static void sg_load_player_units(struct loaddata *loading, /* Create a dummy unit. */ punit = unit_virtual_create(plr, NULL, type, 0); - if (!sg_load_player_unit(loading, plr, punit, buf)) { + if (!sg_load_player_unit(loading, plr, punit, orders_max_length, buf)) { unit_virtual_destroy(punit); sg_failure_ret(FALSE, "Error loading unit %d of player %d.", i, plrno); } @@ -5903,9 +5909,9 @@ static void sg_load_player_units(struct loaddata *loading, ****************************************************************************/ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, struct unit *punit, + int orders_max_length, const char *unitstr) { - int j; enum unit_activity activity; int nat_x, nat_y; struct extra_type *pextra = NULL; @@ -6167,12 +6173,14 @@ static bool sg_load_player_unit(struct loaddata *loading, punit->stay = secfile_lookup_bool_default(loading->file, FALSE, "%s.stay", unitstr); - /* load the unit orders */ + /* Load the unit orders */ { int len = secfile_lookup_int_default(loading->file, 0, "%s.orders_length", unitstr); + if (len > 0) { const char *orders_unitstr, *dir_unitstr, *act_unitstr; + int j; punit->orders.list = fc_malloc(len * sizeof(*(punit->orders.list))); punit->orders.length = len; @@ -6197,6 +6205,7 @@ static bool sg_load_player_unit(struct loaddata *loading, "%s.activity_list", unitstr); punit->has_orders = TRUE; + for (j = 0; j < len; j++) { struct unit_order *order = &punit->orders.list[j]; bool action_wants_extra = FALSE; @@ -6297,6 +6306,7 @@ static bool sg_load_player_unit(struct loaddata *loading, break; } } + if (order->order == ORDER_ACTIVITY || action_wants_extra) { enum unit_activity act; @@ -6335,7 +6345,18 @@ static bool sg_load_player_unit(struct loaddata *loading, order->sub_target = NO_TARGET; } } + + for (; j < orders_max_length; j++) { + (void) secfile_entry_lookup(loading->file, + "%s.action_vec,%d", unitstr, j); + (void) secfile_entry_lookup(loading->file, + "%s.tgt_vec,%d", unitstr, j); + (void) secfile_entry_lookup(loading->file, + "%s.sub_tgt_vec,%d", unitstr, j); + } } else { + int j; + punit->has_orders = FALSE; punit->orders.list = NULL; @@ -6348,6 +6369,15 @@ static bool sg_load_player_unit(struct loaddata *loading, (void) secfile_entry_lookup(loading->file, "%s.action_vec", unitstr); (void) secfile_entry_lookup(loading->file, "%s.tgt_vec", unitstr); (void) secfile_entry_lookup(loading->file, "%s.sub_tgt_vec", unitstr); + + for (j = 1; j < orders_max_length; j++) { + (void) secfile_entry_lookup(loading->file, + "%s.action_vec,%d", unitstr, j); + (void) secfile_entry_lookup(loading->file, + "%s.tgt_vec,%d", unitstr, j); + (void) secfile_entry_lookup(loading->file, + "%s.sub_tgt_vec,%d", unitstr, j); + } } } @@ -6416,12 +6446,13 @@ static void sg_save_player_units(struct savedata *saving, { int i = 0; int longest_order = 0; + int plrno = player_number(plr); /* Check status and return if not OK (sg_success FALSE). */ sg_check_ret(); secfile_insert_int(saving->file, unit_list_size(plr->units), - "player%d.nunits", player_number(plr)); + "player%d.nunits", plrno); /* Find the longest unit order so different order length won't break * storing units in the tabular format. */ @@ -6433,13 +6464,16 @@ static void sg_save_player_units(struct savedata *saving, } } unit_list_iterate_end; + secfile_insert_int(saving->file, longest_order, + "player%d.orders_max_length", plrno); + unit_list_iterate(plr->units, punit) { char buf[32]; char dirbuf[2] = " "; int nat_x, nat_y; int last_order, j; - fc_snprintf(buf, sizeof(buf), "player%d.u%d", player_number(plr), i); + fc_snprintf(buf, sizeof(buf), "player%d.u%d", plrno, i); dirbuf[0] = dir2char(punit->facing); secfile_insert_int(saving->file, punit->id, "%s.id", buf); -- 2.39.2