Prev: 45623 Up: Map Next: 45924
45646: Handler: User Pressed "ENTER"
Used by the routine at HandlerUserInput.
Turn the finished command into word tokens.
Input
HL Current position in the command buffer
A Which contains 13 ("ENTER") at this point.
HandlerUserPressedENTER 45646 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.
45647 CALL SwitchToNormalScreenOutput Call SwitchToNormalScreenOutput.
45650 CALL PrintCharacter Call PrintCharacter to print the character.
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.
45653 LD HL,44846 Point HL at the first word token.
45656 LD A,255 Use the the terminator key code.
45658 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 45660 LD (HL),A Mark every word slot empty.
45661 INC HL
45662 DJNZ EmptyWordTokenBuffer_Loop
45664 LD HL,44796 Point HL at the command buffer.
45667 LD C,10 Allow ten words in the command.
Parser_MatchInputWord 45669 LD DE,44857 Blank the four-byte word scratch with spaces.
45672 LD B,4
45674 LD A,32
HandlerUserPressedENTER_0 45676 LD (DE),A Fill the word scratch with spaces.
45677 INC DE
45678 DJNZ HandlerUserPressedENTER_0
45680 XOR A Finish when every word slot is full.
45681 OR C
45682 JP Z,HandlerUnknownWord
45685 LD A,(HL) Read the next character of the command.
45686 CP 13 Finish the command at ENTER.
45688 JP Z,HandlerUnknownWord
45691 LD B,4 Copy the next word into the four-byte scratch.
45693 LD DE,44857
HandlerUserPressedENTER_1 45696 LD A,(HL) Read the next character of this word.
45697 CP 13 End the word at ENTER.
45699 JR Z,HandlerUserPressedENTER_4
45701 CP 32 End the word at a space.
45703 JR Z,HandlerUserPressedENTER_3
45705 CP 44 End the word at a comma.
45707 JR Z,HandlerUserPressedENTER_3
45709 LD (DE),A Store that character in the word scratch.
45710 INC HL Advance through the command and the word scratch.
45711 INC DE
45712 DJNZ HandlerUserPressedENTER_1 Copy up to four letters of this word into the word scratch.
HandlerUserPressedENTER_2 45714 LD A,(HL) Read the character past a word that did not fit.
45715 CP 13 Stop skipping once ENTER is reached.
45717 JR Z,HandlerUserPressedENTER_4
45719 CP 32 Stop skipping at a space.
45721 JR Z,HandlerUserPressedENTER_3
45723 CP 44 Stop skipping at a comma.
45725 JR Z,HandlerUserPressedENTER_3
45727 INC HL Skip the rest of a word that does not fit the scratch.
45728 JR HandlerUserPressedENTER_2 Keep skipping to the end of a long word.
HandlerUserPressedENTER_3 45730 INC HL Step past the space or comma between words.
45731 LD A,(HL) Read the character after that space or comma.
45732 CP 32 Skip spaces between words.
45734 JR Z,HandlerUserPressedENTER_3
45736 CP 44 Skip commas between words.
45738 JR Z,HandlerUserPressedENTER_3
HandlerUserPressedENTER_4 45740 PUSH HL Remember the command, the scratch and the word count.
45741 PUSH DE
45742 PUSH BC
45743 LD HL,44884 Compare the scratch word with "THE ".
45746 LD DE,44857
45749 LD B,4
CompareWord_Loop 45751 LD A,(DE) Jump to HandlerUserPressedENTER_5 if the characters don't match (the word is not "The").
45752 CP (HL)
45753 JR NZ,HandlerUserPressedENTER_5
45755 INC DE Compare the next letter of "THE".
45756 INC HL
45757 DJNZ CompareWord_Loop Keep going until all four letters have been compared.
HandlerUserPressedENTER_5 45759 POP BC Restore BC, DE, HL from the stack.
45760 POP DE
45761 POP HL
45762 JR Z,Parser_MatchInputWord Jump to Parser_MatchInputWord if the words matched (this was "The", so skip it and process the next word).
45764 DEC C Use up one of the ten word slots for this word.
45765 LD (44869),HL Write HL to the word scratch.
45768 LD (44871),DE Write DE to the word pointer.
45772 LD (44873),BC Write BC to *Workspace_Tail.
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.
45776 LD HL,(44752) Load the pointer for the vocabulary pointer into HL.
45779 LD C,0 Select the north exit.
VocabularyLookup_Loop 45781 LD A,(HL) Jump to ProcessUnrecognisedWords if *HL is equal to 255 (end of vocabulary table).
45782 CP 255
45784 JR Z,ProcessUnrecognisedWords
CompareVocabularyWord 45786 LD DE,44857 Compare this vocabulary word with the scratch word.
45789 LD B,4
CompareVocabularyWord_Loop 45791 LD A,(DE) Jump to VocabularyWordNoMatch if the characters don't match (this vocabulary entry doesn't match the word).
45792 CP (HL)
45793 JR NZ,VocabularyWordNoMatch
45795 INC DE Move to the next character in the extracted word.
45796 INC HL Move to the next character in the vocabulary entry.
45797 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.
45799 LD HL,44873 Work out which token slot this word fills.
45802 LD A,9
45804 SUB (HL)
45805 LD D,0
45807 LD E,A Use the filled slot as an offset into the token buffer.
45808 LD HL,44846 Calculate the address of the token buffer slot: HL=the first word token+DE.
45811 ADD HL,DE
45812 LD (HL),C Store the vocabulary index (C) in the token buffer slot.
45813 LD HL,(44869) Put back the command buffer position from the word scratch.
45816 LD DE,(44871) Put back the word buffer position from the word pointer.
45820 LD BC,(44873) Put back the word counter from *Workspace_Tail.
45824 JP Parser_MatchInputWord Take the next word.
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 45827 LD E,B Step past the vocabulary word that did not match.
45828 LD D,0
45830 ADD HL,DE
45831 LD A,(HL) Read the character at this position and store it in A.
45832 CP 44 Jump to VocabularyNextEntry if this character is not a comma (44, meaning no synonym exists).
45834 JR NZ,VocabularyNextEntry
45836 INC HL Move past the comma to the synonym.
45837 JR CompareVocabularyWord Jump to CompareVocabularyWord to try matching the synonym.
VocabularyNextEntry 45839 INC C Advance to the next vocabulary word.
45840 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 45842 LD HL,(44869) Load the pointer for the word scratch into HL.
45845 LD DE,(44871) Restore the word pointer.
45849 LD BC,(44873) Restore the word count.
45853 LD A,10 Count the words still to check.
45855 SUB C
45856 LD C,A
45857 LD HL,44796 Point HL at the command buffer.
ProcessUnrecognisedWords_Loop 45860 DEC C Count off one recognised word; the unrecognised word is reached when the count runs out.
45861 JR Z,PrintUnrecognisedWord Jump to PrintUnrecognisedWord if C is equal to 10 (no unrecognised words found).
FindUnrecognisedWord_Loop 45863 LD A,(HL) Read the character from *HL and store it in A.
45864 CP 32 Jump to SkipDelimiters_Loop2 if the character is an ASCII "SPACE" (32).
45866 JR Z,SkipDelimiters_Loop2
45868 CP 44 Jump to SkipDelimiters_Loop2 if the character is an ASCII comma (44).
45870 JR Z,SkipDelimiters_Loop2
45872 INC HL Move to the next character in the command buffer.
45873 JR FindUnrecognisedWord_Loop Keep reading this unrecognised word.
SkipDelimiters_Loop2 45875 INC HL Move to the next character in the command buffer.
45876 LD A,(HL) Read the character from *HL and store it in A.
45877 CP 32 Jump to SkipDelimiters_Loop2 if the character is an ASCII "SPACE" (32).
45879 JR Z,SkipDelimiters_Loop2
45881 CP 44 Jump to SkipDelimiters_Loop2 if the character is an ASCII comma (44).
45883 JR Z,SkipDelimiters_Loop2
45885 JR ProcessUnrecognisedWords_Loop Look for the next unrecognised word.
Print the unrecognised word(s) in quotes.
PrintUnrecognisedWord 45887 PUSH HL Remember where the unrecognised word starts.
45888 LD HL,44909 Print "I don't know the word:-" and start a new line.
45891 CALL PrintStringAndANewline
45894 POP HL Restore the start of the unrecognised word from the stack.
45895 LD A,34 Open a quote around the unrecognised word.
45897 CALL PrintCharacter Call PrintCharacter to print the character.
PrintUnrecognisedWord_Loop 45900 LD A,(HL) Read the character from *HL and store it in A.
45901 CP 33 Jump to PrintUnrecognisedWord_End if this character is less than 33 (a control character, indicating end of word).
45903 JR C,PrintUnrecognisedWord_End
45905 INC HL Move to the next character in the command buffer.
45906 CALL PrintCharacter Call PrintCharacter to print the character.
45909 JR PrintUnrecognisedWord_Loop Print the next character of the unrecognised word.
PrintUnrecognisedWord_End 45911 LD A,34 Close the quote around the unrecognised word.
45913 CALL PrintCharacter Call PrintCharacter to print the character.
45916 LD HL,45323 Print "." and start a new line.
45919 CALL PrintStringAndANewline
45922 JR HandlerUnknownWord_0 Read another command.
Prev: 45623 Up: Map Next: 45924