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.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;
40 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
42 vector csqcplayer_origin, csqcplayer_velocity;
43 float csqcplayer_sequence;
45 float csqcplayer_moveframe;
46 vector csqcplayer_predictionerroro;
47 vector csqcplayer_predictionerrorv;
48 float csqcplayer_predictionerrortime;
49 float csqcplayer_predictionerrorfactor;
51 vector CSQCPlayer_GetPredictionErrorO()
53 if (time >= csqcplayer_predictionerrortime) return '0 0 0';
54 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
57 vector CSQCPlayer_GetPredictionErrorV()
59 if (time >= csqcplayer_predictionerrortime) return '0 0 0';
60 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
63 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
65 // error too big to compensate, we LIKELY hit a teleport or a
66 // jumppad, or it's a jump time disagreement that'll get fixed
69 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
71 // commented out as this one did not help
74 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
78 if(vlen(o) > 32 || vlen(v) > 192)
80 //printf("TOO BIG: x=%v v=%v\n", o, v);
84 if(!autocvar_cl_movement_errorcompensation)
86 csqcplayer_predictionerrorfactor = 0;
90 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
91 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
92 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
93 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
96 void CSQCPlayer_Unpredict(entity this)
98 if (csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED) return;
99 if (csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED) LOG_FATALF("Cannot unpredict in current status (%d)\n", csqcplayer_status);
100 this.origin = csqcplayer_origin;
101 this.velocity = csqcplayer_velocity;
102 csqcplayer_moveframe = csqcplayer_sequence + 1; // + 1 because the recieved frame has the move already done (server side)
103 this.flags = player_pmflags;
106 void CSQCPlayer_SetMinsMaxs(entity this)
108 if ((this.flags & FL_DUCKED) || !this.isplayermodel)
110 this.mins = PL_CROUCH_MIN;
111 this.maxs = PL_CROUCH_MAX;
112 this.view_ofs = PL_CROUCH_VIEW_OFS;
118 this.view_ofs = PL_VIEW_OFS;
122 void CSQCPlayer_SavePrediction(entity this)
124 player_pmflags = this.flags;
125 csqcplayer_origin = this.origin;
126 csqcplayer_velocity = this.velocity;
127 csqcplayer_sequence = servercommandframe;
128 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
131 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
132 void _Movetype_Physics_ClientFrame(entity this, float movedt);
134 void Movetype_Physics_Spam(entity this) // optimized
136 _Movetype_Physics_ClientFrame(this, PHYS_INPUT_TIMELENGTH);
140 this.avelocity = this.move_avelocity;
141 this.velocity = this.move_velocity;
142 this.angles = this.move_angles;
143 this.flags = BITSET(this.flags, FL_ONGROUND, boolean(this.move_flags & FL_ONGROUND));
144 this.flags = BITSET(this.flags, FL_WATERJUMP, boolean(this.move_flags & FL_WATERJUMP));
145 this.waterlevel = this.move_waterlevel;
146 this.watertype = this.move_watertype;
147 setorigin(this, this.move_origin);
150 void CSQCPlayer_CheckWater(entity this)
152 this.move_origin = this.origin;
153 this.move_waterlevel = this.waterlevel;
154 this.move_watertype = this.watertype;
155 _Movetype_CheckWater(this);
156 this.waterlevel = this.move_waterlevel;
157 this.watertype = this.move_watertype;
160 void CSQCPlayer_Physics(entity this)
162 if(autocvar_cl_movement)
164 if(autocvar_cl_movement == 1)
165 CSQCPlayer_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
167 CSQC_ClientMovement_PlayerMove_Frame(this);
169 if(autocvar_cl_movement == 1)
171 this.move_origin = this.origin;
172 this.move_angles = this.angles;
173 //this.move_movetype = MOVETYPE_WALK; // temp
174 this.move_velocity = this.velocity;
175 this.move_avelocity = this.avelocity;
176 this.move_flags = BITSET(this.move_flags, FL_ONGROUND, boolean(this.flags & FL_ONGROUND));
177 this.move_flags = BITSET(this.move_flags, FL_WATERJUMP, boolean(this.flags & FL_WATERJUMP));
178 this.move_waterlevel = this.waterlevel;
179 this.move_watertype = this.watertype;
180 Movetype_Physics_Spam(this);
184 ((this.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
185 (!(this.flags & FL_JUMPRELEASED) ? PMF_JUMP_HELD : 0) |
186 ((this.flags & FL_ONGROUND) ? PMF_ONGROUND : 0);
190 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
192 CSQCPlayer_Unpredict(this);
195 this.origin += CSQCPlayer_GetPredictionErrorO();
196 this.velocity += CSQCPlayer_GetPredictionErrorV();
198 CSQCPlayer_SetMinsMaxs(this);
200 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
203 // we don't need this
204 // darkplaces makes servercommandframe == 0 in these cases anyway
205 if (STAT(HEALTH) <= 0)
207 csqcplayer_moveframe = clientcommandframe;
208 getinputstate(csqcplayer_moveframe-1);
209 LOG_INFO("the Weird code path got hit\n");
214 if (csqcplayer_moveframe >= endframe)
216 getinputstate(csqcplayer_moveframe - 1);
222 if (!getinputstate(csqcplayer_moveframe)) break;
223 CSQCPlayer_Physics(this);
224 CSQCPlayer_SetMinsMaxs(this);
225 ++csqcplayer_moveframe;
227 while (csqcplayer_moveframe < endframe);
230 // add in anything that was applied after (for low packet rate protocols)
231 input_angles = view_angles;
234 bool CSQCPlayer_IsLocalPlayer(entity this)
236 return (this == csqcplayer);
239 void CSQCPlayer_SetViewLocation()
241 viewloc_SetViewLocation();
244 /** Called once per CSQC_UpdateView() */
245 void CSQCPlayer_SetCamera()
247 const vector v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
248 const float vh = STAT(VIEWHEIGHT);
249 const vector pl_viewofs = PL_VIEW_OFS;
250 const vector pl_viewofs_crouch = PL_CROUCH_VIEW_OFS;
251 const entity e = csqcplayer;
254 if (servercommandframe == 0 || clientcommandframe == 0)
256 InterpolateOrigin_Do(e);
257 e.view_ofs = '0 0 1' * vh;
259 // get crouch state from the server
260 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
261 else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
263 // get onground state from the server
264 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
266 CSQCPlayer_SetMinsMaxs(e);
268 // override it back just in case
269 e.view_ofs = '0 0 1' * vh;
276 const int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
277 InterpolateOrigin_Do(e);
280 if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
282 const vector o = e.origin;
283 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
284 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
285 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - boolean(e.flags & FL_ONGROUND));
289 // get crouch state from the server
290 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
291 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
293 // get onground state from the server
294 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
296 CSQCPlayer_SavePrediction(e);
298 CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
300 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
301 // get crouch state from the server (LAG)
302 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
303 else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
305 CSQCPlayer_SetMinsMaxs(e);
307 e.angles_y = input_angles.y;
311 setorigin(e, e.origin);
314 const entity view = CSQCModel_server2csqc(player_localentnum - 1);
317 if (view != csqcplayer)
319 InterpolateOrigin_Do(view);
320 view.view_ofs = '0 0 1' * vh;
323 if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
324 if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
325 // note: these two only work in WIP2, but are harmless in WIP1
326 if (STAT(HEALTH) <= 0) refdefflags |= REFDEFFLAG_DEAD;
327 if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
328 V_CalcRefdef(view, refdefflags);
332 // FIXME by CSQC spec we have to do this:
333 // but it breaks chase cam
335 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
336 setproperty(VF_ANGLES, view_angles);
339 CSQCPLAYER_HOOK_POSTCAMERASETUP();
342 void CSQCPlayer_Remove()
345 cvar_settemp("cl_movement_replay", "1");
348 bool CSQCPlayer_PreUpdate(entity this)
350 if (this != csqcplayer) return false;
351 if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
355 bool CSQCPlayer_PostUpdate(entity this)
357 if (this.entnum != player_localnum + 1) return false;
359 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
360 cvar_settemp("cl_movement_replay", "0");
361 this.entremove = CSQCPlayer_Remove;