]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_main.c
cl_main: Keep old CL_Disconnect for simplicity. Move guts to CL_DisconnectEx
[xonotic/darkplaces.git] / cl_main.c
index 3a84e2ddfc7003f53c5e512ba3146c919c347b7b..744ef854f7daf62c0deaafa34c37abb91e59e10e 100644 (file)
--- a/cl_main.c
+++ b/cl_main.c
@@ -340,6 +340,16 @@ void CL_ExpandCSQCRenderEntities(int num)
        }
 }
 
+static void CL_ToggleMenu_Hook(void)
+{
+#ifdef CONFIG_MENU
+       // remove menu
+       if (key_dest == key_menu || key_dest == key_menu_grabbed)
+               MR_ToggleMenu(0);
+#endif
+       key_dest = key_game;
+}
+
 extern cvar_t rcon_secure;
 
 /*
@@ -350,11 +360,26 @@ Sends a disconnect message to the server
 This is also called on Host_Error, so it shouldn't cause any errors
 =====================
 */
-void CL_Disconnect(void)
+
+void CL_DisconnectEx(qbool kicked, const char *fmt, ... )
 {
+       va_list argptr;
+       char reason[512];
+
        if (cls.state == ca_dedicated)
                return;
 
+       if(fmt)
+       {
+               va_start(argptr,fmt);
+               dpvsnprintf(reason,sizeof(reason),fmt,argptr);
+               va_end(argptr);
+       }
+       else
+       {
+               dpsnprintf(reason, sizeof(reason), "Disconnect by user");
+       }
+
        if (Sys_CheckParm("-profilegameonly"))
                Sys_AllowProfiling(false);
 
@@ -385,32 +410,41 @@ void CL_Disconnect(void)
        else if (cls.netcon)
        {
                sizebuf_t buf;
-               unsigned char bufdata[8];
+               unsigned char bufdata[520];
                if (cls.demorecording)
-                       CL_Stop_f(&cmd_client);
+                       CL_Stop_f(cmd_local);
 
-               // send disconnect message 3 times to improve chances of server
-               // receiving it (but it still fails sometimes)
-               memset(&buf, 0, sizeof(buf));
-               buf.data = bufdata;
-               buf.maxsize = sizeof(bufdata);
-               if (cls.protocol == PROTOCOL_QUAKEWORLD)
-               {
-                       Con_DPrint("Sending drop command\n");
-                       MSG_WriteByte(&buf, qw_clc_stringcmd);
-                       MSG_WriteString(&buf, "drop");
-               }
-               else
+               if(!kicked)
                {
-                       Con_DPrint("Sending clc_disconnect\n");
-                       MSG_WriteByte(&buf, clc_disconnect);
+                       // send disconnect message 3 times to improve chances of server
+                       // receiving it (but it still fails sometimes)
+                       memset(&buf, 0, sizeof(buf));
+                       buf.data = bufdata;
+                       buf.maxsize = sizeof(bufdata);
+                       if (cls.protocol == PROTOCOL_QUAKEWORLD)
+                       {
+                               Con_DPrint("Sending drop command\n");
+                               MSG_WriteByte(&buf, qw_clc_stringcmd);
+                               MSG_WriteString(&buf, "drop");
+                       }
+                       else
+                       {
+                               Con_DPrint("Sending clc_disconnect\n");
+                               MSG_WriteByte(&buf, clc_disconnect);
+                               if(cls.protocol == PROTOCOL_DARKPLACES8)
+                                       MSG_WriteString(&buf, reason);
+                       }
+                       NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol, 10000, 0, false);
+                       NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol, 10000, 0, false);
+                       NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol, 10000, 0, false);
                }
-               NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol, 10000, 0, false);
-               NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol, 10000, 0, false);
-               NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol, 10000, 0, false);
+
                NetConn_Close(cls.netcon);
                cls.netcon = NULL;
-               Con_Printf("Disconnected\n");
+               if(fmt)
+                       Con_Printf("Disconnect: %s\n", reason);
+               else
+                       Con_Printf("Disconnected\n");
        }
        cls.state = ca_disconnected;
        cl.islocalgame = false;
@@ -418,8 +452,18 @@ void CL_Disconnect(void)
        cls.demoplayback = cls.timedemo = host.restless = false;
        cls.signon = 0;
 
+       Cvar_Callback(&cl_netport);
+
        // If we're dropped mid-connection attempt, it won't clear otherwise.
        SCR_ClearLoadingScreen(false);
+
+       if(host.hook.SV_Shutdown)
+               host.hook.SV_Shutdown();
+}
+
+void CL_Disconnect(void)
+{
+       CL_DisconnectEx(false, NULL);
 }
 
 /*
@@ -430,7 +474,7 @@ This command causes the client to wait for the signon messages again.
 This is sent just before a server changes levels
 ==================
 */
-void CL_Reconnect_f(cmd_state_t *cmd)
+static void CL_Reconnect_f(cmd_state_t *cmd)
 {
        char temp[128];
        // if not connected, reconnect to the most recent server
@@ -500,9 +544,7 @@ static void CL_Connect_f(cmd_state_t *cmd)
 
 void CL_Disconnect_f(cmd_state_t *cmd)
 {
-       CL_Disconnect ();
-       if (sv.active)
-               SV_Shutdown ();
+       Cmd_Argc(cmd) < 1 ? CL_Disconnect() : CL_DisconnectEx(false, Cmd_Argv(cmd, 1));
 }
 
 
@@ -534,8 +576,6 @@ void CL_EstablishConnection(const char *address, int firstarg)
 
        if (LHNETADDRESS_FromString(&cls.connect_address, address, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
        {
-               // Disconnect from the current server, or stop a running demo.
-               CL_Disconnect();
                cls.connect_trying = true;
                cls.connect_remainingtries = 3;
                cls.connect_nextsendtime = 0;
@@ -545,8 +585,8 @@ void CL_EstablishConnection(const char *address, int firstarg)
                {
                        int i;
                        *cls.connect_userinfo = 0;
-                       for(i = firstarg; i+2 <= Cmd_Argc(&cmd_client); i += 2)
-                               InfoString_SetValue(cls.connect_userinfo, sizeof(cls.connect_userinfo), Cmd_Argv(&cmd_client, i), Cmd_Argv(&cmd_client, i+1));
+                       for(i = firstarg; i+2 <= Cmd_Argc(cmd_local); i += 2)
+                               InfoString_SetValue(cls.connect_userinfo, sizeof(cls.connect_userinfo), Cmd_Argv(cmd_local, i), Cmd_Argv(cmd_local, i+1));
                }
                else if(firstarg < -1)
                {
@@ -574,6 +614,11 @@ static void CL_EstablishConnection_Local(void)
                CL_EstablishConnection("local:1", -2);
 }
 
+static qbool CL_Intermission(void)
+{
+       return cl.intermission;
+}
+
 /*
 ==============
 CL_PrintEntities_f
@@ -609,7 +654,7 @@ List information on all models in the client modelindex
 static void CL_ModelIndexList_f(cmd_state_t *cmd)
 {
        int i;
-       dp_model_t *model;
+       model_t *model;
 
        // Print Header
        Con_Printf("%3s: %-30s %-8s %-8s\n", "ID", "Name", "Type", "Triangles");
@@ -656,7 +701,7 @@ void CL_UpdateRenderEntity(entity_render_t *ent)
 {
        vec3_t org;
        vec_t scale;
-       dp_model_t *model = ent->model;
+       model_t *model = ent->model;
        // update the inverse matrix for the renderer
        Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
        // update the animation blend state
@@ -766,7 +811,7 @@ entity_render_t *CL_NewTempEntity(double shadertime)
        return render;
 }
 
-void CL_Effect(vec3_t org, dp_model_t *model, int startframe, int framecount, float framerate)
+void CL_Effect(vec3_t org, model_t *model, int startframe, int framecount, float framerate)
 {
        int i;
        cl_effect_t *e;
@@ -2463,7 +2508,7 @@ void CL_Locs_Reload_f(cmd_state_t *cmd)
 }
 
 entity_t cl_meshentities[NUM_MESHENTITIES];
-dp_model_t cl_meshentitymodels[NUM_MESHENTITIES];
+model_t cl_meshentitymodels[NUM_MESHENTITIES];
 const char *cl_meshentitynames[NUM_MESHENTITIES] =
 {
        "MESH_SCENE",
@@ -2475,12 +2520,35 @@ static void CL_MeshEntities_Restart(void)
        int i;
        entity_t *ent;
        for (i = 0; i < NUM_MESHENTITIES; i++)
+       {
+               ent = cl_meshentities + i;
+               Mod_Mesh_Destroy(ent->render.model);
+               Mod_Mesh_Create(ent->render.model, cl_meshentitynames[i]);
+       }
+}
+
+static void CL_MeshEntities_Start(void)
+{
+       int i;
+       entity_t *ent;
+       for(i = 0; i < NUM_MESHENTITIES; i++)
        {
                ent = cl_meshentities + i;
                Mod_Mesh_Create(ent->render.model, cl_meshentitynames[i]);
        }
 }
 
+static void CL_MeshEntities_Shutdown(void)
+{
+       int i;
+       entity_t *ent;
+       for(i = 0; i < NUM_MESHENTITIES; i++)
+       {
+               ent = cl_meshentities + i;
+               Mod_Mesh_Destroy(ent->render.model);
+       }
+}
+
 static void CL_MeshEntities_Init(void)
 {
        int i;
@@ -2517,7 +2585,7 @@ static void CL_MeshEntities_Init(void)
                CL_UpdateRenderEntity(&ent->render);
        }
        cl_meshentities[MESH_UI].render.flags = RENDER_NOSELFSHADOW;
-       R_RegisterModule("cl_meshentities", CL_MeshEntities_Restart, CL_MeshEntities_Restart, CL_MeshEntities_Restart, CL_MeshEntities_Restart, CL_MeshEntities_Restart);
+       R_RegisterModule("cl_meshentities", CL_MeshEntities_Start, CL_MeshEntities_Shutdown, CL_MeshEntities_Restart, CL_MeshEntities_Restart, CL_MeshEntities_Restart);
 }
 
 void CL_MeshEntities_Scene_Clear(void)
@@ -2539,10 +2607,6 @@ void CL_MeshEntities_Scene_FinalizeRenderEntity(void)
        VectorCopy(ent->render.model->normalmaxs, ent->render.maxs);
 }
 
-static void CL_MeshEntities_Shutdown(void)
-{
-}
-
 extern cvar_t r_overheadsprites_pushback;
 extern cvar_t r_fullbright_directed_pitch_relative;
 extern cvar_t r_fullbright_directed_pitch;
@@ -2715,6 +2779,21 @@ void CL_UpdateEntityShading(void)
                CL_UpdateEntityShading_Entity(r_refdef.scene.entities[i]);
 }
 
+qbool vid_opened = false;
+void CL_StartVideo(void)
+{
+       if (!vid_opened && cls.state != ca_dedicated)
+       {
+               vid_opened = true;
+#ifdef WIN32
+               // make sure we open sockets before opening video because the Windows Firewall "unblock?" dialog can screw up the graphics context on some graphics drivers
+               NetConn_UpdateSockets();
+#endif
+               VID_Start();
+               CDAudio_Startup();
+       }
+}
+
 extern cvar_t host_framerate;
 extern cvar_t host_speeds;
 
@@ -2723,7 +2802,6 @@ double CL_Frame (double time)
        static double clframetime;
        static double cl_timer = 0;
        static double time1 = 0, time2 = 0, time3 = 0;
-       static double wait;
        int pass1, pass2, pass3;
 
        CL_VM_PreventInformationLeaks();
@@ -2766,9 +2844,8 @@ double CL_Frame (double time)
                {
                        clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxfps.value);
                        // when running slow, we need to sleep to keep input responsive
-                       wait = bound(0, cl_maxfps_alwayssleep.value * 1000, 100000);
-                       if (wait > 0)
-                               Sys_Sleep((int)wait);
+                       if (cl_maxfps_alwayssleep.value > 0)
+                               Sys_Sleep((int)bound(0, cl_maxfps_alwayssleep.value * 1000, 100000));
                }
                else if (!vid_activewindow && cl_maxidlefps.value >= 1 && !cls.timedemo)
                        clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxidlefps.value);
@@ -3056,6 +3133,11 @@ void CL_Init (void)
 
                CL_Video_Init();
 
+               NetConn_UpdateSockets_Client();
+
                host.hook.ConnectLocal = CL_EstablishConnection_Local;
+               host.hook.Disconnect = CL_DisconnectEx;
+               host.hook.CL_Intermission = CL_Intermission;
+               host.hook.ToggleMenu = CL_ToggleMenu_Hook;
        }
 }