]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/csqcmodellib/cl_player.qc
invert the sense in which prediction error is stored; document the cvar
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodellib / cl_player.qc
index d1cfa4060093f25eac80c0e17c45766f98530166..a11b3962b26bb8fda38ca4d314f8cfe7d7cbae62 100644 (file)
@@ -35,24 +35,43 @@ entity csqcplayer;
 vector csqcplayer_origin, csqcplayer_velocity;
 float csqcplayer_sequence, player_pmflags;
 float csqcplayer_moveframe;
-vector csqcplayer_predictionerror;
+vector csqcplayer_predictionerroro;
+vector csqcplayer_predictionerrorv;
 float csqcplayer_predictionerrortime;
+float csqcplayer_predictionerrorfactor;
 
-vector CSQCPlayer_GetPredictionError()
+vector CSQCPlayer_GetPredictionErrorO()
 {
-       if(!autocvar_cl_predictionerrorcompensation)
+       if(time >= csqcplayer_predictionerrortime)
+               return '0 0 0';
+       return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
+}
+
+vector CSQCPlayer_GetPredictionErrorV()
+{
+       if(time >= csqcplayer_predictionerrortime)
                return '0 0 0';
-       if(time < csqcplayer_predictionerrortime)
-               return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
-       return '0 0 0';
+       return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
 }
 
-void CSQCPlayer_SetPredictionError(vector v)
+void CSQCPlayer_SetPredictionError(vector o, vector v)
 {
        if(!autocvar_cl_predictionerrorcompensation)
+       {
+               csqcplayer_predictionerrorfactor = 0;
                return;
-       csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
-       csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
+       }
+
+       // error too big to compensate, we LIKELY hit a teleport or a
+       // jumppad, or it's a jump time disagreement that'll get fixed
+       // next frame
+       if(vlen(o) > 32 || vlen(v) > 128)
+               return;
+
+       csqcplayer_predictionerrorfactor = autocvar_cl_predictionerrorcompensation / ticrate;
+       csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
+       csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
+       csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
 }
 
 void CSQCPlayer_Unpredict()
@@ -92,29 +111,43 @@ void CSQCPlayer_SavePrediction()
        csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
 }
 
-void CSQCPlayer_PredictTo(float endframe)
+void CSQCPlayer_PredictTo(float endframe, float apply_error)
 {
        CSQCPlayer_Unpredict();
+       if(apply_error)
+       {
+               self.origin += CSQCPlayer_GetPredictionErrorO();
+               self.velocity += CSQCPlayer_GetPredictionErrorV();
+       }
        CSQCPlayer_SetMinsMaxs();
 
        csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
 
+       // FIXME do we really NEED this? dead players have servercommandframe
+       // == 0 and thus won't predict
        if (getstatf(STAT_HEALTH) <= 0)
        {
                csqcplayer_moveframe = clientcommandframe;
                getinputstate(csqcplayer_moveframe-1);
+               print("the Weird code path got hit\n");
                return;
        }
 
-       while(csqcplayer_moveframe < endframe)
+       if(csqcplayer_moveframe >= endframe)
+       {
+               getinputstate(csqcplayer_moveframe - 1);
+       }
+       else
        {
-               if (!getinputstate(csqcplayer_moveframe))
+               do
                {
-                       break;
+                       if (!getinputstate(csqcplayer_moveframe))
+                               break;
+                       runstandardplayerphysics(self);
+                       CSQCPlayer_SetMinsMaxs();
+                       csqcplayer_moveframe++;
                }
-               runstandardplayerphysics(self);
-               CSQCPlayer_SetMinsMaxs();
-               csqcplayer_moveframe++;
+               while(csqcplayer_moveframe < endframe);
        }
 
        //add in anything that was applied after (for low packet rate protocols)
@@ -130,6 +163,9 @@ void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
 
 void CSQCPlayer_SetCamera()
 {
+       vector v0;
+       v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
+
        if(csqcplayer)
        {
                entity oldself;
@@ -161,6 +197,9 @@ void CSQCPlayer_SetCamera()
 
                        // override it back just in case
                        self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
+
+                       // set velocity
+                       self.velocity = v0;
                }
                else
                {
@@ -168,10 +207,10 @@ void CSQCPlayer_SetCamera()
                        {
                                vector o, v;
                                o = self.origin;
-                               v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
+                               v = v0;
                                csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
-                               CSQCPlayer_PredictTo(servercommandframe + 1);
-                               CSQCPlayer_SetPredictionError(o - self.origin);
+                               CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
+                               CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v);
                                self.origin = o;
                                self.velocity = v;
 
@@ -189,7 +228,7 @@ void CSQCPlayer_SetCamera()
 
                                CSQCPlayer_SavePrediction();
                        }
-                       CSQCPlayer_PredictTo(clientcommandframe + 1);
+                       CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
 
                        CSQCPlayer_SetMinsMaxs();
 
@@ -209,6 +248,15 @@ void CSQCPlayer_SetCamera()
        view = CSQCModel_server2csqc(player_localentnum);
 #endif
 
+       if(view && view != csqcplayer)
+       {
+               entity oldself = self;
+               self = view;
+               InterpolateOrigin_Do();
+               self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
+               self = oldself;
+       }
+
 #ifdef COMPAT_XON050_ENGINE
        if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
        {
@@ -228,12 +276,23 @@ void CSQCPlayer_SetCamera()
                if(input_buttons & 4)
                        refdefflags |= REFDEFFLAG_JUMPING;
 
+               // note: these two only work in WIP2, but are harmless in WIP1
+               if(getstati(STAT_HEALTH) <= 0)
+                       refdefflags |= REFDEFFLAG_DEAD;
+
+               if(intermission)
+                       refdefflags |= REFDEFFLAG_INTERMISSION;
+
                V_CalcRefdef(view, refdefflags);
        }
        else
        {
+               // FIXME by CSQC spec we have to do this:
+               // but it breaks chase cam
+               /*
                setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
                setproperty(VF_ANGLES, view_angles);
+               */
        }
 
        { CSQCPLAYER_HOOK_POSTCAMERASETUP }
@@ -241,17 +300,14 @@ void CSQCPlayer_SetCamera()
 
 void CSQCPlayer_Remove()
 {
-       if(self.entnum != player_localnum + 1)
-               return;
        csqcplayer = world;
-       cvar_clientsettemp("cl_movement_replay", "1");
+       cvar_settemp("cl_movement_replay", "1");
 }
 
 float CSQCPlayer_PreUpdate()
 {
-       if(self.entnum != player_localnum + 1)
+       if(self != csqcplayer)
                return 0;
-       cvar_clientsettemp("cl_movement_replay", "0");
        if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
                CSQCPlayer_Unpredict();
        return 1;
@@ -259,10 +315,11 @@ float CSQCPlayer_PreUpdate()
 
 float CSQCPlayer_PostUpdate()
 {
-       if(self.entnum != player_localentnum)
+       if(self.entnum != player_localnum + 1)
                return 0;
-       csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
        csqcplayer = self;
+       csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
+       cvar_settemp("cl_movement_replay", "0");
        self.entremove = CSQCPlayer_Remove;
        return 1;
 }