]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_player.qc
Smooth v_deathtiltangle effect; don't apply v_deathtiltangle if cl_eventchase_death...
[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) ? ticrate : 1);
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                 if (IS_DEAD(this))
112                         this.maxs.z = 5;
113                 this.view_ofs = PHYS_PL_VIEWOFS(this);
114         }
115 }
116
117 void CSQCPlayer_SavePrediction(entity this)
118 {
119         player_pmflags = this.flags;
120         csqcplayer_origin = this.origin;
121         csqcplayer_velocity = this.velocity;
122         csqcplayer_sequence = servercommandframe;
123         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
124 }
125
126 void CSQC_ClientMovement_PlayerMove_Frame(entity this);
127
128 void CSQCPlayer_Physics(entity this)
129 {
130         if(!autocvar_cl_movement) { return; }
131
132         //_Movetype_CheckWater(this); // we apparently need to check water *before* physics so it can use this for water jump
133
134         vector oldv_angle = this.v_angle;
135         vector oldangles = this.angles; // we need to save these, as they're abused by other code
136         this.v_angle = PHYS_INPUT_ANGLES(this);
137         this.angles = PHYS_WORLD_ANGLES(this);
138
139         CSQC_ClientMovement_PlayerMove_Frame(this);
140
141         Movetype_Physics_NoMatchTicrate(this, PHYS_INPUT_TIMELENGTH, true);
142
143         view_angles = this.v_angle;
144         input_angles = this.angles;
145         this.v_angle = oldv_angle;
146         this.angles = oldangles;
147
148         this.pmove_flags =
149                         ((IS_DUCKED(this)) ? PMF_DUCKED : 0) |
150                         ((IS_JUMP_HELD(this)) ? PMF_JUMP_HELD : 0) |
151                         ((IS_ONGROUND(this)) ? PMF_ONGROUND : 0);
152 }
153
154 void CSQCPlayer_PredictTo(entity this, float endframe, bool apply_error)
155 {
156         CSQCPlayer_Unpredict(this);
157         if (apply_error)
158         {
159                 this.origin += CSQCPlayer_GetPredictionErrorO();
160                 this.velocity += CSQCPlayer_GetPredictionErrorV();
161         }
162         CSQCPlayer_SetMinsMaxs(this);
163
164         csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
165
166 #if 0
167         // we don't need this
168         // darkplaces makes servercommandframe == 0 in these cases anyway
169         if (STAT(HEALTH) <= 0)
170         {
171                 csqcplayer_moveframe = clientcommandframe;
172                 getinputstate(csqcplayer_moveframe-1);
173                 LOG_INFO("the Weird code path got hit");
174                 return;
175         }
176 #endif
177
178         if (csqcplayer_moveframe >= endframe)
179         {
180                 getinputstate(csqcplayer_moveframe - 1);
181         }
182         else
183         {
184                 do
185                 {
186                         if (!getinputstate(csqcplayer_moveframe)) break;
187                         /*if (input_timelength > 0.0005)
188                         {
189                                 if (input_timelength > 0.05)
190                                 {
191                                         input_timelength /= 2;
192                                         CSQCPlayer_Physics(this);
193                                 }
194                                 CSQCPlayer_Physics(this);
195                         }*/
196                         CSQCPlayer_Physics(this);
197                         CSQCPlayer_SetMinsMaxs(this);
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(entity this)
208 {
209         return (this == csqcplayer);
210 }
211
212 float stairsmoothz;
213 float autocvar_cl_stairsmoothspeed = 200;
214 float autocvar_cl_smoothviewheight = 0.05;
215 float smooth_prevtime;
216 float viewheightavg;
217 vector CSQCPlayer_ApplySmoothing(entity this, vector v)
218 {
219         float smoothtime = bound(0, time - smooth_prevtime, 0.1);
220         smooth_prevtime = max(smooth_prevtime, drawtime); // drawtime is the previous frame's time at this point
221
222         if(this.csqcmodel_teleported || !(this.pmove_flags & PMF_ONGROUND) || autocvar_cl_stairsmoothspeed <= 0 || this.ground_networkentity)
223                 stairsmoothz = v.z;
224         else
225         {
226                 if(stairsmoothz < v.z)
227                         v.z = stairsmoothz = bound(v.z - PHYS_STEPHEIGHT(this), stairsmoothz + smoothtime * autocvar_cl_stairsmoothspeed, v.z);
228                 else if(stairsmoothz > v.z)
229                         v.z = stairsmoothz = bound(v.z, stairsmoothz - smoothtime * 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         smooth_prevtime = time;
237
238         return v;
239 }
240
241 // simplified copy of CSQCPlayer_ApplySmoothing for use on player models
242 .float stairsmooth_offset;
243 .float stairsmooth_prevtime;
244 .float stairsmooth_drawtime; // holds the previous draw time
245 vector CSQCModel_ApplyStairSmoothing(entity this, bool isonground, vector v)
246 {
247         float smoothtime = bound(0, time - this.stairsmooth_prevtime, 0.1);
248         this.stairsmooth_prevtime = max(this.stairsmooth_prevtime, this.stairsmooth_drawtime); // stairsmooth_drawtime is the previous frame's time at this point
249
250         if(this.csqcmodel_teleported || !isonground || autocvar_cl_stairsmoothspeed <= 0 || this.ground_networkentity)
251                 this.stairsmooth_offset = v.z;
252         else
253         {
254                 if(this.stairsmooth_offset < v.z)
255                         v.z = this.stairsmooth_offset = bound(v.z - PHYS_STEPHEIGHT(this), this.stairsmooth_offset + smoothtime * autocvar_cl_stairsmoothspeed, v.z);
256                 else if(this.stairsmooth_offset > v.z)
257                         v.z = this.stairsmooth_offset = bound(v.z, this.stairsmooth_offset - smoothtime * autocvar_cl_stairsmoothspeed, v.z + PHYS_STEPHEIGHT(this));
258         }
259
260         this.stairsmooth_prevtime = time;
261         this.stairsmooth_drawtime = drawtime;
262
263         return v;
264 }
265
266 bool autocvar_v_deathtilt;
267 float autocvar_v_deathtiltangle;
268 void CSQCPlayer_ApplyDeathTilt(entity this)
269 {
270         if(!this.csqcmodel_isdead || !autocvar_v_deathtilt)
271                 return;
272         // incompatible with cl_eventchase_death 2 because tilting is applied while the corpse is
273         // in the air and immediately turned off when it touches the ground
274         if (autocvar_cl_eventchase_death == 2 && this.csqcmodel_isdead)
275                 return;
276         view_angles.z += min((time - this.death_time) * 8, 1) * autocvar_v_deathtiltangle;
277 }
278
279 float autocvar_v_idlescale;
280 float autocvar_v_ipitch_cycle;
281 float autocvar_v_iyaw_cycle;
282 float autocvar_v_iroll_cycle;
283 float autocvar_v_ipitch_level;
284 float autocvar_v_iyaw_level;
285 float autocvar_v_iroll_level;
286 void CSQCPlayer_ApplyIdleScaling(entity this)
287 {
288         if(!autocvar_v_idlescale)
289                 return;
290         view_angles.x += autocvar_v_idlescale * sin(time * autocvar_v_ipitch_cycle) * autocvar_v_ipitch_level;
291         view_angles.y += autocvar_v_idlescale * sin(time * autocvar_v_iyaw_cycle) * autocvar_v_iyaw_level;
292         view_angles.z += autocvar_v_idlescale * sin(time * autocvar_v_iroll_cycle) * autocvar_v_iroll_level;
293         //setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
294 }
295
296 float autocvar_cl_bob = 0;
297 float autocvar_cl_bobcycle = 0.5;
298 float autocvar_cl_bob_limit = 7;
299 float autocvar_cl_bob_limit_heightcheck = 0;
300 float autocvar_cl_bob_velocity_limit = 400;
301 float autocvar_cl_bobup = 0.5;
302 float autocvar_cl_bobfall = 0.05;
303 float autocvar_cl_bobfallcycle = 3;
304 float autocvar_cl_bobfallminspeed = 200;
305 float autocvar_cl_bob2 = 0;
306 float autocvar_cl_bob2cycle = 1;
307 float autocvar_cl_bob2smooth = 0.05;
308 float bobfall_swing;
309 float bobfall_speed;
310 float bob2_smooth;
311 vector CSQCPlayer_ApplyBobbing(entity this, vector v)
312 {
313         if(this.csqcmodel_isdead || PHYS_INVEHICLE(this) || !(this.isplayermodel & ISPLAYER_PLAYER))
314                 return v;
315
316         // bounded XY speed, used by several effects below
317         float bob, cycle;
318
319         // vertical view bobbing code
320         if(autocvar_cl_bob && autocvar_cl_bobcycle)
321         {
322                 float bob_limit = autocvar_cl_bob_limit;
323
324                 if(autocvar_cl_bob_limit_heightcheck)
325                 {
326                         // use traces to determine what range the view can bob in, and scale down the bob as needed
327                         vector bob_height_check_dest = v;
328                         bob_height_check_dest.z += autocvar_cl_bob_limit * 1.1;
329                         traceline(v, bob_height_check_dest, MOVE_NOMONSTERS, NULL);
330                         float trace1fraction = trace_fraction;
331
332                         bob_height_check_dest = v;
333                         bob_height_check_dest.z += autocvar_cl_bob_limit * -0.5;
334                         traceline(v, bob_height_check_dest, MOVE_NOMONSTERS, NULL);
335                         float trace2fraction = trace_fraction;
336
337                         bob_limit *= min(trace1fraction, trace2fraction);
338                 }
339
340                 // LordHavoc: figured out bobup: the time at which the sin is at 180
341                 // degrees (which allows lengthening or squishing the peak or valley)
342                 cycle = time / autocvar_cl_bobcycle;
343                 cycle -= rint(cycle);
344                 if(cycle < autocvar_cl_bobup)
345                         cycle = sin(M_PI * cycle / autocvar_cl_bobup);
346                 else
347                         cycle = sin(M_PI + M_PI * (cycle - autocvar_cl_bobup) / (1.0 - autocvar_cl_bobup));
348                 // bob is proportional to velocity in the xy plane
349                 // (don't count Z, or jumping messes it up)
350                 float xyspeed = bound(0, sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y), autocvar_cl_bob_velocity_limit);
351                 bob = xyspeed * autocvar_cl_bob;
352                 bob = bound(0, bob, bob_limit);
353                 bob = bob * 0.3 + bob * 0.7 * cycle;
354                 v.z += bob;
355         }
356
357         // horizontal view bobbing code
358         if(autocvar_cl_bob2 && autocvar_cl_bob2cycle)
359         {
360                 cycle = time / autocvar_cl_bob2cycle;
361                 cycle -= rint(cycle);
362                 if(cycle < 0.5)
363                         cycle = cos(M_PI * cycle / 0.5); // cos looks better here with the other view bobbing using sin
364                 else
365                         cycle = cos(M_PI + M_PI * (cycle - 0.5) / 0.5);
366                 bob = autocvar_cl_bob2 * cycle;
367
368                 // this value slowly decreases from 1 to 0 when we stop touching the ground.
369                 // The cycle is later multiplied with it so the view smooths back to normal
370                 if(IS_ONGROUND(this) && !(input_buttons & BIT(1))) // also block the effect while the jump button is pressed, to avoid twitches when bunny-hopping
371                         bob2_smooth = 1;
372                 else
373                 {
374                         if(bob2_smooth > 0)
375                                 bob2_smooth -= bound(0, autocvar_cl_bob2smooth, 1);
376                         else
377                                 bob2_smooth = 0;
378                 }
379
380                 // calculate the front and side of the player between the X and Y axes
381                 makevectors(view_angles);
382                 // now get the speed based on those angles. The bounds should match the same value as xyspeed's
383                 float side = bound(-autocvar_cl_bob_velocity_limit, (this.velocity * v_right) * bob2_smooth, autocvar_cl_bob_velocity_limit);
384                 float front = bound(-autocvar_cl_bob_velocity_limit, (this.velocity * v_forward) * bob2_smooth, autocvar_cl_bob_velocity_limit);
385                 v_forward = v_forward * bob;
386                 v_right = v_right * bob;
387                 // we use side with forward and front with right, so the bobbing goes
388                 // to the side when we walk forward and to the front when we strafe
389                 vector bob2vel;
390                 bob2vel.x = side * v_forward.x + front * v_right.x + 0 * v_up.x;
391                 bob2vel.y = side * v_forward.y + front * v_right.y + 0 * v_up.y;
392                 bob2vel.z = side * v_forward.z + front * v_right.z + 0 * v_up.z;
393                 v.x += bob2vel.x;
394                 v.y += bob2vel.y;
395         }
396
397         // fall bobbing code
398         // causes the view to swing down and back up when touching the ground
399         if(autocvar_cl_bobfall && autocvar_cl_bobfallcycle)
400         {
401                 if(!IS_ONGROUND(this))
402                 {
403                         bobfall_speed = bound(-400, this.velocity.z, 0) * bound(0, autocvar_cl_bobfall, 0.1);
404                         if(this.velocity.z < -autocvar_cl_bobfallminspeed)
405                                 bobfall_swing = 1;
406                         else
407                                 bobfall_swing = 0; // really?
408                 }
409                 else
410                 {
411                         bobfall_swing = max(0, bobfall_swing - autocvar_cl_bobfallcycle * frametime);
412                         float bobfall = sin(M_PI * bobfall_swing) * bobfall_speed;
413                         v.z += bobfall;
414                 }
415         }
416
417         return v;
418 }
419
420 float autocvar_cl_rollangle;
421 float autocvar_cl_rollspeed;
422 float CSQCPlayer_CalcRoll(entity this)
423 {
424         if (!autocvar_cl_rollangle) return 0;
425
426         makevectors(view_angles);
427         float side = (this.velocity * v_right);
428         float sign = (side < 0) ? -1 : 1;
429         side = fabs(side);
430
431         if(side < autocvar_cl_rollspeed)
432                 side = side * autocvar_cl_rollangle / autocvar_cl_rollspeed;
433         else
434                 side = autocvar_cl_rollangle;
435
436         return side * sign;
437 }
438
439 float autocvar_chase_back;
440 float autocvar_chase_up;
441 bool autocvar_chase_overhead;
442 float autocvar_chase_pitchangle;
443 vector CSQCPlayer_ApplyChase(entity this, vector v)
444 {
445         vector forward;
446         vector chase_dest;
447
448         if(autocvar_chase_overhead)
449         {
450                 view_angles.x = 0;
451                 makevectors(view_angles);
452                 forward = v_forward;
453                 vector up = v_up;
454                 // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
455                 chase_dest.x = v.x - forward.x * autocvar_chase_back + up.x * autocvar_chase_up;
456                 chase_dest.y = v.y - forward.y * autocvar_chase_back + up.y * autocvar_chase_up;
457                 chase_dest.z = v.z - forward.z * autocvar_chase_back + up.z * autocvar_chase_up;
458
459                 // trace from first person view location to our chosen third person view location
460                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
461
462                 vector bestvieworg = trace_endpos;
463                 vector offset = '0 0 0';
464                 for(offset.x = -16; offset.x <= 16; offset.x += 8)
465                 {
466                         for(offset.y = -16; offset.y <= 16; offset.y += 8)
467                         {
468                                 makevectors(view_angles);
469                                 up = v_up;
470                                 chase_dest.x = v.x - forward.x * autocvar_chase_back + up.x * autocvar_chase_up + offset.x;
471                                 chase_dest.y = v.y - forward.y * autocvar_chase_back + up.y * autocvar_chase_up + offset.y;
472                                 chase_dest.z = v.z - forward.z * autocvar_chase_back + up.z * autocvar_chase_up + offset.z;
473                                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
474                                 if(bestvieworg.z > trace_endpos.z)
475                                         bestvieworg.z = trace_endpos.z;
476                         }
477                 }
478                 bestvieworg.z -= 8;
479                 v = bestvieworg;
480
481                 view_angles.x = autocvar_chase_pitchangle;
482                 //setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
483         }
484         else
485         {
486                 makevectors(view_angles);
487                 forward = v_forward;
488                 if(autocvar_chase_front && spectatee_status != -1)
489                         forward = normalize(forward * -1);
490                 // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
491                 float cdist = -autocvar_chase_back - 8;
492                 chase_dest.x = v.x + forward.x * cdist;
493                 chase_dest.y = v.y + forward.y * cdist;
494                 chase_dest.z = v.z + forward.z * cdist + autocvar_chase_up;
495                 if (this.move_movetype == MOVETYPE_NOCLIP)
496                 {
497                         // as if traceline hasn't hit anything
498                         trace_endpos = chase_dest;
499                         trace_plane_normal = '0 0 0';
500                 }
501                 else
502                 {
503                         traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
504                 }
505                 v.x = 1 * trace_endpos.x + 8 * forward.x + 4 * trace_plane_normal.x;
506                 v.y = 1 * trace_endpos.y + 8 * forward.y + 4 * trace_plane_normal.y;
507                 v.z = 1 * trace_endpos.z + 8 * forward.z + 4 * trace_plane_normal.z;
508
509                 if(autocvar_chase_front && spectatee_status != -1)
510                 {
511                         // now flip the view so the player is looking at themselves
512                         vector newang = vectoangles(forward);
513                         view_angles.x = view_angles.x * -1; // inverse up-down looking direction
514                         view_angles.y = newang.y;
515                 }
516         }
517
518 #if 0
519         tracebox(v, '-4 -4 -4', '4 4 4', v - v_forward * autocvar_chase_back, MOVE_NORMAL, this);
520         v = trace_endpos;
521         tracebox(v, '-4 -4 -4', '4 4 4', v + v_up * autocvar_chase_up, MOVE_NORMAL, this);
522         v = trace_endpos;
523 #endif
524         return v;
525 }
526
527 void CSQCPlayer_CalcRefdef(entity this)
528 {
529         vector vieworg = this.origin;
530         if(intermission)
531         {
532                 // just update view offset, don't need to do anything else
533                 vieworg.z += this.view_ofs.z;
534         }
535         else
536         {
537                 vieworg = CSQCPlayer_ApplySmoothing(this, vieworg);
538                 if(autocvar_chase_active)
539                         vieworg = CSQCPlayer_ApplyChase(this, vieworg);
540                 else
541                 {
542                         // angles
543                         CSQCPlayer_ApplyDeathTilt(this);
544                         view_angles = view_angles + view_punchangle;
545                         view_angles.z += CSQCPlayer_CalcRoll(this);
546                         // TODO? we don't have damage time accessible here
547                         // origin
548                         vieworg = vieworg + view_punchvector;
549                         vieworg = CSQCPlayer_ApplyBobbing(this, vieworg);
550                 }
551                 CSQCPlayer_ApplyIdleScaling(this);
552         }
553         setproperty(VF_ORIGIN, vieworg);
554         setproperty(VF_ANGLES, view_angles);
555 }
556
557 bool autocvar_cl_useenginerefdef = false;
558
559 /** Called once per CSQC_UpdateView() */
560 void CSQCPlayer_SetCamera()
561 {
562         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
563         float vh = PHYS_VIEWHEIGHT(NULL);
564         vector pl_viewofs = PHYS_PL_VIEWOFS(NULL);
565         vector pl_viewofs_crouch = PHYS_PL_CROUCH_VIEWOFS(NULL);
566         entity e = csqcplayer;
567         if (e)
568         {
569                 if (servercommandframe == 0 || clientcommandframe == 0)
570                 {
571                         InterpolateOrigin_Do(e);
572                         e.view_ofs = '0 0 1' * vh;
573
574                         // get crouch state from the server
575                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
576                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
577
578                         // get onground state from the server
579                         e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
580
581                         CSQCPlayer_SetMinsMaxs(e);
582
583                         // override it back just in case
584                         e.view_ofs = '0 0 1' * vh;
585
586                         // set velocity
587                         e.velocity = v0;
588                 }
589                 else
590                 {
591                         int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
592                         InterpolateOrigin_Do(e);
593                         e.iflags = flg;
594
595                         if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
596                         {
597                                 vector o = e.origin;
598                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
599                                 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
600                                 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
601                                 e.origin = o;
602                                 e.velocity = v0;
603
604                                 // get crouch state from the server
605                                 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
606                                 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
607
608                                 // get onground state from the server
609                                 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
610
611                                 CSQCPlayer_SavePrediction(e);
612                         }
613                         CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
614
615 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
616                         // get crouch state from the server (LAG)
617                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
618                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
619 #endif
620                         CSQCPlayer_SetMinsMaxs(e);
621
622                         if (!IS_DEAD(e))
623                                 e.angles.y = input_angles.y;
624                 }
625
626                 // relink
627                 e.stairsmooth_drawtime = drawtime; // since drawtime is a frame old at this point, copy it now to avoid using a drawtime 2 frames old!
628                 e.origin = CSQCModel_ApplyStairSmoothing(e, (e.pmove_flags & PMF_ONGROUND), e.origin);
629                 setorigin(e, e.origin);
630         }
631
632         const entity view = CSQCModel_server2csqc(player_localentnum - 1);
633         if (view)
634         {
635                 if (view != csqcplayer)
636                 {
637                         InterpolateOrigin_Do(view);
638                         view.view_ofs = '0 0 1' * vh;
639                 }
640                 if(autocvar_cl_useenginerefdef)
641                 {
642                         int refdefflags = 0;
643                         if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
644                         if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
645                         // note: these two only work in WIP2, but are harmless in WIP1
646                         if (PHYS_HEALTH(NULL) <= 0 && PHYS_HEALTH(NULL) != -666 && PHYS_HEALTH(NULL) != -2342) refdefflags |= REFDEFFLAG_DEAD;
647                         if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
648                         V_CalcRefdef(view, refdefflags); // TODO? uses .health stat in the engine when this isn't called here, may be broken!
649                 }
650                 else
651                 {
652                         CSQCPlayer_CalcRefdef(view);
653                 }
654         }
655         else
656         {
657                 // FIXME by CSQC spec we have to do this:
658                 // but it breaks chase cam
659                 /*
660                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
661                 setproperty(VF_ANGLES, view_angles);
662                 */
663         }
664         CSQCPLAYER_HOOK_POSTCAMERASETUP();
665 }
666
667 void CSQCPlayer_Remove(entity this)
668 {
669         csqcplayer = NULL;
670         cvar_settemp("cl_movement_replay", "1");
671 }
672
673 bool CSQCPlayer_PreUpdate(entity this)
674 {
675         if (this != csqcplayer) return false;
676         if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
677         return true;
678 }
679
680 bool CSQCPlayer_PostUpdate(entity this)
681 {
682         if (this.entnum != player_localnum + 1) return false;
683         csqcplayer = this;
684         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
685         if (cvar("cl_movement_replay"))
686                 cvar_settemp("cl_movement_replay", "0");
687         this.entremove = CSQCPlayer_Remove;
688         return true;
689 }