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_movement_errorcompensation = 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
34 vector csqcplayer_origin, csqcplayer_velocity;
35 float csqcplayer_sequence, player_pmflags;
36 float csqcplayer_moveframe;
37 vector csqcplayer_predictionerroro;
38 vector csqcplayer_predictionerrorv;
39 float csqcplayer_predictionerrortime;
40 float csqcplayer_predictionerrorfactor;
42 vector CSQCPlayer_GetPredictionErrorO()
44 if(time >= csqcplayer_predictionerrortime)
46 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
49 vector CSQCPlayer_GetPredictionErrorV()
51 if(time >= csqcplayer_predictionerrortime)
53 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
56 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
58 // error too big to compensate, we LIKELY hit a teleport or a
59 // jumppad, or it's a jump time disagreement that'll get fixed
62 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
64 // commented out as this one did not help
67 print(sprintf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v));
71 if(vlen(o) > 32 || vlen(v) > 192)
73 //print(sprintf("TOO BIG: x=%v v=%v\n", o, v));
77 if(!autocvar_cl_movement_errorcompensation)
79 csqcplayer_predictionerrorfactor = 0;
83 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
84 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
85 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
86 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
89 void CSQCPlayer_Unpredict()
91 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
93 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
94 error("Cannot unpredict in current status");
95 self.origin = csqcplayer_origin;
96 self.velocity = csqcplayer_velocity;
97 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
98 self.pmove_flags = player_pmflags;
101 void CSQCPlayer_SetMinsMaxs()
103 if(self.pmove_flags & PMF_DUCKED)
105 self.mins = PL_CROUCH_MIN;
106 self.maxs = PL_CROUCH_MAX;
107 self.view_ofs = PL_CROUCH_VIEW_OFS;
113 self.view_ofs = PL_VIEW_OFS;
117 void CSQCPlayer_SavePrediction()
119 player_pmflags = self.pmove_flags;
120 csqcplayer_origin = self.origin;
121 csqcplayer_velocity = self.velocity;
122 csqcplayer_sequence = servercommandframe;
123 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
126 void CSQCPlayer_PredictTo(float endframe, float apply_error)
128 CSQCPlayer_Unpredict();
131 self.origin += CSQCPlayer_GetPredictionErrorO();
132 self.velocity += CSQCPlayer_GetPredictionErrorV();
134 CSQCPlayer_SetMinsMaxs();
136 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
138 // FIXME do we really NEED this? dead players have servercommandframe
139 // == 0 and thus won't predict
140 if (getstatf(STAT_HEALTH) <= 0)
142 csqcplayer_moveframe = clientcommandframe;
143 getinputstate(csqcplayer_moveframe-1);
144 print("the Weird code path got hit\n");
148 if(csqcplayer_moveframe >= endframe)
150 getinputstate(csqcplayer_moveframe - 1);
156 if (!getinputstate(csqcplayer_moveframe))
158 runstandardplayerphysics(self);
159 CSQCPlayer_SetMinsMaxs();
160 csqcplayer_moveframe++;
162 while(csqcplayer_moveframe < endframe);
165 //add in anything that was applied after (for low packet rate protocols)
166 input_angles = view_angles;
169 float CSQCPlayer_IsLocalPlayer()
171 return (self == csqcplayer);
174 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
176 void CSQCPlayer_SetCamera()
179 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
187 #ifdef COMPAT_XON050_ENGINE
188 if(servercommandframe == 0 || clientcommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
190 if(servercommandframe == 0 || clientcommandframe == 0)
193 InterpolateOrigin_Do();
194 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
196 // get crouch state from the server
197 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
198 self.pmove_flags &~= PMF_DUCKED;
199 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
200 self.pmove_flags |= PMF_DUCKED;
202 // get onground state from the server
204 self.pmove_flags |= PMF_ONGROUND;
206 self.pmove_flags &~= PMF_ONGROUND;
208 CSQCPlayer_SetMinsMaxs();
210 // override it back just in case
211 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
218 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
223 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
224 CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
225 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.pmove_flags & PMF_ONGROUND));
229 // get crouch state from the server
230 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
231 self.pmove_flags &~= PMF_DUCKED;
232 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
233 self.pmove_flags |= PMF_DUCKED;
235 // get onground state from the server
237 self.pmove_flags |= PMF_ONGROUND;
239 self.pmove_flags &~= PMF_ONGROUND;
241 CSQCPlayer_SavePrediction();
243 CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
245 CSQCPlayer_SetMinsMaxs();
247 self.angles_y = input_angles_y;
251 setorigin(self, self.origin);
257 #ifdef COMPAT_XON050_ENGINE
258 view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
260 view = CSQCModel_server2csqc(player_localentnum);
263 if(view && view != csqcplayer)
265 entity oldself = self;
267 InterpolateOrigin_Do();
268 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
272 #ifdef COMPAT_XON050_ENGINE
273 if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
275 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
276 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
277 setproperty(VF_ANGLES, view_angles);
283 var float refdefflags = 0;
285 if(view.csqcmodel_teleported)
286 refdefflags |= REFDEFFLAG_TELEPORTED;
288 if(input_buttons & 4)
289 refdefflags |= REFDEFFLAG_JUMPING;
291 // note: these two only work in WIP2, but are harmless in WIP1
292 if(getstati(STAT_HEALTH) <= 0)
293 refdefflags |= REFDEFFLAG_DEAD;
296 refdefflags |= REFDEFFLAG_INTERMISSION;
298 V_CalcRefdef(view, refdefflags);
302 // FIXME by CSQC spec we have to do this:
303 // but it breaks chase cam
305 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
306 setproperty(VF_ANGLES, view_angles);
310 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
313 void CSQCPlayer_Remove()
316 cvar_settemp("cl_movement_replay", "1");
319 float CSQCPlayer_PreUpdate()
321 if(self != csqcplayer)
323 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
324 CSQCPlayer_Unpredict();
328 float CSQCPlayer_PostUpdate()
330 if(self.entnum != player_localnum + 1)
333 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
334 cvar_settemp("cl_movement_replay", "0");
335 self.entremove = CSQCPlayer_Remove;