Isolate the grotty save/resume code. Most of it will soon go away.
[open-adventure.git] / saveresume.c
1 #include <stdlib.h>
2
3 #include "advent.h"
4 #include "database.h"
5 #include "linenoise/linenoise.h"
6
7 #define VRSION  25      /* bump on save format change */
8
9 static void fSAVWDS(long*,long*,long*,long*,long*,long*,long*);
10 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
11 static void fSAVARR(long*,long);
12 #define SAVARR(ARR,N) fSAVARR(ARR,N)
13 static void fSAVWRD(long,long*);
14 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
15 static void fSAVEIO(long,long,long*);
16 #define SAVEIO(OP,IN,ARR) fSAVEIO(OP,IN,ARR)
17
18 /*  Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
19
20 static void fSAVWDS(long *W1, long *W2, long *W3, long *W4,
21              long *W5, long *W6, long *W7)
22 /* Write or read 7 variables.  See SAVWRD. */
23 {
24     SAVWRD(0,(*W1));
25     SAVWRD(0,(*W2));
26     SAVWRD(0,(*W3));
27     SAVWRD(0,(*W4));
28     SAVWRD(0,(*W5));
29     SAVWRD(0,(*W6));
30     SAVWRD(0,(*W7));
31 }
32
33 static void fSAVARR(long arr[], long n)
34 /* Write or read an array of n words.  See SAVWRD. */
35 {
36     long i;
37
38     for (i=1; i<=n; i++) {
39         SAVWRD(0,arr[i]);
40     }
41     return;
42 }
43
44 static void fSAVWRD(long op, long *pword) 
45 /*  If OP<0, start writing a file, using word to initialise encryption; save
46  *  word in the file.  If OP>0, start reading a file; read the file to find
47  *  the value with which to decrypt the rest.  In either case, if a file is
48  *  already open, finish writing/reading it and don't start a new one.  If OP=0,
49  *  read/write a single word.  Words are buffered in case that makes for more
50  *  efficient disk use.  We also compute a simple checksum to catch elementary
51  *  poking within the saved file.  When we finish reading/writing the file,
52  *  we store zero into *PWORD if there's no checksum error, else nonzero. */
53 {
54     static long buf[250], cksum = 0, h1, hash = 0, n = 0, state = 0;
55
56     if (op != 0)
57     {
58         long ifvar = state; 
59         switch (ifvar<0 ? -1 : (ifvar>0 ? 1 : 0)) 
60         { 
61         case -1:
62         case 1:
63             if (n == 250)SAVEIO(1,state > 0,buf);
64             n=MOD(n,250)+1;
65             if (state <= 0) {
66                 n--; buf[n]=cksum; n++;
67                 SAVEIO(1,false,buf);
68             }
69             n--; *pword=buf[n]-cksum; n++;
70             SAVEIO(-1,state > 0,buf);
71             state=0;
72             break;
73         case 0: /* FIXME: Huh? should be impossible */
74             state=op;
75             SAVEIO(0,state > 0,buf);
76             n=1;
77             if (state <= 0) {
78                 hash=MOD(*pword,1048576L);
79                 buf[0]=1234L*5678L-hash;
80             }
81             SAVEIO(1,true,buf);
82             hash=MOD(1234L*5678L-buf[0],1048576L);
83             cksum=buf[0];
84             return;
85         }
86     }
87     if (state == 0)
88         return;
89     if (n == 250)
90         SAVEIO(1,state > 0,buf);
91     n=MOD(n,250)+1;
92     h1=MOD(hash*1093L+221573L,1048576L);
93     hash=MOD(h1*1093L+221573L,1048576L);
94     h1=MOD(h1,1234)*765432+MOD(hash,123);
95     n--;
96     if (state > 0)
97         *pword=buf[n]+h1;
98     buf[n]=*pword-h1;
99     n++;
100     cksum=MOD(cksum*13+*pword,1000000000L);
101 }
102
103 static void fSAVEIO(long op, long in, long arr[]) 
104 /*  If OP=0, ask for a file name and open a file.  (If IN=true, the file is for
105  *  input, else output.)  If OP>0, read/write ARR from/into the previously-opened
106  *  file.  (ARR is a 250-integer array.)  If OP<0, finish reading/writing the
107  *  file.  (Finishing writing can be a no-op if a "stop" statement does it
108  *  automatically.  Finishing reading can be a no-op as long as a subsequent
109  *  SAVEIO(0,false,X) will still work.) */
110 {
111     static FILE *fp = NULL;
112     char* name;
113
114     switch (op < 0 ? -1 : (op > 0 ? 1 : 0)) 
115     { 
116     case -1:
117         fclose(fp);
118         break;
119     case 0:
120         while (fp == NULL) {
121             name = linenoise("File name: ");
122             fp = fopen(name,(in ? READ_MODE : WRITE_MODE));
123             if (fp == NULL)
124                 printf("Can't open file %s, try again.\n", name); 
125         }
126         linenoiseFree(name);
127         break;
128     case 1: 
129         if (in)
130             IGNORE(fread(arr,sizeof(long),250,fp));
131         else
132             IGNORE(fwrite(arr,sizeof(long),250,fp));
133         break;
134     }
135 }
136
137 int saveresume(FILE *input, bool resume)
138 /* Suspend and resume */
139 {
140     int kk;
141     long i;
142     if (!resume) {
143         /*  Suspend.  Offer to save things in a file, but charging
144          *  some points (so can't win by using saved games to retry
145          *  battles or to start over after learning zzword). */
146         SPK=201;
147         RSPEAK(260);
148         if (!YES(input,200,54,54)) return(2012);
149         game.saved=game.saved+5;
150         kk= -1;
151     }
152     else
153     {
154         /*  Resume.  Read a suspended game back from a file. */
155         kk=1;
156         if (game.loc != 1 || game.abbrev[1] != 1) {
157             RSPEAK(268);
158             if (!YES(input,200,54,54)) return(2012);
159         }
160     }
161
162     /*  Suspend vs resume cases are distinguished by the value of kk
163      *  (-1 for suspend, +1 for resume). */
164
165     /* 
166      * FIXME: This is way more complicated than it needs to be in C.
167      * What we ought to do is define a save-block structure that
168      * includes a game state block and then use a single fread/fwrite
169      * for I/O. All the SAV* functions can be scrapped.
170      */
171
172     DATIME(&i,&K);
173     K=i+650*K;
174     SAVWRD(kk,K);
175     K=VRSION;
176     SAVWRD(0,K);
177     if (K != VRSION) {
178         SETPRM(1,K/10,MOD(K,10));
179         SETPRM(3,VRSION/10,MOD(VRSION,10));
180         RSPEAK(269);
181         return(2000);
182     }
183     /* Herewith are all the variables whose values can change during a game,
184      * omitting a few (such as I, J) whose values between turns are
185      * irrelevant and some whose values when a game is
186      * suspended or resumed are guaranteed to match.  If unsure whether a value
187      * needs to be saved, include it.  Overkill can't hurt.  Pad the last savwds
188      * with junk variables to bring it up to 7 values. */
189     SAVWDS(game.abbnum,game.blklin,game.bonus,game.clock1,game.clock2,game.closed,game.closng);
190     SAVWDS(game.detail,game.dflag,game.dkill,game.dtotal,game.foobar,game.holdng,game.iwest);
191     SAVWDS(game.knfloc,game.limit,K,game.lmwarn,game.loc,game.newloc,game.numdie);
192     SAVWDS(K,game.oldlc2,game.oldloc,game.oldobj,game.panic,game.saved,game.setup);
193     SAVWDS(SPK,game.tally,game.thresh,game.trndex,game.trnluz,game.turns,OBJTXT[OYSTER]);
194     SAVWDS(K,WD1,WD1X,WD2,game.wzdark,game.zzword,OBJSND[BIRD]);
195     SAVWDS(OBJTXT[SIGN],game.clshnt,game.novice,K,K,K,K);
196     SAVARR(game.abbrev,LOCSIZ);
197     SAVARR(game.atloc,LOCSIZ);
198     SAVARR(game.dloc,NDWARVES);
199     SAVARR(game.dseen,NDWARVES);
200     SAVARR(game.fixed,NOBJECTS);
201     SAVARR(game.hinted,HNTSIZ);
202     SAVARR(game.hintlc,HNTSIZ);
203     SAVARR(game.link,NOBJECTS*2);
204     SAVARR(game.odloc,NDWARVES);
205     SAVARR(game.place,NOBJECTS);
206     SAVARR(game.prop,NOBJECTS);
207     SAVWRD(kk,K);
208     if (K != 0) {
209         RSPEAK(270);
210         exit(0);
211     }
212     K=NUL;
213     game.zzword=RNDVOC(3,game.zzword);
214     if (kk > 0) return(8);
215     RSPEAK(266);
216     exit(0);
217 }