From a208d0561ea5d9eb6a7719446014f0dc38d46ad3 Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Thu, 22 May 2025 21:11:55 +0200 Subject: [PATCH] Fix countdown bug --- TODO.txt | 5 +- game.h | 328 +++++++++++++++++++++++++++-------------------------- settings.h | 2 +- 3 files changed, 169 insertions(+), 166 deletions(-) diff --git a/TODO.txt b/TODO.txt index f582632..9f9812e 100644 --- a/TODO.txt +++ b/TODO.txt @@ -38,12 +38,11 @@ fuck issue trackers :D =========== BUGS ================= -- immediately after starting the map countdown seems to be lower (seems to - perhaps be caused by nextPhysicsFrameTime, look into it) - =========== HANDLED ============== - should drifting make a sound? NO NEED +- immediately after starting the map countdown seems to be lower (seems to + perhaps be caused by nextPhysicsFrameTime, look into it) - hitting ramps at higher speed still often bugs, try to fiddle with physics again (reshape iterations, tension, ...); seem acceptable now maybe? - in tiny resolution the sky jumps when rotating diff --git a/game.h b/game.h index 92ddf91..51d80cb 100644 --- a/game.h +++ b/game.h @@ -248,8 +248,8 @@ void _LCR_logNum(uint32_t n) #define LCR_GAME_STATE_RUN_STARTING 0x01 #define LCR_GAME_STATE_RUN 0x02 #define LCR_GAME_STATE_RUN_FINISHED 0x03 - #define LCR_GAME_STATE_LOADING 0x04 +#define LCR_GAME_STATE_LOADED 0x05 #define LCR_GAME_STATE_END 0xff @@ -1613,207 +1613,211 @@ uint8_t LCR_gameStep(uint32_t time) LCR_currentMap.targetTime = LCR_racing.replay.achievedTime; } - LCR_gameResetRun( + LCR_gameSetState(LCR_GAME_STATE_LOADED); + } + else if (LCR_game.state == LCR_GAME_STATE_LOADED) + LCR_gameResetRun( // start countdown now, loading the map lost many frames LCR_game.menu.selectedTab == 2, LCR_game.menu.selectedTab == 3); - } - - LCR_gameHandleInput(); - - // handle simulation: - while (time >= LCR_game.nextRacingTickTime) + else { - LCR_LOG2("gonna step racing engine"); + LCR_gameHandleInput(); - unsigned int input = - (LCR_game.cameraMode == LCR_CAMERA_MODE_FREE || - LCR_game.state == LCR_GAME_STATE_RUN_FINISHED) ? 0 : - ((LCR_game.keyStates[LCR_KEY_UP] ? LCR_RACING_INPUT_FORW : 0) | - (LCR_game.keyStates[LCR_KEY_RIGHT] ? LCR_RACING_INPUT_RIGHT : 0) | - (LCR_game.keyStates[LCR_KEY_DOWN] ? LCR_RACING_INPUT_BACK : 0) | - (LCR_game.keyStates[LCR_KEY_LEFT] ? LCR_RACING_INPUT_LEFT : 0)); + // handle simulation: + while (time >= LCR_game.nextRacingTickTime) + { + LCR_LOG2("gonna step racing engine"); + + unsigned int input = + (LCR_game.cameraMode == LCR_CAMERA_MODE_FREE || + LCR_game.state == LCR_GAME_STATE_RUN_FINISHED) ? 0 : + ((LCR_game.keyStates[LCR_KEY_UP] ? LCR_RACING_INPUT_FORW : 0) | + (LCR_game.keyStates[LCR_KEY_RIGHT] ? LCR_RACING_INPUT_RIGHT : 0) | + (LCR_game.keyStates[LCR_KEY_DOWN] ? LCR_RACING_INPUT_BACK : 0) | + (LCR_game.keyStates[LCR_KEY_LEFT] ? LCR_RACING_INPUT_LEFT : 0)); #ifdef LCR_FPS_GET_MS - frameTime = LCR_FPS_GET_MS; + frameTime = LCR_FPS_GET_MS; #endif - uint32_t events = paused ? 0 : LCR_racingStep(input); + uint32_t events = paused ? 0 : LCR_racingStep(input); #if LCR_SETTING_PARTICLES - LCR_rendererSetParticles(0); + LCR_rendererSetParticles(0); - if (LCR_racingGetCarSpeedUnsigned() > 60) - { - if (LCR_racingCurrentGroundMaterial() == LCR_BLOCK_MATERIAL_GRASS) - LCR_rendererSetParticles( - LCR_CONVERT_COLOR(LCR_SETTING_PARTICLE_COLOR_GRASS)); - else if (LCR_racingCurrentGroundMaterial() == LCR_BLOCK_MATERIAL_DIRT) - LCR_rendererSetParticles( - LCR_CONVERT_COLOR(LCR_SETTING_PARTICLE_COLOR_DIRT)); - else if (LCR_racingCarIsDrifting()) - LCR_rendererSetParticles( - LCR_CONVERT_COLOR(LCR_SETTING_PARTICLE_COLOR_DRIFT)); - } + if (LCR_racingGetCarSpeedUnsigned() > 60) + { + if (LCR_racingCurrentGroundMaterial() == LCR_BLOCK_MATERIAL_GRASS) + LCR_rendererSetParticles( + LCR_CONVERT_COLOR(LCR_SETTING_PARTICLE_COLOR_GRASS)); + else if (LCR_racingCurrentGroundMaterial() == LCR_BLOCK_MATERIAL_DIRT) + LCR_rendererSetParticles( + LCR_CONVERT_COLOR(LCR_SETTING_PARTICLE_COLOR_DIRT)); + else if (LCR_racingCarIsDrifting()) + LCR_rendererSetParticles( + LCR_CONVERT_COLOR(LCR_SETTING_PARTICLE_COLOR_DRIFT)); + } #endif #ifdef LCR_FPS_GET_MS - LCR_game.physicsFrameMS += LCR_FPS_GET_MS - frameTime; - LCR_game.physicsFramesMeasured++; + LCR_game.physicsFrameMS += LCR_FPS_GET_MS - frameTime; + LCR_game.physicsFramesMeasured++; #endif - if (events & LCR_RACING_EVENT_CP_TAKEN) - { - int carBlock[3]; - char str[10]; - - LCR_LOG1("CP taken"); - LCR_racingGetCarBlockCoords(carBlock); - LCR_rendererMarkTakenCP(carBlock[0],carBlock[1],carBlock[2]); - LCR_audioPlaySound(LCR_SOUND_CLICK); - LCR_gameTimeToStr(LCR_timeTicksToMS(LCR_game.runTime),str); - LCR_gamePopupMessage(str); - } - else if (events & LCR_RACING_EVENT_FINISHED && - LCR_game.state != LCR_GAME_STATE_RUN_FINISHED) - { - LCR_LOG1("finished, time:"); - LCR_LOG1_NUM(LCR_game.runTime); - - if (LCR_game.runTime <= LCR_currentMap.targetTime && - !LCR_racing.playingReplay) + if (events & LCR_RACING_EVENT_CP_TAKEN) { - LCR_gameSaveReplay(); + int carBlock[3]; + char str[10]; - if (!LCR_game.mapBeaten && !LCR_game.ghost.active && + LCR_LOG1("CP taken"); + LCR_racingGetCarBlockCoords(carBlock); + LCR_rendererMarkTakenCP(carBlock[0],carBlock[1],carBlock[2]); + LCR_audioPlaySound(LCR_SOUND_CLICK); + LCR_gameTimeToStr(LCR_timeTicksToMS(LCR_game.runTime),str); + LCR_gamePopupMessage(str); + } + else if (events & LCR_RACING_EVENT_FINISHED && + LCR_game.state != LCR_GAME_STATE_RUN_FINISHED) + { + LCR_LOG1("finished, time:"); + LCR_LOG1_NUM(LCR_game.runTime); + + if (LCR_game.runTime <= LCR_currentMap.targetTime && !LCR_racing.playingReplay) { - LCR_LOG1("map beaten"); - LCR_game.mapBeaten = 1; + LCR_gameSaveReplay(); - // record that the map is beaten in the data file: - _LCR_gameDataCharWrite(LCR_DATA_FILE_SEPARATOR); - _LCR_gameDataCharWrite('B'); + if (!LCR_game.mapBeaten && !LCR_game.ghost.active && + !LCR_racing.playingReplay) + { + LCR_LOG1("map beaten"); + LCR_game.mapBeaten = 1; - for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i) - if (LCR_currentMap.name[i]) - _LCR_gameDataCharWrite(LCR_currentMap.name[i]); - else - break; + // record that the map is beaten in the data file: + _LCR_gameDataCharWrite(LCR_DATA_FILE_SEPARATOR); + _LCR_gameDataCharWrite('B'); - _LCR_gameDataCharWrite(';'); - _LCR_gameDataCharWrite('\n'); + for (int i = 0; i < LCR_MAP_NAME_MAX_LEN; ++i) + if (LCR_currentMap.name[i]) + _LCR_gameDataCharWrite(LCR_currentMap.name[i]); + else + break; + + _LCR_gameDataCharWrite(';'); + _LCR_gameDataCharWrite('\n'); + } } + + LCR_audioPlaySound(LCR_SOUND_CLICK); + LCR_gameSetState(LCR_GAME_STATE_RUN_FINISHED); } - LCR_audioPlaySound(LCR_SOUND_CLICK); - LCR_gameSetState(LCR_GAME_STATE_RUN_FINISHED); + if (events & LCR_RACING_EVENT_CRASH_SMALL) + { + LCR_audioPlaySound(LCR_SOUND_CRASH_SMALL); + LCR_LOG2("crash (small)"); + } + else if (events & LCR_RACING_EVENT_CRASH_BIG) + { + LCR_audioPlaySound(LCR_SOUND_CRASH_BIG); + LCR_LOG2("crash (big)"); + } + else if (events & LCR_RACING_EVENT_ACCELERATOR) + LCR_audioPlaySoundIfFree(LCR_SOUND_ACCELERATOR); + else if (events & LCR_RACING_EVENT_FAN) + LCR_audioPlaySoundIfFree(LCR_SOUND_FAN); + + int engineIntensity = LCR_carSpeedKMH() * 2; + + LCR_audioSetEngineIntensity(paused ? 0 : + (engineIntensity < 256 ? engineIntensity : 255)); + + if (LCR_game.state != LCR_GAME_STATE_RUN_FINISHED) + LCR_game.runTime = LCR_racing.tick; + + LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS; } - if (events & LCR_RACING_EVENT_CRASH_SMALL) - { - LCR_audioPlaySound(LCR_SOUND_CRASH_SMALL); - LCR_LOG2("crash (small)"); - } - else if (events & LCR_RACING_EVENT_CRASH_BIG) - { - LCR_audioPlaySound(LCR_SOUND_CRASH_BIG); - LCR_LOG2("crash (big)"); - } - else if (events & LCR_RACING_EVENT_ACCELERATOR) - LCR_audioPlaySoundIfFree(LCR_SOUND_ACCELERATOR); - else if (events & LCR_RACING_EVENT_FAN) - LCR_audioPlaySoundIfFree(LCR_SOUND_FAN); + sleep = (3 * (LCR_game.nextRacingTickTime - time)) / 4; - int engineIntensity = LCR_carSpeedKMH() * 2; - - LCR_audioSetEngineIntensity(paused ? 0 : - (engineIntensity < 256 ? engineIntensity : 255)); - - if (LCR_game.state != LCR_GAME_STATE_RUN_FINISHED) - LCR_game.runTime = LCR_racing.tick; - - LCR_game.nextRacingTickTime += LCR_RACING_TICK_MS; - } - - sleep = (3 * (LCR_game.nextRacingTickTime - time)) / 4; - - // handle rendering: - if (time >= LCR_game.nextRenderFrameTime || - LCR_game.state == LCR_GAME_STATE_LOADING) - { - LCR_LOG2("rendering next frame"); - - while (time >= LCR_game.nextRenderFrameTime) - LCR_game.nextRenderFrameTime += 1000 / LCR_SETTING_FPS; - -#ifdef LCR_FPS_GET_MS - frameTime = LCR_FPS_GET_MS; -#endif - - if ((LCR_game.state == LCR_GAME_STATE_MENU) || + // handle rendering: + if (time >= LCR_game.nextRenderFrameTime || LCR_game.state == LCR_GAME_STATE_LOADING) - LCR_rendererDrawMenu(LCR_texts[LCR_TEXTS_TABS - + LCR_game.menu.selectedTab],LCR_game.menu.itemNamePtrs, - LCR_game.menu.itemCount,LCR_game.menu.selectedItem, - (LCR_game.menu.selectedTab > 0) && - LCR_game.dataFile.firstItemIndex + LCR_DATA_ITEM_CHUNK - < LCR_game.dataFile.itemsTotal); - else - LCR_gameDraw3DView(); - - if (LCR_game.popupCountdown) { - LCR_gameDrawPopupMessage(); - LCR_game.popupCountdown--; + LCR_LOG2("rendering next frame"); + + while (time >= LCR_game.nextRenderFrameTime) + LCR_game.nextRenderFrameTime += 1000 / LCR_SETTING_FPS; + +#ifdef LCR_FPS_GET_MS + frameTime = LCR_FPS_GET_MS; +#endif + + if ((LCR_game.state == LCR_GAME_STATE_MENU) || + LCR_game.state == LCR_GAME_STATE_LOADING) + LCR_rendererDrawMenu(LCR_texts[LCR_TEXTS_TABS + + LCR_game.menu.selectedTab],LCR_game.menu.itemNamePtrs, + LCR_game.menu.itemCount,LCR_game.menu.selectedItem, + (LCR_game.menu.selectedTab > 0) && + LCR_game.dataFile.firstItemIndex + LCR_DATA_ITEM_CHUNK + < LCR_game.dataFile.itemsTotal); + else + LCR_gameDraw3DView(); + + if (LCR_game.popupCountdown) + { + LCR_gameDrawPopupMessage(); + LCR_game.popupCountdown--; + } + +#ifdef LCR_FPS_GET_MS + LCR_game.renderFrameMS += LCR_FPS_GET_MS - frameTime; + LCR_game.renderFramesMeasured++; +#endif + } + else + { + uint32_t tmp = (3 * (LCR_game.nextRenderFrameTime - time)) / 4; + sleep = tmp < sleep ? tmp : sleep; } -#ifdef LCR_FPS_GET_MS - LCR_game.renderFrameMS += LCR_FPS_GET_MS - frameTime; - LCR_game.renderFramesMeasured++; -#endif - } - else - { - uint32_t tmp = (3 * (LCR_game.nextRenderFrameTime - time)) / 4; - sleep = tmp < sleep ? tmp : sleep; - } + if (LCR_game.state == LCR_GAME_STATE_LOADING) + { + // show the "loading" screen - if (LCR_game.state == LCR_GAME_STATE_LOADING) - { - // show the "loading" screen + LCR_gamePopupMessage(LCR_texts[LCR_TEXTS_LOADING]); + LCR_game.popupCountdown = 2; + LCR_gameDrawPopupMessage(); + } - LCR_gamePopupMessage(LCR_texts[LCR_TEXTS_LOADING]); - LCR_game.popupCountdown = 2; - LCR_gameDrawPopupMessage(); - } + if (sleep) + LCR_sleep(sleep); + else + { + LCR_LOG2("can't sleep"); + } - if (sleep) - LCR_sleep(sleep); - else - { - LCR_LOG2("can't sleep"); - } - - LCR_game.frame++; - LCR_LOG2("game step (end)"); + LCR_game.frame++; + LCR_LOG2("game step (end)"); #ifdef LCR_FPS_GET_MS - if (LCR_game.renderFramesMeasured >= 64) - { - LCR_LOG0("us/frame (render):"); - LCR_LOG0_NUM((LCR_game.renderFrameMS * 1000) / 64); - LCR_game.renderFramesMeasured = 0; - LCR_game.renderFrameMS = 0; - } + if (LCR_game.renderFramesMeasured >= 64) + { + LCR_LOG0("us/frame (render):"); + LCR_LOG0_NUM((LCR_game.renderFrameMS * 1000) / 64); + LCR_game.renderFramesMeasured = 0; + LCR_game.renderFrameMS = 0; + } - if (LCR_game.physicsFramesMeasured >= 64) - { - LCR_LOG0("us/frame (physics):"); - LCR_LOG0_NUM((LCR_game.physicsFrameMS * 1000) / 64); - LCR_game.physicsFramesMeasured = 0; - LCR_game.physicsFrameMS = 0; - } + if (LCR_game.physicsFramesMeasured >= 64) + { + LCR_LOG0("us/frame (physics):"); + LCR_LOG0_NUM((LCR_game.physicsFrameMS * 1000) / 64); + LCR_game.physicsFramesMeasured = 0; + LCR_game.physicsFrameMS = 0; + } #endif + } return 1; } diff --git a/settings.h b/settings.h index 0c94b00..63fc09c 100644 --- a/settings.h +++ b/settings.h @@ -191,7 +191,7 @@ #ifndef LCR_SETTING_COUNTDOWN_SECONDS /** Run start countdown length in seconds. */ - #define LCR_SETTING_COUNTDOWN_SECONDS 1 // for release make 3 + #define LCR_SETTING_COUNTDOWN_SECONDS 3 //1 // for release make 3 #endif #ifndef LCR_SETTING_MAP_CHUNK_RELOAD_INTERVAL