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
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;
41 float csqcplayer_predictionerrorfactor;
43 vector CSQCPlayer_GetPredictionErrorO()
45 if(time >= csqcplayer_predictionerrortime)
47 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
50 vector CSQCPlayer_GetPredictionErrorV()
52 if(time >= csqcplayer_predictionerrortime)
54 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
57 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
59 // error too big to compensate, we LIKELY hit a teleport or a
60 // jumppad, or it's a jump time disagreement that'll get fixed
63 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
65 // commented out as this one did not help
68 print(sprintf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v));
72 if(vlen(o) > 32 || vlen(v) > 192)
74 //print(sprintf("TOO BIG: x=%v v=%v\n", o, v));
78 if(!autocvar_cl_movement_errorcompensation)
80 csqcplayer_predictionerrorfactor = 0;
84 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
85 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
86 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
87 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
90 void CSQCPlayer_Unpredict()
92 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
94 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
95 error("Cannot unpredict in current status");
96 self.origin = csqcplayer_origin;
97 self.velocity = csqcplayer_velocity;
98 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
99 self.pmove_flags = player_pmflags;
102 void CSQCPlayer_SetMinsMaxs()
104 if(self.pmove_flags & PMF_DUCKED)
106 self.mins = PL_CROUCH_MIN;
107 self.maxs = PL_CROUCH_MAX;
108 self.view_ofs = PL_CROUCH_VIEW_OFS;
114 self.view_ofs = PL_VIEW_OFS;
118 void CSQCPlayer_SavePrediction()
120 player_pmflags = self.pmove_flags;
121 csqcplayer_origin = self.origin;
122 csqcplayer_velocity = self.velocity;
123 csqcplayer_sequence = servercommandframe;
124 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
127 void CSQCPlayer_PredictTo(float endframe, float apply_error)
129 CSQCPlayer_Unpredict();
132 self.origin += CSQCPlayer_GetPredictionErrorO();
133 self.velocity += CSQCPlayer_GetPredictionErrorV();
135 CSQCPlayer_SetMinsMaxs();
137 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
139 // FIXME do we really NEED this? dead players have servercommandframe
140 // == 0 and thus won't predict
141 if (getstatf(STAT_HEALTH) <= 0)
143 csqcplayer_moveframe = clientcommandframe;
144 getinputstate(csqcplayer_moveframe-1);
145 print("the Weird code path got hit\n");
149 if(csqcplayer_moveframe >= endframe)
151 getinputstate(csqcplayer_moveframe - 1);
157 if (!getinputstate(csqcplayer_moveframe))
159 runstandardplayerphysics(self);
160 CSQCPlayer_SetMinsMaxs();
161 csqcplayer_moveframe++;
163 while(csqcplayer_moveframe < endframe);
166 //add in anything that was applied after (for low packet rate protocols)
167 input_angles = view_angles;
170 float CSQCPlayer_IsLocalPlayer()
172 return (self == csqcplayer);
175 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
177 void CSQCPlayer_SetCamera()
180 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
188 #ifdef COMPAT_XON050_ENGINE
189 if(servercommandframe == 0 || clientcommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
191 if(servercommandframe == 0 || clientcommandframe == 0)
194 InterpolateOrigin_Do();
195 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
197 // get crouch state from the server
198 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
199 self.pmove_flags &~= PMF_DUCKED;
200 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
201 self.pmove_flags |= PMF_DUCKED;
203 // get onground state from the server
205 self.pmove_flags |= PMF_ONGROUND;
207 self.pmove_flags &~= PMF_ONGROUND;
209 CSQCPlayer_SetMinsMaxs();
211 // override it back just in case
212 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
219 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
224 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
225 CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
226 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.pmove_flags & PMF_ONGROUND));
230 // get crouch state from the server
231 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
232 self.pmove_flags &~= PMF_DUCKED;
233 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
234 self.pmove_flags |= PMF_DUCKED;
236 // get onground state from the server
238 self.pmove_flags |= PMF_ONGROUND;
240 self.pmove_flags &~= PMF_ONGROUND;
242 CSQCPlayer_SavePrediction();
244 CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
246 CSQCPlayer_SetMinsMaxs();
248 self.angles_y = input_angles_y;
252 setorigin(self, self.origin);
258 #ifdef COMPAT_XON050_ENGINE
259 view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
261 view = CSQCModel_server2csqc(player_localentnum);
264 if(view && view != csqcplayer)
266 entity oldself = self;
268 InterpolateOrigin_Do();
269 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
273 #ifdef COMPAT_XON050_ENGINE
274 if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
276 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
277 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
278 setproperty(VF_ANGLES, view_angles);
284 var float refdefflags = 0;
286 if(view.csqcmodel_teleported)
287 refdefflags |= REFDEFFLAG_TELEPORTED;
289 if(input_buttons & 4)
290 refdefflags |= REFDEFFLAG_JUMPING;
292 // note: these two only work in WIP2, but are harmless in WIP1
293 if(getstati(STAT_HEALTH) <= 0)
294 refdefflags |= REFDEFFLAG_DEAD;
297 refdefflags |= REFDEFFLAG_INTERMISSION;
299 V_CalcRefdef(view, refdefflags);
303 // FIXME by CSQC spec we have to do this:
304 // but it breaks chase cam
306 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
307 setproperty(VF_ANGLES, view_angles);
311 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
314 void CSQCPlayer_Remove()
317 cvar_settemp("cl_movement_replay", "1");
320 float CSQCPlayer_PreUpdate()
322 if(self != csqcplayer)
324 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
325 CSQCPlayer_Unpredict();
329 float CSQCPlayer_PostUpdate()
331 if(self.entnum != player_localnum + 1)
334 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
335 cvar_settemp("cl_movement_replay", "0");
336 self.entremove = CSQCPlayer_Remove;