| Routines

28029: Check for main kids near a teacher

Used by the routine at 30102. Returns with the character number of the nearest main kid (or 0 if none of them was close enough) in A.
Input
H | Teacher's character number (200-204)
28029 | CALL 27886 | Get the lower and upper x-coordinates of the teacher's range of vision in C and B
28032 | LD L,0 | Default assumption: no kids nearby
28034 | EXX |
28035 | LD H,206 | 206=BOY WANDER
28037 | LD BC,1535 | B'=5 (main kids including ERIC), C'=255 (initial value for the distance of the nearest main kid)
28040 | EXX |
28041 | CALL 27954 | Find the first main kid within range (if any)
28044 | AND A | Set the zero flag if we've checked all the main kids
28045 | LD A,L | A=character number of the nearest kid so far, or 0 if there are none
28046 | RET Z | Return if we've finished checking all the main kids
28047 | LD A,E | A=teacher's x-coordinate
28048 | EXX |
28049 | SUB E | Subtract the x-coordinate of the kid
28050 | JR NC,28054 | Jump if the kid's to the left of teacher
28052 | NEG |
28054 | CP C | Is this kid closer to the teacher than the last?
28055 | JR NC,28062 | Jump if not
28057 | LD C,A | C'=distance from the teacher of the nearest kid found so far
28058 | LD A,H | A=character number of this kid
28059 | EXX |
28060 | LD L,A | Store the character number of the nearest kid in L
28061 | EXX |
28062 | CALL 27981 | Find the next main kid within range (if any)
28065 | JR 28044 | Jump back to compare distances