]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sv_main.c
cmd: Re-re-re-re-implement the command parser. Allocate text only as needed.
[xonotic/darkplaces.git] / sv_main.c
index 5551acb51b9660bf919465bf001445bad0bc9ef8..1005083da74a84a32d530513148ef2e99f54238a 100644 (file)
--- a/sv_main.c
+++ b/sv_main.c
@@ -96,7 +96,6 @@ cvar_t sv_debugmove = {CVAR_SERVER | CVAR_NOTIFY, "sv_debugmove", "0", "disables
 cvar_t sv_echobprint = {CVAR_SERVER | CVAR_SAVE, "sv_echobprint", "1", "prints gamecode bprint() calls to server console"};
 cvar_t sv_edgefriction = {CVAR_SERVER, "edgefriction", "1", "how much you slow down when nearing a ledge you might fall off, multiplier of sv_friction (Quake used 2, QuakeWorld used 1 due to a bug in physics code)"};
 cvar_t sv_entpatch = {CVAR_SERVER, "sv_entpatch", "1", "enables loading of .ent files to override entities in the bsp (for example Threewave CTF server pack contains .ent patch files enabling play of CTF on id1 maps)"};
-cvar_t sv_fixedframeratesingleplayer = {CVAR_SERVER, "sv_fixedframeratesingleplayer", "1", "allows you to use server-style timing system in singleplayer (don't run faster than sys_ticrate)"};
 cvar_t sv_freezenonclients = {CVAR_SERVER | CVAR_NOTIFY, "sv_freezenonclients", "0", "freezes time, except for players, allowing you to walk around and take screenshots of explosions"};
 cvar_t sv_friction = {CVAR_SERVER | CVAR_NOTIFY, "sv_friction","4", "how fast you slow down"};
 cvar_t sv_gameplayfix_blowupfallenzombies = {CVAR_SERVER, "sv_gameplayfix_blowupfallenzombies", "1", "causes findradius to detect SOLID_NOT entities such as zombies and corpses on the floor, allowing splash damage to apply to them"};
@@ -142,7 +141,7 @@ cvar_t sv_playerphysicsqc = {CVAR_SERVER | CVAR_NOTIFY, "sv_playerphysicsqc", "1
 cvar_t sv_progs = {CVAR_SERVER, "sv_progs", "progs.dat", "selects which quakec progs.dat file to run" };
 cvar_t sv_protocolname = {CVAR_SERVER, "sv_protocolname", "DP7", "selects network protocol to host for (values include QUAKE, QUAKEDP, NEHAHRAMOVIE, DP1 and up)"};
 cvar_t sv_random_seed = {CVAR_SERVER, "sv_random_seed", "", "random seed; when set, on every map start this random seed is used to initialize the random number generator. Don't touch it unless for benchmarking or debugging"};
-cvar_t sv_ratelimitlocalplayer = {CVAR_SERVER, "sv_ratelimitlocalplayer", "0", "whether to apply rate limiting to the local player in a listen server (only useful for testing)"};
+cvar_t host_limitlocal = {CVAR_SERVER, "host_limitlocal", "0", "whether to apply rate limiting to the local player in a listen server (only useful for testing)"};
 cvar_t sv_sound_land = {CVAR_SERVER, "sv_sound_land", "demon/dland2.wav", "sound to play when MOVETYPE_STEP entity hits the ground at high speed (empty cvar disables the sound)"};
 cvar_t sv_sound_watersplash = {CVAR_SERVER, "sv_sound_watersplash", "misc/h2ohit1.wav", "sound to play when MOVETYPE_FLY/TOSS/BOUNCE/STEP entity enters or leaves water (empty cvar disables the sound)"};
 cvar_t sv_stepheight = {CVAR_SERVER | CVAR_NOTIFY, "sv_stepheight", "18", "how high you can step up (TW_SV_STEPCONTROL extension)"};
@@ -162,6 +161,9 @@ cvar_t teamplay = {CVAR_SERVER | CVAR_NOTIFY, "teamplay","0", "teamplay mode, va
 cvar_t timelimit = {CVAR_SERVER | CVAR_NOTIFY, "timelimit","0", "ends level at this time (in minutes)"};
 cvar_t sv_threaded = {CVAR_SERVER, "sv_threaded", "0", "enables a separate thread for server code, improving performance, especially when hosting a game while playing, EXPERIMENTAL, may be crashy"};
 
+cvar_t sv_rollspeed = {CVAR_CLIENT, "sv_rollspeed", "200", "how much strafing is necessary to tilt the view"};
+cvar_t sv_rollangle = {CVAR_CLIENT, "sv_rollangle", "2.0", "how much to tilt the view when strafing"};
+
 cvar_t saved1 = {CVAR_SERVER | CVAR_SAVE, "saved1", "0", "unused cvar in quake that is saved to config.cfg on exit, can be used by mods"};
 cvar_t saved2 = {CVAR_SERVER | CVAR_SAVE, "saved2", "0", "unused cvar in quake that is saved to config.cfg on exit, can be used by mods"};
 cvar_t saved3 = {CVAR_SERVER | CVAR_SAVE, "saved3", "0", "unused cvar in quake that is saved to config.cfg on exit, can be used by mods"};
@@ -518,7 +520,6 @@ void SV_Init (void)
        Cvar_RegisterVariable (&sv_echobprint);
        Cvar_RegisterVariable (&sv_edgefriction);
        Cvar_RegisterVariable (&sv_entpatch);
-       Cvar_RegisterVariable (&sv_fixedframeratesingleplayer);
        Cvar_RegisterVariable (&sv_freezenonclients);
        Cvar_RegisterVariable (&sv_friction);
        Cvar_RegisterVariable (&sv_gameplayfix_blowupfallenzombies);
@@ -564,7 +565,8 @@ void SV_Init (void)
        Cvar_RegisterVariable (&sv_progs);
        Cvar_RegisterVariable (&sv_protocolname);
        Cvar_RegisterVariable (&sv_random_seed);
-       Cvar_RegisterVariable (&sv_ratelimitlocalplayer);
+       Cvar_RegisterVariable (&host_limitlocal);
+       Cvar_RegisterAlias(&host_limitlocal, "sv_ratelimitlocalplayer");
        Cvar_RegisterVariable (&sv_sound_land);
        Cvar_RegisterVariable (&sv_sound_watersplash);
        Cvar_RegisterVariable (&sv_stepheight);
@@ -584,6 +586,9 @@ void SV_Init (void)
        Cvar_RegisterVariable (&timelimit);
        Cvar_RegisterVariable (&sv_threaded);
 
+       Cvar_RegisterVariable (&sv_rollangle);
+       Cvar_RegisterVariable (&sv_rollspeed);
+
        Cvar_RegisterVariable (&saved1);
        Cvar_RegisterVariable (&saved2);
        Cvar_RegisterVariable (&saved3);
@@ -728,7 +733,7 @@ Larger attenuations will drop off.  (max 4 attenuation)
 
 ==================
 */
-void SV_StartSound (prvm_edict_t *entity, int channel, const char *sample, int nvolume, float attenuation, qboolean reliable, float speed)
+void SV_StartSound (prvm_edict_t *entity, int channel, const char *sample, int nvolume, float attenuation, qbool reliable, float speed)
 {
        prvm_prog_t *prog = SVVM_prog;
        sizebuf_t *dest;
@@ -1162,7 +1167,7 @@ Called when the player is getting totally kicked off the host
 if (crash = true), don't bother sending signofs
 =====================
 */
-void SV_DropClient(qboolean crash)
+void SV_DropClient(qbool crash)
 {
        prvm_prog_t *prog = SVVM_prog;
        int i;
@@ -1294,7 +1299,7 @@ crosses a waterline.
 =============================================================================
 */
 
-static qboolean SV_PrepareEntityForSending (prvm_edict_t *ent, entity_state_t *cs, int enumber)
+static qbool SV_PrepareEntityForSending (prvm_edict_t *ent, entity_state_t *cs, int enumber)
 {
        prvm_prog_t *prog = SVVM_prog;
        int i;
@@ -1623,7 +1628,7 @@ static void SV_PrepareEntitiesForSending(void)
 
 #define MAX_LINEOFSIGHTTRACES 64
 
-qboolean SV_CanSeeBox(int numtraces, vec_t eyejitter, vec_t enlarge, vec_t entboxexpand, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
+qbool SV_CanSeeBox(int numtraces, vec_t eyejitter, vec_t enlarge, vec_t entboxexpand, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
 {
        prvm_prog_t *prog = SVVM_prog;
        float pitchsign;
@@ -1970,11 +1975,11 @@ void SV_AddCameraEyes(void)
 static void SV_WriteEntitiesToClient(client_t *client, prvm_edict_t *clent, sizebuf_t *msg, int maxsize)
 {
        prvm_prog_t *prog = SVVM_prog;
-       qboolean need_empty = false;
+       qbool need_empty = false;
        int i, numsendstates, numcsqcsendstates;
        entity_state_t *s;
        prvm_edict_t *camera;
-       qboolean success;
+       qbool success;
        vec3_t eye;
 
        // if there isn't enough space to accomplish anything, skip it
@@ -2536,7 +2541,7 @@ static void SV_SendClientDatagram (client_t *client)
                break;
        }
 
-       if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !sv_ratelimitlocalplayer.integer)
+       if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !host_limitlocal.integer)
        {
                // for good singleplayer, send huge packets
                maxsize = sizeof(sv_sendclientdatagram_buf);
@@ -2649,18 +2654,7 @@ static void SV_UpdateToReliableMessages (void)
                //strlcpy (host_client->name, name, sizeof (host_client->name));
                if (name != host_client->name) // prevent buffer overlap SIGABRT on Mac OSX
                        strlcpy (host_client->name, name, sizeof (host_client->name));
-               PRVM_serveredictstring(host_client->edict, netname) = PRVM_SetEngineString(prog, host_client->name);
-               if (strcmp(host_client->old_name, host_client->name))
-               {
-                       if (host_client->begun)
-                               SV_BroadcastPrintf("%s ^7changed name to %s\n", host_client->old_name, host_client->name);
-                       strlcpy(host_client->old_name, host_client->name, sizeof(host_client->old_name));
-                       // send notification to all clients
-                       MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
-                       MSG_WriteByte (&sv.reliable_datagram, i);
-                       MSG_WriteString (&sv.reliable_datagram, host_client->name);
-                       SV_WriteNetnameIntoDemo(host_client);
-               }
+               SV_Name(i);
 
                // DP_SV_CLIENTCOLORS
                host_client->colors = (int)PRVM_serveredictfloat(host_client->edict, clientcolors);
@@ -2828,7 +2822,7 @@ static void Download_CheckExtensions(cmd_state_t *cmd)
 static void SV_Download_f(cmd_state_t *cmd)
 {
        const char *whichpack, *whichpack2, *extension;
-       qboolean is_csqc; // so we need to check only once
+       qbool is_csqc; // so we need to check only once
 
        if (Cmd_Argc(cmd) < 2)
        {
@@ -3416,7 +3410,7 @@ void SV_SpawnServer (const char *map)
        char modelname[sizeof(sv.worldname)];
        char vabuf[1024];
 
-       Con_DPrintf("SpawnServer: %s\n", map);
+       Con_Printf("SpawnServer: %s\n", map);
 
        dpsnprintf (modelname, sizeof(modelname), "maps/%s.bsp", map);
 
@@ -3425,7 +3419,7 @@ void SV_SpawnServer (const char *map)
                dpsnprintf (modelname, sizeof(modelname), "maps/%s", map);
                if (!FS_FileExists(modelname))
                {
-                       Con_Printf("SpawnServer: no map file named maps/%s.bsp\n", map);
+                       Con_Printf("SpawnServer: no map file named %s\n", modelname);
                        return;
                }
        }
@@ -3522,7 +3516,7 @@ void SV_SpawnServer (const char *map)
        if(*sv_random_seed.string)
        {
                srand(sv_random_seed.integer);
-               Con_Printf("NOTE: random seed is %d; use for debugging/benchmarking only!\nUnset sv_random_seed to get real random numbers again.\n", sv_random_seed.integer);
+               Con_Printf(CON_WARN "NOTE: random seed is %d; use for debugging/benchmarking only!\nUnset sv_random_seed to get real random numbers again.\n", sv_random_seed.integer);
        }
 
        SV_VM_Setup();
@@ -3544,7 +3538,7 @@ void SV_SpawnServer (const char *map)
        {
                char buffer[1024];
                Protocol_Names(buffer, sizeof(buffer));
-               Con_Printf("Unknown sv_protocolname \"%s\", valid values are:\n%s\n", sv_protocolname.string, buffer);
+               Con_Printf(CON_ERROR "Unknown sv_protocolname \"%s\", valid values are:\n%s\n", sv_protocolname.string, buffer);
                sv.protocol = PROTOCOL_QUAKE;
        }
 
@@ -3703,7 +3697,7 @@ void SV_SpawnServer (const char *map)
        strlcpy(sv.worldmessage, PRVM_GetString(prog, PRVM_serveredictstring(prog->edicts, message)), sizeof(sv.worldmessage)); // map title (not related to filename)
        Cvar_SetQuick(&sv_worldmessage, sv.worldmessage);
 
-       Con_DPrint("Server spawned.\n");
+       Con_Printf("Server spawned.\n");
        NetConn_Heartbeat (2);
 
        if(cls.state == ca_dedicated)
@@ -3890,7 +3884,7 @@ static void SVVM_count_edicts(prvm_prog_t *prog)
        Con_Printf("step      :%3i\n", step);
 }
 
-static qboolean SVVM_load_edict(prvm_prog_t *prog, prvm_edict_t *ent)
+static qbool SVVM_load_edict(prvm_prog_t *prog, prvm_edict_t *ent)
 {
        // remove things from different skill levels or deathmatch
        if (gamemode != GAME_TRANSFUSION) //Transfusion does this in QC
@@ -4104,10 +4098,181 @@ static void SV_VM_Setup(void)
 
 extern cvar_t host_maxwait;
 extern cvar_t host_framerate;
+extern cvar_t cl_maxphysicsframesperserverframe;
+double SV_Frame(double time)
+{
+       static double sv_timer;
+       int i;
+       char vabuf[1024];
+       qbool playing = false;
+
+       if (!svs.threaded)
+       {
+               svs.perf_acc_sleeptime = host.sleeptime;
+               svs.perf_acc_realtime += time;
+
+               // Look for clients who have spawned
+               for (i = 0, host_client = svs.clients; i < svs.maxclients; i++, host_client++)
+                       if(host_client->begun && host_client->netconnection)
+                               playing = true;
+
+               if(svs.perf_acc_realtime > 5)
+               {
+                       svs.perf_cpuload = 1 - svs.perf_acc_sleeptime / svs.perf_acc_realtime;
+                       svs.perf_lost = svs.perf_acc_lost / svs.perf_acc_realtime;
+
+                       if(svs.perf_acc_offset_samples > 0)
+                       {
+                               svs.perf_offset_max = svs.perf_acc_offset_max;
+                               svs.perf_offset_avg = svs.perf_acc_offset / svs.perf_acc_offset_samples;
+                               svs.perf_offset_sdev = sqrt(svs.perf_acc_offset_squared / svs.perf_acc_offset_samples - svs.perf_offset_avg * svs.perf_offset_avg);
+                       }
+
+                       if(svs.perf_lost > 0 && developer_extra.integer && playing) // only complain if anyone is looking
+                               Con_DPrintf("Server can't keep up: %s\n", Host_TimingReport(vabuf, sizeof(vabuf)));
+               }
+
+               if(svs.perf_acc_realtime > 5 || sv.time < 10)
+               {
+                       /*
+                        * Don't accumulate time for the first 10 seconds of a match
+                        * so things can settle
+                        */
+                       svs.perf_acc_realtime = svs.perf_acc_sleeptime =
+                       svs.perf_acc_lost = svs.perf_acc_offset =
+                       svs.perf_acc_offset_squared = svs.perf_acc_offset_max =
+                       svs.perf_acc_offset_samples = host.sleeptime = 0;
+               }
+
+               /*
+                * Receive packets on each main loop iteration, as the main loop may
+                * be undersleeping due to select() detecting a new packet
+                */
+               if (sv.active)
+                       NetConn_ServerFrame();
+       }
+
+       /*
+        * If the accumulator hasn't become positive, don't
+        * run the frame. Everything that happens before this
+        * point will happen even if we're sleeping this frame.
+        */
+       if((sv_timer += time) < 0)
+               return sv_timer;
+
+       // limit the frametime steps to no more than 100ms each
+       if (sv_timer > 0.1)
+       {
+               if (!svs.threaded)
+                       svs.perf_acc_lost += (sv_timer - 0.1);
+               sv_timer = 0.1;
+       }
+
+       if (sv.active && sv_timer > 0 && !svs.threaded)
+       {
+               /*
+                * Execute one or more server frames, with an upper limit on how much
+                * execution time to spend on server frames to avoid freezing the game if
+                * the server is overloaded. This execution time limit means the game will
+                * slow down if the server is taking too long.
+                */
+               int framecount, framelimit = 1;
+               double advancetime, aborttime = 0;
+               float offset;
+               prvm_prog_t *prog = SVVM_prog;
+
+               // run the world state
+               // don't allow simulation to run too fast or too slow or logic glitches can occur
+
+               // stop running server frames if the wall time reaches this value
+               if (sys_ticrate.value <= 0)
+                       advancetime = sv_timer;
+               else
+               {
+                       advancetime = sys_ticrate.value;
+                       // listen servers can run multiple server frames per client frame
+                       framelimit = cl_maxphysicsframesperserverframe.integer;
+                       aborttime = Sys_DirtyTime() + 0.1;
+               }
+
+               if(host_timescale.value > 0 && host_timescale.value < 1)
+                       advancetime = min(advancetime, 0.1 / host_timescale.value);
+               else
+                       advancetime = min(advancetime, 0.1);
+
+               if(advancetime > 0)
+               {
+                       offset = Sys_DirtyTime() - host.dirtytime;
+                       if (offset < 0 || offset >= 1800)
+                               offset = 0;
+
+                       offset += sv_timer;
+                       ++svs.perf_acc_offset_samples;
+                       svs.perf_acc_offset += offset;
+                       svs.perf_acc_offset_squared += offset * offset;
+                       
+                       if(svs.perf_acc_offset_max < offset)
+                               svs.perf_acc_offset_max = offset;
+               }
+
+               // only advance time if not paused
+               // the game also pauses in singleplayer when menu or console is used
+               sv.frametime = advancetime * host_timescale.value;
+               if (host_framerate.value)
+                       sv.frametime = host_framerate.value;
+               if (sv.paused || host.paused)
+                       sv.frametime = 0;
+
+               for (framecount = 0; framecount < framelimit && sv_timer > 0; framecount++)
+               {
+                       sv_timer -= advancetime;
+
+                       // move things around and think unless paused
+                       if (sv.frametime)
+                               SV_Physics();
+
+                       // if this server frame took too long, break out of the loop
+                       if (framelimit > 1 && Sys_DirtyTime() >= aborttime)
+                               break;
+               }
+
+               R_TimeReport("serverphysics");
+
+               // send all messages to the clients
+               SV_SendClientMessages();
+
+               if (sv.paused == 1 && host.realtime > sv.pausedstart && sv.pausedstart > 0) {
+                       prog->globals.fp[OFS_PARM0] = host.realtime - sv.pausedstart;
+                       PRVM_serverglobalfloat(time) = sv.time;
+                       prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PausedTic), "QC function SV_PausedTic is missing");
+               }
+
+               // send an heartbeat if enough time has passed since the last one
+               NetConn_Heartbeat(0);
+               R_TimeReport("servernetwork");
+       }
+       else
+       {
+               // don't let r_speeds display jump around
+               R_TimeReport("serverphysics");
+               R_TimeReport("servernetwork");
+       }
+
+       // if there is some time remaining from this frame, reset the timer
+       if (sv_timer >= 0)
+       {
+               if (!svs.threaded)
+                       svs.perf_acc_lost += sv_timer;
+               sv_timer = 0;
+       }
+
+       return sv_timer;
+}
+
 static int SV_ThreadFunc(void *voiddata)
 {
        prvm_prog_t *prog = SVVM_prog;
-       qboolean playing = false;
+       qbool playing = false;
        double sv_timer = 0;
        double sv_deltarealtime, sv_oldrealtime, sv_realtime;
        double wait;
@@ -4210,7 +4375,7 @@ static int SV_ThreadFunc(void *voiddata)
                        sv.frametime = advancetime * host_timescale.value;
                        if (host_framerate.value)
                                sv.frametime = host_framerate.value;
-                       if (sv.paused || (cl.islocalgame && (key_dest != key_game || key_consoleactive || cl.csqc_paused)))
+                       if (sv.paused || host.paused)
                                sv.frametime = 0;
 
                        sv_timer -= advancetime;