| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "glk.h" |
| 13 |
|
| 14 |
#include "common.h" |
| 15 |
#include "definitions.h" |
| 16 |
#include "extracommands.h" |
| 17 |
#include "parseinput.h" |
| 18 |
#include "restorestate.h" |
| 19 |
|
| 20 |
char **InputWordStrings = NULL; |
| 21 |
int WordsInInput = 0; |
| 22 |
int WordIndex = 0; |
| 23 |
int InitialIndex = 0; |
| 24 |
int ProtagonistString = 0; |
| 25 |
|
| 26 |
DictWord *Verbs; |
| 27 |
DictWord *Nouns; |
| 28 |
DictWord *Adverbs; |
| 29 |
DictWord *Prepositions; |
| 30 |
|
| 31 |
int CurrentVerb = 0, CurrentNoun = 0, CurrentPrep = 0, CurrentPartp = 0, CurrentNoun2 = 0, CurrentAdverb = 0, LastVerb = 0, LastNoun = 0, LastPrep = 0, LastPartp = 0, LastNoun2 = 0, LastAdverb = 0; |
| 32 |
|
| 33 |
void FreeInputWords(void) |
| 34 |
{ |
| 35 |
WordIndex = 0; |
| 36 |
if (InputWordStrings != NULL) { |
| 37 |
for (int i = 0; i < WordsInInput && InputWordStrings[i] != NULL; i++) { |
| 38 |
free(InputWordStrings[i]); |
| 39 |
InputWordStrings[i] = NULL; |
| 40 |
} |
| 41 |
free(InputWordStrings); |
| 42 |
InputWordStrings = NULL; |
| 43 |
} |
| 44 |
WordsInInput = 0; |
| 45 |
} |
| 46 |
|
| 47 |
int CompareUpToHashSign(char *word1, char *word2) |
| 48 |
{ |
| 49 |
size_t len1 = strlen(word1); |
| 50 |
size_t len2 = strlen(word2); |
| 51 |
size_t longest = MAX(len1, len2); |
| 52 |
char w1[longest]; |
| 53 |
char w2[longest]; |
| 54 |
memcpy(w1, word1, len1); |
| 55 |
memcpy(w2, word2, len2); |
| 56 |
for (int i = 0; i < longest && w1[i] != '#' && w2[i] != '#'; i++) { |
| 57 |
if (w1[i] != w2[i]) |
| 58 |
return 0; |
| 59 |
} |
| 60 |
return 1; |
| 61 |
} |
| 62 |
|
| 63 |
int YesOrNo(void) |
| 64 |
{ |
| 65 |
glk_request_char_event(Bottom); |
| 66 |
|
| 67 |
event_t ev; |
| 68 |
int result = 0; |
| 69 |
const char y = tolower((unsigned char)sys[YES][0]); |
| 70 |
const char n = tolower((unsigned char)sys[NO][0]); |
| 71 |
|
| 72 |
do { |
| 73 |
glk_select(&ev); |
| 74 |
if (ev.type == evtype_CharInput) { |
| 75 |
const char reply = tolower(ev.val1); |
| 76 |
if (reply == y) { |
| 77 |
result = 1; |
| 78 |
} else if (reply == n) { |
| 79 |
result = 2; |
| 80 |
} else { |
| 81 |
Output(sys[ANSWER_YES_OR_NO]); |
| 82 |
glk_request_char_event(Bottom); |
| 83 |
} |
| 84 |
} else |
| 85 |
Updates(ev); |
| 86 |
} while (result == 0); |
| 87 |
|
| 88 |
Display(Bottom, "%c\n", (char)ev.val1); |
| 89 |
lastwasnewline = 1; |
| 90 |
return (result == 1); |
| 91 |
} |
| 92 |
|
| 93 |
static int FindNounWithHash(char *p) |
| 94 |
{ |
| 95 |
int num_matches = 0; |
| 96 |
int matches[10]; |
| 97 |
|
| 98 |
for (int i = 0; i <= GameHeader.NumItems; i++) { |
| 99 |
if (Items[i].Dictword && (Items[i].Location == MyLoc || Items[i].Location == CARRIED)) |
| 100 |
if (CompareUpToHashSign(Nouns[GetDictWord(Items[i].Dictword)].Word, p)) { |
| 101 |
matches[num_matches] = i; |
| 102 |
num_matches++; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (num_matches == 1) |
| 107 |
return Items[matches[0]].Dictword; |
| 108 |
|
| 109 |
if (num_matches) { |
| 110 |
for (int i = 0; i < num_matches; i++) { |
| 111 |
Display(Bottom, "When you say %s, do you mean %s? ", p, Items[matches[i]].Text); |
| 112 |
if (YesOrNo()) { |
| 113 |
Output("Thank-you! "); |
| 114 |
return Items[matches[i]].Dictword; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
static int ParseWord(char *p, DictWord *dict) |
| 122 |
{ |
| 123 |
for (int i = 0; dict[i].Word != NULL; i++) { |
| 124 |
if (strcmp(dict[i].Word, p) == 0) { |
| 125 |
return (dict[i].Group); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int MatchingChars(const char *synonym, const char *original) |
| 133 |
{ |
| 134 |
if (synonym[0] == 0 || original[0] == 0 || synonym == NULL || original == NULL) |
| 135 |
return 0; |
| 136 |
int i; |
| 137 |
for (i = 0; synonym[i] != 0 && original[i] != 0; i++) { |
| 138 |
if (synonym[i] != original[i]) |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
return (i == strlen(synonym) && (original[i] == 0 || original[i] == ' ' || (i > 0 && original[i - 1] == 0))); |
| 143 |
} |
| 144 |
|
| 145 |
static int IsSynonymMatch(const char *synonym, const char *original) |
| 146 |
{ |
| 147 |
char c = synonym[0]; |
| 148 |
if (c == 0) |
| 149 |
return -1; |
| 150 |
char *found = strchr(original, c); |
| 151 |
while (found) { |
| 152 |
if (found == original || *(found - 1) == ' ') { |
| 153 |
if (MatchingChars(synonym, found)) |
| 154 |
return (int)(found - original); |
| 155 |
} |
| 156 |
found = strchr(found + 1, c); |
| 157 |
} |
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
static char *StripDoubleSpaces(char *result, int *len) |
| 162 |
{ |
| 163 |
int found = 0; |
| 164 |
do { |
| 165 |
found = 0; |
| 166 |
for (int i = 0; i < *len - 1; i++) { |
| 167 |
if (isspace(result[i]) && isspace(result[i + 1])) { |
| 168 |
found = 1; |
| 169 |
char new[512]; |
| 170 |
if (i > 0) |
| 171 |
memcpy(new, result, i); |
| 172 |
int taillen = *len - i; |
| 173 |
if (taillen > 0) { |
| 174 |
memcpy(new + i, result + i + 1, taillen); |
| 175 |
} |
| 176 |
free(result); |
| 177 |
(*len)--; |
| 178 |
new[*len] = 0; |
| 179 |
result = MemAlloc(*len + 1); |
| 180 |
memcpy(result, new, *len + 1); |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
} while (found == 1); |
| 185 |
result[*len] = 0; |
| 186 |
return result; |
| 187 |
} |
| 188 |
|
| 189 |
static char *StripPunctuation(char *buf, int *len) |
| 190 |
{ |
| 191 |
for (int i = 0; i < *len; i++) { |
| 192 |
char c = buf[i]; |
| 193 |
if (c == '.' || c == ',' || c == ':' || c == ';' || c == '\'' || c == '\"' || c == '-' || c == '!') { |
| 194 |
buf[i] = ' '; |
| 195 |
} |
| 196 |
} |
| 197 |
buf[*len] = 0; |
| 198 |
char *final = MemAlloc(*len + 1); |
| 199 |
memcpy(final, buf, *len + 1); |
| 200 |
return final; |
| 201 |
} |
| 202 |
|
| 203 |
static char *ConcatStrings(const char *head, int headlen, const char *mid, int midlen, const char *tail, int taillen) |
| 204 |
{ |
| 205 |
int totallen = headlen + midlen + taillen; |
| 206 |
char *result = MemAlloc(totallen + 1); |
| 207 |
memcpy(result, head, headlen); |
| 208 |
memcpy(result + headlen, mid, midlen); |
| 209 |
memcpy(result + headlen + midlen, tail, taillen); |
| 210 |
result[totallen] = 0; |
| 211 |
if (totallen > 1) |
| 212 |
result = StripDoubleSpaces(result, &totallen); |
| 213 |
return result; |
| 214 |
} |
| 215 |
|
| 216 |
static char *ReplaceString(char *source, const char *pattern, const char *replacement, int *startpos) |
| 217 |
{ |
| 218 |
char *result = NULL; |
| 219 |
char *head = NULL, *tail = NULL; |
| 220 |
int headlength = *startpos; |
| 221 |
head = MemAlloc(headlength + 1); |
| 222 |
memcpy(head, source, headlength); |
| 223 |
head[*startpos] = 0; |
| 224 |
int tailpos = headlength + (int)strlen(pattern); |
| 225 |
int sourcelen = (int)strlen(source); |
| 226 |
int taillength = sourcelen - tailpos; |
| 227 |
if (taillength < 0 || taillength > sourcelen) { |
| 228 |
taillength = 0; |
| 229 |
tailpos = sourcelen; |
| 230 |
} |
| 231 |
tail = MemAlloc(taillength + 1); |
| 232 |
memcpy(tail, source + tailpos, taillength); |
| 233 |
tail[taillength] = 0; |
| 234 |
int midlength = (int)strlen(replacement); |
| 235 |
result = ConcatStrings(head, headlength, replacement, midlength, tail, taillength); |
| 236 |
free(head); |
| 237 |
free(tail); |
| 238 |
free(source); |
| 239 |
*startpos = headlength + midlength + taillength; |
| 240 |
return result; |
| 241 |
} |
| 242 |
|
| 243 |
static char *ReplaceSynonyms(char *buf, int *len) |
| 244 |
{ |
| 245 |
char *result = MemAlloc(*len + 1); |
| 246 |
for (int i = 0; i < *len; i++) |
| 247 |
result[i] = toupper(buf[i]); |
| 248 |
result = StripDoubleSpaces(result, len); |
| 249 |
int finallen = (int)strlen(result); |
| 250 |
for (int i = 0; Substitutions[i].SynonymString != NULL; i++) { |
| 251 |
char *syn = Substitutions[i].SynonymString; |
| 252 |
finallen = IsSynonymMatch(syn, result); |
| 253 |
while (finallen >= 0) { |
| 254 |
debug_print("Replaced string \"%s\" with \"%s\".\n", syn, Substitutions[i].ReplacementString); |
| 255 |
result = ReplaceString(result, syn, Substitutions[i].ReplacementString, &finallen); |
| 256 |
debug_print("Result: \"%s\"\n", result); |
| 257 |
finallen = IsSynonymMatch(syn, result); |
| 258 |
} |
| 259 |
} |
| 260 |
*len = (int)strlen(result); |
| 261 |
result[*len] = 0; |
| 262 |
free(buf); |
| 263 |
return result; |
| 264 |
} |
| 265 |
|
| 266 |
static char **SplitIntoWords(const char *string, int length) |
| 267 |
{ |
| 268 |
if (length < 1) { |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
int startpos[MAX_WORDS]; |
| 273 |
int wordlength[MAX_WORDS]; |
| 274 |
|
| 275 |
int words_found = 0; |
| 276 |
int word_index = 0; |
| 277 |
startpos[0] = 0; |
| 278 |
wordlength[0] = 0; |
| 279 |
int lastwasspace = 1; |
| 280 |
for (int i = 0; string[i] != 0 && i < length && word_index < MAX_WORDS; i++) { |
| 281 |
if (!isspace(string[i])) { |
| 282 |
if (lastwasspace) { |
| 283 |
|
| 284 |
startpos[words_found] = i; |
| 285 |
words_found++; |
| 286 |
wordlength[words_found] = 0; |
| 287 |
} |
| 288 |
wordlength[words_found - 1]++; |
| 289 |
lastwasspace = 0; |
| 290 |
} else { |
| 291 |
lastwasspace = 1; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (words_found == 0) { |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
|
| 299 |
wordlength[words_found]--; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
char **words = MemAlloc(words_found * sizeof(*words)); |
| 304 |
|
| 305 |
debug_print("%d words found\n", words_found); |
| 306 |
for (int i = 0; i < words_found; i++) { |
| 307 |
words[i] = (char *)MemAlloc((wordlength[i] + 1)); |
| 308 |
memcpy(words[i], string + startpos[i], wordlength[i]); |
| 309 |
words[i][wordlength[i]] = 0; |
| 310 |
debug_print("Word %d: \"%s\"\n", i, words[i]); |
| 311 |
} |
| 312 |
WordsInInput = words_found; |
| 313 |
|
| 314 |
return words; |
| 315 |
} |
| 316 |
|
| 317 |
WordToken *TokenWords = NULL; |
| 318 |
|
| 319 |
static int IntendedAsVerb(int i) |
| 320 |
{ |
| 321 |
if (i == InitialIndex || i == 0) |
| 322 |
return 1; |
| 323 |
|
| 324 |
if (TokenWords[i - 1].Type == ADVERB_TYPE && (i == InitialIndex + 1 || (i > InitialIndex + 1 && TokenWords[i - 2].Type != VERB_TYPE))) |
| 325 |
return 1; |
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
static void WordNotFoundError(int i) |
| 330 |
{ |
| 331 |
if (TokenWords[i].Type == ADVERB_TYPE) |
| 332 |
Display(Bottom, "%s do what?\n", InputWordStrings[i]); |
| 333 |
else if (IntendedAsVerb(i)) |
| 334 |
Display(Bottom, "I don't know how to %s something!\n", InputWordStrings[i]); |
| 335 |
else |
| 336 |
Display(Bottom, "I don't know what %s means.\n", InputWordStrings[i]); |
| 337 |
} |
| 338 |
|
| 339 |
static int TokenizeInputWords(void) |
| 340 |
{ |
| 341 |
if (TokenWords != NULL) |
| 342 |
free(TokenWords); |
| 343 |
int word_not_found = -1; |
| 344 |
int found_verb = 0; |
| 345 |
int verb_position = -1; |
| 346 |
|
| 347 |
TokenWords = MemAlloc(WordsInInput * sizeof(WordToken)); |
| 348 |
int result = 0; |
| 349 |
|
| 350 |
for (int i = 0; i < WordsInInput; i++) { |
| 351 |
|
| 352 |
if (i == 0) { |
| 353 |
result = ParseWord(InputWordStrings[i], Verbs); |
| 354 |
if (result > 0) { |
| 355 |
debug_print("Found verb %s at %d\n", InputWordStrings[i], i); |
| 356 |
found_verb = 1; |
| 357 |
verb_position = i; |
| 358 |
TokenWords[i].Index = result; |
| 359 |
TokenWords[i].Type = VERB_TYPE; |
| 360 |
continue; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
result = ParseWord(InputWordStrings[i], Adverbs); |
| 365 |
if (result > 1) { |
| 366 |
debug_print("Found adverb %s at %d\n", InputWordStrings[i], i); |
| 367 |
TokenWords[i].Index = result; |
| 368 |
TokenWords[i].Type = ADVERB_TYPE; |
| 369 |
continue; |
| 370 |
} |
| 371 |
|
| 372 |
result = ParseWord(InputWordStrings[i], Nouns); |
| 373 |
if (result > 0 || strcmp(InputWordStrings[i], "ANY") == 0) { |
| 374 |
debug_print("Found noun %s at %d\n", InputWordStrings[i], i); |
| 375 |
TokenWords[i].Index = result; |
| 376 |
LastNoun = result; |
| 377 |
TokenWords[i].Type = NOUN_TYPE; |
| 378 |
continue; |
| 379 |
} |
| 380 |
|
| 381 |
result = FindNounWithHash(InputWordStrings[i]); |
| 382 |
if (result > 0) { |
| 383 |
debug_print("Found noun %s at %d\n", InputWordStrings[i], i); |
| 384 |
TokenWords[i].Index = result; |
| 385 |
LastNoun = result; |
| 386 |
TokenWords[i].Type = NOUN_TYPE; |
| 387 |
continue; |
| 388 |
} |
| 389 |
|
| 390 |
result = ParseWord(InputWordStrings[i], Prepositions); |
| 391 |
if (result > 1) { |
| 392 |
if (result == 2 && !found_verb) { |
| 393 |
Output("-USE- may NOT be the first word of your command! "); |
| 394 |
WordIndex = 255; |
| 395 |
return 0; |
| 396 |
} else { |
| 397 |
debug_print("Found preposition %s at %d\n", InputWordStrings[i], i); |
| 398 |
TokenWords[i].Index = result; |
| 399 |
TokenWords[i].Type = PREPOSITION_TYPE; |
| 400 |
continue; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
if (i > 0) |
| 405 |
result = ParseWord(InputWordStrings[i], Verbs); |
| 406 |
if (result > 0) { |
| 407 |
debug_print("Found verb %s at %d\n", InputWordStrings[i], i); |
| 408 |
TokenWords[i].Index = result; |
| 409 |
TokenWords[i].Type = VERB_TYPE; |
| 410 |
found_verb = 1; |
| 411 |
verb_position = i; |
| 412 |
continue; |
| 413 |
} |
| 414 |
|
| 415 |
result = ParseWord(InputWordStrings[i], ExtraWords); |
| 416 |
if (result > 0) { |
| 417 |
debug_print("Found extra word %s at %d\n", InputWordStrings[i], i); |
| 418 |
if (result == COM_IT) { |
| 419 |
TokenWords[i].Index = LastNoun; |
| 420 |
TokenWords[i].Type = NOUN_TYPE; |
| 421 |
} else if (result == COM_WHERE) { |
| 422 |
Output("I'm only your puppet! You must figure things out for yourself! "); |
| 423 |
WordIndex = 255; |
| 424 |
return 0; |
| 425 |
} else { |
| 426 |
TokenWords[i].Index = result; |
| 427 |
TokenWords[i].Type = OTHER_TYPE; |
| 428 |
} |
| 429 |
continue; |
| 430 |
} |
| 431 |
|
| 432 |
TokenWords[i].Type = WORD_NOT_FOUND; |
| 433 |
word_not_found = i; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
if (word_not_found >= 0) { |
| 440 |
if (!found_verb || verb_position > word_not_found) |
| 441 |
WordNotFoundError(0); |
| 442 |
if (word_not_found != 0) |
| 443 |
for (int j = 1; j <= word_not_found; j++) |
| 444 |
if (TokenWords[j].Type == WORD_NOT_FOUND) |
| 445 |
WordNotFoundError(j); |
| 446 |
WordIndex = word_not_found; |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
return 1; |
| 451 |
} |
| 452 |
|
| 453 |
int IsNextParticiple(int partp, int noun2) |
| 454 |
{ |
| 455 |
if (WordIndex >= WordsInInput) { |
| 456 |
return 0; |
| 457 |
} |
| 458 |
if (partp == 1) { |
| 459 |
return 0; |
| 460 |
} |
| 461 |
if (TokenWords[WordIndex].Type == NOUN_TYPE && partp == 2 && (noun2 == 0 || TokenWords[WordIndex].Index == noun2)) { |
| 462 |
CurrentPartp = 2; |
| 463 |
CurrentNoun2 = TokenWords[WordIndex++].Index; |
| 464 |
return 1; |
| 465 |
} else if (TokenWords[WordIndex].Type == PREPOSITION_TYPE && WordIndex < WordsInInput - 1 && (partp == 0 || TokenWords[WordIndex].Index == partp)) { |
| 466 |
if (TokenWords[WordIndex + 1].Type == NOUN_TYPE && (noun2 == 0 || TokenWords[WordIndex + 1].Index == noun2)) { |
| 467 |
CurrentPartp = TokenWords[WordIndex++].Index; |
| 468 |
CurrentNoun2 = TokenWords[WordIndex++].Index; |
| 469 |
return 1; |
| 470 |
} |
| 471 |
} |
| 472 |
return 0; |
| 473 |
} |
| 474 |
|
| 475 |
static int CommandFromTokens(int verb, int noun) |
| 476 |
{ |
| 477 |
debug_print("CommandFromTokens: WordIndex: %d verb: %d\n", WordIndex, verb); |
| 478 |
|
| 479 |
if (WordIndex == 0 && verb != 0) |
| 480 |
debug_print("Bug!"); |
| 481 |
|
| 482 |
if (!WordsInInput || (WordIndex >= WordsInInput)) { |
| 483 |
FreeInputWords(); |
| 484 |
return 1; |
| 485 |
} |
| 486 |
|
| 487 |
InitialIndex = WordIndex; |
| 488 |
|
| 489 |
for (int i = WordIndex; i < WordsInInput; i++) { |
| 490 |
char str[128]; |
| 491 |
snprintf(str, sizeof str, "%s", InputWordStrings[i]); |
| 492 |
debug_print("Word %d: %s\n", i, str); |
| 493 |
} |
| 494 |
|
| 495 |
if (TokenWords[WordIndex].Type == OTHER_TYPE) { |
| 496 |
int word = TokenWords[WordIndex].Index; |
| 497 |
WordIndex++; |
| 498 |
int nextword = 0; |
| 499 |
if (WordIndex < WordsInInput) { |
| 500 |
nextword = ParseWord(InputWordStrings[WordIndex], ExtraWords); |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
if (nextword < COM_ON) { |
| 506 |
nextword = 0; |
| 507 |
}; |
| 508 |
ExtraCommandResult result = PerformExtraCommand(word, nextword); |
| 509 |
if (result != RESULT_NOT_UNDERSTOOD) { |
| 510 |
if (result == RESULT_AGAIN) |
| 511 |
return 0; |
| 512 |
SetBit(STOPTIMEBIT); |
| 513 |
WordIndex += (result == RESULT_TWO_WORDS); |
| 514 |
return 1; |
| 515 |
} else { |
| 516 |
WordIndex = InitialIndex; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
CurrentPartp = 0; |
| 521 |
CurrentPrep = 0; |
| 522 |
CurrentNoun2 = 0; |
| 523 |
CurrentAdverb = 0; |
| 524 |
|
| 525 |
if (TokenWords[WordIndex].Type == VERB_TYPE) { |
| 526 |
verb = TokenWords[WordIndex++].Index; |
| 527 |
} |
| 528 |
|
| 529 |
if (WordIndex < WordsInInput && TokenWords[WordIndex].Type == ADVERB_TYPE) { |
| 530 |
CurrentAdverb = TokenWords[WordIndex++].Index; |
| 531 |
debug_print("Found adverb \""); |
| 532 |
PrintDictWord(CurrentAdverb, Adverbs); |
| 533 |
debug_print("\" (%d)\n", CurrentAdverb); |
| 534 |
if (WordIndex < WordsInInput && verb == 0 && TokenWords[WordIndex].Type == VERB_TYPE) { |
| 535 |
verb = TokenWords[WordIndex++].Index; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
if (verb == 0) { |
| 540 |
WordNotFoundError(InitialIndex); |
| 541 |
Output("Try again please. "); |
| 542 |
StopProcessingCommand(); |
| 543 |
SetBit(STOPTIMEBIT); |
| 544 |
return 1; |
| 545 |
} |
| 546 |
|
| 547 |
debug_print("Found verb \""); |
| 548 |
PrintDictWord(verb, Verbs); |
| 549 |
debug_print("\" (%d)\n", verb); |
| 550 |
|
| 551 |
if (WordIndex >= WordsInInput) { |
| 552 |
CurrentVerb = verb; |
| 553 |
CurrentNoun = noun; |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
if (TokenWords[WordIndex].Type == NOUN_TYPE) { |
| 558 |
noun = TokenWords[WordIndex++].Index; |
| 559 |
} |
| 560 |
|
| 561 |
if (noun == 0) { |
| 562 |
if (WordIndex < WordsInInput && TokenWords[WordIndex].Type == PREPOSITION_TYPE) { |
| 563 |
CurrentPrep = TokenWords[WordIndex++].Index; |
| 564 |
if (WordIndex < WordsInInput && TokenWords[WordIndex].Type == NOUN_TYPE) { |
| 565 |
noun = TokenWords[WordIndex++].Index; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if (CurrentPrep != 0) { |
| 571 |
debug_print("Found preposition \""); |
| 572 |
PrintDictWord(CurrentPrep, Prepositions); |
| 573 |
debug_print("\" (%d)\n", CurrentPrep); |
| 574 |
} |
| 575 |
|
| 576 |
if (noun != 0) { |
| 577 |
debug_print("Found noun \""); |
| 578 |
PrintDictWord(noun, Nouns); |
| 579 |
debug_print("\" (%d)\n", noun); |
| 580 |
} |
| 581 |
|
| 582 |
if (WordIndex < WordsInInput && TokenWords[WordIndex].Type == ADVERB_TYPE) { |
| 583 |
CurrentAdverb = TokenWords[WordIndex++].Index; |
| 584 |
if (CurrentAdverb > 0) { |
| 585 |
debug_print("Found adverb \""); |
| 586 |
PrintDictWord(CurrentAdverb, Adverbs); |
| 587 |
debug_print("\" (%d)\n", CurrentAdverb); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
if (WordIndex < WordsInInput && CurrentPrep != 0) { |
| 592 |
if (TokenWords[WordIndex].Type == NOUN_TYPE) { |
| 593 |
if (noun != 0) { |
| 594 |
CurrentNoun2 = TokenWords[WordIndex++].Index; |
| 595 |
} else { |
| 596 |
noun = TokenWords[WordIndex++].Index; |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
if (WordIndex < WordsInInput && TokenWords[WordIndex].Type == PREPOSITION_TYPE) { |
| 602 |
CurrentPartp = TokenWords[WordIndex++].Index; |
| 603 |
if (WordIndex < WordsInInput && TokenWords[WordIndex].Type == NOUN_TYPE) { |
| 604 |
if (CurrentNoun2 != 0) |
| 605 |
debug_print("Found a third noun. This should never happen.\n"); |
| 606 |
CurrentNoun2 = TokenWords[WordIndex++].Index; |
| 607 |
} else { |
| 608 |
CurrentPartp = CurrentPrep; |
| 609 |
CurrentPrep = 0; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
if (CurrentPartp != 0) { |
| 614 |
debug_print("Found participle \""); |
| 615 |
PrintDictWord(CurrentPartp, Prepositions); |
| 616 |
debug_print("\" (%d)\n", CurrentPartp); |
| 617 |
} |
| 618 |
|
| 619 |
if (CurrentNoun2 != 0) { |
| 620 |
debug_print("Found second noun \""); |
| 621 |
PrintDictWord(CurrentNoun2, Nouns); |
| 622 |
debug_print("\" (%d)\n", CurrentNoun2); |
| 623 |
} else if (CurrentPartp != 0) { |
| 624 |
debug_print("Found participle but no second noun. This should never happen.\n"); |
| 625 |
} |
| 626 |
|
| 627 |
if (WordIndex <= InitialIndex) |
| 628 |
WordIndex++; |
| 629 |
|
| 630 |
debug_print("Index: %d Words in input: %d ", WordIndex, WordsInInput); |
| 631 |
|
| 632 |
if (WordIndex < WordsInInput) |
| 633 |
debug_print("There are %d unmatched words remaining. Word at index: %s\n", WordsInInput - WordIndex, InputWordStrings[WordIndex]); |
| 634 |
else |
| 635 |
debug_print("No remaining input words.\n"); |
| 636 |
|
| 637 |
CurrentVerb = verb; |
| 638 |
CurrentNoun = noun; |
| 639 |
|
| 640 |
return 0; |
| 641 |
} |
| 642 |
|
| 643 |
static void LineInput(void) |
| 644 |
{ |
| 645 |
event_t ev; |
| 646 |
char buf[512]; |
| 647 |
FreeInputWords(); |
| 648 |
|
| 649 |
do { |
| 650 |
Display(Bottom, "\n\n%s, %s", Messages[ProtagonistString], sys[WHAT_NOW]); |
| 651 |
glk_request_line_event(Bottom, buf, (glui32)511, 0); |
| 652 |
|
| 653 |
while (1) { |
| 654 |
glk_select(&ev); |
| 655 |
|
| 656 |
if (ev.type == evtype_LineInput) |
| 657 |
break; |
| 658 |
else |
| 659 |
Updates(ev); |
| 660 |
} |
| 661 |
|
| 662 |
int length = ev.val1; |
| 663 |
buf[length] = 0; |
| 664 |
|
| 665 |
char *result = StripPunctuation(buf, &length); |
| 666 |
debug_print("After stripping punctuation: \"%s\"\n", result); |
| 667 |
result = ReplaceSynonyms(result, &length); |
| 668 |
|
| 669 |
debug_print("Final result: \"%s\"\n", result); |
| 670 |
|
| 671 |
InputWordStrings = SplitIntoWords(result, length); |
| 672 |
free(result); |
| 673 |
|
| 674 |
Output("\n"); |
| 675 |
|
| 676 |
if (WordsInInput > 0 && InputWordStrings) { |
| 677 |
return; |
| 678 |
} |
| 679 |
|
| 680 |
WordsInInput = 0; |
| 681 |
Output(sys[HUH]); |
| 682 |
|
| 683 |
} while (WordsInInput == 0 || InputWordStrings == NULL); |
| 684 |
} |
| 685 |
|
| 686 |
static int RemainderContainsVerb(void) |
| 687 |
{ |
| 688 |
debug_print("RemainderContainsVerb: WordIndex: %d WordsInInput: %d\n", WordIndex, WordsInInput); |
| 689 |
if (WordIndex < WordsInInput - 1) { |
| 690 |
for (int i = WordIndex; i < WordsInInput; i++) |
| 691 |
if (TokenWords[i].Type == VERB_TYPE) |
| 692 |
return 1; |
| 693 |
} |
| 694 |
return 0; |
| 695 |
} |
| 696 |
|
| 697 |
void StopProcessingCommand(void) |
| 698 |
{ |
| 699 |
if (RemainderContainsVerb()) { |
| 700 |
Output("\nThe rest of your input was ignored. "); |
| 701 |
} |
| 702 |
FreeInputWords(); |
| 703 |
WordIndex = 0; |
| 704 |
SetBit(STOPTIMEBIT); |
| 705 |
} |
| 706 |
|
| 707 |
int GetInput(void) |
| 708 |
{ |
| 709 |
int result = 0; |
| 710 |
|
| 711 |
|
| 712 |
if (WordIndex >= WordsInInput || WordsInInput < 2) { |
| 713 |
LineInput(); |
| 714 |
|
| 715 |
if (WordsInInput == 0 || InputWordStrings == NULL) |
| 716 |
return 1; |
| 717 |
|
| 718 |
lastwasnewline = 1; |
| 719 |
|
| 720 |
if (!TokenizeInputWords()) { |
| 721 |
if (WordIndex < 255) |
| 722 |
Output("Try again please. "); |
| 723 |
SetBit(STOPTIMEBIT); |
| 724 |
StopProcessingCommand(); |
| 725 |
return 1; |
| 726 |
} |
| 727 |
|
| 728 |
result = CommandFromTokens(0, 0); |
| 729 |
} else { |
| 730 |
result = CommandFromTokens(CurrentVerb, 0); |
| 731 |
} |
| 732 |
|
| 733 |
return result; |
| 734 |
} |