Author: sveinung
Date: Sat Oct 24 23:37:46 2015
New Revision: 30204

URL: http://svn.gna.org/viewcvs/freeciv?rev=30204&view=rev
Log:
savegames: use stored diplstate_type order

when loading save games.

See patch #6468

Modified:
    branches/S2_6/server/savecompat.c
    branches/S2_6/server/savecompat.h
    branches/S2_6/server/savegame2.c

Modified: branches/S2_6/server/savecompat.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/savecompat.c?rev=30204&r1=30203&r2=30204&view=diff
==============================================================================
--- branches/S2_6/server/savecompat.c   (original)
+++ branches/S2_6/server/savecompat.c   Sat Oct 24 23:37:46 2015
@@ -993,6 +993,27 @@
     }
     secfile_insert_int(loading->file, count, "research.count");
   }
+
+  /* Add diplstate type order. */
+  secfile_insert_int(loading->file, DS_LAST,
+                     "savefile.diplstate_type_size");
+  if (DS_LAST > 0) {
+    const char **modname;
+    int i;
+    int j;
+
+    i = 0;
+    modname = fc_calloc(DS_LAST, sizeof(*modname));
+
+    for (j = 0; j < DS_LAST; j++) {
+      modname[i++] = diplstate_type_name(j);
+    }
+
+    secfile_insert_str_vec(loading->file, modname,
+                           DS_LAST,
+                           "savefile.diplstate_type_vector");
+    free(modname);
+  }
 }
 
 /****************************************************************************

Modified: branches/S2_6/server/savecompat.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/savecompat.h?rev=30204&r1=30203&r2=30204&view=diff
==============================================================================
--- branches/S2_6/server/savecompat.h   (original)
+++ branches/S2_6/server/savecompat.h   Sat Oct 24 23:37:46 2015
@@ -92,6 +92,11 @@
     struct specialist **order;
     size_t size;
   } specialist;
+  /* loaded in sg_load_savefile(); needed in sg_load_player_main(), ... */
+  struct {
+    enum diplstate_type *order;
+    size_t size;
+  } ds_t;
 
   /* loaded in sg_load_game(); needed in sg_load_random(), ... */
   enum server_states server_state;

Modified: branches/S2_6/server/savegame2.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/savegame2.c?rev=30204&r1=30203&r2=30204&view=diff
==============================================================================
--- branches/S2_6/server/savegame2.c    (original)
+++ branches/S2_6/server/savegame2.c    Sat Oct 24 23:37:46 2015
@@ -676,6 +676,8 @@
   loading->road.size = -1;
   loading->specialist.order = NULL;
   loading->specialist.size = -1;
+  loading->ds_t.order = NULL;
+  loading->ds_t.size = -1;
 
   loading->server_state = S_S_INITIAL;
   loading->rstate = fc_rand_state();
@@ -723,6 +725,10 @@
 
   if (loading->specialist.order != NULL) {
     free(loading->specialist.order);
+  }
+
+  if (loading->ds_t.order != NULL) {
+    free(loading->ds_t.order);
   }
 
   if (loading->worked_tiles != NULL) {
@@ -1800,6 +1806,33 @@
       loading->specialist.order[j] = NULL;
     }
   }
+
+  /* Load diplomatic state type order. */
+  loading->ds_t.size
+    = secfile_lookup_int_default(loading->file, 0,
+                                 "savefile.diplstate_type_size");
+
+  sg_failure_ret(loading->ds_t.size > 0,
+                 "Failed to load diplomatic state type order: %s",
+                 secfile_error());
+
+  if (loading->ds_t.size) {
+    const char **modname;
+    int j;
+
+    modname = secfile_lookup_str_vec(loading->file, &loading->ds_t.size,
+                                     "savefile.diplstate_type_vector");
+
+    loading->ds_t.order = fc_calloc(loading->ds_t.size,
+                                  sizeof(*loading->ds_t.order));
+
+    for (j = 0; j < loading->ds_t.size; j++) {
+      loading->ds_t.order[j] = diplstate_type_by_name(modname[j],
+                                                    fc_strcasecmp);
+    }
+
+    free(modname);
+  }
 }
 
 /****************************************************************************
@@ -3980,16 +4013,37 @@
   BV_CLR_ALL(plr->real_embassy);
   players_iterate(pplayer) {
     char buf[32];
+    int unconverted;
     struct player_diplstate *ds = player_diplstate_get(plr, pplayer);
     i = player_index(pplayer);
 
     /* load diplomatic status */
     fc_snprintf(buf, sizeof(buf), "player%d.diplstate%d", plrno, i);
 
-    ds->type =
-      secfile_lookup_int_default(loading->file, DS_WAR, "%s.type", buf);
-    ds->max_state =
-      secfile_lookup_int_default(loading->file, DS_WAR, "%s.max_state", buf);
+    unconverted =
+      secfile_lookup_int_default(loading->file, -1, "%s.type", buf);
+    if (unconverted >= 0 && unconverted < loading->ds_t.size) {
+      /* Look up what state the unconverted number represents. */
+      ds->type = loading->ds_t.order[unconverted];
+    } else {
+      log_sg("No valid diplomatic state type between players %d and %d",
+             plrno, i);
+
+      ds->type = DS_WAR;
+    }
+
+    unconverted =
+      secfile_lookup_int_default(loading->file, -1, "%s.max_state", buf);
+    if (unconverted >= 0 && unconverted < loading->ds_t.size) {
+      /* Look up what state the unconverted number represents. */
+      ds->max_state = loading->ds_t.order[unconverted];
+    } else {
+      log_sg("No valid diplomatic max_state between players %d and %d",
+             plrno, i);
+
+      ds->max_state = DS_WAR;
+    }
+
     ds->first_contact_turn =
       secfile_lookup_int_default(loading->file, 0,
                                  "%s.first_contact_turn", buf);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to