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/stats.qh"
32 #include "../../common/triggers/trigger/viewloc.qh"
33 #include "../../common/util.qh"
34 #include "../../common/viewloc.qh"
36 float autocvar_cl_movement_errorcompensation = 0;
37 int autocvar_cl_movement = 1;
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)
55 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
58 vector CSQCPlayer_GetPredictionErrorV()
60 if(time >= csqcplayer_predictionerrortime)
62 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
65 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
67 // error too big to compensate, we LIKELY hit a teleport or a
68 // jumppad, or it's a jump time disagreement that'll get fixed
71 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
73 // commented out as this one did not help
76 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
80 if(vlen(o) > 32 || vlen(v) > 192)
82 //printf("TOO BIG: x=%v v=%v\n", o, v);
86 if(!autocvar_cl_movement_errorcompensation)
88 csqcplayer_predictionerrorfactor = 0;
92 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
93 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
94 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
95 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
98 void CSQCPlayer_Unpredict()
100 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
102 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
103 error("Cannot unpredict in current status");
104 self.origin = csqcplayer_origin;
105 self.velocity = csqcplayer_velocity;
106 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
107 self.flags = player_pmflags;
110 void CSQCPlayer_SetMinsMaxs()
112 if(self.flags & FL_DUCKED)
114 self.mins = PL_CROUCH_MIN;
115 self.maxs = PL_CROUCH_MAX;
116 self.view_ofs = PL_CROUCH_VIEW_OFS;
122 self.view_ofs = PL_VIEW_OFS;
126 void CSQCPlayer_SavePrediction()
128 player_pmflags = self.flags;
129 csqcplayer_origin = self.origin;
130 csqcplayer_velocity = self.velocity;
131 csqcplayer_sequence = servercommandframe;
132 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
135 void CSQC_ClientMovement_PlayerMove_Frame();
137 void PM_Movement_Move()
139 runstandardplayerphysics(self);
142 ((self.pmove_flags & PMF_DUCKED) ? FL_DUCKED : 0) |
143 (!(self.pmove_flags & PMF_JUMP_HELD) ? FL_JUMPRELEASED : 0) |
144 ((self.pmove_flags & PMF_ONGROUND) ? FL_ONGROUND : 0);
148 void CSQCPlayer_Physics(void)
150 switch(autocvar_cl_movement)
152 case 1: CSQC_ClientMovement_PlayerMove_Frame(); break;
153 case 2: PM_Movement_Move(); break;
157 void CSQCPlayer_PredictTo(float endframe, float apply_error)
159 CSQCPlayer_Unpredict();
162 self.origin += CSQCPlayer_GetPredictionErrorO();
163 self.velocity += CSQCPlayer_GetPredictionErrorV();
165 CSQCPlayer_SetMinsMaxs();
167 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
170 // we don't need this
171 // darkplaces makes servercommandframe == 0 in these cases anyway
172 if (getstatf(STAT_HEALTH) <= 0)
174 csqcplayer_moveframe = clientcommandframe;
175 getinputstate(csqcplayer_moveframe-1);
176 LOG_INFO("the Weird code path got hit\n");
181 if(csqcplayer_moveframe >= endframe)
183 getinputstate(csqcplayer_moveframe - 1);
189 if (!getinputstate(csqcplayer_moveframe))
191 CSQCPlayer_Physics();
192 CSQCPlayer_SetMinsMaxs();
193 csqcplayer_moveframe++;
195 while(csqcplayer_moveframe < endframe);
198 //add in anything that was applied after (for low packet rate protocols)
199 input_angles = view_angles;
202 bool CSQCPlayer_IsLocalPlayer()
204 return (self == csqcplayer);
207 void CSQCPlayer_SetViewLocation()
209 viewloc_SetViewLocation();
212 void CSQCPlayer_SetCamera()
215 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
221 if(servercommandframe == 0 || clientcommandframe == 0)
223 InterpolateOrigin_Do();
224 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
226 // get crouch state from the server
227 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
228 self.flags &= ~FL_DUCKED;
229 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
230 self.flags |= FL_DUCKED;
232 // get onground state from the server
234 self.flags |= FL_ONGROUND;
236 self.flags &= ~FL_ONGROUND;
238 CSQCPlayer_SetMinsMaxs();
240 // override it back just in case
241 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
248 float flg = self.iflags;
249 self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
250 InterpolateOrigin_Do();
253 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
258 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
259 CSQCPlayer_PredictTo(servercommandframe + 1, false);
260 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
264 // get crouch state from the server
265 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
266 self.flags &= ~FL_DUCKED;
267 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
268 self.flags |= FL_DUCKED;
270 // get onground state from the server
272 self.flags |= FL_ONGROUND;
274 self.flags &= ~FL_ONGROUND;
276 CSQCPlayer_SavePrediction();
278 CSQCPlayer_PredictTo(clientcommandframe + 1, true);
280 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
281 // get crouch state from the server (LAG)
282 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
283 self.flags &= ~FL_DUCKED;
284 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
285 self.flags |= FL_DUCKED;
288 CSQCPlayer_SetMinsMaxs();
290 self.angles_y = input_angles.y;
294 setorigin(self, self.origin);
299 entity view = CSQCModel_server2csqc(player_localentnum);
301 if(view && view != csqcplayer)
303 WITH(entity, self, view, InterpolateOrigin_Do());
304 view.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
311 if(view.csqcmodel_teleported)
312 refdefflags |= REFDEFFLAG_TELEPORTED;
314 if(input_buttons & 4)
315 refdefflags |= REFDEFFLAG_JUMPING;
317 // note: these two only work in WIP2, but are harmless in WIP1
318 if(getstati(STAT_HEALTH) <= 0)
319 refdefflags |= REFDEFFLAG_DEAD;
322 refdefflags |= REFDEFFLAG_INTERMISSION;
324 V_CalcRefdef(view, refdefflags);
328 // FIXME by CSQC spec we have to do this:
329 // but it breaks chase cam
331 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
332 setproperty(VF_ANGLES, view_angles);
336 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
339 void CSQCPlayer_Remove()
342 cvar_settemp("cl_movement_replay", "1");
345 float CSQCPlayer_PreUpdate()
347 if(self != csqcplayer)
349 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
350 CSQCPlayer_Unpredict();
354 float CSQCPlayer_PostUpdate()
356 if(self.entnum != player_localnum + 1)
359 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
360 cvar_settemp("cl_movement_replay", "0");
361 self.entremove = CSQCPlayer_Remove;