From 0aebaa861295894d8bfd51ee7d02da0d4b63e477 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Mon, 3 Apr 2017 10:57:04 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D0=B0=20=D0=B7=D0=B0=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D0=B0=20?= =?utf8?q?=D0=BC=D0=B5=D0=B6=D0=B4=D1=83=20=D0=B2=D1=8B=D1=81=D1=82=D1=80?= =?utf8?q?=D0=B5=D0=BB=D0=B0=D0=BC=D0=B8.=20=D0=A3=20=D0=BA=D0=BB=D0=B8?= =?utf8?q?=D0=B5=D0=BD=D1=82=D0=B0=20=D0=BF=D1=83=D0=BB=D0=B8=20=D0=BD?= =?utf8?q?=D0=B5=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D1=8E=D1=82=D1=81=D1=8F?= =?utf8?q?=20=D0=B1=D0=B5=D0=B7=20=D0=BF=D0=BE=D0=B4=D1=82=D0=B2=D0=B5?= =?utf8?q?=D0=B6=D0=B4=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=B5=D1=80=D0=B2?= =?utf8?q?=D0=B5=D1=80=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- client.c | 5 ++++- game.c | 32 +++++++++++++++++++++++++------- game.h | 15 +++++++++------ netwar.c | 7 +------ protocol.h | 18 ++++++++++-------- server.c | 7 ++++--- 6 files changed, 53 insertions(+), 31 deletions(-) diff --git a/client.c b/client.c index 6c00cd2..390a04e 100644 --- a/client.c +++ b/client.c @@ -39,7 +39,8 @@ static void cl_update_svplayer(ProtocolMessage m) { b2f(m.sv.splr.r), b2f(m.sv.splr.vx), b2f(m.sv.splr.vy), - b2f(m.sv.splr.vr) + b2f(m.sv.splr.vr), + m.sv.splr.shoot ); } @@ -109,6 +110,8 @@ void cl_connect(const char * host, uint16_t port) { SDL_Log("Invalid first message %i", m.type); exit(1); } + + g_init(false); } diff --git a/game.c b/game.c index 9b7f239..cacaf62 100644 --- a/game.c +++ b/game.c @@ -6,14 +6,16 @@ #include "game.h" -#define SPEED 0.00006 -#define ROTATE 0.00004 -#define BULL_SPEED 0.008 +#define SPEED 0.00006 +#define ROTATE 0.00004 +#define BULL_SPEED 0.00800 Player g_player[MAX_PLAYERS]; Bullet g_bullet[MAX_BULLETS]; -void g_player_set(unsigned int id, bool live, float x, float y, float r, float vx, float vy, float vr) { +static bool svmode; + +void g_player_set(unsigned int id, bool live, float x, float y, float r, float vx, float vy, float vr, int shoot) { assert(id < MAX_PLAYERS); g_player[id] = (Player) { .updated = true, @@ -62,9 +64,16 @@ static void fireplayer(int id) { if(j < 0) return; - float vx = BULL_SPEED * cos(g_player[id].r * 2 * M_PI); - float vy = BULL_SPEED * sin(g_player[id].r * 2 * M_PI); - g_bullet_set(j, id, true, g_player[id].x, g_player[id].y, vx, vy, 0); + if(g_player[id].shoot < PLAYER_SHOOT) + return; + + g_player[id].shoot = 0; + + if(svmode) { + float vx = BULL_SPEED * cos(g_player[id].r * 2 * M_PI); + float vy = BULL_SPEED * sin(g_player[id].r * 2 * M_PI); + g_bullet_set(j, id, true, g_player[id].x, g_player[id].y, vx, vy, 0); + } } static void checkspacebound(float * a) { @@ -105,6 +114,10 @@ void g_update() { g_player[i].r += g_player[i].vr; checkspacebound(&g_player[i].x); checkspacebound(&g_player[i].y); + + ++g_player[i].shoot; + if(g_player[i].shoot > PLAYER_SHOOT) + g_player[i].shoot = PLAYER_SHOOT; } } @@ -129,3 +142,8 @@ void g_update() { } } } + +void g_init(bool server_mode) { + svmode = server_mode; + // TODO memset & co +} diff --git a/game.h b/game.h index 44bad63..a2d44b4 100644 --- a/game.h +++ b/game.h @@ -2,17 +2,19 @@ #include "protocol.h" -#define MAX_PLAYERS 32 -#define MAX_BULLETS (MAX_PLAYERS * 8) -#define TICK_DELAY (1000 / 40) -#define BULLET_TIME (TICK_DELAY * 6) -#define PLAYER_SIZE 0.01785 +#define MAX_PLAYERS 32 +#define MAX_BULLETS (MAX_PLAYERS * 8) +#define TICK_DELAY (1000 / 20) // 20 World updates per second +#define BULLET_TIME (TICK_DELAY * 6) // Bullet live time 6.0s +#define PLAYER_SIZE 0.01785 // Palyer radius +#define PLAYER_SHOOT (TICK_DELAY * 0.3) // Can shoot every 0.15s typedef struct Player { /* public */ bool live; float x, y, r; float vx, vy, vr; + int shoot; /* internal */ bool updated; @@ -33,7 +35,8 @@ typedef struct Bullet { extern Player g_player[MAX_PLAYERS]; extern Bullet g_bullet[MAX_BULLETS]; -void g_player_set(unsigned int id, bool live, float x, float y, float r, float vx, float vy, float vr); +void g_player_set(unsigned int id, bool live, float x, float y, float r, float vx, float vy, float vr, int shoot); void g_bullet_set(unsigned int id, unsigned int owner, bool live, float x, float y, float vx, float vy, int tick); void g_player_does(unsigned int id, DoesBits code); void g_update(); +void g_init(bool server_mode); diff --git a/netwar.c b/netwar.c index 88ff5c7..a3236f1 100644 --- a/netwar.c +++ b/netwar.c @@ -134,13 +134,10 @@ static void keyboardhandle() { .down = key[SDL_SCANCODE_DOWN], .left = key[SDL_SCANCODE_LEFT], .right = key[SDL_SCANCODE_RIGHT], + .fire = key[SDL_SCANCODE_LCTRL], }); } -static void fire() { - cl_move((DoesBits) { .fire = true }); -} - int main(int argc, char ** argv) { sysinit(); useargs(argv); @@ -153,8 +150,6 @@ int main(int argc, char ** argv) { while(SDL_PollEvent(&event)) if(event.type == SDL_QUIT) goto cleanup; - else if(event.type == SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_LCTRL) - fire(); keyboardhandle(); diff --git a/protocol.h b/protocol.h index 68ec54a..a96d693 100644 --- a/protocol.h +++ b/protocol.h @@ -6,7 +6,7 @@ #include #define PROTOCOL_PORT 29386 -#define PROTOCOL_VERSION 2 +#define PROTOCOL_VERSION 3 #define PROTOCOL_F8FRAC (1 << 7) #define PACKED __attribute__((__packed__)) @@ -69,6 +69,7 @@ typedef struct PACKED SvSplr { uint8_t live; uint8_t x, y, r; uint8_t vx, vy, vr; + uint8_t shoot; } SvSplr; typedef struct PACKED SvSbul { @@ -164,17 +165,18 @@ static inline ProtocolMessage sv_kill(const char * msg) { return (ProtocolMessage) (SvMessage) m; } -static inline ProtocolMessage sv_splr(int clid, bool live, float x, float y, float r, float vx, float vy, float vr) { +static inline ProtocolMessage sv_splr(int clid, bool live, float x, float y, float r, float vx, float vy, float vr, int shoot) { return (ProtocolMessage) (SvMessage) (SvSplr) { - .type = SV_SPLR, - .clid = clid, - .live = live, - .x = f2b(x), - .y = f2b(y), - .r = f2b(r), + .type = SV_SPLR, + .clid = clid, + .live = live, + .x = f2b(x), + .y = f2b(y), + .r = f2b(r), .vx = f2b(vx), .vy = f2b(vy), .vr = f2b(vr), + .shoot = shoot, }; } diff --git a/server.c b/server.c index 5e48234..e28b302 100644 --- a/server.c +++ b/server.c @@ -47,7 +47,7 @@ static void sv_sync_client(int id, bool full) { for(int i = 0; i < clientlimit; i++) if(full || g_player[i].updated) - SendMessage(sock, client[id].address, sv_splr(i, g_player[i].live, g_player[i].x, g_player[i].y, g_player[i].r, g_player[i].vx, g_player[i].vy, g_player[i].vr)); + SendMessage(sock, client[id].address, sv_splr(i, g_player[i].live, g_player[i].x, g_player[i].y, g_player[i].r, g_player[i].vx, g_player[i].vy, g_player[i].vr, g_player[i].shoot)); for(int i = 0; i < MAX_BULLETS; i++) if(full || g_bullet[i].updated) @@ -56,7 +56,7 @@ static void sv_sync_client(int id, bool full) { static void sv_spawn_player(int id) { assert(id >= 0); - g_player_set(id, true, randfm(), randfm(), randfm(), 0, 0, 0); + g_player_set(id, true, randfm(), randfm(), randfm(), 0, 0, 0, PLAYER_SHOOT); } static void sv_kill_player(int id, bool send, const char * msg) { @@ -67,7 +67,7 @@ static void sv_kill_player(int id, bool send, const char * msg) { SendMessage(sock, client[id].address, sv_kill(msg)); client[id].connected = false; - g_player_set(id, false, 0, 0, 0, 0, 0, 0); + g_player_set(id, false, 0, 0, 0, 0, 0, 0, 0); SDL_Log("Player %s (%i) disconnected: %s", client[id].name, id, msg); } @@ -134,6 +134,7 @@ static void sv_send() { } void sv_start(uint16_t port) { + g_init(true); sock = OpenPort(port); lastTime = SDL_GetTicks(); currTime = SDL_GetTicks(); -- 2.29.2