10 /* hack to ignore GCC Unused Result */
11 #define IGNORE(r) do{if(r){}}while(0)
13 /* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
15 void SPEAK(vocab_t N) {
16 long BLANK, CASE, I, K, L, NEG, NPARMS, PARM, PRMTYP, STATE;
18 /* Print the message which starts at LINES(N). Precede it with a blank line
19 * unless game.blklin is false. */
25 L10: L=labs(LINES[K])-1;
30 for (I=K; I<=L; I++) {
31 PUTTXT(LINES[I],STATE,2);
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
52 if(PRMTYP < 1 || PRMTYP > 9) goto L30;
53 SHFTXT(LNPOSN+2,PRMTYP-2);
55 PARM=labs(PARMS[NPARMS]);
57 if(PARMS[NPARMS] < 0)NEG=9;
58 /* 390 */ for (I=1; I<=PRMTYP; I++) {
60 INLINE[LNPOSN]=MOD(PARM,10)+64;
61 if(I == 1 || PARM != 0) goto L390;
67 L395: NPARMS=NPARMS+1;
70 L320: SHFTXT(LNPOSN+2,-1);
72 if(PARMS[NPARMS] == 1)SHFTXT(LNPOSN+1,-1);
75 L340: SHFTXT(LNPOSN+2,-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);
84 L360: PRMTYP=PARMS[NPARMS];
85 SHFTXT(LNPOSN+2,PRMTYP-2);
86 if(PRMTYP == 0) goto L395;
87 for (I=1; I<=PRMTYP; I++) {
93 L380: SHFTXT(LNPOSN+2,-2);
96 if(PRMTYP == 31)CASE=1;
97 if(PRMTYP == 33)CASE=0;
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;
106 L40: if(BLANK)TYPE0();
110 if(LINES[K] >= 0) goto L10;
114 void PSPEAK(vocab_t MSG,int SKIP)
115 /* Find the skip+1st message from msg and print it. MSG should be
116 * the index of the inventory message for object. (INVEN+N+1 message
117 * is game.prop=N message). */
124 for (I=0; I<=SKIP; I++) {
125 L1: M=labs(LINES[M]);
132 void RSPEAK(vocab_t i)
133 /* Print the i-th "random" message (section 6 of database). */
139 void SETPRM(long first, long p1, long p2)
140 /* Stores parameters into the PRMCOM parms array for use by speak. P1 and P2
141 * are stored into PARMS(first) and PARMS(first+1). */
143 if(first >= MAXPARMS)
152 #define WORD1 (*wORD1)
153 #define WORD1X (*wORD1X)
154 #define WORD2 (*wORD2)
155 #define WORD2X (*wORD2X)
156 bool fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) {
159 /* Get a command from the adventurer. Snarf out the first word, pad it with
160 * blanks, and return it in WORD1. Chars 6 thru 10 are returned in WORD1X, in
161 * case we need to print out the whole word in an error message. Any number of
162 * blanks may follow the word. If a second word appears, it is returned in
163 * WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
166 L10: if(game.blklin)TYPE0();
170 WORD1=GETTXT(true,true,true);
171 if(game.blklin && WORD1 < 0) goto L10;
172 WORD1X=GETTXT(false,true,true);
173 L12: JUNK=GETTXT(false,true,true);
174 if(JUNK > 0) goto L12;
175 WORD2=GETTXT(true,true,true);
176 WORD2X=GETTXT(false,true,true);
177 L22: JUNK=GETTXT(false,true,true);
178 if(JUNK > 0) goto L22;
179 if(GETTXT(true,true,true) <= 0)return true;
190 #define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
192 long YES(FILE *input, vocab_t X, vocab_t Y, vocab_t Z) {
193 token_t YEAH, REPLY, JUNK1, JUNK2, JUNK3;
195 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
196 * if no, print Z and return false. */
199 GETIN(input, REPLY,JUNK1,JUNK2,JUNK3);
200 if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
201 if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
213 /* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
215 /* The routines on this page handle all the stuff that would normally be
216 * taken care of by format statements. We do it this way instead so that
217 * we can handle textual data in a machine independent fashion. All the
218 * machine dependent i/o stuff is on the following page. See that page
219 * for a description of MAPCOM's inline array. */
221 long GETTXT(bool SKIP,bool ONEWRD, bool UPPER) {
222 /* Take characters from an input line and pack them into 30-bit words.
223 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
224 * blank. UPPER says to map all letters to uppercase. If we reach the
225 * end of the line, the word is filled up with blanks (which encode as 0's).
226 * If we're already at end of line when TEXT is called, we return -1. */
229 static long SPLITTING = -1;
231 if(LNPOSN != SPLITTING)
237 if((!SKIP) || INLINE[LNPOSN] != 0)
243 for (int I=1; I<=TOKLEN; I++) {
245 if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
247 char current=INLINE[LNPOSN];
250 if(UPPER && current >= 37)
256 if(SPLITTING != LNPOSN) {
262 TEXT=TEXT+current-63;
270 token_t MAKEWD(long LETTRS) {
273 /* Combine TOKLEN (currently 5) uppercase letters (represented by
274 * pairs of decimal digits in lettrs) to form a 30-bit value matching
275 * the one that GETTXT would return given those characters plus
276 * trailing blanks. Caution: lettrs will overflow 31 bits if
277 * 5-letter word starts with V-Z. As a kludgey workaround, you can
278 * increment a letter by 5 by adding 50 to the next pair of
283 for (L=LETTRS; L != 0; L=L/100) {
284 WORD=WORD+I*(MOD(L,50)+10);
286 if(MOD(L,100) > 50)WORD=WORD+I*5;
288 I=64L*64L*64L*64L*64L/I;
294 #define STATE (*sTATE)
295 void fPUTTXT(token_t WORD, long *sTATE, long CASE) {
296 long ALPH1, ALPH2, BYTE, DIV, I, W;
298 /* Unpack the 30-bit value in word to obtain up to TOKLEN (currently
299 * 5) integer-encoded chars, and store them in inline starting at
300 * LNPOSN. If LNLENG>=LNPOSN, shift existing characters to the right
301 * to make room. STATE will be zero when puttxt is called with the
302 * first of a sequence of words, but is thereafter unchanged by the
303 * caller, so PUTTXT can use it to maintain state across calls.
304 * LNPOSN and LNLENG are incremented by the number of chars stored.
305 * If CASE=1, all letters are made uppercase; if -1, lowercase; if 0,
306 * as is. any other value for case is the same as 0 but also causes
307 * trailing blanks to be included (in anticipation of subsequent
308 * additional text). */
310 ALPH2=26*labs(CASE)+ALPH1;
311 if(labs(CASE) > 1)ALPH1=ALPH2;
312 /* ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
315 /* 18 */ for (I=1; I<=TOKLEN; I++) {
316 if(W <= 0 && STATE == 0 && labs(CASE) <= 1)return;
318 if(STATE != 0 || BYTE != 63) goto L12;
322 L12: SHFTXT(LNPOSN,1);
324 if(STATE < ALPH2 && STATE >= ALPH1)STATE=STATE-26*CASE;
325 INLINE[LNPOSN]=STATE;
328 L18: W=(W-BYTE*DIV)*64;
334 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
336 void SHFTXT(long FROM, long DELTA) {
339 /* Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG. Delta can be
340 * negative. LNLENG is updated; LNPOSN is not changed. */
343 if(LNLENG < FROM || DELTA == 0) goto L2;
344 for (I=FROM; I<=LNLENG; I++) {
346 if(DELTA > 0)II=FROM+LNLENG-I;
348 INLINE[JJ]=INLINE[II];
350 L2: LNLENG=LNLENG+DELTA;
356 /* Type a blank line. This procedure is provided as a convenience for callers
357 * who otherwise have no use for MAPCOM. */
368 /* Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
371 void fSAVWDS(long *W1, long *W2, long *W3, long *W4,
372 long *W5, long *W6, long *W7)
373 /* Write or read 7 variables. See SAVWRD. */
384 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
387 void fSAVARR(long arr[], long n)
388 /* Write or read an array of n words. See SAVWRD. */
392 for (i=1; i<=n; i++) {
397 #define SAVARR(ARR,N) fSAVARR(ARR,N)
401 void fSAVWRD(long OP, long *wORD) {
402 static long BUF[250], CKSUM = 0, H1, HASH = 0, N = 0, STATE = 0;
404 /* If OP<0, start writing a file, using word to initialise encryption; save
405 * word in the file. If OP>0, start reading a file; read the file to find
406 * the value with which to decrypt the rest. In either case, if a file is
407 * already open, finish writing/reading it and don't start a new one. If OP=0,
408 * read/write a single word. Words are buffered in case that makes for more
409 * efficient disk use. We also compute a simple checksum to catch elementary
410 * poking within the saved file. When we finish reading/writing the file,
411 * we store zero into WORD if there's no checksum error, else nonzero. */
413 if(OP != 0){long ifvar; ifvar=(STATE); switch (ifvar<0? -1 : ifvar>0? 1 :
414 0) { case -1: goto L30; case 0: goto L10; case 1: goto L30; }}
415 if(STATE == 0)return;
416 if(N == 250)SAVEIO(1,STATE > 0,BUF);
418 H1=MOD(HASH*1093L+221573L,1048576L);
419 HASH=MOD(H1*1093L+221573L,1048576L);
420 H1=MOD(H1,1234)*765432+MOD(HASH,123);
422 if(STATE > 0)WORD=BUF[N]+H1;
425 CKSUM=MOD(CKSUM*13+WORD,1000000000L);
429 SAVEIO(0,STATE > 0,BUF);
431 if(STATE > 0) goto L15;
432 HASH=MOD(WORD,1048576L);
433 BUF[0]=1234L*5678L-HASH;
437 L15: SAVEIO(1,true,BUF);
438 HASH=MOD(1234L*5678L-BUF[0],1048576L);
441 L30: if(N == 250)SAVEIO(1,STATE > 0,BUF);
443 if(STATE > 0) goto L32;
444 N--; BUF[N]=CKSUM; N++;
446 L32: N--; WORD=BUF[N]-CKSUM; N++;
447 SAVEIO(-1,STATE > 0,BUF);
456 /* Data struc. routines (VOCAB, DSTROY, JUGGLE, MOVE, PUT, CARRY, DROP, ATDWRF)
460 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
462 long VOCAB(long ID, long INIT) {
465 /* Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
466 * -1 if not found. If INIT is positive, this is an initialisation call setting
467 * up a keyword variable, and not finding it constitutes a bug. It also means
468 * that only KTAB values which taken over 1000 equal INIT may be considered.
469 * (Thus "STEPS", which is a motion verb as well as an object, may be located
470 * as an object.) And it also means the KTAB value is taken modulo 1000. */
472 /* 1 */ for (I=1; I<=TABSIZ; I++) {
473 if(KTAB[I] == -1) goto L2;
474 if(INIT >= 0 && KTAB[I]/1000 != INIT) goto L1;
475 if(ATAB[I] == ID) goto L3;
481 if(INIT < 0)return(LEXEME);
485 if(INIT >= 0)LEXEME=MOD(LEXEME,1000);
489 void DSTROY(long object)
490 /* Permanently eliminate "object" by moving to a non-existent location. */
495 void JUGGLE(long object)
496 /* Juggle an object by picking it up and putting it down again, the purpose
497 * being to get the object to the front of the chain of things at its loc. */
501 i=game.place[object];
502 j=game.fixed[object];
504 MOVE(object+NOBJECTS,j);
507 void MOVE(long object, long where)
508 /* Place any object anywhere by picking it up and dropping it. May
509 * already be toting, in which case the carry is a no-op. Mustn't
510 * pick up objects which are not at any loc, since carry wants to
511 * remove objects from game.atloc chains. */
515 if(object > NOBJECTS) goto L1;
516 from=game.place[object];
518 L1: from=game.fixed[object-NOBJECTS];
519 L2: if(from > 0 && from <= 300)
524 long PUT(long object, long where, long pval)
525 /* PUT is the same as MOVE, except it returns a value used to set up the
526 * negated game.prop values for the repository objects. */
532 void CARRY(long object, long where) {
535 /* Start toting an object, removing it from the list of things at its former
536 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
537 * (moving "fixed" second loc), don't change game.place or game.holdng. */
539 if(object > NOBJECTS) goto L5;
540 if(game.place[object] == -1)return;
541 game.place[object]= -1;
542 game.holdng=game.holdng+1;
543 L5: if(game.atloc[where] != object) goto L6;
544 game.atloc[where]=game.link[object];
546 L6: TEMP=game.atloc[where];
547 L7: if(game.link[TEMP] == object) goto L8;
548 TEMP=game.link[TEMP];
550 L8: game.link[TEMP]=game.link[object];
554 void DROP(long object, long where)
555 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
556 * game.holdng if the object was being toted. */
558 if(object > NOBJECTS) goto L1;
559 if(game.place[object] == -1)
560 game.holdng=game.holdng-1;
561 game.place[object]=where;
563 L1: game.fixed[object-NOBJECTS]=where;
566 game.link[object]=game.atloc[where];
567 game.atloc[where]=object;
570 long ATDWRF(long where)
571 /* Return the index of first dwarf at the given location, zero if no dwarf is
572 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
573 * the pirate (6th dwarf). */
581 for (i=1; i<=NDWARVES-1; i++) {
582 if(game.dloc[i] == where)
584 if(game.dloc[i] != 0)
590 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
591 * randrange, RNDVOC, BUG) */
593 long SETBIT(long bit)
594 /* Returns 2**bit for use in constructing bit-masks. */
599 bool TSTBIT(long mask, int bit)
600 /* Returns true if the specified bit is set in the mask. */
602 return (mask & (1 << bit)) != 0;
605 void set_seed(long seedval)
606 /* Set the LCG seed */
608 lcgstate.x = (unsigned long) seedval % lcgstate.m;
611 unsigned long get_next_lcg_value(void)
612 /* Return the LCG's current value, and then iterate it. */
614 unsigned long old_x = lcgstate.x;
615 lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
619 long randrange(long range)
620 /* Return a random integer from [0, range). */
622 return range * get_next_lcg_value() / lcgstate.m;
625 long RNDVOC(long second, long force)
626 /* Searches the vocabulary ATAB for a word whose second character is
627 * char, and changes that word such that each of the other four
628 * characters is a random letter. If force is non-zero, it is used
629 * as the new word. Returns the new word. */
634 for (int i = 1; i <= 5; i++) {
635 long j = 11 + randrange(26);
642 long div = 64L * 64L * 64L;
643 for (int i = 1; i <= TABSIZ; i++) {
644 if (MOD(ATAB[i]/div, 64L) == second)
655 /* The following conditions are currently considered fatal bugs. Numbers < 20
656 * are detected while reading the database; the others occur at "run time".
657 * 0 Message line > 70 characters
658 * 1 Null line in message
659 * 2 Too many words of messages
660 * 3 Too many travel options
661 * 4 Too many vocabulary words
662 * 5 Required vocabulary word not found
663 * 6 Too many RTEXT messages
665 * 8 Location has cond bit being set twice
666 * 9 Invalid section number in database
667 * 10 Too many locations
668 * 11 Too many class or turn messages
669 * 20 Special travel (500>L>300) exceeds goto list
670 * 21 Ran off end of vocabulary table
671 * 22 Vocabulary type (N/1000) not between 0 and 3
672 * 23 Intransitive action verb exceeds goto list
673 * 24 Transitive action verb exceeds goto list
674 * 25 Conditional travel entry with no alternative
675 * 26 Location has no travel entries
676 * 27 Hint number exceeds goto list
677 * 28 Invalid month returned by date function
678 * 29 Too many parameters given to SETPRM */
681 printf("Fatal error %ld. See source code for interpretation.\n", num);
685 /* Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
687 void MAPLIN(FILE *OPENED)
691 /* Read a line of input, from the specified input source,
692 * translate the chars to integers in the range 0-126 and store
693 * them in the common array "INLINE". Integer values are as follows:
694 * 0 = space [ASCII CODE 40 octal, 32 decimal]
695 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
696 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
697 * 11-36 = upper-case letters
698 * 37-62 = lower-case letters
699 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
700 * 64-73 = digits, 0 through 9
701 * Remaining characters can be translated any way that is convenient;
702 * The "TYPE" routine below is used to map them back to characters when
703 * necessary. The above mappings are required so that certain special
704 * characters are known to fit in 6 bits and/or can be easily spotted.
705 * Array elements beyond the end of the line should be filled with 0,
706 * and LNLENG should be set to the index of the last character.
708 * If the data file uses a character other than space (e.g., tab) to
709 * separate numbers, that character should also translate to 0.
711 * This procedure may use the map1,map2 arrays to maintain static data for
712 * the mapping. MAP2(1) is set to 0 when the program starts
713 * and is not changed thereafter unless the routines on this page choose
716 if(MAP2[1] == 0)MPINIT();
718 if (!oldstyle && OPENED == stdin)
721 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,OPENED));
723 (!feof(OPENED) && rawbuf[0] == '#');
725 if (logfp && OPENED == stdin)
728 if (logfp && OPENED == stdin)
729 IGNORE(fputs(rawbuf, logfp));
731 IGNORE(fputs(rawbuf, stdout));
732 strcpy(INLINE+1, rawbuf);
734 for (I=1; I<=(long)sizeof(INLINE) && INLINE[I]!=0; I++) {
737 if(INLINE[I] != 0)LNLENG=I;
744 /* Type the first "LNLENG" characters stored in inline, mapping them
745 * from integers to text per the rules described above. INLINE
746 * may be changed by this routine. */
757 for (i=1; i<=LNLENG; i++) {
758 INLINE[i]=MAP2[INLINE[i]+1];
761 printf("%s\n", INLINE+1);
766 long FIRST, I, J, LAST, VAL;
767 static long RUNS[7][2] = { {32,34}, {39,46}, {65,90}, {97,122}, {37,37}, {48,57}, {0,126} };
769 for (I=1; I<=128; I++) {
773 for (I=0; I<7; I++) {
776 /* 22 */ for (J=FIRST; J<=LAST; J++) {
777 J++; if(MAP1[J] >= 0) goto L22;
785 /* For this version, tab (9) maps to space (32), so del (127) uses tab's value */
789 for (I=0; I<=126; I++) {
790 I++; VAL=MAP1[I]+1; I--;
791 MAP2[VAL]=I*('B'-'A');
792 if(I >= 64)MAP2[VAL]=(I-64)*('B'-'A')+'@';
799 void fSAVEIO(long OP, long IN, long ARR[]) {
800 static FILE *F; char NAME[50];
802 /* If OP=0, ask for a file name and open a file. (If IN=true, the file is for
803 * input, else output.) If OP>0, read/write ARR from/into the previously-opened
804 * file. (ARR is a 250-integer array.) If OP<0, finish reading/writing the
805 * file. (Finishing writing can be a no-op if a "stop" statement does it
806 * automatically. Finishing reading can be a no-op as long as a subsequent
807 * SAVEIO(0,false,X) will still work.) If you can catch errors (e.g., no such
808 * file) and try again, great. DEC F40 can't. */
811 {long ifvar; ifvar=(OP); switch (ifvar<0? -1 : ifvar>0? 1 : 0) { case -1:
812 goto L10; case 0: goto L20; case 1: goto L30; }}
817 L20: printf("\nFile name: ");
818 IGNORE(fgets(NAME, sizeof(NAME), stdin));
819 F=fopen(NAME,(IN ? READ_MODE : WRITE_MODE));
820 if(F == NULL) {printf("Can't open file, try again.\n"); goto L20;}
823 L30: if(IN)IGNORE(fread(ARR,sizeof(long),250,F));
824 if(!IN)fwrite(ARR,sizeof(long),250,F);
828 void DATIME(long* D, long* T)
831 gettimeofday(&tv, NULL);
832 *D = (long) tv.tv_sec;
833 *T = (long) tv.tv_usec;
836 long MOD(N,M)long N, M; {return(N%M);}