-
-L1: RSPEAK(X);
- GETIN(REPLY,JUNK1,JUNK2,JUNK3);
- if(REPLY == MAKEWD(250519) || REPLY == MAKEWD(25)) goto L10;
- if(REPLY == MAKEWD(1415) || REPLY == MAKEWD(14)) goto L20;
- RSPEAK(185);
- goto L1;
-L10: YES=true;
- RSPEAK(Y);
- return(YES);
-L20: YES=false;
- RSPEAK(Z);
- return(YES);
-}
-
-
-
-
-
-/* Line-parsing routines (GETNUM, GETTXT, MAKEWD, PUTTXT, SHFTXT, TYPE0)
- */
-
-/* The routines on this page handle all the stuff that would normally be
- * taken care of by format statements. We do it this way instead so that
- * we can handle textual data in a machine independent fashion. All the
- * machine dependent i/o stuff is on the following page. See that page
- * for a description of MAPCOM's inline array. */
-
-#define YES(X,Y,Z) fYES(X,Y,Z)
-#undef GETNUM
-long fGETNUM(long K) {
-long DIGIT, GETNUM, SIGN;
-
-/* Obtain the next integer from an input line. If K>0, we first read a
- * new input line from a file; if K<0, we read a line from the keyboard;
- * if K=0 we use a line that has already been read (and perhaps partially
- * scanned). If we're at the end of the line or encounter an illegal
- * character (not a digit, hyphen, or blank), we return 0. */
-
-
- if(K != 0)MAPLIN(K > 0);
- GETNUM=0;
-L10: if(LNPOSN > LNLENG)return(GETNUM);
- if(INLINE[LNPOSN] != 0) goto L20;
- LNPOSN=LNPOSN+1;
- goto L10;
-
-L20: SIGN=1;
- if(INLINE[LNPOSN] != 9) goto L32;
- SIGN= -1;
-L30: LNPOSN=LNPOSN+1;
-L32: if(LNPOSN > LNLENG || INLINE[LNPOSN] == 0) goto L42;
- DIGIT=INLINE[LNPOSN]-64;
- if(DIGIT < 0 || DIGIT > 9) goto L40;
- GETNUM=GETNUM*10+DIGIT;
- goto L30;
-
-L40: GETNUM=0;
-L42: GETNUM=GETNUM*SIGN;
- LNPOSN=LNPOSN+1;
- return(GETNUM);
-}
-
-
-
-#define GETNUM(K) fGETNUM(K)
-#undef GETTXT
-long fGETTXT(long SKIP,long ONEWRD, long UPPER, long HASH) {
-long CHAR, GETTXT, I; static long SPLITTING = -1;
-
-/* Take characters from an input line and pack them into 30-bit words.
- * Skip says to skip leading blanks. ONEWRD says stop if we come to a
- * blank. UPPER says to map all letters to uppercase. HASH may be used
- * as a parameter for encrypting the text if desired; however, a hash of 0
- * should result in unmodified bytes being packed. If we reach the
- * end of the line, the word is filled up with blanks (which encode as 0's).
- * If we're already at end of line when GETTXT is called, we return -1. */
-
- if(LNPOSN != SPLITTING)SPLITTING = -1;
- GETTXT= -1;
-L10: if(LNPOSN > LNLENG)return(GETTXT);
- if((!SKIP) || INLINE[LNPOSN] != 0) goto L11;
- LNPOSN=LNPOSN+1;
- goto L10;
-
-L11: GETTXT=0;
- /* 15 */ for (I=1; I<=5; I++) {
- GETTXT=GETTXT*64;
- if(LNPOSN > LNLENG || (ONEWRD && INLINE[LNPOSN] == 0)) goto L15;
- CHAR=INLINE[LNPOSN];
- if(CHAR >= 63) goto L12;
- SPLITTING = -1;
- if(UPPER && CHAR >= 37)CHAR=CHAR-26;
- GETTXT=GETTXT+CHAR;
- goto L14;
-
-L12: if(SPLITTING == LNPOSN) goto L13;
- GETTXT=GETTXT+63;
- SPLITTING = LNPOSN;
- goto L15;
-
-L13: GETTXT=GETTXT+CHAR-63;
- SPLITTING = -1;
-L14: LNPOSN=LNPOSN+1;
-L15: /*etc*/ ;
- } /* end loop */
-
- if(HASH)GETTXT=GETTXT+MOD(HASH*13579L+5432L,97531L)*12345L+HASH;
- return(GETTXT);
-}
-
-
-
-#define GETTXT(SKIP,ONEWRD,UPPER,HASH) fGETTXT(SKIP,ONEWRD,UPPER,HASH)
-#undef MAKEWD
-long fMAKEWD(long LETTRS) {
-long I, L, MAKEWD;
-
-/* Combine five uppercase letters (represented by pairs of decimal digits
- * in lettrs) to form a 30-bit value matching the one that GETTXT would
- * return given those characters plus trailing blanks and HASH=0. Caution:
- * lettrs will overflow 31 bits if 5-letter word starts with V-Z. As a
- * kludgey workaround, you can increment a letter by 5 by adding 50 to
- * the next pair of digits. */
-
-
- MAKEWD=0;
- I=1;
- L=LETTRS;
-L10: MAKEWD=MAKEWD+I*(MOD(L,50)+10);
- I=I*64;
- if(MOD(L,100) > 50)MAKEWD=MAKEWD+I*5;
- L=L/100;
- if(L != 0) goto L10;
- I=64L*64L*64L*64L*64L/I;
- MAKEWD=MAKEWD*I;
- return(MAKEWD);
-}
-
-
-
-#define MAKEWD(LETTRS) fMAKEWD(LETTRS)
-#undef PUTTXT
-#define STATE (*sTATE)
-void fPUTTXT(long WORD, long *sTATE, long CASE, long HASH) {
-long ALPH1, ALPH2, BYTE, DIV, I, W;
-
-/* Unpack the 30-bit value in word to obtain up to 5 integer-encoded chars,
- * and store them in inline starting at LNPOSN. If LNLENG>=LNPOSN, shift
- * existing characters to the right to make room. HASH must be the same
- * as it was when gettxt created the 30-bit word. STATE will be zero when
- * puttxt is called with the first of a sequence of words, but is thereafter
- * unchanged by the caller, so PUTTXT can use it to maintain state across
- * calls. LNPOSN and LNLENG are incremented by the number of chars stored.
- * If CASE=1, all letters are made uppercase; if -1, lowercase; if 0, as is.
- * any other value for case is the same as 0 but also causes trailing blanks
- * to be included (in anticipation of subsequent additional text). */
-
-
- ALPH1=13*CASE+24;
- ALPH2=26*IABS(CASE)+ALPH1;
- if(IABS(CASE) > 1)ALPH1=ALPH2;
-/* ALPH1&2 DEFINE RANGE OF WRONG-CASE CHARS, 11-36 OR 37-62 OR EMPTY. */
- DIV=64L*64L*64L*64L;
- W=WORD;
- if(HASH)W=W-MOD(HASH*13579L+5432L,97531L)*12345L-HASH;
- /* 18 */ for (I=1; I<=5; I++) {
- if(W <= 0 && STATE == 0 && IABS(CASE) <= 1)return;
- BYTE=W/DIV;
- if(STATE != 0 || BYTE != 63) goto L12;
- STATE=63;
- goto L18;
-
-L12: SHFTXT(LNPOSN,1);
- STATE=STATE+BYTE;
- if(STATE < ALPH2 && STATE >= ALPH1)STATE=STATE-26*CASE;
- INLINE[LNPOSN]=STATE;
- LNPOSN=LNPOSN+1;
- STATE=0;
-L18: W=(W-BYTE*DIV)*64;
- } /* end loop */
- return;
-}
-
-
-
-#undef STATE
-#define PUTTXT(WORD,STATE,CASE,HASH) fPUTTXT(WORD,&STATE,CASE,HASH)
-#undef SHFTXT
-void fSHFTXT(long FROM, long DELTA) {
-long I, II, JJ;
-
-/* Move INLINE(N) to INLINE(N+DELTA) for N=FROM,LNLENG. Delta can be
- * negative. LNLENG is updated; LNPOSN is not changed. */
-
-
- if(LNLENG < FROM || DELTA == 0) goto L2;
- /* 1 */ for (I=FROM; I<=LNLENG; I++) {
- II=I;
- if(DELTA > 0)II=FROM+LNLENG-I;
- JJ=II+DELTA;
-L1: INLINE[JJ]=INLINE[II];
- } /* end loop */
-L2: LNLENG=LNLENG+DELTA;
- return;
-}
-
-
-
-#define SHFTXT(FROM,DELTA) fSHFTXT(FROM,DELTA)
-#undef TYPE0
-void fTYPE0() {
-long TEMP;
-
-/* Type a blank line. This procedure is provided as a convenience for callers
- * who otherwise have no use for MAPCOM. */
-
-
- TEMP=LNLENG;
- LNLENG=0;
- TYPE();
- LNLENG=TEMP;
- return;
-}
-
-
-
-#define TYPE0() fTYPE0()
-
-
-/* Suspend/resume I/O routines (SAVWDS, SAVARR, SAVWRD) */
-
-#undef SAVWDS
-void fSAVWDS(long *W1, long *W2, long *W3, long *W4, long *W5, long *W6, long *W7) {
-
-/* Write or read 7 variables. See SAVWRD. */
-
-
- SAVWRD(0,(*W1));
- SAVWRD(0,(*W2));
- SAVWRD(0,(*W3));
- SAVWRD(0,(*W4));
- SAVWRD(0,(*W5));
- SAVWRD(0,(*W6));
- SAVWRD(0,(*W7));
- return;
-}
-
-
-#define SAVWDS(W1,W2,W3,W4,W5,W6,W7) fSAVWDS(&W1,&W2,&W3,&W4,&W5,&W6,&W7)
-#undef SAVARR
-void fSAVARR(long ARR[], long N) {
-long I;
-
-/* Write or read an array of N words. See SAVWRD. */
-
-
- /* 1 */ for (I=1; I<=N; I++) {
-L1: SAVWRD(0,ARR[I]);
- } /* end loop */
- return;
-}
-
-
-
-#define SAVARR(ARR,N) fSAVARR(ARR,N)
-#undef SAVWRD
-#define WORD (*wORD)
-void fSAVWRD(long OP, long *wORD) {
-static long BUF[250], CKSUM = 0, H1, HASH = 0, N = 0, STATE = 0;
-
-/* If OP<0, start writing a file, using word to initialise encryption; save
- * word in the file. If OP>0, start reading a file; read the file to find
- * the value with which to decrypt the rest. In either case, if a file is
- * already open, finish writing/reading it and don't start a new one. If OP=0,
- * read/write a single word. Words are buffered in case that makes for more
- * efficient disk use. We also compute a simple checksum to catch elementary
- * poking within the saved file. When we finish reading/writing the file,
- * we store zero into WORD if there's no checksum error, else nonzero. */
-
-
- if(OP != 0){long ifvar; ifvar=(STATE); switch (ifvar<0? -1 : ifvar>0? 1 :
- 0) { case -1: goto L30; case 0: goto L10; case 1: goto L30; }}
- if(STATE == 0)return;
- if(N == 250)SAVEIO(1,STATE > 0,BUF);
- N=MOD(N,250)+1;
- H1=MOD(HASH*1093L+221573L,1048576L);
- HASH=MOD(H1*1093L+221573L,1048576L);
- H1=MOD(H1,1234)*765432+MOD(HASH,123);
- N--;
- if(STATE > 0)WORD=BUF[N]+H1;
- BUF[N]=WORD-H1;
- N++;
- CKSUM=MOD(CKSUM*13+WORD,1000000000L);
- return;
-
-L10: STATE=OP;
- SAVEIO(0,STATE > 0,BUF);
- N=1;
- if(STATE > 0) goto L15;
- HASH=MOD(WORD,1048576L);
- BUF[0]=1234L*5678L-HASH;
-L13: CKSUM=BUF[0];
- return;
-
-L15: SAVEIO(1,true,BUF);
- HASH=MOD(1234L*5678L-BUF[0],1048576L);
- goto L13;
-
-L30: if(N == 250)SAVEIO(1,STATE > 0,BUF);
- N=MOD(N,250)+1;
- if(STATE > 0) goto L32;
- N--; BUF[N]=CKSUM; N++;
- SAVEIO(1,false,BUF);
-L32: N--; WORD=BUF[N]-CKSUM; N++;
- SAVEIO(-1,STATE > 0,BUF);
- STATE=0;
- return;
-}
-
-
-
-
-
-/* Data struc. routines (VOCAB, DSTROY, JUGGLE, MOVE, PUT, CARRY, DROP, ATDWRF)
- */
-
-#undef WORD
-#define SAVWRD(OP,WORD) fSAVWRD(OP,&WORD)
-#undef VOCAB
-long fVOCAB(long ID, long INIT) {
-long HASH, I, VOCAB;
-
-/* Look up ID in the vocabulary (ATAB) and return its "definition" (KTAB), or
- * -1 if not found. If INIT is positive, this is an initialisation call setting
- * up a keyword variable, and not finding it constitutes a bug. It also means
- * that only KTAB values which taken over 1000 equal INIT may be considered.
- * (Thus "STEPS", which is a motion verb as well as an object, may be located
- * as an object.) And it also means the KTAB value is taken modulo 1000. */
-
- HASH=10000;
- /* 1 */ for (I=1; I<=TABSIZ; I++) {
- if(KTAB[I] == -1) goto L2;
- HASH=HASH+7;
- if(INIT >= 0 && KTAB[I]/1000 != INIT) goto L1;
- if(ATAB[I] == ID+HASH*HASH) goto L3;
-L1: /*etc*/ ;
- } /* end loop */
- BUG(21);
-
-L2: VOCAB= -1;
- if(INIT < 0)return(VOCAB);
- BUG(5);
-
-L3: VOCAB=KTAB[I];
- if(INIT >= 0)VOCAB=MOD(VOCAB,1000);
- return(VOCAB);
-}
-
-
-
-#define VOCAB(ID,INIT) fVOCAB(ID,INIT)
-#undef DSTROY
-void fDSTROY(long OBJECT) {
-;
-
-/* Permanently eliminate "OBJECT" by moving to a non-existent location. */
-
-
- MOVE(OBJECT,0);
- return;
-}
-
-
-
-#define DSTROY(OBJECT) fDSTROY(OBJECT)
-#undef JUGGLE
-void fJUGGLE(long OBJECT) {
-long I, J;
-
+{
+ bool outcome = false;
+
+ for (;;) {
+ speak(question);
+
+ char* reply = get_input();
+ if (reply == NULL) {
+ // LCOV_EXCL_START
+ // Should be unreachable. Reply should never be NULL
+ free(reply);
+ exit(EXIT_SUCCESS);
+ // LCOV_EXCL_STOP
+ }
+
+ if (strlen(reply) == 0) {
+ free(reply);
+ rspeak(PLEASE_ANSWER);
+ continue;
+ }
+
+ char* firstword = (char*) xcalloc(strlen(reply) + 1);
+ sscanf(reply, "%s", firstword);
+
+ free(reply);
+
+ for (int i = 0; i < (int)strlen(firstword); ++i)
+ firstword[i] = tolower(firstword[i]);
+
+ int yes = strncmp("yes", firstword, sizeof("yes") - 1);
+ int y = strncmp("y", firstword, sizeof("y") - 1);
+ int no = strncmp("no", firstword, sizeof("no") - 1);
+ int n = strncmp("n", firstword, sizeof("n") - 1);
+
+ free(firstword);
+
+ if (yes == 0 ||
+ y == 0) {
+ speak(yes_response);
+ outcome = true;
+ break;
+ } else if (no == 0 ||
+ n == 0) {
+ speak(no_response);
+ outcome = false;
+ break;
+ } else
+ rspeak(PLEASE_ANSWER);
+
+ }
+
+ return (outcome);
+}
+
+/* Data structure routines */
+
+static int get_motion_vocab_id(const char* word)
+// Return the first motion number that has 'word' as one of its words.
+{
+ for (int i = 0; i < NMOTIONS; ++i) {
+ for (int j = 0; j < motions[i].words.n; ++j) {
+ if (strncasecmp(word, motions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
+ strchr(ignore, word[0]) == NULL ||
+ !settings.oldstyle))
+ return (i);
+ }
+ }
+ // If execution reaches here, we didn't find the word.
+ return (WORD_NOT_FOUND);
+}
+
+static int get_object_vocab_id(const char* word)
+// Return the first object number that has 'word' as one of its words.
+{
+ for (int i = 0; i < NOBJECTS + 1; ++i) { // FIXME: the + 1 should go when 1-indexing for objects is removed
+ for (int j = 0; j < objects[i].words.n; ++j) {
+ if (strncasecmp(word, objects[i].words.strs[j], TOKLEN) == 0)
+ return (i);
+ }
+ }
+ // If execution reaches here, we didn't find the word.
+ return (WORD_NOT_FOUND);
+}
+
+static int get_action_vocab_id(const char* word)
+// Return the first motion number that has 'word' as one of its words.
+{
+ for (int i = 0; i < NACTIONS; ++i) {
+ for (int j = 0; j < actions[i].words.n; ++j) {
+ if (strncasecmp(word, actions[i].words.strs[j], TOKLEN) == 0 && (strlen(word) > 1 ||
+ strchr(ignore, word[0]) == NULL ||
+ !settings.oldstyle))
+ return (i);
+ }
+ }
+ // If execution reaches here, we didn't find the word.
+ return (WORD_NOT_FOUND);
+}
+
+static bool is_valid_int(const char *str)
+/* Returns true if the string passed in is represents a valid integer,
+ * that could then be parsed by atoi() */
+{
+ // Handle negative number
+ if (*str == '-')
+ ++str;
+
+ // Handle empty string or just "-". Should never reach this
+ // point, because this is only used with transitive verbs.
+ if (!*str)
+ return false; // LCOV_EXCL_LINE
+
+ // Check for non-digit chars in the rest of the stirng.
+ while (*str) {
+ if (!isdigit(*str))
+ return false;
+ else
+ ++str;
+ }
+
+ return true;
+}
+
+static void get_vocab_metadata(const char* word, vocab_t* id, word_type_t* type)
+{
+ /* Check for an empty string */
+ if (strncmp(word, "", sizeof("")) == 0) {
+ *id = WORD_EMPTY;
+ *type = NO_WORD_TYPE;
+ return;
+ }
+
+ vocab_t ref_num;
+
+ ref_num = get_motion_vocab_id(word);
+ if (ref_num != WORD_NOT_FOUND) {
+ *id = ref_num;
+ *type = MOTION;
+ return;
+ }
+
+ ref_num = get_object_vocab_id(word);
+ if (ref_num != WORD_NOT_FOUND) {
+ *id = ref_num;
+ *type = OBJECT;
+ return;
+ }
+
+ ref_num = get_action_vocab_id(word);
+ if (ref_num != WORD_NOT_FOUND) {
+ *id = ref_num;
+ *type = ACTION;
+ return;
+ }
+
+ // Check for the reservoir magic word.
+ if (strcasecmp(word, game.zzword) == 0) {
+ *id = PART;
+ *type = ACTION;
+ return;
+ }
+
+ // Check words that are actually numbers.
+ if (is_valid_int(word)) {
+ *id = WORD_EMPTY;
+ *type = NUMERIC;
+ return;
+ }
+
+ *id = WORD_NOT_FOUND;
+ *type = NO_WORD_TYPE;
+ return;
+}
+
+static void tokenize(char* raw, command_t *cmd)
+{
+ /*
+ * Be caereful about modifing this. We do not want to nuke the
+ * the speech part or ID from the previous turn.
+ */
+ memset(&cmd->word[0].raw, '\0', sizeof(cmd->word[0],raw));
+ memset(&cmd->word[1].raw, '\0', sizeof(cmd->word[1].raw));
+
+ /* Bound prefix on the %s would be needed to prevent buffer
+ * overflow. but we shortstop this more simply by making each
+ * raw-input buffer as long as the entire input buffer. */
+ sscanf(raw, "%s%s", cmd->word[0].raw, cmd->word[1].raw);
+
+ /* (ESR) In oldstyle mode, simulate the uppercasing and truncating
+ * effect on raw tokens of packing them into sixbit characters, 5
+ * to a 32-bit word. This is something the FORTRAN version did
+ * becuse archaic FORTRAN had no string types. Don Wood's
+ * mechanical translation of 2.5 to C retained the packing and
+ * thus this misfeature.
+ *
+ * It's philosophically questionable whether this is the right
+ * thing to do even in oldstyle mode. On one hand, the text
+ * mangling was not authorial intent, but a result of limitations
+ * in their tools. On the other, not simulating this misbehavior
+ * goes against the goal of making oldstyle as accurate as
+ * possible an emulation of the original UI.
+ */
+ if (settings.oldstyle) {
+ cmd->word[0].raw[TOKLEN + TOKLEN] = cmd->word[1].raw[TOKLEN + TOKLEN] = '\0';
+ for (size_t i = 0; i < strlen(cmd->word[0].raw); i++)
+ cmd->word[0].raw[i] = toupper(cmd->word[0].raw[i]);
+ for (size_t i = 0; i < strlen(cmd->word[1].raw); i++)
+ cmd->word[1].raw[i] = toupper(cmd->word[1].raw[i]);
+ }
+
+ /* populate command with parsed vocabulary metadata */
+ get_vocab_metadata(cmd->word[0].raw, &(cmd->word[0].id), &(cmd->word[0].type));
+ get_vocab_metadata(cmd->word[1].raw, &(cmd->word[1].id), &(cmd->word[1].type));
+}
+
+bool get_command_input(command_t *command)
+/* Get user input on stdin, parse and map to command */
+{
+ char inputbuf[LINESIZE];
+ char* input;
+
+ for (;;) {
+ input = get_input();
+ if (input == NULL)
+ return false;
+ if (word_count(input) > 2) {
+ rspeak(TWO_WORDS);
+ free(input);
+ continue;
+ }
+ if (strcmp(input, "") != 0)
+ break;
+ free(input);
+ }
+
+ strncpy(inputbuf, input, LINESIZE - 1);
+ free(input);
+
+ tokenize(inputbuf, command);
+
+ return true;
+}
+
+void juggle(obj_t object)