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_predictionerroro;
39 vector csqcplayer_predictionerrorv;
40 float csqcplayer_predictionerrortime;
42 vector CSQCPlayer_GetPredictionErrorO()
44 if(!autocvar_cl_predictionerrorcompensation)
46 if(time < csqcplayer_predictionerrortime)
47 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
51 vector CSQCPlayer_GetPredictionErrorV()
53 if(!autocvar_cl_predictionerrorcompensation)
55 if(time < csqcplayer_predictionerrortime)
56 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
60 void CSQCPlayer_SetPredictionError(vector o, vector v)
62 if(!autocvar_cl_predictionerrorcompensation)
64 csqcplayer_predictionerroro = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerroro + o;
65 csqcplayer_predictionerrorv = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerrorv + v;
66 csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
69 void CSQCPlayer_Unpredict()
71 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
73 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
74 error("Cannot unpredict in current status");
75 self.origin = csqcplayer_origin;
76 self.velocity = csqcplayer_velocity;
77 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
78 self.pmove_flags = player_pmflags;
81 void CSQCPlayer_SetMinsMaxs()
83 if(self.pmove_flags & PMF_DUCKED)
85 self.mins = PL_CROUCH_MIN;
86 self.maxs = PL_CROUCH_MAX;
87 self.view_ofs = PL_CROUCH_VIEW_OFS;
93 self.view_ofs = PL_VIEW_OFS;
97 void CSQCPlayer_SavePrediction()
99 player_pmflags = self.pmove_flags;
100 csqcplayer_origin = self.origin;
101 csqcplayer_velocity = self.velocity;
102 csqcplayer_sequence = servercommandframe;
103 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
106 void CSQCPlayer_PredictTo(float endframe, float apply_error)
108 CSQCPlayer_Unpredict();
111 self.origin += CSQCPlayer_GetPredictionErrorO();
112 self.velocity += CSQCPlayer_GetPredictionErrorV();
114 CSQCPlayer_SetMinsMaxs();
116 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
118 // FIXME do we really NEED this? dead players have servercommandframe
119 // == 0 and thus won't predict
120 if (getstatf(STAT_HEALTH) <= 0)
122 csqcplayer_moveframe = clientcommandframe;
123 getinputstate(csqcplayer_moveframe-1);
124 print("the Weird code path got hit\n");
128 if(csqcplayer_moveframe >= endframe)
130 getinputstate(csqcplayer_moveframe - 1);
136 if (!getinputstate(csqcplayer_moveframe))
138 runstandardplayerphysics(self);
139 CSQCPlayer_SetMinsMaxs();
140 csqcplayer_moveframe++;
142 while(csqcplayer_moveframe < endframe);
145 //add in anything that was applied after (for low packet rate protocols)
146 input_angles = view_angles;
149 float CSQCPlayer_IsLocalPlayer()
151 return (self == csqcplayer);
154 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
156 void CSQCPlayer_SetCamera()
159 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
167 #ifdef COMPAT_XON050_ENGINE
168 if(servercommandframe == 0 || clientcommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
170 if(servercommandframe == 0 || clientcommandframe == 0)
173 InterpolateOrigin_Do();
174 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
176 // get crouch state from the server
177 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
178 self.pmove_flags &~= PMF_DUCKED;
179 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
180 self.pmove_flags |= PMF_DUCKED;
182 // get onground state from the server
184 self.pmove_flags |= PMF_ONGROUND;
186 self.pmove_flags &~= PMF_ONGROUND;
188 CSQCPlayer_SetMinsMaxs();
190 // override it back just in case
191 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
198 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
203 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
204 CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
205 CSQCPlayer_SetPredictionError(o - self.origin, v0 - self.velocity);
209 // get crouch state from the server
210 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
211 self.pmove_flags &~= PMF_DUCKED;
212 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
213 self.pmove_flags |= PMF_DUCKED;
215 // get onground state from the server
217 self.pmove_flags |= PMF_ONGROUND;
219 self.pmove_flags &~= PMF_ONGROUND;
221 CSQCPlayer_SavePrediction();
223 CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
225 CSQCPlayer_SetMinsMaxs();
227 self.angles_y = input_angles_y;
231 setorigin(self, self.origin);
237 #ifdef COMPAT_XON050_ENGINE
238 view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
240 view = CSQCModel_server2csqc(player_localentnum);
243 if(view && view != csqcplayer)
245 entity oldself = self;
247 InterpolateOrigin_Do();
248 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
252 #ifdef COMPAT_XON050_ENGINE
253 if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
255 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
256 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
257 setproperty(VF_ANGLES, view_angles);
263 var float refdefflags = 0;
265 if(view.csqcmodel_teleported)
266 refdefflags |= REFDEFFLAG_TELEPORTED;
268 if(input_buttons & 4)
269 refdefflags |= REFDEFFLAG_JUMPING;
271 // note: these two only work in WIP2, but are harmless in WIP1
272 if(getstati(STAT_HEALTH) <= 0)
273 refdefflags |= REFDEFFLAG_DEAD;
276 refdefflags |= REFDEFFLAG_INTERMISSION;
278 V_CalcRefdef(view, refdefflags);
282 // FIXME by CSQC spec we have to do this:
283 // but it breaks chase cam
285 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
286 setproperty(VF_ANGLES, view_angles);
290 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
293 void CSQCPlayer_Remove()
296 cvar_settemp("cl_movement_replay", "1");
299 float CSQCPlayer_PreUpdate()
301 if(self != csqcplayer)
303 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
304 CSQCPlayer_Unpredict();
308 float CSQCPlayer_PostUpdate()
310 if(self.entnum != player_localnum + 1)
313 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
314 cvar_settemp("cl_movement_replay", "0");
315 self.entremove = CSQCPlayer_Remove;