Skool Daze Routines
30890: Make teacher give lines to closest main kid
Used by the routine at 27196.
H Character number of teacher (163-166)
30890 PUSH HL
30891 LD A,(32763) Pick up ERIC's status flags in 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, C=142
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 155=top floor
30910 CP 156 Is ERIC on the top floor?
30912 JR C,30927 Jump if so
30914 LD D,162 162=middle floor
30916 CP 163 Is ERIC on the middle floor?
30918 JR C,30927 Jump if so
30920 LD D,169 169=bottom floor
30922 JR 30927
30924 CALL 28310 Get 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 Get floor nearest the character in D
30936 LD L,C L=142, 144 or 146
30937 LD A,H Store the character number in A temporarily
30938 LD H,127 HL=32654
30940 LD (HL),E Store the character's normalised coordinates in 32654, 32656 or 32658
30941 INC L
30942 LD (HL),D
30943 INC L
30944 LD C,L Save L in C temporarily
30945 LD H,A Restore the character number (167-169) to H
30946 INC H Next character
30947 DJNZ 30933 Jump back until done
30949 POP HL Restore teacher's character number (163-166) to H
30950 PUSH HL
30951 LD L,98 Pick up the character's coordinates in DE
30953 LD E,(HL)
30954 DEC L
30955 LD D,(HL)
30956 CALL 28327 Get lower and upper limits of character's visibility range in C and B
30959 LD HL,32659 32659 holds EINSTEIN's normalised y-coordinate
Now we enter a loop to calculate the distance of each main kid from the teacher.
30962 LD A,4 There are four main kids to consider
30964 PUSH AF
30965 LD A,(HL) Pick up a main kid's normalised y-coordinate
30966 DEC HL
30967 CP D Compare it with the teacher's y-coordinate
30968 LD A,(HL) A=main kid's x-coordinate
30969 LD (HL),255 Signal: kid and teacher on different floors
30971 JR NZ,30987 Jump unless the y-coordinates match
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 teacher and kid (possibly < 0)
30982 JR NC,30986 Jump if kid is to the right of the teacher (A>=0)
30984 NEG
30986 LD (HL),A Store the absolute distance between teacher and kid
30987 DEC HL Point HL at the normalised coordinates of the next kid and jump back until all four have been considered
30988 POP AF
30989 DEC A
30990 JR NZ,30964
30992 INC HL HL=32652 (holding ERIC's distance from the teacher)
Finally, we enter a loop to determine which main kid (if any) is closest to 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
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 teacher's character number to H before returning
31009 RET
So there was at least one kid within range. Which one will get lines?
31010 DEC A A=distance between teacher and nearest main 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 (lines message: NOW DON'T DO IT AGAIN), C=172 (ERIC)
31017 CP (HL) Was 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 kid closest to the teacher
31029 CALL 30464 Give lines to the kid who was closest to the teacher
31032 POP HL Restore teacher's character number to H before returning
31033 RET