diff --git a/game.h b/game.h index 9c54624..63b4e1a 100644 --- a/game.h +++ b/game.h @@ -278,13 +278,13 @@ LCR_GameUnit LCR_carSpeedKMH(void) LCR_RACING_FPS) / (8 * LCR_GAME_UNIT); } -void LCR_gameResetRun(void) +void LCR_gameResetRun(uint8_t replay) { LCR_GameUnit carTransform[6]; LCR_LOG0("resetting run"); LCR_mapReset(); - LCR_racingRestart(); + LCR_racingRestart(replay); LCR_rendererUnmarkCPs(); LCR_racingGetCarTransform(carTransform,carTransform + 3,0); LCR_rendererSetCarTransform(carTransform,carTransform + 3); @@ -754,7 +754,8 @@ void LCR_gameHandleInput(void) case LCR_GAME_STATE_RUN_FINISHED: if (LCR_game.keyStates[LCR_KEY_A] == 1) - LCR_gameResetRun(); + //LCR_gameResetRun(LCR_racing.playingReplay); + LCR_gameResetRun(1); break; @@ -806,7 +807,7 @@ void LCR_gameHandleInput(void) LCR_rendererMoveCamera(offsets,offsets + 3); } else if (LCR_game.keyStates[LCR_KEY_A] == 1) - LCR_gameResetRun(); + LCR_gameResetRun(LCR_racing.playingReplay); break; } @@ -837,7 +838,7 @@ uint8_t LCR_gameStep(uint32_t time) if (LCR_game.state == LCR_GAME_STATE_LOADING_MAP) { LCR_rendererLoadMap(); - LCR_gameResetRun(); + LCR_gameResetRun(LCR_racing.playingReplay); } LCR_gameHandleInput(); diff --git a/racing.h b/racing.h index 39bd7b7..cec6ead 100644 --- a/racing.h +++ b/racing.h @@ -101,6 +101,8 @@ struct // for fixing physics bugs: TPE_Vec3 carOKPositions[LCR_CAR_JOINTS]; uint8_t carNotOKCount; + + uint8_t playingReplay; } LCR_racing; struct @@ -161,7 +163,7 @@ uint8_t LCR_replayGetNextInput(void) LCR_replay.currentFrame++; return LCR_replay.currentEvent ? - (LCR_replay.events[LCR_replay.currentEvent - 1] & 0xff) : 0; + (LCR_replay.events[LCR_replay.currentEvent - 1] & 0x0f) : 0; } int LCR_replayHasFinished(void) @@ -182,10 +184,10 @@ int LCR_replayRecordEvent(uint32_t frame, uint8_t input) uint8_t previousInput = 0; uint32_t previousFrame = 0; - if (LCR_replay.eventCount) + for (int i = 0; i < LCR_replay.eventCount; ++i) { - previousInput = LCR_replay.events[LCR_replay.eventCount - 1] & 0x0f; - previousFrame = LCR_replay.events[LCR_replay.eventCount - 1] >> 4; + previousInput = LCR_replay.events[i] & 0x0f; + previousFrame += LCR_replay.events[i] >> 4; } if (input == previousInput) @@ -199,7 +201,6 @@ int LCR_replayRecordEvent(uint32_t frame, uint8_t input) while (frame > 4095 && LCR_replay.eventCount < LCR_SETTING_REPLAY_MAX_SIZE) { // add intermediate events - frame -= 4095; previousFrame += 4095; @@ -211,7 +212,7 @@ int LCR_replayRecordEvent(uint32_t frame, uint8_t input) if (LCR_replay.eventCount >= LCR_SETTING_REPLAY_MAX_SIZE) return 0; - LCR_replay.events[LCR_replay.eventCount] = (frame << 4) | input; + LCR_replay.events[LCR_replay.eventCount] = (frame << 4) | (input & 0x0f); LCR_replay.eventCount++; #endif @@ -685,7 +686,7 @@ void _LCR_racingUpdateCarPosRot(void) /** Initializes new run. */ -void LCR_racingRestart(void) +void LCR_racingRestart(uint8_t replay) { LCR_LOG0("restarting race"); LCR_mapReset(); @@ -693,6 +694,8 @@ void LCR_racingRestart(void) LCR_racing.tick = 0; LCR_racing.fanForce = 0; + LCR_racing.playingReplay = replay; + TPE_bodyActivate(&(LCR_racing.carBody)); LCR_racing.wheelCollisions = 0; @@ -766,8 +769,6 @@ void LCR_racingRestart(void) LCR_racing.carPositions[1] = LCR_racing.carPositions[0]; LCR_racing.carRotations[1] = LCR_racing.carRotations[0]; - - LCR_replayInitRecording(); } /** @@ -780,6 +781,7 @@ void LCR_racingInit(void) TPE_worldInit(&(LCR_racing.physicsWorld), &(LCR_racing.carBody),1,_LCR_racingEnvironmentFunction); + LCR_racing.playingReplay = 0; LCR_racing.physicsWorld.collisionCallback = _LCR_racingCollisionHandler; } @@ -935,7 +937,21 @@ uint32_t LCR_racingStep(unsigned int input) uint8_t onAccel = 0; // standing on accelerator? int groundBlockIndex = -1; - LCR_replayRecordEvent(LCR_racing.tick,input); + if (LCR_racing.playingReplay) + { + if (LCR_racing.tick == 0) + LCR_replayInitPlaying(); + + input = LCR_replayGetNextInput(); + + } + else + { + if (LCR_racing.tick == 0) + LCR_replayInitRecording(); + + LCR_replayRecordEvent(LCR_racing.tick,input); + } carForw = TPE_vec3Normalized(TPE_vec3Plus( TPE_vec3Minus(LCR_racing.carBody.joints[0].position,