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) */
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 BLKLIN is false. */
25 L10: L=IABS(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=IABS(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(long MSG,long SKIP) {
117 /* Find the skip+1st message from msg and print it. MSG should be the index of
118 * the inventory message for object. (INVEN+N+1 message is PROP=N message). */
122 if(SKIP < 0) goto L9;
123 for (I=0; I<=SKIP; I++) {
124 L1: M=IABS(LINES[M]);
125 if(LINES[M] >= 0) goto L1;
132 void RSPEAK(long I) {
134 /* Print the I-TH "random" message (section 6 of database). */
137 if(I != 0)SPEAK(RTEXT[I]);
142 void SETPRM(long FIRST, long P1, long P2) {
145 /* Stores parameters into the PRMCOM parms array for use by speak. P1 and P2
146 * are stored into PARMS(FIRST) and PARMS(FIRST+1). */
149 if(FIRST >= 25)BUG(29);
151 {long x = FIRST+1; PARMS[x]=P2;}
157 #define WORD1 (*wORD1)
158 #define WORD1X (*wORD1X)
159 #define WORD2 (*wORD2)
160 #define WORD2X (*wORD2X)
161 bool fGETIN(FILE *input, long *wORD1, long *wORD1X, long *wORD2, long *wORD2X) {
164 /* Get a command from the adventurer. Snarf out the first word, pad it with
165 * blanks, and return it in WORD1. Chars 6 thru 10 are returned in WORD1X, in
166 * case we need to print out the whole word in an error message. Any number of
167 * blanks may follow the word. If a second word appears, it is returned in
168 * WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
171 L10: if(BLKLIN)TYPE0();
175 WORD1=GETTXT(true,true,true);
176 if(BLKLIN && WORD1 < 0) goto L10;
177 WORD1X=GETTXT(false,true,true);
178 L12: JUNK=GETTXT(false,true,true);
179 if(JUNK > 0) goto L12;
180 WORD2=GETTXT(true,true,true);
181 WORD2X=GETTXT(false,true,true);
182 L22: JUNK=GETTXT(false,true,true);
183 if(JUNK > 0) goto L22;
184 if(GETTXT(true,true,true) <= 0)return true;
195 #define GETIN(SRC,WORD1,WORD1X,WORD2,WORD2X) fGETIN(SRC,&WORD1,&WORD1X,&WORD2,&WORD2X)
197 long YES(FILE *input, long X, long Y, long Z) {
198 long YEAH, REPLY, JUNK1, JUNK2, JUNK3;
200 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
201 * if no, print Z and return false. */
204 GETIN(input, REPLY,JUNK1,JUNK2,JUNK3);
205 if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
206 if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
218 /* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
220 /* The routines on this page handle all the stuff that would normally be
221 * taken care of by format statements. We do it this way instead so that
222 * we can handle textual data in a machine independent fashion. All the
223 * machine dependent i/o stuff is on the following page. See that page
224 * for a description of MAPCOM's inline array. */
226 long GETTXT(long SKIP,long ONEWRD, long UPPER) {
227 /* Take characters from an input line and pack them into 30-bit words.
228 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
229 * blank. UPPER says to map all letters to uppercase. If we reach the
230 * end of the line, the word is filled up with blanks (which encode as 0's).
231 * If we're already at end of line when TEXT is called, we return -1. */
234 static long SPLITTING = -1;
236 if(LNPOSN != SPLITTING)
242 if((!SKIP) || INLINE[LNPOSN] != 0)
248 for (int I=1; I<=5; I++) {
250 if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0))
252 char current=INLINE[LNPOSN];
255 if(UPPER && current >= 37)
261 if(SPLITTING != LNPOSN) {
267 TEXT=TEXT+current-63;
275 long MAKEWD(long LETTRS) {
278 /* Combine five uppercase letters (represented by pairs of decimal digits
279 * in lettrs) to form a 30-bit value matching the one that GETTXT would
280 * return given those characters plus trailing blanks. Caution:
281 * lettrs will overflow 31 bits if 5-letter word starts with V-Z. As a
282 * kludgey workaround, you can increment a letter by 5 by adding 50 to
283 * the next pair of digits. */
289 L10: WORD=WORD+I*(MOD(L,50)+10);
291 if(MOD(L,100) > 50)WORD=WORD+I*5;
294 I=64L*64L*64L*64L*64L/I;
300 #define STATE (*sTATE)
301 void fPUTTXT(long WORD, long *sTATE, long CASE) {
302 long ALPH1, ALPH2, BYTE, DIV, I, W;
304 /* Unpack the 30-bit value in word to obtain up to 5 integer-encoded chars,
305 * and store them in inline starting at LNPOSN. If LNLENG>=LNPOSN, shift
306 * existing characters to the right to make room. STATE will be zero when
307 * puttxt is called with the first of a sequence of words, but is thereafter
308 * unchanged by the caller, so PUTTXT can use it to maintain state across
309 * calls. LNPOSN and LNLENG are incremented by the number of chars stored.
310 * If CASE=1, all letters are made uppercase; if -1, lowercase; if 0, as is.
311 * any other value for case is the same as 0 but also causes trailing blanks
312 * to be included (in anticipation of subsequent additional text). */
316 ALPH2=26*IABS(CASE)+ALPH1;
317 if(IABS(CASE) > 1)ALPH1=ALPH2;
318 /* ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
321 /* 18 */ for (I=1; I<=5; I++) {
322 if(W <= 0 && STATE == 0 && IABS(CASE) <= 1)return;
324 if(STATE != 0 || BYTE != 63) goto L12;
328 L12: SHFTXT(LNPOSN,1);
330 if(STATE < ALPH2 && STATE >= ALPH1)STATE=STATE-26*CASE;
331 INLINE[LNPOSN]=STATE;
334 L18: W=(W-BYTE*DIV)*64;
340 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
342 void SHFTXT(long FROM, long DELTA) {
345 /* Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG. Delta can be
346 * negative. LNLENG is updated; LNPOSN is not changed. */
349 if(LNLENG < FROM || DELTA == 0) goto L2;
350 for (I=FROM; I<=LNLENG; I++) {
352 if(DELTA > 0)II=FROM+LNLENG-I;
354 INLINE[JJ]=INLINE[II];
356 L2: LNLENG=LNLENG+DELTA;
363 /* Type a blank line. This procedure is provided as a convenience for callers
364 * who otherwise have no use for MAPCOM. */
373 /* Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
376 void fSAVWDS(long *W1, long *W2, long *W3, long *W4, long *W5, long *W6, long *W7) {
378 /* Write or read 7 variables. See SAVWRD. */
391 #define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
393 void fSAVARR(long ARR[], long N) {
396 /* Write or read an array of N words. See SAVWRD. */
399 for (I=1; I<=N; I++) {
407 #define SAVARR(ARR,N) fSAVARR(ARR,N)
410 void fSAVWRD(long OP, long *wORD) {
411 static long BUF[250], CKSUM = 0, H1, HASH = 0, N = 0, STATE = 0;
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 WORD if there's no checksum error, else nonzero. */
422 if(OP != 0){long ifvar; ifvar=(STATE); switch (ifvar<0? -1 : ifvar>0? 1 :
423 0) { case -1: goto L30; case 0: goto L10; case 1: goto L30; }}
424 if(STATE == 0)return;
425 if(N == 250)SAVEIO(1,STATE > 0,BUF);
427 H1=MOD(HASH*1093L+221573L,1048576L);
428 HASH=MOD(H1*1093L+221573L,1048576L);
429 H1=MOD(H1,1234)*765432+MOD(HASH,123);
431 if(STATE > 0)WORD=BUF[N]+H1;
434 CKSUM=MOD(CKSUM*13+WORD,1000000000L);
438 SAVEIO(0,STATE > 0,BUF);
440 if(STATE > 0) goto L15;
441 HASH=MOD(WORD,1048576L);
442 BUF[0]=1234L*5678L-HASH;
446 L15: SAVEIO(1,true,BUF);
447 HASH=MOD(1234L*5678L-BUF[0],1048576L);
450 L30: if(N == 250)SAVEIO(1,STATE > 0,BUF);
452 if(STATE > 0) goto L32;
453 N--; BUF[N]=CKSUM; N++;
455 L32: N--; WORD=BUF[N]-CKSUM; N++;
456 SAVEIO(-1,STATE > 0,BUF);
465 /* Data struc. routines (VOCAB, DSTROY, JUGGLE, MOVE, PUT, CARRY, DROP, ATDWRF)
469 #define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
471 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. */
481 /* 1 */ for (I=1; I<=TABSIZ; I++) {
482 if(KTAB[I] == -1) goto L2;
483 if(INIT >= 0 && KTAB[I]/1000 != INIT) goto L1;
484 if(ATAB[I] == ID) goto L3;
490 if(INIT < 0)return(LEXEME);
494 if(INIT >= 0)LEXEME=MOD(LEXEME,1000);
498 void DSTROY(long OBJECT) {
499 /* Permanently eliminate "OBJECT" by moving to a non-existent location. */
505 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. */
518 void MOVE(long OBJECT, long WHERE) {
521 /* Place any object anywhere by picking it up and dropping it. May already be
522 * toting, in which case the carry is a no-op. Mustn't pick up objects which
523 * are not at any loc, since carry wants to remove objects from ATLOC chains. */
526 if(OBJECT > 100) goto L1;
529 L1: {long x = OBJECT-100; FROM=FIXED[x];}
530 L2: if(FROM > 0 && FROM <= 300)CARRY(OBJECT,FROM);
535 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 PROP values for the repository objects. */
546 void CARRY(long OBJECT, long WHERE) {
549 /* Start toting an object, removing it from the list of things at its former
550 * location. Incr holdng unless it was already being toted. If OBJECT>100
551 * (moving "fixed" second loc), don't change PLACE or HOLDNG. */
553 if(OBJECT > 100) goto L5;
554 if(PLACE[OBJECT] == -1)return;
557 L5: if(ATLOC[WHERE] != OBJECT) goto L6;
558 ATLOC[WHERE]=LINK[OBJECT];
560 L6: TEMP=ATLOC[WHERE];
561 L7: if(LINK[TEMP] == OBJECT) goto L8;
564 L8: LINK[TEMP]=LINK[OBJECT];
568 void DROP(long OBJECT, long WHERE) {
569 /* Place an object at a given loc, prefixing it onto the ATLOC list. Decr
570 * HOLDNG if the object was being toted. */
572 if(OBJECT > 100) goto L1;
573 if(PLACE[OBJECT] == -1)HOLDNG=HOLDNG-1;
576 L1: {long x = OBJECT-100; FIXED[x]=WHERE;}
577 L2: if(WHERE <= 0)return;
578 LINK[OBJECT]=ATLOC[WHERE];
585 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). */
594 if(DFLAG < 2)return(AT);
596 for (I=1; I<=5; I++) {
597 if(DLOC[I] == WHERE) goto L2;
598 if(DLOC[I] != 0)AT=0;
606 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
607 * randrange, RNDVOC, BUG) */
609 long SETBIT(long bit) {
610 /* Returns 2**bit for use in constructing bit-masks. */
614 bool TSTBIT(long mask, int bit) {
615 /* Returns true if the specified bit is set in the mask. */
616 return((mask & (1 << bit)) != 0);
619 void set_seed(long seedval)
621 lcgstate.x = (unsigned long) seedval % lcgstate.m;
624 unsigned long get_next_lcg_value(void)
626 /* Return the LCG's current value, and then iterate it. */
627 unsigned long old_x = lcgstate.x;
628 lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
632 long randrange(long range)
634 /* Return a random integer from [0, range). */
635 long result = range * get_next_lcg_value() / lcgstate.m;
639 long RNDVOC(long CHAR, long FORCE) {
640 /* Searches the vocabulary for a word whose second character is char, and
641 * changes that word such that each of the other four characters is a
642 * random letter. If force is non-zero, it is used as the new word.
643 * Returns the new word. */
650 for (int I = 1; I <= 5; I++) {
651 long J = 11 + randrange(26);
658 long DIV = 64L * 64L * 64L;
659 for (int I = 1; I <= TABSIZ; I++) {
660 if (MOD(ATAB[I]/DIV, 64L) == CHAR)
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 */
697 printf("Fatal error %ld. See source code for interpretation.\n",
706 /* Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
708 void MAPLIN(FILE *OPENED) {
711 /* Read a line of input, from the specified input source,
712 * translate the chars to integers in the range 0-126 and store
713 * them in the common array "INLINE". Integer values are as follows:
714 * 0 = space [ASCII CODE 40 octal, 32 decimal]
715 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
716 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
717 * 11-36 = upper-case letters
718 * 37-62 = lower-case letters
719 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
720 * 64-73 = digits, 0 through 9
721 * Remaining characters can be translated any way that is convenient;
722 * The "TYPE" routine below is used to map them back to characters when
723 * necessary. The above mappings are required so that certain special
724 * characters are known to fit in 6 bits and/or can be easily spotted.
725 * Array elements beyond the end of the line should be filled with 0,
726 * and LNLENG should be set to the index of the last character.
728 * If the data file uses a character other than space (e.g., tab) to
729 * separate numbers, that character should also translate to 0.
731 * This procedure may use the map1,map2 arrays to maintain static data for
732 * the mapping. MAP2(1) is set to 0 when the program starts
733 * and is not changed thereafter unless the routines on this page choose
736 if(MAP2[1] == 0)MPINIT();
738 if (!oldstyle && SETUP && OPENED == stdin)
741 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,OPENED));
743 (!feof(OPENED) && rawbuf[0] == '#');
745 if (logfp && OPENED == stdin)
748 if (logfp && OPENED == stdin)
749 IGNORE(fputs(rawbuf, logfp));
751 IGNORE(fputs(rawbuf, stdout));
752 strcpy(INLINE+1, rawbuf);
754 for (I=1; I<=(long)sizeof(INLINE) && INLINE[I]!=0; I++) {
757 if(INLINE[I] != 0)LNLENG=I;
766 /* Type the first "LNLENG" characters stored in inline, mapping them
767 * from integers to text per the rules described above. INLINE(I),
768 * I=1,LNLENG may be changed by this routine. */
770 if(LNLENG != 0) goto L10;
774 L10: if(MAP2[1] == 0)MPINIT();
775 for (I=1; I<=LNLENG; I++) {
777 {long x = VAL+1; INLINE[I]=MAP2[x];}
779 {long x = LNLENG+1; INLINE[x]=0;}
780 printf("%s\n",INLINE+1);
785 long FIRST, I, J, LAST, VAL;
786 static long RUNS[7][2] = { {32,34}, {39,46}, {65,90}, {97,122}, {37,37}, {48,57}, {0,126} };
788 for (I=1; I<=128; I++) {
792 for (I=0; I<7; I++) {
795 /* 22 */ for (J=FIRST; J<=LAST; J++) {
796 J++; if(MAP1[J] >= 0) goto L22;
804 /* For this version, tab (9) maps to space (32), so del (127) uses tab's value */
808 for (I=0; I<=126; I++) {
809 I++; VAL=MAP1[I]+1; I--;
810 MAP2[VAL]=I*('B'-'A');
811 if(I >= 64)MAP2[VAL]=(I-64)*('B'-'A')+'@';
818 void fSAVEIO(long OP, long IN, long ARR[]) {
819 static FILE *F; char NAME[50];
821 /* If OP=0, ask for a file name and open a file. (If IN=true, the file is for
822 * input, else output.) If OP>0, read/write ARR from/into the previously-opened
823 * file. (ARR is a 250-integer array.) If OP<0, finish reading/writing the
824 * file. (Finishing writing can be a no-op if a "stop" statement does it
825 * automatically. Finishing reading can be a no-op as long as a subsequent
826 * SAVEIO(0,false,X) will still work.) If you can catch errors (e.g., no such
827 * file) and try again, great. DEC F40 can't. */
830 {long ifvar; ifvar=(OP); switch (ifvar<0? -1 : ifvar>0? 1 : 0) { case -1:
831 goto L10; case 0: goto L20; case 1: goto L30; }}
836 L20: printf("\nFile name: ");
837 IGNORE(fgets(NAME, sizeof(NAME), stdin));
838 F=fopen(NAME,(IN ? READ_MODE : WRITE_MODE));
839 if(F == NULL) {printf("Can't open file, try again.\n"); goto L20;}
842 L30: if(IN)IGNORE(fread(ARR,sizeof(long),250,F));
843 if(!IN)fwrite(ARR,sizeof(long),250,F);
848 void DATIME(long* D, long* T) {
850 gettimeofday(&tv, NULL);
851 *D = (long) tv.tv_sec;
852 *T = (long) tv.tv_usec;
855 long IABS(N)long N; {return(N<0? -N : N);}
856 long MOD(N,M)long N, M; {return(N%M);}