2 * Copyright (c) 2011 Rudolf Polzer
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 var float autocvar_cl_predictionerrorcompensation = 0;
27 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
29 #define PMF_ONGROUND 8
30 #define REFDEFFLAG_TELEPORTED 1
31 #define REFDEFFLAG_JUMPING 2
34 vector csqcplayer_origin, csqcplayer_velocity;
35 float csqcplayer_sequence, player_pmflags;
36 float csqcplayer_moveframe;
37 vector csqcplayer_predictionerror;
38 float csqcplayer_predictionerrortime;
40 vector CSQCPlayer_GetPredictionError()
42 if(!autocvar_cl_predictionerrorcompensation)
44 if(time < csqcplayer_predictionerrortime)
45 return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
49 void CSQCPlayer_SetPredictionError(vector v)
51 if(!autocvar_cl_predictionerrorcompensation)
53 csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
54 csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
57 void CSQCPlayer_Unpredict()
59 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
61 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
62 error("Cannot unpredict in current status");
63 self.origin = csqcplayer_origin;
64 self.velocity = csqcplayer_velocity;
65 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
66 self.pmove_flags = player_pmflags;
69 void CSQCPlayer_SetMinsMaxs()
71 if(self.pmove_flags & PMF_DUCKED)
73 self.mins = PL_CROUCH_MIN;
74 self.maxs = PL_CROUCH_MAX;
75 self.view_ofs = PL_CROUCH_VIEW_OFS;
81 self.view_ofs = PL_VIEW_OFS;
85 void CSQCPlayer_SavePrediction()
87 player_pmflags = self.pmove_flags;
88 csqcplayer_origin = self.origin;
89 csqcplayer_velocity = self.velocity;
90 csqcplayer_sequence = servercommandframe;
91 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
94 void CSQCPlayer_PredictTo(float endframe)
96 CSQCPlayer_Unpredict();
97 CSQCPlayer_SetMinsMaxs();
99 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
101 if (getstatf(STAT_HEALTH) <= 0)
103 csqcplayer_moveframe = clientcommandframe;
104 getinputstate(csqcplayer_moveframe-1);
108 while(csqcplayer_moveframe < endframe)
110 if (!getinputstate(csqcplayer_moveframe))
114 runstandardplayerphysics(self);
115 CSQCPlayer_SetMinsMaxs();
116 csqcplayer_moveframe++;
119 //add in anything that was applied after (for low packet rate protocols)
120 input_angles = view_angles;
123 float CSQCPlayer_IsLocalPlayer()
125 return (self == csqcplayer);
128 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
130 void CSQCPlayer_SetCamera()
138 #ifdef COMPAT_XON050_ENGINE
139 if(servercommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
141 if(servercommandframe == 0)
144 InterpolateOrigin_Do();
145 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
147 // get crouch state from the server
148 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
149 self.pmove_flags &~= PMF_DUCKED;
150 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
151 self.pmove_flags |= PMF_DUCKED;
153 // get onground state from the server
155 self.pmove_flags |= PMF_ONGROUND;
157 self.pmove_flags &~= PMF_ONGROUND;
159 CSQCPlayer_SetMinsMaxs();
161 // override it back just in case
162 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
166 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
170 v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
171 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
172 CSQCPlayer_PredictTo(servercommandframe + 1);
173 CSQCPlayer_SetPredictionError(o - self.origin);
177 // get crouch state from the server
178 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
179 self.pmove_flags &~= PMF_DUCKED;
180 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
181 self.pmove_flags |= PMF_DUCKED;
183 // get onground state from the server
185 self.pmove_flags |= PMF_ONGROUND;
187 self.pmove_flags &~= PMF_ONGROUND;
189 CSQCPlayer_SavePrediction();
191 CSQCPlayer_PredictTo(clientcommandframe);
193 CSQCPlayer_SetMinsMaxs();
197 setorigin(self, self.origin);
199 self.angles_y = input_angles_y;
205 #ifdef COMPAT_XON050_ENGINE
206 view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
208 view = CSQCModel_server2csqc(player_localentnum);
211 #ifdef COMPAT_XON050_ENGINE
212 if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
214 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
215 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
216 setproperty(VF_ANGLES, view.angles);
222 var float refdefflags = 0;
224 if(view.csqcmodel_teleported)
226 refdefflags |= REFDEFFLAG_TELEPORTED;
227 view.csqcmodel_teleported = 0;
230 if(input_buttons & 4)
231 refdefflags |= REFDEFFLAG_JUMPING;
233 V_CalcRefdef(view, refdefflags);
237 // no setup, keep engine provided values
238 // to support a legacy entity being the view
241 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
244 void CSQCPlayer_Remove()
246 if(self.entnum != player_localnum + 1)
249 cvar_clientsettemp("cl_movement_replay", "1");
252 float CSQCPlayer_PreUpdate()
254 if(self.entnum != player_localnum + 1)
256 cvar_clientsettemp("cl_movement_replay", "0");
257 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
258 CSQCPlayer_Unpredict();
262 float CSQCPlayer_PostUpdate()
264 if(self.entnum != player_localentnum)
266 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
268 self.entremove = CSQCPlayer_Remove;