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 var float autocvar_cl_predictionerrorcompensation = 0;
24 var float autocvar_chase_active;
25 var float autocvar_chase_back;
30 #define PMF_TELEPORTED 16
33 vector csqcplayer_origin, csqcplayer_velocity;
34 float csqcplayer_sequence, player_pmflags;
35 float csqcplayer_moveframe;
36 vector csqcplayer_predictionerror;
37 float csqcplayer_predictionerrortime;
39 vector CSQCPlayer_GetPredictionError()
41 if(!autocvar_cl_predictionerrorcompensation)
43 if(time < csqcplayer_predictionerrortime)
44 return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
48 void CSQCPlayer_SetPredictionError(vector v)
50 if(!autocvar_cl_predictionerrorcompensation)
52 csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
53 csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
56 void CSQCPlayer_Unpredict()
58 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
60 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
61 error("Cannot unpredict in current status");
62 self.origin = csqcplayer_origin;
63 self.velocity = csqcplayer_velocity;
64 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
65 self.pmove_flags = player_pmflags;
68 void CSQCPlayer_SetMinsMaxs()
70 if(self.pmove_flags & PMF_DUCKED)
72 self.mins = PL_CROUCH_MIN;
73 self.maxs = PL_CROUCH_MAX;
74 self.view_ofs = PL_CROUCH_VIEW_OFS;
80 self.view_ofs = PL_VIEW_OFS;
84 void CSQCPlayer_SavePrediction()
86 player_pmflags = self.pmove_flags;
87 csqcplayer_origin = self.origin;
88 csqcplayer_velocity = self.velocity;
89 csqcplayer_sequence = servercommandframe;
90 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
93 void CSQCPlayer_PredictTo(float endframe)
95 CSQCPlayer_Unpredict();
96 CSQCPlayer_SetMinsMaxs();
98 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
100 if (getstatf(STAT_HEALTH) <= 0)
102 csqcplayer_moveframe = clientcommandframe;
103 getinputstate(csqcplayer_moveframe-1);
107 while(csqcplayer_moveframe < endframe)
109 if (!getinputstate(csqcplayer_moveframe))
113 runstandardplayerphysics(self);
114 CSQCPlayer_SetMinsMaxs();
115 csqcplayer_moveframe++;
118 //add in anything that was applied after (for low packet rate protocols)
119 input_angles = view_angles;
122 float CSQCPlayer_IsLocalPlayer()
124 return (self == csqcplayer);
127 void CSQCPlayer_SetCamera()
136 if(servercommandframe == 0)
138 InterpolateOrigin_Do();
139 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
143 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
147 v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
148 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
149 CSQCPlayer_PredictTo(servercommandframe + 1);
150 CSQCPlayer_SetPredictionError(o - self.origin);
154 // get crouch state from the server
155 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
156 self.pmove_flags &~= PMF_DUCKED;
157 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
158 self.pmove_flags |= PMF_DUCKED;
160 CSQCPlayer_SavePrediction();
162 CSQCPlayer_PredictTo(clientcommandframe);
168 setorigin(csqcplayer, csqcplayer.origin);
170 if(checkextension("DP_CSQC_V_CALCREFDEF"))
173 if(csqcplayer.csqcmodel_teleported)
175 csqcplayer.pmove_flags |= PMF_TELEPORTED;
176 csqcplayer.csqcmodel_teleported = 0;
179 csqcplayer.pmove_flags &~= PMF_TELEPORTED;
181 V_CalcRefdef(csqcplayer);
184 R_SetView3fv(VF_ORIGIN, csqcplayer.origin + csqcplayer.view_ofs);
186 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
190 void CSQCPlayer_Remove()
192 if(self.entnum != player_localentnum)
195 cvar_clientsettemp("cl_movement_replay", "1");
198 float CSQCPlayer_PreUpdate()
200 if(self.entnum != player_localentnum)
202 cvar_clientsettemp("cl_movement_replay", "0");
203 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
204 CSQCPlayer_Unpredict();
208 float CSQCPlayer_PostUpdate()
210 if(self.entnum == player_localentnum)
211 self.renderflags |= RF_EXTERNALMODEL;
213 self.renderflags &~= RF_EXTERNALMODEL;
214 if(self.entnum != player_localentnum)
216 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
218 self.entremove = CSQCPlayer_Remove;
222 entity CSQCPlayer_GetPlayer(float pl)
224 return findfloat(world, entnum, pl); // FIXME optimize this using an array