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