1a8ba710ae182c7984b5cec0924b2eb99216aaf8
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / csqcmodel / cl_player.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  * Copyright (c) 2015 Micah Talkiewicz
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to
7  * deal in the Software without restriction, including without limitation the
8  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9  * sell copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #include "cl_player.qh"
24
25 #include "cl_model.qh"
26 #include "common.qh"
27 #include "interpolate.qh"
28 #include <client/defs.qh>
29 #include <client/main.qh>
30 #include <common/constants.qh>
31 #include <common/physics/player.qh>
32 #include <common/stats.qh>
33 #include <common/triggers/trigger/viewloc.qh>
34 #include <common/util.qh>
35 #include <common/viewloc.qh>
36
37 float autocvar_cl_movement_errorcompensation = 0;
38 bool autocvar_cl_movement_intermissionrunning = false;
39
40 // engine stuff
41 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
42
43 vector csqcplayer_origin, csqcplayer_velocity;
44 float csqcplayer_sequence;
45 int player_pmflags;
46 float csqcplayer_moveframe;
47 vector csqcplayer_predictionerroro;
48 vector csqcplayer_predictionerrorv;
49 float csqcplayer_predictionerrortime;
50 float csqcplayer_predictionerrorfactor;
51
52 vector CSQCPlayer_GetPredictionErrorO()
53 {
54         if (time >= csqcplayer_predictionerrortime) return '0 0 0';
55         return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
56 }
57
58 vector CSQCPlayer_GetPredictionErrorV()
59 {
60         if (time >= csqcplayer_predictionerrortime) return '0 0 0';
61         return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
62 }
63
64 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
65 {
66         // error too big to compensate, we LIKELY hit a teleport or a
67         // jumppad, or it's a jump time disagreement that'll get fixed
68         // next frame
69
70         // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
71         /*
72         // commented out as this one did not help
73         if(onground_diff)
74         {
75                 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
76                 return;
77         }
78         */
79         if(vdist(o, >, 32) || vdist(v, >, 192))
80         {
81                 //printf("TOO BIG: x=%v v=%v\n", o, v);
82                 return;
83         }
84
85         if(!autocvar_cl_movement_errorcompensation)
86         {
87                 csqcplayer_predictionerrorfactor = 0;
88                 return;
89         }
90
91         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
92         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
93         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
94         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
95 }
96
97 void CSQCPlayer_Unpredict(entity this)
98 {
99         if (csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED) return;
100         if (csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED) LOG_FATALF("Cannot unpredict in current status (%d)\n", csqcplayer_status);
101         this.origin = csqcplayer_origin;
102         this.velocity = csqcplayer_velocity;
103         csqcplayer_moveframe = csqcplayer_sequence + 1; // + 1 because the recieved frame has the move already done (server side)
104         this.flags = player_pmflags;
105 }
106
107 void CSQCPlayer_SetMinsMaxs(entity this)
108 {
109         if ((this.flags & FL_DUCKED) || !this.isplayermodel)
110         {
111                 this.mins = STAT(PL_CROUCH_MIN, NULL);
112                 this.maxs = STAT(PL_CROUCH_MAX, NULL);
113                 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, NULL);
114         }
115         else
116         {
117                 this.mins = STAT(PL_MIN, NULL);
118                 this.maxs = STAT(PL_MAX, NULL);
119                 this.view_ofs = STAT(PL_VIEW_OFS, NULL);
120         }
121 }
122
123 void CSQCPlayer_SavePrediction(entity this)
124 {
125         player_pmflags = this.flags;
126         csqcplayer_origin = this.origin;
127         csqcplayer_velocity = this.velocity;
128         csqcplayer_sequence = servercommandframe;
129         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
130 }
131
132 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
133 void _Movetype_Physics_ClientFrame(entity this, float movedt);
134
135 void Movetype_Physics_Spam(entity this)  // optimized
136 {
137         _Movetype_Physics_ClientFrame(this, PHYS_INPUT_TIMELENGTH);
138         if(wasfreed(this))
139                 return;
140
141         this.avelocity = this.move_avelocity;
142         this.velocity = this.move_velocity;
143         this.angles = this.move_angles;
144         this.flags = BITSET(this.flags, FL_ONGROUND, boolean(this.move_flags & FL_ONGROUND));
145         this.flags = BITSET(this.flags, FL_WATERJUMP, boolean(this.move_flags & FL_WATERJUMP));
146         this.waterlevel = this.move_waterlevel;
147         this.watertype = this.move_watertype;
148         setorigin(this, this.move_origin);
149 }
150
151 void CSQCPlayer_CheckWater(entity this)
152 {
153         this.move_origin = this.origin;
154         this.move_waterlevel = this.waterlevel;
155         this.move_watertype = this.watertype;
156         _Movetype_CheckWater(this);
157         this.waterlevel = this.move_waterlevel;
158         this.watertype = this.move_watertype;
159 }
160
161 void CSQCPlayer_Physics(entity this)
162 {
163         if(autocvar_cl_movement)
164         {
165                 if(autocvar_cl_movement == 1)
166                         CSQCPlayer_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
167
168                 vector oldv_angle = this.v_angle;
169                 vector oldangles = this.angles; // we need to save these, as they're abused by other code
170                 this.v_angle = PHYS_INPUT_ANGLES(this);
171                 this.angles = PHYS_WORLD_ANGLES(this);
172
173                 CSQC_ClientMovement_PlayerMove_Frame(this);
174
175                 if(autocvar_cl_movement == 1)
176                 {
177                         this.move_origin = this.origin;
178                         this.move_angles = this.angles;
179                         //this.move_movetype = MOVETYPE_WALK; // temp
180                         this.move_velocity = this.velocity;
181                         this.move_avelocity = this.avelocity;
182                         this.move_flags = BITSET(this.move_flags, FL_ONGROUND, IS_ONGROUND(this));
183                         this.move_flags = BITSET(this.move_flags, FL_WATERJUMP, boolean(this.flags & FL_WATERJUMP));
184                         this.move_waterlevel = this.waterlevel;
185                         this.move_watertype = this.watertype;
186                         Movetype_Physics_Spam(this);
187                 }
188
189                 view_angles = this.v_angle;
190                 input_angles = this.angles;
191                 this.v_angle = oldv_angle;
192                 this.angles = oldangles;
193
194                 this.pmove_flags =
195                                 ((this.flags & FL_DUCKED) ? PMF_DUCKED : 0) |
196                                 (!(this.flags & FL_JUMPRELEASED) ? PMF_JUMP_HELD : 0) |
197                                 ((IS_ONGROUND(this)) ? PMF_ONGROUND : 0);
198         }
199 }
200
201 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
202 {
203         CSQCPlayer_Unpredict(this);
204         if (apply_error)
205         {
206                 this.origin += CSQCPlayer_GetPredictionErrorO();
207                 this.velocity += CSQCPlayer_GetPredictionErrorV();
208         }
209         CSQCPlayer_SetMinsMaxs(this);
210
211         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
212
213 #if 0
214         // we don't need this
215         // darkplaces makes servercommandframe == 0 in these cases anyway
216         if (STAT(HEALTH) <= 0)
217         {
218                 csqcplayer_moveframe = clientcommandframe;
219                 getinputstate(csqcplayer_moveframe-1);
220                 LOG_INFO("the Weird code path got hit\n");
221                 return;
222         }
223 #endif
224
225         if (csqcplayer_moveframe >= endframe)
226         {
227                 getinputstate(csqcplayer_moveframe - 1);
228         }
229         else
230         {
231                 do
232                 {
233                         if (!getinputstate(csqcplayer_moveframe)) break;
234                         CSQCPlayer_Physics(this);
235                         CSQCPlayer_SetMinsMaxs(this);
236                         ++csqcplayer_moveframe;
237                 }
238                 while (csqcplayer_moveframe < endframe);
239         }
240
241         // add in anything that was applied after (for low packet rate protocols)
242         input_angles = view_angles;
243 }
244
245 bool CSQCPlayer_IsLocalPlayer(entity this)
246 {
247         return (this == csqcplayer);
248 }
249
250 void CSQCPlayer_SetViewLocation()
251 {
252         viewloc_SetViewLocation();
253 }
254
255 /** Called once per CSQC_UpdateView() */
256 void CSQCPlayer_SetCamera()
257 {
258         const vector v0 = ((intermission && !autocvar_cl_movement_intermissionrunning) ? '0 0 0' : pmove_vel); // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
259         const float vh = STAT(VIEWHEIGHT);
260         const vector pl_viewofs = STAT(PL_VIEW_OFS, NULL);
261         const vector pl_viewofs_crouch = STAT(PL_CROUCH_VIEW_OFS, NULL);
262         const entity e = csqcplayer;
263         if (e)
264         {
265                 if (servercommandframe == 0 || clientcommandframe == 0)
266                 {
267                         InterpolateOrigin_Do(e);
268                         e.view_ofs = '0 0 1' * vh;
269
270                         // get crouch state from the server
271                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
272                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
273
274                         // get onground state from the server
275                         e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
276
277                         CSQCPlayer_SetMinsMaxs(e);
278
279                         // override it back just in case
280                         e.view_ofs = '0 0 1' * vh;
281
282                         // set velocity
283                         e.velocity = v0;
284                 }
285                 else
286                 {
287                         const int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
288                         InterpolateOrigin_Do(e);
289                         e.iflags = flg;
290
291                         if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
292                         {
293                                 const vector o = e.origin;
294                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
295                                 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
296                                 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
297                                 e.origin = o;
298                                 e.velocity = v0;
299
300                                 // get crouch state from the server
301                                 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
302                                 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
303
304                                 // get onground state from the server
305                                 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
306
307                                 CSQCPlayer_SavePrediction(e);
308                         }
309                         CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
310
311 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
312                         // get crouch state from the server (LAG)
313                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
314                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
315 #endif
316                         CSQCPlayer_SetMinsMaxs(e);
317
318                         e.angles_y = input_angles.y;
319                 }
320
321                 // relink
322                 setorigin(e, e.origin);
323         }
324
325         const entity view = CSQCModel_server2csqc(player_localentnum - 1);
326         if (view)
327         {
328                 if (view != csqcplayer)
329                 {
330                         InterpolateOrigin_Do(view);
331                         view.view_ofs = '0 0 1' * vh;
332                 }
333                 int refdefflags = 0;
334                 if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
335                 if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
336                 // note: these two only work in WIP2, but are harmless in WIP1
337                 if (STAT(HEALTH) <= 0 && STAT(HEALTH) != -666 && STAT(HEALTH) != -2342) refdefflags |= REFDEFFLAG_DEAD;
338                 if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
339                 V_CalcRefdef(view, refdefflags);
340         }
341         else
342         {
343                 // FIXME by CSQC spec we have to do this:
344                 // but it breaks chase cam
345                 /*
346                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
347                 setproperty(VF_ANGLES, view_angles);
348                 */
349         }
350         CSQCPLAYER_HOOK_POSTCAMERASETUP();
351 }
352
353 void CSQCPlayer_Remove(entity this)
354 {
355         csqcplayer = NULL;
356         cvar_settemp("cl_movement_replay", "1");
357 }
358
359 bool CSQCPlayer_PreUpdate(entity this)
360 {
361         if (this != csqcplayer) return false;
362         if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
363         return true;
364 }
365
366 bool CSQCPlayer_PostUpdate(entity this)
367 {
368         if (this.entnum != player_localnum + 1) return false;
369         csqcplayer = this;
370         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
371         cvar_settemp("cl_movement_replay", "0");
372         this.entremove = CSQCPlayer_Remove;
373         return true;
374 }