2 * Copyright (c) 2011 Rudolf Polzer
3 * Copyright (c) 2015 Micah Talkiewicz
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "../dpdefs/csprogsdefs.qh"
25 #include "../client/defs.qh"
26 #include "../common/constants.qh"
27 #include "../common/p2mathlib.qh"
28 #include "../common/stats.qh"
29 #include "../common/util.qh"
30 #include "interpolate.qh"
31 #include "../client/main.qh"
33 #include "cl_model.qh"
34 #include "cl_player.qh"
35 #include "../common/triggers/trigger/viewloc.qh"
36 #include "../common/viewloc.qh"
41 float autocvar_cl_movement_errorcompensation = 0;
42 int autocvar_cl_movement = 1;
45 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
47 vector csqcplayer_origin, csqcplayer_velocity;
48 float csqcplayer_sequence;
50 float csqcplayer_moveframe;
51 vector csqcplayer_predictionerroro;
52 vector csqcplayer_predictionerrorv;
53 float csqcplayer_predictionerrortime;
54 float csqcplayer_predictionerrorfactor;
56 vector CSQCPlayer_GetPredictionErrorO()
58 if(time >= csqcplayer_predictionerrortime)
60 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
63 vector CSQCPlayer_GetPredictionErrorV()
65 if(time >= csqcplayer_predictionerrortime)
67 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
70 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
72 // error too big to compensate, we LIKELY hit a teleport or a
73 // jumppad, or it's a jump time disagreement that'll get fixed
76 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
78 // commented out as this one did not help
81 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
85 if(vlen(o) > 32 || vlen(v) > 192)
87 //printf("TOO BIG: x=%v v=%v\n", o, v);
91 if(!autocvar_cl_movement_errorcompensation)
93 csqcplayer_predictionerrorfactor = 0;
97 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
98 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
99 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
100 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
103 void CSQCPlayer_Unpredict()
105 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
107 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
108 error("Cannot unpredict in current status");
109 self.origin = csqcplayer_origin;
110 self.velocity = csqcplayer_velocity;
111 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
112 self.flags = player_pmflags;
115 void CSQCPlayer_SetMinsMaxs()
117 if(self.flags & FL_DUCKED)
119 self.mins = PL_CROUCH_MIN;
120 self.maxs = PL_CROUCH_MAX;
121 self.view_ofs = PL_CROUCH_VIEW_OFS;
127 self.view_ofs = PL_VIEW_OFS;
131 void CSQCPlayer_SavePrediction()
133 player_pmflags = self.flags;
134 csqcplayer_origin = self.origin;
135 csqcplayer_velocity = self.velocity;
136 csqcplayer_sequence = servercommandframe;
137 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
140 void CSQC_ClientMovement_PlayerMove_Frame();
142 void PM_Movement_Move()
144 runstandardplayerphysics(self);
147 ((self.pmove_flags & PMF_DUCKED) ? FL_DUCKED : 0) |
148 (!(self.pmove_flags & PMF_JUMP_HELD) ? FL_JUMPRELEASED : 0) |
149 ((self.pmove_flags & PMF_ONGROUND) ? FL_ONGROUND : 0);
153 void CSQCPlayer_Physics(void)
155 switch(autocvar_cl_movement)
157 case 1: CSQC_ClientMovement_PlayerMove_Frame(); break;
158 case 2: PM_Movement_Move(); break;
162 void CSQCPlayer_PredictTo(float endframe, float apply_error)
164 CSQCPlayer_Unpredict();
167 self.origin += CSQCPlayer_GetPredictionErrorO();
168 self.velocity += CSQCPlayer_GetPredictionErrorV();
170 CSQCPlayer_SetMinsMaxs();
172 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
175 // we don't need this
176 // darkplaces makes servercommandframe == 0 in these cases anyway
177 if (getstatf(STAT_HEALTH) <= 0)
179 csqcplayer_moveframe = clientcommandframe;
180 getinputstate(csqcplayer_moveframe-1);
181 print("the Weird code path got hit\n");
186 if(csqcplayer_moveframe >= endframe)
188 getinputstate(csqcplayer_moveframe - 1);
194 if (!getinputstate(csqcplayer_moveframe))
196 CSQCPlayer_Physics();
197 CSQCPlayer_SetMinsMaxs();
198 csqcplayer_moveframe++;
200 while(csqcplayer_moveframe < endframe);
203 //add in anything that was applied after (for low packet rate protocols)
204 input_angles = view_angles;
207 bool CSQCPlayer_IsLocalPlayer()
209 return (self == csqcplayer);
212 void CSQCPlayer_SetViewLocation()
214 viewloc_SetViewLocation();
217 void CSQCPlayer_SetCamera()
220 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
228 if(servercommandframe == 0 || clientcommandframe == 0)
230 InterpolateOrigin_Do();
231 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
233 // get crouch state from the server
234 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
235 self.flags &= ~FL_DUCKED;
236 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
237 self.flags |= FL_DUCKED;
239 // get onground state from the server
241 self.flags |= FL_ONGROUND;
243 self.flags &= ~FL_ONGROUND;
245 CSQCPlayer_SetMinsMaxs();
247 // override it back just in case
248 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
255 float flg = self.iflags;
256 self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
257 InterpolateOrigin_Do();
260 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
265 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
266 CSQCPlayer_PredictTo(servercommandframe + 1, false);
267 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
271 // get crouch state from the server
272 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
273 self.flags &= ~FL_DUCKED;
274 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
275 self.flags |= FL_DUCKED;
277 // get onground state from the server
279 self.flags |= FL_ONGROUND;
281 self.flags &= ~FL_ONGROUND;
283 CSQCPlayer_SavePrediction();
285 CSQCPlayer_PredictTo(clientcommandframe + 1, true);
287 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
288 // get crouch state from the server (LAG)
289 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
290 self.flags &= ~FL_DUCKED;
291 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
292 self.flags |= FL_DUCKED;
295 CSQCPlayer_SetMinsMaxs();
297 self.angles_y = input_angles.y;
301 setorigin(self, self.origin);
307 view = CSQCModel_server2csqc(player_localentnum);
309 if(view && view != csqcplayer)
311 entity oldself = self;
313 InterpolateOrigin_Do();
314 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
322 if(view.csqcmodel_teleported)
323 refdefflags |= REFDEFFLAG_TELEPORTED;
325 if(input_buttons & 4)
326 refdefflags |= REFDEFFLAG_JUMPING;
328 // note: these two only work in WIP2, but are harmless in WIP1
329 if(getstati(STAT_HEALTH) <= 0)
330 refdefflags |= REFDEFFLAG_DEAD;
333 refdefflags |= REFDEFFLAG_INTERMISSION;
335 V_CalcRefdef(view, refdefflags);
339 // FIXME by CSQC spec we have to do this:
340 // but it breaks chase cam
342 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
343 setproperty(VF_ANGLES, view_angles);
347 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
350 void CSQCPlayer_Remove()
353 cvar_settemp("cl_movement_replay", "1");
356 float CSQCPlayer_PreUpdate()
358 if(self != csqcplayer)
360 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
361 CSQCPlayer_Unpredict();
365 float CSQCPlayer_PostUpdate()
367 if(self.entnum != player_localnum + 1)
370 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
371 cvar_settemp("cl_movement_replay", "0");
372 self.entremove = CSQCPlayer_Remove;