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) A=*Flag_TurnBasedEventState.
BED1 OR A Jump to Handler_TurnBasedEvents_Scenic if no turn-based events are currently active.
BED2 JR Z,Handler_TurnBasedEvents_Scenic
Eight turn-based events share the flag byte.
BED4 LD C,A Copy the turn-based event flags into C.
BED5 LD B,$08 Set a counter in B for the number of possible turn-based events (08).
Keep shifting the event flags one-by-one to find which bits are set.
Handler_TurnBasedEvents_Loop BED7 SRL C Shift the next event flag into the carry.
BED9 JR NC,Handler_TurnBasedEvents_Next Jump to Handler_TurnBasedEvents_Next if this event bit is not set.
Count down this event's timer.
BEDB LD DE,$0000 Initialise DE to 00.
BEDE LD HL,$BA37 HL=Counter_WoodNymph.
BEE1 LD A,$08 Calculate the event index (08-B) and store it in E.
BEE3 SUB B
BEE4 LD E,A
BEE5 ADD HL,DE Point HL at this event's countdown timer.
BEE6 DEC (HL) Decrease this event's countdown timer by one.
BEE7 JR NZ,Handler_TurnBasedEvents_Next Jump to Handler_TurnBasedEvents_Next if this event's countdown timer is still running.
The timer has expired, so run this event's handler.
BEE9 LD IX,($BADE) IX=*Pointer_TurnBasedEvents_Jump.
BEED SLA E Double the event index, as each entry in the table is two bytes.
BEEF ADD IX,DE Point IX at this event's entry in the table.
BEF1 LD L,(IX+$00) Read that handler's address into HL.
BEF4 LD H,(IX+$01)
BEF7 PUSH BC Stash BC on the stack.
BEF8 LD DE,$BEFD Stash BEFD on the stack so the next return resumes here.
BEFB PUSH DE
BEFC JP (HL) Jump to that handler.
BEFD POP BC Restore BC 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.
Now handle the scenic events.
Handler_TurnBasedEvents_Scenic BF00 NOP No operation.
BF01 LD HL,$BA48 HL=Table_ScenicEventLocations.
BF04 LD A,($BB00) Fetch the number of active scenic events from Count_ScenicEvents.
BF07 OR A
BF08 JR Z,Handler_TurnBasedEvents_Return Jump to Handler_TurnBasedEvents_Return if there are no active scenic events.
BF0A LD B,A Set a counter in B for the number of scenic events.
BF0B JR Handler_TurnBasedEvents_ScenicSlot Jump to Handler_TurnBasedEvents_ScenicSlot to process the first scenic event without advancing the table pointer.
Handler_TurnBasedEvents_ScenicAdvance BF0D INC HL Move on to the next scenic item.
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
Pick a room for this scenic event.
BF18 PUSH HL Stash HL and BC on the stack.
BF19 PUSH BC
BF1A LD A,($BB00) Calculate which event's group this is.
BF1D SUB B
BF1E LD E,A
Point at this event's room list.
BF1F LD IX,$BA9C IX=Table_ScenicEventRooms.
BF23 CALL GetTableEntry Call GetTableEntry to fetch this event's room group.
The frame counter picks how far to walk into the room group. FF ends the group and the walk wraps.
BF26 LD A,($5C78) Use the frame counter to pick a random step count and store it in B.
BF29 LD B,A
BF2A LD A,$FF A=the room group terminator (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 Move to the next room in the group.
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) Fetch the destination room into A.
BF3B POP BC Restore BC and HL 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