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 46783.
First process the turn-based events (if any are active).
Handler_GameEvents 41600 LD A,(41160) Jump to ScenicEvents_Start if no turn-based events are currently active.
41603 OR A
41604 JR Z,ScenicEvents_Start
Eight turn-based events share the flag byte.
41606 LD C,A Start with the first turn-based event.
41607 LD B,8
Keep shifting the event flags one-by-one to find which bits are set.
TurnEvent_NextBit 41609 SRL C Jump to TurnEvent_Next if this event bit is not set.
41611 JR NC,TurnEvent_Next
Count down this event. It fires when the counter at TurnEventCounters reaches zero.
41613 LD DE,0 Jump to TurnEvent_Next if the counter is still running.
41616 LD HL,41161
41619 LD A,8
41621 SUB B
41622 LD E,A
41623 ADD HL,DE
41624 DEC (HL)
41625 JR NZ,TurnEvent_Next
The counter has expired, so run this event's script from JumpTable_TurnEvents.
41627 PUSH IX The scripts are listed at JumpTable_TurnEvents.
41629 PUSH BC
41630 LD IX,48057
41634 SLA E Point at this event's script entry.
41636 ADD IX,DE
41638 LD L,(IX+0) Read that script's address.
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 Run that script.
41650 POP BC Restore the event counter and event pointer 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.
Process scenic events.
ScenicEvents_Start 41655 LD HL,46778 Jump to Handler_GameEvents_Return if no scenic event is active.
41658 LD A,(48079)
41661 OR A
41662 JR Z,Handler_GameEvents_Return
41664 LD B,A Visit each scenic event in turn.
41665 JR ScenicEvent_Item
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 item's ID.
41669 PUSH HL
41670 CALL ItemLocator Call ItemLocator to read where this item is.
41673 POP HL Restore the event-list 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 Jump to ScenicEvent_Done if this event's room is *CurrentRoom.
41678 LD A,(41222)
41681 CP C
41682 JR Z,ScenicEvent_Done
Each scenic event has a room group in 46783.
41684 PUSH IX Keep the scenic scan while a room is chosen.
41686 PUSH BC
41687 PUSH HL
41688 LD A,(48079) Calculate which event's group this is.
41691 SUB B
41692 LD E,A
41693 LD IX,46783 The room groups are listed at 46783.
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 Load a step count from the refresh register and set the group terminator in A.
41702 LD B,A
41703 LD A,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 pointer from the stack.
41721 LD B,(HL) Fetch the item ID into B and stash the pointer again.
41722 PUSH HL
41723 CALL Handler_UpdateItemLocation Call Handler_UpdateItemLocation.
41726 POP HL Restore the scenic scan 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