]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodellib/cl_player.qc
33e5f95d72fcc832c86c962b021d1dfe22638818
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodellib / cl_player.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * IN THE SOFTWARE.
21  */
22
23 var float autocvar_cl_predictionerrorcompensation = 0;
24
25 // engine stuff
26 .float pmove_flags;
27 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
28 #define PMF_JUMP_HELD 1
29 #define PMF_DUCKED 4
30 #define PMF_ONGROUND 8
31 #define REFDEFFLAG_TELEPORTED 1
32 #define REFDEFFLAG_JUMPING 2
33
34 entity csqcplayer;
35 vector csqcplayer_origin, csqcplayer_velocity;
36 float csqcplayer_sequence, player_pmflags;
37 float csqcplayer_moveframe;
38 vector csqcplayer_predictionerroro;
39 vector csqcplayer_predictionerrorv;
40 float csqcplayer_predictionerrortime;
41
42 vector CSQCPlayer_GetPredictionErrorO()
43 {
44         if(!autocvar_cl_predictionerrorcompensation)
45                 return '0 0 0';
46         if(time < csqcplayer_predictionerrortime)
47                 return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
48         return '0 0 0';
49 }
50
51 vector CSQCPlayer_GetPredictionErrorV()
52 {
53         if(!autocvar_cl_predictionerrorcompensation)
54                 return '0 0 0';
55         if(time < csqcplayer_predictionerrortime)
56                 return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation;
57         return '0 0 0';
58 }
59
60 void CSQCPlayer_SetPredictionError(vector o, vector v)
61 {
62         if(!autocvar_cl_predictionerrorcompensation)
63                 return;
64         csqcplayer_predictionerroro = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerroro + o;
65         csqcplayer_predictionerrorv = (csqcplayer_predictionerrortime - time) * autocvar_cl_predictionerrorcompensation * csqcplayer_predictionerrorv + v;
66         csqcplayer_predictionerrortime = time + 1.0 / autocvar_cl_predictionerrorcompensation;
67 }
68
69 void CSQCPlayer_Unpredict()
70 {
71         if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
72                 return;
73         if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
74                 error("Cannot unpredict in current status");
75         self.origin = csqcplayer_origin;
76         self.velocity = csqcplayer_velocity;
77         csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
78         self.pmove_flags = player_pmflags;
79 }
80
81 void CSQCPlayer_SetMinsMaxs()
82 {
83         if(self.pmove_flags & PMF_DUCKED)
84         {
85                 self.mins = PL_CROUCH_MIN;
86                 self.maxs = PL_CROUCH_MAX;
87                 self.view_ofs = PL_CROUCH_VIEW_OFS;
88         }
89         else
90         {
91                 self.mins = PL_MIN;
92                 self.maxs = PL_MAX;
93                 self.view_ofs = PL_VIEW_OFS;
94         }
95 }
96
97 void CSQCPlayer_SavePrediction()
98 {
99         player_pmflags = self.pmove_flags;
100         csqcplayer_origin = self.origin;
101         csqcplayer_velocity = self.velocity;
102         csqcplayer_sequence = servercommandframe;
103         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
104 }
105
106 void CSQCPlayer_PredictTo(float endframe, float apply_error)
107 {
108         CSQCPlayer_Unpredict();
109         if(apply_error)
110         {
111                 self.origin += CSQCPlayer_GetPredictionErrorO();
112                 self.velocity += CSQCPlayer_GetPredictionErrorV();
113         }
114         CSQCPlayer_SetMinsMaxs();
115
116         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
117
118         // FIXME do we really NEED this? dead players have servercommandframe
119         // == 0 and thus won't predict
120         if (getstatf(STAT_HEALTH) <= 0)
121         {
122                 csqcplayer_moveframe = clientcommandframe;
123                 getinputstate(csqcplayer_moveframe-1);
124                 print("the Weird code path got hit\n");
125                 return;
126         }
127
128         if(csqcplayer_moveframe >= endframe)
129         {
130                 getinputstate(csqcplayer_moveframe - 1);
131         }
132         else
133         {
134                 do
135                 {
136                         if (!getinputstate(csqcplayer_moveframe))
137                                 break;
138                         runstandardplayerphysics(self);
139                         CSQCPlayer_SetMinsMaxs();
140                         csqcplayer_moveframe++;
141                 }
142                 while(csqcplayer_moveframe < endframe);
143         }
144
145         //add in anything that was applied after (for low packet rate protocols)
146         input_angles = view_angles;
147 }
148
149 float CSQCPlayer_IsLocalPlayer()
150 {
151         return (self == csqcplayer);
152 }
153
154 void(entity e, float fl) V_CalcRefdef = #640; // DP_CSQC_V_CALCREFDEF
155
156 void CSQCPlayer_SetCamera()
157 {
158         vector v0;
159         v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
160
161         if(csqcplayer)
162         {
163                 entity oldself;
164                 oldself = self;
165                 self = csqcplayer;
166
167 #ifdef COMPAT_XON050_ENGINE
168                 if(servercommandframe == 0 || clientcommandframe == 0 || !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
169 #else
170                 if(servercommandframe == 0 || clientcommandframe == 0)
171 #endif
172                 {
173                         InterpolateOrigin_Do();
174                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
175
176                         // get crouch state from the server
177                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
178                                 self.pmove_flags &~= PMF_DUCKED;
179                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
180                                 self.pmove_flags |= PMF_DUCKED;
181
182                         // get onground state from the server
183                         if(pmove_onground)
184                                 self.pmove_flags |= PMF_ONGROUND;
185                         else
186                                 self.pmove_flags &~= PMF_ONGROUND;
187
188                         CSQCPlayer_SetMinsMaxs();
189
190                         // override it back just in case
191                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
192
193                         // set velocity
194                         self.velocity = v0;
195                 }
196                 else
197                 {
198                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
199                         {
200                                 vector o, v;
201                                 o = self.origin;
202                                 v = v0;
203                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
204                                 CSQCPlayer_PredictTo(servercommandframe + 1, FALSE);
205                                 CSQCPlayer_SetPredictionError(o - self.origin, v0 - self.velocity);
206                                 self.origin = o;
207                                 self.velocity = v;
208
209                                 // get crouch state from the server
210                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS_z)
211                                         self.pmove_flags &~= PMF_DUCKED;
212                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS_z)
213                                         self.pmove_flags |= PMF_DUCKED;
214
215                                 // get onground state from the server
216                                 if(pmove_onground)
217                                         self.pmove_flags |= PMF_ONGROUND;
218                                 else
219                                         self.pmove_flags &~= PMF_ONGROUND;
220
221                                 CSQCPlayer_SavePrediction();
222                         }
223                         CSQCPlayer_PredictTo(clientcommandframe + 1, TRUE);
224
225                         CSQCPlayer_SetMinsMaxs();
226
227                         self.angles_y = input_angles_y;
228                 }
229
230                 // relink
231                 setorigin(self, self.origin);
232
233                 self = oldself;
234         }
235
236         entity view;
237 #ifdef COMPAT_XON050_ENGINE
238         view = CSQCModel_server2csqc((spectatee_status > 0) ? spectatee_status : player_localentnum);
239 #else
240         view = CSQCModel_server2csqc(player_localentnum);
241 #endif
242
243         if(view && view != csqcplayer)
244         {
245                 entity oldself = self;
246                 self = view;
247                 InterpolateOrigin_Do();
248                 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
249                 self = oldself;
250         }
251
252 #ifdef COMPAT_XON050_ENGINE
253         if(view && !(checkextension("DP_CSQC_V_CALCREFDEF") || checkextension("DP_CSQC_V_CALCREFDEF_WIP1")))
254         {
255                 // legacy code, not totally correct, but good enough for not having V_CalcRefdef
256                 setproperty(VF_ORIGIN, view.origin + '0 0 1' * getstati(STAT_VIEWHEIGHT));
257                 setproperty(VF_ANGLES, view_angles);
258         }
259         else
260 #endif
261         if(view)
262         {
263                 var float refdefflags = 0;
264
265                 if(view.csqcmodel_teleported)
266                         refdefflags |= REFDEFFLAG_TELEPORTED;
267
268                 if(input_buttons & 4)
269                         refdefflags |= REFDEFFLAG_JUMPING;
270
271                 // note: these two only work in WIP2, but are harmless in WIP1
272                 if(getstati(STAT_HEALTH) <= 0)
273                         refdefflags |= REFDEFFLAG_DEAD;
274
275                 if(intermission)
276                         refdefflags |= REFDEFFLAG_INTERMISSION;
277
278                 V_CalcRefdef(view, refdefflags);
279         }
280         else
281         {
282                 // FIXME by CSQC spec we have to do this:
283                 // but it breaks chase cam
284                 /*
285                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
286                 setproperty(VF_ANGLES, view_angles);
287                 */
288         }
289
290         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
291 }
292
293 void CSQCPlayer_Remove()
294 {
295         csqcplayer = world;
296         cvar_settemp("cl_movement_replay", "1");
297 }
298
299 float CSQCPlayer_PreUpdate()
300 {
301         if(self != csqcplayer)
302                 return 0;
303         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
304                 CSQCPlayer_Unpredict();
305         return 1;
306 }
307
308 float CSQCPlayer_PostUpdate()
309 {
310         if(self.entnum != player_localnum + 1)
311                 return 0;
312         csqcplayer = self;
313         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
314         cvar_settemp("cl_movement_replay", "0");
315         self.entremove = CSQCPlayer_Remove;
316         return 1;
317 }