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
23 #include "cl_player.qh"
25 #include "cl_model.qh"
27 #include "interpolate.qh"
29 float autocvar_cl_movement_errorcompensation = 0;
30 bool autocvar_cl_movement_intermissionrunning = false;
33 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
35 vector csqcplayer_origin, csqcplayer_velocity;
36 float csqcplayer_sequence;
38 float csqcplayer_moveframe;
39 vector csqcplayer_predictionerroro;
40 vector csqcplayer_predictionerrorv;
41 float csqcplayer_predictionerrortime;
42 float csqcplayer_predictionerrorfactor;
44 vector CSQCPlayer_GetPredictionErrorO()
46 if (time >= csqcplayer_predictionerrortime) return '0 0 0';
47 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
50 vector CSQCPlayer_GetPredictionErrorV()
52 if (time >= csqcplayer_predictionerrortime) return '0 0 0';
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 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
71 if(vdist(o, >, 32) || vdist(v, >, 192))
73 //printf("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(entity this)
91 if (csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED) return;
92 if (csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED) LOG_FATALF("Cannot unpredict in current status (%d)", csqcplayer_status);
93 this.origin = csqcplayer_origin;
94 this.velocity = csqcplayer_velocity;
95 csqcplayer_moveframe = csqcplayer_sequence + 1; // + 1 because the recieved frame has the move already done (server side)
96 this.flags = player_pmflags;
99 void CSQCPlayer_SetMinsMaxs(entity this)
101 if (IS_DUCKED(this) || !this.isplayermodel)
103 this.mins = PHYS_PL_CROUCH_MIN(this);
104 this.maxs = PHYS_PL_CROUCH_MAX(this);
105 this.view_ofs = PHYS_PL_CROUCH_VIEWOFS(this);
109 this.mins = PHYS_PL_MIN(this);
110 this.maxs = PHYS_PL_MAX(this);
111 this.view_ofs = PHYS_PL_VIEWOFS(this);
115 void CSQCPlayer_SavePrediction(entity this)
117 player_pmflags = this.flags;
118 csqcplayer_origin = this.origin;
119 csqcplayer_velocity = this.velocity;
120 csqcplayer_sequence = servercommandframe;
121 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
124 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
126 void CSQCPlayer_Physics(entity this)
128 if(!autocvar_cl_movement) { return; }
130 _Movetype_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
132 vector oldv_angle = this.v_angle;
133 vector oldangles = this.angles; // we need to save these, as they're abused by other code
134 this.v_angle = PHYS_INPUT_ANGLES(this);
135 this.angles = PHYS_WORLD_ANGLES(this);
137 CSQC_ClientMovement_PlayerMove_Frame(this);
139 Movetype_Physics_NoMatchTicrate(this, PHYS_INPUT_TIMELENGTH, true);
141 view_angles = this.v_angle;
142 input_angles = this.angles;
143 this.v_angle = oldv_angle;
144 this.angles = oldangles;
147 ((IS_DUCKED(this)) ? PMF_DUCKED : 0) |
148 ((IS_JUMP_HELD(this)) ? PMF_JUMP_HELD : 0) |
149 ((IS_ONGROUND(this)) ? PMF_ONGROUND : 0);
152 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
154 CSQCPlayer_Unpredict(this);
157 this.origin += CSQCPlayer_GetPredictionErrorO();
158 this.velocity += CSQCPlayer_GetPredictionErrorV();
160 CSQCPlayer_SetMinsMaxs(this);
162 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
165 // we don't need this
166 // darkplaces makes servercommandframe == 0 in these cases anyway
167 if (STAT(HEALTH) <= 0)
169 csqcplayer_moveframe = clientcommandframe;
170 getinputstate(csqcplayer_moveframe-1);
171 LOG_INFO("the Weird code path got hit");
176 if (csqcplayer_moveframe >= endframe)
178 getinputstate(csqcplayer_moveframe - 1);
184 if (!getinputstate(csqcplayer_moveframe)) break;
185 /*if (input_timelength > 0.0005)
187 if (input_timelength > 0.05)
189 input_timelength /= 2;
190 CSQCPlayer_Physics(this);
192 CSQCPlayer_Physics(this);
194 CSQCPlayer_Physics(this);
195 CSQCPlayer_SetMinsMaxs(this);
196 ++csqcplayer_moveframe;
198 while (csqcplayer_moveframe < endframe);
201 // add in anything that was applied after (for low packet rate protocols)
202 input_angles = view_angles;
205 bool CSQCPlayer_IsLocalPlayer(entity this)
207 return (this == csqcplayer);
211 float autocvar_cl_stairsmoothspeed;
212 float autocvar_cl_smoothviewheight;
213 float smooth_prevtime;
215 float autocvar_cl_bobfall;
216 float autocvar_cl_bobfallcycle;
217 float autocvar_cl_bobfallminspeed;
220 vector CSQCPlayer_ApplySmoothing(entity this, vector v)
222 if(this.csqcmodel_teleported || !IS_ONGROUND(this) || autocvar_cl_stairsmoothspeed <= 0)
226 if(v.z > stairsmoothz)
227 v.z = stairsmoothz = bound(v.z - PHYS_STEPHEIGHT(this), stairsmoothz + frametime * autocvar_cl_stairsmoothspeed, v.z);
228 else if(v.z < stairsmoothz)
229 v.z = stairsmoothz = bound(v.z, stairsmoothz - frametime * autocvar_cl_stairsmoothspeed, v.z + PHYS_STEPHEIGHT(this));
232 float viewheight = bound(0, (time - smooth_prevtime) / max(0.0001, autocvar_cl_smoothviewheight), 1);
233 viewheightavg = viewheightavg * (1 - viewheight) + this.view_ofs.z * viewheight;
234 v.z += viewheightavg;
236 if(autocvar_cl_bobfall && autocvar_cl_bobfallcycle)
238 if(!IS_ONGROUND(this))
240 bobfall_speed = bound(-400, this.velocity.z, 0) * bound(0, autocvar_cl_bobfall, 0.1);
241 if(this.velocity.z < -autocvar_cl_bobfallminspeed)
244 bobfall_swing = 0; // really?
248 bobfall_swing = max(0, bobfall_swing - autocvar_cl_bobfallcycle * frametime);
249 float bobfall = sin(M_PI * bobfall_swing) * bobfall_speed;
254 smooth_prevtime = time;
259 bool autocvar_v_deathtilt;
260 float autocvar_v_deathtiltangle;
261 void CSQCPlayer_ApplyDeathTilt(entity this)
263 if(!autocvar_v_deathtilt)
265 view_angles.y = autocvar_v_deathtiltangle;
268 float autocvar_v_idlescale;
269 float autocvar_v_ipitch_cycle;
270 float autocvar_v_iyaw_cycle;
271 float autocvar_v_iroll_cycle;
272 float autocvar_v_ipitch_level;
273 float autocvar_v_iyaw_level;
274 float autocvar_v_iroll_level;
275 void CSQCPlayer_ApplyIdleScaling(entity this)
277 view_angles.x += autocvar_v_idlescale * sin(time * autocvar_v_ipitch_cycle) * autocvar_v_ipitch_level;
278 view_angles.y += autocvar_v_idlescale * sin(time * autocvar_v_iyaw_cycle) * autocvar_v_iyaw_level;
279 view_angles.z += autocvar_v_idlescale * sin(time * autocvar_v_iroll_cycle) * autocvar_v_iroll_level;
280 setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
283 float autocvar_chase_back;
284 float autocvar_chase_up;
285 vector CSQCPlayer_ApplyChase(entity this, vector v)
287 // TODO: chase_overhead
289 makevectors(view_angles);
290 tracebox(v, '-4 -4 -4', '4 4 4', v - v_forward * autocvar_chase_back, MOVE_NORMAL, this);
292 tracebox(v, '-4 -4 -4', '4 4 4', v + v_up * autocvar_chase_up, MOVE_NORMAL, this);
297 /** Called once per CSQC_UpdateView() */
298 void CSQCPlayer_SetCamera()
300 vector v0 = ((intermission && !autocvar_cl_movement_intermissionrunning) ? '0 0 0' : pmove_vel); // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
301 float vh = PHYS_VIEWHEIGHT(NULL);
302 vector pl_viewofs = PHYS_PL_VIEWOFS(NULL);
303 vector pl_viewofs_crouch = PHYS_PL_CROUCH_VIEWOFS(NULL);
304 entity e = csqcplayer;
307 if (servercommandframe == 0 || clientcommandframe == 0)
309 InterpolateOrigin_Do(e);
310 e.view_ofs = '0 0 1' * vh;
312 // get crouch state from the server
313 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
314 else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
316 // get onground state from the server
317 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
319 CSQCPlayer_SetMinsMaxs(e);
321 // override it back just in case
322 e.view_ofs = '0 0 1' * vh;
329 int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
330 InterpolateOrigin_Do(e);
333 if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
336 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
337 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
338 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
342 // get crouch state from the server
343 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
344 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
346 // get onground state from the server
347 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
349 CSQCPlayer_SavePrediction(e);
351 CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
353 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
354 // get crouch state from the server (LAG)
355 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
356 else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
358 CSQCPlayer_SetMinsMaxs(e);
360 e.angles_y = input_angles.y;
364 setorigin(e, e.origin);
367 const entity view = CSQCModel_server2csqc(player_localentnum - 1);
370 if (view != csqcplayer)
372 InterpolateOrigin_Do(view);
373 view.view_ofs = '0 0 1' * vh;
375 //int refdefflags = 0;
376 //if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
377 //if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
378 // note: these two only work in WIP2, but are harmless in WIP1
379 //if (PHYS_HEALTH(NULL) <= 0 && PHYS_HEALTH(NULL) != -666 && PHYS_HEALTH(NULL) != -2342) refdefflags |= REFDEFFLAG_DEAD;
380 //if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
381 vector vieworg = view.origin;
382 if(autocvar_chase_active)
383 vieworg = CSQCPlayer_ApplyChase(view, vieworg);
386 vieworg = CSQCPlayer_ApplySmoothing(view, vieworg);
388 CSQCPlayer_ApplyDeathTilt(view);
390 if(autocvar_v_idlescale)
391 CSQCPlayer_ApplyIdleScaling(view);
392 setproperty(VF_ORIGIN, vieworg);
393 setproperty(VF_ANGLES, view_angles);
394 //V_CalcRefdef(view, refdefflags); // TODO? uses .health stat in the engine when this isn't called here, may be broken!
398 // FIXME by CSQC spec we have to do this:
399 // but it breaks chase cam
401 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
402 setproperty(VF_ANGLES, view_angles);
405 CSQCPLAYER_HOOK_POSTCAMERASETUP();
408 void CSQCPlayer_Remove(entity this)
411 cvar_settemp("cl_movement_replay", "1");
414 bool CSQCPlayer_PreUpdate(entity this)
416 if (this != csqcplayer) return false;
417 if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
421 bool CSQCPlayer_PostUpdate(entity this)
423 if (this.entnum != player_localnum + 1) return false;
425 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
426 cvar_settemp("cl_movement_replay", "0");
427 this.entremove = CSQCPlayer_Remove;