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.qc"
24 #include "../client/Defs.qc"
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;
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, player_pmflags;
44 float csqcplayer_moveframe;
45 vector csqcplayer_predictionerroro;
46 vector csqcplayer_predictionerrorv;
47 float csqcplayer_predictionerrortime;
48 float csqcplayer_predictionerrorfactor;
50 vector CSQCPlayer_GetPredictionErrorO()
52 if(time >= csqcplayer_predictionerrortime)
54 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
57 vector CSQCPlayer_GetPredictionErrorV()
59 if(time >= csqcplayer_predictionerrortime)
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(vlen(o) > 32 || vlen(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()
99 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
101 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
102 error("Cannot unpredict in current status");
103 self.origin = csqcplayer_origin;
104 self.velocity = csqcplayer_velocity;
105 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
106 self.pmove_flags = player_pmflags;
109 void CSQCPlayer_SetMinsMaxs()
111 if(self.pmove_flags & PMF_DUCKED)
113 self.mins = PL_CROUCH_MIN;
114 self.maxs = PL_CROUCH_MAX;
115 self.view_ofs = PL_CROUCH_VIEW_OFS;
121 self.view_ofs = PL_VIEW_OFS;
125 void CSQCPlayer_SavePrediction()
127 player_pmflags = self.pmove_flags;
128 csqcplayer_origin = self.origin;
129 csqcplayer_velocity = self.velocity;
130 csqcplayer_sequence = servercommandframe;
131 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
134 void CSQCPlayer_PredictTo(float endframe, float apply_error)
136 CSQCPlayer_Unpredict();
139 self.origin += CSQCPlayer_GetPredictionErrorO();
140 self.velocity += CSQCPlayer_GetPredictionErrorV();
142 CSQCPlayer_SetMinsMaxs();
144 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
147 // we don't need this
148 // darkplaces makes servercommandframe == 0 in these cases anyway
149 if (getstatf(STAT_HEALTH) <= 0)
151 csqcplayer_moveframe = clientcommandframe;
152 getinputstate(csqcplayer_moveframe-1);
153 print("the Weird code path got hit\n");
158 if(csqcplayer_moveframe >= endframe)
160 getinputstate(csqcplayer_moveframe - 1);
166 if (!getinputstate(csqcplayer_moveframe))
168 runstandardplayerphysics(self);
169 CSQCPlayer_SetMinsMaxs();
170 csqcplayer_moveframe++;
172 while(csqcplayer_moveframe < endframe);
175 //add in anything that was applied after (for low packet rate protocols)
176 input_angles = view_angles;
179 float CSQCPlayer_IsLocalPlayer()
181 return (self == csqcplayer);
184 void CSQCPlayer_SetCamera()
187 v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
195 if(servercommandframe == 0 || clientcommandframe == 0)
197 InterpolateOrigin_Do();
198 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
200 // get crouch state from the server
201 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
202 self.pmove_flags &= ~PMF_DUCKED;
203 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
204 self.pmove_flags |= PMF_DUCKED;
206 // get onground state from the server
208 self.pmove_flags |= PMF_ONGROUND;
210 self.pmove_flags &= ~PMF_ONGROUND;
212 CSQCPlayer_SetMinsMaxs();
214 // override it back just in case
215 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
222 float flg = self.iflags;
223 self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
224 InterpolateOrigin_Do();
227 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
232 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
233 CSQCPlayer_PredictTo(servercommandframe + 1, false);
234 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.pmove_flags & PMF_ONGROUND));
238 // get crouch state from the server
239 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
240 self.pmove_flags &= ~PMF_DUCKED;
241 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
242 self.pmove_flags |= PMF_DUCKED;
244 // get onground state from the server
246 self.pmove_flags |= PMF_ONGROUND;
248 self.pmove_flags &= ~PMF_ONGROUND;
250 CSQCPlayer_SavePrediction();
252 CSQCPlayer_PredictTo(clientcommandframe + 1, true);
254 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
255 // get crouch state from the server (LAG)
256 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
257 self.pmove_flags &= ~PMF_DUCKED;
258 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
259 self.pmove_flags |= PMF_DUCKED;
262 CSQCPlayer_SetMinsMaxs();
264 self.angles_y = input_angles.y;
268 setorigin(self, self.origin);
274 view = CSQCModel_server2csqc(player_localentnum);
276 if(view && view != csqcplayer)
278 entity oldself = self;
280 InterpolateOrigin_Do();
281 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
289 if(view.csqcmodel_teleported)
290 refdefflags |= REFDEFFLAG_TELEPORTED;
292 if(input_buttons & 4)
293 refdefflags |= REFDEFFLAG_JUMPING;
295 // note: these two only work in WIP2, but are harmless in WIP1
296 if(getstati(STAT_HEALTH) <= 0)
297 refdefflags |= REFDEFFLAG_DEAD;
300 refdefflags |= REFDEFFLAG_INTERMISSION;
302 V_CalcRefdef(view, refdefflags);
306 // FIXME by CSQC spec we have to do this:
307 // but it breaks chase cam
309 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
310 setproperty(VF_ANGLES, view_angles);
314 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
317 void CSQCPlayer_Remove()
320 cvar_settemp("cl_movement_replay", "1");
323 float CSQCPlayer_PreUpdate()
325 if(self != csqcplayer)
327 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
328 CSQCPlayer_Unpredict();
332 float CSQCPlayer_PostUpdate()
334 if(self.entnum != player_localnum + 1)
337 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
338 cvar_settemp("cl_movement_replay", "0");
339 self.entremove = CSQCPlayer_Remove;