Author: sveinung
Date: Tue Apr  5 10:15:37 2016
New Revision: 32304

URL: http://svn.gna.org/viewcvs/freeciv?rev=32304&view=rev
Log:
Store action decision want as an integer.

Save the order of the strings in the action decision enum once. Save only
the order index number for each unit.

Make it mandatory for a unit to have an action decision field.

Requested by Marko Lindqvist <cazfi>

See patch #6863

Modified:
    trunk/common/fc_types.h
    trunk/server/savecompat.c
    trunk/server/savecompat.h
    trunk/server/savegame3.c

Modified: trunk/common/fc_types.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/fc_types.h?rev=32304&r1=32303&r2=32304&view=diff
==============================================================================
--- trunk/common/fc_types.h     (original)
+++ trunk/common/fc_types.h     Tue Apr  5 10:15:37 2016
@@ -731,6 +731,7 @@
 /* Wants a decision because of something the actor did. */
 #define SPECENUM_VALUE2 ACT_DEC_ACTIVE
 #define SPECENUM_VALUE2NAME N_("active")
+#define SPECENUM_COUNT ACT_DEC_COUNT
 #include "specenum_gen.h"
 
 #ifdef __cplusplus

Modified: trunk/server/savecompat.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/savecompat.c?rev=32304&r1=32303&r2=32304&view=diff
==============================================================================
--- trunk/server/savecompat.c   (original)
+++ trunk/server/savecompat.c   Tue Apr  5 10:15:37 2016
@@ -1601,6 +1601,63 @@
   }
 
   secfile_replace_int(loading->file, num_settings, "settings.set_count");
+
+  /* Unit action decision aren't text any more. */
+  if (secfile_section_lookup(loading->file,
+                             "savefile.action_decision_vector") == NULL) {
+    int i;
+
+    /* Write action decision order. */
+    secfile_insert_int(loading->file, ACT_DEC_COUNT,
+                       "savefile.action_decision_size");
+    if (ACT_DEC_COUNT > 0) {
+      const char **modname;
+      int j;
+
+      i = 0;
+      modname = fc_calloc(ACT_DEC_COUNT, sizeof(*modname));
+
+      for (j = 0; j < ACT_DEC_COUNT; j++) {
+        modname[i++] = action_decision_name(j);
+      }
+
+      secfile_insert_str_vec(loading->file, modname,
+                             ACT_DEC_COUNT,
+                             "savefile.action_decision_vector");
+      free(modname);
+    }
+
+    /* Upgrade each unit. */
+    player_slots_iterate(pslot) {
+      int unit;
+      int units_num;
+      int plrno = player_slot_index(pslot);
+
+      if (secfile_section_lookup(loading->file, "player%d", plrno)
+          == NULL) {
+        continue;
+      }
+
+      /* Number of units the player has. */
+      units_num = secfile_lookup_int_default(loading->file, 0,
+                                             "player%d.nunits",
+                                             plrno);
+
+      for (unit = 0; unit < units_num; unit++) {
+        int want;
+
+        /* Load old enum action decision want. */
+        want = secfile_lookup_enum_default(loading->file,
+            ACT_DEC_NOTHING, action_decision,
+            "player%d.u%d.action_decision_want", plrno, unit);
+
+        /* Store the action decision want. */
+        secfile_insert_int(loading->file, want,
+                           "player%d.u%d.action_decision",
+                           plrno, unit);
+      }
+    } player_slots_iterate_end;
+  }
 }
 #endif /* FREECIV_DEV_SAVE_COMPAT */
 

Modified: trunk/server/savecompat.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/savecompat.h?rev=32304&r1=32303&r2=32304&view=diff
==============================================================================
--- trunk/server/savecompat.h   (original)
+++ trunk/server/savecompat.h   Tue Apr  5 10:15:37 2016
@@ -100,6 +100,11 @@
     enum diplstate_type *order;
     size_t size;
   } ds_t;
+  /* loaded in sg_load_savefile(); needed in sg_load_player_unit(), ... */
+  struct {
+    enum action_decision *order;
+    size_t size;
+  } act_dec;
 
   /* loaded in sg_load_game(); needed in sg_load_random(), ... */
   enum server_states server_state;

Modified: trunk/server/savegame3.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/savegame3.c?rev=32304&r1=32303&r2=32304&view=diff
==============================================================================
--- trunk/server/savegame3.c    (original)
+++ trunk/server/savegame3.c    Tue Apr  5 10:15:37 2016
@@ -577,6 +577,8 @@
   loading->multiplier.size = -1;
   loading->specialist.order = NULL;
   loading->specialist.size = -1;
+  loading->act_dec.order = NULL;
+  loading->act_dec.size = -1;
 
   loading->server_state = S_S_INITIAL;
   loading->rstate = fc_rand_state();
@@ -612,6 +614,10 @@
 
   if (loading->specialist.order != NULL) {
     free(loading->specialist.order);
+  }
+
+  if (loading->act_dec.order != NULL) {
+    free(loading->act_dec.order);
   }
 
   if (loading->worked_tiles != NULL) {
@@ -1458,6 +1464,33 @@
     }
   }
 
+  /* Load action decision order. */
+  loading->act_dec.size
+    = secfile_lookup_int_default(loading->file, 0,
+                                 "savefile.action_decision_size");
+
+  sg_failure_ret(loading->act_dec.size > 0,
+                 "Failed to load action decision order: %s",
+                 secfile_error());
+
+  if (loading->act_dec.size) {
+    const char **modname;
+    int j;
+
+    modname = secfile_lookup_str_vec(loading->file, &loading->act_dec.size,
+                                     "savefile.action_decision_vector");
+
+    loading->act_dec.order = fc_calloc(loading->act_dec.size,
+                                       sizeof(*loading->act_dec.order));
+
+    for (j = 0; j < loading->act_dec.size; j++) {
+      loading->act_dec.order[j] = action_decision_by_name(modname[j],
+                                                          fc_strcasecmp);
+    }
+
+    free(modname);
+  }
+
   terrain_type_iterate(pterr) {
     pterr->identifier_load = '\0';
   } terrain_type_iterate_end;
@@ -1706,6 +1739,26 @@
     secfile_insert_str_vec(saving->file, modname,
                            ACTION_COUNT,
                            "savefile.action_vector");
+    free(modname);
+  }
+
+  /* Save action decision order in the savegame. */
+  secfile_insert_int(saving->file, ACT_DEC_COUNT,
+                     "savefile.action_decision_size");
+  if (ACT_DEC_COUNT > 0) {
+    const char **modname;
+    int j;
+
+    i = 0;
+    modname = fc_calloc(ACT_DEC_COUNT, sizeof(*modname));
+
+    for (j = 0; j < ACT_DEC_COUNT; j++) {
+      modname[i++] = action_decision_name(j);
+    }
+
+    secfile_insert_str_vec(saving->file, modname,
+                           ACT_DEC_COUNT,
+                           "savefile.action_decision_vector");
     free(modname);
   }
 
@@ -4996,6 +5049,7 @@
   const char *facing_str;
   enum tile_special_type cfspe;
   int natnbr;
+  int unconverted;
 
   sg_warn_ret_val(secfile_lookup_int(loading->file, &punit->id, "%s.id",
                                      unitstr), FALSE, "%s", secfile_error());
@@ -5261,10 +5315,19 @@
     punit->upkeep[o] = utype_upkeep_cost(unit_type_get(punit), plr, o);
   } output_type_iterate_end;
 
-  punit->action_decision_want
-      = secfile_lookup_enum_default(loading->file,
-                                    ACT_DEC_NOTHING, action_decision,
-                                    "%s.action_decision_want", unitstr);
+  sg_warn_ret_val(secfile_lookup_int(loading->file, &unconverted,
+                                     "%s.action_decision", unitstr),
+                  FALSE, "%s", secfile_error());
+
+  if (unconverted >= 0 && unconverted < loading->act_dec.size) {
+    /* Look up what action decision want the unconverted number
+     * represents. */
+    punit->action_decision_want = loading->act_dec.order[unconverted];
+  } else {
+    log_sg("Invalid action decision want for unit %d", punit->id);
+
+    punit->action_decision_want = ACT_DEC_NOTHING;
+  }
 
   if (punit->action_decision_want != ACT_DEC_NOTHING) {
     /* Load the tile to act against. */
@@ -5624,8 +5687,8 @@
                                      ? unit_transport_get(punit)->id : -1,
                        "%s.transported_by", buf);
 
-    secfile_insert_enum(saving->file, punit->action_decision_want,
-                        action_decision, "%s.action_decision_want", buf);
+    secfile_insert_int(saving->file, punit->action_decision_want,
+                       "%s.action_decision", buf);
 
     /* Stored as tile rather than direction to make sure the target tile is
      * sane. */


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

Reply via email to