Prev: 7C46 Up: Map Next: 7CBE
7C82: Find nearby item
This returns the first item within range of the hero not by distance but by item order. A radius of one is used when outdoors, otherwise a radius of six is used.
Used by the routine at pick_up_item.
Output
AF Z set if item found, NZ otherwise.
HL If item was found contains pointer to the item.
Select a pick up radius based on the room.
find_nearby_item 7C82 LD C,$01 Set the pick up radius to one
7C84 LD A,($68A0) Fetch the global current room index
7C87 AND A Is it room_0_OUTDOORS?
7C88 JR Z,find_nearby_item_0 Jump if so
7C8A LD C,$06 Otherwise set the pick up radius to six
Loop for all items.
find_nearby_item_0 7C8C LD B,$10 Set B for 16 iterations (item__LIMIT)
7C8E LD HL,$76C9 Point HL at the first item_struct's room member
find_nearby_item_loop 7C91 BIT 7,(HL) Is the itemstruct_ROOM_FLAG_ITEM_NEARBY_7 flag set? ($80)
7C93 JR Z,find_nearby_next If not, jump to the next iteration
7C95 PUSH BC Save item counter
7C96 PUSH HL Save item_struct pointer
7C97 INC HL Point HL at itemstruct position
7C98 LD DE,$81B8 Point DE at global map position (hero)
7C9B LD B,$02 Do two loop iterations (once for x then y)
Range check.
find_nearby_position_loop 7C9D LD A,(DE) Read a map position byte
7C9E SUB C if (map_pos_byte (A) - pick_up_radius (C) >= itemstruct_byte || map_pos_byte + pick_up_radius < itemstruct_byte) jump to pop_next
7C9F CP (HL)
7CA0 JR NC,find_nearby_pop_next
7CA2 ADD A,C
7CA3 ADD A,C
7CA4 CP (HL)
7CA5 JR C,find_nearby_pop_next
7CA7 INC HL Move to next itemstruct byte
7CA8 INC DE Move to next map position byte
7CA9 DJNZ find_nearby_position_loop ...loop for both bytes
7CAB POP HL Restore item_struct pointer
7CAC DEC HL Compensate for overshoot. Point to itemstruct.item for return value
7CAD POP BC Restore item counter
7CAE XOR A Set Z (found)
Bug: The next instruction is written as RET Z but there's no need for it to be conditional.
7CAF RET Z Return
find_nearby_pop_next 7CB0 POP HL Restore item_struct pointer
7CB1 POP BC Restore item counter
find_nearby_next 7CB2 LD A,$07 Step HL to the next item_struct
7CB4 ADD A,L
7CB5 LD L,A
7CB6 JR NC,find_nearby_item_1
7CB8 INC H
find_nearby_item_1 7CB9 DJNZ find_nearby_item_loop ...loop for each item
7CBB OR $01 Ran out of items: set NZ (not found)
7CBD RET Return
Prev: 7C46 Up: Map Next: 7CBE