Prev: 39264 Up: Map Next: 39408
39338: Run Main Loop
Used by the routine at UpdateCounters.
Every actor (the player, the animals, the tribesmen, the objects and so on) is held in a 12 byte record in the actor table, which runs from ImmortalData to StoredActorTable. The first byte of each record is the sprite ID, and this is also the index into the jump table at JumpTable which selects the handler routine for the actor.
The handler for one actor is run per pass of MainLoop_NextActor. When the frame counter changes the actor currently being processed is remembered in SavedActorPointer (this happens every frame, 50 times a second), the player's actor (ImmortalData) is processed straight away and then the loop carries on from where it was interrupted. Once every actor has been processed, UpdateCounters runs.
MainLoop 39338 LD SP,24576 Set the stack pointer to GameEntry.
39341 EI Enable interrupts.
39342 LD IX,38670 Point IX at the first actor after the player, at ImmortalData_Actors.
This entry point is used by the routine at NextActor.
Check if the frame counter has changed since the last time through.
MainLoop_NextActor 39346 LD A,(23672) Fetch the low byte of the frame counter from FRAMES into C.
39349 LD C,A
39350 LD A,(38550) Fetch the frame counter from the previous pass from LastFrame.
39353 CP C Jump to MainLoop_RunHandler if the frame counter has not changed.
39354 JR Z,MainLoop_RunHandler
FrameUpdate 39356 DI Disable interrupts.
39357 LD A,(23672) Remember the new frame counter in LastFrame.
39360 LD (38550),A
39363 LD A,1 Set the frame updated flag in FrameUpdated.
39365 LD (38551),A
39368 LD (38569),IX Remember which actor was being processed in SavedActorPointer.
39372 LD IX,38658 Point IX at the player's actor at ImmortalData.
Update the random number seed using the Z80 refresh register.
MainLoop_RunHandler 39376 LD A,R Fetch the Z80 refresh register as a source of randomness.
39378 LD C,A Stash it in C.
39379 LD A,(38549) Fetch the random seed from RandomNumber.
39382 ADC A,C Mix in the refresh register value and the carry flag.
39383 LD (38549),A Store the new random seed in RandomNumber.
Run the handler for this actor. The handler returns to NextActor, so push that address first, then use the sprite ID to find the handler in the jump table.
39386 LD HL,39408 Stash the return address NextActor on the stack so the handler returns to it.
39389 PUSH HL
39390 LD BC,39742 Point BC at the handler jump table at JumpTable.
See See Ending.
39393 LD A,(IX+0) Fetch the sprite ID of the actor into HL.
39396 LD L,A
39397 LD H,0
39399 ADD HL,HL Double the sprite ID to get the offset into the jump table.
39400 ADD HL,BC Add the jump table address to get the address of the actor's entry.
39401 LD A,(HL) Fetch the address of the handler from the jump table.
39402 INC HL
39403 LD H,(HL)
39404 LD L,A
39405 JP IndirectJump Jump to IndirectJump, which jumps to the address held in HL.
Prev: 39264 Up: Map Next: 39408