Prev: 48831 Up: Map Next: 48961
48846: 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 48846 LD A,(47670) A=*Flag_TurnBasedEventState.
48849 OR A Jump to Handler_TurnBasedEvents_Scenic if no turn-based events are currently active.
48850 JR Z,Handler_TurnBasedEvents_Scenic
Eight turn-based events share the flag byte.
48852 LD C,A Copy the turn-based event flags into C.
48853 LD B,8 Set a counter in B for the number of possible turn-based events (8).
Keep shifting the event flags one-by-one to find which bits are set.
Handler_TurnBasedEvents_Loop 48855 SRL C Shift the next event flag into the carry.
48857 JR NC,Handler_TurnBasedEvents_Next Jump to Handler_TurnBasedEvents_Next if this event bit is not set.
Count down this event's timer.
48859 LD DE,0 Initialise DE to 0.
48862 LD HL,47671 HL=Counter_WoodNymph.
48865 LD A,8 Calculate the event index (8-B) and store it in E.
48867 SUB B
48868 LD E,A
48869 ADD HL,DE Point HL at this event's countdown timer.
48870 DEC (HL) Decrease this event's countdown timer by one.
48871 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.
48873 LD IX,(47838) IX=*Pointer_TurnBasedEvents_Jump.
48877 SLA E Double the event index, as each entry in the table is two bytes.
48879 ADD IX,DE Point IX at this event's entry in the table.
48881 LD L,(IX+0) Read that handler's address into HL.
48884 LD H,(IX+1)
48887 PUSH BC Stash BC on the stack.
48888 LD DE,48893 Stash 48893 on the stack so the next return resumes here.
48891 PUSH DE
48892 JP (HL) Jump to that handler.
48893 POP BC Restore BC from the stack.
Test the next turn-based event.
Handler_TurnBasedEvents_Next 48894 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 48896 NOP No operation.
48897 LD HL,47688 HL=Table_ScenicEventLocations.
48900 LD A,(47872) Fetch the number of active scenic events from Count_ScenicEvents.
48903 OR A
48904 JR Z,Handler_TurnBasedEvents_Return Jump to Handler_TurnBasedEvents_Return if there are no active scenic events.
48906 LD B,A Set a counter in B for the number of scenic events.
48907 JR Handler_TurnBasedEvents_ScenicSlot Jump to Handler_TurnBasedEvents_ScenicSlot to process the first scenic event without advancing the table pointer.
Handler_TurnBasedEvents_ScenicAdvance 48909 INC HL Move on to the next scenic item.
Handler_TurnBasedEvents_ScenicSlot 48910 LD A,(HL) Jump to Handler_TurnBasedEvents_ScenicNext when this scenic slot is empty.
48911 OR A
48912 JR Z,Handler_TurnBasedEvents_ScenicNext
48914 LD A,(47771) Jump to Handler_TurnBasedEvents_ScenicNext when this event is already in the current room.
48917 CP (HL)
48918 JR Z,Handler_TurnBasedEvents_ScenicNext
Pick a room for this scenic event.
48920 PUSH HL Stash HL and BC on the stack.
48921 PUSH BC
48922 LD A,(47872) Calculate which event's group this is.
48925 SUB B
48926 LD E,A
Point at this event's room list.
48927 LD IX,47772 IX=Table_ScenicEventRooms.
48931 CALL GetTableEntry Call GetTableEntry to fetch this event's room group.
The frame counter picks how far to walk into the room group. 255 ends the group and the walk wraps.
48934 LD A,(23672) Use the frame counter to pick a random step count and store it in B.
48937 LD B,A
48938 LD A,255 A=the room group terminator (255).
Walk the room group. 255 restarts it, and the frame count chooses where to stop.
Handler_TurnBasedEvents_RoomWalk 48940 LD L,(IX+0) Point HL at the start of this room group.
48943 LD H,(IX+1)
48946 JR Handler_TurnBasedEvents_RoomCheck Jump to Handler_TurnBasedEvents_RoomCheck.
Handler_TurnBasedEvents_RoomStep 48948 INC HL Move to the next room in the group.
Handler_TurnBasedEvents_RoomCheck 48949 CP (HL) Jump back to Handler_TurnBasedEvents_RoomWalk when the end marker matches.
48950 JR Z,Handler_TurnBasedEvents_RoomWalk
48952 DJNZ Handler_TurnBasedEvents_RoomStep Decrease the step counter by one and loop back to Handler_TurnBasedEvents_RoomStep until the counter is zero.
48954 LD A,(HL) Fetch the destination room into A.
48955 POP BC Restore BC and HL from the stack.
48956 POP HL
48957 LD (HL),A Store that room back in this scenic slot.
Move on to the next scenic event.
Handler_TurnBasedEvents_ScenicNext 48958 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 48960 RET Return.
View the equivalent code in;
Prev: 48831 Up: Map Next: 48961