diff --git a/client.c b/client.c
index defe5ec757891aad33ee0c4d92dc8d3338163b1d..6c00cd29460d141be8b819193194005c95a7ec36 100644 (file)
--- a/client.c
+++ b/client.c
#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);
);
}
+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);
}
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));
-}
-
-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)
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();
}