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