08fdcfaf6f1b1a3a89e41c8c5793096cda5c442f
[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         if (!MAPLIN(input))
198             return false;
199         *pword1=GETTXT(true,true,true);
200         if (game.blklin && *pword1 < 0)
201             continue;
202         *pword1x=GETTXT(false,true,true);
203         do {    
204             junk=GETTXT(false,true,true);
205         } while 
206             (junk > 0);
207         *pword2=GETTXT(true,true,true);
208         *pword2x=GETTXT(false,true,true);
209         do {
210             junk=GETTXT(false,true,true);
211         } while 
212             (junk > 0);
213         if (GETTXT(true,true,true) <= 0)
214             return true;
215         RSPEAK(53);
216     }
217 }
218
219 long YES(FILE *input, vocab_t x, vocab_t y, vocab_t z)
220 /*  Print message X, wait for yes/no answer.  If yes, print Y and return true;
221  *  if no, print Z and return false. */
222 {
223     token_t reply, junk1, junk2, junk3;
224
225     for (;;) {
226         RSPEAK(x);
227         GETIN(input, &reply, &junk1, &junk2, &junk3);
228         if (reply == MAKEWD(250519) || reply == MAKEWD(25)) {
229             RSPEAK(y);
230             return true;
231         }
232         if (reply == MAKEWD(1415) || reply == MAKEWD(14)) {
233             RSPEAK(z);
234             return false;
235         }
236         RSPEAK(185);
237     }
238 }
239
240 /*  Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
241
242 long GETTXT(bool skip, bool onewrd, bool upper)
243 /*  Take characters from an input line and pack them into 30-bit words.
244  *  Skip says to skip leading blanks.  ONEWRD says stop if we come to a
245  *  blank.  UPPER says to map all letters to uppercase.  If we reach the
246  *  end of the line, the word is filled up with blanks (which encode as 0's).
247  *  If we're already at end of line when TEXT is called, we return -1. */
248 {
249     long text;
250     static long splitting = -1;
251
252     if (LNPOSN != splitting)
253         splitting = -1;
254     text= -1;
255     while (true) {
256         if (LNPOSN > LNLENG)
257             return(text);
258         if ((!skip) || INLINE[LNPOSN] != 0)
259             break;
260         LNPOSN=LNPOSN+1;
261     }
262
263     text=0;
264     for (int I=1; I<=TOKLEN; I++) {
265         text=text*64;
266         if (LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
267             continue;
268         char current=INLINE[LNPOSN];
269         if (current < PERCENT) {
270             splitting = -1;
271             if (upper && current >= 37)
272                 current=current-26;
273             text=text+current;
274             LNPOSN=LNPOSN+1;
275             continue;
276         }
277         if (splitting != LNPOSN) {
278             text=text+PERCENT;
279             splitting = LNPOSN;
280             continue;
281         }
282
283         text=text+current-PERCENT;
284         splitting = -1;
285         LNPOSN=LNPOSN+1;
286     }
287
288     return text;
289 }
290
291 token_t MAKEWD(long letters)
292 /*  Combine TOKLEN (currently 5) uppercase letters (represented by
293  *  pairs of decimal digits in lettrs) to form a 30-bit value matching
294  *  the one that GETTXT would return given those characters plus
295  *  trailing blanks.  Caution: lettrs will overflow 31 bits if
296  *  5-letter word starts with V-Z.  As a kludgey workaround, you can
297  *  increment a letter by 5 by adding 50 to the next pair of
298  *  digits. */
299 {
300     long i = 1, word = 0;
301
302     for (long k=letters; k != 0; k=k/100) {
303         word=word+i*(MOD(k,50)+10);
304         i=i*64;
305         if (MOD(k,100) > 50)word=word+i*5;
306     }
307     i=64L*64L*64L*64L*64L/i;
308     word=word*i;
309     return word;
310 }
311
312 void PUTTXT(token_t word, long *state, long casemake)
313 /*  Unpack the 30-bit value in word to obtain up to TOKLEN (currently
314  *  5) integer-encoded chars, and store them in inline starting at
315  *  LNPOSN.  If LNLENG>=LNPOSN, shift existing characters to the right
316  *  to make room.  STATE will be zero when puttxt is called with the
317  *  first of a sequence of words, but is thereafter unchanged by the
318  *  caller, so PUTTXT can use it to maintain state across calls.
319  *  LNPOSN and LNLENG are incremented by the number of chars stored.
320  *  If CASEMAKE=1, all letters are made uppercase; if -1, lowercase; if 0,
321  *  as is.  any other value for case is the same as 0 but also causes
322  *  trailing blanks to be included (in anticipation of subsequent
323  *  additional text). */
324 {
325     long alph1, alph2, byte, div, i, w;
326
327     alph1=13*casemake+24;
328     alph2=26*labs(casemake)+alph1;
329     if (labs(casemake) > 1)
330         alph1=alph2;
331     /*  alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
332     div=64L*64L*64L*64L;
333     w=word;
334     for (i=1; i<=TOKLEN; i++) 
335     {
336         if (w <= 0 && *state == 0 && labs(casemake) <= 1)
337             return;
338         byte=w/div;
339         w=(w-byte*div)*64;
340         if (!(*state != 0 || byte != PERCENT)) {
341             *state=PERCENT;
342             continue;
343         }
344         SHFTXT(LNPOSN,1);
345         *state=*state+byte;
346         if (*state < alph2 && *state >= alph1)*state=*state-26*casemake;
347         INLINE[LNPOSN]=*state;
348         LNPOSN=LNPOSN+1;
349         *state=0;
350     }
351 }
352 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
353
354 void SHFTXT(long from, long delta) 
355 /*  Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG.  Delta can be
356  *  negative.  LNLENG is updated; LNPOSN is not changed. */
357 {
358     long I, k, j;
359
360     if (!(LNLENG < from || delta == 0)) {
361         for (I=from; I<=LNLENG; I++) {
362             k=I;
363             if (delta > 0)
364                 k=from+LNLENG-I;
365             j=k+delta;
366             INLINE[j]=INLINE[k];
367         }
368     }
369     LNLENG=LNLENG+delta;
370 }
371
372 void TYPE0(void)
373 /*  Type a blank line.  This procedure is provided as a convenience for callers
374  *  who otherwise have no use for MAPCOM. */
375 {
376     long temp;
377
378     temp=LNLENG;
379     LNLENG=0;
380     TYPE();
381     LNLENG=temp;
382     return;
383 }
384
385 /*  Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
386
387 void fSAVWDS(long *W1, long *W2, long *W3, long *W4,
388              long *W5, long *W6, long *W7)
389 /* Write or read 7 variables.  See SAVWRD. */
390 {
391     SAVWRD(0,(*W1));
392     SAVWRD(0,(*W2));
393     SAVWRD(0,(*W3));
394     SAVWRD(0,(*W4));
395     SAVWRD(0,(*W5));
396     SAVWRD(0,(*W6));
397     SAVWRD(0,(*W7));
398 }
399
400 void fSAVARR(long arr[], long n)
401 /* Write or read an array of n words.  See SAVWRD. */
402 {
403     long i;
404
405     for (i=1; i<=n; i++) {
406         SAVWRD(0,arr[i]);
407     }
408     return;
409 }
410
411 void fSAVWRD(long op, long *pword) 
412 /*  If OP<0, start writing a file, using word to initialise encryption; save
413  *  word in the file.  If OP>0, start reading a file; read the file to find
414  *  the value with which to decrypt the rest.  In either case, if a file is
415  *  already open, finish writing/reading it and don't start a new one.  If OP=0,
416  *  read/write a single word.  Words are buffered in case that makes for more
417  *  efficient disk use.  We also compute a simple checksum to catch elementary
418  *  poking within the saved file.  When we finish reading/writing the file,
419  *  we store zero into *PWORD if there's no checksum error, else nonzero. */
420 {
421     static long buf[250], cksum = 0, h1, hash = 0, n = 0, state = 0;
422
423     if (op != 0)
424     {
425         long ifvar = state; 
426         switch (ifvar<0 ? -1 : (ifvar>0 ? 1 : 0)) 
427         { 
428         case -1:
429         case 1:
430             if (n == 250)SAVEIO(1,state > 0,buf);
431             n=MOD(n,250)+1;
432             if (state <= 0) {
433                 n--; buf[n]=cksum; n++;
434                 SAVEIO(1,false,buf);
435             }
436             n--; *pword=buf[n]-cksum; n++;
437             SAVEIO(-1,state > 0,buf);
438             state=0;
439             break;
440         case 0: /* FIXME: Huh? should be impossible */
441             state=op;
442             SAVEIO(0,state > 0,buf);
443             n=1;
444             if (state <= 0) {
445                 hash=MOD(*pword,1048576L);
446                 buf[0]=1234L*5678L-hash;
447             }
448             SAVEIO(1,true,buf);
449             hash=MOD(1234L*5678L-buf[0],1048576L);
450             cksum=buf[0];
451             return;
452         }
453     }
454     if (state == 0)
455         return;
456     if (n == 250)
457         SAVEIO(1,state > 0,buf);
458     n=MOD(n,250)+1;
459     h1=MOD(hash*1093L+221573L,1048576L);
460     hash=MOD(h1*1093L+221573L,1048576L);
461     h1=MOD(h1,1234)*765432+MOD(hash,123);
462     n--;
463     if (state > 0)
464         *pword=buf[n]+h1;
465     buf[n]=*pword-h1;
466     n++;
467     cksum=MOD(cksum*13+*pword,1000000000L);
468 }
469
470 /*  Data structure  routines */
471
472 long VOCAB(long id, long init) 
473 /*  Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
474  *  -1 if not found.  If INIT is positive, this is an initialisation call setting
475  *  up a keyword variable, and not finding it constitutes a bug.  It also means
476  *  that only KTAB values which taken over 1000 equal INIT may be considered.
477  *  (Thus "STEPS", which is a motion verb as well as an object, may be located
478  *  as an object.)  And it also means the KTAB value is taken modulo 1000. */
479 {
480     long i, lexeme;
481
482     for (i=1; i<=TABSIZ; i++) {
483         if (KTAB[i] == -1) {
484             lexeme= -1;
485             if (init < 0)
486                 return(lexeme);
487             BUG(5);
488         }
489         if (init >= 0 && KTAB[i]/1000 != init) 
490             continue;
491         if (ATAB[i] == id) {
492             lexeme=KTAB[i];
493             if (init >= 0)
494                 lexeme=MOD(lexeme,1000);
495             return(lexeme);
496         }
497     }
498     BUG(21);
499 }
500
501 void DSTROY(long object)
502 /*  Permanently eliminate "object" by moving to a non-existent location. */
503 {
504     MOVE(object,0);
505 }
506
507 void JUGGLE(long object)
508 /*  Juggle an object by picking it up and putting it down again, the purpose
509  *  being to get the object to the front of the chain of things at its loc. */
510 {
511     long i, j;
512
513     i=game.place[object];
514     j=game.fixed[object];
515     MOVE(object,i);
516     MOVE(object+NOBJECTS,j);
517 }
518
519 void MOVE(long object, long where)
520 /*  Place any object anywhere by picking it up and dropping it.  May
521  *  already be toting, in which case the carry is a no-op.  Mustn't
522  *  pick up objects which are not at any loc, since carry wants to
523  *  remove objects from game.atloc chains. */
524 {
525     long from;
526
527     if (object > NOBJECTS) 
528         from=game.fixed[object-NOBJECTS];
529     else
530         from=game.place[object];
531     if (from > 0 && from <= 300)
532         CARRY(object,from);
533     DROP(object,where);
534 }
535
536 long PUT(long object, long where, long pval)
537 /*  PUT is the same as MOVE, except it returns a value used to set up the
538  *  negated game.prop values for the repository objects. */
539 {
540     MOVE(object,where);
541     return (-1)-pval;;
542 }
543
544 void CARRY(long object, long where) 
545 /*  Start toting an object, removing it from the list of things at its former
546  *  location.  Incr holdng unless it was already being toted.  If object>NOBJECTS
547  *  (moving "fixed" second loc), don't change game.place or game.holdng. */
548 {
549     long temp;
550
551     if (object <= NOBJECTS) {
552         if (game.place[object] == -1)
553             return;
554         game.place[object]= -1;
555         game.holdng=game.holdng+1;
556     }
557     if (game.atloc[where] == object) {
558         game.atloc[where]=game.link[object];
559         return;
560     }
561     temp=game.atloc[where];
562     while (game.link[temp] != object) {
563         temp=game.link[temp];
564     }
565     game.link[temp]=game.link[object];
566 }
567
568 void DROP(long object, long where)
569 /*  Place an object at a given loc, prefixing it onto the game.atloc list.  Decr
570  *  game.holdng if the object was being toted. */
571 {
572     if (object > NOBJECTS)
573         game.fixed[object-NOBJECTS] = where;
574     else
575     {
576         if (game.place[object] == -1)
577             --game.holdng;
578         game.place[object] = where;
579     }
580     if (where <= 0)
581         return;
582     game.link[object] = game.atloc[where];
583     game.atloc[where] = object;
584 }
585
586 long ATDWRF(long where)
587 /*  Return the index of first dwarf at the given location, zero if no dwarf is
588  *  there (or if dwarves not active yet), -1 if all dwarves are dead.  Ignore
589  *  the pirate (6th dwarf). */
590 {
591     long at, i;
592
593     at =0;
594     if (game.dflag < 2)
595         return(at);
596     at = -1;
597     for (i=1; i<=NDWARVES-1; i++) {
598         if (game.dloc[i] == where)
599             return i;
600         if (game.dloc[i] != 0)
601             at=0;
602     }
603     return(at);
604 }
605
606 /*  Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
607  *  randrange, RNDVOC, BUG) */
608
609 long SETBIT(long bit)
610 /*  Returns 2**bit for use in constructing bit-masks. */
611 {
612     return(1 << bit);
613 }
614
615 bool TSTBIT(long mask, int bit)
616 /*  Returns true if the specified bit is set in the mask. */
617 {
618     return (mask & (1 << bit)) != 0;
619 }
620
621 void set_seed(long seedval)
622 /* Set the LCG seed */
623 {
624     lcgstate.x = (unsigned long) seedval % lcgstate.m;
625 }
626
627 unsigned long get_next_lcg_value(void)
628 /* Return the LCG's current value, and then iterate it. */
629 {
630     unsigned long old_x = lcgstate.x;
631     lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
632     return old_x;
633 }
634
635 long randrange(long range)
636 /* Return a random integer from [0, range). */
637 {
638     return range * get_next_lcg_value() / lcgstate.m;
639 }
640
641 long RNDVOC(long second, long force)
642 /*  Searches the vocabulary ATAB for a word whose second character is
643  *  char, and changes that word such that each of the other four
644  *  characters is a random letter.  If force is non-zero, it is used
645  *  as the new word.  Returns the new word. */
646 {
647     long rnd = force;
648
649     if (rnd == 0) {
650         for (int i = 1; i <= 5; i++) {
651             long j = 11 + randrange(26);
652             if (i == 2)
653                 j = second;
654             rnd = rnd * 64 + j;
655         }
656     }
657
658     long div = 64L * 64L * 64L;
659     for (int i = 1; i <= TABSIZ; i++) {
660         if (MOD(ATAB[i]/div, 64L) == second)
661         {
662             ATAB[i] = rnd;
663             break;
664         }
665     }
666
667     return rnd;
668 }
669
670 void BUG(long num)
671 /*  The following conditions are currently considered fatal bugs.  Numbers < 20
672  *  are detected while reading the database; the others occur at "run time".
673  *      0       Message line > 70 characters
674  *      1       Null line in message
675  *      2       Too many words of messages
676  *      3       Too many travel options
677  *      4       Too many vocabulary words
678  *      5       Required vocabulary word not found
679  *      6       Too many RTEXT messages
680  *      7       Too many hints
681  *      8       Location has cond bit being set twice
682  *      9       Invalid section number in database
683  *      10      Too many locations
684  *      11      Too many class or turn messages
685  *      20      Special travel (500>L>300) exceeds goto list
686  *      21      Ran off end of vocabulary table
687  *      22      Vocabulary type (N/1000) not between 0 and 3
688  *      23      Intransitive action verb exceeds goto list
689  *      24      Transitive action verb exceeds goto list
690  *      25      Conditional travel entry with no alternative
691  *      26      Location has no travel entries
692  *      27      Hint number exceeds goto list
693  *      28      Invalid month returned by date function
694  *      29      Too many parameters given to SETPRM */
695 {
696
697     printf("Fatal error %ld.  See source code for interpretation.\n", num);
698     exit(0);
699 }
700
701 /*  Machine dependent routines (MAPLIN, TYPE, SAVEIO) */
702
703 bool MAPLIN(FILE *fp)
704 {
705     long i, val;
706     bool eof;
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 && !isatty(1))
734         fputs("> ", stdout);
735     do {
736         if (oldstyle) {
737             IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
738             eof = (feof(fp));
739         } else {
740             char *cp = linenoise("> ");
741             eof = (cp == NULL);
742             if (!eof) {
743                 strncpy(rawbuf, cp, sizeof(rawbuf)-1);
744                 linenoiseHistoryAdd(rawbuf);
745                 strncat(rawbuf, "\n", sizeof(rawbuf)-1);
746                 linenoiseFree(cp);
747             }
748         }
749     } while
750             (!eof && rawbuf[0] == '#');
751     if (eof) {
752         if (logfp && fp == stdin)
753             fclose(logfp);
754         return false;
755     } else {
756         if (logfp && fp == stdin)
757             IGNORE(fputs(rawbuf, logfp));
758         else if (!isatty(0))
759             IGNORE(fputs(rawbuf, stdout));
760         strcpy(INLINE+1, rawbuf);
761         LNLENG=0;
762         for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) {
763             val=INLINE[i]+1;
764             INLINE[i]=ascii_to_advent[val];
765             if (INLINE[i] != 0)
766                 LNLENG=i;
767         }
768         LNPOSN=1;
769         return true;
770     }
771 }
772
773 void TYPE(void)
774 /*  Type the first "LNLENG" characters stored in inline, mapping them
775  *  from integers to text per the rules described above.  INLINE
776  *  may be changed by this routine. */
777 {
778     long i;
779
780     if (LNLENG == 0) {
781         printf("\n");
782         return;
783     }
784
785     for (i=1; i<=LNLENG; i++) {
786         INLINE[i]=advent_to_ascii[INLINE[i]+1];
787     }
788     INLINE[LNLENG+1]=0;
789     printf("%s\n", INLINE+1);
790     return;
791 }
792
793 void fSAVEIO(long op, long in, long arr[]) 
794 /*  If OP=0, ask for a file name and open a file.  (If IN=true, the file is for
795  *  input, else output.)  If OP>0, read/write ARR from/into the previously-opened
796  *  file.  (ARR is a 250-integer array.)  If OP<0, finish reading/writing the
797  *  file.  (Finishing writing can be a no-op if a "stop" statement does it
798  *  automatically.  Finishing reading can be a no-op as long as a subsequent
799  *  SAVEIO(0,false,X) will still work.) */
800 {
801     static FILE *fp = NULL;
802     char* name;
803
804     switch (op < 0 ? -1 : (op > 0 ? 1 : 0)) 
805     { 
806     case -1:
807         fclose(fp);
808         break;
809     case 0:
810         while (fp == NULL) {
811             name = linenoise("File name: ");
812             fp = fopen(name,(in ? READ_MODE : WRITE_MODE));
813             if (fp == NULL)
814                 printf("Can't open file %s, try again.\n", name); 
815         }
816         linenoiseFree(name);
817         break;
818     case 1: 
819         if (in)
820             IGNORE(fread(arr,sizeof(long),250,fp));
821         else
822             IGNORE(fwrite(arr,sizeof(long),250,fp));
823         break;
824     }
825 }
826
827 void DATIME(long* d, long* t)
828 {
829     struct timeval tv;
830     gettimeofday(&tv, NULL);
831     *d = (long) tv.tv_sec;
832     *t = (long) tv.tv_usec;
833 }
834
835 long MOD(long n, long m) 
836 {
837     return(n%m);
838 }