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