Prev: C058 Up: Map Next: C1F0
C17D: 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 C17D LD A,($BC66) Load *Flag_TurnBasedEventState into A.
C180 OR A Jump to GameScenicEventsProcessor if no turn-based events are currently active.
C181 JR Z,GameScenicEventsProcessor
One of the events has triggered, find which one.
C183 LD C,A Copy the turn-based event flags into C.
C184 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.
GameEventChecker_Loop C186 SRL C Shift C one position left to check the next event flag bit.
C188 JR NC,GameEventChecker_Next Jump to GameEventChecker_Next if this event bit isn't set.
The currently processed turn-based event is active, so process it.
This is: Counter_Crab+(08-the current event counter).
C18A LD DE,$0000 Calculate the turn-based event index and store it in HL.
C18D LD HL,$BC67
C190 LD A,$08
C192 SUB B
C193 LD E,A
C194 ADD HL,DE
C195 DEC (HL) Decrease the turn-based event counter at *HL by one.
C196 JR NZ,GameEventChecker_Next Jump to GameEventChecker_Next if the event turn counter at *HL was still more than zero.
The event turn counter is zero, so activate the event itself.
C198 LD IX,($BD0E) Load *Pointer_TurnBasedEvents_Jump into IX.
C19C SLA E Double the index as this table contains addresses.
C19E ADD IX,DE Add this offset to IX to point to the current handler.
C1A0 LD L,(IX+$00) Get the handler address and store it in HL.
C1A3 LD H,(IX+$01)
C1A6 PUSH BC Stash the bit index and event flag byte on the stack.
C1A7 LD DE,$C1AC Push GameEventChecker_Return onto the stack so that the next return will go on to continue processing events.
C1AA PUSH DE
C1AB 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 C1AC POP BC Restore the event bit index counter and event flag byte from the stack.
GameEventChecker_Next C1AD DJNZ GameEventChecker_Loop Decrease the event bit counter by one and loop back to GameEventChecker_Loop until all the event status bits have been processed.
Process scenic events.
GameScenicEventsProcessor C1AF NOP No operation.
C1B0 LD HL,$BC78 Load HL with Table_ScenicEventLocations.
C1B3 LD A,($BD30) Load *Count_ScenicEvents into A.
C1B6 OR A Jump to GameEventsProcessor_Return if there are no more scenic events to run (the count is zero).
C1B7 JR Z,GameEventsProcessor_Return
C1B9 LD B,A Copy *Count_ScenicEvents into B as an event counter for the loop.
C1BA JR GameScenicEventChecker Jump to GameScenicEventChecker.
This is the main scenic event loop.
GameScenicEventChecker_Loop C1BC INC HL Increment the scenic event pointer location by one.
GameScenicEventChecker C1BD LD A,(HL) Jump to GameScenicEventChecker_Next if the event room is 00 (deactivated).
C1BE OR A
C1BF JR Z,GameScenicEventChecker_Next
C1C1 LD A,($BCCB) Jump to GameScenicEventChecker_Next if *CurrentRoom is equal to the event room.
C1C4 CP (HL)
C1C5 JR Z,GameScenicEventChecker_Next
C1C7 PUSH HL Stash the scenic event location pointer and event counter on the stack.
C1C8 PUSH BC
This is: E=*Count_ScenicEvents-B.
C1C9 LD A,($BD30) Calculate the index of the currently processed event.
C1CC SUB B
C1CD LD E,A
C1CE LD IX,$BCCC Load Table_ScenicEventRooms into IX.
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).
C1D2 CALL GetTableEntry Call GetTableEntry.
C1D5 CALL FetchFrames Load a random number from FetchFrames into B.
C1D8 LD B,A
C1D9 LD A,$FF Set A to FF (the room group terminator) for the comparison.
The table at Table_ScenicEventRooms is indexed by event (there are 08 entries), but 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 FF.
GameScenicEvent_FetchRoomGroup C1DB LD L,(IX+$00) Get the room group address pointer and store it in HL.
C1DE LD H,(IX+$01)
C1E1 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 C1E3 INC HL Move to the next room ID in the group.
GameScenicEvent_CountRoomsInGroup C1E4 CP (HL) Jump to GameScenicEvent_FetchRoomGroup if the terminator is read from *HL.
C1E5 JR Z,GameScenicEvent_FetchRoomGroup
C1E7 DJNZ GameScenicEvent_CountRooms_Loop Decrease the random counter by one and loop back to GameScenicEvent_CountRooms_Loop until the counter is zero.
C1E9 LD A,(HL) Load the current room ID into A.
C1EA POP BC Restore the event counter and scenic event location pointer from the stack.
C1EB POP HL
C1EC LD (HL),A Update the scenic event location with the newly selected room.
GameScenicEventChecker_Next C1ED 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 C1EF RET Return.
Prev: C058 Up: Map Next: C1F0