Prev: C164 Up: Map Next: C1B9
C19C: Find Nearest Monster
Used by the routine at UpdateBall.
Finds the living monster that is nearest to the object at IX, using the sum of the differences in columns and rows as the distance.
Input
IX Object
Output
A The nearest monster from 00 to 03 (03 is ObjectTable_Monsters)
Start with the first monster and no nearest so far.
FindNearestMonster C19C LD IY,$BD5E Start with the first monster.
C1A0 LD B,$04 Set a counter for the four monsters.
C1A2 LD DE,$04FF Start with the shortest distance as FF and no monster.
Keep this monster if it is closer than the nearest so far.
FindNearestMonster_Loop C1A5 CALL GetDistanceToMonster Find the distance to this monster.
C1A8 CP E Compare it with the shortest distance so far.
C1A9 JR NC,FindNearestMonster_Next Jump to FindNearestMonster_Next if it is not shorter.
C1AB LD E,A Remember the shortest distance.
C1AC LD D,B Remember the monster.
Move on to the next monster.
FindNearestMonster_Next C1AD PUSH DE Stash the shortest distance and the monster on the stack.
C1AE LD DE,$0010 Set the size of an object to 10 bytes.
C1B1 ADD IY,DE Move on to the next monster.
C1B3 POP DE Restore the shortest distance and the monster from the stack.
C1B4 DJNZ FindNearestMonster_Loop Loop back until all four monsters have been checked.
Convert the nearest monster to a number from 00 to 03.
C1B6 LD A,D Fetch the monster and convert it to a number from 00 to 03.
C1B7 DEC A
C1B8 RET Return.
Prev: C164 Up: Map Next: C1B9