9 #include "linenoise/linenoise.h"
11 /* hack to ignore GCC Unused Result */
12 #define IGNORE(r) do{if (r){}}while(0)
14 #define PERCENT 63 /* partly hide the packed encoding */
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};
20 /* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
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. */
26 long blank, casemake, i, nxt, neg, nparms, param, prmtyp, state;
33 nxt=labs(LINES[msg])-1;
38 for (i = msg; i <= nxt; i++) {
39 PUTTXT(LINES[i],&state,2);
44 while (LNPOSN <= LNLENG) {
45 if (INLINE[LNPOSN] != PERCENT) {
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. */
63 if (PARMS[nparms] == 1)
73 while (PARMS[nparms] > 0) {
74 if (PARMS[nparms+1] < 0)
76 PUTTXT(PARMS[nparms],&state,casemake);
84 SHFTXT(LNPOSN+2,prmtyp-2);
86 for (i=1; i<=prmtyp; i++) {
94 if (prmtyp == 33 || prmtyp == 22 || prmtyp == 31 || prmtyp == 13) {
103 PUTTXT(PARMS[nparms],&state,casemake);
104 PUTTXT(PARMS[nparms+1],&state,casemake);
105 if (prmtyp == 13 && INLINE[i] >= 37 && INLINE[i] <= 62)
112 if (prmtyp < 1 || prmtyp > 9) {
116 SHFTXT(LNPOSN+2,prmtyp-2);
118 param=labs(PARMS[nparms]);
120 if (PARMS[nparms] < 0)
122 for (i=1; i <= prmtyp; i++) {
124 INLINE[LNPOSN]=MOD(param,10)+64;
125 if (i != 1 && param == 0) {
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). */
154 for (i=0; i <=skip; i++) {
164 void RSPEAK(vocab_t i)
165 /* Print the i-th "random" message (section 6 of database). */
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). */
175 if (first >= MAXPARMS)
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. */
200 *pword1=GETTXT(true,true,true);
201 if (game.blklin && *pword1 < 0)
203 *pword1x=GETTXT(false,true,true);
205 junk=GETTXT(false,true,true);
208 *pword2=GETTXT(true,true,true);
209 *pword2x=GETTXT(false,true,true);
211 junk=GETTXT(false,true,true);
214 if (GETTXT(true,true,true) <= 0)
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. */
224 token_t reply, junk1, junk2, junk3;
228 GETIN(input, &reply, &junk1, &junk2, &junk3);
229 if (reply == MAKEWD(250519) || reply == MAKEWD(25)) {
233 if (reply == MAKEWD(1415) || reply == MAKEWD(14)) {
241 /* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
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. */
251 static long splitting = -1;
253 if (LNPOSN != splitting)
259 if ((!skip) || INLINE[LNPOSN] != 0)
265 for (int I=1; I<=TOKLEN; I++) {
267 if (LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
269 char current=INLINE[LNPOSN];
270 if (current < PERCENT) {
272 if (upper && current >= 37)
278 if (splitting != LNPOSN) {
284 text=text+current-PERCENT;
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
301 long i = 1, word = 0;
303 for (long k=letters; k != 0; k=k/100) {
304 word=word+i*(MOD(k,50)+10);
306 if (MOD(k,100) > 50)word=word+i*5;
308 i=64L*64L*64L*64L*64L/i;
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). */
326 long alph1, alph2, byte, div, i, w;
328 alph1=13*casemake+24;
329 alph2=26*labs(casemake)+alph1;
330 if (labs(casemake) > 1)
332 /* alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
335 for (i=1; i<=TOKLEN; i++)
337 if (w <= 0 && *state == 0 && labs(casemake) <= 1)
341 if (!(*state != 0 || byte != PERCENT)) {
347 if (*state < alph2 && *state >= alph1)*state=*state-26*casemake;
348 INLINE[LNPOSN]=*state;
353 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
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. */
361 if (!(LNLENG < from || delta == 0)) {
362 for (I=from; I<=LNLENG; I++) {
374 /* Type a blank line. This procedure is provided as a convenience for callers
375 * who otherwise have no use for MAPCOM. */
386 /* Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
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. */
401 void fSAVARR(long arr[], long n)
402 /* Write or read an array of n words. See SAVWRD. */
406 for (i=1; i<=n; i++) {
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. */
422 static long buf[250], cksum = 0, h1, hash = 0, n = 0, state = 0;
427 switch (ifvar<0 ? -1 : (ifvar>0 ? 1 : 0))
431 if (n == 250)SAVEIO(1,state > 0,buf);
434 n--; buf[n]=cksum; n++;
437 n--; *pword=buf[n]-cksum; n++;
438 SAVEIO(-1,state > 0,buf);
441 case 0: /* FIXME: Huh? should be impossible */
443 SAVEIO(0,state > 0,buf);
446 hash=MOD(*pword,1048576L);
447 buf[0]=1234L*5678L-hash;
450 hash=MOD(1234L*5678L-buf[0],1048576L);
458 SAVEIO(1,state > 0,buf);
460 h1=MOD(hash*1093L+221573L,1048576L);
461 hash=MOD(h1*1093L+221573L,1048576L);
462 h1=MOD(h1,1234)*765432+MOD(hash,123);
468 cksum=MOD(cksum*13+*pword,1000000000L);
471 /* Data structure routines */
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. */
483 for (i=1; i<=TABSIZ; i++) {
490 if (init >= 0 && KTAB[i]/1000 != init)
495 lexeme=MOD(lexeme,1000);
502 void DSTROY(long object)
503 /* Permanently eliminate "object" by moving to a non-existent location. */
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. */
514 i=game.place[object];
515 j=game.fixed[object];
517 MOVE(object+NOBJECTS,j);
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. */
528 if (object > NOBJECTS)
529 from=game.fixed[object-NOBJECTS];
531 from=game.place[object];
532 if (from > 0 && from <= 300)
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. */
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. */
552 if (object <= NOBJECTS) {
553 if (game.place[object] == -1)
555 game.place[object]= -1;
556 game.holdng=game.holdng+1;
558 if (game.atloc[where] == object) {
559 game.atloc[where]=game.link[object];
562 temp=game.atloc[where];
563 while (game.link[temp] != object) {
564 temp=game.link[temp];
566 game.link[temp]=game.link[object];
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. */
573 if (object > NOBJECTS)
574 game.fixed[object-NOBJECTS] = where;
577 if (game.place[object] == -1)
579 game.place[object] = where;
583 game.link[object] = game.atloc[where];
584 game.atloc[where] = object;
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). */
598 for (i=1; i<=NDWARVES-1; i++) {
599 if (game.dloc[i] == where)
601 if (game.dloc[i] != 0)
607 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
608 * randrange, RNDVOC, BUG) */
610 long SETBIT(long bit)
611 /* Returns 2**bit for use in constructing bit-masks. */
616 bool TSTBIT(long mask, int bit)
617 /* Returns true if the specified bit is set in the mask. */
619 return (mask & (1 << bit)) != 0;
622 void set_seed(long seedval)
623 /* Set the LCG seed */
625 lcgstate.x = (unsigned long) seedval % lcgstate.m;
628 unsigned long get_next_lcg_value(void)
629 /* Return the LCG's current value, and then iterate it. */
631 unsigned long old_x = lcgstate.x;
632 lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
636 long randrange(long range)
637 /* Return a random integer from [0, range). */
639 return range * get_next_lcg_value() / lcgstate.m;
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. */
651 for (int i = 1; i <= 5; i++) {
652 long j = 11 + randrange(26);
659 long div = 64L * 64L * 64L;
660 for (int i = 1; i <= TABSIZ; i++) {
661 if (MOD(ATAB[i]/div, 64L) == second)
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
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 */
698 printf("Fatal error %ld. See source code for interpretation.\n", num);
702 /* Machine dependent routines (MAPLIN, TYPE, SAVEIO) */
704 void MAPLIN(FILE *fp)
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.
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.
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
733 if (!oldstyle && fp == stdin)
736 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
738 (!feof(fp) && rawbuf[0] == '#');
740 if (logfp && fp == stdin)
743 if (logfp && fp == stdin)
744 IGNORE(fputs(rawbuf, logfp));
746 IGNORE(fputs(rawbuf, stdout));
747 strcpy(INLINE+1, rawbuf);
749 for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) {
751 INLINE[i]=ascii_to_advent[val];
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. */
771 for (i=1; i<=LNLENG; i++) {
772 INLINE[i]=advent_to_ascii[INLINE[i]+1];
775 printf("%s\n", INLINE+1);
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.) */
787 static FILE *fp = NULL;
790 switch (op < 0 ? -1 : (op > 0 ? 1 : 0))
797 name = linenoise("File name: ");
798 fp = fopen(name,(in ? READ_MODE : WRITE_MODE));
800 printf("Can't open file %s, try again.\n", name);
806 IGNORE(fread(arr,sizeof(long),250,fp));
808 IGNORE(fwrite(arr,sizeof(long),250,fp));
813 void DATIME(long* d, long* t)
816 gettimeofday(&tv, NULL);
817 *d = (long) tv.tv_sec;
818 *t = (long) tv.tv_usec;
821 long MOD(long n, long m)