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"
28 #include <client/defs.qh>
29 #include <client/main.qh>
30 #include <common/constants.qh>
31 #include <common/physics/player.qh>
32 #include <common/stats.qh>
33 #include <common/triggers/trigger/viewloc.qh>
34 #include <common/util.qh>
35 #include <common/viewloc.qh>
37 float autocvar_cl_movement_errorcompensation = 0;
38 bool autocvar_cl_movement_intermissionrunning = false;
41 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
43 vector csqcplayer_origin, csqcplayer_velocity;
44 float csqcplayer_sequence;
46 float csqcplayer_moveframe;
47 vector csqcplayer_predictionerroro;
48 vector csqcplayer_predictionerrorv;
49 float csqcplayer_predictionerrortime;
50 float csqcplayer_predictionerrorfactor;
52 vector CSQCPlayer_GetPredictionErrorO()
54 if (time >= csqcplayer_predictionerrortime) return '0 0 0';
55 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
58 vector CSQCPlayer_GetPredictionErrorV()
60 if (time >= csqcplayer_predictionerrortime) return '0 0 0';
61 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
64 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
66 // error too big to compensate, we LIKELY hit a teleport or a
67 // jumppad, or it's a jump time disagreement that'll get fixed
70 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
72 // commented out as this one did not help
75 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
79 if(vdist(o, >, 32) || vdist(v, >, 192))
81 //printf("TOO BIG: x=%v v=%v\n", o, v);
85 if(!autocvar_cl_movement_errorcompensation)
87 csqcplayer_predictionerrorfactor = 0;
91 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
92 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
93 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
94 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
97 void CSQCPlayer_Unpredict(entity this)
99 if (csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED) return;
100 if (csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED) LOG_FATALF("Cannot unpredict in current status (%d)", csqcplayer_status);
101 this.origin = csqcplayer_origin;
102 this.velocity = csqcplayer_velocity;
103 csqcplayer_moveframe = csqcplayer_sequence + 1; // + 1 because the recieved frame has the move already done (server side)
104 this.flags = player_pmflags;
107 void CSQCPlayer_SetMinsMaxs(entity this)
109 if (IS_DUCKED(this) || !this.isplayermodel)
111 this.mins = STAT(PL_CROUCH_MIN, NULL);
112 this.maxs = STAT(PL_CROUCH_MAX, NULL);
113 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, NULL);
117 this.mins = STAT(PL_MIN, NULL);
118 this.maxs = STAT(PL_MAX, NULL);
119 this.view_ofs = STAT(PL_VIEW_OFS, NULL);
123 void CSQCPlayer_SavePrediction(entity this)
125 player_pmflags = this.flags;
126 csqcplayer_origin = this.origin;
127 csqcplayer_velocity = this.velocity;
128 csqcplayer_sequence = servercommandframe;
129 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
132 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
134 void CSQCPlayer_Physics(entity this)
136 if(!autocvar_cl_movement) { return; }
138 _Movetype_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
140 vector oldv_angle = this.v_angle;
141 vector oldangles = this.angles; // we need to save these, as they're abused by other code
142 this.v_angle = PHYS_INPUT_ANGLES(this);
143 this.angles = PHYS_WORLD_ANGLES(this);
145 CSQC_ClientMovement_PlayerMove_Frame(this);
147 Movetype_Physics_NoMatchTicrate(this, PHYS_INPUT_TIMELENGTH, true);
149 view_angles = this.v_angle;
150 input_angles = this.angles;
151 this.v_angle = oldv_angle;
152 this.angles = oldangles;
155 ((IS_DUCKED(this)) ? PMF_DUCKED : 0) |
156 ((IS_JUMP_HELD(this)) ? PMF_JUMP_HELD : 0) |
157 ((IS_ONGROUND(this)) ? PMF_ONGROUND : 0);
160 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
162 CSQCPlayer_Unpredict(this);
165 this.origin += CSQCPlayer_GetPredictionErrorO();
166 this.velocity += CSQCPlayer_GetPredictionErrorV();
168 CSQCPlayer_SetMinsMaxs(this);
170 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
173 // we don't need this
174 // darkplaces makes servercommandframe == 0 in these cases anyway
175 if (STAT(HEALTH) <= 0)
177 csqcplayer_moveframe = clientcommandframe;
178 getinputstate(csqcplayer_moveframe-1);
179 LOG_INFO("the Weird code path got hit\n");
184 if (csqcplayer_moveframe >= endframe)
186 getinputstate(csqcplayer_moveframe - 1);
192 if (!getinputstate(csqcplayer_moveframe)) break;
193 /*if (input_timelength > 0.0005)
195 if (input_timelength > 0.05)
197 input_timelength /= 2;
198 CSQCPlayer_Physics(this);
200 CSQCPlayer_Physics(this);
202 CSQCPlayer_Physics(this);
203 CSQCPlayer_SetMinsMaxs(this);
204 ++csqcplayer_moveframe;
206 while (csqcplayer_moveframe < endframe);
209 // add in anything that was applied after (for low packet rate protocols)
210 input_angles = view_angles;
213 bool CSQCPlayer_IsLocalPlayer(entity this)
215 return (this == csqcplayer);
218 void CSQCPlayer_SetViewLocation()
220 viewloc_SetViewLocation();
223 /** Called once per CSQC_UpdateView() */
224 void CSQCPlayer_SetCamera()
226 const 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
227 const float vh = STAT(VIEWHEIGHT);
228 const vector pl_viewofs = STAT(PL_VIEW_OFS, NULL);
229 const vector pl_viewofs_crouch = STAT(PL_CROUCH_VIEW_OFS, NULL);
230 const entity e = csqcplayer;
233 if (servercommandframe == 0 || clientcommandframe == 0)
235 InterpolateOrigin_Do(e);
236 e.view_ofs = '0 0 1' * vh;
238 // get crouch state from the server
239 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
240 else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
242 // get onground state from the server
243 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
245 CSQCPlayer_SetMinsMaxs(e);
247 // override it back just in case
248 e.view_ofs = '0 0 1' * vh;
255 const int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
256 InterpolateOrigin_Do(e);
259 if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
261 const vector o = e.origin;
262 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
263 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
264 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
268 // get crouch state from the server
269 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
270 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
272 // get onground state from the server
273 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
275 CSQCPlayer_SavePrediction(e);
277 CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
279 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
280 // get crouch state from the server (LAG)
281 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
282 else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
284 CSQCPlayer_SetMinsMaxs(e);
286 e.angles_y = input_angles.y;
290 setorigin(e, e.origin);
293 const entity view = CSQCModel_server2csqc(player_localentnum - 1);
296 if (view != csqcplayer)
298 InterpolateOrigin_Do(view);
299 view.view_ofs = '0 0 1' * vh;
302 if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
303 if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
304 // note: these two only work in WIP2, but are harmless in WIP1
305 if (STAT(HEALTH) <= 0 && STAT(HEALTH) != -666 && STAT(HEALTH) != -2342) refdefflags |= REFDEFFLAG_DEAD;
306 if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
307 V_CalcRefdef(view, refdefflags);
311 // FIXME by CSQC spec we have to do this:
312 // but it breaks chase cam
314 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
315 setproperty(VF_ANGLES, view_angles);
318 CSQCPLAYER_HOOK_POSTCAMERASETUP();
321 void CSQCPlayer_Remove(entity this)
324 cvar_settemp("cl_movement_replay", "1");
327 bool CSQCPlayer_PreUpdate(entity this)
329 if (this != csqcplayer) return false;
330 if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
334 bool CSQCPlayer_PostUpdate(entity this)
336 if (this.entnum != player_localnum + 1) return false;
338 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
339 cvar_settemp("cl_movement_replay", "0");
340 this.entremove = CSQCPlayer_Remove;