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