Ready for 1.8 release.
[open-adventure.git] / saveresume.c
1 /*
2  * Saving and resuming.
3  *
4  * (ESR) This replaces  a bunch of particularly nasty FORTRAN-derived code;
5  * see the history.adoc file in the source distribution for discussion.
6  *
7  * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
8  * Copyright (c) 2017 by Eric S. Raymond
9  * SPDX-License-Identifier: BSD-2-clause
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <editline/readline.h>
15 #include <time.h>
16 #include <inttypes.h>
17
18 #include "advent.h"
19 #include "dungeon.h"
20
21 /*
22  * Bump on save format change.
23  *
24  * Note: Verify that the tests run clean before bumping this, then rebuild the check
25  * files afterwards.  Otherwise you will get a spurious failure due to the old version
26  * having been generated into a check file.
27  */
28 #define VRSION  29
29
30 /*
31  * If you change the first three members, the resume function may not properly
32  * reject saves from older versions.  Yes, this glues us to a hardware-
33  * dependent length of int.  Later members can change, but bump the version
34  * when you do that.
35  */
36 struct save_t {
37     int64_t savetime;
38     int32_t mode;               /* not used, must be present for version detection */
39     int32_t version;
40     struct game_t game;
41 };
42 struct save_t save;
43
44 #define IGNORE(r) do{if (r){}}while(0)
45
46 int savefile(FILE *fp, int32_t version)
47 /* Save game to file. No input or output from user. */
48 {
49     save.savetime = time(NULL);
50     save.mode = -1;
51     save.version = (version == 0) ? VRSION : version;
52
53     save.game = game;
54     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
55     return (0);
56 }
57
58 /* Suspend and resume */
59 int suspend(void)
60 {
61     /*  Suspend.  Offer to save things in a file, but charging
62      *  some points (so can't win by using saved games to retry
63      *  battles or to start over after learning zzword).
64      *  If ADVENT_NOSAVE is defined, do nothing instead. */
65
66 #ifdef ADVENT_NOSAVE
67     return GO_UNKNOWN;
68 #endif
69     FILE *fp = NULL;
70
71     rspeak(SUSPEND_WARNING);
72     if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
73         return GO_CLEAROBJ;
74     game.saved = game.saved + 5;
75
76     while (fp == NULL) {
77         char* name = readline("\nFile name: ");
78         if (name == NULL)
79             return GO_TOP;
80         fp = fopen(name, WRITE_MODE);
81         if (fp == NULL)
82             printf("Can't open file %s, try again.\n", name);
83         free(name);
84     }
85
86     savefile(fp, VRSION);
87     fclose(fp);
88     rspeak(RESUME_HELP);
89     exit(EXIT_SUCCESS);
90 }
91
92 int resume(void)
93 {
94     /*  Resume.  Read a suspended game back from a file.
95      *  If ADVENT_NOSAVE is defined, do nothing instead. */
96
97 #ifdef ADVENT_NOSAVE
98     return GO_UNKNOWN;
99 #endif
100     FILE *fp = NULL;
101
102     if (game.loc != 1 ||
103         game.abbrev[1] != 1) {
104         rspeak(RESUME_ABANDON);
105         if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
106             return GO_CLEAROBJ;
107     }
108
109     while (fp == NULL) {
110         char* name = readline("\nFile name: ");
111         if (name == NULL)
112             return GO_TOP;
113         fp = fopen(name, READ_MODE);
114         if (fp == NULL)
115             printf("Can't open file %s, try again.\n", name);
116         free(name);
117     }
118
119     return restore(fp);
120 }
121
122 int restore(FILE* fp)
123 {
124     /*  Read and restore game state from file, assuming
125      *  sane initial state.
126      *  If ADVENT_NOSAVE is defined, do nothing instead. */
127 #ifdef ADVENT_NOSAVE
128     return GO_UNKNOWN;
129 #endif
130
131     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
132     fclose(fp);
133     if (save.version != VRSION) {
134         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
135     } else if (is_valid(save.game)) {
136         game = save.game;
137     }
138     return GO_TOP;
139 }
140
141 bool is_valid(struct game_t valgame)
142 {
143     /*  Save files can be roughly grouped into three groups:
144      *  With valid, reaceable state, with valid, but unreachable
145      *  state and with invaild state. We check that state is
146      *  valid: no states are outside minimal or maximal value
147      */
148
149     /* Prevent division by zero */
150     if (valgame.abbnum == 0) {
151         return false;   // LCOV_EXCL_LINE
152     }
153
154     /* Check for RNG overflow. Truncate */
155     if (valgame.lcg_x >= LCG_M) {
156         valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
157     }
158
159     /* Check for RNG underflow. Transpose */
160     if (valgame.lcg_x < LCG_M) {
161         valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
162     }
163
164     /*  Bounds check for locations */
165     if ( valgame.chloc  < -1 || valgame.chloc  > NLOCATIONS ||
166          valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
167          valgame.loc    <  0 || valgame.loc    > NLOCATIONS ||
168          valgame.newloc <  0 || valgame.newloc > NLOCATIONS ||
169          valgame.oldloc <  0 || valgame.oldloc > NLOCATIONS ||
170          valgame.oldlc2 <  0 || valgame.oldlc2 > NLOCATIONS) {
171         return false;   // LCOV_EXCL_LINE
172     }
173     /*  Bounds check for location arrays */
174     for (int i = 0; i <= NDWARVES; i++) {
175         if (valgame.dloc[i]  < -1 || valgame.dloc[i]  > NLOCATIONS  ||
176             valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
177             return false;       // LCOV_EXCL_LINE
178         }
179     }
180
181     for (int i = 0; i <= NOBJECTS; i++) {
182         if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS  ||
183             valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
184             return false;       // LCOV_EXCL_LINE
185         }
186     }
187
188     /*  Bounds check for dwarves */
189     if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
190         valgame.dkill < 0  || valgame.dkill  > NDWARVES) {
191         return false;   // LCOV_EXCL_LINE
192     }
193
194     /*  Validate that we didn't die too many times in save */
195     if (valgame.numdie >= NDEATHS) {
196         return false;   // LCOV_EXCL_LINE
197     }
198
199     /* Recalculate tally, throw the towel if in disagreement */
200     int temp_tally = 0;
201     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
202         if (objects[treasure].is_treasure) {
203             if (valgame.prop[treasure] == STATE_NOTFOUND) {
204                 ++temp_tally;
205             }
206         }
207     }
208     if (temp_tally != valgame.tally) {
209         return false;   // LCOV_EXCL_LINE
210     }
211
212     /* Check that properties of objects aren't beyond expected */
213     for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
214         if (valgame.prop[obj] < STATE_NOTFOUND || valgame.prop[obj] > 1) {
215             switch (obj) {
216             case RUG:
217             case DRAGON:
218             case BIRD:
219             case BOTTLE:
220             case PLANT:
221             case PLANT2:
222             case TROLL:
223             case URN:
224             case EGGS:
225             case VASE:
226             case CHAIN:
227                 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
228                     continue;
229             /* FALLTHRU */
230             case BEAR:
231                 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
232                     continue;
233             /* FALLTHRU */
234             default:
235                 return false;   // LCOV_EXCL_LINE
236             }
237         }
238     }
239
240     /* Check that values in linked lists for objects in locations are inside bounds */
241     for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
242         if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
243             return false;       // LCOV_EXCL_LINE
244         }
245     }
246     for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
247         if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
248             return false;       // LCOV_EXCL_LINE
249         }
250     }
251
252     return true;
253 }
254
255 /* end */