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 * Copyright (c) 1977, 2005 by Will Crowther and Don Woods
8 * Copyright (c) 2017 by Eric S. Raymond
9 * SPDX-License-Identifier: BSD-2-clause
14 #include <editline/readline.h>
22 * Bump on save format change.
24 * Note: Verify that the tests run clean before bumping this, then rebuild the check
25 * files afterwards. Otherwise you will get a spurious failure due to the old version
26 * having been generated into a check file.
31 * If you change the first three members, the resume function may not properly
32 * reject saves from older versions. Yes, this glues us to a hardware-
33 * dependent length of int. Later members can change, but bump the version
38 int32_t mode; /* not used, must be present for version detection */
44 #define IGNORE(r) do{if (r){}}while(0)
46 int savefile(FILE *fp, int32_t version)
47 /* Save game to file. No input or output from user. */
49 save.savetime = time(NULL);
51 save.version = (version == 0) ? VRSION : version;
54 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
58 /* Suspend and resume */
61 /* Suspend. Offer to save things in a file, but charging
62 * some points (so can't win by using saved games to retry
63 * battles or to start over after learning zzword).
64 * If ADVENT_NOSAVE is defined, do nothing instead. */
71 rspeak(SUSPEND_WARNING);
72 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
74 game.saved = game.saved + 5;
77 char* name = readline("\nFile name: ");
80 fp = fopen(name, WRITE_MODE);
82 printf("Can't open file %s, try again.\n", name);
94 /* Resume. Read a suspended game back from a file.
95 * If ADVENT_NOSAVE is defined, do nothing instead. */
103 game.abbrev[1] != 1) {
104 rspeak(RESUME_ABANDON);
105 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
110 char* name = readline("\nFile name: ");
111 // Autocomplete can leave the input with an extra traoling space.
112 if (name != NULL && strlen(name) > 0 && name[strlen(name) - 1] == ' ')
113 name[strlen(name) - 1] = '\0';
116 fp = fopen(name, READ_MODE);
118 printf("Can't open file %s, try again.\n", name);
125 int restore(FILE* fp)
127 /* Read and restore game state from file, assuming
128 * sane initial state.
129 * If ADVENT_NOSAVE is defined, do nothing instead. */
134 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
136 if (save.version != VRSION) {
137 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
138 } else if (is_valid(save.game)) {
144 bool is_valid(struct game_t valgame)
146 /* Save files can be roughly grouped into three groups:
147 * With valid, reaceable state, with valid, but unreachable
148 * state and with invaild state. We check that state is
149 * valid: no states are outside minimal or maximal value
152 /* Prevent division by zero */
153 if (valgame.abbnum == 0) {
154 return false; // LCOV_EXCL_LINE
157 /* Check for RNG overflow. Truncate */
158 if (valgame.lcg_x >= LCG_M) {
159 valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
162 /* Check for RNG underflow. Transpose */
163 if (valgame.lcg_x < LCG_M) {
164 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
167 /* Bounds check for locations */
168 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
169 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
170 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
171 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
172 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
173 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
174 return false; // LCOV_EXCL_LINE
176 /* Bounds check for location arrays */
177 for (int i = 0; i <= NDWARVES; i++) {
178 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
179 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
180 return false; // LCOV_EXCL_LINE
184 for (int i = 0; i <= NOBJECTS; i++) {
185 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
186 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
187 return false; // LCOV_EXCL_LINE
191 /* Bounds check for dwarves */
192 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
193 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
194 return false; // LCOV_EXCL_LINE
197 /* Validate that we didn't die too many times in save */
198 if (valgame.numdie >= NDEATHS) {
199 return false; // LCOV_EXCL_LINE
202 /* Recalculate tally, throw the towel if in disagreement */
204 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
205 if (objects[treasure].is_treasure) {
206 if (valgame.prop[treasure] == STATE_NOTFOUND) {
211 if (temp_tally != valgame.tally) {
212 return false; // LCOV_EXCL_LINE
215 /* Check that properties of objects aren't beyond expected */
216 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
217 if (valgame.prop[obj] < STATE_NOTFOUND || valgame.prop[obj] > 1) {
230 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
234 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
238 return false; // LCOV_EXCL_LINE
243 /* Check that values in linked lists for objects in locations are inside bounds */
244 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
245 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
246 return false; // LCOV_EXCL_LINE
249 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
250 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
251 return false; // LCOV_EXCL_LINE