]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_player.qc
Accurate port of chase cam, including overhead view
[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         {
73                 //printf("TOO BIG: x=%v v=%v\n", o, v);
74                 return;
75         }
76
77         if(!autocvar_cl_movement_errorcompensation)
78         {
79                 csqcplayer_predictionerrorfactor = 0;
80                 return;
81         }
82
83         csqcplayer_predictionerroro = CSQCPlayer_GetPredictionErrorO() + o;
84         csqcplayer_predictionerrorv = CSQCPlayer_GetPredictionErrorV() + v;
85         csqcplayer_predictionerrorfactor = autocvar_cl_movement_errorcompensation / ticrate;
86         csqcplayer_predictionerrortime = time + 1.0 / csqcplayer_predictionerrorfactor;
87 }
88
89 void CSQCPlayer_Unpredict(entity this)
90 {
91         if (csqcplayer_status == CSQCPLAYERSTATUS_UNPREDICTED) return;
92         if (csqcplayer_status != CSQCPLAYERSTATUS_PREDICTED) LOG_FATALF("Cannot unpredict in current status (%d)", csqcplayer_status);
93         this.origin = csqcplayer_origin;
94         this.velocity = csqcplayer_velocity;
95         csqcplayer_moveframe = csqcplayer_sequence + 1; // + 1 because the recieved frame has the move already done (server side)
96         this.flags = player_pmflags;
97 }
98
99 void CSQCPlayer_SetMinsMaxs(entity this)
100 {
101         if (IS_DUCKED(this) || !(this.isplayermodel & ISPLAYER_PLAYER))
102         {
103                 this.mins = PHYS_PL_CROUCH_MIN(this);
104                 this.maxs = PHYS_PL_CROUCH_MAX(this);
105                 this.view_ofs = PHYS_PL_CROUCH_VIEWOFS(this);
106         }
107         else
108         {
109                 this.mins = PHYS_PL_MIN(this);
110                 this.maxs = PHYS_PL_MAX(this);
111                 this.view_ofs = PHYS_PL_VIEWOFS(this);
112         }
113 }
114
115 void CSQCPlayer_SavePrediction(entity this)
116 {
117         player_pmflags = this.flags;
118         csqcplayer_origin = this.origin;
119         csqcplayer_velocity = this.velocity;
120         csqcplayer_sequence = servercommandframe;
121         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
122 }
123
124 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
125
126 void CSQCPlayer_Physics(entity this)
127 {
128         if(!autocvar_cl_movement) { return; }
129
130         _Movetype_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
131
132         vector oldv_angle = this.v_angle;
133         vector oldangles = this.angles; // we need to save these, as they're abused by other code
134         this.v_angle = PHYS_INPUT_ANGLES(this);
135         this.angles = PHYS_WORLD_ANGLES(this);
136
137         CSQC_ClientMovement_PlayerMove_Frame(this);
138
139         Movetype_Physics_NoMatchTicrate(this, PHYS_INPUT_TIMELENGTH, true);
140
141         view_angles = this.v_angle;
142         input_angles = this.angles;
143         this.v_angle = oldv_angle;
144         this.angles = oldangles;
145
146         this.pmove_flags =
147                         ((IS_DUCKED(this)) ? PMF_DUCKED : 0) |
148                         ((IS_JUMP_HELD(this)) ? PMF_JUMP_HELD : 0) |
149                         ((IS_ONGROUND(this)) ? PMF_ONGROUND : 0);
150 }
151
152 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
153 {
154         CSQCPlayer_Unpredict(this);
155         if (apply_error)
156         {
157                 this.origin += CSQCPlayer_GetPredictionErrorO();
158                 this.velocity += CSQCPlayer_GetPredictionErrorV();
159         }
160         CSQCPlayer_SetMinsMaxs(this);
161
162         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
163
164 #if 0
165         // we don't need this
166         // darkplaces makes servercommandframe == 0 in these cases anyway
167         if (STAT(HEALTH) <= 0)
168         {
169                 csqcplayer_moveframe = clientcommandframe;
170                 getinputstate(csqcplayer_moveframe-1);
171                 LOG_INFO("the Weird code path got hit");
172                 return;
173         }
174 #endif
175
176         if (csqcplayer_moveframe >= endframe)
177         {
178                 getinputstate(csqcplayer_moveframe - 1);
179         }
180         else
181         {
182                 do
183                 {
184                         if (!getinputstate(csqcplayer_moveframe)) break;
185                         /*if (input_timelength > 0.0005)
186                         {
187                                 if (input_timelength > 0.05)
188                                 {
189                                         input_timelength /= 2;
190                                         CSQCPlayer_Physics(this);
191                                 }
192                                 CSQCPlayer_Physics(this);
193                         }*/
194                         CSQCPlayer_Physics(this);
195                         CSQCPlayer_SetMinsMaxs(this);
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(entity this)
206 {
207         return (this == csqcplayer);
208 }
209
210 float stairsmoothz;
211 float autocvar_cl_stairsmoothspeed;
212 float autocvar_cl_smoothviewheight;
213 float smooth_prevtime;
214 float viewheightavg;
215 float autocvar_cl_bobfall;
216 float autocvar_cl_bobfallcycle;
217 float autocvar_cl_bobfallminspeed;
218 float bobfall_swing;
219 float bobfall_speed;
220 vector CSQCPlayer_ApplySmoothing(entity this, vector v)
221 {
222         if(this.csqcmodel_teleported || !IS_ONGROUND(this) || autocvar_cl_stairsmoothspeed <= 0)
223                 stairsmoothz = v.z;
224         else
225         {
226                 if(v.z > stairsmoothz)
227                         v.z = stairsmoothz = bound(v.z - PHYS_STEPHEIGHT(this), stairsmoothz + frametime * autocvar_cl_stairsmoothspeed, v.z);
228                 else if(v.z < stairsmoothz)
229                         v.z = stairsmoothz = bound(v.z, stairsmoothz - frametime * autocvar_cl_stairsmoothspeed, v.z + PHYS_STEPHEIGHT(this));
230         }
231
232         float viewheight = bound(0, (time - smooth_prevtime) / max(0.0001, autocvar_cl_smoothviewheight), 1);
233         viewheightavg = viewheightavg * (1 - viewheight) + this.view_ofs.z * viewheight;
234         v.z += viewheightavg;
235
236         if(autocvar_cl_bobfall && autocvar_cl_bobfallcycle)
237         {
238                 if(!IS_ONGROUND(this))
239                 {
240                         bobfall_speed = bound(-400, this.velocity.z, 0) * bound(0, autocvar_cl_bobfall, 0.1);
241                         if(this.velocity.z < -autocvar_cl_bobfallminspeed)
242                                 bobfall_swing = 1;
243                         else
244                                 bobfall_swing = 0; // really?
245                 }
246                 else
247                 {
248                         bobfall_swing = max(0, bobfall_swing - autocvar_cl_bobfallcycle * frametime);
249                         float bobfall = sin(M_PI * bobfall_swing) * bobfall_speed;
250                         v.z += bobfall;
251                 }
252         }
253
254         smooth_prevtime = time;
255
256         return v;
257 }
258
259 bool autocvar_v_deathtilt;
260 float autocvar_v_deathtiltangle;
261 void CSQCPlayer_ApplyDeathTilt(entity this)
262 {
263         if(!autocvar_v_deathtilt)
264                 return;
265         view_angles.y = autocvar_v_deathtiltangle;
266 }
267
268 float autocvar_v_idlescale;
269 float autocvar_v_ipitch_cycle;
270 float autocvar_v_iyaw_cycle;
271 float autocvar_v_iroll_cycle;
272 float autocvar_v_ipitch_level;
273 float autocvar_v_iyaw_level;
274 float autocvar_v_iroll_level;
275 void CSQCPlayer_ApplyIdleScaling(entity this)
276 {
277         view_angles.x += autocvar_v_idlescale * sin(time * autocvar_v_ipitch_cycle) * autocvar_v_ipitch_level;
278         view_angles.y += autocvar_v_idlescale * sin(time * autocvar_v_iyaw_cycle) * autocvar_v_iyaw_level;
279         view_angles.z += autocvar_v_idlescale * sin(time * autocvar_v_iroll_cycle) * autocvar_v_iroll_level;
280         setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
281 }
282
283 float autocvar_chase_back;
284 float autocvar_chase_up;
285 bool autocvar_chase_overhead;
286 float autocvar_chase_pitchangle;
287 vector CSQCPlayer_ApplyChase(entity this, vector v)
288 {
289         // don't need to do offset for view height here, it's done in smoothing!
290         //v += this.view_ofs;
291         vector forward;
292         vector chase_dest;
293
294         if(autocvar_chase_overhead)
295         {
296                 view_angles.x = 0;
297                 makevectors(view_angles);
298                 forward = v_forward;
299                 vector up = v_up;
300                 // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
301                 chase_dest.x = v.x - forward.x * autocvar_chase_back + up.x * autocvar_chase_up;
302                 chase_dest.y = v.y - forward.y * autocvar_chase_back + up.y * autocvar_chase_up;
303                 chase_dest.z = v.z - forward.z * autocvar_chase_back + up.z * autocvar_chase_up;
304
305                 // trace from first person view location to our chosen third person view location
306                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
307
308                 vector bestvieworg = trace_endpos;
309                 vector offset = '0 0 0';
310                 for(offset.x = -16; offset.x <= 16; offset.x += 8)
311                 {
312                         for(offset.y = -16; offset.y <= 16; offset.y += 8)
313                         {
314                                 makevectors(view_angles);
315                                 up = v_up;
316                                 chase_dest.x = v.x - forward.x * autocvar_chase_back + up.x * autocvar_chase_up + offset.x;
317                                 chase_dest.y = v.y - forward.y * autocvar_chase_back + up.y * autocvar_chase_up + offset.y;
318                                 chase_dest.z = v.z - forward.z * autocvar_chase_back + up.z * autocvar_chase_up + offset.z;
319                                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
320                                 if(bestvieworg.z > trace_endpos.z)
321                                         bestvieworg.z = trace_endpos.z;
322                         }
323                 }
324                 bestvieworg.z -= 8;
325                 v = bestvieworg;
326
327                 view_angles.x = autocvar_chase_pitchangle;
328                 //setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
329         }
330         else
331         {
332                 makevectors(view_angles);
333                 forward = v_forward;
334                 // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
335                 float cdist = -autocvar_chase_back - 8;
336                 chase_dest.x = v.x + forward.x * cdist;
337                 chase_dest.y = v.y + forward.y * cdist;
338                 chase_dest.z = v.z + forward.z * cdist + autocvar_chase_up;
339                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
340                 v.x = 1 * trace_endpos.x + 8 * forward.x + 4 * trace_plane_normal.x;
341                 v.y = 1 * trace_endpos.y + 8 * forward.y + 4 * trace_plane_normal.y;
342                 v.z = 1 * trace_endpos.z + 8 * forward.z + 4 * trace_plane_normal.z;
343         }
344
345 #if 0
346         tracebox(v, '-4 -4 -4', '4 4 4', v - v_forward * autocvar_chase_back, MOVE_NORMAL, this);
347         v = trace_endpos;
348         tracebox(v, '-4 -4 -4', '4 4 4', v + v_up * autocvar_chase_up, MOVE_NORMAL, this);
349         v = trace_endpos;
350 #endif
351         return v;
352 }
353
354 /** Called once per CSQC_UpdateView() */
355 void CSQCPlayer_SetCamera()
356 {
357         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
358         float vh = PHYS_VIEWHEIGHT(NULL);
359         vector pl_viewofs = PHYS_PL_VIEWOFS(NULL);
360         vector pl_viewofs_crouch = PHYS_PL_CROUCH_VIEWOFS(NULL);
361         entity e = csqcplayer;
362         if (e)
363         {
364                 if (servercommandframe == 0 || clientcommandframe == 0)
365                 {
366                         InterpolateOrigin_Do(e);
367                         e.view_ofs = '0 0 1' * vh;
368
369                         // get crouch state from the server
370                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
371                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
372
373                         // get onground state from the server
374                         e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
375
376                         CSQCPlayer_SetMinsMaxs(e);
377
378                         // override it back just in case
379                         e.view_ofs = '0 0 1' * vh;
380
381                         // set velocity
382                         e.velocity = v0;
383                 }
384                 else
385                 {
386                         int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
387                         InterpolateOrigin_Do(e);
388                         e.iflags = flg;
389
390                         if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
391                         {
392                                 vector o = e.origin;
393                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
394                                 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
395                                 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
396                                 e.origin = o;
397                                 e.velocity = v0;
398
399                                 // get crouch state from the server
400                                 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
401                                 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
402
403                                 // get onground state from the server
404                                 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
405
406                                 CSQCPlayer_SavePrediction(e);
407                         }
408                         CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
409
410 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
411                         // get crouch state from the server (LAG)
412                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
413                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
414 #endif
415                         CSQCPlayer_SetMinsMaxs(e);
416
417                         e.angles_y = input_angles.y;
418                 }
419
420                 // relink
421                 setorigin(e, e.origin);
422         }
423
424         const entity view = CSQCModel_server2csqc(player_localentnum - 1);
425         if (view)
426         {
427                 if (view != csqcplayer)
428                 {
429                         InterpolateOrigin_Do(view);
430                         view.view_ofs = '0 0 1' * vh;
431                 }
432                 //int refdefflags = 0;
433                 //if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
434                 //if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
435                 // note: these two only work in WIP2, but are harmless in WIP1
436                 //if (PHYS_HEALTH(NULL) <= 0 && PHYS_HEALTH(NULL) != -666 && PHYS_HEALTH(NULL) != -2342) refdefflags |= REFDEFFLAG_DEAD;
437                 //if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
438                 vector vieworg = view.origin;
439                 vieworg = CSQCPlayer_ApplySmoothing(view, vieworg);
440                 if(autocvar_chase_active)
441                         vieworg = CSQCPlayer_ApplyChase(view, vieworg);
442                 else if(IS_DEAD(view))
443                 {
444                         CSQCPlayer_ApplyDeathTilt(view);
445                 }
446                 if(autocvar_v_idlescale)
447                         CSQCPlayer_ApplyIdleScaling(view);
448                 setproperty(VF_ORIGIN, vieworg);
449                 setproperty(VF_ANGLES, view_angles);
450                 //V_CalcRefdef(view, refdefflags); // TODO? uses .health stat in the engine when this isn't called here, may be broken!
451         }
452         else
453         {
454                 // FIXME by CSQC spec we have to do this:
455                 // but it breaks chase cam
456                 /*
457                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
458                 setproperty(VF_ANGLES, view_angles);
459                 */
460         }
461         CSQCPLAYER_HOOK_POSTCAMERASETUP();
462 }
463
464 void CSQCPlayer_Remove(entity this)
465 {
466         csqcplayer = NULL;
467         cvar_settemp("cl_movement_replay", "1");
468 }
469
470 bool CSQCPlayer_PreUpdate(entity this)
471 {
472         if (this != csqcplayer) return false;
473         if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
474         return true;
475 }
476
477 bool CSQCPlayer_PostUpdate(entity this)
478 {
479         if (this.entnum != player_localnum + 1) return false;
480         csqcplayer = this;
481         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
482         cvar_settemp("cl_movement_replay", "0");
483         this.entremove = CSQCPlayer_Remove;
484         return true;
485 }