Prev: AFB7 Up: Map Next: B088
AFBA: Run Game
Used by the routine at LeaveRoomToWest.
The main game loop. The first call builds the objects of the room the player is in. Then every pass of the loop visits each of the 28 objects in the objects table in turn, calling the routine for its graphic number from the jump table at UpdateRoutineTable to move and update it. Once every object has been dealt with the objects are sorted and drawn, the screen is brought up to date and the game waits a little (the more objects in view, the shorter the wait) before the next pass.
After the first pass
run-game
RunGame AFBA CALL BuildRoomObjects Build the objects for the room the player is in.
Every pass of the main loop starts here. The random seed used by the fire is refreshed, then each object in the table is updated in turn.
RunGame_Frame AFBD LD A,($5BA2) Fetch the frame counter from FrameCounter.
AFC0 LD ($5BBC),A Store it as the seed for the fire at FireSeed (it is stepped for every object).
AFC3 LD IX,$5C08 Point IX at ObjectsTable.
Update the object that IX points at.
RunGame_NextObject AFC7 LD SP,$5BA0 Reset the stack pointer to RandomSeed1, as the update routines do not have to tidy up the stack.
AFCA LD HL,$5BBC Point HL at FireSeed.
AFCD INC (HL) Step the fire seed on.
AFCE LD HL,$AFE4 Point HL at RunGame_ObjectUpdated.
AFD1 PUSH HL Stash it on the stack, so that the update routine returns there.
AFD2 CALL SaveSpriteInfo Work out the screen position and size of the object, and keep a note of its old ones.
This entry point is used by the routine at HandleLastAppearingSparkle.
RunGame_UpdateObject AFD5 LD L,(IX+$00) Fetch the graphic number of the object into L.
AFD8 LD BC,$B096 Point BC at UpdateRoutineTable.
This entry point is used by the routines at HandleEndingSparkles, GetGuardSpeeds, SetPlayerSpeed and DrawObjectsInOrder.
Jump to the routine whose address is held in word L of the table that BC points at (H is cleared first).
RunGame_JumpToHandler AFDB LD H,$00 Clear H, so that HL holds the graphic number.
AFDD ADD HL,HL Double it to get the offset of a word in the table.
AFDE ADD HL,BC Add the address of the table.
AFDF LD A,(HL) Read the low byte of the routine address.
AFE0 INC HL Move on to the high byte.
AFE1 LD H,(HL) Read the high byte into H.
AFE2 LD L,A Put the low byte into L.
AFE3 JP (HL) Jump to the update routine.
The update routine of each object returns here.
RunGame_ObjectUpdated AFE4 LD A,R Read the refresh register, which is as good as random.
AFE6 LD C,A Move it into C.
AFE7 LD A,($5BA5) Fetch the second random seed from RandomSeed2.
AFEA ADD A,C Add the random value to it.
AFEB LD ($5BA5),A Store it back as the new second seed at RandomSeed2.
AFEE LD BC,$0020 Set BC to the size of an object.
AFF1 ADD IX,BC Move IX on to the next object.
AFF3 PUSH IX Copy IX to HL (using the stack).
AFF5 POP HL
AFF6 LD BC,$6108 Set BC to the address just beyond the objects table, Font.
AFF9 AND A Subtract it from HL, to see if IX is past the end of the table.
AFFA SBC HL,BC
AFFC JR NC,RunGame_FinishFrame Jump to RunGame_FinishFrame if every object has been updated.
AFFE JR RunGame_NextObject Jump to RunGame_NextObject for the next object.
Every object has been updated; now finish the frame.
RunGame_FinishFrame B000 LD HL,($5BA2) Fetch the frame counter from FrameCounter.
B003 INC HL Increase it.
B004 LD ($5BA2),HL Store it again at FrameCounter.
B007 LD A,($5BA5) Fetch the second random seed from RandomSeed2.
B00A ADD A,(HL) Add the byte of the ROM that the frame counter points at, and the frame counter itself, to it.
B00B ADD A,L
B00C ADD A,H
B00D LD ($5BA5),A Store it as the new second seed at RandomSeed2.
B010 LD HL,$5BB2 Point HL at NotFirstRoomFlag.
B013 SET 0,(HL) Set bit 0, so that the special objects table gets updated the next time round.
B015 CALL PauseGame Check for the pause key.
B018 CALL SpawnCauldronBubbles Create the bubbles of the cauldron if the player is in the cauldron room.
B01B CALL ListObjectsToDraw Make a list of the objects that need to be drawn.
B01E CALL DrawChangedObjects Draw the objects of the list into the screen buffer.
B021 LD A,($5BC5) Fetch the rising blocks flag from SparkleHeight.
B024 AND A Test it.
B025 CALL NZ,PlaySoundFromHeight_PlaySoundFromValue Play the sound of rising blocks if there are any.
Slow the game down when few objects were drawn this frame: the delay is made up of as many short delay loops as the number of objects drawn is below 06.
B028 LD A,($5BBE) Fetch the number of objects that were drawn from ObjectsDrawn.
B02B NEG Negate it.
B02D ADD A,$06 Add 06 to it, to get the number of delay loops.
B02F LD B,A Copy it into B.
B030 JP M,RunGame_UpdateDisplay Jump to RunGame_UpdateDisplay if it is negative (more than 06 objects were drawn).
B033 JR Z,RunGame_UpdateDisplay Jump to RunGame_UpdateDisplay if it is zero.
RunGame_Delay B035 LD HL,$0500 Set HL to the number of times to count down for each delay loop.
RunGame_DelayLoop B038 DEC HL Decrement the counter.
B039 LD A,L Check whether the counter has reached zero.
B03A OR H
B03B JR NZ,RunGame_DelayLoop Jump to RunGame_DelayLoop until the counter is zero.
B03D DJNZ RunGame_Delay Jump to RunGame_Delay until all of the delay loops are done.
If a new room has just been entered, draw the panel and the scenery.
RunGame_UpdateDisplay B03F LD A,($5BB7) Fetch the flag which says that the room has just been drawn from RedrawPanelFlag.
B042 AND A Jump to RunGame_Continue if it is zero.
B043 JR Z,RunGame_Continue
B045 XOR A Reset the flag at RedrawPanelFlag.
B046 LD ($5BB7),A
B049 LD A,($5BAD) Fetch the attribute of the room from RoomAttribute.
B04C CALL FillAttributes Colour the whole screen with it.
B04F CALL DisplayObjects Draw the objects the player is carrying.
B052 CALL SetBorderAttributes Colour the panel.
B055 CALL ColourSunMoon Colour the sun and moon window.
B058 CALL DrawPanel Draw the panel.
B05B LD IX,$C44D Point IX at SunMoonObject.
B05F CALL DisplaySunMoonFrame Draw the sun or moon.
B062 CALL PrintDayCaption Draw the day number.
B065 CALL PrintDays Draw the number of days.
B068 CALL PrintLivesGraphic Draw the lives graphics.
B06B CALL PrintLives Print the number of lives.
B06E CALL UpdateScreen Copy the screen buffer to the display.
B071 CALL ResetWipeFlags Reset the wipe flag of all of the objects.
Start another frame, or lose a life if the player is gone.
RunGame_Continue B074 XOR A Write 00 to *SparkleHeight (the rising blocks flag).
B075 LD ($5BC5),A
B078 LD IX,$5C08 Point IX at ObjectsTable.
B07C LD A,(IX+$00) Fetch the graphic number of the bottom half of the player.
B07F OR (IX+$20) Combine it with the graphic number of the top half of the player.
B082 JP Z,StartLife Jump to StartLife if neither half is present, as the player has been killed.
B085 JP RunGame_Frame Jump to RunGame_Frame for another pass of the game loop.
View the equivalent code in;
Prev: AFB7 Up: Map Next: B088