Just say no to encrypting internal state.
[open-adventure.git] / misc.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "main.h"
6 #include "share.h"      /* for SETUP */
7 #include "misc.h"
8 #include "funcs.h"
9
10 /* hack to ignore GCC Unused Result */
11 #define IGNORE(r) do{if(r){}}while(0)
12
13 /*  I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
14
15 #undef SPEAK
16 void fSPEAK(long N) {
17 long BLANK, CASE, I, K, L, NEG, NPARMS, PARM, PRMTYP, STATE;
18
19 /*  Print the message which starts at LINES(N).  Precede it with a blank line
20  *  unless BLKLIN is false. */
21
22
23         if(N == 0)return;
24         BLANK=BLKLIN;
25         K=N;
26         NPARMS=1;
27 L10:    L=IABS(LINES[K])-1;
28         K=K+1;
29         LNLENG=0;
30         LNPOSN=1;
31         STATE=0;
32         for (I=K; I<=L; I++) {
33         PUTTXT(LINES[I],STATE,2);
34         } /* end loop */
35         LNPOSN=0;
36 L30:    LNPOSN=LNPOSN+1;
37 L32:    if(LNPOSN > LNLENG) goto L40;
38         if(INLINE[LNPOSN] != 63) goto L30;
39         {long x = LNPOSN+1; PRMTYP=INLINE[x];}
40 /*  63 is a "%"; the next character determine the type of parameter:  1 (!) =
41  *  suppress message completely, 29 (S) = NULL If PARM=1, else 'S' (optional
42  *  plural ending), 33 (W) = word (two 30-bit values) with trailing spaces
43  *  suppressed, 22 (L) or 31 (U) = word but map to lower/upper case, 13 (C) =
44  *  word in lower case with first letter capitalised, 30 (T) = text ending
45  *  with a word of -1, 65-73 (1-9) = number using that many characters,
46  *  12 (B) = variable number of blanks. */
47         if(PRMTYP == 1)return;
48         if(PRMTYP == 29) goto L320;
49         if(PRMTYP == 30) goto L340;
50         if(PRMTYP == 12) goto L360;
51         if(PRMTYP == 33 || PRMTYP == 22 || PRMTYP == 31 || PRMTYP == 13) goto
52                 L380;
53         PRMTYP=PRMTYP-64;
54         if(PRMTYP < 1 || PRMTYP > 9) goto L30;
55         SHFTXT(LNPOSN+2,PRMTYP-2);
56         LNPOSN=LNPOSN+PRMTYP;
57         PARM=IABS(PARMS[NPARMS]);
58         NEG=0;
59         if(PARMS[NPARMS] < 0)NEG=9;
60         /* 390 */ for (I=1; I<=PRMTYP; I++) {
61         LNPOSN=LNPOSN-1;
62         INLINE[LNPOSN]=MOD(PARM,10)+64;
63         if(I == 1 || PARM != 0) goto L390;
64         INLINE[LNPOSN]=NEG;
65         NEG=0;
66 L390:   PARM=PARM/10;
67         } /* end loop */
68         LNPOSN=LNPOSN+PRMTYP;
69 L395:   NPARMS=NPARMS+1;
70          goto L32;
71
72 L320:   SHFTXT(LNPOSN+2,-1);
73         INLINE[LNPOSN]=55;
74         if(PARMS[NPARMS] == 1)SHFTXT(LNPOSN+1,-1);
75          goto L395;
76
77 L340:   SHFTXT(LNPOSN+2,-2);
78         STATE=0;
79         CASE=2;
80 L345:   if(PARMS[NPARMS] < 0) goto L395;
81         {long x = NPARMS+1; if(PARMS[x] < 0)CASE=0;}
82         PUTTXT(PARMS[NPARMS],STATE,CASE);
83         NPARMS=NPARMS+1;
84          goto L345;
85
86 L360:   PRMTYP=PARMS[NPARMS];
87         SHFTXT(LNPOSN+2,PRMTYP-2);
88         if(PRMTYP == 0) goto L395;
89         for (I=1; I<=PRMTYP; I++) {
90         INLINE[LNPOSN]=0;
91         LNPOSN=LNPOSN+1;
92         } /* end loop */
93          goto L395;
94
95 L380:   SHFTXT(LNPOSN+2,-2);
96         STATE=0;
97         CASE= -1;
98         if(PRMTYP == 31)CASE=1;
99         if(PRMTYP == 33)CASE=0;
100         I=LNPOSN;
101         PUTTXT(PARMS[NPARMS],STATE,CASE);
102         {long x = NPARMS+1; PUTTXT(PARMS[x],STATE,CASE);}
103         if(PRMTYP == 13 && INLINE[I] >= 37 && INLINE[I] <=
104                 62)INLINE[I]=INLINE[I]-26;
105         NPARMS=NPARMS+2;
106          goto L32;
107
108 L40:    if(BLANK)TYPE0();
109         BLANK=false;
110         TYPE();
111         K=L+1;
112         if(LINES[K] >= 0) goto L10;
113         return;
114 }
115
116
117
118 #define SPEAK(N) fSPEAK(N)
119 #undef PSPEAK
120 void fPSPEAK(long MSG,long SKIP) {
121 long I, M;
122
123 /*  Find the skip+1st message from msg and print it.  MSG should be the index of
124  *  the inventory message for object.  (INVEN+N+1 message is PROP=N message). */
125
126
127         M=PTEXT[MSG];
128         if(SKIP < 0) goto L9;
129         for (I=0; I<=SKIP; I++) {
130 L1:     M=IABS(LINES[M]);
131         if(LINES[M] >= 0) goto L1;
132         /*etc*/ ;
133         } /* end loop */
134 L9:     SPEAK(M);
135         return;
136 }
137
138
139
140 #define PSPEAK(MSG,SKIP) fPSPEAK(MSG,SKIP)
141 #undef RSPEAK
142 void fRSPEAK(long I) {
143 ;
144
145 /*  Print the I-TH "random" message (section 6 of database). */
146
147
148         if(I != 0)SPEAK(RTEXT[I]);
149         return;
150 }
151
152
153
154 #define RSPEAK(I) fRSPEAK(I)
155 #undef SETPRM
156 void fSETPRM(long FIRST, long P1, long P2) {
157 ;
158
159 /*  Stores parameters into the PRMCOM parms array for use by speak.  P1 and P2
160  *  are stored into PARMS(FIRST) and PARMS(FIRST+1). */
161
162
163         if(FIRST >= 25)BUG(29);
164         PARMS[FIRST]=P1;
165         {long x = FIRST+1; PARMS[x]=P2;}
166         return;
167 }
168
169
170
171 #define SETPRM(FIRST,P1,P2) fSETPRM(FIRST,P1,P2)
172 #undef GETIN
173 #define WORD1 (*wORD1)
174 #define WORD1X (*wORD1X)
175 #define WORD2 (*wORD2)
176 #define WORD2X (*wORD2X)
177 bool fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) {
178 long JUNK;
179
180 /*  Get a command from the adventurer.  Snarf out the first word, pad it with
181  *  blanks, and return it in WORD1.  Chars 6 thru 10 are returned in WORD1X, in
182  *  case we need to print out the whole word in an error message.  Any number of
183  *  blanks may follow the word.  If a second word appears, it is returned in
184  *  WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
185
186
187 L10:    if(BLKLIN)TYPE0();
188         MAPLIN(input);
189         if (feof(input))
190             return false;
191         WORD1=GETTXT(true,true,true);
192         if(BLKLIN && WORD1 < 0) goto L10;
193         WORD1X=GETTXT(false,true,true);
194 L12:    JUNK=GETTXT(false,true,true);
195         if(JUNK > 0) goto L12;
196         WORD2=GETTXT(true,true,true);
197         WORD2X=GETTXT(false,true,true);
198 L22:    JUNK=GETTXT(false,true,true);
199         if(JUNK > 0) goto L22;
200         if(GETTXT(true,true,true) <= 0)return true;
201         RSPEAK(53);
202          goto L10;
203 }
204
205
206
207 #undef WORD1
208 #undef WORD1X
209 #undef WORD2
210 #undef WORD2X
211 #define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
212 #undef YES
213 long fYES(FILE *input, long X, long Y, long Z) {
214
215 long YES, REPLY, JUNK1, JUNK2, JUNK3;
216
217 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
218  *  if no, print Z and return false. */
219
220 L1:     RSPEAK(X);
221         GETIN(input, REPLY,JUNK1,JUNK2,JUNK3);
222         if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
223         if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
224         RSPEAK(185);
225          goto L1;
226 L10:    YES=true;
227         RSPEAK(Y);
228         return(YES);
229 L20:    YES=false;
230         RSPEAK(Z);
231         return(YES);
232 }
233
234
235
236
237
238 /*  Line-parsing routines (GETNUM, GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
239                 */
240
241 /*  The routines on this page handle all the stuff that would normally be
242  *  taken care of by format statements.  We do it this way instead so that
243  *  we can handle textual data in a machine independent fashion.  All the
244  *  machine dependent i/o stuff is on the following page.  See that page
245  *  for a description of MAPCOM's inline array. */
246
247 #define YES(X,Y,Z) fYES(X,Y,Z)
248 #undef GETNUM
249 long fGETNUM(FILE *source) {
250 long DIGIT, GETNUM, SIGN;
251
252 /*  Obtain the next integer from an input line.  If K>0, we first read a
253  *  new input line from a file; if K<0, we read a line from the keyboard;
254  *  if K=0 we use a line that has already been read (and perhaps partially
255  *  scanned).  If we're at the end of the line or encounter an illegal
256  *  character (not a digit, hyphen, or blank), we return 0. */
257
258
259         if(source != NULL)MAPLIN(source);
260         GETNUM=0;
261 L10:    if(LNPOSN > LNLENG)return(GETNUM);
262         if(INLINE[LNPOSN] != 0) goto L20;
263         LNPOSN=LNPOSN+1;
264          goto L10;
265
266 L20:    SIGN=1;
267         if(INLINE[LNPOSN] != 9) goto L32;
268         SIGN= -1;
269 L30:    LNPOSN=LNPOSN+1;
270 L32:    if(LNPOSN > LNLENG || INLINE[LNPOSN] == 0) goto L42;
271         DIGIT=INLINE[LNPOSN]-64;
272         if(DIGIT < 0 || DIGIT > 9) goto L40;
273         GETNUM=GETNUM*10+DIGIT;
274          goto L30;
275
276 L40:    GETNUM=0;
277 L42:    GETNUM=GETNUM*SIGN;
278         LNPOSN=LNPOSN+1;
279         return(GETNUM);
280 }
281
282
283
284 #define GETNUM(K) fGETNUM(K)
285 #undef GETTXT
286 long fGETTXT(long SKIP,long ONEWRD, long UPPER) {
287 long CHAR, GETTXT, I; static long SPLITTING = -1;
288
289 /*  Take characters from an input line and pack them into 30-bit words.
290  *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
291  *  blank.  UPPER says to map all letters to uppercase.  If we reach the
292  *  end of the line, the word is filled up with blanks (which encode as 0's).
293  *  If we're already at end of line when GETTXT is called, we return -1. */
294
295         if(LNPOSN != SPLITTING)SPLITTING = -1;
296         GETTXT= -1;
297 L10:    if(LNPOSN > LNLENG)return(GETTXT);
298         if((!SKIP) || INLINE[LNPOSN] != 0) goto L11;
299         LNPOSN=LNPOSN+1;
300          goto L10;
301
302 L11:    GETTXT=0;
303         /* 15 */ for (I=1; I<=5; I++) {
304         GETTXT=GETTXT*64;
305         if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) goto L15;
306         CHAR=INLINE[LNPOSN];
307         if(CHAR >= 63) goto L12;
308         SPLITTING = -1;
309         if(UPPER && CHAR >= 37)CHAR=CHAR-26;
310         GETTXT=GETTXT+CHAR;
311          goto L14;
312
313 L12:    if(SPLITTING == LNPOSN) goto L13;
314         GETTXT=GETTXT+63;
315         SPLITTING = LNPOSN;
316          goto L15;
317
318 L13:    GETTXT=GETTXT+CHAR-63;
319         SPLITTING = -1;
320 L14:    LNPOSN=LNPOSN+1;
321 L15:    /*etc*/ ;
322         } /* end loop */
323
324         return(GETTXT);
325 }
326
327
328
329 #define GETTXT(SKIP,ONEWRD,UPPER) fGETTXT(SKIP,ONEWRD,UPPER)
330 #undef MAKEWD
331 long fMAKEWD(long LETTRS) {
332 long I, L, MAKEWD;
333
334 /*  Combine five uppercase letters (represented by pairs of decimal digits
335  *  in lettrs) to form a 30-bit value matching the one that GETTXT would
336  *  return given those characters plus trailing blanks.  Caution:
337  *  lettrs will overflow 31 bits if 5-letter word starts with V-Z.  As a
338  *  kludgey workaround, you can increment a letter by 5 by adding 50 to
339  *  the next pair of digits. */
340
341
342         MAKEWD=0;
343         I=1;
344         L=LETTRS;
345 L10:    MAKEWD=MAKEWD+I*(MOD(L,50)+10);
346         I=I*64;
347         if(MOD(L,100) > 50)MAKEWD=MAKEWD+I*5;
348         L=L/100;
349         if(L != 0) goto L10;
350         I=64L*64L*64L*64L*64L/I;
351         MAKEWD=MAKEWD*I;
352         return(MAKEWD);
353 }
354
355
356
357 #define MAKEWD(LETTRS) fMAKEWD(LETTRS)
358 #undef PUTTXT
359 #define STATE (*sTATE)
360 void fPUTTXT(long WORD, long *sTATE, long CASE) {
361 long ALPH1, ALPH2, BYTE, DIV, I, W;
362
363 /*  Unpack the 30-bit value in word to obtain up to 5 integer-encoded chars,
364  *  and store them in inline starting at LNPOSN.  If LNLENG>=LNPOSN, shift
365  *  existing characters to the right to make room.  STATE will be zero when
366  *  puttxt is called with the first of a sequence of words, but is thereafter
367  *  unchanged by the caller, so PUTTXT can use it to maintain state across
368  *  calls.  LNPOSN and LNLENG are incremented by the number of chars stored.
369  *  If CASE=1, all letters are made uppercase; if -1, lowercase; if 0, as is.
370  *  any other value for case is the same as 0 but also causes trailing blanks
371  *  to be included (in anticipation of subsequent additional text). */
372
373
374         ALPH1=13*CASE+24;
375         ALPH2=26*IABS(CASE)+ALPH1;
376         if(IABS(CASE) > 1)ALPH1=ALPH2;
377 /*  ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
378         DIV=64L*64L*64L*64L;
379         W=WORD;
380         /* 18 */ for (I=1; I<=5; I++) {
381         if(W <= 0 && STATE == 0 && IABS(CASE) <= 1)return;
382         BYTE=W/DIV;
383         if(STATE != 0 || BYTE != 63) goto L12;
384         STATE=63;
385          goto L18;
386
387 L12:    SHFTXT(LNPOSN,1);
388         STATE=STATE+BYTE;
389         if(STATE < ALPH2 && STATE >= ALPH1)STATE=STATE-26*CASE;
390         INLINE[LNPOSN]=STATE;
391         LNPOSN=LNPOSN+1;
392         STATE=0;
393 L18:    W=(W-BYTE*DIV)*64;
394         } /* end loop */
395         return;
396 }
397
398
399
400 #undef STATE
401 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
402 #undef SHFTXT
403 void fSHFTXT(long FROM, long DELTA) {
404 long I, II, JJ;
405
406 /*  Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG.  Delta can be
407  *  negative.  LNLENG is updated; LNPOSN is not changed. */
408
409
410         if(LNLENG < FROM || DELTA == 0) goto L2;
411         for (I=FROM; I<=LNLENG; I++) {
412         II=I;
413         if(DELTA > 0)II=FROM+LNLENG-I;
414         JJ=II+DELTA;
415         INLINE[JJ]=INLINE[II];
416         } /* end loop */
417 L2:     LNLENG=LNLENG+DELTA;
418         return;
419 }
420
421
422
423 #define SHFTXT(FROM,DELTA) fSHFTXT(FROM,DELTA)
424 #undef TYPE0
425 void fTYPE0() {
426 long TEMP;
427
428 /*  Type a blank line.  This procedure is provided as a convenience for callers
429  *  who otherwise have no use for MAPCOM. */
430
431
432         TEMP=LNLENG;
433         LNLENG=0;
434         TYPE();
435         LNLENG=TEMP;
436         return;
437 }
438
439
440
441 #define TYPE0() fTYPE0()
442
443
444 /*  Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
445
446 #undef SAVWDS
447 void fSAVWDS(long *W1, long *W2, long *W3, long *W4, long *W5, long *W6, long *W7) {
448
449 /*  Write or read 7 variables.  See SAVWRD. */
450
451
452         SAVWRD(0,(*W1));
453         SAVWRD(0,(*W2));
454         SAVWRD(0,(*W3));
455         SAVWRD(0,(*W4));
456         SAVWRD(0,(*W5));
457         SAVWRD(0,(*W6));
458         SAVWRD(0,(*W7));
459         return;
460 }
461
462
463 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
464 #undef SAVARR
465 void fSAVARR(long ARR[], long N) {
466 long I;
467
468 /*  Write or read an array of N words.  See SAVWRD. */
469
470
471         for (I=1; I<=N; I++) {
472         SAVWRD(0,ARR[I]);
473         } /* end loop */
474         return;
475 }
476
477
478
479 #define SAVARR(ARR,N) fSAVARR(ARR,N)
480 #undef SAVWRD
481 #define WORD (*wORD)
482 void fSAVWRD(long OP, long *wORD) {
483 static long BUF[250], CKSUM = 0, H1, HASH = 0, N = 0, STATE = 0;
484
485 /*  If OP<0, start writing a file, using word to initialise encryption; save
486  *  word in the file.  If OP>0, start reading a file; read the file to find
487  *  the value with which to decrypt the rest.  In either case, if a file is
488  *  already open, finish writing/reading it and don't start a new one.  If OP=0,
489  *  read/write a single word.  Words are buffered in case that makes for more
490  *  efficient disk use.  We also compute a simple checksum to catch elementary
491  *  poking within the saved file.  When we finish reading/writing the file,
492  *  we store zero into WORD if there's no checksum error, else nonzero. */
493
494
495         if(OP != 0){long ifvar; ifvar=(STATE); switch (ifvar<0? -1 : ifvar>0? 1 :
496                 0) { case -1: goto L30; case 0: goto L10; case 1: goto L30; }}
497         if(STATE == 0)return;
498         if(N == 250)SAVEIO(1,STATE > 0,BUF);
499         N=MOD(N,250)+1;
500         H1=MOD(HASH*1093L+221573L,1048576L);
501         HASH=MOD(H1*1093L+221573L,1048576L);
502         H1=MOD(H1,1234)*765432+MOD(HASH,123);
503         N--;
504         if(STATE > 0)WORD=BUF[N]+H1;
505         BUF[N]=WORD-H1;
506         N++;
507         CKSUM=MOD(CKSUM*13+WORD,1000000000L);
508         return;
509
510 L10:    STATE=OP;
511         SAVEIO(0,STATE > 0,BUF);
512         N=1;
513         if(STATE > 0) goto L15;
514         HASH=MOD(WORD,1048576L);
515         BUF[0]=1234L*5678L-HASH;
516 L13:    CKSUM=BUF[0];
517         return;
518
519 L15:    SAVEIO(1,true,BUF);
520         HASH=MOD(1234L*5678L-BUF[0],1048576L);
521          goto L13;
522
523 L30:    if(N == 250)SAVEIO(1,STATE > 0,BUF);
524         N=MOD(N,250)+1;
525         if(STATE > 0) goto L32;
526         N--; BUF[N]=CKSUM; N++;
527         SAVEIO(1,false,BUF);
528 L32:    N--; WORD=BUF[N]-CKSUM; N++;
529         SAVEIO(-1,STATE > 0,BUF);
530         STATE=0;
531         return;
532 }
533
534
535
536
537
538 /*  Data struc. routines (VOCAB, DSTROY, JUGGLE, MOVE, PUT, CARRY, DROP, ATDWRF)
539                 */
540
541 #undef WORD
542 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
543 #undef VOCAB
544 long fVOCAB(long ID, long INIT) {
545 long I, VOCAB;
546
547 /*  Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
548  *  -1 if not found.  If INIT is positive, this is an initialisation call setting
549  *  up a keyword variable, and not finding it constitutes a bug.  It also means
550  *  that only KTAB values which taken over 1000 equal INIT may be considered.
551  *  (Thus "STEPS", which is a motion verb as well as an object, may be located
552  *  as an object.)  And it also means the KTAB value is taken modulo 1000. */
553
554         /* 1 */ for (I=1; I<=TABSIZ; I++) {
555         if(KTAB[I] == -1) goto L2;
556         if(INIT >= 0 && KTAB[I]/1000 != INIT) goto L1;
557         if(ATAB[I] == ID) goto L3;
558 L1:     /*etc*/ ;
559         } /* end loop */
560         BUG(21);
561
562 L2:     VOCAB= -1;
563         if(INIT < 0)return(VOCAB);
564         BUG(5);
565
566 L3:     VOCAB=KTAB[I];
567         if(INIT >= 0)VOCAB=MOD(VOCAB,1000);
568         return(VOCAB);
569 }
570
571
572
573 #define VOCAB(ID,INIT) fVOCAB(ID,INIT)
574 #undef DSTROY
575 void fDSTROY(long OBJECT) {
576 ;
577
578 /*  Permanently eliminate "OBJECT" by moving to a non-existent location. */
579
580
581         MOVE(OBJECT,0);
582         return;
583 }
584
585
586
587 #define DSTROY(OBJECT) fDSTROY(OBJECT)
588 #undef JUGGLE
589 void fJUGGLE(long OBJECT) {
590 long I, J;
591
592 /*  Juggle an object by picking it up and putting it down again, the purpose
593  *  being to get the object to the front of the chain of things at its loc. */
594
595
596         I=PLACE[OBJECT];
597         J=FIXED[OBJECT];
598         MOVE(OBJECT,I);
599         MOVE(OBJECT+100,J);
600         return;
601 }
602
603
604
605 #define JUGGLE(OBJECT) fJUGGLE(OBJECT)
606 #undef MOVE
607 void fMOVE(long OBJECT, long WHERE) {
608 long FROM;
609
610 /*  Place any object anywhere by picking it up and dropping it.  May already be
611  *  toting, in which case the carry is a no-op.  Mustn't pick up objects which
612  *  are not at any loc, since carry wants to remove objects from ATLOC chains. */
613
614
615         if(OBJECT > 100) goto L1;
616         FROM=PLACE[OBJECT];
617          goto L2;
618 L1:     {long x = OBJECT-100; FROM=FIXED[x];}
619 L2:     if(FROM > 0 && FROM <= 300)CARRY(OBJECT,FROM);
620         DROP(OBJECT,WHERE);
621         return;
622 }
623
624
625
626 #define MOVE(OBJECT,WHERE) fMOVE(OBJECT,WHERE)
627 #undef PUT
628 long fPUT(long OBJECT, long WHERE, long PVAL) {
629 long PUT;
630
631 /*  PUT is the same as MOVE, except it returns a value used to set up the
632  *  negated PROP values for the repository objects. */
633
634
635         MOVE(OBJECT,WHERE);
636         PUT=(-1)-PVAL;
637         return(PUT);
638 }
639
640
641
642 #define PUT(OBJECT,WHERE,PVAL) fPUT(OBJECT,WHERE,PVAL)
643 #undef CARRY
644 void fCARRY(long OBJECT, long WHERE) {
645 long TEMP;
646
647 /*  Start toting an object, removing it from the list of things at its former
648  *  location.  Incr holdng unless it was already being toted.  If OBJECT>100
649  *  (moving "fixed" second loc), don't change PLACE or HOLDNG. */
650
651
652         if(OBJECT > 100) goto L5;
653         if(PLACE[OBJECT] == -1)return;
654         PLACE[OBJECT]= -1;
655         HOLDNG=HOLDNG+1;
656 L5:     if(ATLOC[WHERE] != OBJECT) goto L6;
657         ATLOC[WHERE]=LINK[OBJECT];
658         return;
659 L6:     TEMP=ATLOC[WHERE];
660 L7:     if(LINK[TEMP] == OBJECT) goto L8;
661         TEMP=LINK[TEMP];
662          goto L7;
663 L8:     LINK[TEMP]=LINK[OBJECT];
664         return;
665 }
666
667
668
669 #define CARRY(OBJECT,WHERE) fCARRY(OBJECT,WHERE)
670 #undef DROP
671 void fDROP(long OBJECT, long WHERE) {
672 ;
673
674 /*  Place an object at a given loc, prefixing it onto the ATLOC list.  Decr
675  *  HOLDNG if the object was being toted. */
676
677
678         if(OBJECT > 100) goto L1;
679         if(PLACE[OBJECT] == -1)HOLDNG=HOLDNG-1;
680         PLACE[OBJECT]=WHERE;
681          goto L2;
682 L1:     {long x = OBJECT-100; FIXED[x]=WHERE;}
683 L2:     if(WHERE <= 0)return;
684         LINK[OBJECT]=ATLOC[WHERE];
685         ATLOC[WHERE]=OBJECT;
686         return;
687 }
688
689
690
691 #define DROP(OBJECT,WHERE) fDROP(OBJECT,WHERE)
692 #undef ATDWRF
693 long fATDWRF(long WHERE) {
694 long ATDWRF, I;
695
696 /*  Return the index of first dwarf at the given location, zero if no dwarf is
697  *  there (or if dwarves not active yet), -1 if all dwarves are dead.  Ignore
698  *  the pirate (6th dwarf). */
699
700
701         ATDWRF=0;
702         if(DFLAG < 2)return(ATDWRF);
703         ATDWRF= -1;
704         for (I=1; I<=5; I++) {
705         if(DLOC[I] == WHERE) goto L2;
706         if(DLOC[I] != 0)ATDWRF=0;
707         } /* end loop */
708         return(ATDWRF);
709
710 L2:     ATDWRF=I;
711         return(ATDWRF);
712 }
713
714
715
716
717 #define ATDWRF(WHERE) fATDWRF(WHERE)
718
719
720
721 /*  Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
722  *  randrange, RNDVOC, BUG) */
723
724 #undef SETBIT
725 long fSETBIT(long BIT) {
726 long I, SETBIT;
727
728 /*  Returns 2**bit for use in constructing bit-masks. */
729
730
731         SETBIT=1;
732         if(BIT <= 0)return(SETBIT);
733         for (I=1; I<=BIT; I++) {
734         SETBIT=SETBIT+SETBIT;
735         } /* end loop */
736         return(SETBIT);
737 }
738
739
740
741 #define SETBIT(BIT) fSETBIT(BIT)
742 #undef TSTBIT
743 long fTSTBIT(long MASK, long BIT) {
744 long TSTBIT;
745
746 /*  Returns true if the specified bit is set in the mask. */
747
748
749         TSTBIT=MOD(MASK/SETBIT(BIT),2) != 0;
750         return(TSTBIT);
751 }
752
753
754
755 #define TSTBIT(MASK,BIT) fTSTBIT(MASK,BIT)
756
757 void set_seed(long seedval)
758 {
759   lcgstate.x = (unsigned long) seedval % lcgstate.m;
760 }
761
762 unsigned long get_next_lcg_value(void)
763 {
764   /* Return the LCG's current value, and then iterate it. */
765   unsigned long old_x = lcgstate.x;
766   lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
767   return(old_x);
768 }
769
770 long randrange(long range)
771 {
772   /* Return a random integer from [0, range). */
773   long result = range * get_next_lcg_value() / lcgstate.m;
774   return(result);
775 }
776
777 #undef RNDVOC
778 long fRNDVOC(long CHAR, long FORCE) {
779 long DIV, I, J, RNDVOC;
780
781 /*  Searches the vocabulary for a word whose second character is char, and
782  *  changes that word such that each of the other four characters is a
783  *  random letter.  If force is non-zero, it is used as the new word.
784  *  Returns the new word. */
785
786
787         RNDVOC=FORCE;
788         if(RNDVOC != 0) goto L3;
789         for (I=1; I<=5; I++) {
790         J=11+randrange(26);
791         if(I == 2)J=CHAR;
792         RNDVOC=RNDVOC*64+J;
793         } /* end loop */
794 L3:     J=10000;
795         DIV=64L*64L*64L;
796         for (I=1; I<=TABSIZ; I++) {
797         J=J+7;
798         if(MOD((ATAB[I]-J*J)/DIV,64L) == CHAR) goto L8;
799         /*etc*/ ;
800         } /* end loop */
801         BUG(5);
802
803 L8:     ATAB[I]=RNDVOC+J*J;
804         return(RNDVOC);
805 }
806
807
808
809 #define RNDVOC(CHAR,FORCE) fRNDVOC(CHAR,FORCE)
810 #undef BUG
811 void fBUG(long NUM) {
812
813 /*  The following conditions are currently considered fatal bugs.  Numbers < 20
814  *  are detected while reading the database; the others occur at "run time".
815  *      0       Message line > 70 characters
816  *      1       Null line in message
817  *      2       Too many words of messages
818  *      3       Too many travel options
819  *      4       Too many vocabulary words
820  *      5       Required vocabulary word not found
821  *      6       Too many RTEXT messages
822  *      7       Too many hints
823  *      8       Location has cond bit being set twice
824  *      9       Invalid section number in database
825  *      10      Too many locations
826  *      11      Too many class or turn messages
827  *      20      Special travel (500>L>300) exceeds goto list
828  *      21      Ran off end of vocabulary table
829  *      22      Vocabulary type (N/1000) not between 0 and 3
830  *      23      Intransitive action verb exceeds goto list
831  *      24      Transitive action verb exceeds goto list
832  *      25      Conditional travel entry with no alternative
833  *      26      Location has no travel entries
834  *      27      Hint number exceeds goto list
835  *      28      Invalid month returned by date function
836  *      29      Too many parameters given to SETPRM */
837
838         printf("Fatal error %ld.  See source code for interpretation.\n",
839            NUM);
840         exit(0);
841 }
842
843
844
845
846
847 /*  Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
848
849 #define BUG(NUM) fBUG(NUM)
850 #undef MAPLIN
851 void fMAPLIN(FILE *OPENED) {
852 long I, VAL;
853
854 /*  Read a line of input, from the specified input source,
855  *  translate the chars to integers in the range 0-126 and store
856  *  them in the common array "INLINE".  Integer values are as follows:
857  *     0   = space [ASCII CODE 40 octal, 32 decimal]
858  *    1-2  = !" [ASCII 41-42 octal, 33-34 decimal]
859  *    3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
860  *   11-36 = upper-case letters
861  *   37-62 = lower-case letters
862  *    63   = percent (%) [ASCII 45 octal, 37 decimal]
863  *   64-73 = digits, 0 through 9
864  *  Remaining characters can be translated any way that is convenient;
865  *  The "TYPE" routine below is used to map them back to characters when
866  *  necessary.  The above mappings are required so that certain special
867  *  characters are known to fit in 6 bits and/or can be easily spotted.
868  *  Array elements beyond the end of the line should be filled with 0,
869  *  and LNLENG should be set to the index of the last character.
870  *
871  *  If the data file uses a character other than space (e.g., tab) to
872  *  separate numbers, that character should also translate to 0.
873  *
874  *  This procedure may use the map1,map2 arrays to maintain static data for
875  *  the mapping.  MAP2(1) is set to 0 when the program starts
876  *  and is not changed thereafter unless the routines on this page choose
877  *  to do so. */
878
879         if(MAP2[1] == 0)MPINIT();
880
881         if (!oldstyle && SETUP && OPENED == stdin)
882                 fputs("> ", stdout);
883         do {
884                 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,OPENED));
885         } while
886                 (!feof(OPENED) && rawbuf[0] == '#');
887         if (feof(OPENED)) {
888                 if (logfp && OPENED == stdin)
889                         fclose(logfp);
890         } else {
891                 if (logfp && OPENED == stdin)
892                         IGNORE(fputs(rawbuf, logfp));
893                 else if (!isatty(0))
894                         IGNORE(fputs(rawbuf, stdout));
895                 strcpy(INLINE+1, rawbuf);
896                 LNLENG=0;
897                 for (I=1; I<=sizeof(INLINE) && INLINE[I]!=0; I++) {
898                 VAL=INLINE[I]+1;
899                 INLINE[I]=MAP1[VAL];
900                 if(INLINE[I] != 0)LNLENG=I;
901                 } /* end loop */
902                 LNPOSN=1;
903         }
904 }
905 #define MAPLIN(FIL) fMAPLIN(FIL)
906
907 #undef TYPE
908 void fTYPE(void) {
909 long I, VAL;
910
911 /*  Type the first "LNLENG" characters stored in inline, mapping them
912  *  from integers to text per the rules described above.  INLINE(I),
913  *  I=1,LNLENG may be changed by this routine. */
914
915
916         if(LNLENG != 0) goto L10;
917         printf("\n");
918         return;
919
920 L10:    if(MAP2[1] == 0)MPINIT();
921         for (I=1; I<=LNLENG; I++) {
922         VAL=INLINE[I];
923         {long x = VAL+1; INLINE[I]=MAP2[x];}
924         } /* end loop */
925         {long x = LNLENG+1; INLINE[x]=0;}
926         printf("%s\n",INLINE+1);
927         return;
928 }
929
930
931
932 #define TYPE() fTYPE()
933 #undef MPINIT
934 void fMPINIT(void) {
935 long FIRST, I, J, LAST, VAL;
936 static long RUNS[7][2] = {32,34, 39,46, 65,90, 97,122, 37,37, 48,57, 0,126};
937
938
939         for (I=1; I<=128; I++) {
940         MAP1[I]= -1;
941         } /* end loop */
942         VAL=0;
943         for (I=0; I<7; I++) {
944         FIRST=RUNS[I][0];
945         LAST=RUNS[I][1];
946         /* 22 */ for (J=FIRST; J<=LAST; J++) {
947         J++; if(MAP1[J] >= 0) goto L22;
948         MAP1[J]=VAL;
949         VAL=VAL+1;
950 L22:    J--;
951         } /* end loop */
952         /*etc*/ ;
953         } /* end loop */
954         MAP1[128]=MAP1[10];
955 /*  For this version, tab (9) maps to space (32), so del (127) uses tab's value */
956         MAP1[10]=MAP1[33];
957         MAP1[11]=MAP1[33];
958
959         for (I=0; I<=126; I++) {
960         I++; VAL=MAP1[I]+1; I--;
961         MAP2[VAL]=I*('B'-'A');
962         if(I >= 64)MAP2[VAL]=(I-64)*('B'-'A')+'@';
963         } /* end loop */
964
965         return;
966 }
967
968
969
970 #define MPINIT() fMPINIT()
971 #undef SAVEIO
972 void fSAVEIO(long OP, long IN, long ARR[]) {
973 static FILE *F; char NAME[50];
974
975 /*  If OP=0, ask for a file name and open a file.  (If IN=true, the file is for
976  *  input, else output.)  If OP>0, read/write ARR from/into the previously-opened
977  *  file.  (ARR is a 250-integer array.)  If OP<0, finish reading/writing the
978  *  file.  (Finishing writing can be a no-op if a "stop" statement does it
979  *  automatically.  Finishing reading can be a no-op as long as a subsequent
980  *  SAVEIO(0,false,X) will still work.)  If you can catch errors (e.g., no such
981  *  file) and try again, great.  DEC F40 can't. */
982
983
984         {long ifvar; ifvar=(OP); switch (ifvar<0? -1 : ifvar>0? 1 : 0) { case -1:
985                 goto L10; case 0: goto L20; case 1: goto L30; }}
986
987 L10:    fclose(F);
988         return;
989
990 L20:    printf("\nFile name: ");
991         IGNORE(fgets(NAME, sizeof(NAME), stdin));
992         F=fopen(NAME,(IN ? READ_MODE : WRITE_MODE));
993         if(F == NULL) {printf("Can't open file, try again.\n"); goto L20;}
994         return;
995
996 L30:    if(IN)IGNORE(fread(ARR,sizeof(long),250,F));
997         if(!IN)fwrite(ARR,sizeof(long),250,F);
998         return;
999
1000 }
1001
1002
1003
1004 long fIABS(N)long N; {return(N<0? -N : N);}
1005 long fMOD(N,M)long N, M; {return(N%M);}