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 int restore(FILE* fp)
117 /* Read and restore game state from file, assuming
118 * sane initial state.
119 * If ADVENT_NOSAVE is defined, do nothing instead. */
124 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
126 if (save.version != VRSION) {
127 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
128 } else if (is_valid(save.game)) {
134 bool is_valid(struct game_t valgame)
136 /* Save files can be roughly grouped into three groups:
137 * With valid, reaceable state, with valid, but unreachable
138 * state and with invaild state. We check that state is
139 * valid: no states are outside minimal or maximal value
142 /* Prevent division by zero */
143 if (valgame.abbnum == 0) {
147 /* Check for RNG overflow. Truncate */
148 if (valgame.lcg_x >= LCG_M) {
149 valgame.lcg_x %= LCG_M;
152 /* Check for RNG underflow. Transpose */
153 if (valgame.lcg_x < LCG_M) {
154 valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
157 /* Bounds check for locations */
158 if ( valgame.chloc < -1 || valgame.chloc > NLOCATIONS ||
159 valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
160 valgame.loc < 0 || valgame.loc > NLOCATIONS ||
161 valgame.newloc < 0 || valgame.newloc > NLOCATIONS ||
162 valgame.oldloc < 0 || valgame.oldloc > NLOCATIONS ||
163 valgame.oldlc2 < 0 || valgame.oldlc2 > NLOCATIONS) {
166 /* Bounds check for location arrays */
167 for (int i = 0; i <= NDWARVES; i++) {
168 if (valgame.dloc[i] < -1 || valgame.dloc[i] > NLOCATIONS ||
169 valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
174 for (int i = 0; i <= NOBJECTS; i++) {
175 if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS ||
176 valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
181 /* Bounds check for dwarves */
182 if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
183 valgame.dkill < 0 || valgame.dkill > NDWARVES) {
187 /* Validate that we didn't die too many times in save */
188 if (valgame.numdie >= NDEATHS) {
192 /* Recalculate tally, throw the towel if in disagreement */
194 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
195 if (objects[treasure].is_treasure) {
196 if (valgame.prop[treasure] == STATE_NOTFOUND) {
201 if (temp_tally != valgame.tally) {
205 /* Check that properties of objects aren't beyond expected */
206 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
207 if (valgame.prop[obj] < STATE_NOTFOUND || valgame.prop[obj] > 1) {
220 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
224 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
233 /* Check that values in linked lists for objects in locations are inside bounds */
234 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
235 if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
239 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
240 if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {