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