2 * Copyright (c) 2011 Rudolf Polzer
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 #include "../dpdefs/csprogsdefs.qh"
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"
37 float autocvar_cl_movement_errorcompensation = 0;
38 int autocvar_cl_movement = 1;
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)
56 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
59 vector CSQCPlayer_GetPredictionErrorV()
61 if(time >= csqcplayer_predictionerrortime)
63 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
66 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
68 // error too big to compensate, we LIKELY hit a teleport or a
69 // jumppad, or it's a jump time disagreement that'll get fixed
72 // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
74 // commented out as this one did not help
77 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
81 if(vlen(o) > 32 || vlen(v) > 192)
83 //printf("TOO BIG: x=%v v=%v\n", o, v);
87 if(!autocvar_cl_movement_errorcompensation)
89 csqcplayer_predictionerrorfactor = 0;
93 csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
94 csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
95 csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
96 csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
99 void CSQCPlayer_Unpredict()
101 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
103 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
104 error("Cannot unpredict in current status");
105 self.origin = csqcplayer_origin;
106 self.velocity = csqcplayer_velocity;
107 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
108 self.flags = player_pmflags;
111 void CSQCPlayer_SetMinsMaxs()
113 if(self.flags & FL_DUCKED)
115 self.mins = PL_CROUCH_MIN;
116 self.maxs = PL_CROUCH_MAX;
117 self.view_ofs = PL_CROUCH_VIEW_OFS;
123 self.view_ofs = PL_VIEW_OFS;
127 void CSQCPlayer_SavePrediction()
129 player_pmflags = self.flags;
130 csqcplayer_origin = self.origin;
131 csqcplayer_velocity = self.velocity;
132 csqcplayer_sequence = servercommandframe;
133 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
136 void CSQC_ClientMovement_PlayerMove_Frame();
138 void PM_Movement_Move()
140 runstandardplayerphysics(self);
143 ((self.pmove_flags & PMF_DUCKED) ? FL_DUCKED : 0) |
144 (!(self.pmove_flags & PMF_JUMP_HELD) ? FL_JUMPRELEASED : 0) |
145 ((self.pmove_flags & PMF_ONGROUND) ? FL_ONGROUND : 0);
149 void CSQCPlayer_Physics(void)
151 switch(autocvar_cl_movement)
153 case 1: CSQC_ClientMovement_PlayerMove_Frame(); break;
154 case 2: PM_Movement_Move(); break;
158 void CSQCPlayer_PredictTo(float endframe, float apply_error)
160 CSQCPlayer_Unpredict();
163 self.origin += CSQCPlayer_GetPredictionErrorO();
164 self.velocity += CSQCPlayer_GetPredictionErrorV();
166 CSQCPlayer_SetMinsMaxs();
168 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
171 // we don't need this
172 // darkplaces makes servercommandframe == 0 in these cases anyway
173 if (getstatf(STAT_HEALTH) <= 0)
175 csqcplayer_moveframe = clientcommandframe;
176 getinputstate(csqcplayer_moveframe-1);
177 print("the Weird code path got hit\n");
182 if(csqcplayer_moveframe >= endframe)
184 getinputstate(csqcplayer_moveframe - 1);
190 if (!getinputstate(csqcplayer_moveframe))
192 CSQCPlayer_Physics();
193 CSQCPlayer_SetMinsMaxs();
194 csqcplayer_moveframe++;
196 while(csqcplayer_moveframe < endframe);
199 //add in anything that was applied after (for low packet rate protocols)
200 input_angles = view_angles;
203 bool CSQCPlayer_IsLocalPlayer()
205 return (self == csqcplayer);
208 void CSQCPlayer_SetCamera()
211 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
219 if(servercommandframe == 0 || clientcommandframe == 0)
221 InterpolateOrigin_Do();
222 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
224 // get crouch state from the server
225 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
226 self.flags &= ~FL_DUCKED;
227 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
228 self.flags |= FL_DUCKED;
230 // get onground state from the server
232 self.flags |= FL_ONGROUND;
234 self.flags &= ~FL_ONGROUND;
236 CSQCPlayer_SetMinsMaxs();
238 // override it back just in case
239 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
246 float flg = self.iflags;
247 self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
248 InterpolateOrigin_Do();
251 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
256 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
257 CSQCPlayer_PredictTo(servercommandframe + 1, false);
258 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
262 // get crouch state from the server
263 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
264 self.flags &= ~FL_DUCKED;
265 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
266 self.flags |= FL_DUCKED;
268 // get onground state from the server
270 self.flags |= FL_ONGROUND;
272 self.flags &= ~FL_ONGROUND;
274 CSQCPlayer_SavePrediction();
276 CSQCPlayer_PredictTo(clientcommandframe + 1, true);
278 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
279 // get crouch state from the server (LAG)
280 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
281 self.flags &= ~FL_DUCKED;
282 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
283 self.flags |= FL_DUCKED;
286 CSQCPlayer_SetMinsMaxs();
288 self.angles_y = input_angles.y;
292 setorigin(self, self.origin);
298 view = CSQCModel_server2csqc(player_localentnum);
300 if(view && view != csqcplayer)
302 entity oldself = self;
304 InterpolateOrigin_Do();
305 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
313 if(view.csqcmodel_teleported)
314 refdefflags |= REFDEFFLAG_TELEPORTED;
316 if(input_buttons & 4)
317 refdefflags |= REFDEFFLAG_JUMPING;
319 // note: these two only work in WIP2, but are harmless in WIP1
320 if(getstati(STAT_HEALTH) <= 0)
321 refdefflags |= REFDEFFLAG_DEAD;
324 refdefflags |= REFDEFFLAG_INTERMISSION;
326 V_CalcRefdef(view, refdefflags);
330 // FIXME by CSQC spec we have to do this:
331 // but it breaks chase cam
333 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
334 setproperty(VF_ANGLES, view_angles);
338 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
341 void CSQCPlayer_Remove()
344 cvar_settemp("cl_movement_replay", "1");
347 float CSQCPlayer_PreUpdate()
349 if(self != csqcplayer)
351 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
352 CSQCPlayer_Unpredict();
356 float CSQCPlayer_PostUpdate()
358 if(self.entnum != player_localnum + 1)
361 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
362 cvar_settemp("cl_movement_replay", "0");
363 self.entremove = CSQCPlayer_Remove;