diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2023-04-30 14:06:36 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2023-04-30 14:06:36 +0200 |
| commit | 9e70ced6b48d627f236af8ea8366c6ed73748ab1 (patch) | |
| tree | 0c8306a6744923364e7b4c17f9b334ff2529e700 | |
| parent | d402270d68d7d4a1c887b34cd93e5f11aa61b638 (diff) | |
fix zombie pathfinding bug
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | build/SDL2.dll | bin | 1561088 -> 0 bytes | |||
| -rw-r--r-- | build/SDL2_mixer.dll | bin | 123904 -> 0 bytes | |||
| -rw-r--r-- | build/zombies.exe | bin | 1948702 -> 1949153 bytes | |||
| -rw-r--r-- | include/game.h | 1 | ||||
| -rw-r--r-- | include/pathfinding.h | 1 | ||||
| -rw-r--r-- | include/protocol.h | 2 | ||||
| -rw-r--r-- | include/zombies.h | 5 | ||||
| -rw-r--r-- | libs/SDL2.dll | bin | 1561088 -> 0 bytes | |||
| -rw-r--r-- | libs/SDL2.lib | bin | 165034 -> 0 bytes | |||
| -rw-r--r-- | libs/SDL2_mixer.dll | bin | 123904 -> 0 bytes | |||
| -rw-r--r-- | libs/SDL2_mixer.lib | bin | 17182 -> 0 bytes | |||
| -rw-r--r-- | libs/libFLAC-8.dll | bin | 441344 -> 0 bytes | |||
| -rw-r--r-- | libs/libmodplug-1.dll | bin | 252928 -> 0 bytes | |||
| -rw-r--r-- | libs/libmpg123-0.dll | bin | 337408 -> 0 bytes | |||
| -rw-r--r-- | libs/libogg-0.dll | bin | 52224 -> 0 bytes | |||
| -rw-r--r-- | libs/libopus-0.dll | bin | 124928 -> 0 bytes | |||
| -rw-r--r-- | libs/libopusfile-0.dll | bin | 46592 -> 0 bytes | |||
| -rw-r--r-- | libs/libvorbis-0.dll | bin | 251904 -> 0 bytes | |||
| -rw-r--r-- | libs/libvorbisfile-3.dll | bin | 69632 -> 0 bytes | |||
| -rw-r--r-- | src/bullets.c | 2 | ||||
| -rw-r--r-- | src/pathfinding.c | 1 | ||||
| -rw-r--r-- | src/players.c | 24 | ||||
| -rw-r--r-- | src/protocol.c | 12 | ||||
| -rw-r--r-- | src/zombies.c | 23 |
25 files changed, 42 insertions, 33 deletions
@@ -2,8 +2,6 @@ main: rm -rf "build/" mkdir -p "build/" cp -a "data/." "build/data" - cp -a "libs/SDL2_mixer.dll" "build/" - cp -a "libs/SDL2.dll" "build/" - gcc -m64 -g -DMODE_DEBUG main.c -o build/zombies.exe -lprojectbase-debug -Llibs/ -lSDL2 -lSDL2_mixer -lWs2_32 + gcc -m64 -g -DMODE_DEBUG main.c -o build/zombies.exe -lprojectbase-debug ./build/zombies.exe -ip 127.0.0.1 -port 27015 diff --git a/build/SDL2.dll b/build/SDL2.dll Binary files differdeleted file mode 100644 index ddba03c..0000000 --- a/build/SDL2.dll +++ /dev/null diff --git a/build/SDL2_mixer.dll b/build/SDL2_mixer.dll Binary files differdeleted file mode 100644 index 40bb1c1..0000000 --- a/build/SDL2_mixer.dll +++ /dev/null diff --git a/build/zombies.exe b/build/zombies.exe Binary files differindex da33497..cd30d67 100644 --- a/build/zombies.exe +++ b/build/zombies.exe diff --git a/include/game.h b/include/game.h index d2461b2..f2fd0cb 100644 --- a/include/game.h +++ b/include/game.h @@ -9,6 +9,7 @@ #define SERVER_TICK_RATE (1.0f/60.0f) #define SERVER_MAX_PLAYERS (10) +#define SERVER_PATHFINDING_INTERVAL (0.25f) typedef enum t_game_state { GAMESTATE_IDLE, diff --git a/include/pathfinding.h b/include/pathfinding.h index 9dcaab3..e648f2c 100644 --- a/include/pathfinding.h +++ b/include/pathfinding.h @@ -15,6 +15,7 @@ typedef struct t_pathfinding_request array *to_fill; uint64_t timestamp; mutex mutex; + bool active; } pathfinding_request; array global_pathfinding_queue; diff --git a/include/protocol.h b/include/protocol.h index 8fd33b9..9750d62 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -53,7 +53,7 @@ typedef struct t_protocol_user_list typedef struct t_protocol_zombie_list { network_message_type type; - zombie zombies[MAX_ZOMBIES]; + zombie zombies[SERVER_MAX_ZOMBIES]; } protocol_zombie_list; #include "drops.h" diff --git a/include/zombies.h b/include/zombies.h index 72836ea..0c18bef 100644 --- a/include/zombies.h +++ b/include/zombies.h @@ -28,11 +28,10 @@ typedef struct t_spawner { } spawner; #define MAX_SPAWNERS (5) -// data data that is stored on disk spawner spawner_tiles[MAX_SPAWNERS] = {0}; -#define MAX_ZOMBIES (50) -zombie zombies[MAX_ZOMBIES] = {0}; +#define SERVER_MAX_ZOMBIES (50) +zombie zombies[SERVER_MAX_ZOMBIES] = {0}; void create_spawner(vec2 position); void draw_spawners(platform_window* window); diff --git a/libs/SDL2.dll b/libs/SDL2.dll Binary files differdeleted file mode 100644 index ddba03c..0000000 --- a/libs/SDL2.dll +++ /dev/null diff --git a/libs/SDL2.lib b/libs/SDL2.lib Binary files differdeleted file mode 100644 index ea6fa42..0000000 --- a/libs/SDL2.lib +++ /dev/null diff --git a/libs/SDL2_mixer.dll b/libs/SDL2_mixer.dll Binary files differdeleted file mode 100644 index 40bb1c1..0000000 --- a/libs/SDL2_mixer.dll +++ /dev/null diff --git a/libs/SDL2_mixer.lib b/libs/SDL2_mixer.lib Binary files differdeleted file mode 100644 index 3b96d7d..0000000 --- a/libs/SDL2_mixer.lib +++ /dev/null diff --git a/libs/libFLAC-8.dll b/libs/libFLAC-8.dll Binary files differdeleted file mode 100644 index 71f2e19..0000000 --- a/libs/libFLAC-8.dll +++ /dev/null diff --git a/libs/libmodplug-1.dll b/libs/libmodplug-1.dll Binary files differdeleted file mode 100644 index 7c05126..0000000 --- a/libs/libmodplug-1.dll +++ /dev/null diff --git a/libs/libmpg123-0.dll b/libs/libmpg123-0.dll Binary files differdeleted file mode 100644 index c7809b1..0000000 --- a/libs/libmpg123-0.dll +++ /dev/null diff --git a/libs/libogg-0.dll b/libs/libogg-0.dll Binary files differdeleted file mode 100644 index 5133481..0000000 --- a/libs/libogg-0.dll +++ /dev/null diff --git a/libs/libopus-0.dll b/libs/libopus-0.dll Binary files differdeleted file mode 100644 index 9ba6c38..0000000 --- a/libs/libopus-0.dll +++ /dev/null diff --git a/libs/libopusfile-0.dll b/libs/libopusfile-0.dll Binary files differdeleted file mode 100644 index 97a88b6..0000000 --- a/libs/libopusfile-0.dll +++ /dev/null diff --git a/libs/libvorbis-0.dll b/libs/libvorbis-0.dll Binary files differdeleted file mode 100644 index f5ae1bf..0000000 --- a/libs/libvorbis-0.dll +++ /dev/null diff --git a/libs/libvorbisfile-3.dll b/libs/libvorbisfile-3.dll Binary files differdeleted file mode 100644 index d078736..0000000 --- a/libs/libvorbisfile-3.dll +++ /dev/null diff --git a/src/bullets.c b/src/bullets.c index d701b50..fb56a12 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -115,7 +115,7 @@ bool check_if_bullet_collided_with_zombie(bullet* b, platform_window* window, pl int index_of_closest_zombie = -1; vec2f intersect_point_of_closest_zombie; - for (int i = 0; i < MAX_ZOMBIES; i++) { + for (int i = 0; i < SERVER_MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; diff --git a/src/pathfinding.c b/src/pathfinding.c index 6897596..f3fa354 100644 --- a/src/pathfinding.c +++ b/src/pathfinding.c @@ -311,6 +311,7 @@ void make_pathfinding_request(vec2f start, vec2f end, array *to_fill, pathfindin request->start = start; request->end = end; request->to_fill = to_fill; + request->active = true; request->timestamp = 0; array_push(&global_pathfinding_queue, (uint8_t*)&request); diff --git a/src/players.c b/src/players.c index 64518d1..8009b79 100644 --- a/src/players.c +++ b/src/players.c @@ -182,32 +182,36 @@ void take_player_input(platform_window* window) { #endif if (keyboard_is_key_down(KEY_W)) { - if (!global_state.server) { + //if (!global_state.server) + { network_message message = create_protocol_user_moved(MOVE_UP, player_id); add_message_to_outgoing_queuex(message, *global_state.client); } - move_user(window, player_id, MOVE_UP, update_delta); + //move_user(window, player_id, MOVE_UP, update_delta); } if (keyboard_is_key_down(KEY_S)) { - if (!global_state.server) { + //if (!global_state.server) + { network_message message = create_protocol_user_moved(MOVE_DOWN, player_id); add_message_to_outgoing_queuex(message, *global_state.client); } - move_user(window, player_id, MOVE_DOWN, update_delta); + //move_user(window, player_id, MOVE_DOWN, update_delta); } if (keyboard_is_key_down(KEY_A)) { - if (!global_state.server) { + //if (!global_state.server) + { network_message message = create_protocol_user_moved(MOVE_LEFT, player_id); add_message_to_outgoing_queuex(message, *global_state.client); } - move_user(window, player_id, MOVE_LEFT, update_delta); + //move_user(window, player_id, MOVE_LEFT, update_delta); } if (keyboard_is_key_down(KEY_D)) { - if (!global_state.server) { + //if (!global_state.server) + { network_message message = create_protocol_user_moved(MOVE_RIGHT, player_id); add_message_to_outgoing_queuex(message, *global_state.client); } - move_user(window, player_id, MOVE_RIGHT, update_delta); + //move_user(window, player_id, MOVE_RIGHT, update_delta); } // Send gun position @@ -221,8 +225,8 @@ void take_player_input(platform_window* window) { float gun_offset_x = (get_player_size_in_tile()/2) + dirx; float gun_offset_y = (get_player_size_in_tile()/2) + diry; - p->gunx = p->playerx + gun_offset_x; - p->guny = p->playery + gun_offset_y; + //p->gunx = p->playerx + gun_offset_x; + //p->guny = p->playery + gun_offset_y; add_message_to_outgoing_queuex(create_protocol_user_look(player_id, gun_offset_x, gun_offset_y), *global_state.client); } diff --git a/src/protocol.c b/src/protocol.c index b54091e..555295c 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -116,18 +116,18 @@ 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); + network_message_type type = *(network_message_type*)(entry.message.data+NETWORK_PACKET_OVERHEAD); bool can_overwrite = type != MESSAGE_USER_SHOOT && type != MESSAGE_USER_MOVED && type != MESSAGE_USER_LOOK; - //mutex_lock(&messages_to_send_queue_mutex); + mutex_lock(&messages_to_send_queue_mutex); for (int i = 0; i < OUTGOING_QUEUE_SIZE; i++) { if (messages_to_send_queue[i].active) { - network_message_type type_existing = *(network_message_type*)(messages_to_send_queue[i].message.data+12); + network_message_type type_existing = *(network_message_type*)(messages_to_send_queue[i].message.data+NETWORK_PACKET_OVERHEAD); if (type == type_existing && can_overwrite) { messages_to_send_queue[i] = entry; - //mutex_unlock(&messages_to_send_queue_mutex); + mutex_unlock(&messages_to_send_queue_mutex); return; } else { @@ -136,10 +136,10 @@ void add_message_to_outgoing_queue(send_queue_entry entry) { } messages_to_send_queue[i] = entry; messages_to_send_queue[i].active = true; - //mutex_unlock(&messages_to_send_queue_mutex); + mutex_unlock(&messages_to_send_queue_mutex); return; } - //mutex_unlock(&messages_to_send_queue_mutex); + mutex_unlock(&messages_to_send_queue_mutex); log_info("Outgoing network queue is full"); } diff --git a/src/zombies.c b/src/zombies.c index f578701..2c19220 100644 --- a/src/zombies.c +++ b/src/zombies.c @@ -39,7 +39,7 @@ void create_spawner(vec2 position) { } void spawn_zombie(int x, int y) { - for (int i = 0; i < MAX_ZOMBIES; i++) { + for (int i = 0; i < SERVER_MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (o.alive) continue; @@ -159,14 +159,14 @@ static bool is_within_tile(zombie o, vec2f dest) { static bool is_within_next_tile(zombie o) { if (o.path.length > 0) { vec2f dest = *(vec2f*)array_at(&o.path, o.path.length-1); - is_within_tile(o, dest); + return is_within_tile(o, dest); } return false; } void update_zombies_client(platform_window* window) { float speed = 4.0f * update_delta; - for (int i = 0; i < MAX_ZOMBIES; i++) { + for (int i = 0; i < SERVER_MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; if (o.next2tiles[0].x == -1 || o.next2tiles[0].y == -1) continue; // ran out of stored path. @@ -203,32 +203,37 @@ static vec2f get_random_point_around_player(player p, zombie o) { void update_zombies_server(platform_window* window) { float speed = 4.0f * SERVER_TICK_RATE; - for (int i = 0; i < MAX_ZOMBIES; i++) { + for (int i = 0; i < SERVER_MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; zombies[i].time_since_last_path += SERVER_TICK_RATE; - if (zombies[i].time_since_last_path > 0.05f) { + if (zombies[i].time_since_last_path > SERVER_PATHFINDING_INTERVAL) { 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)}; + array_clear(zombies[i].request.to_fill); make_pathfinding_request((vec2f){o.position.x,o.position.y}, target_tile, &zombies[i].next_path, &zombies[i].request); zombies[i].time_since_last_path = 0; } else { - if (mutex_trylock(&zombies[i].request.mutex)) + if (zombies[i].request.active) { if (zombies[i].request.to_fill->length) { + mutex_trylock(&zombies[i].request.mutex); + array_destroy(&zombies[i].path); zombies[i].path = array_copy(zombies[i].request.to_fill); player closest_player = get_closest_player_to_tile((int)o.position.x, (int)o.position.y); vec2f final_pos = get_random_point_around_player(closest_player, zombies[i]); array_push_at(&zombies[i].path, (u8*)&final_pos, 0); - array_clear(zombies[i].request.to_fill); + + zombies[i].request.active = false; + + mutex_unlock(&zombies[i].request.mutex); } } - mutex_unlock(&zombies[i].request.mutex); } if (is_within_next_tile(zombies[i])) { @@ -256,7 +261,7 @@ void update_zombies_server(platform_window* window) { void draw_zombies(platform_window* window) { map_info info = get_map_info(window); - for (int i = 0; i < MAX_ZOMBIES; i++) { + for (int i = 0; i < SERVER_MAX_ZOMBIES; i++) { zombie o = zombies[i]; if (!o.alive) continue; |
