| Routines

78AA: Make a teacher give lines to the closest main kid

Used by the routine at 6A3C. Makes a teacher who has been knocked over give lines to the closest main kid (if any).
Input
H | Teacher's character number (0xA3-0xA6)
78AA | PUSH HL |
78AB | LD A,($7FFB) | Copy ERIC's status flags from 7FFB to A
78AE | LD HL,$AC62 | Pick up ERIC's coordinates in DE
78B1 | LD E,(HL)
78B2 | DEC L
78B3 | LD D,(HL)
78B4 | LD BC,$038E | B=3 (number of main kids besides ERIC), C=LSB of 7F8E
78B7 | BIT 2,A | Is ERIC jumping?
78B9 | JR Z,$78CC | Jump if not
78BB | LD A,D | A=ERIC's y-coordinate
78BC | LD D,$9B | This is the y-coordinate of the top floor
78BE | CP $9C | Is ERIC jumping on the top floor?
78C0 | JR C,$78CF | Jump if so
78C2 | LD D,$A2 | This is the y-coordinate of the middle floor
78C4 | CP $A3 | Is ERIC jumping on the middle floor?
78C6 | JR C,$78CF | Jump if so
78C8 | LD D,$A9 | This is the y-coordinate of the bottom floor
78CA | JR $78CF |
78CC | CALL $6E96 | Get the floor nearest ERIC (0x9B, 0xA2, 0xA9) in D
78CF | LD ($7F8C),DE | Store ERIC's normalised coordinates in 7F8C
Here we enter a loop that calculates BOY WANDER's, ANGELFACE's and EINSTEIN's normalised coordinates and stores them in 7F8E, 7F90 and 7F92 respectively. (If a character's coordinates are (x, y), then his normalised coordinates are (x, Y), where Y=155, 162, 169, i.e. the y-coordinate of the floor he is closest to.)
78D3 | LD H,$A7 | 0xA7=BOY WANDER
78D5 | CALL $6E90 | D=floor nearest the kid (0x9B, 0xA2, 0xA9)
78D8 | LD L,C | L=0x8E, 0x90 or 0x92
78D9 | LD A,H | Store the kid's character number in A temporarily
78DA | LD H,$7F | HL=7F8E, 7F90 or 7F92
78DC | LD (HL),E | Store the kid's normalised coordinates here
78DD | INC L
78DE | LD (HL),D
78DF | INC L | Point HL at the next normalised coordinates slot
78E0 | LD C,L | Save L in C temporarily
78E1 | LD H,A | Restore the kid's character number (0xA7-0xA9) to H
78E2 | INC H | Next kid
78E3 | DJNZ $78D5 | Jump back until the three main kids have been done
78E5 | POP HL | Restore the teacher's character number (0xA3-0xA6) to H
78E6 | PUSH HL
78E7 | LD L,$62 | Pick up the teacher's coordinates in DE
78E9 | LD E,(HL)
78EA | DEC L
78EB | LD D,(HL)
78EC | CALL $6EA7 | Get the lower and upper limits of the teacher's visibility range in C and B
Now we enter a loop to calculate the distance of each main kid from the teacher.
78EF | LD HL,$7F93 | Point HL at EINSTEIN's normalised y-coordinate
78F2 | LD A,$04 | There are four main kids to check
78F4 | PUSH AF |
78F5 | LD A,(HL) | Pick up a kid's normalised y-coordinate
78F6 | DEC HL | Point HL at the kid's x-coordinate
78F7 | CP D | Compare the normalised y-coordinate with the teacher's y-coordinate
78F8 | LD A,(HL) | A=kid's x-coordinate
78F9 | LD (HL),$FF | Default assumption: kid and teacher are on different floors
78FB | JR NZ,$790B | Jump unless the kid's normalised y-coordinate matches the teacher's y-coordinate
78FD | CP B | Jump forward if the teacher cannot see the kid
78FE | JR Z,$7902
7900 | JR NC,$790B
7902 | CP C
7903 | JR C,$790B
7905 | SUB E | A=distance between the teacher and the kid (possibly < 0)
7906 | JR NC,$790A | Jump if the kid is to the right of the teacher (A>=0)
7908 | NEG |
790A | LD (HL),A | Store the absolute distance between the teacher and the kid (if they are close to each other)
790B | DEC HL | Point HL at the normalised y-coordinate of the next main kid and jump back until all four have been checked
790C | POP AF
790D | DEC A
790E | JR NZ,$78F4
Finally, we enter a loop to determine which main kid (if any) is closest to the teacher.
7910 | INC HL | HL=7F8C (holding ERIC's distance from the teacher)
7911 | LD B,$04 | There are four main kids to consider
7913 | LD A,$FF | 0xFF was used to signal that the kid was not on the same floor as the teacher
7915 | CP (HL) | When this loop is finished, A will hold the distance between the teacher and the closest main kid (or 0xFF if none of the kids were nearby)
7916 | JR C,$7919
7918 | LD A,(HL)
7919 | INC HL
791A | INC HL
791B | DJNZ $7915
791D | INC A | Were any of the main kids close to the teacher?
791E | JR NZ,$7922 | Jump if so
7920 | POP HL | Restore the teacher's character number to H
7921 | RET |
So at least one of the main kids is within range. Which one will get lines?
7922 | DEC A | A=distance between the teacher and the nearest kid
7923 | LD L,$8C | HL=7F8C (holding ERIC's distance from the teacher)
7925 | INC D | D=teacher's y-coordinate + 1
7926 | LD BC,$08AC | B=0x08 (NOW DON'T DO IT AGAIN), C=0xAC (ERIC)
7929 | CP (HL) | Is ERIC closest to the teacher?
792A | JR Z,$7934 | Jump if so
792C | LD C,$A6 | On exit from this loop, C will hold 0xA7 (BOY WANDER), 0xA8 (ANGELFACE), or 0xA9 (EINSTEIN)
792E | INC C
792F | INC L
7930 | INC L
7931 | CP (HL)
7932 | JR NZ,$792E
7934 | LD A,C | A=character number of the kid closest to the teacher
7935 | CALL $7700 | Give lines to the kid who is closest to the teacher
7938 | POP HL | Restore the teacher's character number to H
7939 | RET |