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
28 #define PMF_JUMP_HELD 1
30 #define PMF_ONGROUND 8
31 #define REFDEFFLAG_TELEPORTED 1
32 #define REFDEFFLAG_JUMPING 2
35 vector csqcplayer_origin, csqcplayer_velocity;
36 float csqcplayer_sequence, player_pmflags;
37 float csqcplayer_moveframe;
38 vector csqcplayer_predictionerror;
39 float csqcplayer_predictionerrortime;
41 vector CSQCPlayer_GetPredictionError()
43 if(!autocvar_cl_predictionerrorcompensation)
45 if(time < csqcplayer_predictionerrortime)
46 return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
50 void CSQCPlayer_SetPredictionError(vector v)
52 if(!autocvar_cl_predictionerrorcompensation)
54 csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
55 csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
58 void CSQCPlayer_Unpredict()
60 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
62 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
63 error("Cannot unpredict in current status");
64 self.origin = csqcplayer_origin;
65 self.velocity = csqcplayer_velocity;
66 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
67 self.pmove_flags = player_pmflags;
70 void CSQCPlayer_SetMinsMaxs()
72 if(self.pmove_flags & PMF_DUCKED)
74 self.mins = PL_CROUCH_MIN;
75 self.maxs = PL_CROUCH_MAX;
76 self.view_ofs = PL_CROUCH_VIEW_OFS;
82 self.view_ofs = PL_VIEW_OFS;
86 void CSQCPlayer_SavePrediction()
88 player_pmflags = self.pmove_flags;
89 csqcplayer_origin = self.origin;
90 csqcplayer_velocity = self.velocity;
91 csqcplayer_sequence = servercommandframe;
92 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
95 void CSQCPlayer_PredictTo(float endframe)
97 CSQCPlayer_Unpredict();
98 CSQCPlayer_SetMinsMaxs();
100 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
102 // FIXME do we really NEED this? dead players have servercommandframe
103 // == 0 and thus won't predict
104 if (getstatf(STAT_HEALTH) <= 0)
106 csqcplayer_moveframe = clientcommandframe;
107 getinputstate(csqcplayer_moveframe-1);
108 print("the Weird code path got hit\n");
112 if(csqcplayer_moveframe >= endframe)
114 getinputstate(csqcplayer_moveframe - 1);
120 if (!getinputstate(csqcplayer_moveframe))
122 runstandardplayerphysics(self);
123 CSQCPlayer_SetMinsMaxs();
124 csqcplayer_moveframe++;
126 while(csqcplayer_moveframe < endframe);
129 //add in anything that was applied after (for low packet rate protocols)
130 input_angles = view_angles;
133 float CSQCPlayer_IsLocalPlayer()
135 return (self == csqcplayer);
138 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
140 void CSQCPlayer_SetCamera()
143 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
151 #ifdef COMPAT_XON050_ENGINE
152 if(servercommandframe == 0 || clientcommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
154 if(servercommandframe == 0 || clientcommandframe == 0)
157 InterpolateOrigin_Do();
158 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
160 // get crouch state from the server
161 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
162 self.pmove_flags &~= PMF_DUCKED;
163 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
164 self.pmove_flags |= PMF_DUCKED;
166 // get onground state from the server
168 self.pmove_flags |= PMF_ONGROUND;
170 self.pmove_flags &~= PMF_ONGROUND;
172 CSQCPlayer_SetMinsMaxs();
174 // override it back just in case
175 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
182 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
186 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
187 CSQCPlayer_PredictTo(servercommandframe + 1);
188 CSQCPlayer_SetPredictionError(o - self.origin);
192 // get crouch state from the server
193 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
194 self.pmove_flags &~= PMF_DUCKED;
195 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
196 self.pmove_flags |= PMF_DUCKED;
198 // get onground state from the server
200 self.pmove_flags |= PMF_ONGROUND;
202 self.pmove_flags &~= PMF_ONGROUND;
204 CSQCPlayer_SavePrediction();
206 CSQCPlayer_PredictTo(clientcommandframe + 1);
208 CSQCPlayer_SetMinsMaxs();
210 self.angles_y = input_angles_y;
214 setorigin(self, self.origin);
220 #ifdef COMPAT_XON050_ENGINE
221 view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
223 view = CSQCModel_server2csqc(player_localentnum);
226 if(view && view != csqcplayer)
228 entity oldself = self;
230 InterpolateOrigin_Do();
231 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
235 #ifdef COMPAT_XON050_ENGINE
236 if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
238 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
239 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
240 setproperty(VF_ANGLES, view_angles);
246 var float refdefflags = 0;
248 if(view.csqcmodel_teleported)
249 refdefflags |= REFDEFFLAG_TELEPORTED;
251 if(input_buttons & 4)
252 refdefflags |= REFDEFFLAG_JUMPING;
254 // note: these two only work in WIP2, but are harmless in WIP1
255 if(getstati(STAT_HEALTH) <= 0)
256 refdefflags |= REFDEFFLAG_DEAD;
259 refdefflags |= REFDEFFLAG_INTERMISSION;
261 V_CalcRefdef(view, refdefflags);
265 // FIXME by CSQC spec we have to do this:
266 // but it breaks chase cam
268 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
269 setproperty(VF_ANGLES, view_angles);
273 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
276 void CSQCPlayer_Remove()
279 cvar_settemp("cl_movement_replay", "1");
282 float CSQCPlayer_PreUpdate()
284 if(self != csqcplayer)
286 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
287 CSQCPlayer_Unpredict();
291 float CSQCPlayer_PostUpdate()
293 if(self.entnum != player_localnum + 1)
296 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
297 cvar_settemp("cl_movement_replay", "0");
298 self.entremove = CSQCPlayer_Remove;