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