From: Lukas Krickl Date: Mon, 2 Mar 2026 11:37:59 +0000 (+0100) Subject: commands: Commands are not a ring buffer anymore. X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=182bc6fa04c7edbcd5401e17fc88eeb34bfe5be6;p=lrts%2F.git commands: Commands are not a ring buffer anymore. Commands now will need to be sorted before they are executed. This will ensure consistent execution. --- diff --git a/src/t_command.c b/src/t_command.c index 885400e..29dd1b9 100644 --- a/src/t_command.c +++ b/src/t_command.c @@ -1,26 +1,44 @@ #include "t_command.h" +#include "u_defs.h" struct t_command_queue t_command_queue; u32 t_command_queue_process(void) { + u32 i = 0; + struct t_command *c; + struct lrts_state *state = lrts_state(); + + /* TODO: sort command queue by player and tick */ + + /* run all commands until type is 0 + * or the ticks do not match */ + for (i = 0; i < t_command_queue.idx; i++) { + c = &t_command_queue.buffer[i]; + if (c->type == T_COMMAND_NONE || c->run_at_tick > state->tick_count) { + break; + } + t_command_exec(c); + } + return 0; } lrts_bool t_command_exec(struct t_command *c) { - LRTS_UNUSED(c); + if (c->type == T_COMMAND_NONE) { + u_log(U_LOG_WARN, "Command of type none was passed to exec\n"); + return LRTS_TRUE; + } + + c->type = T_COMMAND_NONE; return LRTS_TRUE; } void t_command_push(struct t_command c) { - if (t_command_queue.write_idx >= T_COMMANDS_MAX) { - t_command_queue.write_idx = 0; - } - - if (t_command_queue.buffer[t_command_queue.write_idx].type != T_COMMAND_NONE) { + if (t_command_queue.idx >= T_COMMANDS_MAX) { u_log(U_LOG_CRIT, "Command queue overflow!\n"); p_exit(P_EXIT_FAIL); return; } - t_command_queue.buffer[t_command_queue.write_idx++] = c; + t_command_queue.buffer[t_command_queue.idx++] = c; } diff --git a/src/t_command.h b/src/t_command.h index 5bb9d12..ad35c19 100644 --- a/src/t_command.h +++ b/src/t_command.h @@ -12,6 +12,8 @@ enum t_command_type { T_COMMAND_NONE = 0, + /* no-op command */ + T_COMMAND_NOOP, /* moves camera in the direction of a * provided vector */ @@ -30,18 +32,18 @@ struct t_command { enum t_command_type type; enum t_command_flags flags; u32 run_at_tick; + u8 player; union t_command_data d; }; /** * This is a comamnd buffer * It wraps around and keeps track of the current command - * This is a ring buffer. If the current write is already allocated (not type NONE - * the program will exit!) + * The command queue should always be sorted by command type index, player number and ticks + * to ensure consistent execution */ struct t_command_queue { - u16 write_idx; - u16 read_idx; + u16 idx; struct t_command buffer[T_COMMANDS_MAX]; }; diff --git a/src/tests/t_command.c b/src/tests/t_command.c index 2d6a033..99ccef6 100644 --- a/src/tests/t_command.c +++ b/src/tests/t_command.c @@ -7,14 +7,14 @@ int test_t_command_push(void) { t.type = T_COMMAND_MOVE_CAMERA; t_command_push(t); - T_ASSERT(t_command_queue.write_idx == 1, ("write index moved\n")); + T_ASSERT(t_command_queue.idx == 1, ("write index moved\n")); T_ASSERT(t_command_queue.buffer[0].type == T_COMMAND_MOVE_CAMERA, ("command written\n")); for (i = 0; i < T_COMMANDS_MAX+1; i++) { t_command_push(t); } - T_ASSERT(t_command_queue.write_idx == 0, ("write index out of bounds")); + T_ASSERT(t_command_queue.idx == T_COMMANDS_MAX, ("write index out of bounds\n")); return 0; }