Prev: 49508 Up: Map Next: 49593
49564: 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 0 to 3 (3 is ObjectTable_Monsters)
Start with the first monster and no nearest so far.
FindNearestMonster 49564 LD IY,48478 Start with the first monster.
49568 LD B,4 Set a counter for the four monsters.
49570 LD DE,1279 Start with the shortest distance as 255 and no monster.
Keep this monster if it is closer than the nearest so far.
FindNearestMonster_Loop 49573 CALL GetDistanceToMonster Find the distance to this monster.
49576 CP E Compare it with the shortest distance so far.
49577 JR NC,FindNearestMonster_Next Jump to FindNearestMonster_Next if it is not shorter.
49579 LD E,A Remember the shortest distance.
49580 LD D,B Remember the monster.
Move on to the next monster.
FindNearestMonster_Next 49581 PUSH DE Stash the shortest distance and the monster on the stack.
49582 LD DE,16 Set the size of an object to 16 bytes.
49585 ADD IY,DE Move on to the next monster.
49587 POP DE Restore the shortest distance and the monster from the stack.
49588 DJNZ FindNearestMonster_Loop Loop back until all four monsters have been checked.
Convert the nearest monster to a number from 0 to 3.
49590 LD A,D Fetch the monster and convert it to a number from 0 to 3.
49591 DEC A
49592 RET Return.
Prev: 49508 Up: Map Next: 49593