5 #include "linenoise/linenoise.h"
7 #define VRSION 25 /* bump on save format change */
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)
18 /* Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
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. */
33 static void fSAVARR(long arr[], long n)
34 /* Write or read an array of n words. See SAVWRD. */
38 for (i=1; i<=n; i++) {
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. */
54 static long buf[250], cksum = 0, h1, hash = 0, n = 0, state = 0;
59 switch (ifvar<0 ? -1 : (ifvar>0 ? 1 : 0))
63 if (n == 250)SAVEIO(1,state > 0,buf);
66 n--; buf[n]=cksum; n++;
69 n--; *pword=buf[n]-cksum; n++;
70 SAVEIO(-1,state > 0,buf);
73 case 0: /* FIXME: Huh? should be impossible */
75 SAVEIO(0,state > 0,buf);
78 hash=MOD(*pword,1048576L);
79 buf[0]=1234L*5678L-hash;
82 hash=MOD(1234L*5678L-buf[0],1048576L);
90 SAVEIO(1,state > 0,buf);
92 h1=MOD(hash*1093L+221573L,1048576L);
93 hash=MOD(h1*1093L+221573L,1048576L);
94 h1=MOD(h1,1234)*765432+MOD(hash,123);
100 cksum=MOD(cksum*13+*pword,1000000000L);
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.) */
111 static FILE *fp = NULL;
114 switch (op < 0 ? -1 : (op > 0 ? 1 : 0))
121 name = linenoise("File name: ");
122 fp = fopen(name,(in ? READ_MODE : WRITE_MODE));
124 printf("Can't open file %s, try again.\n", name);
130 IGNORE(fread(arr,sizeof(long),250,fp));
132 IGNORE(fwrite(arr,sizeof(long),250,fp));
137 int saveresume(FILE *input, bool resume)
138 /* Suspend and 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). */
148 if (!YES(input,200,54,54)) return(2012);
149 game.saved=game.saved+5;
154 /* Resume. Read a suspended game back from a file. */
156 if (game.loc != 1 || game.abbrev[1] != 1) {
158 if (!YES(input,200,54,54)) return(2012);
162 /* Suspend vs resume cases are distinguished by the value of kk
163 * (-1 for suspend, +1 for resume). */
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.
178 SETPRM(1,K/10,MOD(K,10));
179 SETPRM(3,VRSION/10,MOD(VRSION,10));
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);
213 game.zzword=RNDVOC(3,game.zzword);
214 if (kk > 0) return(8);