Prev: 40227 Up: Map Next: 40337
40257: Test Amulet Proximity
Used by the routine at HandlerTribesman.
Tests whether an actor is close to any of the four amulet pieces which have not been collected yet (those with a non-zero sprite ID in the records at AmuletData). A piece counts as close if either of these is true:
  • the column of the room (bits 0-3 of the room number) matches the column of the room of the piece and the X positions are less than 16 apart
  • the row of the room (bits 4-7 of the room number) matches the row of the room of the piece and the Y positions are less than 16 apart
The room is only compared in part so the test does not require the actor and the piece to be in exactly the same room.
Input
IX Actor record
Output
A The distance to the piece which is close
F Carry flag set if an amulet piece is close
Check each of the four amulet piece records in turn.
TestAmuletProximity 40257 LD DE,11 Set the offset to the next amulet piece in DE to 11.
40260 LD HL,38910 Point HL at the first amulet piece record at AmuletData.
40263 LD B,4 Set the amulet piece counter in B to 4.
TestAmuletProximity_Loop 40265 LD A,(HL) Fetch the sprite ID of the amulet piece.
40266 INC HL Move onto the room of the amulet piece.
40267 AND A Jump to TestAmuletProximity_Next if the amulet piece is not present.
40268 JR Z,TestAmuletProximity_Next
Compare the columns of the rooms.
40270 LD A,(HL) Fetch the room of the amulet piece.
40271 AND %00001111 Keep only the column of the room (bits 0-3).
40273 LD C,A Stash the column of the amulet piece in C.
40274 LD A,(IX+1) Fetch the room of the actor.
40277 AND %00001111 Keep only the column of the room (bits 0-3).
40279 CP C Jump to TestAmuletProximity_Columns if the actor is in the same column as the amulet piece.
40280 JR Z,TestAmuletProximity_Columns
Compare the rows of the rooms.
TestAmuletProximity_Rows 40282 LD A,(HL) Fetch the room of the amulet piece.
40283 AND %11110000 Keep only the row of the room (bits 4-7).
40285 LD C,A Stash the row of the amulet piece in C.
40286 LD A,(IX+1) Fetch the room of the actor.
40289 AND %11110000 Keep only the row of the room (bits 4-7).
40291 CP C Jump to TestAmuletProximity_Rows_Same if the actor is in the same row as the amulet piece.
40292 JR Z,TestAmuletProximity_Rows_Same
Move on to the next piece.
TestAmuletProximity_Next 40294 ADD HL,DE Move onto the next amulet piece.
40295 DJNZ TestAmuletProximity_Loop Decrease the amulet piece counter by one and loop back to TestAmuletProximity_Loop until all four pieces have been checked.
40297 AND A Clear the carry flag (no amulet piece is close).
40298 RET Return.
The columns are the same so compare the X positions.
TestAmuletProximity_Columns 40299 INC HL Move onto the X position of the amulet piece.
40300 INC HL
40301 LD A,(HL) Fetch the X position of the amulet piece.
40302 DEC HL Move back onto the room of the amulet piece.
40303 DEC HL
40304 SUB (IX+3) Subtract the X position of the actor to get the horizontal distance.
40307 JP P,TestAmuletProximity_Columns_Positive Jump to TestAmuletProximity_Columns_Positive if the distance is positive.
40310 NEG Make the distance positive.
TestAmuletProximity_Columns_Positive 40312 CP 16 Jump to TestAmuletProximity_Rows if the horizontal distance is 16 or more.
40314 JR NC,TestAmuletProximity_Rows
40316 RET Return with the carry flag set (the amulet piece is close).
The rows are the same so compare the Y positions.
TestAmuletProximity_Rows_Same 40317 INC HL Move onto the Y position of the amulet piece.
40318 INC HL
40319 INC HL
40320 LD A,(HL) Fetch the Y position of the amulet piece.
40321 DEC HL Move back onto the room of the amulet piece.
40322 DEC HL
40323 DEC HL
40324 SUB (IX+4) Subtract the Y position of the actor to get the vertical distance.
40327 JP P,TestAmuletProximity_Rows_Same_Positive Jump to TestAmuletProximity_Rows_Same_Positive if the distance is positive.
40330 NEG Make the distance positive.
TestAmuletProximity_Rows_Same_Positive 40332 CP 16 Jump to TestAmuletProximity_Next if the vertical distance is 16 or more.
40334 JR NC,TestAmuletProximity_Next
40336 RET Return with the carry flag set (the amulet piece is close).
Prev: 40227 Up: Map Next: 40337