diff options
Diffstat (limited to 'src')
| -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 |
5 files changed, 36 insertions, 26 deletions
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; |
