155ad7f9229103526c52881c4a42106aa8827d2a
[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.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 int autocvar_cl_movement = 1;
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)
55                 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)
62                 return '0 0 0';
63         return csqcplayer_predictionerrorv * (csqcplayer_predictionerrortime - time) * csqcplayer_predictionerrorfactor;
64 }
65
66 void CSQCPlayer_SetPredictionError(vector o, vector v, float onground_diff)
67 {
68         // error too big to compensate, we LIKELY hit a teleport or a
69         // jumppad, or it's a jump time disagreement that'll get fixed
70         // next frame
71
72         // FIXME we sometimes have disagreement in order of jump velocity. Do not act on them!
73         /*
74         // commented out as this one did not help
75         if(onground_diff)
76         {
77                 printf("ONGROUND MISMATCH: %d x=%v v=%v\n", onground_diff, o, v);
78                 return;
79         }
80         */
81         if(vlen(o) > 32 || vlen(v) > 192)
82         {
83                 //printf("TOO BIG: x=%v v=%v\n", o, v);
84                 return;
85         }
86
87         if(!autocvar_cl_movement_errorcompensation)
88         {
89                 csqcplayer_predictionerrorfactor = 0;
90                 return;
91         }
92
93         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
94         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
95         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
96         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
97 }
98
99 void CSQCPlayer_Unpredict()
100 {SELFPARAM();
101         if(csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED)
102                 return;
103         if(csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED)
104                 error("Cannot unpredict in current status");
105         self.origin = csqcplayer_origin;
106         self.velocity = csqcplayer_velocity;
107         csqcplayer_moveframe = csqcplayer_sequence+1; //+1 because the recieved frame has the move already done (server side)
108         self.flags = player_pmflags;
109 }
110
111 void CSQCPlayer_SetMinsMaxs()
112 {SELFPARAM();
113         if(self.flags & FL_DUCKED)
114         {
115                 self.mins = PL_CROUCH_MIN;
116                 self.maxs = PL_CROUCH_MAX;
117                 self.view_ofs = PL_CROUCH_VIEW_OFS;
118         }
119         else
120         {
121                 self.mins = PL_MIN;
122                 self.maxs = PL_MAX;
123                 self.view_ofs = PL_VIEW_OFS;
124         }
125 }
126
127 void CSQCPlayer_SavePrediction()
128 {SELFPARAM();
129         player_pmflags = self.flags;
130         csqcplayer_origin = self.origin;
131         csqcplayer_velocity = self.velocity;
132         csqcplayer_sequence = servercommandframe;
133         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
134 }
135
136 void CSQC_ClientMovement_PlayerMove_Frame();
137
138 void PM_Movement_Move()
139 {SELFPARAM();
140         runstandardplayerphysics(self);
141 #ifdef CSQC
142         self.flags =
143                         ((self.pmove_flags & PMF_DUCKED) ? FL_DUCKED : 0) |
144                         (!(self.pmove_flags & PMF_JUMP_HELD) ? FL_JUMPRELEASED : 0) |
145                         ((self.pmove_flags & PMF_ONGROUND) ? FL_ONGROUND : 0);
146 #endif
147 }
148
149 void CSQCPlayer_Physics()
150 {
151         switch(autocvar_cl_movement)
152         {
153                 case 1: CSQC_ClientMovement_PlayerMove_Frame(); break;
154                 case 2: PM_Movement_Move(); break;
155         }
156 }
157
158 void CSQCPlayer_PredictTo(float endframe, float apply_error)
159 {SELFPARAM();
160         CSQCPlayer_Unpredict();
161         if(apply_error)
162         {
163                 self.origin += CSQCPlayer_GetPredictionErrorO();
164                 self.velocity += CSQCPlayer_GetPredictionErrorV();
165         }
166         CSQCPlayer_SetMinsMaxs();
167
168         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
169
170 #if 0
171         // we don't need this
172         // darkplaces makes servercommandframe == 0 in these cases anyway
173         if (getstatf(STAT_HEALTH) <= 0)
174         {
175                 csqcplayer_moveframe = clientcommandframe;
176                 getinputstate(csqcplayer_moveframe-1);
177                 LOG_INFO("the Weird code path got hit\n");
178                 return;
179         }
180 #endif
181
182         if(csqcplayer_moveframe >= endframe)
183         {
184                 getinputstate(csqcplayer_moveframe - 1);
185         }
186         else
187         {
188                 do
189                 {
190                         if (!getinputstate(csqcplayer_moveframe))
191                                 break;
192                         CSQCPlayer_Physics();
193                         CSQCPlayer_SetMinsMaxs();
194                         csqcplayer_moveframe++;
195                 }
196                 while(csqcplayer_moveframe < endframe);
197         }
198
199         //add in anything that was applied after (for low packet rate protocols)
200         input_angles = view_angles;
201 }
202
203 bool CSQCPlayer_IsLocalPlayer()
204 {SELFPARAM();
205         return (self == csqcplayer);
206 }
207
208 void CSQCPlayer_SetViewLocation()
209 {
210         viewloc_SetViewLocation();
211 }
212
213 void CSQCPlayer_SetCamera()
214 {SELFPARAM();
215         vector v0;
216         v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
217
218         if(csqcplayer)
219         {
220                 setself(csqcplayer);
221
222                 if(servercommandframe == 0 || clientcommandframe == 0)
223                 {
224                         InterpolateOrigin_Do();
225                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
226
227                         // get crouch state from the server
228                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
229                                 self.flags &= ~FL_DUCKED;
230                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
231                                 self.flags |= FL_DUCKED;
232
233                         // get onground state from the server
234                         if(pmove_onground)
235                                 self.flags |= FL_ONGROUND;
236                         else
237                                 self.flags &= ~FL_ONGROUND;
238
239                         CSQCPlayer_SetMinsMaxs();
240
241                         // override it back just in case
242                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
243
244                         // set velocity
245                         self.velocity = v0;
246                 }
247                 else
248                 {
249                         float flg = self.iflags;
250                         self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
251                         InterpolateOrigin_Do();
252                         self.iflags = flg;
253
254                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
255                         {
256                                 vector o, v;
257                                 o = self.origin;
258                                 v = v0;
259                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
260                                 CSQCPlayer_PredictTo(servercommandframe + 1, false);
261                                 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
262                                 self.origin = o;
263                                 self.velocity = v;
264
265                                 // get crouch state from the server
266                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
267                                         self.flags &= ~FL_DUCKED;
268                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
269                                         self.flags |= FL_DUCKED;
270
271                                 // get onground state from the server
272                                 if(pmove_onground)
273                                         self.flags |= FL_ONGROUND;
274                                 else
275                                         self.flags &= ~FL_ONGROUND;
276
277                                 CSQCPlayer_SavePrediction();
278                         }
279                         CSQCPlayer_PredictTo(clientcommandframe + 1, true);
280
281 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
282                         // get crouch state from the server (LAG)
283                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
284                                 self.flags &= ~FL_DUCKED;
285                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
286                                 self.flags |= FL_DUCKED;
287 #endif
288
289                         CSQCPlayer_SetMinsMaxs();
290
291                         self.angles_y = input_angles.y;
292                 }
293
294                 // relink
295                 setorigin(self, self.origin);
296
297                 setself(this);
298         }
299
300         entity view = CSQCModel_server2csqc(player_localentnum);
301
302         if(view && view != csqcplayer)
303         {
304                 WITH(entity, self, view, InterpolateOrigin_Do());
305                 view.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
306         }
307
308         if(view)
309         {
310                 int refdefflags = 0;
311
312                 if(view.csqcmodel_teleported)
313                         refdefflags |= REFDEFFLAG_TELEPORTED;
314
315                 if(input_buttons & 4)
316                         refdefflags |= REFDEFFLAG_JUMPING;
317
318                 // note: these two only work in WIP2, but are harmless in WIP1
319                 if(getstati(STAT_HEALTH) <= 0)
320                         refdefflags |= REFDEFFLAG_DEAD;
321
322                 if(intermission)
323                         refdefflags |= REFDEFFLAG_INTERMISSION;
324
325                 V_CalcRefdef(view, refdefflags);
326         }
327         else
328         {
329                 // FIXME by CSQC spec we have to do this:
330                 // but it breaks chase cam
331                 /*
332                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
333                 setproperty(VF_ANGLES, view_angles);
334                 */
335         }
336
337         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
338 }
339
340 void CSQCPlayer_Remove()
341 {
342         csqcplayer = world;
343         cvar_settemp("cl_movement_replay", "1");
344 }
345
346 float CSQCPlayer_PreUpdate()
347 {SELFPARAM();
348         if(self != csqcplayer)
349                 return 0;
350         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
351                 CSQCPlayer_Unpredict();
352         return 1;
353 }
354
355 float CSQCPlayer_PostUpdate()
356 {SELFPARAM();
357         if(self.entnum != player_localnum + 1)
358                 return 0;
359         csqcplayer = self;
360         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
361         cvar_settemp("cl_movement_replay", "0");
362         self.entremove = CSQCPlayer_Remove;
363         return 1;
364 }