Complain to user on save/resume.restore when it's disabled.
[open-adventure.git] / saveresume.c
1 /*
2  * Saving and resuming.
3  *
4  * (ESR) This replaces  a bunch of particularly nasty FORTRAN-derived code;
5  * see the history.adoc file in the source distribution for discussion.
6  *
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
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <inttypes.h>
16
17 #include "advent.h"
18 #include "dungeon.h"
19
20 /*
21  * Bump on save format change.
22  *
23  * Note: Verify that the tests run clean before bumping this, then rebuild the check
24  * files afterwards.  Otherwise you will get a spurious failure due to the old version
25  * having been generated into a check file.
26  */
27 #define VRSION  29
28
29 /*
30  * If you change the first three members, the resume function may not properly
31  * reject saves from older versions.  Yes, this glues us to a hardware-
32  * dependent length of int.  Later members can change, but bump the version
33  * when you do that.
34  */
35 struct save_t {
36     int64_t savetime;
37     int32_t mode;               /* not used, must be present for version detection */
38     int32_t version;
39     struct game_t game;
40 };
41 struct save_t save;
42
43 #define IGNORE(r) do{if (r){}}while(0)
44
45 int savefile(FILE *fp, int32_t version)
46 /* Save game to file. No input or output from user. */
47 {
48     save.savetime = time(NULL);
49     save.mode = -1;
50     save.version = (version == 0) ? VRSION : version;
51
52     save.game = game;
53     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
54     return (0);
55 }
56
57 /* Suspend and resume */
58 int suspend(void)
59 {
60     /*  Suspend.  Offer to save things in a file, but charging
61      *  some points (so can't win by using saved games to retry
62      *  battles or to start over after learning zzword).
63      *  If ADVENT_NOSAVE is defined, gripe instead. */
64
65 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
66     rspeak(SAVERESUME_DISABLED)
67     return GO_TOP;
68 #endif
69     FILE *fp = NULL;
70
71     rspeak(SUSPEND_WARNING);
72     if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
73         return GO_CLEAROBJ;
74     game.saved = game.saved + 5;
75
76     while (fp == NULL) {
77         char* name = myreadline("\nFile name: ");
78         if (name == NULL)
79             return GO_TOP;
80         fp = fopen(name, WRITE_MODE);
81         if (fp == NULL)
82             printf("Can't open file %s, try again.\n", name);
83         free(name);
84     }
85
86     savefile(fp, VRSION);
87     fclose(fp);
88     rspeak(RESUME_HELP);
89     exit(EXIT_SUCCESS);
90 }
91
92 int resume(void)
93 {
94     /*  Resume.  Read a suspended game back from a file.
95      *  If ADVENT_NOSAVE is defined, gripe instead. */
96
97 #if defined ADVENT_NOSAVE || defined ADVENT_AUTOSAVE
98     rspeak(SAVERESUME_DISABLED)
99     return GO_TOP;
100 #endif
101     FILE *fp = NULL;
102
103     if (game.loc != 1 ||
104         game.abbrev[1] != 1) {
105         rspeak(RESUME_ABANDON);
106         if (!yes_or_no(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
107             return GO_CLEAROBJ;
108     }
109
110     while (fp == NULL) {
111         char* name = myreadline("\nFile name: ");
112         // Autocomplete can leave the input with an extra trailing space.
113         if (name != NULL && strlen(name) > 0 && name[strlen(name) - 1] == ' ')
114             name[strlen(name) - 1] = '\0';
115         if (name == NULL)
116             return GO_TOP;
117         fp = fopen(name, READ_MODE);
118         if (fp == NULL)
119             printf("Can't open file %s, try again.\n", name);
120         free(name);
121     }
122
123     return restore(fp);
124 }
125
126 int restore(FILE* fp)
127 {
128     /*  Read and restore game state from file, assuming
129      *  sane initial state.
130      *  If ADVENT_NOSAVE is defined, gripe instead. */
131 #ifdef ADVENT_NOSAVE
132     rspeak(SAVERESUME_DISABLED)
133     return GO_TOP;
134 #endif
135
136     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
137     fclose(fp);
138     if (save.version != VRSION) {
139         rspeak(VERSION_SKEW, save.version / 10, MOD(save.version, 10), VRSION / 10, MOD(VRSION, 10));
140     } else if (is_valid(save.game)) {
141         game = save.game;
142     }
143     return GO_TOP;
144 }
145
146 bool is_valid(struct game_t valgame)
147 {
148     /*  Save files can be roughly grouped into three groups:
149      *  With valid, reacheable state, with valid, but unreachable
150      *  state and with invalid state. We check that state is
151      *  valid: no states are outside minimal or maximal value
152      */
153
154     /* Prevent division by zero */
155     if (valgame.abbnum == 0) {
156         return false;   // LCOV_EXCL_LINE
157     }
158
159     /* Check for RNG overflow. Truncate */
160     if (valgame.lcg_x >= LCG_M) {
161         valgame.lcg_x %= LCG_M; // LCOV_EXCL_LINE
162     }
163
164     /* Check for RNG underflow. Transpose */
165     if (valgame.lcg_x < LCG_M) {
166         valgame.lcg_x = LCG_M + (valgame.lcg_x % LCG_M);
167     }
168
169     /*  Bounds check for locations */
170     if ( valgame.chloc  < -1 || valgame.chloc  > NLOCATIONS ||
171          valgame.chloc2 < -1 || valgame.chloc2 > NLOCATIONS ||
172          valgame.loc    <  0 || valgame.loc    > NLOCATIONS ||
173          valgame.newloc <  0 || valgame.newloc > NLOCATIONS ||
174          valgame.oldloc <  0 || valgame.oldloc > NLOCATIONS ||
175          valgame.oldlc2 <  0 || valgame.oldlc2 > NLOCATIONS) {
176         return false;   // LCOV_EXCL_LINE
177     }
178     /*  Bounds check for location arrays */
179     for (int i = 0; i <= NDWARVES; i++) {
180         if (valgame.dloc[i]  < -1 || valgame.dloc[i]  > NLOCATIONS  ||
181             valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
182             return false;       // LCOV_EXCL_LINE
183         }
184     }
185
186     for (int i = 0; i <= NOBJECTS; i++) {
187         if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS  ||
188             valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
189             return false;       // LCOV_EXCL_LINE
190         }
191     }
192
193     /*  Bounds check for dwarves */
194     if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
195         valgame.dkill < 0  || valgame.dkill  > NDWARVES) {
196         return false;   // LCOV_EXCL_LINE
197     }
198
199     /*  Validate that we didn't die too many times in save */
200     if (valgame.numdie >= NDEATHS) {
201         return false;   // LCOV_EXCL_LINE
202     }
203
204     /* Recalculate tally, throw the towel if in disagreement */
205     int temp_tally = 0;
206     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
207         if (objects[treasure].is_treasure) {
208             if (valgame.prop[treasure] == STATE_NOTFOUND) {
209                 ++temp_tally;
210             }
211         }
212     }
213     if (temp_tally != valgame.tally) {
214         return false;   // LCOV_EXCL_LINE
215     }
216
217     /* Check that properties of objects aren't beyond expected */
218     for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
219         if (valgame.prop[obj] < STATE_NOTFOUND || valgame.prop[obj] > 1) {
220             switch (obj) {
221             case RUG:
222             case DRAGON:
223             case BIRD:
224             case BOTTLE:
225             case PLANT:
226             case PLANT2:
227             case TROLL:
228             case URN:
229             case EGGS:
230             case VASE:
231             case CHAIN:
232                 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
233                     continue;
234             /* FALLTHRU */
235             case BEAR:
236                 if (valgame.prop[BEAR] == CONTENTED_BEAR || valgame.prop[BEAR] == BEAR_DEAD)
237                     continue;
238             /* FALLTHRU */
239             default:
240                 return false;   // LCOV_EXCL_LINE
241             }
242         }
243     }
244
245     /* Check that values in linked lists for objects in locations are inside bounds */
246     for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
247         if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
248             return false;       // LCOV_EXCL_LINE
249         }
250     }
251     for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
252         if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
253             return false;       // LCOV_EXCL_LINE
254         }
255     }
256
257     return true;
258 }
259
260 /* end */