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 /* Print the message which starts at LINES(N). Precede it with a blank line
17 * unless game.blklin is false. */
19 long blank, casemake, I, K, L, NEG, NPARMS, PARM, PRMTYP, state;
32 for (I=K; I<=L; I++) {
33 PUTTXT(LINES[I],state,2);
41 if (INLINE[LNPOSN] != 63)
43 {long x = LNPOSN+1; PRMTYP=INLINE[x];}
44 /* 63 is a "%"; the next character determine the type of
45 * parameter: 1 (!) = suppress message completely, 29 (S) = NULL
46 * If PARM=1, else 'S' (optional plural ending), 33 (W) = word
47 * (two 30-bit values) with trailing spaces suppressed, 22 (L) or
48 * 31 (U) = word but map to lower/upper case, 13 (C) = word in
49 * lower case with first letter capitalised, 30 (T) = text ending
50 * with a word of -1, 65-73 (1-9) = number using that many
51 * characters, 12 (B) = variable number of blanks. */
60 if (PRMTYP == 33 || PRMTYP == 22 || PRMTYP == 31 || PRMTYP == 13)
63 if (PRMTYP < 1 || PRMTYP > 9) goto L30;
64 SHFTXT(LNPOSN+2,PRMTYP-2);
66 PARM=labs(PARMS[NPARMS]);
68 if (PARMS[NPARMS] < 0)
70 /* 390 */ for (I=1; I<=PRMTYP; I++) {
72 INLINE[LNPOSN]=MOD(PARM,10)+64;
73 if (I == 1 || PARM != 0)
88 if (PARMS[NPARMS] == 1)
97 if (PARMS[NPARMS] < 0) goto L395;
98 {long x = NPARMS+1; if (PARMS[x] < 0)
100 PUTTXT(PARMS[NPARMS],state,casemake);
105 PRMTYP=PARMS[NPARMS];
106 SHFTXT(LNPOSN+2,PRMTYP-2);
107 if (PRMTYP == 0) goto L395;
108 for (I=1; I<=PRMTYP; I++) {
123 PUTTXT(PARMS[NPARMS],state,casemake);
124 {long x = NPARMS+1; PUTTXT(PARMS[x],state,casemake);}
125 if (PRMTYP == 13 && INLINE[I] >= 37 && INLINE[I] <=
126 62)INLINE[I]=INLINE[I]-26;
140 void PSPEAK(vocab_t msg,int skip)
141 /* Find the skip+1st message from msg and print it. msg should be
142 * the index of the inventory message for object. (INVEN+N+1 message
143 * is game.prop=N message). */
149 for (i=0; i <=skip; i++) {
159 void RSPEAK(vocab_t i)
160 /* Print the i-th "random" message (section 6 of database). */
166 void SETPRM(long first, long p1, long p2)
167 /* Stores parameters into the PRMCOM parms array for use by speak. P1 and P2
168 * are stored into PARMS(first) and PARMS(first+1). */
170 if (first >= MAXPARMS)
178 bool fGETIN(FILE *input,
179 long *pword1, long *pword1x,
180 long *pword2, long *pword2x)
181 /* Get a command from the adventurer. Snarf out the first word, pad it with
182 * blanks, and return it in WORD1. Chars 6 thru 10 are returned in WORD1X, in
183 * case we need to print out the whole word in an error message. Any number of
184 * blanks may follow the word. If a second word appears, it is returned in
185 * WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
195 *pword1=GETTXT(true,true,true);
196 if (game.blklin && *pword1 < 0)
198 *pword1x=GETTXT(false,true,true);
200 junk=GETTXT(false,true,true);
203 *pword2=GETTXT(true,true,true);
204 *pword2x=GETTXT(false,true,true);
206 junk=GETTXT(false,true,true);
209 if (GETTXT(true,true,true) <= 0)
215 long YES(FILE *input, vocab_t x, vocab_t y, vocab_t z)
216 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
217 * if no, print Z and return false. */
219 token_t reply, junk1, junk2, junk3;
223 GETIN(input, reply,junk1,junk2,junk3);
224 if (reply == MAKEWD(250519) || reply == MAKEWD(25)) {
228 if (reply == MAKEWD(1415) || reply == MAKEWD(14)) {
236 /* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
238 long GETTXT(bool skip, bool onewrd, bool upper)
239 /* Take characters from an input line and pack them into 30-bit words.
240 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
241 * blank. UPPER says to map all letters to uppercase. If we reach the
242 * end of the line, the word is filled up with blanks (which encode as 0's).
243 * If we're already at end of line when TEXT is called, we return -1. */
246 static long splitting = -1;
248 if (LNPOSN != splitting)
254 if ((!skip) || INLINE[LNPOSN] != 0)
260 for (int I=1; I<=TOKLEN; I++) {
262 if (LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
264 char current=INLINE[LNPOSN];
267 if (upper && current >= 37)
273 if (splitting != LNPOSN) {
279 text=text+current-63;
287 token_t MAKEWD(long letters)
288 /* Combine TOKLEN (currently 5) uppercase letters (represented by
289 * pairs of decimal digits in lettrs) to form a 30-bit value matching
290 * the one that GETTXT would return given those characters plus
291 * trailing blanks. Caution: lettrs will overflow 31 bits if
292 * 5-letter word starts with V-Z. As a kludgey workaround, you can
293 * increment a letter by 5 by adding 50 to the next pair of
296 long i = 1, word = 0;
298 for (long k=letters; k != 0; k=k/100) {
299 word=word+i*(MOD(k,50)+10);
301 if (MOD(k,100) > 50)word=word+i*5;
303 i=64L*64L*64L*64L*64L/i;
308 void fPUTTXT(token_t word, long *state, long casemake)
309 /* Unpack the 30-bit value in word to obtain up to TOKLEN (currently
310 * 5) integer-encoded chars, and store them in inline starting at
311 * LNPOSN. If LNLENG>=LNPOSN, shift existing characters to the right
312 * to make room. STATE will be zero when puttxt is called with the
313 * first of a sequence of words, but is thereafter unchanged by the
314 * caller, so PUTTXT can use it to maintain state across calls.
315 * LNPOSN and LNLENG are incremented by the number of chars stored.
316 * If CASEMAKE=1, all letters are made uppercase; if -1, lowercase; if 0,
317 * as is. any other value for case is the same as 0 but also causes
318 * trailing blanks to be included (in anticipation of subsequent
319 * additional text). */
321 long alph1, alph2, byte, div, i, w;
323 alph1=13*casemake+24;
324 alph2=26*labs(casemake)+alph1;
325 if (labs(casemake) > 1)
327 /* alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
330 for (i=1; i<=TOKLEN; i++)
332 if (w <= 0 && *state == 0 && labs(casemake) <= 1)
336 if (!(*state != 0 || byte != 63)) {
342 if (*state < alph2 && *state >= alph1)*state=*state-26*casemake;
343 INLINE[LNPOSN]=*state;
348 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
350 void SHFTXT(long from, long delta)
351 /* Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG. Delta can be
352 * negative. LNLENG is updated; LNPOSN is not changed. */
356 if (!(LNLENG < from || delta == 0)) {
357 for (I=from; I<=LNLENG; I++) {
369 /* Type a blank line. This procedure is provided as a convenience for callers
370 * who otherwise have no use for MAPCOM. */
381 /* Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
383 void fSAVWDS(long *W1, long *W2, long *W3, long *W4,
384 long *W5, long *W6, long *W7)
385 /* Write or read 7 variables. See SAVWRD. */
396 void fSAVARR(long arr[], long n)
397 /* Write or read an array of n words. See SAVWRD. */
401 for (i=1; i<=n; i++) {
407 void fSAVWRD(long op, long *pword)
408 /* If OP<0, start writing a file, using word to initialise encryption; save
409 * word in the file. If OP>0, start reading a file; read the file to find
410 * the value with which to decrypt the rest. In either case, if a file is
411 * already open, finish writing/reading it and don't start a new one. If OP=0,
412 * read/write a single word. Words are buffered in case that makes for more
413 * efficient disk use. We also compute a simple checksum to catch elementary
414 * poking within the saved file. When we finish reading/writing the file,
415 * we store zero into *PWORD if there's no checksum error, else nonzero. */
417 static long buf[250], cksum = 0, h1, hash = 0, n = 0, state = 0;
422 switch (ifvar<0 ? -1 : (ifvar>0 ? 1 : 0))
426 if (n == 250)SAVEIO(1,state > 0,buf);
429 n--; buf[n]=cksum; n++;
432 n--; *pword=buf[n]-cksum; n++;
433 SAVEIO(-1,state > 0,buf);
436 case 0: /* FIXME: Huh? should be impossible */
438 SAVEIO(0,state > 0,buf);
441 hash=MOD(*pword,1048576L);
442 buf[0]=1234L*5678L-hash;
445 hash=MOD(1234L*5678L-buf[0],1048576L);
453 SAVEIO(1,state > 0,buf);
455 h1=MOD(hash*1093L+221573L,1048576L);
456 hash=MOD(h1*1093L+221573L,1048576L);
457 h1=MOD(h1,1234)*765432+MOD(hash,123);
463 cksum=MOD(cksum*13+*pword,1000000000L);
466 /* Data structure routines */
468 long VOCAB(long id, long init)
469 /* Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
470 * -1 if not found. If INIT is positive, this is an initialisation call setting
471 * up a keyword variable, and not finding it constitutes a bug. It also means
472 * that only KTAB values which taken over 1000 equal INIT may be considered.
473 * (Thus "STEPS", which is a motion verb as well as an object, may be located
474 * as an object.) And it also means the KTAB value is taken modulo 1000. */
478 for (i=1; i<=TABSIZ; i++) {
485 if (init >= 0 && KTAB[i]/1000 != init)
490 lexeme=MOD(lexeme,1000);
497 void DSTROY(long object)
498 /* Permanently eliminate "object" by moving to a non-existent location. */
503 void JUGGLE(long object)
504 /* Juggle an object by picking it up and putting it down again, the purpose
505 * being to get the object to the front of the chain of things at its loc. */
509 i=game.place[object];
510 j=game.fixed[object];
512 MOVE(object+NOBJECTS,j);
515 void MOVE(long object, long where)
516 /* Place any object anywhere by picking it up and dropping it. May
517 * already be toting, in which case the carry is a no-op. Mustn't
518 * pick up objects which are not at any loc, since carry wants to
519 * remove objects from game.atloc chains. */
523 if (object > NOBJECTS)
525 from=game.place[object];
528 from=game.fixed[object-NOBJECTS];
530 if (from > 0 && from <= 300)
535 long PUT(long object, long where, long pval)
536 /* PUT is the same as MOVE, except it returns a value used to set up the
537 * negated game.prop values for the repository objects. */
543 void CARRY(long object, long where)
544 /* Start toting an object, removing it from the list of things at its former
545 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
546 * (moving "fixed" second loc), don't change game.place or game.holdng. */
550 if (object <= NOBJECTS) {
551 if (game.place[object] == -1)
553 game.place[object]= -1;
554 game.holdng=game.holdng+1;
556 if (game.atloc[where] == object) {
557 game.atloc[where]=game.link[object];
560 temp=game.atloc[where];
561 L7: if (game.link[temp] == object)
563 temp=game.link[temp];
565 L8: game.link[temp]=game.link[object];
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. */
572 if (object > NOBJECTS)
574 if (game.place[object] == -1)
575 game.holdng=game.holdng-1;
576 game.place[object]=where;
578 L1: game.fixed[object-NOBJECTS]=where;
581 game.link[object]=game.atloc[where];
582 game.atloc[where]=object;
585 long ATDWRF(long where)
586 /* Return the index of first dwarf at the given location, zero if no dwarf is
587 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
588 * the pirate (6th dwarf). */
596 for (i=1; i<=NDWARVES-1; i++) {
597 if (game.dloc[i] == where)
599 if (game.dloc[i] != 0)
605 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
606 * randrange, RNDVOC, BUG) */
608 long SETBIT(long bit)
609 /* 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. */
617 return (mask & (1 << bit)) != 0;
620 void set_seed(long seedval)
621 /* Set the LCG seed */
623 lcgstate.x = (unsigned long) seedval % lcgstate.m;
626 unsigned long get_next_lcg_value(void)
627 /* Return the LCG's current value, and then iterate it. */
629 unsigned long old_x = lcgstate.x;
630 lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
634 long randrange(long range)
635 /* Return a random integer from [0, range). */
637 return range * get_next_lcg_value() / lcgstate.m;
640 long RNDVOC(long second, long force)
641 /* Searches the vocabulary ATAB for a word whose second character is
642 * char, and changes that word such that each of the other four
643 * characters is a random letter. If force is non-zero, it is used
644 * as the new word. Returns the new word. */
649 for (int i = 1; i <= 5; i++) {
650 long j = 11 + randrange(26);
657 long div = 64L * 64L * 64L;
658 for (int i = 1; i <= TABSIZ; i++) {
659 if (MOD(ATAB[i]/div, 64L) == second)
670 /* The following conditions are currently considered fatal bugs. Numbers < 20
671 * are detected while reading the database; the others occur at "run time".
672 * 0 Message line > 70 characters
673 * 1 Null line in message
674 * 2 Too many words of messages
675 * 3 Too many travel options
676 * 4 Too many vocabulary words
677 * 5 Required vocabulary word not found
678 * 6 Too many RTEXT messages
680 * 8 Location has cond bit being set twice
681 * 9 Invalid section number in database
682 * 10 Too many locations
683 * 11 Too many class or turn messages
684 * 20 Special travel (500>L>300) exceeds goto list
685 * 21 Ran off end of vocabulary table
686 * 22 Vocabulary type (N/1000) not between 0 and 3
687 * 23 Intransitive action verb exceeds goto list
688 * 24 Transitive action verb exceeds goto list
689 * 25 Conditional travel entry with no alternative
690 * 26 Location has no travel entries
691 * 27 Hint number exceeds goto list
692 * 28 Invalid month returned by date function
693 * 29 Too many parameters given to SETPRM */
696 printf("Fatal error %ld. See source code for interpretation.\n", num);
700 /* Machine dependent routines (MAPLIN, TYPE, MPINIT, SAVEIO) */
702 void MAPLIN(FILE *fp)
706 /* Read a line of input, from the specified input source,
707 * translate the chars to integers in the range 0-126 and store
708 * them in the common array "INLINE". Integer values are as follows:
709 * 0 = space [ASCII CODE 40 octal, 32 decimal]
710 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
711 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
712 * 11-36 = upper-case letters
713 * 37-62 = lower-case letters
714 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
715 * 64-73 = digits, 0 through 9
716 * Remaining characters can be translated any way that is convenient;
717 * The "TYPE" routine below is used to map them back to characters when
718 * necessary. The above mappings are required so that certain special
719 * characters are known to fit in 6 bits and/or can be easily spotted.
720 * Array elements beyond the end of the line should be filled with 0,
721 * and LNLENG should be set to the index of the last character.
723 * If the data file uses a character other than space (e.g., tab) to
724 * separate numbers, that character should also translate to 0.
726 * This procedure may use the map1,map2 arrays to maintain static data for
727 * the mapping. MAP2(1) is set to 0 when the program starts
728 * and is not changed thereafter unless the routines on this page choose
734 if (!oldstyle && fp == stdin)
737 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
739 (!feof(fp) && rawbuf[0] == '#');
741 if (logfp && fp == stdin)
744 if (logfp && fp == stdin)
745 IGNORE(fputs(rawbuf, logfp));
747 IGNORE(fputs(rawbuf, stdout));
748 strcpy(INLINE+1, rawbuf);
750 for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) {
761 /* Type the first "LNLENG" characters stored in inline, mapping them
762 * from integers to text per the rules described above. INLINE
763 * may be changed by this routine. */
774 for (i=1; i<=LNLENG; i++) {
775 INLINE[i]=MAP2[INLINE[i]+1];
778 printf("%s\n", INLINE+1);
784 long first, i, j, last, val;
785 static long RUNS[7][2] = { {32,34}, {39,46}, {65,90}, {97,122},
786 {37,37}, {48,57}, {0,126} };
787 for (i=1; i<=128; i++) {
791 for (i=0; i<7; i++) {
794 for (j=first; j<=last; j++) {
804 /* For this version, tab (9) maps to space (32), so del (127)
805 * uses tab's value */
809 for (i=0; i<=126; i++) {
810 i++; val=MAP1[i]+1; i--;
811 MAP2[val] = i*('B'-'A');
813 MAP2[val]=(i-64)*('B'-'A')+'@';
817 void fSAVEIO(long op, long in, long arr[])
818 /* If OP=0, ask for a file name and open a file. (If IN=true, the file is for
819 * input, else output.) If OP>0, read/write ARR from/into the previously-opened
820 * file. (ARR is a 250-integer array.) If OP<0, finish reading/writing the
821 * file. (Finishing writing can be a no-op if a "stop" statement does it
822 * automatically. Finishing reading can be a no-op as long as a subsequent
823 * SAVEIO(0,false,X) will still work.) */
825 static FILE *fp = NULL;
828 switch (op < 0 ? -1 : (op > 0 ? 1 : 0))
835 printf("\nFile name: ");
836 IGNORE(fgets(name, sizeof(name), stdin));
837 fp = fopen(name,(in ? READ_MODE : WRITE_MODE));
839 printf("Can't open file %s, try again.\n", name);
844 IGNORE(fread(arr,sizeof(long),250,fp));
846 IGNORE(fwrite(arr,sizeof(long),250,fp));
851 void DATIME(long* d, long* t)
854 gettimeofday(&tv, NULL);
855 *d = (long) tv.tv_sec;
856 *t = (long) tv.tv_usec;
859 long MOD(long n, long m)