]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/csqcmodel/cl_player.qc
4d41cd316103ef35a59b99c6578b44b0d2ba8179
[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 vector CSQCPlayer_ApplySmoothing(entity this, vector v)
216 {
217         float smoothtime = bound(0, time - smooth_prevtime, 0.1);
218         smooth_prevtime = max(smooth_prevtime, drawtime); // drawtime is the previous frame's time at this point
219
220         if(this.csqcmodel_teleported || !(this.pmove_flags & PMF_ONGROUND) || autocvar_cl_stairsmoothspeed <= 0)
221                 stairsmoothz = v.z;
222         else
223         {
224                 if(stairsmoothz < v.z)
225                         v.z = stairsmoothz = bound(v.z - PHYS_STEPHEIGHT(this), stairsmoothz + smoothtime * autocvar_cl_stairsmoothspeed, v.z);
226                 else if(stairsmoothz > v.z)
227                         v.z = stairsmoothz = bound(v.z, stairsmoothz - smoothtime * autocvar_cl_stairsmoothspeed, v.z + PHYS_STEPHEIGHT(this));
228         }
229
230         float viewheight = bound(0, (time - smooth_prevtime) / max(0.0001, autocvar_cl_smoothviewheight), 1);
231         viewheightavg = viewheightavg * (1 - viewheight) + this.view_ofs.z * viewheight;
232         v.z += viewheightavg;
233
234         smooth_prevtime = time;
235
236         return v;
237 }
238
239 bool autocvar_v_deathtilt;
240 float autocvar_v_deathtiltangle;
241 void CSQCPlayer_ApplyDeathTilt(entity this)
242 {
243         if(!IS_DEAD(this) || !autocvar_v_deathtilt)
244                 return;
245         view_angles.z = autocvar_v_deathtiltangle;
246 }
247
248 float autocvar_v_idlescale;
249 float autocvar_v_ipitch_cycle;
250 float autocvar_v_iyaw_cycle;
251 float autocvar_v_iroll_cycle;
252 float autocvar_v_ipitch_level;
253 float autocvar_v_iyaw_level;
254 float autocvar_v_iroll_level;
255 void CSQCPlayer_ApplyIdleScaling(entity this)
256 {
257         if(!autocvar_v_idlescale)
258                 return;
259         view_angles.x += autocvar_v_idlescale * sin(time * autocvar_v_ipitch_cycle) * autocvar_v_ipitch_level;
260         view_angles.y += autocvar_v_idlescale * sin(time * autocvar_v_iyaw_cycle) * autocvar_v_iyaw_level;
261         view_angles.z += autocvar_v_idlescale * sin(time * autocvar_v_iroll_cycle) * autocvar_v_iroll_level;
262         //setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
263 }
264
265 float autocvar_cl_bob;
266 float autocvar_cl_bobcycle;
267 float autocvar_cl_bob_limit;
268 float autocvar_cl_bob_limit_heightcheck;
269 float autocvar_cl_bob_velocity_limit;
270 float autocvar_cl_bobup;
271 float autocvar_cl_bobfall;
272 float autocvar_cl_bobfallcycle;
273 float autocvar_cl_bobfallminspeed;
274 float autocvar_cl_bob2;
275 float autocvar_cl_bob2cycle;
276 float autocvar_cl_bob2smooth;
277 float bobfall_swing;
278 float bobfall_speed;
279 float bob2_smooth;
280 vector CSQCPlayer_ApplyBobbing(entity this, vector v)
281 {
282         if(IS_DEAD(this))
283                 return v;
284
285         // bounded XY speed, used by several effects below
286         float bob, cycle;
287
288         // vertical view bobbing code
289         if(autocvar_cl_bob && autocvar_cl_bobcycle)
290         {
291                 float bob_limit = autocvar_cl_bob_limit;
292
293                 if(autocvar_cl_bob_limit_heightcheck)
294                 {
295                         // use traces to determine what range the view can bob in, and scale down the bob as needed
296                         vector bob_height_check_dest = v;
297                         bob_height_check_dest.z += autocvar_cl_bob_limit * 1.1;
298                         traceline(v, bob_height_check_dest, MOVE_NOMONSTERS, NULL);
299                         float trace1fraction = trace_fraction;
300
301                         bob_height_check_dest = v;
302                         bob_height_check_dest.z += autocvar_cl_bob_limit * -0.5;
303                         traceline(v, bob_height_check_dest, MOVE_NOMONSTERS, NULL);
304                         float trace2fraction = trace_fraction;
305
306                         bob_limit *= min(trace1fraction, trace2fraction);
307                 }
308
309                 // LordHavoc: figured out bobup: the time at which the sin is at 180
310                 // degrees (which allows lengthening or squishing the peak or valley)
311                 cycle = time / autocvar_cl_bobcycle;
312                 cycle -= rint(cycle);
313                 if(cycle < autocvar_cl_bobup)
314                         cycle = sin(M_PI * cycle / autocvar_cl_bobup);
315                 else
316                         cycle = sin(M_PI + M_PI * (cycle - autocvar_cl_bobup) / (1.0 - autocvar_cl_bobup));
317                 // bob is proportional to velocity in the xy plane
318                 // (don't count Z, or jumping messes it up)
319                 float xyspeed = bound(0, sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y), autocvar_cl_bob_velocity_limit);
320                 bob = xyspeed * autocvar_cl_bob;
321                 bob = bound(0, bob, bob_limit);
322                 bob = bob * 0.3 + bob * 0.7 * cycle;
323                 v.z += bob;
324         }
325
326         // horizontal view bobbing code
327         if(autocvar_cl_bob2 && autocvar_cl_bob2cycle)
328         {
329                 cycle = time / autocvar_cl_bob2cycle;
330                 cycle -= rint(cycle);
331                 if(cycle < 0.5)
332                         cycle = cos(M_PI * cycle / 0.5); // cos looks better here with the other view bobbing using sin
333                 else
334                         cycle = cos(M_PI + M_PI * (cycle - 0.5) / 0.5);
335                 bob = autocvar_cl_bob2 * cycle;
336
337                 // this value slowly decreases from 1 to 0 when we stop touching the ground.
338                 // The cycle is later multiplied with it so the view smooths back to normal
339                 if(IS_ONGROUND(this) && !(input_buttons & BIT(1))) // also block the effect while the jump button is pressed, to avoid twitches when bunny-hopping
340                         bob2_smooth = 1;
341                 else
342                 {
343                         if(bob2_smooth > 0)
344                                 bob2_smooth -= bound(0, autocvar_cl_bob2smooth, 1);
345                         else
346                                 bob2_smooth = 0;
347                 }
348
349                 // calculate the front and side of the player between the X and Y axes
350                 makevectors(view_angles);
351                 // now get the speed based on those angles. The bounds should match the same value as xyspeed's
352                 float side = bound(-autocvar_cl_bob_velocity_limit, (this.velocity * v_right) * bob2_smooth, autocvar_cl_bob_velocity_limit);
353                 float front = bound(-autocvar_cl_bob_velocity_limit, (this.velocity * v_forward) * bob2_smooth, autocvar_cl_bob_velocity_limit);
354                 v_forward = v_forward * bob;
355                 v_right = v_right * bob;
356                 // we use side with forward and front with right, so the bobbing goes
357                 // to the side when we walk forward and to the front when we strafe
358                 vector bob2vel;
359                 bob2vel.x = side * v_forward.x + front * v_right.x + 0 * v_up.x;
360                 bob2vel.y = side * v_forward.y + front * v_right.y + 0 * v_up.y;
361                 bob2vel.z = side * v_forward.z + front * v_right.z + 0 * v_up.z;
362                 v.x += bob2vel.x;
363                 v.y += bob2vel.y;
364         }
365
366         // fall bobbing code
367         // causes the view to swing down and back up when touching the ground
368         if(autocvar_cl_bobfall && autocvar_cl_bobfallcycle)
369         {
370                 if(!IS_ONGROUND(this))
371                 {
372                         bobfall_speed = bound(-400, this.velocity.z, 0) * bound(0, autocvar_cl_bobfall, 0.1);
373                         if(this.velocity.z < -autocvar_cl_bobfallminspeed)
374                                 bobfall_swing = 1;
375                         else
376                                 bobfall_swing = 0; // really?
377                 }
378                 else
379                 {
380                         bobfall_swing = max(0, bobfall_swing - autocvar_cl_bobfallcycle * frametime);
381                         float bobfall = sin(M_PI * bobfall_swing) * bobfall_speed;
382                         v.z += bobfall;
383                 }
384         }
385
386         return v;
387 }
388
389 float autocvar_cl_rollangle;
390 float autocvar_cl_rollspeed;
391 float CSQCPlayer_CalcRoll(entity this)
392 {
393         makevectors(view_angles);
394         float side = (this.velocity * v_right);
395         float sign = (side < 0) ? -1 : 1;
396         side = fabs(side);
397
398         if(side < autocvar_cl_rollspeed)
399                 side = side * autocvar_cl_rollangle / autocvar_cl_rollspeed;
400         else
401                 side = autocvar_cl_rollangle;
402
403         return side * sign;
404 }
405
406 float autocvar_chase_back;
407 float autocvar_chase_up;
408 bool autocvar_chase_overhead;
409 float autocvar_chase_pitchangle;
410 vector CSQCPlayer_ApplyChase(entity this, vector v)
411 {
412         vector forward;
413         vector chase_dest;
414
415         if(autocvar_chase_overhead)
416         {
417                 view_angles.x = 0;
418                 makevectors(view_angles);
419                 forward = v_forward;
420                 vector up = v_up;
421                 // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
422                 chase_dest.x = v.x - forward.x * autocvar_chase_back + up.x * autocvar_chase_up;
423                 chase_dest.y = v.y - forward.y * autocvar_chase_back + up.y * autocvar_chase_up;
424                 chase_dest.z = v.z - forward.z * autocvar_chase_back + up.z * autocvar_chase_up;
425
426                 // trace from first person view location to our chosen third person view location
427                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
428
429                 vector bestvieworg = trace_endpos;
430                 vector offset = '0 0 0';
431                 for(offset.x = -16; offset.x <= 16; offset.x += 8)
432                 {
433                         for(offset.y = -16; offset.y <= 16; offset.y += 8)
434                         {
435                                 makevectors(view_angles);
436                                 up = v_up;
437                                 chase_dest.x = v.x - forward.x * autocvar_chase_back + up.x * autocvar_chase_up + offset.x;
438                                 chase_dest.y = v.y - forward.y * autocvar_chase_back + up.y * autocvar_chase_up + offset.y;
439                                 chase_dest.z = v.z - forward.z * autocvar_chase_back + up.z * autocvar_chase_up + offset.z;
440                                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
441                                 if(bestvieworg.z > trace_endpos.z)
442                                         bestvieworg.z = trace_endpos.z;
443                         }
444                 }
445                 bestvieworg.z -= 8;
446                 v = bestvieworg;
447
448                 view_angles.x = autocvar_chase_pitchangle;
449                 //setproperty(VF_CL_VIEWANGLES, view_angles); // update view angles as well so we can aim
450         }
451         else
452         {
453                 makevectors(view_angles);
454                 forward = v_forward;
455                 // trace a little further so it hits a surface more consistently (to avoid 'snapping' on the edge of the range)
456                 float cdist = -autocvar_chase_back - 8;
457                 chase_dest.x = v.x + forward.x * cdist;
458                 chase_dest.y = v.y + forward.y * cdist;
459                 chase_dest.z = v.z + forward.z * cdist + autocvar_chase_up;
460                 traceline(v, chase_dest, MOVE_NOMONSTERS, NULL);
461                 v.x = 1 * trace_endpos.x + 8 * forward.x + 4 * trace_plane_normal.x;
462                 v.y = 1 * trace_endpos.y + 8 * forward.y + 4 * trace_plane_normal.y;
463                 v.z = 1 * trace_endpos.z + 8 * forward.z + 4 * trace_plane_normal.z;
464         }
465
466 #if 0
467         tracebox(v, '-4 -4 -4', '4 4 4', v - v_forward * autocvar_chase_back, MOVE_NORMAL, this);
468         v = trace_endpos;
469         tracebox(v, '-4 -4 -4', '4 4 4', v + v_up * autocvar_chase_up, MOVE_NORMAL, this);
470         v = trace_endpos;
471 #endif
472         return v;
473 }
474
475 void CSQCPlayer_CalcRefdef(entity this)
476 {
477         vector vieworg = this.origin;
478         if(intermission)
479         {
480                 // just update view offset, don't need to do anything else
481                 vieworg.z += this.view_ofs.z;
482         }
483         else
484         {
485                 vieworg = CSQCPlayer_ApplySmoothing(this, vieworg);
486                 if(autocvar_chase_active)
487                         vieworg = CSQCPlayer_ApplyChase(this, vieworg);
488                 else
489                 {
490                         // angles
491                         CSQCPlayer_ApplyDeathTilt(this);
492                         view_angles = view_angles + view_punchangle;
493                         view_angles.z += CSQCPlayer_CalcRoll(this);
494                         // TODO? we don't have damage time accessible here
495                         // origin
496                         vieworg = vieworg + view_punchvector;
497                         vieworg = CSQCPlayer_ApplyBobbing(this, vieworg);
498                 }
499                 CSQCPlayer_ApplyIdleScaling(this);
500         }
501         setproperty(VF_ORIGIN, vieworg);
502         setproperty(VF_ANGLES, view_angles);
503 }
504
505 bool autocvar_cl_useenginerefdef = true;
506
507 /** Called once per CSQC_UpdateView() */
508 void CSQCPlayer_SetCamera()
509 {
510         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
511         float vh = PHYS_VIEWHEIGHT(NULL);
512         vector pl_viewofs = PHYS_PL_VIEWOFS(NULL);
513         vector pl_viewofs_crouch = PHYS_PL_CROUCH_VIEWOFS(NULL);
514         entity e = csqcplayer;
515         if (e)
516         {
517                 if (servercommandframe == 0 || clientcommandframe == 0)
518                 {
519                         InterpolateOrigin_Do(e);
520                         e.view_ofs = '0 0 1' * vh;
521
522                         // get crouch state from the server
523                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
524                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
525
526                         // get onground state from the server
527                         e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
528
529                         CSQCPlayer_SetMinsMaxs(e);
530
531                         // override it back just in case
532                         e.view_ofs = '0 0 1' * vh;
533
534                         // set velocity
535                         e.velocity = v0;
536                 }
537                 else
538                 {
539                         int flg = e.iflags; e.iflags &= ~(IFLAG_ORIGIN | IFLAG_ANGLES);
540                         InterpolateOrigin_Do(e);
541                         e.iflags = flg;
542
543                         if (csqcplayer_status == CSQCPLAYERSTATUS_FROMSERVER)
544                         {
545                                 vector o = e.origin;
546                                 csqcplayer_status = CSQCPLAYERSTATUS_PREDICTED;
547                                 CSQCPlayer_PredictTo(e, servercommandframe + 1, false);
548                                 CSQCPlayer_SetPredictionError(e.origin - o, e.velocity - v0, pmove_onground - IS_ONGROUND(e));
549                                 e.origin = o;
550                                 e.velocity = v0;
551
552                                 // get crouch state from the server
553                                 if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
554                                 else if(vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
555
556                                 // get onground state from the server
557                                 e.flags = BITSET(e.flags, FL_ONGROUND, pmove_onground);
558
559                                 CSQCPlayer_SavePrediction(e);
560                         }
561                         CSQCPlayer_PredictTo(e, clientcommandframe + 1, true);
562
563 #ifdef CSQCMODEL_SERVERSIDE_CROUCH
564                         // get crouch state from the server (LAG)
565                         if (vh == pl_viewofs.z) e.flags &= ~FL_DUCKED;
566                         else if (vh == pl_viewofs_crouch.z) e.flags |= FL_DUCKED;
567 #endif
568                         CSQCPlayer_SetMinsMaxs(e);
569
570                         e.angles_y = input_angles.y;
571                 }
572
573                 // relink
574                 setorigin(e, e.origin);
575         }
576
577         const entity view = CSQCModel_server2csqc(player_localentnum - 1);
578         if (view)
579         {
580                 if (view != csqcplayer)
581                 {
582                         InterpolateOrigin_Do(view);
583                         view.view_ofs = '0 0 1' * vh;
584                 }
585                 if(autocvar_cl_useenginerefdef)
586                 {
587                         int refdefflags = 0;
588                         if (view.csqcmodel_teleported) refdefflags |= REFDEFFLAG_TELEPORTED;
589                         if (input_buttons & BIT(1)) refdefflags |= REFDEFFLAG_JUMPING;
590                         // note: these two only work in WIP2, but are harmless in WIP1
591                         if (PHYS_HEALTH(NULL) <= 0 && PHYS_HEALTH(NULL) != -666 && PHYS_HEALTH(NULL) != -2342) refdefflags |= REFDEFFLAG_DEAD;
592                         if (intermission) refdefflags |= REFDEFFLAG_INTERMISSION;
593                         V_CalcRefdef(view, refdefflags); // TODO? uses .health stat in the engine when this isn't called here, may be broken!
594                 }
595                 else
596                 {
597                         CSQCPlayer_CalcRefdef(view);
598                 }
599                         
600         }
601         else
602         {
603                 // FIXME by CSQC spec we have to do this:
604                 // but it breaks chase cam
605                 /*
606                 setproperty(VF_ORIGIN, pmove_org + '0 0 1' * vh);
607                 setproperty(VF_ANGLES, view_angles);
608                 */
609         }
610         CSQCPLAYER_HOOK_POSTCAMERASETUP();
611 }
612
613 void CSQCPlayer_Remove(entity this)
614 {
615         csqcplayer = NULL;
616         cvar_settemp("cl_movement_replay", "1");
617 }
618
619 bool CSQCPlayer_PreUpdate(entity this)
620 {
621         if (this != csqcplayer) return false;
622         if (csqcplayer_status != CSQCPLAYERSTATUS_FROMSERVER) CSQCPlayer_Unpredict(this);
623         return true;
624 }
625
626 bool CSQCPlayer_PostUpdate(entity this)
627 {
628         if (this.entnum != player_localnum + 1) return false;
629         csqcplayer = this;
630         csqcplayer_status = CSQCPLAYERSTATUS_FROMSERVER;
631         cvar_settemp("cl_movement_replay", "0");
632         this.entremove = CSQCPlayer_Remove;
633         return true;
634 }