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