shithub: choc

Download patch

ref: af6bdd73396e166417484c2efe4a0eb48aac780e
parent: 2cfa7c372d5eec32e2fe893cbe999d9434427395
author: Simon Howard <fraggle@gmail.com>
date: Wed Jan 11 21:11:52 EST 2006

Game start packets

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 284

--- a/src/net_client.c
+++ b/src/net_client.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_client.c 279 2006-01-10 19:59:26Z fraggle $
+// $Id: net_client.c 284 2006-01-12 02:11:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.17  2006/01/12 02:11:52  fraggle
+// Game start packets
+//
 // Revision 1.16  2006/01/10 19:59:25  fraggle
 // Reliable packet transport mechanism
 //
@@ -96,6 +99,7 @@
 #include "net_io.h"
 #include "net_packet.h"
 #include "net_server.h"
+#include "net_structrw.h"
 
 typedef enum
 {
@@ -157,6 +161,29 @@
     }
 }
 
+void NET_CL_StartGame(void)
+{
+    net_packet_t *packet;
+    net_gamesettings_t settings;
+
+    // Fill in game settings structure with appropriate parameters
+    // for the new game
+
+    settings.ticdup = 1;
+    settings.extratics = 0;
+    settings.deathmatch = deathmatch;
+    settings.episode = startepisode;
+    settings.map = startmap;
+    settings.skill = startskill;
+
+    // Send packet
+
+    packet = NET_Conn_NewReliable(&client_connection, 
+                                  NET_PACKET_TYPE_GAMESTART);
+
+    NET_WriteSettings(packet, &settings);
+}
+
 // data received while we are waiting for the game to start
 
 static void NET_CL_ParseWaitingData(net_packet_t *packet)
@@ -211,6 +238,36 @@
     }
 }
 
+static void NET_CL_ParseGameStart(net_packet_t *packet)
+{
+    net_gamesettings_t settings;
+    unsigned int player_number, num_players;
+    int i;
+
+    if (!NET_ReadInt8(packet, &player_number)
+     || !NET_ReadInt8(packet, &num_players)
+     || !NET_ReadSettings(packet, &settings))
+    {
+        return;
+    }
+
+    consoleplayer = player_number;
+    
+    for (i=0; i<MAXPLAYERS; ++i) 
+    {
+        playeringame[i] = i < num_players;
+    }
+
+    client_state = CLIENT_STATE_IN_GAME;
+
+    deathmatch = settings.deathmatch;
+    ticdup = settings.ticdup;
+//    extratic = settings.extratics;
+    startepisode = settings.episode;
+    startmap = settings.map;
+    startskill = settings.skill;
+}
+
 // parse a received packet
 
 static void NET_CL_ParsePacket(net_packet_t *packet)
@@ -235,6 +292,7 @@
                 break;
 
             case NET_PACKET_TYPE_GAMESTART:
+                NET_CL_ParseGameStart(packet);
                 break;
 
             case NET_PACKET_TYPE_GAMEDATA:
--- a/src/net_client.h
+++ b/src/net_client.h
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_client.h 278 2006-01-09 02:03:39Z fraggle $
+// $Id: net_client.h 284 2006-01-12 02:11:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.10  2006/01/12 02:11:52  fraggle
+// Game start packets
+//
 // Revision 1.9  2006/01/09 02:03:39  fraggle
 // Send clients their player number, and indicate on the waiting screen
 // which client we are.
@@ -74,6 +77,7 @@
 void NET_CL_Disconnect(void);
 void NET_CL_Run(void);
 void NET_CL_Init(void);
+void NET_CL_StartGame();
 void NET_Init(void);
 
 extern boolean net_client_connected;
--- a/src/net_gui.c
+++ b/src/net_gui.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_gui.c 280 2006-01-10 22:14:13Z fraggle $
+// $Id: net_gui.c 284 2006-01-12 02:11:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.8  2006/01/12 02:11:52  fraggle
+// Game start packets
+//
 // Revision 1.7  2006/01/10 22:14:13  fraggle
 // Shut up compiler warnings
 //
@@ -182,6 +185,12 @@
             // Music change
 
             RandomMusic();
+        }
+        else if (ev->type == ev_keydown && ev->data1 == ' ')
+        {
+            // Start game
+
+            NET_CL_StartGame();
         }
     }
 }
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -1,7 +1,7 @@
 // Emacs style mode select   -*- C++ -*- 
 //-----------------------------------------------------------------------------
 //
-// $Id: net_server.c 279 2006-01-10 19:59:26Z fraggle $
+// $Id: net_server.c 284 2006-01-12 02:11:52Z fraggle $
 //
 // Copyright(C) 2005 Simon Howard
 //
@@ -21,6 +21,9 @@
 // 02111-1307, USA.
 //
 // $Log$
+// Revision 1.20  2006/01/12 02:11:52  fraggle
+// Game start packets
+//
 // Revision 1.19  2006/01/10 19:59:26  fraggle
 // Reliable packet transport mechanism
 //
@@ -110,6 +113,7 @@
 #include "net_packet.h"
 #include "net_server.h"
 #include "net_sdl.h"
+#include "net_structrw.h"
 
 typedef enum
 {
@@ -137,6 +141,7 @@
 static net_context_t *server_context;
 static int sv_gamemode;
 static int sv_gamemission;
+static net_gamesettings_t sv_settings;
 
 static void NET_SV_DisconnectClient(net_client_t *client)
 {
@@ -383,6 +388,49 @@
     }
 }
 
+// Parse a game start packet
+
+static void NET_SV_ParseGameStart(net_packet_t *packet, net_client_t *client)
+{
+    net_gamesettings_t settings;
+    net_packet_t *startpacket;
+    int i;
+    
+    if (client != NET_SV_Controller())
+    {
+        // Only the controller can start a new game
+
+        return;
+    }
+
+    if (!NET_ReadSettings(packet, &settings))
+    {
+        // Malformed packet
+
+        return;
+    }
+
+    // Change server state
+
+    server_state = SERVER_IN_GAME;
+    sv_settings = settings;
+
+    // Send start packets to each connected node
+
+    for (i=0; i<MAXNETNODES; ++i) 
+    {
+        if (ClientConnected(&clients[i]))
+        {
+            startpacket = NET_Conn_NewReliable(&clients[i].connection,
+                                               NET_PACKET_TYPE_GAMESTART);
+
+            NET_WriteInt8(startpacket, NET_SV_NumClients());
+            NET_WriteInt8(startpacket, NET_SV_ClientIndex(&clients[i]));
+            NET_WriteSettings(startpacket, &settings);
+        }
+    }
+}
+
 // Process a packet received by the server
 
 static void NET_SV_Packet(net_packet_t *packet, net_addr_t *addr)
@@ -422,6 +470,7 @@
         switch (packet_type)
         {
             case NET_PACKET_TYPE_GAMESTART:
+                NET_SV_ParseGameStart(packet, client);
                 break;
             default:
                 // unknown packet type