]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodellib/cl_player.qc
Default cl_movement to 1
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodellib / cl_player.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 #if defined(CSQC)
23         #include "../dpdefs/csprogsdefs.qh"
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 #elif defined(MENUQC)
34 #elif defined(SVQC)
35 #endif
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 {
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 {
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 {
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 CSQCPlayer_Physics(void)
139 {
140         switch(autocvar_cl_movement)
141         {
142                 case 1: runstandardplayerphysics(self); break;
143                 case 2: CSQC_ClientMovement_PlayerMove_Frame(); break;
144         }
145 }
146
147 void CSQCPlayer_PredictTo(float endframe, float apply_error)
148 {
149         CSQCPlayer_Unpredict();
150         if(apply_error)
151         {
152                 self.origin += CSQCPlayer_GetPredictionErrorO();
153                 self.velocity += CSQCPlayer_GetPredictionErrorV();
154         }
155         CSQCPlayer_SetMinsMaxs();
156
157         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
158
159 #if 0
160         // we don't need this
161         // darkplaces makes servercommandframe == 0 in these cases anyway
162         if (getstatf(STAT_HEALTH) <= 0)
163         {
164                 csqcplayer_moveframe = clientcommandframe;
165                 getinputstate(csqcplayer_moveframe-1);
166                 print("the Weird code path got hit\n");
167                 return;
168         }
169 #endif
170
171         if(csqcplayer_moveframe >= endframe)
172         {
173                 getinputstate(csqcplayer_moveframe - 1);
174         }
175         else
176         {
177                 do
178                 {
179                         if (!getinputstate(csqcplayer_moveframe))
180                                 break;
181                         CSQCPlayer_Physics();
182                         CSQCPlayer_SetMinsMaxs();
183                         csqcplayer_moveframe++;
184                 }
185                 while(csqcplayer_moveframe < endframe);
186         }
187
188         //add in anything that was applied after (for low packet rate protocols)
189         input_angles = view_angles;
190 }
191
192 float CSQCPlayer_IsLocalPlayer()
193 {
194         return (self == csqcplayer);
195 }
196
197 void CSQCPlayer_SetCamera()
198 {
199         vector v0;
200         v0 = pmove_vel; // TRICK: pmove_vel is set by the engine when we get here. No need to network velocity
201
202         if(csqcplayer)
203         {
204                 entity oldself;
205                 oldself = self;
206                 self = csqcplayer;
207
208                 if(servercommandframe == 0 || clientcommandframe == 0)
209                 {
210                         InterpolateOrigin_Do();
211                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
212
213                         // get crouch state from the server
214                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
215                                 self.flags &= ~FL_DUCKED;
216                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
217                                 self.flags |= FL_DUCKED;
218
219                         // get onground state from the server
220                         if(pmove_onground)
221                                 self.flags |= FL_ONGROUND;
222                         else
223                                 self.flags &= ~FL_ONGROUND;
224
225                         CSQCPlayer_SetMinsMaxs();
226
227                         // override it back just in case
228                         self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
229
230                         // set velocity
231                         self.velocity = v0;
232                 }
233                 else
234                 {
235                         float flg = self.iflags;
236                         self.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
237                         InterpolateOrigin_Do();
238                         self.iflags = flg;
239
240                         if(csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
241                         {
242                                 vector o, v;
243                                 o = self.origin;
244                                 v = v0;
245                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
246                                 CSQCPlayer_PredictTo(servercommandframe + 1, false);
247                                 CSQCPlayer_SetPredictionError(self.origin - o, self.velocity - v, pmove_onground - !!(self.flags & FL_ONGROUND));
248                                 self.origin = o;
249                                 self.velocity = v;
250
251                                 // get crouch state from the server
252                                 if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
253                                         self.flags &= ~FL_DUCKED;
254                                 else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
255                                         self.flags |= FL_DUCKED;
256
257                                 // get onground state from the server
258                                 if(pmove_onground)
259                                         self.flags |= FL_ONGROUND;
260                                 else
261                                         self.flags &= ~FL_ONGROUND;
262
263                                 CSQCPlayer_SavePrediction();
264                         }
265                         CSQCPlayer_PredictTo(clientcommandframe + 1, true);
266
267 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
268                         // get crouch state from the server (LAG)
269                         if(getstati(STAT_VIEWHEIGHT) == PL_VIEW_OFS.z)
270                                 self.flags &= ~FL_DUCKED;
271                         else if(getstati(STAT_VIEWHEIGHT) == PL_CROUCH_VIEW_OFS.z)
272                                 self.flags |= FL_DUCKED;
273 #endif
274
275                         CSQCPlayer_SetMinsMaxs();
276
277                         self.angles_y = input_angles.y;
278                 }
279
280                 // relink
281                 setorigin(self, self.origin);
282
283                 self = oldself;
284         }
285
286         entity view;
287         view = CSQCModel_server2csqc(player_localentnum);
288
289         if(view && view != csqcplayer)
290         {
291                 entity oldself = self;
292                 self = view;
293                 InterpolateOrigin_Do();
294                 self.view_ofs = '0 0 1' * getstati(STAT_VIEWHEIGHT);
295                 self = oldself;
296         }
297
298         if(view)
299         {
300                 int refdefflags = 0;
301
302                 if(view.csqcmodel_teleported)
303                         refdefflags |= REFDEFFLAG_TELEPORTED;
304
305                 if(input_buttons & 4)
306                         refdefflags |= REFDEFFLAG_JUMPING;
307
308                 // note: these two only work in WIP2, but are harmless in WIP1
309                 if(getstati(STAT_HEALTH) <= 0)
310                         refdefflags |= REFDEFFLAG_DEAD;
311
312                 if(intermission)
313                         refdefflags |= REFDEFFLAG_INTERMISSION;
314
315                 V_CalcRefdef(view, refdefflags);
316         }
317         else
318         {
319                 // FIXME by CSQC spec we have to do this:
320                 // but it breaks chase cam
321                 /*
322                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * getstati(STAT_VIEWHEIGHT));
323                 setproperty(VF_ANGLES, view_angles);
324                 */
325         }
326
327         { CSQCPLAYER_HOOK_POSTCAMERASETUP }
328 }
329
330 void CSQCPlayer_Remove()
331 {
332         csqcplayer = world;
333         cvar_settemp("cl_movement_replay", "1");
334 }
335
336 float CSQCPlayer_PreUpdate()
337 {
338         if(self != csqcplayer)
339                 return 0;
340         if(csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER)
341                 CSQCPlayer_Unpredict();
342         return 1;
343 }
344
345 float CSQCPlayer_PostUpdate()
346 {
347         if(self.entnum != player_localnum + 1)
348                 return 0;
349         csqcplayer = self;
350         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
351         cvar_settemp("cl_movement_replay", "0");
352         self.entremove = CSQCPlayer_Remove;
353         return 1;
354 }