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