Prev: 41589 Up: Map Next: 41733
41600: Handler: Process Game Events
Used by the routine at GameStart.
Counts down the turn-based events in Flags_TurnEvents and, when a counter reaches zero, runs the handler from JumpTable_TurnEvents.
Then relocates any scenic event that is not already in the current room, picking the new room from the group at Table_ScenicEventRoomGroups.
First process the turn-based events (if any are active).
Handler_GameEvents 41600 LD A,(41160) A=*Flags_TurnEvents.
41603 OR A Jump to ScenicEvents_Start if no turn-based events are currently active.
41604 JR Z,ScenicEvents_Start
Eight turn-based events share the flag byte.
41606 LD C,A Copy the turn-based event flags into C.
41607 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.
TurnEvent_NextBit 41609 SRL C Shift the next event flag into the carry.
41611 JR NC,TurnEvent_Next Jump to TurnEvent_Next if this event bit is not set.
Count down this event's timer.
41613 LD DE,0 Initialise DE to 0.
41616 LD HL,41161 HL=TurnEventCounters.
41619 LD A,8 Calculate the event index (8-B) and store it in E.
41621 SUB B
41622 LD E,A
41623 ADD HL,DE Point HL at this event's countdown timer.
41624 DEC (HL) Decrease this event's countdown timer by one.
41625 JR NZ,TurnEvent_Next Jump to TurnEvent_Next if this event's countdown timer is still running.
The timer has expired, so run this event's script.
41627 PUSH IX Stash IX and BC on the stack.
41629 PUSH BC
41630 LD IX,48057 IX=JumpTable_TurnEvents.
41634 SLA E Double the event index, as each entry in the table is two bytes.
41636 ADD IX,DE Point IX at this event's entry in the table.
41638 LD L,(IX+0) Read that script's address into HL.
41641 LD H,(IX+1)
41644 PUSH HL Copy the script address from HL into IX (using the stack).
41645 POP IX
41647 CALL Handler_RunScript Call Handler_RunScript.
41650 POP BC Restore BC and IX from the stack.
41651 POP IX
Test the next turn-based event.
TurnEvent_Next 41653 DJNZ TurnEvent_NextBit Decrease the event bit counter by one and loop back to TurnEvent_NextBit until all eight events have been considered.
Now handle the scenic events.
ScenicEvents_Start 41655 LD HL,46778 HL=Table_ScenicEventItems.
41658 LD A,(48079) Fetch the number of active scenic events from Count_ScenicEvents.
41661 OR A
41662 JR Z,Handler_GameEvents_Return Jump to Handler_GameEvents_Return if there are no active scenic events.
41664 LD B,A Set a counter in B for the number of scenic events.
41665 JR ScenicEvent_Item Jump to ScenicEvent_Item to process the first scenic event without advancing the table pointer.
Room 0 means this scenic item is inactive.
ScenicEvent_Next 41667 INC HL Move on to the next scenic item.
ScenicEvent_Item 41668 LD A,(HL) Fetch this scenic event's item ID.
41669 PUSH HL Stash the scenic event table pointer on the stack.
41670 CALL ItemLocator Call ItemLocator to read where this item is.
41673 POP HL Restore the scenic event table pointer from the stack.
41674 AND A Jump to ScenicEvent_Done if the item is inactive.
41675 JR Z,ScenicEvent_Done
An event that is already in the player's room stays there.
41677 LD C,A Hold this event's room in C.
41678 LD A,(41222) A=*CurrentRoom.
41681 CP C Jump to ScenicEvent_Done if this event's room is the current room.
41682 JR Z,ScenicEvent_Done
Pick a room for this scenic event.
41684 PUSH IX Stash IX, BC and HL on the stack.
41686 PUSH BC
41687 PUSH HL
41688 LD A,(48079) Calculate which event's group this is.
41691 SUB B
41692 LD E,A
Point at this event's room list.
41693 LD IX,46783 IX=Table_ScenicEventRoomGroups.
41697 CALL GetTableEntry Call GetTableEntry to fetch this event's room group.
The refresh register picks how far to walk into the room group. 255 ends the group and the walk wraps.
41700 LD A,R Use the Memory Refresh Register to pick a random step count and store it in B.
41702 LD B,A
41703 LD A,255 A=the room group terminator (255).
ScenicRoom_Reload 41705 LD L,(IX+0) Point HL at the start of this room group.
41708 LD H,(IX+1)
41711 JR ScenicRoom_Check Jump to ScenicRoom_Check.
Walk the room group. 255 restarts it, and the refresh count chooses where to stop. Then move the scenic item there.
ScenicRoom_Next 41713 INC HL Move to the next room in the group.
ScenicRoom_Check 41714 CP (HL) Jump back to ScenicRoom_Reload when the terminator matches.
41715 JR Z,ScenicRoom_Reload
41717 DJNZ ScenicRoom_Next Decrease the step counter by one and loop back to ScenicRoom_Next until the counter is zero.
41719 LD C,(HL) Fetch the destination room into C.
41720 POP HL Restore the scenic event table pointer from the stack.
41721 LD B,(HL) Fetch this scenic event's item ID into B.
41722 PUSH HL Stash the scenic event table pointer on the stack.
41723 CALL Handler_UpdateItemLocation Call Handler_UpdateItemLocation.
41726 POP HL Restore HL, BC and IX from the stack.
41727 POP BC
41728 POP IX
Move on to the next scenic event.
ScenicEvent_Done 41730 DJNZ ScenicEvent_Next Decrease the scenic event counter by one and loop back to ScenicEvent_Next until every scenic event has been considered.
Handler_GameEvents_Return 41732 RET Return.
View the equivalent code in;
Prev: 41589 Up: Map Next: 41733