Prev: 990A Up: Map Next: 9A22
993C: Main Loop
Runs once per frame. It checks whether the round is over, runs the handler of every entity, checks one cell of the map, draws the world and then updates the battery, the messages and the attacks on the sectors.
Check whether the program packet is still on its way, and whether the round has ended.
MainLoop 993C LD A,($9770) Fetch the type of the program packet (entity 0E).
993F AND A Jump if there is no program packet.
9940 JR Z,MainLoop_CheckRoundEnd
9942 LD A,($977E) Fetch the state of the program packet.
9945 AND $0F Skip the end of round checks while the state is not zero.
9947 JR NZ,MainLoop_Entities
MainLoop_CheckRoundEnd 9949 LD A,($9659) Fetch the number of sectors that have been destroyed.
994C CP $09 Jump if all nine sectors have been destroyed, as the game is lost.
994E JP Z,LoseGame
9951 LD L,A Keep the number of destroyed sectors.
9952 LD A,($9658) Fetch the number of sectors that have been activated.
9955 CP $09 Jump if all nine have been activated, as the round has been won.
9957 JP Z,WinRound
995A ADD A,L Add the number of destroyed sectors.
995B CP $09 Jump if all nine sectors have been dealt with, one way or the other, to see how well the player has done.
995D JP Z,EndRound
9960 CALL UpdatePacketMarker Update the markers on the sector map using UpdatePacketMarker.
Run the handler of each entity, starting with the robot.
MainLoop_Entities 9963 LD IX,$9690 Point IX at the first entity (the robot).
9967 LD HL,$D355 Point HL at DrawListEntries.
996A LD ($962A),HL Save it as the current position in the draw list.
996D CALL ReadControls Read the controls using ReadControls.
This entry point is used by the routine at FinishRobotTurn.
MainLoop_Dispatch 9970 LD HL,$9985 Push the address of MainLoop_NextEntity, which is where the handler returns to.
9973 PUSH HL
9974 LD L,(IX+$00) Fetch the type of the entity.
9977 LD BC,$9F5B Point BC at EntityHandlers.
This entry point is used by the routine at MoveLights.
This entry point jumps to the routine in the table at BC that is selected by L.
MainLoop_Dispatch_Table 997A LD H,$00 Clear the high byte of the index.
997C ADD HL,HL Double the index.
997D LD H,$00 Clear the high byte again, which discards the flip flag that was in bit 7 of the type.
997F ADD HL,BC Add the address of the table.
9980 LD A,(HL) Fetch the low byte of the handler address...
9981 INC HL ...and then the high byte...
9982 LD H,(HL) ...into HL.
9983 LD L,A Assemble the low byte of the handler address.
9984 JP (HL) Run the handler.
Each handler returns here.
MainLoop_NextEntity 9985 LD A,($9615) Fetch the frame counter.
9988 LD C,A Keep the frame counter.
9989 LD A,($9617) Fetch the random number.
998C ADD A,C Add the frame counter to it...
998D LD ($9617),A ...and store the result as the new random number.
9990 LD BC,$0010 Each entity is 10 bytes long.
9993 ADD IX,BC Move on to the next entity.
9995 PUSH IX Copy the address of the entity into HL.
9997 POP HL
9998 LD BC,$98D0 Point BC at the end of the entity table.
999B AND A Subtract the address of the end of the entity table.
999C SBC HL,BC
999E JR NC,MainLoop_EndOfFrame Jump if every entity has been handled.
99A0 JR MainLoop_Dispatch Otherwise run the handler of the next entity.
Check one cell of the map, and then draw the frame.
MainLoop_EndOfFrame 99A2 LD HL,($9615) Fetch the frame counter.
99A5 INC HL Increment it...
99A6 LD ($9615),HL ...and store it.
99A9 LD A,R Fetch a number from the memory refresh register.
99AB LD L,A Keep the refresh value.
99AC LD A,($9617) Fetch the random number.
99AF ADD A,H Add the high byte of the frame counter to it...
99B0 ADD A,L ...and then the number from the memory refresh register...
99B1 LD ($9617),A ...and store the result as the new random number.
99B4 LD HL,($963D) Fetch the position of the map cell that is checked next.
99B7 INC L Move to the next column.
99B8 LD A,L Limit the column to 00-3F.
99B9 AND $3F
99BB LD L,A
99BC CP $30 Jump if the column is inside the 30 columns of the map.
99BE JR C,MainLoop_StoreCell
99C0 LD L,$00 Otherwise go back to column 00...
99C2 INC H ...and move on to the next row.
99C3 LD A,H Limit the row to 00-3F.
99C4 AND $3F
99C6 LD H,A
99C7 CP $30 Jump if the row is inside the 30 rows of the map.
99C9 JR C,MainLoop_StoreCell
99CB LD H,$00 Otherwise go back to row 00.
MainLoop_StoreCell 99CD LD ($963D),HL Store the position of the cell to check.
99D0 EX DE,HL Put the position in DE.
99D1 CALL FindMapCell_ColumnRow Find the address of the cell in the map using FindMapCell_ColumnRow.
99D4 LD HL,($962E) Fetch the address of the cell.
99D7 LD A,(HL) Fetch the contents of the cell.
99D8 CP $88 Is this a program transmitter (current and initial types both 08)? If so, send a program packet using SendProgramPacket.
99DA CALL Z,SendProgramPacket
99DD AND $F0 Keep the initial type of the cell.
99DF CP $30 Compare it with 30 (the result is never used), and jump over the code that is never run.
99E1 JR MainLoop_Draw
This code is never run.
MainLoop_Unused 99E3 JR NZ,MainLoop_Draw Skip the rest if the initial type is not 30.
99E5 LD A,(HL) Fetch the contents of the cell.
99E6 AND $F0 Keep the initial type...
99E8 LD B,A ...in B.
99E9 RRA Move the initial type into the low nibble.
99EA RRA
99EB RRA
99EC RRA
99ED OR B Combine it with the initial type...
99EE LD (HL),A ...and write it back, so that the cell goes back to its initial type.
Draw the world, then deal with the battery, the messages and the attacks on the sectors.
MainLoop_Draw 99EF CALL SortDrawList Sort the draw list using SortDrawList.
99F2 CALL AddMapCells Add the cells of the map to the draw list using AddMapCells.
99F5 CALL SortDrawList Sort the draw list again.
99F8 CALL DrawList Draw everything in the draw list into the off-screen buffer using DrawList.
99FB CALL DrawPlayWindow Copy the off-screen buffer to the screen using DrawPlayWindow.
99FE CALL SpawnEnemies Try to start a new enemy using SpawnEnemies.
9A01 LD ($9635),SP Save the stack pointer (this is never used).
9A05 CALL CheckPause Check for the pause key using CheckPause.
9A08 LD HL,$0001 Set up a delay that is only one iteration long.
MainLoop_Delay 9A0B DEC HL Count down.
9A0C LD A,L Loop back until the delay has finished.
9A0D OR H
9A0E JR NZ,MainLoop_Delay
9A10 CALL PlaySoundStep Play the next part of the current sound effect using PlaySoundStep.
9A13 CALL UpdateBatteryBar Update the battery bar using UpdateBatteryBar.
9A16 CALL CheckBatteryLow Flash the battery if it is low using CheckBatteryLow.
9A19 CALL AttackSector Count down to the next attack on a sector using AttackSector.
9A1C CALL PrintMessage Print the next character of the current message using PrintMessage.
9A1F JP MainLoop Go round again.
Prev: 990A Up: Map Next: 9A22