Skip to navigation

Revs on the BBC Micro

Main loop: FinishRace

Name: FinishRace [Show more] Type: Subroutine Category: Main loop Summary: Continue running the race until all the non-player drivers have finished and we have a result
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * MainDrivingLoop (Part 4 of 5) calls FinishRace

This routine is beavering away in the background when the game displays the "PLEASE WAIT" message, after we finish qualifying or racing. It keeps running the simulation in the background until all the other drivers have finished their race or qualifying laps.
.FinishRace LDA #0 \ Set playerMoving = 0 to denote that the player's car STA playerMoving \ is stationary STA raceStarting \ Set raceStarting = 0 so the call to MoveCars below \ will move the cars round the track JSR HideAllCars \ Set all the cars to be hidden LDX currentPlayer \ Clear the current player's total race time if this is JSR ClearTotalRaceTime \ an incomplete race .fini1 JSR ProcessTime \ Increment the timers and the main loop counter, and \ set the speed for the next non-player driver LDY #0 \ Check for SHIFT and right arrow JSR ProcessShiftedKeys LDA configStop \ If bit 7 of configStop is set then we must be pressing BMI fini4 \ either SHIFT-f0 for a pit stop or SHIFT and right \ arrow to restart the game, so jump to fini4 to return \ from the subroutine \ If we get here then the race or qualifying lap has \ finished naturally, rather than being aborted, and \ this is not a pit stop \ \ So we now need to keep running the race or qualifying \ lap (albeit with everything hidden from the player) to \ get the final set of results for the whole pack JSR MoveCars \ Move the cars around the track, to keep the race going JSR ProcessOvertaking \ Process overtaking manoeuvres for the non-player \ drivers JSR SetPlayerPositions \ Set the current player's position, plus the position \ ahead and the position behind LDX #19 \ We now loop through the drivers, checking whether they \ have finished the race, so set a loop counter in X for \ the driver number LDA raceStarted \ If bit 7 of raceStarted is set then this is a race BMI fini2 \ rather than practice or qualifying, so jump to fini2 \ to work through the drivers and wait for them all to \ finish the race \ If we get here, then this is a qualifying lap CPX currentPlayer \ If the player is not driver 19, then jump to fini4 to BNE fini4 \ return from the subroutine, as there is more than one \ human player and this is not the first human to race, \ so we already have qualifying times for all the \ drivers \ If we get here, then this is a qualifying lap and the \ current player is driver 19, which means this is \ either the only human player, or the first human to \ race \ \ In any event, we need to make sure that we run the \ qualifying lap long enough to get lap times for all \ the other drivers, so we run the race for at least \ 14 * 256 main loop iterations \ \ This ensures we get lap times for all the drivers, \ even if the player decides to quit qualifying early LDA mainLoopCounterHi \ If the high byte main loop counter < 14, loop back to CMP #14 \ fini1 to keep running the race BCC fini1 RTS \ Return from the subroutine .fini2 \ If we get here, then this is a race rather than \ qualifying LDA objectStatus,X \ If bit 6 of driver X's objectStatus is set, then AND #%01000000 \ driver X has finished the race, so jump to fini3 BNE fini3 \ to check the next driver LDA numberOfLaps \ If numberOfLaps >= driver X's lap number, jump to CMP driverLapNumber,X \ fini1 to keep running the race, as driver X hasn't BCS fini1 \ finished the race yet .fini3 DEX \ Decrement the driver counter in X BPL fini2 \ Loop back until all the drivers have finished the race .fini4 RTS \ Return from the subroutine