Structurize the informaruin about dwarves.
[open-adventure.git] / saveresume.c
index 0ed8cd37c56a538b1d0834e03474e78c0b091756..d66b52c928285b52bb88d12f41bebc8234054a1a 100644 (file)
 #include "advent.h"
 #include "dungeon.h"
 
+/*
+ * Use this to detect endianness mismatch.  Can't be unchanged by byte-swapping.
+ */
+#define ENDIAN_MAGIC   2317
+
 struct save_t save;
 
 #define IGNORE(r) do{if (r){}}while(0)
 
-int savefile(FILE *fp, int32_t version)
+int savefile(FILE *fp)
 /* Save game to file. No input or output from user. */
 {
     memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC));
-    save.version = (version == 0) ? SAVE_VERSION : version;
+    if (save.version == 0)
+       save.version = SAVE_VERSION;
+    if (save.canary == 0)
+       save.canary = ENDIAN_MAGIC;
 
     save.game = game;
     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
@@ -84,7 +92,7 @@ int suspend(void)
         free(name);
     }
 
-    savefile(fp, SAVE_VERSION);
+    savefile(fp);
     fclose(fp);
     rspeak(RESUME_HELP);
     exit(EXIT_SUCCESS);
@@ -135,7 +143,7 @@ int restore(FILE* fp)
 
     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
     fclose(fp);
-    if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0)
+    if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0 || save.canary != ENDIAN_MAGIC)
        rspeak(BAD_SAVE);
     else if (save.version != SAVE_VERSION) {
         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10));
@@ -182,8 +190,8 @@ bool is_valid(struct game_t valgame)
     }
     /*  Bounds check for location arrays */
     for (int i = 0; i <= NDWARVES; i++) {
-        if (valgame.dloc[i]  < -1 || valgame.dloc[i]  > NLOCATIONS  ||
-            valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
+        if (valgame.dwarves[i].loc  < -1 || valgame.dwarves[i].loc  > NLOCATIONS  ||
+            valgame.dwarves[i].oldloc < -1 || valgame.dwarves[i].oldloc > NLOCATIONS) {
             return false;      // LCOV_EXCL_LINE
         }
     }