From f646069ca3baab4021e85de2ba19ce5f57e204fd Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Thu, 15 Dec 2022 17:16:06 +0100 Subject: bullet sync fix --- build/zombies.exe | Bin 1733882 -> 1734476 bytes include/bullets.h | 1 + src/bullets.c | 8 ++++++-- src/game.c | 32 +++++++++++++++++++++----------- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/build/zombies.exe b/build/zombies.exe index 47da708..e786e24 100644 Binary files a/build/zombies.exe and b/build/zombies.exe differ diff --git a/include/bullets.h b/include/bullets.h index be3f466..1828f38 100644 --- a/include/bullets.h +++ b/include/bullets.h @@ -20,6 +20,7 @@ typedef struct t_bullet { bullet bullets[500] = {0}; int max_bullets = 500; +void clear_bullets(); void shoot(platform_window* window, u32 id, float dirx, float diry); void draw_bullets(platform_window* window); diff --git a/src/bullets.c b/src/bullets.c index 4431eff..7397e9f 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -180,6 +180,12 @@ static bool check_if_bullet_collided_with_ground(bullet *b, platform_window* win return false; } +void clear_bullets() { + for (int i = 0; i < max_bullets; i++) { + bullets[i].active = false; + } +} + void update_bullets(platform_window* window) { for (int i = 0; i < max_bullets; i++) { bullet b = bullets[i]; @@ -229,7 +235,5 @@ void draw_bullets(platform_window* window) { float bullet_render_y_end = b.endy*info.tile_height - (b.position.z*info.px_raised_per_h); renderer->render_line(bullet_render_x, bullet_render_y, bullet_render_x_end, bullet_render_y_end, 5, rgb(255, 51, 51)); - - bullets[i].active = false; } } \ No newline at end of file diff --git a/src/game.c b/src/game.c index 9d70d33..875a622 100644 --- a/src/game.c +++ b/src/game.c @@ -113,6 +113,8 @@ static void set_ping_for_player(protocol_generic_message* msg) { float update_timer = 0.0f; void update_server(platform_window* window) { + clear_bullets(); + for (int i = 0; i < messages_received_on_server.length; i++) { protocol_generic_message* msg = *(protocol_generic_message**)array_at(&messages_received_on_server, i); set_ping_for_player(msg); @@ -157,15 +159,10 @@ void update_server(platform_window* window) { update_players_server(); update_zombies_server(window); - if (update_timer > 0.0f) { - broadcast_to_clients(create_protocol_user_list()); - broadcast_to_clients(create_protocol_zombie_list()); - broadcast_to_clients(create_protocol_bullets_list()); - broadcast_to_clients(create_protocol_drop_list()); - update_timer = 0.0f; - } - - update_timer += update_delta; + broadcast_to_clients(create_protocol_user_list()); + broadcast_to_clients(create_protocol_zombie_list()); + broadcast_to_clients(create_protocol_bullets_list()); + broadcast_to_clients(create_protocol_drop_list()); } static void apply_user_list(protocol_user_list* msg_players) { @@ -184,7 +181,20 @@ static void apply_user_list(protocol_user_list* msg_players) { } } +static void load_bullets_into_existing_list(protocol_bullets_list* msg_bullets) { + for (int i = 0; i < max_bullets; i++) { + if (bullets[i].active) continue; + for (int x = 0; x < max_bullets; x++) { + if (!msg_bullets->bullets[x].active) continue; + bullets[i] = msg_bullets->bullets[x]; + msg_bullets->bullets[x].active = false; + } + } +} + void update_client(platform_window* window) { + clear_bullets(); + for (int i = 0; i < messages_received_on_client.length; i++) { protocol_generic_client_message* msg = *(protocol_generic_client_message**)array_at(&messages_received_on_client, i); @@ -194,7 +204,7 @@ void update_client(platform_window* window) { protocol_get_id_downstream* msg_id = (protocol_get_id_downstream*)msg; player_id = msg_id->id; global_state.network_state = CONNECTED; - log_infox("Id received: %d", player_id); + log_infox("Id received: %u", player_id); } break; case MESSAGE_USER_LIST: { @@ -212,7 +222,7 @@ void update_client(platform_window* window) { case MESSAGE_BULLET_LIST: { if (global_state.server) break; // bullets are simulated on server so dont overwrite data. protocol_bullets_list* msg_bullets = (protocol_bullets_list*)msg; - memcpy(bullets, msg_bullets->bullets, sizeof(bullets)); + load_bullets_into_existing_list(msg_bullets); } break; case MESSAGE_DROP_LIST: { -- cgit v1.2.3-70-g09d2