From e917400634a535c6c14afc4e37a759f7c777b73a Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 18 Dec 2022 20:17:16 +0100 Subject: server tick rate --- build.zip | Bin 1184740 -> 0 bytes build/zombies.exe | Bin 1753517 -> 1755593 bytes include/drops.h | 2 +- include/game.h | 2 ++ include/map.h | 2 +- include/wall_item.h | 2 +- src/bullets.c | 2 +- src/drops.c | 5 +++-- src/game.c | 20 +++++++++++--------- src/objects.c | 2 +- src/players.c | 2 +- src/protocol.c | 23 ++++++++++++++++++++--- src/wall_item.c | 4 ++-- src/zombies.c | 11 ++++++----- 14 files changed, 50 insertions(+), 27 deletions(-) delete mode 100644 build.zip diff --git a/build.zip b/build.zip deleted file mode 100644 index e7080d9..0000000 Binary files a/build.zip and /dev/null differ diff --git a/build/zombies.exe b/build/zombies.exe index 713c94d..8469dd2 100644 Binary files a/build/zombies.exe and b/build/zombies.exe differ diff --git a/include/drops.h b/include/drops.h index aa48e24..91ec03e 100644 --- a/include/drops.h +++ b/include/drops.h @@ -28,7 +28,7 @@ drop drops[MAX_DROPS] = {0}; #define DROP_MAX_DURATION (4.0f) #define DROP_FADE_TIME (2.0f) -void update_drops(); +void update_drops_server(); void draw_drops(platform_window* window); void spawn_drop(vec3f pos); diff --git a/include/game.h b/include/game.h index faf2bc9..cc9d0cd 100644 --- a/include/game.h +++ b/include/game.h @@ -7,6 +7,8 @@ #include "../include/players.h" #include "../include/game.h" +#define SERVER_TICK_RATE (1.0f/60.0f) + typedef enum t_game_state { GAMESTATE_IDLE, GAMESTATE_LOADING_MAP, diff --git a/include/map.h b/include/map.h index 3bc8273..26bc749 100644 --- a/include/map.h +++ b/include/map.h @@ -22,7 +22,7 @@ typedef struct t_tile { vec2f br; } tile; -#define MAP_SIZE_X 20 +#define MAP_SIZE_X 40 #define MAP_SIZE_Y 20 tile map_loaded[MAP_SIZE_Y][MAP_SIZE_X]; diff --git a/include/wall_item.h b/include/wall_item.h index 3b896bd..4d437e0 100644 --- a/include/wall_item.h +++ b/include/wall_item.h @@ -27,7 +27,7 @@ typedef struct t_wall_item { #define MAX_WALLITEMS (20) wall_item wallitems[MAX_WALLITEMS] = {0}; -void update_wallitems(); +void update_wallitems_server(); void draw_wallitems(platform_window* window); void create_wallitem(vec3f position, wall_item_type item, wall_item_data data); diff --git a/src/bullets.c b/src/bullets.c index ed9aab4..fea2a90 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -190,7 +190,7 @@ void clear_bullets() { } } -void update_bullets(platform_window* window) { +void update_bullets_server(platform_window* window) { for (int i = 0; i < max_bullets; i++) { bullet b = bullets[i]; if (!b.active) continue; diff --git a/src/drops.c b/src/drops.c index 59bfcc6..95d1af7 100644 --- a/src/drops.c +++ b/src/drops.c @@ -13,14 +13,14 @@ void handle_drop_pickup(player* p, drop* d) { d->active = false; } -void update_drops() { +void update_drops_server() { #define MAX_HEIGHT_DIFF (0.3f) for (int i = 0; i < MAX_DROPS; i++) { drop b = drops[i]; if (!b.active) continue; - drops[i].time_active += update_delta; + drops[i].time_active += SERVER_TICK_RATE; drops[i].position.z = MAX_HEIGHT_DIFF * sin (2 * M_PI * 0.5f * (drops[i].time_active) + 0) + b.start_h; for (int x = 0; x < MAX_PLAYERS; x++) { @@ -49,6 +49,7 @@ void draw_drops(platform_window* window) { if (b.time_active >= DROP_MAX_DURATION - DROP_FADE_TIME) { alpha = 255 - ((b.time_active - (DROP_MAX_DURATION - DROP_FADE_TIME)) / DROP_FADE_TIME)*255; } + if (alpha < 0) alpha = 0; b.position.z = b.start_h; b.size.z = 0.0f; diff --git a/src/game.c b/src/game.c index b426761..d3cbb57 100644 --- a/src/game.c +++ b/src/game.c @@ -59,8 +59,10 @@ void load_map() { outgoing_allocator = create_allocator(MAX_NETWORK_BUFFER_SIZE); messages_to_send_queue_mutex = mutex_create(); - thread send_thread = thread_start(network_send_thread, 0); - thread_detach(&send_thread); + for (int i = 0; i < 3; i++) { + thread send_thread = thread_start(network_send_thread, 0); + thread_detach(&send_thread); + } load_map_from_data(); create_objects(); @@ -171,14 +173,14 @@ void update_server(platform_window* window) { allocator_clear(&server_incomming_allocator); mutex_unlock(&messages_received_on_server.mutex); - update_spawners(); - update_drops(); - update_wallitems(); - update_bullets(window); - update_players_server(); - update_zombies_server(window); + if (update_timer >= SERVER_TICK_RATE) { // send at 60 ticks + update_spawners_server(); + update_drops_server(); + update_wallitems_server(); + update_bullets_server(window); + update_players_server(); + update_zombies_server(window); - if (update_timer >= (1/60.0f)) { // send at 60 ticks broadcast_to_clients(create_protocol_user_list()); broadcast_to_clients(create_protocol_zombie_list()); broadcast_to_clients(create_protocol_bullets_list()); diff --git a/src/objects.c b/src/objects.c index b296c9d..7299aab 100644 --- a/src/objects.c +++ b/src/objects.c @@ -78,7 +78,7 @@ void create_objects() { for (int i = MAP_SIZE_Y-1; i >= 0; i--) { create_box(0, i, 0); - create_box(MAP_SIZE_Y-1, i, 0); + create_box(MAP_SIZE_X-1, i, 0); } create_box(16, 8, 0); diff --git a/src/players.c b/src/players.c index d22ec57..9be2534 100644 --- a/src/players.c +++ b/src/players.c @@ -207,7 +207,7 @@ void take_player_input(platform_window* window) { void update_players_server() { for (int i = 0; i < MAX_PLAYERS; i++) { if (!players[i].active) continue; - players[i].sec_since_last_shot += update_delta; + players[i].sec_since_last_shot += SERVER_TICK_RATE; } } diff --git a/src/protocol.c b/src/protocol.c index 4493604..9dc944e 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -116,9 +116,22 @@ void add_message_to_outgoing_queuex(network_message message, network_client c) { } void add_message_to_outgoing_queue(send_queue_entry entry) { + network_message_type type = *(network_message_type*)(entry.message.data+12); + + bool can_overwrite = type != MESSAGE_USER_SHOOT && type != MESSAGE_USER_MOVED && type != MESSAGE_USER_LOOK; + for (int i = 0; i < OUTGOING_QUEUE_SIZE; i++) { - if (messages_to_send_queue[i].active) continue; + if (messages_to_send_queue[i].active) { + network_message_type type_existing = *(network_message_type*)(messages_to_send_queue[i].message.data+12); + if (type == type_existing && can_overwrite) { + messages_to_send_queue[i] = entry; + return; + } + else { + continue; + } + } messages_to_send_queue[i] = entry; messages_to_send_queue[i].active = true; return; @@ -130,8 +143,11 @@ void* network_send_thread(void* args) { while (1) { for (int i = 0; i < OUTGOING_QUEUE_SIZE; i++) { - if (!messages_to_send_queue[i].active) continue; mutex_lock(&messages_to_send_queue_mutex); + if (!messages_to_send_queue[i].active) { + mutex_unlock(&messages_to_send_queue_mutex); + continue; + } send_queue_entry message = messages_to_send_queue[i]; messages_to_send_queue[i].active = false; mutex_unlock(&messages_to_send_queue_mutex); @@ -142,7 +158,8 @@ void* network_send_thread(void* args) { network_client_send(&c, message.message); } } - mem_free(message.message.data); + mem_free(message.message.data); + thread_sleep(1000); } } } \ No newline at end of file diff --git a/src/wall_item.c b/src/wall_item.c index b1b7f5d..1812db7 100644 --- a/src/wall_item.c +++ b/src/wall_item.c @@ -46,14 +46,14 @@ void create_wallitem(vec3f position, wall_item_type type, wall_item_data data) { } } -void update_wallitems() { +void update_wallitems_server() { #define MAX_HEIGHT_DIFF_FOR_WALLITEM (0.1f) for (int i = 0; i < MAX_WALLITEMS; i++) { wall_item item = wallitems[i]; if (!item.active) continue; - wallitems[i].time_active += update_delta; + wallitems[i].time_active += SERVER_TICK_RATE; wallitems[i].position.z = MAX_HEIGHT_DIFF_FOR_WALLITEM * sin (2 * M_PI * 0.5f * (wallitems[i].time_active) + 0) + item.start_h; } } diff --git a/src/zombies.c b/src/zombies.c index 16ca4aa..d44c46d 100644 --- a/src/zombies.c +++ b/src/zombies.c @@ -45,10 +45,10 @@ void spawn_zombie(int x, int y) { } } -void update_spawners() { +void update_spawners_server() { for (int x = 0; x < MAX_SPAWNERS; x++) { spawner spawner = spawner_tiles[x]; - spawner_tiles[x].sec_since_last_spawn += update_delta; + spawner_tiles[x].sec_since_last_spawn += SERVER_TICK_RATE; if (spawner_tiles[x].sec_since_last_spawn >= 2.0f) { spawn_zombie(spawner.position.x, spawner.position.y); spawner_tiles[x].sec_since_last_spawn = 0; @@ -137,7 +137,7 @@ static bool is_within_next_tile(zombie o) { } void update_zombies_client(platform_window* window) { - float speed = 0.05f; + float speed = 4.0f * update_delta; for (int i = 0; i < MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; @@ -173,13 +173,13 @@ static vec2f get_random_point_around_player(player p, zombie o) { } void update_zombies_server(platform_window* window) { - float speed = 4.0f * update_delta; + float speed = 4.0f * SERVER_TICK_RATE; for (int i = 0; i < MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; - zombies[i].time_since_last_path += update_delta; + zombies[i].time_since_last_path += SERVER_TICK_RATE; if (zombies[i].time_since_last_path > 0.05f) { player closest_player = get_closest_player_to_tile((int)o.position.x, (int)o.position.y); vec2f target_tile = (vec2f){closest_player.playerx, closest_player.playery+(get_player_size_in_tile()/2)}; @@ -217,6 +217,7 @@ void update_zombies_server(platform_window* window) { zombies[i].next2tiles[1] = (vec2f){-1,-1}; if (o.path.length > 0) { zombies[i].next2tiles[0] = *(vec2f*)array_at(&o.path, o.path.length-1); + zombies[i].next2tiles[1] = zombies[i].next2tiles[0]; if (o.path.length > 1) { zombies[i].next2tiles[1] = *(vec2f*)array_at(&o.path, o.path.length-2); } -- cgit v1.2.3-70-g09d2