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 * 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
21 * Bump on save format change.
23 * Note: Verify that the tests run clean before bumping this, then rebuild the check
24 * files afterwards. Otherwise you will get a spurious failure due to the old version
25 * having been generated into a check file.
30 * If you change the first three members, the resume function may not properly
31 * reject saves from older versions. Yes, this glues us to a hardware-
32 * dependent length of int. 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 */
60 /* Suspend. Offer to save things in a file, but charging
61 * some points (so can't win by using saved games to retry
62 * battles or to start over after learning zzword).
63 * If ADVENT_NOSAVE is defined, gripe instead. */
65 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
66 rspeak(SAVERESUME_DISABLED)
71 rspeak(SUSPEND_WARNING);
72 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
74 game.saved = game.saved + 5;
77 char* name = myreadline("\nFile name: ");
80 fp = fopen(name, WRITE_MODE);
82 printf("Can't open file %s, try again.\n", name);
94 /* Resume. Read a suspended game back from a file.
95 * If ADVENT_NOSAVE is defined, gripe instead. */
97 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
98 rspeak(SAVERESUME_DISABLED)
104 game.abbrev[1] != 1) {
105 rspeak(RESUME_ABANDON);
106 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
111 char* name = myreadline("\nFile name: ");
112 // Autocomplete can leave the input with an extra trailing space.
113 if (name != NULL && strlen(name) > 0 && name[strlen(name) - 1] == ' ')
114 name[strlen(name) - 1] = '\0';
117 fp = fopen(name, READ_MODE);
119 printf("Can't open file %s, try again.\n", name);
126 int restore(FILE* fp)
128 /* Read and restore game state from file, assuming
129 * sane initial state.
130 * If ADVENT_NOSAVE is defined, gripe instead. */
132 rspeak(SAVERESUME_DISABLED)
136 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
138 if (save.version != VRSION) {
139 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
140 } else if (!is_valid(save.game)) {
141 rspeak(SAVE_TAMPERING);
149 bool is_valid(struct game_t valgame)
151 /* Save files can be roughly grouped into three groups:
152 * With valid, reacheable state, with valid, but unreachable
153 * state and with invalid state. We check that state is
154 * valid: no states are outside minimal or maximal value
157 /* Prevent division by zero */
158 if (valgame.abbnum == 0) {
159 return false; // LCOV_EXCL_LINE
162 /* Check for RNG overflow. Truncate */
163 if (valgame.lcg_x >= LCG_M) {
164 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
167 /* Check for RNG underflow. Transpose */
168 if (valgame.lcg_x < LCG_M) {
169 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
172 /* Bounds check for locations */
173 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
174 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
175 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
176 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
177 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
178 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
179 return false; // LCOV_EXCL_LINE
181 /* Bounds check for location arrays */
182 for (int i = 0; i <= NDWARVES; i++) {
183 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
184 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
185 return false; // LCOV_EXCL_LINE
189 for (int i = 0; i <= NOBJECTS; i++) {
190 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
191 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
192 return false; // LCOV_EXCL_LINE
196 /* Bounds check for dwarves */
197 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
198 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
199 return false; // LCOV_EXCL_LINE
202 /* Validate that we didn't die too many times in save */
203 if (valgame.numdie >= NDEATHS) {
204 return false; // LCOV_EXCL_LINE
207 /* Recalculate tally, throw the towel if in disagreement */
209 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
210 if (objects[treasure].is_treasure) {
211 if (valgame.prop[treasure] == STATE_NOTFOUND) {
216 if (temp_tally != valgame.tally) {
217 return false; // LCOV_EXCL_LINE
220 /* Check that properties of objects aren't beyond expected */
221 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
222 if (valgame.prop[obj] < STATE_NOTFOUND || valgame.prop[obj] > 1) {
235 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
236 continue; // LCOV_EXCL_LINE
239 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
243 return false; // LCOV_EXCL_LINE
248 /* Check that values in linked lists for objects in locations are inside bounds */
249 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
250 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
251 return false; // LCOV_EXCL_LINE
254 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
255 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
256 return false; // LCOV_EXCL_LINE