Prev: 43779 Up: Map Next: 43912
43786: Process Game Events
Used by the routine at GameLoop.
This routine manages two types of in-game events:
1. Turn-Based Events
These are timer-based hazards that count down with each game turn. When a turn counter reaches zero, the associated event handler is called via a jump table.
2. Scenic Events
Mostly atmospheric events that randomly appear in different locations to add flavor to the game world (like seagulls appearing at the beach).
The routine maintains a table of current locations for each scenic event. On each turn, it checks if any scenic event is NOT in the current room, then randomly relocates it to one of its valid rooms.
Each scenic event has a predefined group of rooms where the event can appear.
The routine ensures that:
  • Multiple turn-based dangers can threaten the player simultaneously
  • Scenic events don't repeatedly trigger in the same room
  • The game world feels dynamic with events occurring in different locations
First process the turn-based events (if any are active).
GameEventsProcessor 43786 LD A,(42878) Load *Flag_TurnBasedEventState into A.
43789 OR A Jump to GameScenicEventsProcessor if no turn-based events are currently active.
43790 JR Z,GameScenicEventsProcessor
Eight turn-based events share the flag byte.
43792 LD C,A Copy the turn-based event flags into C.
43793 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.
GameEventChecker_Loop 43795 SRL C Shift C right so bit 0 (the next event flag) goes into the carry flag.
43797 JR NC,GameEventChecker_Next Jump to GameEventChecker_Next if this event bit is not set.
Count down this event's timer.
43799 LD DE,0 Initialise DE to 0.
43802 LD HL,42879 HL=Counter_Drowning.
43805 LD A,8 Calculate the event index (8-B) and store it in E.
43807 SUB B
43808 LD E,A
43809 ADD HL,DE Point HL at this event's countdown timer.
43810 DEC (HL) Decrease this event's countdown timer by one.
43811 JR NZ,GameEventChecker_Next Jump to GameEventChecker_Next if this event's countdown timer is still running.
The timer has expired, so run this event's handler.
43813 LD IX,(42952) IX=*Pointer_TurnBasedEvents_Jump.
43817 SLA E Double the event index, as each entry in the table is two bytes.
43819 ADD IX,DE Point IX at this event's entry in the table.
43821 LD L,(IX+0) Read that handler's address into HL.
43824 LD H,(IX+1)
43827 PUSH BC Stash BC on the stack.
43828 LD DE,43833 Stash GameEventChecker_Return on the stack so the next return resumes event processing.
43831 PUSH DE
43832 JP (HL) Jump to the routine pointed to by *HL to action the current event.
Once the event handler has run, this is where the routine will resume.
GameEventChecker_Return 43833 POP BC Restore BC from the stack.
Test the next turn-based event.
GameEventChecker_Next 43834 DJNZ GameEventChecker_Loop Decrease the event bit counter by one and loop back to GameEventChecker_Loop until all eight events have been considered.
Now handle the scenic events.
GameScenicEventsProcessor 43836 NOP No operation.
43837 LD HL,(42978) HL=*Pointer_ScenicEventItems.
43840 LD A,(42990) Fetch the number of active scenic events from Count_ScenicEvents.
43843 OR A
43844 JR Z,GameEventsProcessor_Return Jump to GameEventsProcessor_Return if there are no active scenic events.
43846 LD B,A Set a counter in B for the number of scenic events.
43847 JR GameScenicEventChecker Jump to GameScenicEventChecker to process the first scenic event without advancing the table pointer.
This is the main scenic event loop.
GameScenicEventChecker_Loop 43849 INC HL Move on to the next scenic item.
GameScenicEventChecker 43850 LD A,(HL) Fetch this scenic event's item ID.
43851 PUSH HL Stash the scenic event table pointer on the stack.
43852 CALL ItemLocator Call ItemLocator to find where this scenic event's item is.
43855 POP HL Restore the scenic event table pointer from the stack.
43856 AND A Set the flags from the item location in A.
43857 JR Z,GameScenicEventChecker_Next Jump to GameScenicEventChecker_Next if the item is inactive.
43859 LD C,A Hold this event's room in C.
43860 LD A,(42947) A=*CurrentRoom.
43863 CP C Compare the current room with the item location.
43864 JR Z,GameScenicEventChecker_Next Jump to GameScenicEventChecker_Next if the item is in the current room.
Pick a room for this scenic event.
43866 PUSH BC Stash BC and HL on the stack.
43867 PUSH HL
Calculate which scenic event's room group this is.
43868 LD A,(42990) Calculate the index of the currently processed event.
43871 SUB B
43872 LD E,A
Point at this event's room list.
43873 LD IX,(42980) IX=*Pointer_ScenicEventRooms.
The routine at GetTableEntry is usually used to fetch an address from the given table, but here it's used to move IX to point to the index of the currently processed event (and this routine will fetch the address itself).
43877 CALL GetTableEntry Call GetTableEntry.
The routine at FetchFrames picks how far to walk into the room group.
43880 CALL FetchFrames Call FetchFrames to pick a random step count and store it in B.
43883 LD B,A
43884 LD A,255 A=the room group terminator (255).
The table at Pointer_ScenicEventRooms is indexed by event (there are 17 entries), and each address points to a group of room IDs where this event could occur. Each set of room IDs for the event is terminated using 255.
GameScenicEvent_FetchRoomGroup 43886 LD L,(IX+0) Point HL at the start of this room group.
43889 LD H,(IX+1)
43892 JR GameScenicEvent_CountRoomsInGroup Jump to GameScenicEvent_CountRoomsInGroup.
Use the random number to select a room from the group - this loops through the rooms using B as a counter.
GameScenicEvent_CountRooms_Loop 43894 INC HL Move to the next room ID in the group.
GameScenicEvent_CountRoomsInGroup 43895 CP (HL) Jump back to GameScenicEvent_FetchRoomGroup when the terminator matches.
43896 JR Z,GameScenicEvent_FetchRoomGroup
43898 DJNZ GameScenicEvent_CountRooms_Loop Decrease the random counter by one and loop back to GameScenicEvent_CountRooms_Loop until the counter is zero.
43900 LD C,(HL) Fetch the destination room into C.
43901 POP HL Restore the scenic event table pointer from the stack.
43902 LD B,(HL) Fetch this scenic event's item ID into B.
43903 PUSH HL Stash the scenic event table pointer on the stack.
43904 CALL Handler_UpdateItemLocation Call Handler_UpdateItemLocation.
43907 POP HL Restore HL and BC from the stack.
43908 POP BC
GameScenicEventChecker_Next 43909 DJNZ GameScenicEventChecker_Loop Decrease the scenic event counter by one and loop back to GameScenicEventChecker_Loop until all scenic events have been processed.
All done, now return.
GameEventsProcessor_Return 43911 RET Return.
Prev: 43779 Up: Map Next: 43912