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
Eight turn-based events share the flag byte.
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 is not set.
Count down this event's timer.
C18A LD DE,$0000 Initialise DE to 00.
C18D LD HL,$BC67 HL=Counter_Crab.
C190 LD A,$08 Calculate the event index (08-B) and store it in E.
C192 SUB B
C193 LD E,A
C194 ADD HL,DE Point HL at this event's countdown timer.
C195 DEC (HL) Decrease this event's countdown timer by one.
C196 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.
C198 LD IX,($BD0E) IX=*Pointer_TurnBasedEvents_Jump.
C19C SLA E Double the event index, as each entry in the table is two bytes.
C19E ADD IX,DE Point IX at this event's entry in the table.
C1A0 LD L,(IX+$00) Read that handler's address into HL.
C1A3 LD H,(IX+$01)
C1A6 PUSH BC Stash BC on the stack.
C1A7 LD DE,$C1AC Stash GameEventChecker_Return on the stack so the next return resumes event processing.
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 BC from the stack.
Test the next turn-based event.
GameEventChecker_Next C1AD 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 C1AF NOP No operation.
C1B0 LD HL,$BC78 HL=Table_ScenicEventLocations.
C1B3 LD A,($BD30) Fetch the number of active scenic events from Count_ScenicEvents.
C1B6 OR A
C1B7 JR Z,GameEventsProcessor_Return Jump to GameEventsProcessor_Return if there are no active scenic events.
C1B9 LD B,A Set a counter in B for the number of scenic events.
C1BA 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 C1BC INC HL Move on to the next scenic item.
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
Pick a room for this scenic event.
C1C7 PUSH HL Stash HL and BC on the stack.
C1C8 PUSH BC
Calculate which scenic event's room group this is.
C1C9 LD A,($BD30) Calculate the index of the currently processed event.
C1CC SUB B
C1CD LD E,A
Point at this event's room list.
C1CE LD IX,$BCCC IX=Table_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).
C1D2 CALL GetTableEntry Call GetTableEntry.
C1D5 CALL FetchFrames Call FetchFrames to pick a random step count and store it in B.
C1D8 LD B,A
C1D9 LD A,$FF A=the room group terminator (FF).
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) Point HL at the start of this room group.
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 back to GameScenicEvent_FetchRoomGroup when the terminator matches.
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) Fetch the destination room into A.
C1EA POP BC Restore BC and HL 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.
View the equivalent code in;
Prev: C058 Up: Map Next: C1F0