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 "../client/defs.qh"
25 #include "../common/constants.qh"
26 #include "../common/stats.qh"
27 #include "../common/util.qh"
28 #include "interpolate.qh"
29 #include "../client/main.qh"
31 #include "cl_model.qh"
32 #include "cl_player.qh"
33 #include "../common/triggers/trigger/viewloc.qh"
34 #include "../common/viewloc.qh"
39 float autocvar_cl_movement_errorcompensation = 0;
40 int autocvar_cl_movement = 1;
43 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
45 vector csqcplayer_origin, csqcplayer_velocity;
46 float csqcplayer_sequence;
48 float csqcplayer_moveframe;
49 vector csqcplayer_predictionerroro;
50 vector csqcplayer_predictionerrorv;
51 float csqcplayer_predictionerrortime;
52 float csqcplayer_predictionerrorfactor;
54 vector CSQCPlayer_GetPredictionErrorO()
56 if(time >= csqcplayer_predictionerrortime)
58 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
61 vector CSQCPlayer_GetPredictionErrorV()
63 if(time >= csqcplayer_predictionerrortime)
65 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
68 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
70 // error too big to compensate, we LIKELY hit a teleport or a
71 // jumppad, or it's a jump time disagreement that'll get fixed
74 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
76 // commented out as this one did not help
79 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
83 if(vlen(o) > 32 || vlen(v) > 192)
85 //printf("TOO BIG: x=%v v=%v\n", o, v);
89 if(!autocvar_cl_movement_errorcompensation)
91 csqcplayer_predictionerrorfactor = 0;
95 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
96 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
97 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
98 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
101 void CSQCPlayer_Unpredict()
103 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
105 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
106 error("Cannot unpredict in current status");
107 self.origin = csqcplayer_origin;
108 self.velocity = csqcplayer_velocity;
109 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
110 self.flags = player_pmflags;
113 void CSQCPlayer_SetMinsMaxs()
115 if(self.flags & FL_DUCKED)
117 self.mins = PL_CROUCH_MIN;
118 self.maxs = PL_CROUCH_MAX;
119 self.view_ofs = PL_CROUCH_VIEW_OFS;
125 self.view_ofs = PL_VIEW_OFS;
129 void CSQCPlayer_SavePrediction()
131 player_pmflags = self.flags;
132 csqcplayer_origin = self.origin;
133 csqcplayer_velocity = self.velocity;
134 csqcplayer_sequence = servercommandframe;
135 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
138 void CSQC_ClientMovement_PlayerMove_Frame();
140 void PM_Movement_Move()
142 runstandardplayerphysics(self);
145 ((self.pmove_flags & PMF_DUCKED) ? FL_DUCKED : 0) |
146 (!(self.pmove_flags & PMF_JUMP_HELD) ? FL_JUMPRELEASED : 0) |
147 ((self.pmove_flags & PMF_ONGROUND) ? FL_ONGROUND : 0);
151 void CSQCPlayer_Physics(void)
153 switch(autocvar_cl_movement)
155 case 1: CSQC_ClientMovement_PlayerMove_Frame(); break;
156 case 2: PM_Movement_Move(); break;
160 void CSQCPlayer_PredictTo(float endframe, float apply_error)
162 CSQCPlayer_Unpredict();
165 self.origin += CSQCPlayer_GetPredictionErrorO();
166 self.velocity += CSQCPlayer_GetPredictionErrorV();
168 CSQCPlayer_SetMinsMaxs();
170 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
173 // we don't need this
174 // darkplaces makes servercommandframe == 0 in these cases anyway
175 if (getstatf(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))
194 CSQCPlayer_Physics();
195 CSQCPlayer_SetMinsMaxs();
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()
207 return (self == csqcplayer);
210 void CSQCPlayer_SetViewLocation()
212 viewloc_SetViewLocation();
215 void CSQCPlayer_SetCamera()
218 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
224 if(servercommandframe == 0 || clientcommandframe == 0)
226 InterpolateOrigin_Do();
227 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
229 // get crouch state from the server
230 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
231 self.flags &= ~FL_DUCKED;
232 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
233 self.flags |= FL_DUCKED;
235 // get onground state from the server
237 self.flags |= FL_ONGROUND;
239 self.flags &= ~FL_ONGROUND;
241 CSQCPlayer_SetMinsMaxs();
243 // override it back just in case
244 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
251 float flg = self.iflags;
252 self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
253 InterpolateOrigin_Do();
256 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
261 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
262 CSQCPlayer_PredictTo(servercommandframe + 1, false);
263 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
267 // get crouch state from the server
268 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
269 self.flags &= ~FL_DUCKED;
270 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
271 self.flags |= FL_DUCKED;
273 // get onground state from the server
275 self.flags |= FL_ONGROUND;
277 self.flags &= ~FL_ONGROUND;
279 CSQCPlayer_SavePrediction();
281 CSQCPlayer_PredictTo(clientcommandframe + 1, true);
283 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
284 // get crouch state from the server (LAG)
285 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
286 self.flags &= ~FL_DUCKED;
287 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
288 self.flags |= FL_DUCKED;
291 CSQCPlayer_SetMinsMaxs();
293 self.angles_y = input_angles.y;
297 setorigin(self, self.origin);
302 entity view = CSQCModel_server2csqc(player_localentnum);
304 if(view && view != csqcplayer)
306 WITH(entity, self, view, InterpolateOrigin_Do());
307 view.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
314 if(view.csqcmodel_teleported)
315 refdefflags |= REFDEFFLAG_TELEPORTED;
317 if(input_buttons & 4)
318 refdefflags |= REFDEFFLAG_JUMPING;
320 // note: these two only work in WIP2, but are harmless in WIP1
321 if(getstati(STAT_HEALTH) <= 0)
322 refdefflags |= REFDEFFLAG_DEAD;
325 refdefflags |= REFDEFFLAG_INTERMISSION;
327 V_CalcRefdef(view, refdefflags);
331 // FIXME by CSQC spec we have to do this:
332 // but it breaks chase cam
334 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
335 setproperty(VF_ANGLES, view_angles);
339 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
342 void CSQCPlayer_Remove()
345 cvar_settemp("cl_movement_replay", "1");
348 float CSQCPlayer_PreUpdate()
350 if(self != csqcplayer)
352 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
353 CSQCPlayer_Unpredict();
357 float CSQCPlayer_PostUpdate()
359 if(self.entnum != player_localnum + 1)
362 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
363 cvar_settemp("cl_movement_replay", "0");
364 self.entremove = CSQCPlayer_Remove;