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