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 * Use this to detect endianness mismatch. Can't be unchanged by byte-swapping.
24 #define ENDIAN_MAGIC 2317
28 #define IGNORE(r) do{if (r){}}while(0)
30 int savefile(FILE *fp)
31 /* Save game to file. No input or output from user. */
33 memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC));
34 if (save.version == 0)
35 save.version = SAVE_VERSION;
37 save.canary = ENDIAN_MAGIC;
40 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
44 /* Suspend and resume */
46 static char *strip(char *name)
48 // Trim leading whitespace
49 while(isspace((unsigned char)*name))
50 name++; // LCOV_EXCL_LINE
52 // Trim trailing whitespace;
53 // might be left there by autocomplete
54 char *end = name + strlen(name) - 1;
55 while(end > name && isspace((unsigned char)*end))
57 // Write new null terminator character
66 /* Suspend. Offer to save things in a file, but charging
67 * some points (so can't win by using saved games to retry
68 * battles or to start over after learning zzword).
69 * If ADVENT_NOSAVE is defined, gripe instead. */
71 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
72 rspeak(SAVERESUME_DISABLED);
77 rspeak(SUSPEND_WARNING);
78 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
80 game.saved = game.saved + 5;
83 char* name = myreadline("\nFile name: ");
87 if (strlen(name) == 0)
88 return GO_TOP; // LCOV_EXCL_LINE
89 fp = fopen(strip(name), WRITE_MODE);
91 printf("Can't open file %s, try again.\n", name);
103 /* Resume. Read a suspended game back from a file.
104 * If ADVENT_NOSAVE is defined, gripe instead. */
106 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
107 rspeak(SAVERESUME_DISABLED);
112 if (game.loc != LOC_START || game.locs[LOC_START].abbrev != 1) {
113 rspeak(RESUME_ABANDON);
114 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
119 char* name = myreadline("\nFile name: ");
123 if (strlen(name) == 0)
124 return GO_TOP; // LCOV_EXCL_LINE
125 fp = fopen(name, READ_MODE);
127 printf("Can't open file %s, try again.\n", name);
134 int restore(FILE* fp)
136 /* Read and restore game state from file, assuming
137 * sane initial state.
138 * If ADVENT_NOSAVE is defined, gripe instead. */
140 rspeak(SAVERESUME_DISABLED)
144 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
146 if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0 || save.canary != ENDIAN_MAGIC)
148 else if (save.version != SAVE_VERSION) {
149 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10));
150 } else if (!is_valid(save.game)) {
151 rspeak(SAVE_TAMPERING);
159 bool is_valid(struct game_t valgame)
161 /* Save files can be roughly grouped into three groups:
162 * With valid, reachable state, with valid, but unreachable
163 * state and with invalid state. We check that state is
164 * valid: no states are outside minimal or maximal value
167 /* Prevent division by zero */
168 if (valgame.abbnum == 0) {
169 return false; // LCOV_EXCL_LINE
172 /* Check for RNG overflow. Truncate */
173 if (valgame.lcg_x >= LCG_M) {
174 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
177 /* Check for RNG underflow. Transpose */
178 if (valgame.lcg_x < LCG_M) {
179 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
182 /* Bounds check for locations */
183 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
184 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
185 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
186 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
187 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
188 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
189 return false; // LCOV_EXCL_LINE
191 /* Bounds check for location arrays */
192 for (int i = 0; i <= NDWARVES; i++) {
193 if (valgame.dwarves[i].loc < -1 || valgame.dwarves[i].loc > NLOCATIONS ||
194 valgame.dwarves[i].oldloc < -1 || valgame.dwarves[i].oldloc > NLOCATIONS) {
195 return false; // LCOV_EXCL_LINE
199 for (int i = 0; i <= NOBJECTS; i++) {
200 if (valgame.objects[i].place < -1 || valgame.objects[i].place > NLOCATIONS ||
201 valgame.objects[i].fixed < -1 || valgame.objects[i].fixed > NLOCATIONS) {
202 return false; // LCOV_EXCL_LINE
206 /* Bounds check for dwarves */
207 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
208 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
209 return false; // LCOV_EXCL_LINE
212 /* Validate that we didn't die too many times in save */
213 if (valgame.numdie >= NDEATHS) {
214 return false; // LCOV_EXCL_LINE
217 /* Recalculate tally, throw the towel if in disagreement */
219 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
220 if (objects[treasure].is_treasure) {
221 if (PROP_IS_NOTFOUND2(valgame, treasure)) {
226 if (temp_tally != valgame.tally) {
227 return false; // LCOV_EXCL_LINE
230 /* Check that properties of objects aren't beyond expected */
231 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
232 if (PROP_IS_INVALID(valgame.objects[obj].prop)) {
233 return false; // LCOV_EXCL_LINE
237 /* Check that values in linked lists for objects in locations are inside bounds */
238 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
239 if (valgame.locs[loc].atloc < NO_OBJECT || valgame.locs[loc].atloc > NOBJECTS * 2) {
240 return false; // LCOV_EXCL_LINE
243 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
244 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
245 return false; // LCOV_EXCL_LINE