9 #include "linenoise/linenoise.h"
11 #define PERCENT 63 /* partly hide the packed encoding */
13 /* I/O routines (SPEAK, PSPEAK, RSPEAK, SETPRM, GETIN, YES) */
15 void SPEAK(vocab_t msg)
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, nxt, neg, nparms, param, prmtyp, state;
26 nxt=labs(LINES[msg])-1;
31 for (i = msg; i <= nxt; i++) {
32 PUTTXT(LINES[i],&state,2);
37 while (LNPOSN <= LNLENG) {
38 if (INLINE[LNPOSN] != PERCENT) {
42 prmtyp = INLINE[LNPOSN+1];
43 /* A "%"; the next character determine the type of
44 * parameter: 1 (!) = suppress message completely, 29 (S) = NULL
45 * If PARAM=1, else 'S' (optional plural ending), 33 (W) = word
46 * (two 30-bit values) with trailing spaces suppressed, 22 (L) or
47 * 31 (U) = word but map to lower/upper case, 13 (C) = word in
48 * lower case with first letter capitalised, 30 (T) = text ending
49 * with a word of -1, 65-73 (1-9) = number using that many
50 * characters, 12 (B) = variable number of blanks. */
56 if (PARMS[nparms] == 1)
66 while (PARMS[nparms] > 0) {
67 if (PARMS[nparms+1] < 0)
69 PUTTXT(PARMS[nparms],&state,casemake);
77 SHFTXT(LNPOSN+2,prmtyp-2);
79 for (i=1; i<=prmtyp; i++) {
87 if (prmtyp == 33 || prmtyp == 22 || prmtyp == 31 || prmtyp == 13) {
96 PUTTXT(PARMS[nparms],&state,casemake);
97 PUTTXT(PARMS[nparms+1],&state,casemake);
98 if (prmtyp == 13 && INLINE[i] >= 37 && INLINE[i] <= 62)
105 if (prmtyp < 1 || prmtyp > 9) {
109 SHFTXT(LNPOSN+2,prmtyp-2);
111 param=labs(PARMS[nparms]);
113 if (PARMS[nparms] < 0)
115 for (i=1; i <= prmtyp; i++) {
117 INLINE[LNPOSN]=MOD(param,10)+64;
118 if (i != 1 && param == 0) {
138 void PSPEAK(vocab_t msg,int skip)
139 /* Find the skip+1st message from msg and print it. msg should be
140 * the index of the inventory message for object. (INVEN+N+1 message
141 * is game.prop=N message). */
147 for (i=0; i <=skip; i++) {
157 void RSPEAK(vocab_t i)
158 /* Print the i-th "random" message (section 6 of database). */
164 void SETPRM(long first, long p1, long p2)
165 /* Stores parameters into the PRMCOM parms array for use by speak. P1 and P2
166 * are stored into PARMS(first) and PARMS(first+1). */
168 if (first >= MAXPARMS)
176 bool GETIN(FILE *input,
177 long *pword1, long *pword1x,
178 long *pword2, long *pword2x)
179 /* Get a command from the adventurer. Snarf out the first word, pad it with
180 * blanks, and return it in WORD1. Chars 6 thru 10 are returned in WORD1X, in
181 * case we need to print out the whole word in an error message. Any number of
182 * blanks may follow the word. If a second word appears, it is returned in
183 * WORD2 (chars 6 thru 10 in WORD2X), else WORD2 is -1. */
192 *pword1=GETTXT(true,true,true);
193 if (game.blklin && *pword1 < 0)
195 *pword1x=GETTXT(false,true,true);
197 junk=GETTXT(false,true,true);
200 *pword2=GETTXT(true,true,true);
201 *pword2x=GETTXT(false,true,true);
203 junk=GETTXT(false,true,true);
206 if (GETTXT(true,true,true) <= 0)
212 long YES(FILE *input, vocab_t x, vocab_t y, vocab_t z)
213 /* Print message X, wait for yes/no answer. If yes, print Y and return true;
214 * if no, print Z and return false. */
216 token_t reply, junk1, junk2, junk3;
220 GETIN(input, &reply, &junk1, &junk2, &junk3);
221 if (reply == MAKEWD(250519) || reply == MAKEWD(25)) {
225 if (reply == MAKEWD(1415) || reply == MAKEWD(14)) {
233 /* Line-parsing routines (GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0) */
235 long GETTXT(bool skip, bool onewrd, bool upper)
236 /* Take characters from an input line and pack them into 30-bit words.
237 * Skip says to skip leading blanks. ONEWRD says stop if we come to a
238 * blank. UPPER says to map all letters to uppercase. If we reach the
239 * end of the line, the word is filled up with blanks (which encode as 0's).
240 * If we're already at end of line when TEXT is called, we return -1. */
243 static long splitting = -1;
245 if (LNPOSN != splitting)
251 if ((!skip) || INLINE[LNPOSN] != 0)
257 for (int I=1; I<=TOKLEN; I++) {
259 if (LNPOSN > LNLENG || (onewrd && INLINE[LNPOSN] == 0))
261 char current=INLINE[LNPOSN];
262 if (current < PERCENT) {
264 if (upper && current >= 37)
270 if (splitting != LNPOSN) {
276 text=text+current-PERCENT;
284 token_t MAKEWD(long letters)
285 /* Combine TOKLEN (currently 5) uppercase letters (represented by
286 * pairs of decimal digits in lettrs) to form a 30-bit value matching
287 * the one that GETTXT would return given those characters plus
288 * trailing blanks. Caution: lettrs will overflow 31 bits if
289 * 5-letter word starts with V-Z. As a kludgey workaround, you can
290 * increment a letter by 5 by adding 50 to the next pair of
293 long i = 1, word = 0;
295 for (long k=letters; k != 0; k=k/100) {
296 word=word+i*(MOD(k,50)+10);
298 if (MOD(k,100) > 50)word=word+i*5;
300 i=64L*64L*64L*64L*64L/i;
305 void PUTTXT(token_t word, long *state, long casemake)
306 /* Unpack the 30-bit value in word to obtain up to TOKLEN (currently
307 * 5) integer-encoded chars, and store them in inline starting at
308 * LNPOSN. If LNLENG>=LNPOSN, shift existing characters to the right
309 * to make room. STATE will be zero when puttxt is called with the
310 * first of a sequence of words, but is thereafter unchanged by the
311 * caller, so PUTTXT can use it to maintain state across calls.
312 * LNPOSN and LNLENG are incremented by the number of chars stored.
313 * If CASEMAKE=1, all letters are made uppercase; if -1, lowercase; if 0,
314 * as is. any other value for case is the same as 0 but also causes
315 * trailing blanks to be included (in anticipation of subsequent
316 * additional text). */
318 long alph1, alph2, byte, div, i, w;
320 alph1=13*casemake+24;
321 alph2=26*labs(casemake)+alph1;
322 if (labs(casemake) > 1)
324 /* alph1&2 define range of wrong-case chars, 11-36 or 37-62 or empty. */
327 for (i=1; i<=TOKLEN; i++)
329 if (w <= 0 && *state == 0 && labs(casemake) <= 1)
333 if (!(*state != 0 || byte != PERCENT)) {
339 if (*state < alph2 && *state >= alph1)*state=*state-26*casemake;
340 INLINE[LNPOSN]=*state;
345 #define PUTTXT(WORD,STATE,CASE) fPUTTXT(WORD,&STATE,CASE)
347 void SHFTXT(long from, long delta)
348 /* Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG. Delta can be
349 * negative. LNLENG is updated; LNPOSN is not changed. */
353 if (!(LNLENG < from || delta == 0)) {
354 for (I=from; I<=LNLENG; I++) {
366 /* Type a blank line. This procedure is provided as a convenience for callers
367 * who otherwise have no use for MAPCOM. */
378 /* Data structure routines */
380 long VOCAB(long id, long init)
381 /* Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
382 * -1 if not found. If INIT is positive, this is an initialisation call setting
383 * up a keyword variable, and not finding it constitutes a bug. It also means
384 * that only KTAB values which taken over 1000 equal INIT may be considered.
385 * (Thus "STEPS", which is a motion verb as well as an object, may be located
386 * as an object.) And it also means the KTAB value is taken modulo 1000. */
390 for (i=1; i<=TABSIZ; i++) {
397 if (init >= 0 && KTAB[i]/1000 != init)
402 lexeme=MOD(lexeme,1000);
409 void DSTROY(long object)
410 /* Permanently eliminate "object" by moving to a non-existent location. */
415 void JUGGLE(long object)
416 /* Juggle an object by picking it up and putting it down again, the purpose
417 * being to get the object to the front of the chain of things at its loc. */
421 i=game.place[object];
422 j=game.fixed[object];
424 MOVE(object+NOBJECTS,j);
427 void MOVE(long object, long where)
428 /* Place any object anywhere by picking it up and dropping it. May
429 * already be toting, in which case the carry is a no-op. Mustn't
430 * pick up objects which are not at any loc, since carry wants to
431 * remove objects from game.atloc chains. */
435 if (object > NOBJECTS)
436 from=game.fixed[object-NOBJECTS];
438 from=game.place[object];
439 if (from > 0 && from <= 300)
444 long PUT(long object, long where, long pval)
445 /* PUT is the same as MOVE, except it returns a value used to set up the
446 * negated game.prop values for the repository objects. */
452 void CARRY(long object, long where)
453 /* Start toting an object, removing it from the list of things at its former
454 * location. Incr holdng unless it was already being toted. If object>NOBJECTS
455 * (moving "fixed" second loc), don't change game.place or game.holdng. */
459 if (object <= NOBJECTS) {
460 if (game.place[object] == -1)
462 game.place[object]= -1;
465 if (game.atloc[where] == object) {
466 game.atloc[where]=game.link[object];
469 temp=game.atloc[where];
470 while (game.link[temp] != object) {
471 temp=game.link[temp];
473 game.link[temp]=game.link[object];
476 void DROP(long object, long where)
477 /* Place an object at a given loc, prefixing it onto the game.atloc list. Decr
478 * game.holdng if the object was being toted. */
480 if (object > NOBJECTS)
481 game.fixed[object-NOBJECTS] = where;
484 if (game.place[object] == -1)
486 game.place[object] = where;
490 game.link[object] = game.atloc[where];
491 game.atloc[where] = object;
494 long ATDWRF(long where)
495 /* Return the index of first dwarf at the given location, zero if no dwarf is
496 * there (or if dwarves not active yet), -1 if all dwarves are dead. Ignore
497 * the pirate (6th dwarf). */
505 for (i=1; i<=NDWARVES-1; i++) {
506 if (game.dloc[i] == where)
508 if (game.dloc[i] != 0)
514 /* Utility routines (SETBIT, TSTBIT, set_seed, get_next_lcg_value,
515 * randrange, RNDVOC, BUG) */
517 long SETBIT(long bit)
518 /* Returns 2**bit for use in constructing bit-masks. */
523 bool TSTBIT(long mask, int bit)
524 /* Returns true if the specified bit is set in the mask. */
526 return (mask & (1 << bit)) != 0;
529 void set_seed(long seedval)
530 /* Set the LCG seed */
532 lcgstate.x = (unsigned long) seedval % lcgstate.m;
535 unsigned long get_next_lcg_value(void)
536 /* Return the LCG's current value, and then iterate it. */
538 unsigned long old_x = lcgstate.x;
539 lcgstate.x = (lcgstate.a * lcgstate.x + lcgstate.c) % lcgstate.m;
543 long randrange(long range)
544 /* Return a random integer from [0, range). */
546 return range * get_next_lcg_value() / lcgstate.m;
549 long RNDVOC(long second, long force)
550 /* Searches the vocabulary ATAB for a word whose second character is
551 * char, and changes that word such that each of the other four
552 * characters is a random letter. If force is non-zero, it is used
553 * as the new word. Returns the new word. */
558 for (int i = 1; i <= 5; i++) {
559 long j = 11 + randrange(26);
566 long div = 64L * 64L * 64L;
567 for (int i = 1; i <= TABSIZ; i++) {
568 if (MOD(ATAB[i]/div, 64L) == second)
579 /* The following conditions are currently considered fatal bugs. Numbers < 20
580 * are detected while reading the database; the others occur at "run time".
581 * 0 Message line > 70 characters
582 * 1 Null line in message
583 * 2 Too many words of messages
584 * 3 Too many travel options
585 * 4 Too many vocabulary words
586 * 5 Required vocabulary word not found
587 * 6 Too many RTEXT messages
589 * 8 Location has cond bit being set twice
590 * 9 Invalid section number in database
591 * 10 Too many locations
592 * 11 Too many class or turn messages
593 * 20 Special travel (500>L>300) exceeds goto list
594 * 21 Ran off end of vocabulary table
595 * 22 Vocabulary type (N/1000) not between 0 and 3
596 * 23 Intransitive action verb exceeds goto list
597 * 24 Transitive action verb exceeds goto list
598 * 25 Conditional travel entry with no alternative
599 * 26 Location has no travel entries
600 * 27 Hint number exceeds goto list
601 * 28 Invalid month returned by date function
602 * 29 Too many parameters given to SETPRM */
605 printf("Fatal error %ld. See source code for interpretation.\n", num);
609 /* Machine dependent routines (MAPLIN, TYPE, SAVEIO) */
611 bool MAPLIN(FILE *fp)
616 /* Read a line of input, from the specified input source,
617 * translate the chars to integers in the range 0-126 and store
618 * them in the common array "INLINE". Integer values are as follows:
619 * 0 = space [ASCII CODE 40 octal, 32 decimal]
620 * 1-2 = !" [ASCII 41-42 octal, 33-34 decimal]
621 * 3-10 = '()*+,-. [ASCII 47-56 octal, 39-46 decimal]
622 * 11-36 = upper-case letters
623 * 37-62 = lower-case letters
624 * 63 = percent (%) [ASCII 45 octal, 37 decimal]
625 * 64-73 = digits, 0 through 9
626 * Remaining characters can be translated any way that is convenient;
627 * The "TYPE" routine below is used to map them back to characters when
628 * necessary. The above mappings are required so that certain special
629 * characters are known to fit in 6 bits and/or can be easily spotted.
630 * Array elements beyond the end of the line should be filled with 0,
631 * and LNLENG should be set to the index of the last character.
633 * If the data file uses a character other than space (e.g., tab) to
634 * separate numbers, that character should also translate to 0.
636 * This procedure may use the map1,map2 arrays to maintain static data for
637 * the mapping. MAP2(1) is set to 0 when the program starts
638 * and is not changed thereafter unless the routines on this page choose
641 if (!oldstyle && !isatty(1))
645 IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp));
648 char *cp = linenoise("> ");
651 strncpy(rawbuf, cp, sizeof(rawbuf)-1);
652 linenoiseHistoryAdd(rawbuf);
653 strncat(rawbuf, "\n", sizeof(rawbuf)-1);
658 (!eof && rawbuf[0] == '#');
660 if (logfp && fp == stdin)
664 if (logfp && fp == stdin)
665 IGNORE(fputs(rawbuf, logfp));
667 IGNORE(fputs(rawbuf, stdout));
668 strcpy(INLINE+1, rawbuf);
670 for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) {
672 INLINE[i]=ascii_to_advent[val];
682 /* Type the first "LNLENG" characters stored in inline, mapping them
683 * from integers to text per the rules described above. INLINE
684 * may be changed by this routine. */
693 for (i=1; i<=LNLENG; i++) {
694 INLINE[i]=advent_to_ascii[INLINE[i]+1];
697 printf("%s\n", INLINE+1);
701 void DATIME(long* d, long* t)
704 gettimeofday(&tv, NULL);
705 *d = (long) tv.tv_sec;
706 *t = (long) tv.tv_usec;
709 long MOD(long n, long m)