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