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