Use the constants for player hitbox size when applicable (should fix observer hitbox)
[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/net_linked.qh>
32 #include <common/physics/player.qh>
33 #include <common/stats.qh>
34 #include <common/triggers/trigger/viewloc.qh>
35 #include <common/util.qh>
36 #include <common/viewloc.qh>
37
38 float autocvar_cl_movement_errorcompensation = 0;
39 bool autocvar_cl_movement_intermissionrunning = false;
40
41 // engine stuff
42 float pmove_onground; // weird engine flag we shouldn't really use but have to for now
43
44 vector csqcplayer_origin, csqcplayer_velocity;
45 float csqcplayer_sequence;
46 int player_pmflags;
47 float csqcplayer_moveframe;
48 vector csqcplayer_predictionerroro;
49 vector csqcplayer_predictionerrorv;
50 float csqcplayer_predictionerrortime;
51 float csqcplayer_predictionerrorfactor;
52
53 vector CSQCPlayer_GetPredictionErrorO()
54 {
55         if (time >= csqcplayer_predictionerrortime) return '0 0 0';
56         return csqcplayer_predictionerroro * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
57 }
58
59 vector CSQCPlayer_GetPredictionErrorV()
60 {
61         if (time >= csqcplayer_predictionerrortime) return '0 0 0';
62         return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
63 }
64
65 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
66 {
67         // error too big to compensate, we LIKELY hit a teleport or a
68         // jumppad, or it's a jump time disagreement that'll get fixed
69         // next frame
70
71         // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
72         /*
73         // commented out as this one did not help
74         if(onground_diff)
75         {
76                 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
77                 return;
78         }
79         */
80         if(vdist(o, >, 32) || vdist(v, >, 192))
81         {
82                 //printf("TOO BIG: x=%v v=%v\n", o, v);
83                 return;
84         }
85
86         if(!autocvar_cl_movement_errorcompensation)
87         {
88                 csqcplayer_predictionerrorfactor = 0;
89                 return;
90         }
91
92         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
93         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
94         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
95         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
96 }
97
98 void CSQCPlayer_Unpredict(entity this)
99 {
100         if (csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED) return;
101         if (csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED) LOG_FATALF("Cannot unpredict in current status (%d)", csqcplayer_status);
102         this.origin = csqcplayer_origin;
103         this.velocity = csqcplayer_velocity;
104         csqcplayer_moveframe = csqcplayer_sequence + 1; // + 1 because the recieved frame has the move already done (server side)
105         this.flags = player_pmflags;
106 }
107
108 void CSQCPlayer_SetMinsMaxs(entity this)
109 {
110         if (IS_DUCKED(this) || !this.isplayermodel)
111         {
112                 this.mins = STAT(PL_CROUCH_MIN, this);
113                 this.maxs = STAT(PL_CROUCH_MAX, this);
114                 this.view_ofs = STAT(PL_CROUCH_VIEW_OFS, this);
115         }
116         else
117         {
118                 this.mins = STAT(PL_MIN, this);
119                 this.maxs = STAT(PL_MAX, this);
120                 this.view_ofs = STAT(PL_VIEW_OFS, this);
121         }
122 }
123
124 void CSQCPlayer_SavePrediction(entity this)
125 {
126         player_pmflags = this.flags;
127         csqcplayer_origin = this.origin;
128         csqcplayer_velocity = this.velocity;
129         csqcplayer_sequence = servercommandframe;
130         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
131 }
132
133 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
134
135 void CSQCPlayer_Physics(entity this)
136 {
137         if(!autocvar_cl_movement) { return; }
138
139         _Movetype_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
140
141         vector oldv_angle = this.v_angle;
142         vector oldangles = this.angles; // we need to save these, as they're abused by other code
143         this.v_angle = PHYS_INPUT_ANGLES(this);
144         this.angles = PHYS_WORLD_ANGLES(this);
145
146         CSQC_ClientMovement_PlayerMove_Frame(this);
147
148         Movetype_Physics_NoMatchTicrate(this, PHYS_INPUT_TIMELENGTH, true);
149
150         view_angles = this.v_angle;
151         input_angles = this.angles;
152         this.v_angle = oldv_angle;
153         this.angles = oldangles;
154
155         this.pmove_flags =
156                         ((IS_DUCKED(this)) ? PMF_DUCKED : 0) |
157                         ((IS_JUMP_HELD(this)) ? PMF_JUMP_HELD : 0) |
158                         ((IS_ONGROUND(this)) ? PMF_ONGROUND : 0);
159 }
160
161 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
162 {
163         CSQCPlayer_Unpredict(this);
164         if (apply_error)
165         {
166                 this.origin += CSQCPlayer_GetPredictionErrorO();
167                 this.velocity += CSQCPlayer_GetPredictionErrorV();
168         }
169         CSQCPlayer_SetMinsMaxs(this);
170
171         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
172
173 #if 0
174         // we don't need this
175         // darkplaces makes servercommandframe == 0 in these cases anyway
176         if (STAT(HEALTH) <= 0)
177         {
178                 csqcplayer_moveframe = clientcommandframe;
179                 getinputstate(csqcplayer_moveframe-1);
180                 LOG_INFO("the Weird code path got hit\n");
181                 return;
182         }
183 #endif
184
185         if (csqcplayer_moveframe >= endframe)
186         {
187                 getinputstate(csqcplayer_moveframe - 1);
188         }
189         else
190         {
191                 do
192                 {
193                         if (!getinputstate(csqcplayer_moveframe)) break;
194                         /*if (input_timelength > 0.0005)
195                         {
196                                 if (input_timelength > 0.05)
197                                 {
198                                         input_timelength /= 2;
199                                         CSQCPlayer_Physics(this);
200                                 }
201                                 CSQCPlayer_Physics(this);
202                         }*/
203                         CSQCPlayer_Physics(this);
204                         CSQCPlayer_SetMinsMaxs(this);
205                         ++csqcplayer_moveframe;
206                 }
207                 while (csqcplayer_moveframe < endframe);
208         }
209
210         // add in anything that was applied after (for low packet rate protocols)
211         input_angles = view_angles;
212 }
213
214 bool CSQCPlayer_IsLocalPlayer(entity this)
215 {
216         return (this == csqcplayer);
217 }
218
219 void CSQCPlayer_SetViewLocation()
220 {
221         viewloc_SetViewLocation();
222 }
223
224 /** Called once per CSQC_UpdateView() */
225 void CSQCPlayer_SetCamera()
226 {
227         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
228         const float vh = STAT(VIEWHEIGHT);
229         const vector pl_viewofs = STAT(PL_VIEW_OFS);
230         const vector pl_viewofs_crouch = STAT(PL_CROUCH_VIEW_OFS);
231         const entity e = csqcplayer;
232         if (e)
233         {
234                 if (servercommandframe == 0 || clientcommandframe == 0)
235                 {
236                         InterpolateOrigin_Do(e);
237                         e.view_ofs = '0 0 1' * vh;
238
239                         // get crouch state from the server
240                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
241                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
242
243                         // get onground state from the server
244                         e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
245
246                         CSQCPlayer_SetMinsMaxs(e);
247
248                         // override it back just in case
249                         e.view_ofs = '0 0 1' * vh;
250
251                         // set velocity
252                         e.velocity = v0;
253                 }
254                 else
255                 {
256                         const int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
257                         InterpolateOrigin_Do(e);
258                         e.iflags = flg;
259
260                         if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
261                         {
262                                 const vector o = e.origin;
263                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
264                                 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
265                                 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
266                                 e.origin = o;
267                                 e.velocity = v0;
268
269                                 // get crouch state from the server
270                                 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
271                                 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
272
273                                 // get onground state from the server
274                                 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
275
276                                 CSQCPlayer_SavePrediction(e);
277                         }
278                         CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
279
280 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
281                         // get crouch state from the server (LAG)
282                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
283                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
284 #endif
285                         CSQCPlayer_SetMinsMaxs(e);
286
287                         e.angles_y = input_angles.y;
288                 }
289
290                 // relink
291                 setorigin(e, e.origin);
292         }
293
294         const entity view = CSQCModel_server2csqc(player_localentnum - 1);
295         if (view)
296         {
297                 if (view != csqcplayer)
298                 {
299                         InterpolateOrigin_Do(view);
300                         view.view_ofs = '0 0 1' * vh;
301                 }
302                 int refdefflags = 0;
303                 if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
304                 if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
305                 // note: these two only work in WIP2, but are harmless in WIP1
306                 if (STAT(HEALTH) <= 0 && STAT(HEALTH) != -666 && STAT(HEALTH) != -2342) refdefflags |= REFDEFFLAG_DEAD;
307                 if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
308                 V_CalcRefdef(view, refdefflags);
309         }
310         else
311         {
312                 // FIXME by CSQC spec we have to do this:
313                 // but it breaks chase cam
314                 /*
315                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
316                 setproperty(VF_ANGLES, view_angles);
317                 */
318         }
319         CSQCPLAYER_HOOK_POSTCAMERASETUP();
320 }
321
322 void CSQCPlayer_Remove(entity this)
323 {
324         csqcplayer = NULL;
325         cvar_settemp("cl_movement_replay", "1");
326 }
327
328 bool CSQCPlayer_PreUpdate(entity this)
329 {
330         if (this != csqcplayer) return false;
331         if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
332         return true;
333 }
334
335 bool CSQCPlayer_PostUpdate(entity this)
336 {
337         if (this.entnum != player_localnum + 1) return false;
338         csqcplayer = this;
339         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
340         cvar_settemp("cl_movement_replay", "0");
341         this.entremove = CSQCPlayer_Remove;
342         return true;
343 }