Prevent division by zero
[open-adventure.git] / saveresume.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <editline/readline.h>
4 #include <time.h>
5
6 #include "advent.h"
7 #include "dungeon.h"
8
9 /*
10  * (ESR) This replaces  a bunch of particularly nasty FORTRAN-derived code;
11  * see the history.adoc file in the source distribution for discussion.
12  */
13
14 #define VRSION  27      /* bump on save format change */
15
16 /*
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
20  * when you do that.
21  */
22 struct save_t {
23     long savetime;
24     long mode;          /* not used, must be present for version detection */
25     long version;
26     struct game_t game;
27 };
28 struct save_t save;
29
30 #define IGNORE(r) do{if (r){}}while(0)
31
32 int savefile(FILE *fp, long version)
33 /* Save game to file. No input or output from user. */
34 {
35     save.savetime = time(NULL);
36     save.mode = -1;
37     save.version = (version == 0) ? VRSION : version;
38
39     save.game = game;
40     IGNORE(fwrite(&save, sizeof(struct save_t), 1, fp));
41     return (0);
42 }
43
44 /* Suspend and resume */
45 int suspend(void)
46 {
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. */
51
52 #ifdef ADVENT_NOSAVE
53     return GO_UNKNOWN;
54 #endif
55     FILE *fp = NULL;
56
57     rspeak(SUSPEND_WARNING);
58     if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
59         return GO_CLEAROBJ;
60     game.saved = game.saved + 5;
61
62     while (fp == NULL) {
63         char* name = readline("\nFile name: ");
64         if (name == NULL)
65             return GO_TOP;
66         fp = fopen(name, WRITE_MODE);
67         if (fp == NULL)
68             printf("Can't open file %s, try again.\n", name);
69         free(name);
70     }
71
72     savefile(fp, VRSION);
73     fclose(fp);
74     rspeak(RESUME_HELP);
75     exit(EXIT_SUCCESS);
76 }
77
78 int resume(void)
79 {
80     /*  Resume.  Read a suspended game back from a file.
81      *  If ADVENT_NOSAVE is defined, do nothing instead. */
82
83 #ifdef ADVENT_NOSAVE
84     return GO_UNKNOWN;
85 #endif
86     FILE *fp = NULL;
87
88     if (game.loc != 1 ||
89         game.abbrev[1] != 1) {
90         rspeak(RESUME_ABANDON);
91         if (!yes(arbitrary_messages[THIS_ACCEPTABLE], arbitrary_messages[OK_MAN], arbitrary_messages[OK_MAN]))
92             return GO_CLEAROBJ;
93     }
94
95     while (fp == NULL) {
96         char* name = readline("\nFile name: ");
97         if (name == NULL)
98             return GO_TOP;
99         fp = fopen(name, READ_MODE);
100         if (fp == NULL)
101             printf("Can't open file %s, try again.\n", name);
102         free(name);
103     }
104
105     return restore(fp);
106 }
107
108 bool is_valid(struct game_t);
109
110 int restore(FILE* fp)
111 {
112     /*  Read and restore game state from file, assuming
113      *  sane initial state.
114      *  If ADVENT_NOSAVE is defined, do nothing instead. */
115 #ifdef ADVENT_NOSAVE
116     return GO_UNKNOWN;
117 #endif
118
119     IGNORE(fread(&save, sizeof(struct save_t), 1, fp));
120     fclose(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)) {
124         game = save.game;
125     }
126     return GO_TOP;
127 }
128
129 bool is_valid(struct game_t valgame)
130 {
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
135      */
136
137     /* Prevent division by zero */
138     if (valgame.abbnum == 0) {
139         return false;
140     }
141
142     /*  Bounds check for locations */
143     if ( valgame.chloc < -1  || valgame.chloc > NLOCATIONS  ||
144          valgame.chloc < -1  || valgame.chloc > NLOCATIONS  ||
145          valgame.loc < -1    || valgame.loc > NLOCATIONS    ||
146          valgame.newloc < -1 || valgame.newloc > NLOCATIONS ||
147          valgame.oldloc < -1 || valgame.oldloc > NLOCATIONS ||
148          valgame.oldloc < -1 || valgame.oldloc > NLOCATIONS) {
149         return false;
150     }
151     /*  Bounds check for location arrays */
152     for (int i = 0; i <= NDWARVES; i++) {
153         if (valgame.dloc[i]  < -1 || valgame.dloc[i]  > NLOCATIONS  ||
154             valgame.odloc[i] < -1 || valgame.odloc[i] > NLOCATIONS) {
155             return false;
156         }
157     }
158
159     for (int i = 0; i <= NOBJECTS; i++) {
160         if (valgame.place[i] < -1 || valgame.place[i] > NLOCATIONS  ||
161             valgame.fixed[i] < -1 || valgame.fixed[i] > NLOCATIONS) {
162             return false;
163         }
164     }
165
166     /*  Bounds check for dwarves */
167     if (valgame.dtotal < 0 || valgame.dtotal > NDWARVES ||
168         valgame.dkill < 0 || valgame.dkill > NDWARVES) {
169         return false;
170     }
171
172     /*  Validate that we didn't die too many times in save */
173     if (valgame.numdie >= NDEATHS) {
174         return false;
175     }
176
177     /* Recalculate tally, throw the towel if in disagreement */
178     long temp_tally = 0;
179     for (int treasure = 1; treasure <= NOBJECTS; treasure++) {
180         if (objects[treasure].is_treasure) {
181             if (valgame.prop[treasure] == STATE_NOTFOUND) {
182                 ++temp_tally;
183             }
184         }
185     }
186     if (temp_tally != valgame.tally) {
187         return false;
188     }
189
190     /* Check that properties of objects aren't beyond expected */
191     for (obj_t obj = 0; obj <= NOBJECTS; obj++) {
192         if (valgame.prop[obj] < STATE_NOTFOUND || valgame.prop[obj] > 1) {
193             switch (obj) {
194             case RUG:
195             case DRAGON:
196             case BIRD:
197             case BOTTLE:
198             case PLANT:
199             case PLANT2:
200             case TROLL:
201             case URN:
202             case EGGS:
203             case VASE:
204             case CHAIN:
205                 if (valgame.prop[obj] == 2) // There are multiple different states, but it's convenient to clump them together
206                     continue;
207             case BEAR:
208                 if (valgame.prop[BEAR] == CONTENTED_BEAR || game.prop[BEAR] == BEAR_DEAD)
209                     continue;
210             default:
211                 return false;
212             }
213         }
214     }
215
216     /* Check that values in linked lists for objects in locations are inside bounds */
217     for (loc_t loc = LOC_NOWHERE; loc <= NLOCATIONS; loc++) {
218         if (valgame.atloc[loc] < NO_OBJECT || valgame.atloc[loc] > NOBJECTS * 2) {
219             return false;
220         }
221     }
222     for (obj_t obj = 0; obj <= NOBJECTS * 2; obj++ ) {
223         if (valgame.link[obj] < NO_OBJECT || valgame.link[obj] > NOBJECTS * 2) {
224             return false;
225         }
226     }
227
228     return true;
229 }
230
231 /* end */