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 memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC));
29 save.version = (version == 0) ? SAVE_VERSION : version;
32 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
36 /* Suspend and resume */
38 static char *strip(char *name)
40 // Trim leading whitespace
41 while(isspace((unsigned char)*name))
42 name++; // LCOV_EXCL_LINE
44 // Trim trailing whitespace;
45 // might be left there by autocomplete
46 char *end = name + strlen(name) - 1;
47 while(end > name && isspace((unsigned char)*end))
49 // Write new null terminator character
58 /* Suspend. Offer to save things in a file, but charging
59 * some points (so can't win by using saved games to retry
60 * battles or to start over after learning zzword).
61 * If ADVENT_NOSAVE is defined, gripe instead. */
63 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
64 rspeak(SAVERESUME_DISABLED);
69 rspeak(SUSPEND_WARNING);
70 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
72 game.saved = game.saved + 5;
75 char* name = myreadline("\nFile name: ");
79 if (strlen(name) == 0)
80 return GO_TOP; // LCOV_EXCL_LINE
81 fp = fopen(strip(name), WRITE_MODE);
83 printf("Can't open file %s, try again.\n", name);
87 savefile(fp, SAVE_VERSION);
95 /* Resume. Read a suspended game back from a file.
96 * If ADVENT_NOSAVE is defined, gripe instead. */
98 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
99 rspeak(SAVERESUME_DISABLED);
104 if (game.loc != LOC_START || game.abbrev[LOC_START] != 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: ");
115 if (strlen(name) == 0)
116 return GO_TOP; // LCOV_EXCL_LINE
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 (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0)
140 else if (save.version != SAVE_VERSION) {
141 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10));
142 } else if (!is_valid(save.game)) {
143 rspeak(SAVE_TAMPERING);
151 bool is_valid(struct game_t valgame)
153 /* Save files can be roughly grouped into three groups:
154 * With valid, reachable state, with valid, but unreachable
155 * state and with invalid state. We check that state is
156 * valid: no states are outside minimal or maximal value
159 /* Prevent division by zero */
160 if (valgame.abbnum == 0) {
161 return false; // LCOV_EXCL_LINE
164 /* Check for RNG overflow. Truncate */
165 if (valgame.lcg_x >= LCG_M) {
166 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
169 /* Check for RNG underflow. Transpose */
170 if (valgame.lcg_x < LCG_M) {
171 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
174 /* Bounds check for locations */
175 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
176 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
177 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
178 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
179 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
180 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
181 return false; // LCOV_EXCL_LINE
183 /* Bounds check for location arrays */
184 for (int i = 0; i <= NDWARVES; i++) {
185 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
186 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
187 return false; // LCOV_EXCL_LINE
191 for (int i = 0; i <= NOBJECTS; i++) {
192 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
193 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
194 return false; // LCOV_EXCL_LINE
198 /* Bounds check for dwarves */
199 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
200 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
201 return false; // LCOV_EXCL_LINE
204 /* Validate that we didn't die too many times in save */
205 if (valgame.numdie >= NDEATHS) {
206 return false; // LCOV_EXCL_LINE
209 /* Recalculate tally, throw the towel if in disagreement */
211 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
212 if (objects[treasure].is_treasure) {
213 if (valgame.prop[treasure] == STATE_NOTFOUND) {
218 if (temp_tally != valgame.tally) {
219 return false; // LCOV_EXCL_LINE
222 /* Check that properties of objects aren't beyond expected */
223 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
224 /* Magic number -2 allows a STASHED version of state 1 */
225 if (valgame.prop[obj] < -2 || valgame.prop[obj] > 1) {
238 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
239 continue; // LCOV_EXCL_LINE
242 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
246 return false; // LCOV_EXCL_LINE
251 /* Check that values in linked lists for objects in locations are inside bounds */
252 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
253 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
254 return false; // LCOV_EXCL_LINE
257 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
258 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
259 return false; // LCOV_EXCL_LINE