Prev: B237 Up: Map Next: B364
B24E: Handler: User Pressed "ENTER"
Used by the routine at Handler_UserInput.
Turn the finished command into word tokens.
Input
HL Current position in the command buffer
A Which contains 0D ("ENTER") at this point.
UserInput_Enter B24E LD (HL),A Write 0D to the command buffer at the current position for use as a termination character.
Force a newline to be "printed" to the screen.
B24F CALL SwitchNormalScreenOutput Call SwitchNormalScreenOutput.
B252 CALL PrintCharacter Call PrintCharacter to print the character.
Initialise the word token buffer. This buffer will store up to 0A vocabulary tokens (one per word in the command). Each token is the index of the word in the vocabulary table.
B255 LD HL,$AF2E Point HL at UserInput_FirstToken.
B258 LD A,$FF Use the the terminator key code.
B25A LD B,$0A Set a counter in B for the size of the word token buffer (0A bytes).
Fill the word token buffer with FF (the terminator value) to mark all slots as empty.
EmptyWordTokenBuffer_Loop B25C LD (HL),A Mark every word slot empty.
B25D INC HL
B25E DJNZ EmptyWordTokenBuffer_Loop
B260 LD HL,$AEFC Point HL at CommandBuffer.
B263 LD C,$0A Allow ten words in the command.
Clear the four-letter buffer to spaces so a short word cannot match the previous one's tail.
Parser_MatchInputWord B265 LD DE,$AF39 DE=FourLetterBuffer.
B268 LD B,$04 Set a counter in B for the 04 letters.
B26A LD A,$20 A=20.
ClearWordBuffer_Loop B26C LD (DE),A Write a space character (20) to the buffer.
B26D INC DE Move to the next letter.
B26E DJNZ ClearWordBuffer_Loop Decrease the counter by one and loop back to ClearWordBuffer_Loop until the counter is zero.
B270 XOR A Finish when every word slot is full.
B271 OR C
B272 JP Z,HandlerUnknownWord
B275 LD A,(HL) Read the next character of the command.
B276 CP $0D Finish the command at ENTER.
B278 JP Z,HandlerUnknownWord
B27B LD B,$04 Copy the next word into the four-byte scratch.
B27D LD DE,$AF39
UserInput_Enter_0 B280 LD A,(HL) Read the next character of this word.
B281 CP $0D End the word at ENTER.
B283 JR Z,UserInput_Enter_3
B285 CP $20 End the word at a space.
B287 JR Z,UserInput_Enter_2
B289 CP $2C End the word at a comma.
B28B JR Z,UserInput_Enter_2
B28D LD (DE),A Store that character in the word scratch.
B28E INC HL Advance through the command and the word scratch.
B28F INC DE
B290 DJNZ UserInput_Enter_0 Copy up to four letters of this word into the word scratch.
UserInput_Enter_1 B292 LD A,(HL) Read the character past a word that did not fit.
B293 CP $0D Stop skipping once ENTER is reached.
B295 JR Z,UserInput_Enter_3
B297 CP $20 Stop skipping at a space.
B299 JR Z,UserInput_Enter_2
B29B CP $2C Stop skipping at a comma.
B29D JR Z,UserInput_Enter_2
B29F INC HL Skip the rest of a word that does not fit the scratch.
B2A0 JR UserInput_Enter_1 Keep skipping to the end of a long word.
UserInput_Enter_2 B2A2 INC HL Step past the space or comma between words.
B2A3 LD A,(HL) Read the character after that space or comma.
B2A4 CP $20 Skip spaces between words.
B2A6 JR Z,UserInput_Enter_2
B2A8 CP $2C Skip commas between words.
B2AA JR Z,UserInput_Enter_2
Preserve the command pointer, word buffer and token counter while comparing.
UserInput_Enter_3 B2AC PUSH HL Stash HL, DE and BC on the stack.
B2AD PUSH DE
B2AE PUSH BC
B2AF LD HL,$AF54 Compare the scratch word with "THE ".
B2B2 LD DE,$AF39
B2B5 LD B,$04
CompareWord_Loop B2B7 LD A,(DE) Jump to UserInput_Enter_4 if the characters don't match (the word is not "The").
B2B8 CP (HL)
B2B9 JR NZ,UserInput_Enter_4
B2BB INC DE Compare the next letter of "THE".
B2BC INC HL
B2BD DJNZ CompareWord_Loop Decrease the letter counter by one and loop back to CompareWord_Loop until the counter is zero.
Restore the registers saved before the "THE" comparison.
UserInput_Enter_4 B2BF POP BC Restore BC, DE and HL from the stack.
B2C0 POP DE
B2C1 POP HL
B2C2 JR Z,Parser_MatchInputWord Jump to Parser_MatchInputWord if the words matched (this was "The", so skip it and process the next word).
B2C4 DEC C Use up one of the ten word slots for this word.
B2C5 LD ($AF45),HL Write HL to the word scratch.
B2C8 LD ($AF47),DE Write DE to the word pointer.
B2CC LD ($AF49),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.
B2D0 LD HL,($AED0) HL=*Pointer_Vocabulary.
B2D3 LD C,$00 Set the token number in C to 00.
VocabularyLookup_Loop B2D5 LD A,(HL) Jump to ProcessUnrecognisedWords if the word is the FF terminator.
B2D6 CP $FF
B2D8 JR Z,ProcessUnrecognisedWords
CompareVocabularyWord B2DA LD DE,$AF39 DE=FourLetterBuffer.
B2DD LD B,$04 Set a counter in B for the 04 letters.
CompareVocabularyWord_Loop B2DF LD A,(DE) Jump to VocabularyWordNoMatch if this letter does not match.
B2E0 CP (HL)
B2E1 JR NZ,VocabularyWordNoMatch
B2E3 INC DE Move to the next letter of the four-letter buffer.
B2E4 INC HL Move to the next vocabulary letter.
B2E5 DJNZ CompareVocabularyWord_Loop Decrease the letter counter by one and loop back to CompareVocabularyWord_Loop until the counter is zero.
The word matched. Calculate which token-buffer slot to use and store the vocabulary index as the token.
B2E7 LD HL,$AF49 Work out which token slot this word fills.
B2EA LD A,$09
B2EC SUB (HL)
B2ED LD D,$00
B2EF LD E,A Copy the slot index into E.
B2F0 LD HL,$AF2E Calculate the address of the token buffer slot: HL=UserInput_FirstToken+DE.
B2F3 ADD HL,DE
B2F4 LD (HL),C Store the vocabulary index (C) in the token buffer slot.
B2F5 LD HL,($AF45) Put back the command buffer position from the word scratch.
B2F8 LD DE,($AF47) Put back the word buffer position from the word pointer.
B2FC LD BC,($AF49) Put back the word counter from *TempStore_WordCount.
B300 JP Parser_MatchInputWord Jump to Parser_MatchInputWord.
This vocabulary word did not match. Step past the remaining letters. A comma keeps the same token for a synonym; anything else advances the token number for the next entry.
VocabularyWordNoMatch B303 LD E,B Turn the remaining letter count into a 16-bit offset in DE.
B304 LD D,$00
B306 ADD HL,DE Move HL past the unmatched letters.
B307 LD A,(HL) Fetch the next vocabulary byte into A.
B308 CP $2C Jump to VocabularyNextEntry if this byte is not a comma (2C).
B30A JR NZ,VocabularyNextEntry
B30C INC HL Move past the comma.
B30D JR CompareVocabularyWord Jump to CompareVocabularyWord to try matching the synonym.
VocabularyNextEntry B30F INC C Advance the token number for the next vocabulary entry.
B310 JR VocabularyLookup_Loop Jump to VocabularyLookup_Loop.
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 B312 LD HL,($AF45) Load the pointer for the word scratch into HL.
B315 LD DE,($AF47) Restore the word pointer.
B319 LD BC,($AF49) Restore the word count.
B31D LD A,$0A Count the words still to check.
B31F SUB C
B320 LD C,A
B321 LD HL,$AEFC Point HL at CommandBuffer.
ProcessUnrecognisedWords_Loop B324 DEC C Count off one recognised word; the unrecognised word is reached when the count runs out.
B325 JR Z,PrintUnrecognisedWord Jump to PrintUnrecognisedWord if C is equal to 0A (no unrecognised words found).
FindUnrecognisedWord_Loop B327 LD A,(HL) Read the character from *HL and store it in A.
B328 CP $20 Jump to SkipDelimiters_Loop2 if the character is an ASCII "SPACE" (20).
B32A JR Z,SkipDelimiters_Loop2
B32C CP $2C Jump to SkipDelimiters_Loop2 if the character is an ASCII comma (2C).
B32E JR Z,SkipDelimiters_Loop2
B330 INC HL Move to the next character in the command buffer.
B331 JR FindUnrecognisedWord_Loop Keep reading this unrecognised word.
SkipDelimiters_Loop2 B333 INC HL Move to the next character in the command buffer.
B334 LD A,(HL) Read the character from *HL and store it in A.
B335 CP $20 Jump to SkipDelimiters_Loop2 if the character is an ASCII "SPACE" (20).
B337 JR Z,SkipDelimiters_Loop2
B339 CP $2C Jump to SkipDelimiters_Loop2 if the character is an ASCII comma (2C).
B33B JR Z,SkipDelimiters_Loop2
B33D JR ProcessUnrecognisedWords_Loop Look for the next unrecognised word.
Print the unrecognised word(s) in quotes.
PrintUnrecognisedWord B33F PUSH HL Remember where the unrecognised word starts.
B340 LD HL,$AF6D Print "I don't know the word:-" and start a new line.
B343 CALL PrintStringAndNewline
B346 POP HL Restore the start of the unrecognised word from the stack.
B347 LD A,$22 Open a quote around the unrecognised word.
B349 CALL PrintCharacter Call PrintCharacter to print the character.
PrintUnrecognisedWord_Loop B34C LD A,(HL) Read the character from *HL and store it in A.
B34D CP $21 Jump to PrintUnrecognisedWord_End if this character is less than 21 (a control character, indicating end of word).
B34F JR C,PrintUnrecognisedWord_End
B351 INC HL Move to the next character in the command buffer.
B352 CALL PrintCharacter Call PrintCharacter to print the character.
B355 JR PrintUnrecognisedWord_Loop Print the next character of the unrecognised word.
PrintUnrecognisedWord_End B357 LD A,$22 Close the quote around the unrecognised word.
B359 CALL PrintCharacter Call PrintCharacter to print the character.
B35C LD HL,$B10B Print "." and start a new line.
B35F CALL PrintStringAndNewline
B362 JR HandlerUnknownWord_0 Read another command.
Prev: B237 Up: Map Next: B364