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;
26 const int REFDEFFLAG_TELEPORTED = 1;
27 const int REFDEFFLAG_JUMPING = 2;
28 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
30 vector csqcplayer_origin, csqcplayer_velocity;
31 float csqcplayer_sequence, player_pmflags;
32 float csqcplayer_moveframe;
33 vector csqcplayer_predictionerroro;
34 vector csqcplayer_predictionerrorv;
35 float csqcplayer_predictionerrortime;
36 float csqcplayer_predictionerrorfactor;
38 vector CSQCPlayer_GetPredictionErrorO()
40 if(time >= csqcplayer_predictionerrortime)
42 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
45 vector CSQCPlayer_GetPredictionErrorV()
47 if(time >= csqcplayer_predictionerrortime)
49 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
52 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
54 // error too big to compensate, we LIKELY hit a teleport or a
55 // jumppad, or it's a jump time disagreement that'll get fixed
58 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
60 // commented out as this one did not help
63 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
67 if(vlen(o) > 32 || vlen(v) > 192)
69 //printf("TOO BIG: x=%v v=%v\n", o, v);
73 if(!autocvar_cl_movement_errorcompensation)
75 csqcplayer_predictionerrorfactor = 0;
79 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
80 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
81 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
82 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
85 void CSQCPlayer_Unpredict()
87 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
89 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
90 error("Cannot unpredict in current status");
91 self.origin = csqcplayer_origin;
92 self.velocity = csqcplayer_velocity;
93 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
94 self.pmove_flags = player_pmflags;
97 void CSQCPlayer_SetMinsMaxs()
99 if(self.pmove_flags & PMF_DUCKED)
101 self.mins = PL_CROUCH_MIN;
102 self.maxs = PL_CROUCH_MAX;
103 self.view_ofs = PL_CROUCH_VIEW_OFS;
109 self.view_ofs = PL_VIEW_OFS;
113 void CSQCPlayer_SavePrediction()
115 player_pmflags = self.pmove_flags;
116 csqcplayer_origin = self.origin;
117 csqcplayer_velocity = self.velocity;
118 csqcplayer_sequence = servercommandframe;
119 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
122 void CSQCPlayer_PredictTo(float endframe, float apply_error)
124 CSQCPlayer_Unpredict();
127 self.origin += CSQCPlayer_GetPredictionErrorO();
128 self.velocity += CSQCPlayer_GetPredictionErrorV();
130 CSQCPlayer_SetMinsMaxs();
132 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
135 // we don't need this
136 // darkplaces makes servercommandframe == 0 in these cases anyway
137 if (getstatf(STAT_HEALTH) <= 0)
139 csqcplayer_moveframe = clientcommandframe;
140 getinputstate(csqcplayer_moveframe-1);
141 print("the Weird code path got hit\n");
146 if(csqcplayer_moveframe >= endframe)
148 getinputstate(csqcplayer_moveframe - 1);
154 if (!getinputstate(csqcplayer_moveframe))
156 runstandardplayerphysics(self);
157 CSQCPlayer_SetMinsMaxs();
158 csqcplayer_moveframe++;
160 while(csqcplayer_moveframe < endframe);
163 //add in anything that was applied after (for low packet rate protocols)
164 input_angles = view_angles;
167 float CSQCPlayer_IsLocalPlayer()
169 return (self == csqcplayer);
172 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
174 void CSQCPlayer_SetCamera()
177 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
185 if(servercommandframe == 0 || clientcommandframe == 0)
187 InterpolateOrigin_Do();
188 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
190 // get crouch state from the server
191 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
192 self.pmove_flags &= ~PMF_DUCKED;
193 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
194 self.pmove_flags |= PMF_DUCKED;
196 // get onground state from the server
198 self.pmove_flags |= PMF_ONGROUND;
200 self.pmove_flags &= ~PMF_ONGROUND;
202 CSQCPlayer_SetMinsMaxs();
204 // override it back just in case
205 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
212 float flg = self.iflags;
213 self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
214 InterpolateOrigin_Do();
217 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
222 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
223 CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
224 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.pmove_flags & PMF_ONGROUND));
228 // get crouch state from the server
229 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
230 self.pmove_flags &= ~PMF_DUCKED;
231 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
232 self.pmove_flags |= PMF_DUCKED;
234 // get onground state from the server
236 self.pmove_flags |= PMF_ONGROUND;
238 self.pmove_flags &= ~PMF_ONGROUND;
240 CSQCPlayer_SavePrediction();
242 CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
244 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
245 // get crouch state from the server (LAG)
246 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
247 self.pmove_flags &= ~PMF_DUCKED;
248 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
249 self.pmove_flags |= PMF_DUCKED;
252 CSQCPlayer_SetMinsMaxs();
254 self.angles_y = input_angles_y;
258 setorigin(self, self.origin);
264 view = CSQCModel_server2csqc(player_localentnum);
266 if(view && view != csqcplayer)
268 entity oldself = self;
270 InterpolateOrigin_Do();
271 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
279 if(view.csqcmodel_teleported)
280 refdefflags |= REFDEFFLAG_TELEPORTED;
282 if(input_buttons & 4)
283 refdefflags |= REFDEFFLAG_JUMPING;
285 // note: these two only work in WIP2, but are harmless in WIP1
286 if(getstati(STAT_HEALTH) <= 0)
287 refdefflags |= REFDEFFLAG_DEAD;
290 refdefflags |= REFDEFFLAG_INTERMISSION;
292 V_CalcRefdef(view, refdefflags);
296 // FIXME by CSQC spec we have to do this:
297 // but it breaks chase cam
299 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
300 setproperty(VF_ANGLES, view_angles);
304 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
307 void CSQCPlayer_Remove()
310 cvar_settemp("cl_movement_replay", "1");
313 float CSQCPlayer_PreUpdate()
315 if(self != csqcplayer)
317 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
318 CSQCPlayer_Unpredict();
322 float CSQCPlayer_PostUpdate()
324 if(self.entnum != player_localnum + 1)
327 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
328 cvar_settemp("cl_movement_replay", "0");
329 self.entremove = CSQCPlayer_Remove;