3 #include <editline/readline.h>
10 * (ESR) This replaces a bunch of particularly nasty FORTRAN-derived code;
11 * see the history.adoc file in the source distribution for discussion.
14 #define VRSION 27 /* bump on save format change */
17 * If you change the first three members, the resume function may not properly
18 * reject saves from older versions. Yes, this glues us to a hardware-
19 * dependent length of long. Later members can change, but bump the version
24 long mode; /* not used, must be present for version detection */
30 #define IGNORE(r) do{if (r){}}while(0)
32 int savefile(FILE *fp, long version)
33 /* Save game to file. No input or output from user. */
35 save.savetime = time(NULL);
37 save.version = (version == 0) ? VRSION : version;
40 IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
44 /* Suspend and resume */
47 /* Suspend. Offer to save things in a file, but charging
48 * some points (so can't win by using saved games to retry
49 * battles or to start over after learning zzword).
50 * If ADVENT_NOSAVE is defined, do nothing instead. */
57 rspeak(SUSPEND_WARNING);
58 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
60 game.saved = game.saved + 5;
63 char* name = readline("\nFile name: ");
66 fp = fopen(name, WRITE_MODE);
68 printf("Can't open file %s, try again.\n", name);
80 /* Resume. Read a suspended game back from a file.
81 * If ADVENT_NOSAVE is defined, do nothing instead. */
89 game.abbrev[1] != 1) {
90 rspeak(RESUME_ABANDON);
91 if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
96 char* name = readline("\nFile name: ");
99 fp = fopen(name, READ_MODE);
101 printf("Can't open file %s, try again.\n", name);
108 bool is_valid(struct game_t*);
110 int restore(FILE* fp)
112 /* Read and restore game state from file, assuming
113 * sane initial state.
114 * If ADVENT_NOSAVE is defined, do nothing instead. */
119 IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
121 if (save.version != VRSION) {
122 rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
123 } else if (is_valid(&save.game)) {
129 bool is_valid(struct game_t* valgame)
131 /* Save files can be roughly grouped into three groups:
132 * With valid, reaceable state, with valid, but unreachable
133 * state and with invaild state. We check that state is
134 * valid: no states are outside minimal or maximal value
137 /* Prevent division by zero */
138 if (valgame->abbnum == 0) {
142 /* Prevent RNG substitution. Why we are saving PRNG parameters? */
144 if (valgame->lcg_a != game.lcg_a || valgame->lcg_c != game.lcg_c || valgame->lcg_m != game.lcg_m) {
148 /* Check for RNG overflow. Truncate */
149 if (valgame->lcg_x >= game.lcg_m) {
150 valgame->lcg_x %= game.lcg_m;
153 /* Bounds check for locations */
154 if ( valgame->chloc < -1 || valgame->chloc > NLOCATIONS ||
155 valgame->chloc2 < -1 || valgame->chloc2 > NLOCATIONS ||
156 valgame->loc < -1 || valgame->loc > NLOCATIONS ||
157 valgame->newloc < -1 || valgame->newloc > NLOCATIONS ||
158 valgame->oldloc < -1 || valgame->oldloc > NLOCATIONS ||
159 valgame->oldlc2 < -1 || valgame->oldlc2 > NLOCATIONS) {
162 /* Bounds check for location arrays */
163 for (int i = 0; i <= NDWARVES; i++) {
164 if (valgame->dloc[i] < -1 || valgame->dloc[i] > NLOCATIONS ||
165 valgame->odloc[i] < -1 || valgame->odloc[i] > NLOCATIONS) {
170 for (int i = 0; i <= NOBJECTS; i++) {
171 if (valgame->place[i] < -1 || valgame->place[i] > NLOCATIONS ||
172 valgame->fixed[i] < -1 || valgame->fixed[i] > NLOCATIONS) {
177 /* Bounds check for dwarves */
178 if (valgame->dtotal < 0 || valgame->dtotal > NDWARVES ||
179 valgame->dkill < 0 || valgame->dkill > NDWARVES) {
183 /* Validate that we didn't die too many times in save */
184 if (valgame->numdie >= NDEATHS) {
188 /* Recalculate tally, throw the towel if in disagreement */
190 for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
191 if (objects[treasure].is_treasure) {
192 if (valgame->prop[treasure] == STATE_NOTFOUND) {
197 if (temp_tally != valgame->tally) {
201 /* Check that properties of objects aren't beyond expected */
202 for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
203 if (valgame->prop[obj] < STATE_NOTFOUND || valgame->prop[obj] > 1) {
216 if (valgame->prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
220 if (valgame->prop[BEAR] == CONTENTED_BEAR || valgame->prop[BEAR] == BEAR_DEAD)
229 /* Check that values in linked lists for objects in locations are inside bounds */
230 for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
231 if (valgame->atloc[loc] < NO_OBJECT || valgame->atloc[loc] > NOBJECTS * 2) {
235 for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
236 if (valgame->link[obj] < NO_OBJECT || valgame->link[obj] > NOBJECTS * 2) {