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: (C) 1977, 2005 by Will Crowther and Don Woods
8 * SPDX-License-Identifier: BSD-2-Clause
20 * Use this to detect endianness mismatch. Can't be unchanged by byte-swapping.
22 #define ENDIAN_MAGIC 2317
26 #define IGNORE(r) do{if (r){}}while(0)
28 int savefile(FILE *fp)
29 /* Save game to file. No input or output from user. */
31 memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC));
32 if (save.version == 0)
33 save.version = SAVE_VERSION;
35 save.canary = ENDIAN_MAGIC;
38 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
42 /* Suspend and resume */
44 static char *strip(char *name)
46 // Trim leading whitespace
47 while(isspace((unsigned char)*name))
48 name++; // LCOV_EXCL_LINE
50 // Trim trailing whitespace;
51 // might be left there by autocomplete
52 char *end = name + strlen(name) - 1;
53 while(end > name && isspace((unsigned char)*end))
55 // Write new null terminator character
64 /* Suspend. Offer to save things in a file, but charging
65 * some points (so can't win by using saved games to retry
66 * battles or to start over after learning zzword).
67 * If ADVENT_NOSAVE is defined, gripe instead. */
69 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
70 rspeak(SAVERESUME_DISABLED);
75 rspeak(SUSPEND_WARNING);
76 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
78 game.saved = game.saved + 5;
81 char* name = myreadline("\nFile name: ");
85 if (strlen(name) == 0)
86 return GO_TOP; // LCOV_EXCL_LINE
87 fp = fopen(strip(name), WRITE_MODE);
89 printf("Can't open file %s, try again.\n", name);
101 /* Resume. Read a suspended game back from a file.
102 * If ADVENT_NOSAVE is defined, gripe instead. */
104 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
105 rspeak(SAVERESUME_DISABLED);
110 if (game.loc != LOC_START || game.locs[LOC_START].abbrev != 1) {
111 rspeak(RESUME_ABANDON);
112 if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
117 char* name = myreadline("\nFile name: ");
121 if (strlen(name) == 0)
122 return GO_TOP; // LCOV_EXCL_LINE
123 fp = fopen(name, READ_MODE);
125 printf("Can't open file %s, try again.\n", name);
132 int restore(FILE* fp)
134 /* Read and restore game state from file, assuming
135 * sane initial state.
136 * If ADVENT_NOSAVE is defined, gripe instead. */
138 rspeak(SAVERESUME_DISABLED);
142 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
144 if (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0 || save.canary != ENDIAN_MAGIC)
146 else if (save.version != SAVE_VERSION) {
147 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10));
148 } else if (!is_valid(save.game)) {
149 rspeak(SAVE_TAMPERING);
157 bool is_valid(struct game_t valgame)
159 /* Save files can be roughly grouped into three groups:
160 * With valid, reachable state, with valid, but unreachable
161 * state and with invalid state. We check that state is
162 * valid: no states are outside minimal or maximal value
165 /* Prevent division by zero */
166 if (valgame.abbnum == 0) {
167 return false; // LCOV_EXCL_LINE
170 /* Check for RNG overflow. Truncate */
171 if (valgame.lcg_x >= LCG_M) {
172 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
175 /* Check for RNG underflow. Transpose */
176 if (valgame.lcg_x < LCG_M) {
177 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
180 /* Bounds check for locations */
181 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
182 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
183 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
184 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
185 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
186 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
187 return false; // LCOV_EXCL_LINE
189 /* Bounds check for location arrays */
190 for (int i = 0; i <= NDWARVES; i++) {
191 if (valgame.dwarves[i].loc < -1 || valgame.dwarves[i].loc > NLOCATIONS ||
192 valgame.dwarves[i].oldloc < -1 || valgame.dwarves[i].oldloc > NLOCATIONS) {
193 return false; // LCOV_EXCL_LINE
197 for (int i = 0; i <= NOBJECTS; i++) {
198 if (valgame.objects[i].place < -1 || valgame.objects[i].place > NLOCATIONS ||
199 valgame.objects[i].fixed < -1 || valgame.objects[i].fixed > NLOCATIONS) {
200 return false; // LCOV_EXCL_LINE
204 /* Bounds check for dwarves */
205 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
206 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
207 return false; // LCOV_EXCL_LINE
210 /* Validate that we didn't die too many times in save */
211 if (valgame.numdie >= NDEATHS) {
212 return false; // LCOV_EXCL_LINE
215 /* Recalculate tally, throw the towel if in disagreement */
217 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
218 if (objects[treasure].is_treasure) {
219 if (PROP_IS_NOTFOUND2(valgame, treasure)) {
224 if (temp_tally != valgame.tally) {
225 return false; // LCOV_EXCL_LINE
228 /* Check that properties of objects aren't beyond expected */
229 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
230 if (PROP_IS_INVALID(valgame.objects[obj].prop)) {
231 return false; // LCOV_EXCL_LINE
235 /* Check that values in linked lists for objects in locations are inside bounds */
236 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
237 if (valgame.locs[loc].atloc < NO_OBJECT || valgame.locs[loc].atloc > NOBJECTS * 2) {
238 return false; // LCOV_EXCL_LINE
241 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
242 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
243 return false; // LCOV_EXCL_LINE