From f50a3f071f328e216a81821c8c303c11cbcbe142 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 19 Mar 2023 23:02:06 +0200 Subject: [PATCH 20/20] Savegame: Fix unused entry warnings about unit orders vectors See osdn #47498 Signed-off-by: Marko Lindqvist --- server/savegame/savegame2.c | 6 +++-- server/savegame/savegame3.c | 47 +++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/server/savegame/savegame2.c b/server/savegame/savegame2.c index 4e7b98f6ca..07f48f9399 100644 --- a/server/savegame/savegame2.c +++ b/server/savegame/savegame2.c @@ -3865,7 +3865,6 @@ static bool sg_load_player_unit(struct loaddata *loading, struct player *plr, struct unit *punit, const char *unitstr) { - int j; enum unit_activity activity; int nat_x, nat_y; enum tile_special_type target; @@ -4310,10 +4309,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; @@ -4321,6 +4321,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; @@ -4354,6 +4355,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 26aa53326f..6db798b22b 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -368,6 +368,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); @@ -5687,6 +5688,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(); @@ -5699,6 +5701,16 @@ static void sg_load_player_units(struct loaddata *loading, nunits = 0; /* Some old savegames may be buggy. */ } + orders_max_length = 0; + + for (i = 0; i < nunits; i++) { + int ol_length = secfile_lookup_int_default(loading->file, 0, + "player%d.u%d.orders_length", + plrno, i); + + orders_max_length = MAX(orders_max_length, ol_length); + } + for (i = 0; i < nunits; i++) { struct unit *punit; struct city *pcity; @@ -5715,7 +5727,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); } @@ -5749,9 +5761,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; @@ -6013,12 +6025,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; @@ -6043,6 +6057,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; @@ -6143,6 +6158,7 @@ static bool sg_load_player_unit(struct loaddata *loading, break; } } + if (order->order == ORDER_ACTIVITY || action_wants_extra) { enum unit_activity act; @@ -6181,7 +6197,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; @@ -6194,6 +6221,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); + } } } @@ -6262,12 +6298,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. */ @@ -6285,7 +6322,7 @@ static void sg_save_player_units(struct savedata *saving, 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