Continue replays

This commit is contained in:
Miloslav Ciz 2025-01-13 15:20:30 +01:00
parent 0487a87ae5
commit 17c182f474
2 changed files with 32 additions and 15 deletions

11
game.h
View file

@ -278,13 +278,13 @@ LCR_GameUnit LCR_carSpeedKMH(void)
LCR_RACING_FPS) / (8 * LCR_GAME_UNIT); LCR_RACING_FPS) / (8 * LCR_GAME_UNIT);
} }
void LCR_gameResetRun(void) void LCR_gameResetRun(uint8_t replay)
{ {
LCR_GameUnit carTransform[6]; LCR_GameUnit carTransform[6];
LCR_LOG0("resetting run"); LCR_LOG0("resetting run");
LCR_mapReset(); LCR_mapReset();
LCR_racingRestart(); LCR_racingRestart(replay);
LCR_rendererUnmarkCPs(); LCR_rendererUnmarkCPs();
LCR_racingGetCarTransform(carTransform,carTransform + 3,0); LCR_racingGetCarTransform(carTransform,carTransform + 3,0);
LCR_rendererSetCarTransform(carTransform,carTransform + 3); LCR_rendererSetCarTransform(carTransform,carTransform + 3);
@ -754,7 +754,8 @@ void LCR_gameHandleInput(void)
case LCR_GAME_STATE_RUN_FINISHED: case LCR_GAME_STATE_RUN_FINISHED:
if (LCR_game.keyStates[LCR_KEY_A] == 1) if (LCR_game.keyStates[LCR_KEY_A] == 1)
LCR_gameResetRun(); //LCR_gameResetRun(LCR_racing.playingReplay);
LCR_gameResetRun(1);
break; break;
@ -806,7 +807,7 @@ void LCR_gameHandleInput(void)
LCR_rendererMoveCamera(offsets,offsets + 3); LCR_rendererMoveCamera(offsets,offsets + 3);
} }
else if (LCR_game.keyStates[LCR_KEY_A] == 1) else if (LCR_game.keyStates[LCR_KEY_A] == 1)
LCR_gameResetRun(); LCR_gameResetRun(LCR_racing.playingReplay);
break; break;
} }
@ -837,7 +838,7 @@ uint8_t LCR_gameStep(uint32_t time)
if (LCR_game.state == LCR_GAME_STATE_LOADING_MAP) if (LCR_game.state == LCR_GAME_STATE_LOADING_MAP)
{ {
LCR_rendererLoadMap(); LCR_rendererLoadMap();
LCR_gameResetRun(); LCR_gameResetRun(LCR_racing.playingReplay);
} }
LCR_gameHandleInput(); LCR_gameHandleInput();

View file

@ -101,6 +101,8 @@ struct
// for fixing physics bugs: // for fixing physics bugs:
TPE_Vec3 carOKPositions[LCR_CAR_JOINTS]; TPE_Vec3 carOKPositions[LCR_CAR_JOINTS];
uint8_t carNotOKCount; uint8_t carNotOKCount;
uint8_t playingReplay;
} LCR_racing; } LCR_racing;
struct struct
@ -161,7 +163,7 @@ uint8_t LCR_replayGetNextInput(void)
LCR_replay.currentFrame++; LCR_replay.currentFrame++;
return LCR_replay.currentEvent ? 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) int LCR_replayHasFinished(void)
@ -182,10 +184,10 @@ int LCR_replayRecordEvent(uint32_t frame, uint8_t input)
uint8_t previousInput = 0; uint8_t previousInput = 0;
uint32_t previousFrame = 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; previousInput = LCR_replay.events[i] & 0x0f;
previousFrame = LCR_replay.events[LCR_replay.eventCount - 1] >> 4; previousFrame += LCR_replay.events[i] >> 4;
} }
if (input == previousInput) 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) while (frame > 4095 && LCR_replay.eventCount < LCR_SETTING_REPLAY_MAX_SIZE)
{ {
// add intermediate events // add intermediate events
frame -= 4095; frame -= 4095;
previousFrame += 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) if (LCR_replay.eventCount >= LCR_SETTING_REPLAY_MAX_SIZE)
return 0; return 0;
LCR_replay.events[LCR_replay.eventCount] = (frame << 4) | input; LCR_replay.events[LCR_replay.eventCount] = (frame << 4) | (input & 0x0f);
LCR_replay.eventCount++; LCR_replay.eventCount++;
#endif #endif
@ -685,7 +686,7 @@ void _LCR_racingUpdateCarPosRot(void)
/** /**
Initializes new run. Initializes new run.
*/ */
void LCR_racingRestart(void) void LCR_racingRestart(uint8_t replay)
{ {
LCR_LOG0("restarting race"); LCR_LOG0("restarting race");
LCR_mapReset(); LCR_mapReset();
@ -693,6 +694,8 @@ void LCR_racingRestart(void)
LCR_racing.tick = 0; LCR_racing.tick = 0;
LCR_racing.fanForce = 0; LCR_racing.fanForce = 0;
LCR_racing.playingReplay = replay;
TPE_bodyActivate(&(LCR_racing.carBody)); TPE_bodyActivate(&(LCR_racing.carBody));
LCR_racing.wheelCollisions = 0; LCR_racing.wheelCollisions = 0;
@ -766,8 +769,6 @@ void LCR_racingRestart(void)
LCR_racing.carPositions[1] = LCR_racing.carPositions[0]; LCR_racing.carPositions[1] = LCR_racing.carPositions[0];
LCR_racing.carRotations[1] = LCR_racing.carRotations[0]; LCR_racing.carRotations[1] = LCR_racing.carRotations[0];
LCR_replayInitRecording();
} }
/** /**
@ -780,6 +781,7 @@ void LCR_racingInit(void)
TPE_worldInit(&(LCR_racing.physicsWorld), TPE_worldInit(&(LCR_racing.physicsWorld),
&(LCR_racing.carBody),1,_LCR_racingEnvironmentFunction); &(LCR_racing.carBody),1,_LCR_racingEnvironmentFunction);
LCR_racing.playingReplay = 0;
LCR_racing.physicsWorld.collisionCallback = _LCR_racingCollisionHandler; LCR_racing.physicsWorld.collisionCallback = _LCR_racingCollisionHandler;
} }
@ -935,7 +937,21 @@ uint32_t LCR_racingStep(unsigned int input)
uint8_t onAccel = 0; // standing on accelerator? uint8_t onAccel = 0; // standing on accelerator?
int groundBlockIndex = -1; 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( carForw = TPE_vec3Normalized(TPE_vec3Plus(
TPE_vec3Minus(LCR_racing.carBody.joints[0].position, TPE_vec3Minus(LCR_racing.carBody.joints[0].position,