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
23 #define IGNORE(r) do{if (r){}}while(0)
25 int savefile(FILE *fp, int32_t version)
26 /* Save game to file. No input or output from user. */
28 save.savetime = time(NULL);
30 save.version = (version == 0) ? SAVE_VERSION : version;
33 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
37 /* Suspend and resume */
39 static char *strip(char *name)
41 // Trim leading whitespace
42 while(isspace((unsigned char)*name))
43 name++; // LCOV_EXCL_LINE
45 // Trim trailing whitespace;
46 // might be left there by autocomplete
47 char *end = name + strlen(name) - 1;
48 while(end > name && isspace((unsigned char)*end))
50 // Write new null terminator character
59 /* Suspend. Offer to save things in a file, but charging
60 * some points (so can't win by using saved games to retry
61 * battles or to start over after learning zzword).
62 * If ADVENT_NOSAVE is defined, gripe instead. */
64 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
65 rspeak(SAVERESUME_DISABLED);
70 rspeak(SUSPEND_WARNING);
71 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
73 game.saved = game.saved + 5;
76 char* name = myreadline("\nFile name: ");
80 if (strlen(name) == 0)
81 return GO_TOP; // LCOV_EXCL_LINE
82 fp = fopen(strip(name), WRITE_MODE);
84 printf("Can't open file %s, try again.\n", name);
88 savefile(fp, SAVE_VERSION);
96 /* Resume. Read a suspended game back from a file.
97 * If ADVENT_NOSAVE is defined, gripe instead. */
99 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
100 rspeak(SAVERESUME_DISABLED);
105 if (game.loc != LOC_START || game.abbrev[LOC_START] != 1) {
106 rspeak(RESUME_ABANDON);
107 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
112 char* name = myreadline("\nFile name: ");
116 if (strlen(name) == 0)
117 return GO_TOP; // LCOV_EXCL_LINE
118 fp = fopen(name, READ_MODE);
120 printf("Can't open file %s, try again.\n", name);
127 int restore(FILE* fp)
129 /* Read and restore game state from file, assuming
130 * sane initial state.
131 * If ADVENT_NOSAVE is defined, gripe instead. */
133 rspeak(SAVERESUME_DISABLED)
137 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
139 if (save.version != SAVE_VERSION) {
140 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10));
141 } else if (!is_valid(save.game)) {
142 rspeak(SAVE_TAMPERING);
150 bool is_valid(struct game_t valgame)
152 /* Save files can be roughly grouped into three groups:
153 * With valid, reachable state, with valid, but unreachable
154 * state and with invalid state. We check that state is
155 * valid: no states are outside minimal or maximal value
158 /* Prevent division by zero */
159 if (valgame.abbnum == 0) {
160 return false; // LCOV_EXCL_LINE
163 /* Check for RNG overflow. Truncate */
164 if (valgame.lcg_x >= LCG_M) {
165 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
168 /* Check for RNG underflow. Transpose */
169 if (valgame.lcg_x < LCG_M) {
170 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
173 /* Bounds check for locations */
174 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
175 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
176 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
177 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
178 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
179 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
180 return false; // LCOV_EXCL_LINE
182 /* Bounds check for location arrays */
183 for (int i = 0; i <= NDWARVES; i++) {
184 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
185 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
186 return false; // LCOV_EXCL_LINE
190 for (int i = 0; i <= NOBJECTS; i++) {
191 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
192 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
193 return false; // LCOV_EXCL_LINE
197 /* Bounds check for dwarves */
198 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
199 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
200 return false; // LCOV_EXCL_LINE
203 /* Validate that we didn't die too many times in save */
204 if (valgame.numdie >= NDEATHS) {
205 return false; // LCOV_EXCL_LINE
208 /* Recalculate tally, throw the towel if in disagreement */
210 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
211 if (objects[treasure].is_treasure) {
212 if (valgame.prop[treasure] == STATE_NOTFOUND) {
217 if (temp_tally != valgame.tally) {
218 return false; // LCOV_EXCL_LINE
221 /* Check that properties of objects aren't beyond expected */
222 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
223 /* Magic number -2 allows a STASHED version of state 1 */
224 if (valgame.prop[obj] < -2 || valgame.prop[obj] > 1) {
237 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
238 continue; // LCOV_EXCL_LINE
241 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
245 return false; // LCOV_EXCL_LINE
250 /* Check that values in linked lists for objects in locations are inside bounds */
251 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
252 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
253 return false; // LCOV_EXCL_LINE
256 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
257 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
258 return false; // LCOV_EXCL_LINE