From d257b99b1582905e9aba382166b7dcf219da8de0 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Wed, 23 Nov 2022 00:05:03 -0800 Subject: [PATCH] Clamp reprediction on time spent computing --- main.c | 11 +++++------ types.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 6310bfd..85c72b9 100644 --- a/main.c +++ b/main.c @@ -1366,15 +1366,15 @@ static void frame(void) // re-predict the inputs float time_to_repredict = (float)difference; + uint64_t start_prediction_time = stm_now(); if (time_to_repredict > 0.0f) { - if (time_to_repredict > MAX_REPREDICTION_TIME) - { - Log("Can't repredict all of %f, capping!\n", time_to_repredict); - time_to_repredict = MAX_REPREDICTION_TIME; - } while (time_to_repredict > TIMESTEP) { + if (stm_ms(stm_diff(stm_now(), start_prediction_time)) > MAX_MS_SPENT_REPREDICTING) + { + Log("Reprediction took longer than %f milliseconds", MAX_MS_SPENT_REPREDICTING); + } QUEUE_ITER(&input_queue, cur_header) { InputFrame *cur = (InputFrame *)cur_header->data; @@ -1387,7 +1387,6 @@ static void frame(void) process(&gs, TIMESTEP); time_to_repredict -= TIMESTEP; } - assert(time_to_repredict <= TIMESTEP); process(&gs, time_to_repredict); time_to_repredict = 0.0f; } diff --git a/types.h b/types.h index e6e941e..7ca9e1e 100644 --- a/types.h +++ b/types.h @@ -73,7 +73,7 @@ #define VOIP_DISTANCE_WHEN_CANT_HEAR (VISION_RADIUS * 0.8f) // multiplayer -#define MAX_REPREDICTION_TIME (TIMESTEP * 50.0f) +#define MAX_MS_SPENT_REPREDICTING 30.0f #define TIME_BETWEEN_SEND_GAMESTATE (1.0f / 20.0f) #define TIME_BETWEEN_INPUT_PACKETS (1.0f / 20.0f) #define TIMESTEP (1.0f / 60.0f) // server required to simulate at this, defines what tick the game is on