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>
21 #define VRSION 28 /* bump on save format change */
24 * If you change the first three members, the resume function may not properly
25 * reject saves from older versions. Yes, this glues us to a hardware-
26 * dependent length of long. Later members can change, but bump the version
31 int32_t mode; /* not used, must be present for version detection */
37 #define IGNORE(r) do{if (r){}}while(0)
39 int savefile(FILE *fp, int32_t version)
40 /* Save game to file. No input or output from user. */
42 save.savetime = time(NULL);
44 save.version = (version == 0) ? VRSION : version;
47 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
51 /* Suspend and resume */
54 /* Suspend. Offer to save things in a file, but charging
55 * some points (so can't win by using saved games to retry
56 * battles or to start over after learning zzword).
57 * If ADVENT_NOSAVE is defined, do nothing instead. */
64 rspeak(SUSPEND_WARNING);
65 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
67 game.saved = game.saved + 5;
70 char* name = readline("\nFile name: ");
73 fp = fopen(name, WRITE_MODE);
75 printf("Can't open file %s, try again.\n", name);
87 /* Resume. Read a suspended game back from a file.
88 * If ADVENT_NOSAVE is defined, do nothing instead. */
96 game.abbrev[1] != 1) {
97 rspeak(RESUME_ABANDON);
98 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
103 char* name = readline("\nFile name: ");
106 fp = fopen(name, READ_MODE);
108 printf("Can't open file %s, try again.\n", name);
115 bool is_valid(struct game_t*);
117 int restore(FILE* fp)
119 /* Read and restore game state from file, assuming
120 * sane initial state.
121 * If ADVENT_NOSAVE is defined, do nothing instead. */
126 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
128 if (save.version != VRSION) {
129 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
130 } else if (is_valid(&save.game)) {
136 bool is_valid(struct game_t* valgame)
138 /* Save files can be roughly grouped into three groups:
139 * With valid, reaceable state, with valid, but unreachable
140 * state and with invaild state. We check that state is
141 * valid: no states are outside minimal or maximal value
144 /* Prevent division by zero */
145 if (valgame->abbnum == 0) {
149 /* Check for RNG overflow. Truncate */
150 if (valgame->lcg_x >= LCG_M) {
151 valgame->lcg_x %= LCG_M;
154 /* Check for RNG underflow. Transpose */
155 if (valgame->lcg_x < LCG_M) {
156 valgame->lcg_x = LCG_M + (valgame->lcg_x % LCG_M);
159 /* Bounds check for locations */
160 if ( valgame->chloc < -1 || valgame->chloc > NLOCATIONS ||
161 valgame->chloc2 < -1 || valgame->chloc2 > NLOCATIONS ||
162 valgame->loc < 0 || valgame->loc > NLOCATIONS ||
163 valgame->newloc < 0 || valgame->newloc > NLOCATIONS ||
164 valgame->oldloc < 0 || valgame->oldloc > NLOCATIONS ||
165 valgame->oldlc2 < 0 || valgame->oldlc2 > NLOCATIONS) {
168 /* Bounds check for location arrays */
169 for (int i = 0; i <= NDWARVES; i++) {
170 if (valgame->dloc[i] < -1 || valgame->dloc[i] > NLOCATIONS ||
171 valgame->odloc[i] < -1 || valgame->odloc[i] > NLOCATIONS) {
176 for (int i = 0; i <= NOBJECTS; i++) {
177 if (valgame->place[i] < -1 || valgame->place[i] > NLOCATIONS ||
178 valgame->fixed[i] < -1 || valgame->fixed[i] > NLOCATIONS) {
183 /* Bounds check for dwarves */
184 if (valgame->dtotal < 0 || valgame->dtotal > NDWARVES ||
185 valgame->dkill < 0 || valgame->dkill > NDWARVES) {
189 /* Validate that we didn't die too many times in save */
190 if (valgame->numdie >= NDEATHS) {
194 /* Recalculate tally, throw the towel if in disagreement */
196 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
197 if (objects[treasure].is_treasure) {
198 if (valgame->prop[treasure] == STATE_NOTFOUND) {
203 if (temp_tally != valgame->tally) {
207 /* Check that properties of objects aren't beyond expected */
208 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
209 if (valgame->prop[obj] < STATE_NOTFOUND || valgame->prop[obj] > 1) {
222 if (valgame->prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
226 if (valgame->prop[BEAR] == CONTENTED_BEAR || valgame->prop[BEAR] == BEAR_DEAD)
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->atloc[loc] < NO_OBJECT || valgame->atloc[loc] > NOBJECTS * 2) {
241 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
242 if (valgame->link[obj] < NO_OBJECT || valgame->link[obj] > NOBJECTS * 2) {