Debian Codegraph @

terps/agility/savegame.c

raw symbols SCIP
1 /* savegame.c-- Routines for saving and restoring games */
2 /* Copyright (C) 1996-1999,2001 Robert Masenten */
3 /* This program may be redistributed under the terms of the
4 GNU General Public License, version 2; see agility.h for details. */
5 /* */
6 /* This is part of the source for AGiliTy */
7
8 #define SAVE_UNDO
9
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stddef.h>
13
14 #include "agility.h"
15 #include "interp.h"
16 #include "exec.h"
17
18 #define DEBUG_SAVE_SIZE 0
19
20 long state_size;
21
22
23 /*-------------------------------------------------------------------*/
24 /* INITIALISATION ROUTINES */
25 /* These initialize all of the values that can be derived from */
26 /* other data in the game file or that are reset when a game */
27 /* is restored */
28 /* See parser.c for the interpreter's main initialisation routines */
29
30 void init_vals(void)
31 /* Compute quantities that can be deduced from existing data */
32 {
33 int i;
34
35 quitflag=winflag=deadflag=endflag=0;
36 cmd_saveable=0;
37 last_he=last_she=last_it=0;
38 totwt=totsize=0;
39 for(i=0;i<=maxroom-first_room;i++)
40 room[i].contents=0;
41 player_contents=player_worn=0;
42 for(i=0;i<=maxnoun-first_noun;i++) {
43 if (player_has(i+first_noun)) totwt+=noun[i].weight;
44 if (noun[i].location==1) totsize+=noun[i].size;
45 noun[i].something_pos_near_noun=0;
46 noun[i].contents=noun[i].next=0;
47 }
48 for(i=0;i<=maxcreat-first_creat;i++)
49 creature[i].contents=creature[i].next=0;
50 for(i=0;i<=maxnoun-first_noun;i++) {
51 add_object(noun[i].location,i+first_noun);
52 if (noun[i].nearby_noun>=first_noun &&
53 noun[i].nearby_noun<=maxnoun)
54 noun[noun[i].nearby_noun-first_noun].something_pos_near_noun=1;
55 }
56 for(i=0;i<=maxcreat-first_creat;i++)
57 add_object(creature[i].location,i+first_creat);
58 objscore=0; /* Will need to recompute this ... */
59 }
60
61
62
63
64 /*-------------------------------------------------------------------*/
65 /* ROUTINES TO SAVE/RESTORE THE GAME STATE */
66 /* These are used by RESTART and UNDO as well as SAVE and RESTORE */
67
68 /* Game State format: */
69 /* The first two bytes indicate the length of the block (unsigned).*/
70 /* The next two bytes indicate the game file somehow (so we don't try to */
71 /* restore to a different game). */
72 /* After this comes the game information itself. */
73 /* All values are still little-endian (that is, LSB first) */
74
75 /* These are the macros for writing game information to the state block */
76 /* There is no difference between signed and unsigned when storing them;
77 there will be problems when recovering them again. */
78
79 #define g(ft,var) {ft,DT_DEFAULT,&var,0}
80 #define r(ft,str,f) {ft,DT_DEFAULT,NULL,offsetof(str,f)}
81 #define dptype {FT_DESCPTR,DT_DESCPTR,NULL,0}
82
83 static file_info fi_savehead[]={
84 g(FT_INT16,loc), g(FT_INT32,tscore), g(FT_INT16,turncnt),
85 g(FT_BYTE,statusmode),
86 g(FT_BOOL,first_visit_flag), g(FT_BOOL,newlife_flag),
87 g(FT_BOOL,room_firstdesc), g(FT_BOOL,verboseflag),
88 g(FT_BOOL,notify_flag), g(FT_BOOL,listexit_flag),
89 g(FT_BOOL,menu_mode), g(FT_BOOL,sound_on),
90 g(FT_BOOL,agt_answer),g(FT_INT32,agt_number),
91 g(FT_INT16,curr_time),g(FT_INT16,curr_lives),
92 g(FT_INT16,delta_time),
93 endrec
94 };
95
96 static file_info fi_saveroom[]={
97 dptype,
98 r(FT_BOOL,room_rec,seen),
99 r(FT_BOOL,room_rec,locked_door),
100 r(FT_INT16,room_rec,oclass),
101 r(FT_INT16,room_rec,points),
102 r(FT_INT16,room_rec,light),
103 r(FT_PATHARRAY,room_rec,path),
104 r(FT_UINT32,room_rec,flag_noun_bits),
105 endrec
106 };
107
108 static file_info fi_savenoun[]={
109 dptype,
110 r(FT_INT16,noun_rec,location),
111 r(FT_INT16,noun_rec,nearby_noun),
112 r(FT_INT16,noun_rec,num_shots),
113 r(FT_INT16,noun_rec,initdesc),
114 r(FT_INT16,noun_rec,oclass),
115 r(FT_INT16,noun_rec,points),
116 r(FT_INT16,noun_rec,weight),
117 r(FT_INT16,noun_rec,size),
118 r(FT_BOOL,noun_rec,on),
119 r(FT_BOOL,noun_rec,open),
120 r(FT_BOOL,noun_rec,locked),
121 r(FT_BOOL,noun_rec,movable),
122 r(FT_BOOL,noun_rec,seen),
123 r(FT_WORD,noun_rec,pos_prep),
124 r(FT_WORD,noun_rec,pos_name),
125 endrec
126 };
127
128 static file_info fi_savecreat[]={
129 dptype,
130 r(FT_INT16,creat_rec,location),
131 r(FT_INT16,creat_rec,counter),
132 r(FT_INT16,creat_rec,timecounter),
133 r(FT_INT16,creat_rec,initdesc),
134 r(FT_INT16,creat_rec,oclass),
135 r(FT_INT16,creat_rec,points),
136 r(FT_BOOL,creat_rec,groupmemb),
137 r(FT_BOOL,creat_rec,hostile),
138 r(FT_BOOL,creat_rec,seen),
139 endrec
140 };
141
142 static file_info fi_saveustr[]={
143 {FT_TLINE,DT_DEFAULT,NULL,0},
144 endrec
145 };
146
147
148
149 uchar *getstate(uchar *gs)
150 /* Returns block containing game state.
151 If gs!=NULL, uses that space as a buffer;
152 if gs==NULL, we malloc a new block and return it */
153 {
154 rbool new_block; /* True if we allocate a new block */
155 long bp;
156
157 if (gs==NULL) {
158 rm_trap=0; /* Don't exit on out-of-memory condition */
159 gs=rmalloc(state_size); /* This should be enough. */
160 rm_trap=1;
161 if (gs==NULL) /* This is why we set rm_trap to 0 before calling rmalloc */
162 return NULL;
163 new_block=1;
164 } else new_block=0;
165
166 /* First two bytes reserved for block size, which we don't know yet.*/
167 gs[4]=game_sig&0xFF;
168 gs[5]=(game_sig>>8)&0xFF;
169
170 tscore-=objscore; /* Only include "permanent" part of score;
171 objscore we can recompute on RESTORE */
172
173 /* Need to setup here */
174 set_internal_buffer(gs);
175 fi_saveroom[0].ptr=room_ptr;
176 fi_savenoun[0].ptr=noun_ptr;
177 fi_savecreat[0].ptr=creat_ptr;
178
179 bp=6;
180 bp+=write_globalrec(fi_savehead,bp);
181 bp+=write_recblock(flag,FT_BYTE,FLAG_NUM+1,bp);
182 bp+=write_recblock(agt_counter,FT_INT16,CNT_NUM+1,bp);
183 bp+=write_recblock(agt_var,FT_INT32,VAR_NUM+1,bp);
184 bp+=write_recarray(room,sizeof(room_rec),rangefix(maxroom-first_room+1),
185 fi_saveroom,bp);
186 bp+=write_recarray(noun,sizeof(noun_rec),rangefix(maxnoun-first_noun+1),
187 fi_savenoun,bp);
188 bp+=write_recarray(creature,sizeof(creat_rec),
189 rangefix(maxcreat-first_creat+1),
190 fi_savecreat,bp);
191 if (userstr!=NULL)
192 bp+=write_recarray(userstr,sizeof(tline),MAX_USTR,fi_saveustr,bp);
193 if (objflag!=NULL)
194 bp+=write_recblock(objflag,FT_BYTE, objextsize(0), bp);
195 if (objprop!=NULL)
196 bp+=write_recblock(objprop,FT_INT32, objextsize(1), bp);
197 set_internal_buffer(NULL);
198 gs[0]=bp & 0xFF;
199 gs[1]=(bp>>8) & 0xFF;
200 gs[2]=(bp>>16) & 0xFF;
201 gs[3]=(bp>>24) & 0x7F; /* Don't trust top bit */
202 if (new_block)
203 gs=rrealloc(gs,bp);
204 tscore+=objscore;
205 return gs;
206 }
207
208
209
210 void putstate(uchar *gs) /* Restores games state. */
211 {
212 long size, bp, numrec, i;
213
214
215 size=gs[0]+(((long)gs[1])<<8)+(((long)gs[2])<<16)+(((long)gs[3])<<24);
216 if (size!=state_size) {
217 writeln("Size difference in save files!");
218 agt_delay(3);
219 return;
220 }
221 if (gs[4]+(((long)gs[5])<<8)!=game_sig) {
222 writestr("This appears to be a save file for a different game. Is this"
223 " from an earlier chapter in a multi-part game such as"
224 " Klaustrophobia");
225 if (yesno("?"))
226 skip_descr=1; /* We don't want to overwrite the descriptions
227 with the pointers from the save file. */
228 else {
229 writestr("Do you want to try using it anyhow (WARNING: This could"
230 " crash the interpreter)");
231 if (!(yesno("?"))) {
232 writeln("Command cancelled!");
233 agt_delay(3);
234 return;
235 }
236 }
237 }
238
239
240 /* setup... */
241 set_internal_buffer(gs);
242 fi_saveroom[0].ptr=room_ptr;
243 fi_savenoun[0].ptr=noun_ptr;
244 fi_savecreat[0].ptr=creat_ptr;
245 bp=6;
246
247 read_globalrec(fi_savehead,0,bp,0);
248 bp+=compute_recsize(fi_savehead);
249 read_recblock(flag,FT_BYTE,FLAG_NUM+1,bp,0);
250 bp+=ft_leng[FT_BYTE]*(FLAG_NUM+1);
251 read_recblock(agt_counter,FT_INT16,CNT_NUM+1,bp,0);
252 bp+=ft_leng[FT_INT16]*(CNT_NUM+1);
253 read_recblock(agt_var,FT_INT32,VAR_NUM+1,bp,0);
254 bp+=ft_leng[FT_INT32]*(VAR_NUM+1);
255
256 numrec=rangefix(maxroom-first_room+1);
257 read_recarray(room,sizeof(room_rec),numrec,fi_saveroom,0,bp,0);
258 bp+=compute_recsize(fi_saveroom)*numrec;
259 numrec=rangefix(maxnoun-first_noun+1);
260 read_recarray(noun,sizeof(noun_rec),numrec,fi_savenoun,0,bp,0);
261 bp+=compute_recsize(fi_savenoun)*numrec;
262 numrec=rangefix(maxcreat-first_creat+1);
263 read_recarray(creature,sizeof(creat_rec),numrec,fi_savecreat,0,bp,0);
264 bp+=compute_recsize(fi_savecreat)*numrec;
265 if (userstr!=NULL) {
266 read_recarray(userstr,sizeof(tline),MAX_USTR,fi_saveustr,0,bp,0);
267 bp+=ft_leng[FT_TLINE]*MAX_USTR;
268 }
269 if (objflag!=NULL) {
270 i=objextsize(0);
271 read_recblock(objflag,FT_BYTE,i,bp,0);
272 bp+=ft_leng[FT_BYTE]*i;
273 }
274 if (objprop!=NULL) {
275 i=objextsize(1);
276 read_recblock(objprop,FT_INT32,i,bp,0);
277 bp+=ft_leng[FT_INT32]*i;
278 }
279 set_internal_buffer(NULL);
280
281 if (skip_descr) /* Need to "fix" position information. This is a hack. */
282 /* Basically, this sets the position of each object to its default */
283 /* The problem here is that the usual position info is invalid-- we've
284 changed games, and hence dictionaries */
285 for(i=0;i<maxnoun-first_noun;i++) {
286 if (noun[i].position!=NULL && noun[i].position[0]!=0)
287 noun[i].pos_prep=-1;
288 else noun[i].pos_prep=0;
289 }
290 else /* Rebuild position information */
291 for(i=0;i<maxnoun-first_noun;i++)
292 if (noun[i].pos_prep==-1)
293 noun[i].position=noun[i].initpos;
294 else
295 noun[i].position=NULL;
296
297 init_vals();
298 skip_descr=0; /* If we set this to 1, restore it to its original state */
299 /* Now do some simple consistancy checking on major variables */
300 if (loc>maxroom || loc<0 || turncnt<0 ||
301 curr_lives<0 || curr_lives>max_lives) {
302 writeln("Error: Save file inconsistent.");
303 exit(EXIT_FAILURE);
304 }
305 }
306
307 void init_state_sys(void)
308 /* Initializes the state saving mechanisms */
309 /* Mainly it just computes the size of a state block */
310 {
311 state_size=compute_recsize(fi_savehead)
312 + compute_recsize(fi_saveroom)*rangefix(maxroom-first_room+1)
313 + compute_recsize(fi_savenoun)*rangefix(maxnoun-first_noun+1)
314 + compute_recsize(fi_savecreat)*rangefix(maxcreat-first_creat+1)
315 + ft_leng[FT_BYTE]*(FLAG_NUM+1)
316 + ft_leng[FT_INT16]*(CNT_NUM+1)
317 + ft_leng[FT_INT32]*(VAR_NUM+1)
318 + ft_leng[FT_BYTE]*objextsize(0)
319 + ft_leng[FT_INT32]*objextsize(1)
320 + 6; /* Six bytes in header */
321 if (userstr!=NULL) state_size+=ft_leng[FT_TLINE]*MAX_USTR;
322 }
323
324
325 /*-------------------------------------------------------------------*/
326 /* SAVE FILE ROUTINES */
327
328 void savegame(void)
329 {
330 genfile savefile;
331 uchar *gs;
332 long size;
333
334 #ifndef UNDO_SAVE
335 gs=getstate(NULL);
336 #else
337 gs=undo_state;
338 #endif
339 if (gs==NULL) {
340 writeln("Insufficiant memory to support SAVE.");
341 return;
342 }
343 savefile=get_user_file(1);
344 if (!filevalid(savefile,fSAV)) {
345 writeln("That is not a valid save file.");
346 return;
347 }
348 size=gs[0]+(((long)gs[1])<<8)+(((long)gs[2])<<16)+(((long)gs[3])<<24);
349 if (!binwrite(savefile,gs,size,1,0))
350 writeln("Error writing save file.");
351 #ifndef UNDO_SAVE
352 rfree(gs);
353 #endif
354 writeclose(savefile,NO_FILE_ID);
355 }
356
357
358 rbool loadgame(void)
359 /* 1=success, 0=failure */
360 {
361 genfile loadfile;
362 long size;
363 uchar *gs;
364 char *errstr;
365
366 loadfile=get_user_file(2);
367 if (!filevalid(loadfile,fSAV)) {
368 writeln("Unable to open file.");
369 return 0;
370 }
371 size=binsize(loadfile);
372 if (size==-1) {
373 writeln("Could not access file.");
374 readclose(loadfile);
375 return 0;
376 }
377 gs=rmalloc(size);
378 if (!binread(loadfile,gs,size,1,&errstr)) {
379 writeln("Error reading file.");
380 rfree(gs);readclose(loadfile);
381 return 0;
382 }
383 readclose(loadfile);
384 if (size!=gs[0]+(((long)gs[1])<<8)+(((long)gs[2])<<16)+(((long)gs[3])<<24)){
385 if (size==gs[0]+(((long)gs[1])<<8)) {
386 /* Old save file format; patch to look like new format */
387 gs=rrealloc(gs,size+2);
388 memmove(gs+4,gs+2,size-2);
389 gs[2]=gs[3]=0;
390 } else {
391 writeln("Save file corrupted or invalid.");
392 rfree(gs);
393 return 0;
394 }
395 }
396 putstate(gs);
397 rfree(gs);
398 set_statline();
399 look_room();
400 return 1;
401 }
402
403 void restart_game(void)
404 {
405 putstate(restart_state);
406 reset_random();
407 agt_clrscr();
408 set_statline();
409 do_look=do_autoverb=1;
410 if (intro_ptr.size>0) {
411 print_descr(intro_ptr,1);
412 wait_return();
413 agt_clrscr();
414 }
415 newroom();
416 }
417