Prev: 39178 Up: Map Next: 39458
39228: 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 39228 LD A,(38768) Fetch the type of the program packet (entity 14).
39231 AND A Jump if there is no program packet.
39232 JR Z,MainLoop_CheckRoundEnd
39234 LD A,(38782) Fetch the state of the program packet.
39237 AND 15 Skip the end of round checks while the state is not zero.
39239 JR NZ,MainLoop_Entities
MainLoop_CheckRoundEnd 39241 LD A,(38489) Fetch the number of sectors that have been destroyed.
39244 CP 9 Jump if all nine sectors have been destroyed, as the game is lost.
39246 JP Z,LoseGame
39249 LD L,A Keep the number of destroyed sectors.
39250 LD A,(38488) Fetch the number of sectors that have been activated.
39253 CP 9 Jump if all nine have been activated, as the round has been won.
39255 JP Z,WinRound
39258 ADD A,L Add the number of destroyed sectors.
39259 CP 9 Jump if all nine sectors have been dealt with, one way or the other, to see how well the player has done.
39261 JP Z,EndRound
39264 CALL UpdatePacketMarker Update the markers on the sector map using UpdatePacketMarker.
Run the handler of each entity, starting with the robot.
MainLoop_Entities 39267 LD IX,38544 Point IX at the first entity (the robot).
39271 LD HL,54101 Point HL at DrawListEntries.
39274 LD (38442),HL Save it as the current position in the draw list.
39277 CALL ReadControls Read the controls using ReadControls.
This entry point is used by the routine at FinishRobotTurn.
MainLoop_Dispatch 39280 LD HL,39301 Push the address of MainLoop_NextEntity, which is where the handler returns to.
39283 PUSH HL
39284 LD L,(IX+0) Fetch the type of the entity.
39287 LD BC,40795 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 39290 LD H,0 Clear the high byte of the index.
39292 ADD HL,HL Double the index.
39293 LD H,0 Clear the high byte again, which discards the flip flag that was in bit 7 of the type.
39295 ADD HL,BC Add the address of the table.
39296 LD A,(HL) Fetch the low byte of the handler address...
39297 INC HL ...and then the high byte...
39298 LD H,(HL) ...into HL.
39299 LD L,A Assemble the low byte of the handler address.
39300 JP (HL) Run the handler.
Each handler returns here.
MainLoop_NextEntity 39301 LD A,(38421) Fetch the frame counter.
39304 LD C,A Keep the frame counter.
39305 LD A,(38423) Fetch the random number.
39308 ADD A,C Add the frame counter to it...
39309 LD (38423),A ...and store the result as the new random number.
39312 LD BC,16 Each entity is 16 bytes long.
39315 ADD IX,BC Move on to the next entity.
39317 PUSH IX Copy the address of the entity into HL.
39319 POP HL
39320 LD BC,39120 Point BC at the end of the entity table.
39323 AND A Subtract the address of the end of the entity table.
39324 SBC HL,BC
39326 JR NC,MainLoop_EndOfFrame Jump if every entity has been handled.
39328 JR MainLoop_Dispatch Otherwise run the handler of the next entity.
Check one cell of the map, and then draw the frame.
MainLoop_EndOfFrame 39330 LD HL,(38421) Fetch the frame counter.
39333 INC HL Increment it...
39334 LD (38421),HL ...and store it.
39337 LD A,R Fetch a number from the memory refresh register.
39339 LD L,A Keep the refresh value.
39340 LD A,(38423) Fetch the random number.
39343 ADD A,H Add the high byte of the frame counter to it...
39344 ADD A,L ...and then the number from the memory refresh register...
39345 LD (38423),A ...and store the result as the new random number.
39348 LD HL,(38461) Fetch the position of the map cell that is checked next.
39351 INC L Move to the next column.
39352 LD A,L Limit the column to 0-63.
39353 AND 63
39355 LD L,A
39356 CP 48 Jump if the column is inside the 48 columns of the map.
39358 JR C,MainLoop_StoreCell
39360 LD L,0 Otherwise go back to column 0...
39362 INC H ...and move on to the next row.
39363 LD A,H Limit the row to 0-63.
39364 AND 63
39366 LD H,A
39367 CP 48 Jump if the row is inside the 48 rows of the map.
39369 JR C,MainLoop_StoreCell
39371 LD H,0 Otherwise go back to row 0.
MainLoop_StoreCell 39373 LD (38461),HL Store the position of the cell to check.
39376 EX DE,HL Put the position in DE.
39377 CALL FindMapCell_ColumnRow Find the address of the cell in the map using FindMapCell_ColumnRow.
39380 LD HL,(38446) Fetch the address of the cell.
39383 LD A,(HL) Fetch the contents of the cell.
39384 CP 136 Is this a program transmitter (current and initial types both 8)? If so, send a program packet using SendProgramPacket.
39386 CALL Z,SendProgramPacket
39389 AND 240 Keep the initial type of the cell.
39391 CP 48 Compare it with 48 (the result is never used), and jump over the code that is never run.
39393 JR MainLoop_Draw
This code is never run.
MainLoop_Unused 39395 JR NZ,MainLoop_Draw Skip the rest if the initial type is not 48.
39397 LD A,(HL) Fetch the contents of the cell.
39398 AND 240 Keep the initial type...
39400 LD B,A ...in B.
39401 RRA Move the initial type into the low nibble.
39402 RRA
39403 RRA
39404 RRA
39405 OR B Combine it with the initial type...
39406 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 39407 CALL SortDrawList Sort the draw list using SortDrawList.
39410 CALL AddMapCells Add the cells of the map to the draw list using AddMapCells.
39413 CALL SortDrawList Sort the draw list again.
39416 CALL DrawList Draw everything in the draw list into the off-screen buffer using DrawList.
39419 CALL DrawPlayWindow Copy the off-screen buffer to the screen using DrawPlayWindow.
39422 CALL SpawnEnemies Try to start a new enemy using SpawnEnemies.
39425 LD (38453),SP Save the stack pointer (this is never used).
39429 CALL CheckPause Check for the pause key using CheckPause.
39432 LD HL,1 Set up a delay that is only one iteration long.
MainLoop_Delay 39435 DEC HL Count down.
39436 LD A,L Loop back until the delay has finished.
39437 OR H
39438 JR NZ,MainLoop_Delay
39440 CALL PlaySoundStep Play the next part of the current sound effect using PlaySoundStep.
39443 CALL UpdateBatteryBar Update the battery bar using UpdateBatteryBar.
39446 CALL CheckBatteryLow Flash the battery if it is low using CheckBatteryLow.
39449 CALL AttackSector Count down to the next attack on a sector using AttackSector.
39452 CALL PrintMessage Print the next character of the current message using PrintMessage.
39455 JP MainLoop Go round again.
Prev: 39178 Up: Map Next: 39458