10 #include "linenoise/linenoise.h"
12 /* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
14 void SPEAK(vocab_t msg)
15 /* Print the message which starts at LINES[N]. Precede it with a blank line
16 * unless game.blklin is false. */
18 long blank, casemake, i, nxt, neg, nparms, param, prmtyp, state;
25 nxt=labs(LINES[msg])-1;
30 for (i = msg; i <= nxt; i++) {
31 PUTTXT(LINES[i],&state,2);
36 while (LNPOSN <= LNLENG) {
37 if (INLINE[LNPOSN] != ascii_to_advent['%']) {
41 prmtyp = INLINE[LNPOSN+1];
42 /* A "%"; the next character determine the type of
43 * parameter: 1 (!) = suppress message completely, 29 (S) = NULL
44 * If PARAM=1, else 'S' (optional plural ending), 33 (W) = word
45 * (two 30-bit values) with trailing spaces suppressed, 22 (L) or
46 * 31 (U) = word but map to lower/upper case, 13 (C) = word in
47 * lower case with first letter capitalised, 30 (T) = text ending
48 * with a word of -1, 65-73 (1-9) = number using that many
49 * characters, 12 (B) = variable number of blanks. */
50 if (prmtyp == ascii_to_advent['!'])
52 if (prmtyp == ascii_to_advent['S']) {
54 INLINE[LNPOSN] = ascii_to_advent['s'];
55 if (PARMS[nparms] == 1)
60 if (prmtyp == ascii_to_advent['T']) {
65 while (PARMS[nparms] > 0) {
66 if (PARMS[nparms+1] < 0)
68 PUTTXT(PARMS[nparms],&state,casemake);
74 if (prmtyp == ascii_to_advent['B']) {
76 SHFTXT(LNPOSN+2,prmtyp-2);
78 for (i=1; i<=prmtyp; i++) {
86 if (prmtyp == ascii_to_advent['W'] || prmtyp == ascii_to_advent['L'] || prmtyp == ascii_to_advent['U'] || prmtyp == ascii_to_advent['C']) {
90 if (prmtyp == ascii_to_advent['U'])
92 if (prmtyp == ascii_to_advent['W'])
95 PUTTXT(PARMS[nparms],&state,casemake);
96 PUTTXT(PARMS[nparms+1],&state,casemake);
97 if (prmtyp == ascii_to_advent['C'] && INLINE[i] >= ascii_to_advent['a'] && INLINE[i] <= ascii_to_advent['z'])
99 // Convert to uppercase.
100 // Round-trip to ASCII and back so that this code doesn't break when the mapping changes.
101 // This can be simplified when mapping goes away.
102 char this = advent_to_ascii[INLINE[i]];
103 char uc_this = toupper(this);
104 INLINE[i] = ascii_to_advent[uc_this];
111 if (prmtyp < ascii_to_advent['!'] || prmtyp > ascii_to_advent['-']) {
115 SHFTXT(LNPOSN+2,prmtyp-2);
117 param=labs(PARMS[nparms]);
119 if (PARMS[nparms] < 0)
121 for (i=1; i <= prmtyp; i++) {
123 INLINE[LNPOSN]=MOD(param,10)+64;
124 if (i != 1 && param == 0) {
144 void PSPEAK(vocab_t msg,int skip)
145 /* Find the skip+1st message from msg and print it. msg should be
146 * the index of the inventory message for object. (INVEN+N+1 message
147 * is game.prop=N message). */
153 for (i=0; i <=skip; i++) {
163 void RSPEAK(vocab_t i)
164 /* Print the i-th "random" message (section 6 of database). */
170 void SETPRM(long first, long p1, long p2)
171 /* Stores parameters into the PRMCOM parms array for use by speak. P1 and P2
172 * are stored into PARMS(first) and PARMS(first+1). */
174 if (first >= MAXPARMS)
182 bool GETIN(FILE *input,
183 long *pword1, long *pword1x,
184 long *pword2, long *pword2x)
185 /* Get a command from the adventurer. Snarf out the first word, pad it with
186 * blanks, and return it in WORD1. Chars 6 thru 10 are returned in WORD1X, in
187 * case we need to print out the whole word in an error message. Any number of
188 * blanks may follow the word. If a second word appears, it is returned in
189 * WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
198 *pword1=GETTXT(true,true,true);
199 if (game.blklin && *pword1 < 0)
201 *pword1x=GETTXT(false,true,true);
203 junk=GETTXT(false,true,true);
206 *pword2=GETTXT(true,true,true);
207 *pword2x=GETTXT(false,true,true);
209 junk=GETTXT(false,true,true);
212 if (GETTXT(true,true,true) <= 0)
218 long YES(FILE *input, vocab_t x, vocab_t y, vocab_t z)
219 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
220 * if no, print Z and return false. */
222 token_t reply, junk1, junk2, junk3;
226 GETIN(input, &reply, &junk1, &junk2, &junk3);
227 if (reply == MAKEWD(250519) || reply == MAKEWD(25)) {
231 if (reply == MAKEWD(1415) || reply == MAKEWD(14)) {
239 /* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
241 long GETTXT(bool skip, bool onewrd, bool upper)
242 /* Take characters from an input line and pack them into 30-bit words.
243 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
244 * blank. UPPER says to map all letters to uppercase. If we reach the
245 * end of the line, the word is filled up with blanks (which encode as 0's).
246 * If we're already at end of line when TEXT is called, we return -1. */
249 static long splitting = -1;
251 if (LNPOSN != splitting)
257 if ((!skip) || INLINE[LNPOSN] != 0)
263 for (int I=1; I<=TOKLEN; I++) {
265 if (LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
267 char current=INLINE[LNPOSN];
268 if (current < ascii_to_advent['%']) {
270 if (upper && current >= ascii_to_advent['a'])
276 if (splitting != LNPOSN) {
277 text=text+ascii_to_advent['%'];
282 text=text+current-ascii_to_advent['%'];
290 token_t MAKEWD(long letters)
291 /* Combine TOKLEN (currently 5) uppercase letters (represented by
292 * pairs of decimal digits in lettrs) to form a 30-bit value matching
293 * the one that GETTXT would return given those characters plus
294 * trailing blanks. Caution: lettrs will overflow 31 bits if
295 * 5-letter word starts with V-Z. As a kludgey workaround, you can
296 * increment a letter by 5 by adding 50 to the next pair of
299 long i = 1, word = 0;
301 for (long k=letters; k != 0; k=k/100) {
302 word=word+i*(MOD(k,50)+10);
304 if (MOD(k,100) > 50)word=word+i*5;
306 i=64L*64L*64L*64L*64L/i;
311 void PUTTXT(token_t word, long *state, long casemake)
312 /* Unpack the 30-bit value in word to obtain up to TOKLEN (currently
313 * 5) integer-encoded chars, and store them in inline starting at
314 * LNPOSN. If LNLENG>=LNPOSN, shift existing characters to the right
315 * to make room. STATE will be zero when puttxt is called with the
316 * first of a sequence of words, but is thereafter unchanged by the
317 * caller, so PUTTXT can use it to maintain state across calls.
318 * LNPOSN and LNLENG are incremented by the number of chars stored.
319 * If CASEMAKE=1, all letters are made uppercase; if -1, lowercase; if 0,
320 * as is. any other value for case is the same as 0 but also causes
321 * trailing blanks to be included (in anticipation of subsequent
322 * additional text). */
324 long alph1, alph2, byte, div, i, w;
326 alph1=13*casemake+24;
327 alph2=26*labs(casemake)+alph1;
328 if (labs(casemake) > 1)
330 /* alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
333 for (i=1; i<=TOKLEN; i++)
335 if (w <= 0 && *state == 0 && labs(casemake) <= 1)
339 if (!(*state != 0 || byte != ascii_to_advent['%'])) {
340 *state=ascii_to_advent['%'];
345 if (*state < alph2 && *state >= alph1)*state=*state-26*casemake;
346 INLINE[LNPOSN]=*state;
351 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
353 void SHFTXT(long from, long delta)
354 /* Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG. Delta can be
355 * negative. LNLENG is updated; LNPOSN is not changed. */
359 if (!(LNLENG < from || delta == 0)) {
360 for (I=from; I<=LNLENG; I++) {
372 /* Type a blank line. This procedure is provided as a convenience for callers
373 * who otherwise have no use for MAPCOM. */
384 /* Data structure routines */
386 long VOCAB(long id, long init)
387 /* Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
388 * -1 if not found. If INIT is positive, this is an initialisation call setting
389 * up a keyword variable, and not finding it constitutes a bug. It also means
390 * that only KTAB values which taken over 1000 equal INIT may be considered.
391 * (Thus "STEPS", which is a motion verb as well as an object, may be located
392 * as an object.) And it also means the KTAB value is taken modulo 1000. */
396 for (i=1; i<=TABSIZ; i++) {
403 if (init >= 0 && KTAB[i]/1000 != init)
408 lexeme=MOD(lexeme,1000);
415 void DSTROY(long object)
416 /* Permanently eliminate "object" by moving to a non-existent location. */
421 void JUGGLE(long object)
422 /* Juggle an object by picking it up and putting it down again, the purpose
423 * being to get the object to the front of the chain of things at its loc. */
427 i=game.place[object];
428 j=game.fixed[object];
430 MOVE(object+NOBJECTS,j);
433 void MOVE(long object, long where)
434 /* Place any object anywhere by picking it up and dropping it. May
435 * already be toting, in which case the carry is a no-op. Mustn't
436 * pick up objects which are not at any loc, since carry wants to
437 * remove objects from game.atloc chains. */
441 if (object > NOBJECTS)
442 from=game.fixed[object-NOBJECTS];
444 from=game.place[object];
445 if (from > 0 && from <= 300)
450 long PUT(long object, long where, long pval)
451 /* PUT is the same as MOVE, except it returns a value used to set up the
452 * negated game.prop values for the repository objects. */
458 void CARRY(long object, long where)
459 /* Start toting an object, removing it from the list of things at its former
460 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
461 * (moving "fixed" second loc), don't change game.place or game.holdng. */
465 if (object <= NOBJECTS) {
466 if (game.place[object] == -1)
468 game.place[object]= -1;
471 if (game.atloc[where] == object) {
472 game.atloc[where]=game.link[object];
475 temp=game.atloc[where];
476 while (game.link[temp] != object) {
477 temp=game.link[temp];
479 game.link[temp]=game.link[object];
482 void DROP(long object, long where)
483 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
484 * game.holdng if the object was being toted. */
486 if (object > NOBJECTS)
487 game.fixed[object-NOBJECTS] = where;
490 if (game.place[object] == -1)
492 game.place[object] = where;
496 game.link[object] = game.atloc[where];
497 game.atloc[where] = object;
500 long ATDWRF(long where)
501 /* Return the index of first dwarf at the given location, zero if no dwarf is
502 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
503 * the pirate (6th dwarf). */
511 for (i=1; i<=NDWARVES-1; i++) {
512 if (game.dloc[i] == where)
514 if (game.dloc[i] != 0)
520 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
521 * randrange, RNDVOC, BUG) */
523 long SETBIT(long bit)
524 /* Returns 2**bit for use in constructing bit-masks. */
529 bool TSTBIT(long mask, int bit)
530 /* Returns true if the specified bit is set in the mask. */
532 return (mask & (1 << bit)) != 0;
535 void set_seed(long seedval)
536 /* Set the LCG seed */
538 lcgstate.x = (unsigned long) seedval % lcgstate.m;
541 unsigned long get_next_lcg_value(void)
542 /* Return the LCG's current value, and then iterate it. */
544 unsigned long old_x = lcgstate.x;
545 lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
549 long randrange(long range)
550 /* Return a random integer from [0, range). */
552 return range * get_next_lcg_value() / lcgstate.m;
555 long RNDVOC(long second, long force)
556 /* Searches the vocabulary ATAB for a word whose second character is
557 * char, and changes that word such that each of the other four
558 * characters is a random letter. If force is non-zero, it is used
559 * as the new word. Returns the new word. */
564 for (int i = 1; i <= 5; i++) {
565 long j = 11 + randrange(26);
572 long div = 64L * 64L * 64L;
573 for (int i = 1; i <= TABSIZ; i++) {
574 if (MOD(ATAB[i]/div, 64L) == second)
585 /* The following conditions are currently considered fatal bugs. Numbers < 20
586 * are detected while reading the database; the others occur at "run time".
587 * 0 Message line > 70 characters
588 * 1 Null line in message
589 * 2 Too many words of messages
590 * 3 Too many travel options
591 * 4 Too many vocabulary words
592 * 5 Required vocabulary word not found
593 * 6 Too many RTEXT messages
595 * 8 Location has cond bit being set twice
596 * 9 Invalid section number in database
597 * 10 Too many locations
598 * 11 Too many class or turn messages
599 * 20 Special travel (500>L>300) exceeds goto list
600 * 21 Ran off end of vocabulary table
601 * 22 Vocabulary type (N/1000) not between 0 and 3
602 * 23 Intransitive action verb exceeds goto list
603 * 24 Transitive action verb exceeds goto list
604 * 25 Conditional travel entry with no alternative
605 * 26 Location has no travel entries
606 * 27 Hint number exceeds goto list
607 * 28 Invalid month returned by date function
608 * 29 Too many parameters given to SETPRM */
611 printf("Fatal error %ld. See source code for interpretation.\n", num);
615 /* Machine dependent routines (MAPLIN, TYPE, SAVEIO) */
617 bool MAPLIN(FILE *fp)
622 /* Read a line of input, from the specified input source,
623 * translate the chars to integers in the range 0-126 and store
624 * them in the common array "INLINE". Integer values are as follows:
625 * 0 = space [ASCII CODE 40 octal, 32 decimal]
626 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
627 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
628 * 11-36 = upper-case letters
629 * 37-62 = lower-case letters
630 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
631 * 64-73 = digits, 0 through 9
632 * Remaining characters can be translated any way that is convenient;
633 * The "TYPE" routine below is used to map them back to characters when
634 * necessary. The above mappings are required so that certain special
635 * characters are known to fit in 6 bits and/or can be easily spotted.
636 * Array elements beyond the end of the line should be filled with 0,
637 * and LNLENG should be set to the index of the last character.
639 * If the data file uses a character other than space (e.g., tab) to
640 * separate numbers, that character should also translate to 0.
642 * This procedure may use the map1,map2 arrays to maintain static data for
643 * the mapping. MAP2(1) is set to 0 when the program starts
644 * and is not changed thereafter unless the routines on this page choose
647 if (!oldstyle && !isatty(1))
651 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
654 char *cp = linenoise("> ");
657 strncpy(rawbuf, cp, sizeof(rawbuf)-1);
658 linenoiseHistoryAdd(rawbuf);
659 strncat(rawbuf, "\n", sizeof(rawbuf)-1);
664 (!eof && rawbuf[0] == '#');
666 if (logfp && fp == stdin)
670 if (logfp && fp == stdin)
671 IGNORE(fputs(rawbuf, logfp));
673 IGNORE(fputs(rawbuf, stdout));
674 strcpy(INLINE+1, rawbuf);
676 for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) {
678 INLINE[i]=ascii_to_advent[val];
688 /* Type the first "LNLENG" characters stored in inline, mapping them
689 * from integers to text per the rules described above. INLINE
690 * may be changed by this routine. */
699 for (i=1; i<=LNLENG; i++) {
700 INLINE[i]=advent_to_ascii[INLINE[i]];
703 printf("%s\n", INLINE+1);
707 void DATIME(long* d, long* t)
710 gettimeofday(&tv, NULL);
711 *d = (long) tv.tv_sec;
712 *t = (long) tv.tv_usec;
715 long MOD(long n, long m)