| Routines

6558: Make a teacher find ERIC

Used by the routines at 7E7B, F09B, F54A and F55F. Computes the teacher's next move in the search for ERIC.
Input
H | Teacher's character number (0xC8-0xCC)
6558 | LD L,$00 | Point HL at byte 0x00 of the character's buffer
655A | BIT 0,(HL) | Is the teacher midstride?
655C | JR Z,$656B | Jump if not
This entry point is used by the routines at 7E7B, F09B and F54A to make the teacher who his chasing ERIC finish his stride.
655E | CALL $61B4 | Update the SRB for the teacher's current animatory state and location
6561 | LD B,A | Save the animatory state in B temporarily
6562 | LD L,$0F | Byte 0x0F holds the y-coordinate adjustment to make as the teacher finishes this stride: 0 normally, 1 if descending a staircase (see later in this routine)
6564 | LD A,(HL)
6565 | ADD A,D | Make the appropriate y-coordinate adjustment
6566 | LD D,A
6567 | LD A,B | Restore the animatory state to A
6568 | JP $6400 | Make the teacher finish his stride
The teacher is not midstride. Is he close enough to ERIC to stop the chase?
656B | CALL $6E52 | Is the teacher on a staircase?
656E | JR C,$6597 | Jump if so
6570 | LD DE,($D201) | Pick up ERIC's coordinates in DE
6574 | LD L,$00 | Byte 0x00 holds the teacher's animatory state
6576 | BIT 7,(HL) | Is the teacher facing left?
6578 | LD L,$01 |
657A | JR Z,$6580 | Jump if so
657C | LD A,E | A=ERIC's x-coordinate
657D | SUB (HL) | Subtract that of the teacher
657E | JR $6582 |
6580 | LD A,(HL) | A=teacher's x-coordinate
6581 | SUB E | Subtract that of ERIC
6582 | CP $04 | Is ERIC less than 4 horizontal spaces in front of the teacher?
6584 | JR NC,$6594 | Jump if not
6586 | INC L | L=0x02
6587 | LD A,(HL) | A=teacher's y-coordinate
6588 | SUB $04 |
658A | JR C,$658F | Jump if the teacher's on the top floor
658C | CP D | Is ERIC at least 4 vertical spaces above the teacher?
658D | JR NC,$6594 | Jump if so
658F | ADD A,$07 | Set the carry flag if ERIC is at least 4 vertical spaces below the teacher
6591 | CP D
6592 | CCF | Return if ERIC is within 3 vertical spaces either side of the teacher
6593 | RET C
The teacher is not close enough to ERIC yet to stop chasing him. Now to figure out the teacher's next move.
6594 | CALL $63BE | D=ERIC's y-coordinate (or the y-coordinate of the floor he's closest to if his feet are not on the floor)
6597 | CALL $64F3 | Work out how the teacher can reach ERIC from here
659A | BIT 3,A | Is there a closed door in the way?
659C | JP NZ,$70A2 | Open it if so
There is no closed door in the way. Is the teacher on a staircase?
659F | LD BC,$0000 | Initialise the staircase indicator to 0
65A2 | CP $02 | Is the teacher descending a staircase?
65A4 | JR NZ,$65A7 | Jump if not
65A6 | INC B | B=1 (teacher is descending a staircase)
65A7 | JR NC,$65AA | Jump if the teacher is not ascending a staircase
65A9 | DEC C | C=-1 (teacher is ascending a staircase)
65AA | CP $03 | Is the teacher on a staircase?
65AC | JR C,$65BD | Jump if so
65AE | LD L,$00 | Byte 0x00 holds the teacher's animatory state
65B0 | CP $04 | Set the carry flag if the teacher should go left
65B2 | BIT 7,(HL) | Set the zero flag if the teacher is facing left
65B4 | JR C,$65BB | Jump if the teacher must go left to reach ERIC
65B6 | JR NZ,$65BD | Jump if the teacher is facing right
65B8 | JP $6430 | Turn the teacher round
65BB | JR NZ,$65B8 | Turn the teacher round if he's facing right
Now BC is still 0 if the teacher is not on a staircase. Otherwise, B=1 and C=0 if he's descending a staircase, or B=0 and C=-1 if he's ascending a staircase. The value in B is the y-coordinate adjustment to make when the teacher finishes this stride later, and the value in C is the y-coordinate adjustment to make as he goes midstride now.
65BD | LD L,$0F | Set byte 0x0F of teacher's buffer to 1 if the teacher is descending a staircase, or 0 if he's ascending a staircase or not on one
65BF | LD (HL),B
65C0 | PUSH BC |
65C1 | CALL $61B4 | Update the SRB for the teacher's current animatory state and location
65C4 | POP BC |
65C5 | BIT 0,C | Is the teacher ascending a staircase?
65C7 | JR Z,$65CA | Jump if not
65C9 | DEC D | Up a stair as the teacher goes midstride
65CA | INC A | One foot forward (midstride)
65CB | JP $6130 | Update the teacher's animatory state and location and update the SRB