3 #include <editline/readline.h>
11 * (ESR) This replaces a bunch of particularly nasty FORTRAN-derived code;
12 * see the history.adoc file in the source distribution for discussion.
15 #define VRSION 28 /* bump on save format change */
18 * If you change the first three members, the resume function may not properly
19 * reject saves from older versions. Yes, this glues us to a hardware-
20 * dependent length of long. Later members can change, but bump the version
25 int32_t mode; /* not used, must be present for version detection */
31 #define IGNORE(r) do{if (r){}}while(0)
33 int savefile(FILE *fp, int32_t version)
34 /* Save game to file. No input or output from user. */
36 save.savetime = time(NULL);
38 save.version = (version == 0) ? VRSION : version;
41 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
45 /* Suspend and resume */
48 /* Suspend. Offer to save things in a file, but charging
49 * some points (so can't win by using saved games to retry
50 * battles or to start over after learning zzword).
51 * If ADVENT_NOSAVE is defined, do nothing instead. */
58 rspeak(SUSPEND_WARNING);
59 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
61 game.saved = game.saved + 5;
64 char* name = readline("\nFile name: ");
67 fp = fopen(name, WRITE_MODE);
69 printf("Can't open file %s, try again.\n", name);
81 /* Resume. Read a suspended game back from a file.
82 * If ADVENT_NOSAVE is defined, do nothing instead. */
90 game.abbrev[1] != 1) {
91 rspeak(RESUME_ABANDON);
92 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
97 char* name = readline("\nFile name: ");
100 fp = fopen(name, READ_MODE);
102 printf("Can't open file %s, try again.\n", name);
109 bool is_valid(struct game_t*);
111 int restore(FILE* fp)
113 /* Read and restore game state from file, assuming
114 * sane initial state.
115 * If ADVENT_NOSAVE is defined, do nothing instead. */
120 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
122 if (save.version != VRSION) {
123 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
124 } else if (is_valid(&save.game)) {
130 bool is_valid(struct game_t* valgame)
132 /* Save files can be roughly grouped into three groups:
133 * With valid, reaceable state, with valid, but unreachable
134 * state and with invaild state. We check that state is
135 * valid: no states are outside minimal or maximal value
138 /* Prevent division by zero */
139 if (valgame->abbnum == 0) {
143 /* Check for RNG overflow. Truncate */
144 if (valgame->lcg_x >= LCG_M) {
145 valgame->lcg_x %= LCG_M;
148 /* Bounds check for locations */
149 if ( valgame->chloc < -1 || valgame->chloc > NLOCATIONS ||
150 valgame->chloc2 < -1 || valgame->chloc2 > NLOCATIONS ||
151 valgame->loc < -1 || valgame->loc > NLOCATIONS ||
152 valgame->newloc < -1 || valgame->newloc > NLOCATIONS ||
153 valgame->oldloc < -1 || valgame->oldloc > NLOCATIONS ||
154 valgame->oldlc2 < -1 || valgame->oldlc2 > NLOCATIONS) {
157 /* Bounds check for location arrays */
158 for (int i = 0; i <= NDWARVES; i++) {
159 if (valgame->dloc[i] < -1 || valgame->dloc[i] > NLOCATIONS ||
160 valgame->odloc[i] < -1 || valgame->odloc[i] > NLOCATIONS) {
165 for (int i = 0; i <= NOBJECTS; i++) {
166 if (valgame->place[i] < -1 || valgame->place[i] > NLOCATIONS ||
167 valgame->fixed[i] < -1 || valgame->fixed[i] > NLOCATIONS) {
172 /* Bounds check for dwarves */
173 if (valgame->dtotal < 0 || valgame->dtotal > NDWARVES ||
174 valgame->dkill < 0 || valgame->dkill > NDWARVES) {
178 /* Validate that we didn't die too many times in save */
179 if (valgame->numdie >= NDEATHS) {
183 /* Recalculate tally, throw the towel if in disagreement */
185 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
186 if (objects[treasure].is_treasure) {
187 if (valgame->prop[treasure] == STATE_NOTFOUND) {
192 if (temp_tally != valgame->tally) {
196 /* Check that properties of objects aren't beyond expected */
197 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
198 if (valgame->prop[obj] < STATE_NOTFOUND || valgame->prop[obj] > 1) {
211 if (valgame->prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
215 if (valgame->prop[BEAR] == CONTENTED_BEAR || valgame->prop[BEAR] == BEAR_DEAD)
224 /* Check that values in linked lists for objects in locations are inside bounds */
225 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
226 if (valgame->atloc[loc] < NO_OBJECT || valgame->atloc[loc] > NOBJECTS * 2) {
230 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
231 if (valgame->link[obj] < NO_OBJECT || valgame->link[obj] > NOBJECTS * 2) {