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