From c887339bb6eb32f2d05c74e086e0afcf9bb3f20c Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 22 Oct 2022 01:45:26 +0300
Subject: [PATCH 18/18] savegame: Have as little trade routes padding as needed

See osdn #45855

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 server/savegame3.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/server/savegame3.c b/server/savegame3.c
index 8aea8bf4e0..ab4daeaf50 100644
--- a/server/savegame3.c
+++ b/server/savegame3.c
@@ -4955,7 +4955,7 @@ static void sg_load_player_city_citizens(struct loaddata *loading,
 static void sg_save_player_cities(struct savedata *saving,
                                   struct player *plr)
 {
-  int wlist_max_length = 0;
+  int wlist_max_length = 0, routes_max = 0;
   int i = 0;
   int plrno = player_number(plr);
   bool nations[MAX_NUM_PLAYER_SLOTS];
@@ -4975,6 +4975,8 @@ static void sg_save_player_cities(struct savedata *saving,
 
   /* First determine lenght of longest worklist and the nations we have. */
   city_list_iterate(plr->cities, pcity) {
+    int routes;
+
     /* Check the sanity of the city. */
     city_refresh(pcity);
     sanity_check_city(pcity);
@@ -4983,6 +4985,11 @@ static void sg_save_player_cities(struct savedata *saving,
       wlist_max_length = pcity->worklist.length;
     }
 
+    routes = city_num_trade_routes(pcity);
+    if (routes > routes_max) {
+      routes_max = routes;
+    }
+
     if (game.info.citizen_nationality) {
       /* Find all nations of the citizens,*/
       players_iterate(pplayer) {
@@ -5031,7 +5038,7 @@ static void sg_save_player_cities(struct savedata *saving,
     } trade_routes_iterate_end;
 
     /* Save dummy values to keep tabular format happy */
-    for (; j < MAX_TRADE_ROUTES; j++) {
+    for (; j < routes_max; j++) {
       secfile_insert_int(saving->file, 0, "%s.traderoute%d", buf, j);
       secfile_insert_str(saving->file, route_direction_name(RDIR_BIDIRECTIONAL),
                          "%s.route_direction%d", buf, j);
-- 
2.35.1