DEADSOFTWARE

Изменены физические константы
[netwar.git] / client.c
index defe5ec757891aad33ee0c4d92dc8d3338163b1d..390a04e4be7719e9c206a4da5ce63ca40e87d293 100644 (file)
--- a/client.c
+++ b/client.c
@@ -6,12 +6,17 @@
 #include "network.h"
 #include "protocol.h"
 
-int cl_playerid;
+int cl_playerid = -1;
 
-static bool      run;
 static IPaddress addr;
 static UDPsocket sock;
-static char      nickname[32] = "Anonymous";    
+static char      nickname[32] = "Anonymous";
+
+static Uint32 lastTime;
+static Uint32 currTime;
+
+static DoesBits does;
+static bool     does_updated;
 
 static void cl_kill_client(ProtocolMessage m) {
        SDL_Log("Connection refused by server: %.*s\n", (int) sizeof(m.sv.kill.message), m.sv.kill.message);
@@ -34,12 +39,55 @@ 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
+       );
+}
+
+static void cl_update_svbullet(ProtocolMessage m) {
+       g_bullet_set(
+               m.sv.sbul.id,
+               m.sv.sbul.owner,
+               m.sv.sbul.live,
+               b2f(m.sv.sbul.x),
+               b2f(m.sv.sbul.y),
+               b2f(m.sv.sbul.vx),
+               b2f(m.sv.sbul.vy),
+               m.sv.sbul.tick
        );
 }
 
+static void cl_recv() {
+       IPaddress address;
+       ProtocolMessage m;
+       if(!RecvMessage(sock, &address, &m))
+               return;
+
+       switch(m.type) {
+       case SV_INFO: cl_update_svinfo(m); break;
+       case SV_KILL: cl_kill_client(m); break;
+       case SV_SPLR: cl_update_svplayer(m); break;
+       case SV_SBUL: cl_update_svbullet(m); break;
+       default:      SDL_Log("invalid message %i", m.type);
+       }
+}
+
+static void cl_send() {
+       if(does_updated) {
+               SendMessage(sock, addr, cl_does(does));
+
+               does.bits = 0;
+               does_updated = false;
+       }
+}
+
+void cl_move(DoesBits code) {
+       does_updated = true;
+       does.bits |= code.bits;
+}
+
 void cl_connect(const char * host, uint16_t port) {
-       if(SDLNet_ResolveHost(&addr, host, (port) ? (port) : (DEFAULT_PORT)) != 0) {
+       if(SDLNet_ResolveHost(&addr, host, (port) ? (port) : (PROTOCOL_PORT)) != 0) {
                SDL_Log("Unable to resolve host: %s\n", SDLNet_GetError());
                exit(1);
        }
@@ -54,38 +102,21 @@ void cl_connect(const char * host, uint16_t port) {
                exit(1);
        }
 
-       run = true;     
        if(m.type == SV_KILL) {
                cl_kill_client(m);
        } else if(m.type == SV_INFO) {
                cl_update_svinfo(m);
        } else {
-               SDL_Log("Invalid first message %i\n", m.type);
-               run = false;
+               SDL_Log("Invalid first message %i", m.type);
+               exit(1);
        }
-}
 
-void cl_move(DoesCode code) {
-       g_player_does(cl_playerid, code);
-       SendMessage(sock, addr, cl_does(code));
+       g_init(false);
 }
 
-void cl_recv() {
-       IPaddress address;
-       ProtocolMessage m;
-       if(!RecvMessage(sock, &address, &m))
-               return;
-
-       switch(m.type) {
-       case SV_INFO: cl_update_svinfo(m); break;
-       case SV_KILL: cl_kill_client(m); break;
-       case SV_SPLR: cl_update_svplayer(m); break;
-       default:      SDL_Log("invalid message %i", m.type);
-       }
-}
 
 void cl_disconnect(bool force) {
-       if(!run)
+       if(cl_playerid < 0)
                return;
 
        if(!force)
@@ -93,9 +124,21 @@ void cl_disconnect(bool force) {
 
        ClosePort(sock);
 
-       run = false;
+       cl_playerid = -1;
 }
 
-bool cl_isrun() {
-       return run;
+void cl_handle() {
+       cl_recv();
+
+       if(currTime - lastTime >= TICK_DELAY) {
+               if(does_updated)
+                       g_player_does(cl_playerid, does);
+
+               g_update();
+
+               cl_send();
+
+               lastTime = SDL_GetTicks();
+       }
+       currTime = SDL_GetTicks();
 }