| Routines

30890: Make a teacher give lines to the closest main kid

Used by the routine at 27196. Makes a teacher who has been knocked over give lines to the closest main kid (if any).
Input
H | Teacher's character number (163-166)
30890 | PUSH HL |
30891 | LD A,(32763) | Copy ERIC's status flags from 32763 to A
30894 | LD HL,44130 | Pick up ERIC's coordinates in DE
30897 | LD E,(HL)
30898 | DEC L
30899 | LD D,(HL)
30900 | LD BC,910 | B=3 (number of main kids besides ERIC), C=LSB of 32654
30903 | BIT 2,A | Is ERIC jumping?
30905 | JR Z,30924 | Jump if not
30907 | LD A,D | A=ERIC's y-coordinate
30908 | LD D,155 | This is the y-coordinate of the top floor
30910 | CP 156 | Is ERIC jumping on the top floor?
30912 | JR C,30927 | Jump if so
30914 | LD D,162 | This is the y-coordinate of the middle floor
30916 | CP 163 | Is ERIC jumping on the middle floor?
30918 | JR C,30927 | Jump if so
30920 | LD D,169 | This is the y-coordinate of the bottom floor
30922 | JR 30927 |
30924 | CALL 28310 | Get the floor nearest ERIC (155, 162, 169) in D
30927 | LD (32652),DE | Store ERIC's normalised coordinates in 32652
Here we enter a loop that calculates BOY WANDER's, ANGELFACE's and EINSTEIN's normalised coordinates and stores them in 32654, 32656 and 32658 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.)
30931 | LD H,167 | 167=BOY WANDER
30933 | CALL 28304 | D=floor nearest the kid (155, 162, 169)
30936 | LD L,C | L=142, 144 or 146
30937 | LD A,H | Store the kid's character number in A temporarily
30938 | LD H,127 | HL=32654, 32656 or 32658
30940 | LD (HL),E | Store the kid's normalised coordinates here
30941 | INC L
30942 | LD (HL),D
30943 | INC L | Point HL at the next normalised coordinates slot
30944 | LD C,L | Save L in C temporarily
30945 | LD H,A | Restore the kid's character number (167-169) to H
30946 | INC H | Next kid
30947 | DJNZ 30933 | Jump back until the three main kids have been done
30949 | POP HL | Restore the teacher's character number (163-166) to H
30950 | PUSH HL
30951 | LD L,98 | Pick up the teacher's coordinates in DE
30953 | LD E,(HL)
30954 | DEC L
30955 | LD D,(HL)
30956 | CALL 28327 | 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.
30959 | LD HL,32659 | Point HL at EINSTEIN's normalised y-coordinate
30962 | LD A,4 | There are four main kids to check
30964 | PUSH AF |
30965 | LD A,(HL) | Pick up a kid's normalised y-coordinate
30966 | DEC HL | Point HL at the kid's x-coordinate
30967 | CP D | Compare the normalised y-coordinate with the teacher's y-coordinate
30968 | LD A,(HL) | A=kid's x-coordinate
30969 | LD (HL),255 | Default assumption: kid and teacher are on different floors
30971 | JR NZ,30987 | Jump unless the kid's normalised y-coordinate matches the teacher's y-coordinate
30973 | CP B | Jump forward if the teacher cannot see the kid
30974 | JR Z,30978
30976 | JR NC,30987
30978 | CP C
30979 | JR C,30987
30981 | SUB E | A=distance between the teacher and the kid (possibly < 0)
30982 | JR NC,30986 | Jump if the kid is to the right of the teacher (A>=0)
30984 | NEG |
30986 | LD (HL),A | Store the absolute distance between the teacher and the kid (if they are close to each other)
30987 | DEC HL | Point HL at the normalised y-coordinate of the next main kid and jump back until all four have been checked
30988 | POP AF
30989 | DEC A
30990 | JR NZ,30964
Finally, we enter a loop to determine which main kid (if any) is closest to the teacher.
30992 | INC HL | HL=32652 (holding ERIC's distance from the teacher)
30993 | LD B,4 | There are four main kids to consider
30995 | LD A,255 | 255 was used to signal that the kid was not on the same floor as the teacher
30997 | CP (HL) | When this loop is finished, A will hold the distance between the teacher and the closest main kid (or 255 if none of the kids were nearby)
30998 | JR C,31001
31000 | LD A,(HL)
31001 | INC HL
31002 | INC HL
31003 | DJNZ 30997
31005 | INC A | Were any of the main kids close to the teacher?
31006 | JR NZ,31010 | Jump if so
31008 | POP HL | Restore the teacher's character number to H
31009 | RET |
So at least one of the main kids is within range. Which one will get lines?
31010 | DEC A | A=distance between the teacher and the nearest kid
31011 | LD L,140 | HL=32652 (holding ERIC's distance from the teacher)
31013 | INC D | D=teacher's y-coordinate + 1
31014 | LD BC,2220 | B=8 (NOW DON'T DO IT AGAIN), C=172 (ERIC)
31017 | CP (HL) | Is ERIC closest to the teacher?
31018 | JR Z,31028 | Jump if so
31020 | LD C,166 | On exit from this loop, C will hold 167 (BOY WANDER), 168 (ANGELFACE), or 169 (EINSTEIN)
31022 | INC C
31023 | INC L
31024 | INC L
31025 | CP (HL)
31026 | JR NZ,31022
31028 | LD A,C | A=character number of the kid closest to the teacher
31029 | CALL 30464 | Give lines to the kid who is closest to the teacher
31032 | POP HL | Restore the teacher's character number to H
31033 | RET |