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)
26 /* Save game to file. No input or output from user. */
28 memcpy(&save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC));
29 if (save.version == 0)
30 save.version = SAVE_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);
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 (memcmp(save.magic, ADVENT_MAGIC, sizeof(ADVENT_MAGIC)) != 0)
141 else if (save.version != SAVE_VERSION) {
142 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), SAVE_VERSION / 10, MOD(SAVE_VERSION, 10));
143 } else if (!is_valid(save.game)) {
144 rspeak(SAVE_TAMPERING);
152 bool is_valid(struct game_t valgame)
154 /* Save files can be roughly grouped into three groups:
155 * With valid, reachable state, with valid, but unreachable
156 * state and with invalid state. We check that state is
157 * valid: no states are outside minimal or maximal value
160 /* Prevent division by zero */
161 if (valgame.abbnum == 0) {
162 return false; // LCOV_EXCL_LINE
165 /* Check for RNG overflow. Truncate */
166 if (valgame.lcg_x >= LCG_M) {
167 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
170 /* Check for RNG underflow. Transpose */
171 if (valgame.lcg_x < LCG_M) {
172 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
175 /* Bounds check for locations */
176 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
177 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
178 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
179 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
180 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
181 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
182 return false; // LCOV_EXCL_LINE
184 /* Bounds check for location arrays */
185 for (int i = 0; i <= NDWARVES; i++) {
186 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
187 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
188 return false; // LCOV_EXCL_LINE
192 for (int i = 0; i <= NOBJECTS; i++) {
193 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
194 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
195 return false; // LCOV_EXCL_LINE
199 /* Bounds check for dwarves */
200 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
201 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
202 return false; // LCOV_EXCL_LINE
205 /* Validate that we didn't die too many times in save */
206 if (valgame.numdie >= NDEATHS) {
207 return false; // LCOV_EXCL_LINE
210 /* Recalculate tally, throw the towel if in disagreement */
212 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
213 if (objects[treasure].is_treasure) {
214 if (valgame.prop[treasure] == STATE_NOTFOUND) {
219 if (temp_tally != valgame.tally) {
220 return false; // LCOV_EXCL_LINE
223 /* Check that properties of objects aren't beyond expected */
224 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
225 /* Magic number -2 allows a STASHED version of state 1 */
226 if (valgame.prop[obj] < -2 || valgame.prop[obj] > 1) {
239 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
240 continue; // LCOV_EXCL_LINE
243 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
247 return false; // LCOV_EXCL_LINE
252 /* Check that values in linked lists for objects in locations are inside bounds */
253 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
254 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
255 return false; // LCOV_EXCL_LINE
258 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
259 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
260 return false; // LCOV_EXCL_LINE