4 * (ESR) This replaces a bunch of particularly nasty FORTRAN-derived code;
5 * see the history.adoc file in the source distribution for discussion.
7 * SPDX-FileCopyrightText: 1977, 2005 by Will Crowther and Don Woods
8 * SPDX-FileCopyrightText: 2017 by Eric S. Raymond
9 * SPDX-License-Identifier: BSD-2-Clause
22 * Bump on save format change.
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.
31 * If you change the first three members, the resume function may not properly
32 * reject saves from older versions. Later members can change, but bump the version
37 int32_t mode; /* not used, must be present for version detection */
43 #define IGNORE(r) do{if (r){}}while(0)
45 int savefile(FILE *fp, int32_t version)
46 /* Save game to file. No input or output from user. */
48 save.savetime = time(NULL);
50 save.version = (version == 0) ? VRSION : version;
53 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
57 /* Suspend and resume */
59 static char *strip(char *name)
61 // Trim leading whitespace
62 while(isspace((unsigned char)*name))
63 name++; // LCOV_EXCL_LINE
65 // Trim trailing whitespace;
66 // might be left there by autocomplete
67 char *end = name + strlen(name) - 1;
68 while(end > name && isspace((unsigned char)*end))
70 // Write new null terminator character
79 /* Suspend. Offer to save things in a file, but charging
80 * some points (so can't win by using saved games to retry
81 * battles or to start over after learning zzword).
82 * If ADVENT_NOSAVE is defined, gripe instead. */
84 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
85 rspeak(SAVERESUME_DISABLED);
90 rspeak(SUSPEND_WARNING);
91 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
93 game.saved = game.saved + 5;
96 char* name = myreadline("\nFile name: ");
100 if (strlen(name) == 0)
101 return GO_TOP; // LCOV_EXCL_LINE
102 fp = fopen(strip(name), WRITE_MODE);
104 printf("Can't open file %s, try again.\n", name);
108 savefile(fp, VRSION);
116 /* Resume. Read a suspended game back from a file.
117 * If ADVENT_NOSAVE is defined, gripe instead. */
119 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
120 rspeak(SAVERESUME_DISABLED);
125 if (game.loc != LOC_START || game.abbrev[LOC_START] != 1) {
126 rspeak(RESUME_ABANDON);
127 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
132 char* name = myreadline("\nFile name: ");
136 if (strlen(name) == 0)
137 return GO_TOP; // LCOV_EXCL_LINE
138 fp = fopen(name, READ_MODE);
140 printf("Can't open file %s, try again.\n", name);
147 int restore(FILE* fp)
149 /* Read and restore game state from file, assuming
150 * sane initial state.
151 * If ADVENT_NOSAVE is defined, gripe instead. */
153 rspeak(SAVERESUME_DISABLED)
157 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
159 if (save.version != VRSION) {
160 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
161 } else if (!is_valid(save.game)) {
162 rspeak(SAVE_TAMPERING);
170 bool is_valid(struct game_t valgame)
172 /* Save files can be roughly grouped into three groups:
173 * With valid, reachable state, with valid, but unreachable
174 * state and with invalid state. We check that state is
175 * valid: no states are outside minimal or maximal value
178 /* Prevent division by zero */
179 if (valgame.abbnum == 0) {
180 return false; // LCOV_EXCL_LINE
183 /* Check for RNG overflow. Truncate */
184 if (valgame.lcg_x >= LCG_M) {
185 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
188 /* Check for RNG underflow. Transpose */
189 if (valgame.lcg_x < LCG_M) {
190 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
193 /* Bounds check for locations */
194 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
195 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
196 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
197 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
198 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
199 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
200 return false; // LCOV_EXCL_LINE
202 /* Bounds check for location arrays */
203 for (int i = 0; i <= NDWARVES; i++) {
204 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
205 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
206 return false; // LCOV_EXCL_LINE
210 for (int i = 0; i <= NOBJECTS; i++) {
211 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
212 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
213 return false; // LCOV_EXCL_LINE
217 /* Bounds check for dwarves */
218 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
219 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
220 return false; // LCOV_EXCL_LINE
223 /* Validate that we didn't die too many times in save */
224 if (valgame.numdie >= NDEATHS) {
225 return false; // LCOV_EXCL_LINE
228 /* Recalculate tally, throw the towel if in disagreement */
230 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
231 if (objects[treasure].is_treasure) {
232 if (valgame.prop[treasure] == STATE_NOTFOUND) {
237 if (temp_tally != valgame.tally) {
238 return false; // LCOV_EXCL_LINE
241 /* Check that properties of objects aren't beyond expected */
242 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
243 /* Magic number -2 allows a STASHED version of state 1 */
244 if (valgame.prop[obj] < -2 || valgame.prop[obj] > 1) {
257 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
258 continue; // LCOV_EXCL_LINE
261 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
265 return false; // LCOV_EXCL_LINE
270 /* Check that values in linked lists for objects in locations are inside bounds */
271 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
272 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
273 return false; // LCOV_EXCL_LINE
276 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
277 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
278 return false; // LCOV_EXCL_LINE