DEADSOFTWARE

Добавлена задержка между выстрелами. У клиента пули не создаются без подтвеждения...
[netwar.git] / server.c
index ea28583e06de04a2ead618e666593f6e92e2dc22..e28b30203679bdde9c28ac957b2a8f487e9570cc 100644 (file)
--- a/server.c
+++ b/server.c
@@ -47,7 +47,16 @@ 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)
+                       SendMessage(sock, client[id].address, sv_sbul(i, g_bullet[i].owner, g_bullet[i].live, g_bullet[i].x, g_bullet[i].y, g_bullet[i].vx, g_bullet[i].vy, g_bullet[i].tick));
+}
+
+static void sv_spawn_player(int id) {
+       assert(id >= 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) {
@@ -58,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);
 }
 
@@ -66,6 +75,9 @@ static void sv_move_player(int id, ProtocolMessage m) {
        assert(id >= 0);
        assert(client[id].connected);
 
+       if(!g_player[id].live)
+               sv_spawn_player(id);
+
        g_player_does(id, m.cl.does.code);
 }
 
@@ -92,21 +104,20 @@ static void sv_register_player(IPaddress address, ProtocolMessage m) {
        SDL_Log("Player %s (%i) connected", client[i].name, i);
        SendMessage(sock, address, sv_info(name, i, clientlimit));
 
-       g_player_set(i, true, randfm(), randfm(), randfm(), 0, 0, 0);
+       sv_spawn_player(i);
        sv_sync_client(i, true);
 }
 
 static void sv_recv() {
        IPaddress address;
        ProtocolMessage m;
-       if(!RecvMessage(sock, &address, &m))
-               return;
-
-       switch(m.type) {
-       case CL_INFO: sv_register_player(address, m); break;
-       case CL_KILL: sv_kill_player(sv_find_client(address), false, ""); break;
-       case CL_DOES: sv_move_player(sv_find_client(address), m); break;
-       default:      SDL_Log("invalid message %i", m.type);
+       while(RecvMessage(sock, &address, &m)) {
+               switch(m.type) {
+               case CL_INFO: sv_register_player(address, m); break;
+               case CL_KILL: sv_kill_player(sv_find_client(address), false, ""); break;
+               case CL_DOES: sv_move_player(sv_find_client(address), m); break;
+               default:      SDL_Log("invalid message %i", m.type);
+               }
        }
 }
 
@@ -114,12 +125,16 @@ static void sv_send() {
        for(int i = 0; i < clientlimit; i++)
                if(client[i].connected)
                        sv_sync_client(i, false);
-       
+
        for(int i = 0; i < clientlimit; i++)
                g_player[i].updated = false;
+
+       for(int i = 0; i < MAX_BULLETS; i++)
+               g_bullet[i].updated = false;
 }
 
 void sv_start(uint16_t port) {
+       g_init(true);
        sock = OpenPort(port);
        lastTime = SDL_GetTicks();
        currTime = SDL_GetTicks();