Prev: 83A7 Up: Map Next: 8464
8405: Search for a target and an instrument
Tries the objects that fit the noun phrases of the command, in two nested loops: for each candidate target (86BC) it tries each candidate instrument (8708). For every combination, a dry run of the action (84DE, which runs the action with $B5EB clear) says whether it would work. The first combination that works is kept. Counts of the candidates are kept at $8330-$8332 so that 87AD can tell whether there were none, one or several.
When only one candidate is allowed ($8344), the search stops after the first.
Output
F Z reset if no workable combination was found
8405 CALL $86BC Find the next candidate target for which the action would actually work
8408 JR NZ,$841F None found (or several already tried): go and see how many there were in total
840A LD A,($8331) Did EXACTLY one candidate target turn out to work?
840D CP $01
840F RET NZ No (none, or more than one): give up, leaving the caller to explain why
8410 LD A,($8347) Yes: A = that one candidate
8413 LD ($B5D9),A It becomes the target
8416 CALL $84AB Start searching for an instrument too
8419 CALL $84C9 Does this action even need one?
841C JR NZ,$8446 Yes: go and look for it
841E RET No: we are done, with just the target settled
841F LD HL,$8330 No workable target this time: count how many candidates fitted the words at all
8422 INC (HL)
8423 CALL $84C9 Does the action need an instrument?
8426 JR Z,$8441 No: try this same target candidate again, ignoring the instrument entirely
8428 CALL $84AB Yes: start searching for an instrument to go with THIS candidate
842B CALL $8708 Find the next candidate instrument for which it would work
842E JR Z,$8405 None: go back and try a different target instead
8430 LD A,($8344) Found one - but does the caller only want a single candidate overall (rather than checking for ambiguity)?
8433 DEC A
8434 RET Z Yes: stop here, with this pairing as the answer
8435 LD A,($B5D9) No: remember this target as one that works...
8438 LD ($8347),A
843B LD HL,$8331 ...and count it
843E INC (HL)
843F JR $8405 ...then go on looking for more targets, to see whether the choice is ambiguous
8441 CALL $84DE Try the action with just this target and no instrument
8444 JR $842E Treat the result exactly as if an instrument search had just finished
8446 CALL $8708 Look for another candidate instrument for the current target
8449 JR NZ,$8458 None left: count how many there were in total
844B LD A,($8332) Did EXACTLY one candidate instrument work?
844E CP $01
8450 RET NZ No: give up, leaving the caller to explain why
8451 LD A,($8348) Yes: A = that one candidate
8454 LD ($B5DA),A It becomes the instrument
8457 RET
8458 LD A,($B5DA) Found another workable instrument: remember it...
845B LD ($8348),A
845E LD HL,$8332 ...and count it
8461 INC (HL)
8462 JR $8446 ...then keep looking, to see whether the choice is ambiguous
Prev: 83A7 Up: Map Next: 8464