Prev: 49217 Up: Map Next: 49533
49240: Handler: User Pressed "ENTER"
Used by the routine at Handler_UserInput.
Parses and tokenises the user's command input when they press ENTER. The input is broken down into individual words, each word is looked up in the vocabulary table, and unrecognised words are reported to the player.
Input
HL Current position in the command buffer
A Which contains 13 ("ENTER") at this point.
UserInput_Enter 49240 LD (HL),A Write 13 to the command buffer at the current position for use as a termination character.
Force a newline to be "printed" to the screen.
49241 CALL SwitchNormalScreenOutput Call SwitchNormalScreenOutput.
49244 CALL PrintCharacter Call PrintCharacter.
Initialise the word token buffer. This buffer will store up to 10 vocabulary tokens (one per word in the command). Each token is the index of the word in the vocabulary table.
49247 LD HL,48486 HL=UserInput_Token_1.
49250 LD A,255 A=255.
49252 LD B,10 Set a counter in B for the size of the word token buffer (10 bytes).
Fill the word token buffer with 255 (the terminator value) to mark all slots as empty.
EmptyWordTokenBuffer_Loop 49254 LD (HL),A Write A to *HL.
49255 INC HL Increment HL by one.
49256 DJNZ EmptyWordTokenBuffer_Loop Decrease the word token buffer counter by one and loop back to EmptyWordTokenBuffer_Loop until the whole buffer is cleared.
49258 LD HL,48436 HL=CommandBuffer.
49261 LD C,10 C=10.
Begin parsing the command. Extract each word from the input, look it up in the vocabulary, and store its token. Words are separated by spaces or commas, and the word "The" is automatically ignored.
UserInputParser_Loop 49263 LD DE,48497 DE=48497.
49266 LD B,4 B=4.
Clear the word buffer by writing ASCII "SPACE" (32) 4 times.
49268 LD A,32 Load ASCII "SPACE" (32) into A.
ClearWordBuffer_Loop 49270 LD (DE),A Write the ASCII space to *DE.
49271 INC DE Increment DE by one.
49272 DJNZ ClearWordBuffer_Loop Decrease counter by one and loop back to ClearWordBuffer_Loop until counter is zero.
49274 XOR A A=0.
49275 OR C A|=C.
49276 JP Z,CheckIfEmptyInput Jump to CheckIfEmptyInput if DE is equal to C.
49279 LD A,(HL) Jump to CheckIfEmptyInput if *HL is equal to 13.
49280 CP 13
49282 JP Z,CheckIfEmptyInput
49285 LD B,4 B=4.
49287 LD DE,48497 DE=48497.
Extract the next word from the command buffer. Copy up to 4 characters (the maximum word length) into the word buffer, stopping at a space, comma, or end of input.
CopyWord_Loop 49290 LD A,(HL) Read the character from *HL and store it in A.
49291 CP 13 Jump to CheckIfThe if this character is a newline (13 end of input).
49293 JR Z,CheckIfThe
49295 CP 32 Jump to SkipDelimiters_Loop if the character is either an ASCII "SPACE" (32) or an ASCII comma (44).
49297 JR Z,SkipDelimiters_Loop
49299 CP 44
49301 JR Z,SkipDelimiters_Loop
49303 LD (DE),A Write this character to the word buffer at *DE.
49304 INC HL Move to the next character in the command buffer.
49305 INC DE Move to the next position in the word buffer.
49306 DJNZ CopyWord_Loop Decrease counter by one and loop back to CopyWord_Loop until counter is zero.
If the word was longer than 4 characters, skip over the remaining characters until a delimiter (space, comma, or end of input) is found.
SkipRemainingWord_Loop 49308 LD A,(HL) Read the character from *HL and store it in A.
49309 CP 13 Jump to CheckIfThe if this character is a newline (13).
49311 JR Z,CheckIfThe
49313 CP 32 Jump to SkipDelimiters_Loop if the character is an ASCII "SPACE" (32).
49315 JR Z,SkipDelimiters_Loop
49317 CP 44 Jump to SkipDelimiters_Loop if the character is an ASCII comma (44).
49319 JR Z,SkipDelimiters_Loop
49321 INC HL Move to the next character in the command buffer.
49322 JR SkipRemainingWord_Loop Jump to SkipRemainingWord_Loop.
Skip over any consecutive spaces or commas to find the start of the next word.
SkipDelimiters_Loop 49324 INC HL Move to the next character in the command buffer.
49325 LD A,(HL) Read the character from *HL and store it in A.
49326 CP 32 Jump to SkipDelimiters_Loop if the character is an ASCII "SPACE" (32).
49328 JR Z,SkipDelimiters_Loop
49330 CP 44 Jump to SkipDelimiters_Loop if the character is an ASCII comma (44).
49332 JR Z,SkipDelimiters_Loop
Check if the extracted word is "The". If it is, ignore it and continue with the next word (adventure games typically ignore articles like "the", "a", "an").
CheckIfThe 49334 PUSH HL Stash HL, DE and BC on the stack.
49335 PUSH DE
49336 PUSH BC
49337 LD HL,48524 HL=Messaging_The.
49340 LD DE,48497 DE=48497.
49343 LD B,4 B=4.
CompareWord_Loop 49345 LD A,(DE) Jump to AfterCompareWord if the characters don't match (the word is not "The").
49346 CP (HL)
49347 JR NZ,AfterCompareWord
49349 INC DE Move to the next character in the extracted word.
49350 INC HL Move to the next character in "The".
49351 DJNZ CompareWord_Loop Decrease counter by one and loop back to CompareWord_Loop until all 4 characters have been compared.
AfterCompareWord 49353 POP BC Restore BC, DE and HL from the stack.
49354 POP DE
49355 POP HL
49356 JR Z,UserInputParser_Loop Jump to UserInputParser_Loop if the words matched (this was "The", so skip it and process the next word).
49358 DEC C Decrease C by one.
49359 LD (48509),HL Write HL to *TempStore_TablePointer.
49362 LD (48511),DE Write DE to *TempStore_TableIndex.
49366 LD (48513),BC Write BC to *TempStore_WordCount.
Look up the word in the vocabulary table. The vocabulary contains all recognised verbs, nouns, and other words the game understands. Each entry can have synonyms separated by commas.
49370 LD HL,(48396) HL=*Pointer_Vocabulary.
49373 LD C,0 C=0.
VocabularyLookup_Loop 49375 LD A,(HL) Jump to ProcessUnrecognisedWords if *HL is equal to 255 (end of vocabulary table).
49376 CP 255
49378 JR Z,ProcessUnrecognisedWords
CompareVocabularyWord 49380 LD DE,48497 DE=48497.
49383 LD B,4 B=4.
CompareVocabularyWord_Loop 49385 LD A,(DE) Jump to VocabularyWordNoMatch if the characters don't match (this vocabulary entry doesn't match the word).
49386 CP (HL)
49387 JR NZ,VocabularyWordNoMatch
49389 INC DE Move to the next character in the extracted word.
49390 INC HL Move to the next character in the vocabulary entry.
49391 DJNZ CompareVocabularyWord_Loop Decrease counter by one and loop back to CompareVocabularyWord_Loop until all 4 characters have been compared.
Word matched in vocabulary! Calculate which slot in the token buffer to use (based on how many words have been processed so far) and store the vocabulary index as the token.
49393 LD HL,48513 HL=TempStore_WordCount.
49396 LD A,9 A=9.
49398 SUB (HL) Calculate the token buffer slot index: A-=*HL.
49399 LD D,0 D=0.
49401 LD E,A E=A.
49402 LD HL,48486 Calculate the address of the token buffer slot: HL=UserInput_Token_1+DE.
49405 ADD HL,DE
49406 LD (HL),C Store the vocabulary index (C) in the token buffer slot.
49407 LD HL,(48509) Restore the command buffer position from *TempStore_TablePointer.
49410 LD DE,(48511) Restore the word buffer position from *TempStore_TableIndex.
49414 LD BC,(48513) Restore the word counter from *TempStore_WordCount.
49418 JP UserInputParser_Loop Jump to UserInputParser_Loop.
Word didn't match this vocabulary entry. Check if there's a synonym (indicated by a comma). If found, try matching against the synonym instead.
VocabularyWordNoMatch 49421 LD E,B E=B.
49422 LD D,0 D=0.
49424 ADD HL,DE Move the vocabulary pointer past the word that didn't match: HL+=DE.
49425 LD A,(HL) Read the character at this position and store it in A.
49426 CP 44 Jump to VocabularyNextEntry if this character is not a comma (44 meaning no synonym exists).
49428 JR NZ,VocabularyNextEntry
49430 INC HL Move past the comma to the synonym.
49431 JR CompareVocabularyWord Jump to CompareVocabularyWord to try matching the synonym.
VocabularyNextEntry 49433 INC C Increment C by one.
49434 JR VocabularyLookup_Loop Jump to VocabularyLookup_Loop to check the next vocabulary entry.
All recognised words have been tokenised. Now check if there are any unrecognised words remaining in the input. If so, display an error message to the player showing which word(s) weren't understood.
ProcessUnrecognisedWords 49436 LD HL,(48509) HL=*TempStore_TablePointer.
49439 LD DE,(48511) DE=*TempStore_TableIndex.
49443 LD BC,(48513) BC=*TempStore_WordCount.
49447 LD A,10 C=10-C.
49449 SUB C
49450 LD C,A
49451 LD HL,48436 HL=CommandBuffer.
ProcessUnrecognisedWords_Loop 49454 DEC C Decrease C by one.
49455 JR Z,PrintUnrecognisedWord Jump to PrintUnrecognisedWord if C is equal to 10 (no unrecognised words found).
FindUnrecognisedWord_Loop 49457 LD A,(HL) Read the character from *HL and store it in A.
49458 CP 32 Jump to SkipDelimiters_Loop2 if the character is an ASCII "SPACE" (32).
49460 JR Z,SkipDelimiters_Loop2
49462 CP 44 Jump to SkipDelimiters_Loop2 if the character is an ASCII comma (44).
49464 JR Z,SkipDelimiters_Loop2
49466 INC HL Move to the next character in the command buffer.
49467 JR FindUnrecognisedWord_Loop Jump to FindUnrecognisedWord_Loop.
SkipDelimiters_Loop2 49469 INC HL Move to the next character in the command buffer.
49470 LD A,(HL) Read the character from *HL and store it in A.
49471 CP 32 Jump to SkipDelimiters_Loop2 if the character is an ASCII "SPACE" (32).
49473 JR Z,SkipDelimiters_Loop2
49475 CP 44 Jump to SkipDelimiters_Loop2 if the character is an ASCII comma (44).
49477 JR Z,SkipDelimiters_Loop2
49479 JR ProcessUnrecognisedWords_Loop Jump to ProcessUnrecognisedWords_Loop.
Print the unrecognised word(s) in quotes.
PrintUnrecognisedWord 49481 PUSH HL Stash HL on the stack.
Print "I don't know the word:-".
49482 LD HL,48549 HL=Messaging_IDontKnowTheWord.
49485 CALL PrintStringAndNewline Call PrintStringAndNewline.
49488 POP HL Restore HL from the stack.
49489 LD A,34 Print a double quote character: """.
49491 CALL PrintCharacter
PrintUnrecognisedWord_Loop 49494 LD A,(HL) Read the character from *HL and store it in A.
49495 CP 33 Jump to PrintUnrecognisedWord_End if this character is less than 33 (a control character, indicating end of word).
49497 JR C,PrintUnrecognisedWord_End
49499 INC HL Move to the next character in the command buffer.
49500 CALL PrintCharacter Call PrintCharacter.
49503 JR PrintUnrecognisedWord_Loop Jump to PrintUnrecognisedWord_Loop.
PrintUnrecognisedWord_End 49505 LD A,34 Print a double quote character: """.
49507 CALL PrintCharacter
Print ".".
49510 LD HL,48937 HL=Messaging_FullStop.
49513 CALL PrintStringAndNewline Call PrintStringAndNewline.
49516 JR ReturnToInputHandler Jump to ReturnToInputHandler.
Check if the input was empty (no words were tokenised). If tokens were found, return to allow the command to be processed. Otherwise, display "I Don't Understand".
CheckIfEmptyInput 49518 LD A,(48486) Return if *UserInput_Token_1 is not equal to 255 (input was not empty).
49521 CP 255
49523 RET NZ
Print "I don't understand.".
49524 LD HL,48529 HL=Messaging_IDontUnderstand.
49527 CALL PrintStringAndNewline Call PrintStringAndNewline.
ReturnToInputHandler 49530 JP Handler_UserInput Jump to Handler_UserInput to wait for the next command input.
Prev: 49217 Up: Map Next: 49533