diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2022-12-14 00:04:20 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2022-12-14 00:04:20 +0100 |
| commit | 99f328fa19bb9cb266d9525629813cc0268a889e (patch) | |
| tree | 514d5eb5fe51afc5f19bb3acf240a425239eba2a | |
| parent | f8ccfba637267bae8064daa320cfb00b8ffe3e66 (diff) | |
bullets network transfer
| -rw-r--r-- | .vscode/launch.json | 2 | ||||
| -rw-r--r-- | build/zombies.exe | bin | 1712259 -> 1716616 bytes | |||
| -rw-r--r-- | include/bullets.h | 3 | ||||
| -rw-r--r-- | include/guns.h | 29 | ||||
| -rw-r--r-- | include/players.h | 3 | ||||
| -rw-r--r-- | include/protocol.h | 18 | ||||
| -rw-r--r-- | main.c | 2 | ||||
| -rw-r--r-- | project-base.code-workspace | 3 | ||||
| -rw-r--r-- | src/bullets.c | 53 | ||||
| -rw-r--r-- | src/game.c | 20 | ||||
| -rw-r--r-- | src/guns.c | 5 | ||||
| -rw-r--r-- | src/players.c | 46 | ||||
| -rw-r--r-- | src/protocol.c | 17 |
13 files changed, 154 insertions, 47 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json index d3fc5c5..7c697de 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "request": "launch", "name": "Launch(gdb)", "program": "build/zombies.exe ", - "programArgs": "-ip 84.104.98.153 -port 27015", + "programArgs": "-ip 127.0.0.1 -port 27015", "cwd": "${workspaceRoot}" } ] diff --git a/build/zombies.exe b/build/zombies.exe Binary files differindex f498b89..a620285 100644 --- a/build/zombies.exe +++ b/build/zombies.exe diff --git a/include/bullets.h b/include/bullets.h index ed64333..a42e6a4 100644 --- a/include/bullets.h +++ b/include/bullets.h @@ -6,6 +6,7 @@ #include "players.h" #include "objects.h" #include "map.h" +#include "guns.h" typedef struct t_bullet { int player_id; @@ -19,7 +20,7 @@ typedef struct t_bullet { bullet bullets[500] = {0}; int max_bullets = 500; -void shoot(platform_window* window, player p); +void shoot(platform_window* window, u32 id, float dirx, float diry); void draw_bullets(platform_window* window); #endif
\ No newline at end of file diff --git a/include/guns.h b/include/guns.h new file mode 100644 index 0000000..62d43df --- /dev/null +++ b/include/guns.h @@ -0,0 +1,29 @@ +#ifndef INCLUDE_GUNS +#define INCLUDE_GUNS + +typedef enum t_gun_type { + GUN_DESERTEAGLE, + GUN_MP5, + GUN_NOVA, + + GUN_ALL, +} gun_type; + +typedef struct t_gun { + gun_type type; + int magazine_size; + int max_ammunition; + float bullet_spread; + int bullets_per_shot; + float shots_per_second; +} gun; + +gun guns[GUN_ALL] = { + {GUN_DESERTEAGLE, 8, 64, 0.0f, 1, 4.0f}, + {GUN_MP5, 30, 120, 0.1f, 1, 10.0f}, + {GUN_NOVA, 12, 80, 0.2f, 3, 1.2f}, +}; + +gun get_gun_by_type(gun_type type); + +#endif
\ No newline at end of file diff --git a/include/players.h b/include/players.h index 6d401c4..4e148c9 100644 --- a/include/players.h +++ b/include/players.h @@ -7,6 +7,7 @@ #include "objects.h" #include "zombies.h" #include "math_helper.h" +#include "guns.h" typedef struct t_player { int id; @@ -17,6 +18,7 @@ typedef struct t_player { float gunx; float guny; float gun_height; + gun_type guntype; } player; #include "protocol.h" @@ -32,5 +34,6 @@ void draw_bullets(platform_window* window); object check_if_player_collided_with_object(platform_window* window, player p); float get_player_size(platform_window* window); void move_user(platform_window* window, u32 id, protocol_move_type move); +void update_players_server(); #endif
\ No newline at end of file diff --git a/include/protocol.h b/include/protocol.h index 989c4d5..64822a2 100644 --- a/include/protocol.h +++ b/include/protocol.h @@ -10,6 +10,8 @@ typedef enum t_network_message_type { MESSAGE_USER_MOVED, MESSAGE_USER_LOOK, MESSAGE_ZOMBIE_LIST, + MESSAGE_USER_SHOOT, + MESSAGE_BULLET_LIST, } network_message_type; typedef struct t_protocol_generic_client_message { @@ -60,6 +62,20 @@ typedef struct t_protocol_user_look { float guny; } protocol_user_look; +#include "bullets.h" + +typedef struct t_protocol_bullets_list { + network_message_type type; + bullet bullets[500]; +} protocol_bullets_list; + +typedef struct t_protocol_user_shoot { + network_message_type type; + u32 id; + float dirx; + float diry; +} protocol_user_shoot; + #define MAX_NETWORK_BUFFER_SIZE 50000 u8 network_buffer[50000]; network_message create_protocol_get_id_up(); @@ -67,7 +83,9 @@ network_message create_protocol_get_id_down(u32 id); network_message create_protocol_user_list(); network_message create_protocol_user_moved(protocol_move_type move, u32 id); network_message create_protocol_user_look(u32 id, float gunx, float guny); +network_message create_protocol_user_shoot(u32 id, float dirx, float diry); network_message create_protocol_zombie_list(); +network_message create_protocol_bullets_list(); array messages_received_on_server; array messages_received_on_client; @@ -11,6 +11,7 @@ #include "include/allocator.h" #include "include/game.h" #include "include/protocol.h" +#include "include/guns.h" #include "src/map.c" #include "src/players.c" @@ -22,6 +23,7 @@ #include "src/list.c" #include "src/allocator.c" #include "src/game.c" +#include "src/guns.c" #include "src/protocol.c" #define CONFIG_DIRECTORY "zombieshooter" diff --git a/project-base.code-workspace b/project-base.code-workspace index fbeb154..1e9bd72 100644 --- a/project-base.code-workspace +++ b/project-base.code-workspace @@ -62,7 +62,8 @@ "protocol.h": "c", "queue": "cpp", "streambuf": "c", - "game.h": "c" + "game.h": "c", + "bullets.h": "c" } } }
\ No newline at end of file diff --git a/src/bullets.c b/src/bullets.c index 191b342..0357025 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -1,32 +1,39 @@ #include "../include/bullets.h" -void shoot(platform_window* window, player p) { - map_info info = get_map_info(window); - float bullet_range = 100.0f; +void shoot(platform_window* window, u32 id, float dirx, float diry) { + player* p = get_player_by_id(id); + if (!p) { + log_info("User with unknown id shot"); + } + gun g = get_gun_by_type(p->guntype); + float time_between_bullets = 1.0f/g.shots_per_second; - float hh = get_height_of_tile_under_coords(window, p.playerx, p.playery); + if (p->sec_since_last_shot < time_between_bullets) { + return; + } + p->sec_since_last_shot = 0.0f; - float dirx = (_global_mouse.x - (window->width/2)); - float diry = (_global_mouse.y - (window->height/2)); - double length = sqrt(dirx * dirx + diry * diry); - dirx /= length; - diry /= length; + for (int i = 0; i < g.bullets_per_shot; i++) + { + map_info info = get_map_info(window); + float bullet_range = 100.0f; - #define SPRAY_BOUNDS (0.1f) - dirx += ((float)rand()/(float)(RAND_MAX/SPRAY_BOUNDS)-(SPRAY_BOUNDS/2)); - diry += ((float)rand()/(float)(RAND_MAX/SPRAY_BOUNDS)-(SPRAY_BOUNDS/2)); + float hh = get_height_of_tile_under_coords(window, p->playerx, p->playery); + dirx += ((float)rand()/(float)(RAND_MAX/g.bullet_spread)-(g.bullet_spread/2)); + diry += ((float)rand()/(float)(RAND_MAX/g.bullet_spread)-(g.bullet_spread/2)); - float bulletx = p.gunx; - float bullety = p.guny; - float bullet_end_point_x = bulletx+dirx*bullet_range; - float bullet_end_point_y = bullety+diry*bullet_range; + float bulletx = p->gunx; + float bullety = p->guny; + float bullet_end_point_x = bulletx+dirx*bullet_range; + float bullet_end_point_y = bullety+diry*bullet_range; - for (int i = 0; i < max_bullets; i++) { - bullet b = bullets[i]; - if (b.active) continue; + for (int i = 0; i < max_bullets; i++) { + bullet b = bullets[i]; + if (b.active) continue; - bullets[i] = (bullet){p.id, true, bulletx, bullety, hh + 0.5, bullet_end_point_x, bullet_end_point_y}; - break; + bullets[i] = (bullet){p->id, true, bulletx, bullety, hh + 0.5, bullet_end_point_x, bullet_end_point_y}; + break; + } } } @@ -173,6 +180,8 @@ void draw_bullets(platform_window* window) { bullets[i].position.x = p->gunx; bullets[i].position.y = p->guny; bullets[i].position.z = p->gun_height; + + printf("%d\n", i); if (check_if_bullet_collided_with_ground(&b, window)) { bullets[i].endy = b.endy; @@ -193,8 +202,6 @@ void draw_bullets(platform_window* window) { bullets[i].alive_time += update_delta; bullets[i].active = false; - if (!b.active) continue; - BULLET_RENDER_DEPTH(b.position.z); float bullet_render_x = b.position.x*info.tile_width + (b.position.y*info.px_incline); @@ -69,13 +69,14 @@ static void rotate_user(platform_window* window, protocol_user_look *message) { return; } - p->gunx = message->gunx; - p->guny = message->guny; + p->gunx = p->playerx + message->gunx; + p->guny = p->playery + message->guny; } float update_timer = 0.0f; void update_server(platform_window* window) { update_spawners(); + update_players_server(); update_zombies_server(window); for (int i = 0; i < messages_received_on_server.length; i++) { @@ -98,6 +99,12 @@ void update_server(platform_window* window) { case MESSAGE_USER_LOOK: { rotate_user(window, (protocol_user_look*)msg->message); } break; + + case MESSAGE_USER_SHOOT: { + protocol_user_shoot* shoot_msg = (protocol_user_shoot*)msg->message; + shoot(window, shoot_msg->id, shoot_msg->dirx, shoot_msg->diry); + printf("Player %d shot\n", shoot_msg->id); + } break; default: log_info("Unhandled message received"); @@ -113,6 +120,7 @@ void update_server(platform_window* 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()); update_timer = 0.0f; } @@ -142,11 +150,17 @@ void update_client(platform_window* window) { memcpy(players, msg_players->players, sizeof(players)); if (p) *p = copy; } break; + case MESSAGE_ZOMBIE_LIST: { if (global_state.server) break; // zombies are simulated on server so dont overwrite data. protocol_zombie_list* msg_zombies = (protocol_zombie_list*)msg; memcpy(zombies, msg_zombies->zombies, sizeof(zombies)); } break; + + case MESSAGE_BULLET_LIST: { + protocol_bullets_list* msg_bullets = (protocol_bullets_list*)msg; + memcpy(bullets, msg_bullets->bullets, sizeof(bullets)); + } break; default: log_info("Unhandled message received"); break; @@ -159,10 +173,10 @@ void update_client(platform_window* window) { } void update_game(platform_window* window) { - update_client(window); if (global_state.server) { update_server(window); } + update_client(window); if (global_state.network_state == CONNECTED) { if (!global_state.server) { diff --git a/src/guns.c b/src/guns.c new file mode 100644 index 0000000..62f4690 --- /dev/null +++ b/src/guns.c @@ -0,0 +1,5 @@ +#include "../include/guns.h" + +gun get_gun_by_type(gun_type type) { + return guns[type]; +}
\ No newline at end of file diff --git a/src/players.c b/src/players.c index e9260a8..877e2b6 100644 --- a/src/players.c +++ b/src/players.c @@ -28,6 +28,7 @@ void spawn_player(int id) { players[i].guny = 0.0f; players[i].gun_height = 0.0f; players[i].id = id; + players[i].guntype = GUN_NOVA; return; } } @@ -159,10 +160,34 @@ void take_player_input(platform_window* window) { dirx /= length; diry /= length; - p->gunx = p->playerx + (get_player_size_in_tile()/2) + dirx/2; - p->guny = p->playery + (get_player_size_in_tile()/2) + diry/2; + float gun_offset_x = (get_player_size_in_tile()/2) + dirx/2; + float gun_offset_y = (get_player_size_in_tile()/2) + diry/2; + + p->gunx = p->playerx + gun_offset_x; + p->guny = p->playery + gun_offset_y; - network_client_send(global_state.client, create_protocol_user_look(my_id, p->gunx, p->guny)); + network_client_send(global_state.client, create_protocol_user_look(my_id, gun_offset_x, gun_offset_y)); + } + + // shoot + { + if (is_left_down()) { + float dirx = (_global_mouse.x - (window->width/2)); + float diry = (_global_mouse.y - (window->height/2)); + double length = sqrt(dirx * dirx + diry * diry); + dirx /= length; + diry /= length; + + network_message message = create_protocol_user_shoot(my_id, dirx, diry); + network_client_send(global_state.client, message); + } + } +} + +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; } } @@ -171,21 +196,6 @@ void draw_players_at_tile(platform_window* window, int x, int y) { if (!players[i].active) continue; if ((int)players[i].playerx != x || (int)(players[i].playery+get_player_size_in_tile()) != y) continue; - players[i].sec_since_last_shot += update_delta; - float bullets_per_sec = 10; - float time_between_bullets = 1.0f/bullets_per_sec; - if (is_left_down()) { - if (players[i].sec_since_last_shot > time_between_bullets) { - int ix = get_my_player_index(); - if (ix != -1) { - for (int i = 0; i < 3; i++) { - shoot(window, players[ix]); - players[i].sec_since_last_shot = 0.0f; - } - } - } - } - int size = get_tile_width(window) / 2; map_info info = get_map_info(window); float height = get_height_of_tile_under_coords(window, players[i].playerx, players[i].playery); diff --git a/src/protocol.c b/src/protocol.c index 2381159..dc4783b 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -28,6 +28,14 @@ network_message create_protocol_zombie_list() { return network_create_message(network_buffer, sizeof(protocol_zombie_list), MAX_NETWORK_BUFFER_SIZE); } +network_message create_protocol_bullets_list() { + protocol_bullets_list* buf = (protocol_bullets_list*)network_buffer; + buf->type = MESSAGE_BULLET_LIST; + memcpy(buf->bullets, bullets, sizeof(bullets)); + return network_create_message(network_buffer, sizeof(protocol_bullets_list), MAX_NETWORK_BUFFER_SIZE); +} + + network_message create_protocol_user_moved(protocol_move_type move, u32 id) { protocol_move* buf = (protocol_move*)network_buffer; buf->type = MESSAGE_USER_MOVED; @@ -36,6 +44,15 @@ network_message create_protocol_user_moved(protocol_move_type move, u32 id) { return network_create_message(network_buffer, sizeof(protocol_move), MAX_NETWORK_BUFFER_SIZE); } +network_message create_protocol_user_shoot(u32 id, float dirx, float diry) { + protocol_user_shoot* buf = (protocol_user_shoot*)network_buffer; + buf->type = MESSAGE_USER_SHOOT; + buf->id = id; + buf->dirx = dirx; + buf->diry = diry; + return network_create_message(network_buffer, sizeof(protocol_user_shoot), MAX_NETWORK_BUFFER_SIZE); +} + network_message create_protocol_user_look(u32 id, float gunx, float guny) { protocol_user_look* buf = (protocol_user_look*)network_buffer; buf->type = MESSAGE_USER_LOOK; |
