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