Prev: BEBF Up: Map Next: BF41
BECE: Handler: Turn-Based Events
Used by the routine at GameLoop.
Count down the timed threats and, when one expires, run its handler.
First process the turn-based events (if any are active).
Handler_TurnBasedEvents BECE LD A,($BA36) Jump to Handler_TurnBasedEvents_Scenic when the event mask is zero.
BED1 OR A
BED2 JR Z,Handler_TurnBasedEvents_Scenic
BED4 LD C,A Count the eight event bits, and skip a threat whose bit is clear.
BED5 LD B,$08
Handler_TurnBasedEvents_Loop BED7 SRL C
BED9 JR NC,Handler_TurnBasedEvents_Next
BEDB LD DE,$0000 Clear the offset used while counting this threat down.
BEDE LD HL,$BA37 Count this threat's timer down. Leave it alone while time remains.
BEE1 LD A,$08
BEE3 SUB B
BEE4 LD E,A
BEE5 ADD HL,DE
BEE6 DEC (HL)
BEE7 JR NZ,Handler_TurnBasedEvents_Next
BEE9 LD IX,($BADE) Take this threat's handler.
BEED SLA E
BEEF ADD IX,DE
BEF1 LD L,(IX+$00)
BEF4 LD H,(IX+$01)
BEF7 PUSH BC Keep the bit scan safe on the stack.
BEF8 LD DE,$BEFD Push BEFD so the next return resumes here.
BEFB PUSH DE
BEFC JP (HL) Jump to that handler.
BEFD POP BC Restore the bit scan from the stack.
Test the next turn-based event.
Handler_TurnBasedEvents_Next BEFE DJNZ Handler_TurnBasedEvents_Loop Decrease the event bit counter by one and loop back to Handler_TurnBasedEvents_Loop until all eight events have been considered.
Process scenic events.
Handler_TurnBasedEvents_Scenic BF00 NOP Jump to Handler_TurnBasedEvents_Return when the scenic count is zero.
BF01 LD HL,$BA48
BF04 LD A,($BB00)
BF07 OR A
BF08 JR Z,Handler_TurnBasedEvents_Return
BF0A LD B,A Start at the first scenic entry.
BF0B JR Handler_TurnBasedEvents_ScenicSlot
Handler_TurnBasedEvents_ScenicAdvance BF0D INC HL Advance to the next scenic entry.
Handler_TurnBasedEvents_ScenicSlot BF0E LD A,(HL) Jump to Handler_TurnBasedEvents_ScenicNext when this scenic slot is empty.
BF0F OR A
BF10 JR Z,Handler_TurnBasedEvents_ScenicNext
BF12 LD A,($BA9B) Jump to Handler_TurnBasedEvents_ScenicNext when this event is already in the current room.
BF15 CP (HL)
BF16 JR Z,Handler_TurnBasedEvents_ScenicNext
BF18 PUSH HL Call GetTableEntry to fetch this event's room list.
BF19 PUSH BC
BF1A LD A,($BB00)
BF1D SUB B
BF1E LD E,A
BF1F LD IX,$BA9C
BF23 CALL GetTableEntry
BF26 LD A,($5C78) Use the frame counter as a step count and set the group terminator in A.
BF29 LD B,A
BF2A LD A,$FF
Walk the room group. FF restarts it, and the frame count chooses where to stop.
Handler_TurnBasedEvents_RoomWalk BF2C LD L,(IX+$00) Point HL at the start of this room group.
BF2F LD H,(IX+$01)
BF32 JR Handler_TurnBasedEvents_RoomCheck Jump to Handler_TurnBasedEvents_RoomCheck.
Handler_TurnBasedEvents_RoomStep BF34 INC HL Step to the next room in the list.
Handler_TurnBasedEvents_RoomCheck BF35 CP (HL) Jump back to Handler_TurnBasedEvents_RoomWalk when the end marker matches.
BF36 JR Z,Handler_TurnBasedEvents_RoomWalk
BF38 DJNZ Handler_TurnBasedEvents_RoomStep Decrease the step counter by one and loop back to Handler_TurnBasedEvents_RoomStep until the counter is zero.
BF3A LD A,(HL) Take the room the walk landed on.
BF3B POP BC Restore the scenic counter and scenic entry pointer from the stack.
BF3C POP HL
BF3D LD (HL),A Store that room back in this scenic slot.
Move on to the next scenic event.
Handler_TurnBasedEvents_ScenicNext BF3E DJNZ Handler_TurnBasedEvents_ScenicAdvance Decrease the scenic event counter by one and loop back to Handler_TurnBasedEvents_ScenicAdvance until every scenic event has been considered.
All done, now return.
Handler_TurnBasedEvents_Return BF40 RET Return.
View the equivalent code in;
Prev: BEBF Up: Map Next: BF41