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