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;
27 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
29 #define PMF_ONGROUND 8
30 #define REFDEFFLAG_TELEPORTED 1
31 #define REFDEFFLAG_JUMPING 2
34 vector csqcplayer_origin, csqcplayer_velocity;
35 float csqcplayer_sequence, player_pmflags;
36 float csqcplayer_moveframe;
37 vector csqcplayer_predictionerror;
38 float csqcplayer_predictionerrortime;
40 vector CSQCPlayer_GetPredictionError()
42 if(!autocvar_cl_predictionerrorcompensation)
44 if(time < csqcplayer_predictionerrortime)
45 return csqcplayer_predictionerror * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
49 void CSQCPlayer_SetPredictionError(vector v)
51 if(!autocvar_cl_predictionerrorcompensation)
53 csqcplayer_predictionerror = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerror + v;
54 csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
57 void CSQCPlayer_Unpredict()
59 if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
61 if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
62 error("Cannot unpredict in current status");
63 self.origin = csqcplayer_origin;
64 self.velocity = csqcplayer_velocity;
65 csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
66 self.pmove_flags = player_pmflags;
69 void CSQCPlayer_SetMinsMaxs()
71 if(self.pmove_flags & PMF_DUCKED)
73 self.mins = PL_CROUCH_MIN;
74 self.maxs = PL_CROUCH_MAX;
75 self.view_ofs = PL_CROUCH_VIEW_OFS;
81 self.view_ofs = PL_VIEW_OFS;
85 void CSQCPlayer_SavePrediction()
87 player_pmflags = self.pmove_flags;
88 csqcplayer_origin = self.origin;
89 csqcplayer_velocity = self.velocity;
90 csqcplayer_sequence = servercommandframe;
91 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
94 void CSQCPlayer_PredictTo(float endframe)
96 CSQCPlayer_Unpredict();
97 CSQCPlayer_SetMinsMaxs();
99 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
101 if (getstatf(STAT_HEALTH) <= 0)
103 csqcplayer_moveframe = clientcommandframe;
104 getinputstate(csqcplayer_moveframe-1);
108 while(csqcplayer_moveframe < endframe)
110 if (!getinputstate(csqcplayer_moveframe))
114 runstandardplayerphysics(self);
115 CSQCPlayer_SetMinsMaxs();
116 csqcplayer_moveframe++;
119 //add in anything that was applied after (for low packet rate protocols)
120 input_angles = view_angles;
123 float CSQCPlayer_IsLocalPlayer()
125 return (self == csqcplayer);
128 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
130 void CSQCPlayer_SetCamera()
138 if(servercommandframe == 0)
140 InterpolateOrigin_Do();
141 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
143 // get crouch state from the server
144 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
145 self.pmove_flags &~= PMF_DUCKED;
146 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
147 self.pmove_flags |= PMF_DUCKED;
149 // get onground state from the server
151 self.pmove_flags |= PMF_ONGROUND;
153 self.pmove_flags &~= PMF_ONGROUND;
155 CSQCPlayer_SetMinsMaxs();
157 // override it back just in case
158 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
162 if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
166 v = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
167 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
168 CSQCPlayer_PredictTo(servercommandframe + 1);
169 CSQCPlayer_SetPredictionError(o - self.origin);
173 // get crouch state from the server
174 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
175 self.pmove_flags &~= PMF_DUCKED;
176 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
177 self.pmove_flags |= PMF_DUCKED;
179 // get onground state from the server
181 self.pmove_flags |= PMF_ONGROUND;
183 self.pmove_flags &~= PMF_ONGROUND;
185 CSQCPlayer_SavePrediction();
187 CSQCPlayer_PredictTo(clientcommandframe);
189 CSQCPlayer_SetMinsMaxs();
193 setorigin(self, self.origin);
195 // FIXME support svc_setview?
197 if(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1"))
199 var float refdefflags = 0;
201 if(self.csqcmodel_teleported)
203 refdefflags |= REFDEFFLAG_TELEPORTED;
204 self.csqcmodel_teleported = 0;
207 if(input_buttons & 4)
208 refdefflags |= REFDEFFLAG_JUMPING;
210 V_CalcRefdef(self, refdefflags);
213 setproperty(VF_ORIGIN, self.origin + self.view_ofs);
214 self.angles_y = input_angles_y;
216 { CSQCPLAYER_HOOK_POSTCAMERASETUP }
222 void CSQCPlayer_Remove()
224 if(self.entnum != player_localentnum)
227 cvar_clientsettemp("cl_movement_replay", "1");
230 float CSQCPlayer_PreUpdate()
232 if(self.entnum != player_localentnum)
234 cvar_clientsettemp("cl_movement_replay", "0");
235 if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
236 CSQCPlayer_Unpredict();
240 float CSQCPlayer_PostUpdate()
243 if(self.entnum == player_localentnum)
244 self.renderflags |= RF_EXTERNALMODEL;
246 self.renderflags &~= RF_EXTERNALMODEL;
249 if(self.entnum != player_localentnum)
251 csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
253 self.entremove = CSQCPlayer_Remove;
257 entity CSQCPlayer_GetPlayer(float pl)
259 return findfloat(world, entnum, pl); // FIXME optimize this using an array