11 .float spectatorspeed;
17 When you press the jump key
20 void PlayerJump (void)
23 return; // no jumping in freezetag when frozen
25 if(self.player_blocked)
26 return; // no jumping while blocked
28 float doublejump = FALSE;
29 float mjumpheight = autocvar_sv_jumpvelocity;
31 player_multijump = doublejump;
32 player_jumpheight = mjumpheight;
33 if(MUTATOR_CALLHOOK(PlayerJump))
36 doublejump = player_multijump;
37 mjumpheight = player_jumpheight;
39 if (autocvar_sv_doublejump)
41 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
42 if (trace_fraction < 1 && trace_plane_normal_z > 0.7)
46 // we MUST clip velocity here!
48 f = self.velocity * trace_plane_normal;
50 self.velocity -= f * trace_plane_normal;
54 if (self.waterlevel >= WATERLEVEL_SWIMMING)
56 self.velocity_z = self.stat_sv_maxspeed * 0.7;
61 if (!(self.flags & FL_ONGROUND))
64 if(self.cvar_cl_movement_track_canjump)
65 if (!(self.flags & FL_JUMPRELEASED))
68 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
69 // velocity bounds. Final velocity is bound between (jumpheight *
70 // min + jumpheight) and (jumpheight * max + jumpheight);
72 if(autocvar_sv_jumpspeedcap_min != "")
76 minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
78 if (self.velocity_z < minjumpspeed)
79 mjumpheight += minjumpspeed - self.velocity_z;
82 if(autocvar_sv_jumpspeedcap_max != "")
84 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
85 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
87 if(!(trace_fraction < 1 && trace_plane_normal_z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
91 maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
93 if (self.velocity_z > maxjumpspeed)
94 mjumpheight -= self.velocity_z - maxjumpspeed;
98 if(!(self.lastflags & FL_ONGROUND))
100 if(autocvar_speedmeter)
101 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
102 if(self.lastground < time - 0.3)
104 self.velocity_x *= (1 - autocvar_sv_friction_on_land);
105 self.velocity_y *= (1 - autocvar_sv_friction_on_land);
107 if(self.jumppadcount > 1)
108 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
109 self.jumppadcount = 0;
112 self.velocity_z = self.velocity_z + mjumpheight;
113 self.oldvelocity_z = self.velocity_z;
115 self.flags &= ~FL_ONGROUND;
116 self.flags &= ~FL_JUMPRELEASED;
118 animdecide_setaction(self, ANIMACTION_JUMP, TRUE);
120 if(autocvar_g_jump_grunt)
121 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
123 self.restart_jump = -1; // restart jump anim next time
124 // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
126 void CheckWaterJump()
130 // check for a jump-out-of-water
131 makevectors (self.angles);
133 start_z = start_z + 8;
135 normalize(v_forward);
136 end = start + v_forward*24;
137 traceline (start, end, TRUE, self);
138 if (trace_fraction < 1)
140 start_z = start_z + self.maxs_z - 8;
141 end = start + v_forward*24;
142 self.movedir = trace_plane_normal * -50;
143 traceline (start, end, TRUE, self);
144 if (trace_fraction == 1)
145 { // open at eye level
146 self.flags |= FL_WATERJUMP;
147 self.velocity_z = 225;
148 self.flags &= ~FL_JUMPRELEASED;
149 self.teleport_time = time + 2; // safety net
154 void CheckPlayerJump()
156 if (self.BUTTON_JUMP)
159 self.flags |= FL_JUMPRELEASED;
161 if (self.waterlevel == WATERLEVEL_SWIMMING)
165 float racecar_angle(float forward, float down)
167 float ret, angle_mult;
175 ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
177 angle_mult = forward / (800 + forward);
180 return ret * angle_mult + 360 * (1 - angle_mult);
182 return ret * angle_mult;
185 void RaceCarPhysics()
187 // using this move type for "big rigs"
188 // the engine does not push the entity!
190 float accel, steer, f, myspeed, steerfactor;
191 vector angles_save, rigvel;
193 angles_save = self.angles;
194 accel = bound(-1, self.movement_x / self.stat_sv_maxspeed, 1);
195 steer = bound(-1, self.movement_y / self.stat_sv_maxspeed, 1);
197 if(g_bugrigs_reverse_speeding)
201 // back accel is DIGITAL
202 // to prevent speedhack
212 makevectors(self.angles); // new forward direction!
214 if(self.flags & FL_ONGROUND || g_bugrigs_air_steering)
216 float upspeed, accelfactor;
218 myspeed = self.velocity * v_forward;
219 upspeed = self.velocity * v_up;
221 // responsiveness factor for steering and acceleration
222 f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
223 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
225 if(myspeed < 0 && g_bugrigs_reverse_spinning)
226 steerfactor = -myspeed * g_bugrigs_steer;
228 steerfactor = -myspeed * f * g_bugrigs_steer;
230 if(myspeed < 0 && g_bugrigs_reverse_speeding)
231 accelfactor = g_bugrigs_accel;
233 accelfactor = f * g_bugrigs_accel;
234 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
240 myspeed = max(0, myspeed - frametime * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
244 if(!g_bugrigs_reverse_speeding)
245 myspeed = min(0, myspeed + frametime * g_bugrigs_friction_floor);
252 myspeed = max(0, myspeed - frametime * g_bugrigs_friction_floor);
256 if(g_bugrigs_reverse_stopping)
259 myspeed = min(0, myspeed + frametime * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
262 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
263 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
265 self.angles_y += steer * frametime * steerfactor; // apply steering
266 makevectors(self.angles); // new forward direction!
268 myspeed += accel * accelfactor * frametime;
270 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
274 myspeed = vlen(self.velocity);
276 // responsiveness factor for steering and acceleration
277 f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
278 steerfactor = -myspeed * f;
279 self.angles_y += steer * frametime * steerfactor; // apply steering
281 rigvel = self.velocity;
282 makevectors(self.angles); // new forward direction!
285 rigvel = rigvel * max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * frametime);
286 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
287 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
288 //MAXIMA: solve(total_acceleration(v) = 0, v);
290 if(g_bugrigs_planar_movement)
292 vector rigvel_xy, neworigin, up;
295 rigvel_z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
296 rigvel_xy = vec2(rigvel);
298 if(g_bugrigs_planar_movement_car_jumping)
301 mt = MOVE_NOMONSTERS;
303 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
304 up = trace_endpos - self.origin;
306 // BUG RIGS: align the move to the surface instead of doing collision testing
308 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
311 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel_z * frametime, mt, self);
313 if(trace_fraction < 0.5)
316 neworigin = self.origin;
319 neworigin = trace_endpos;
321 if(trace_fraction < 1)
323 // now set angles_x so that the car points parallel to the surface
324 self.angles = vectoangles(
325 '1 0 0' * v_forward_x * trace_plane_normal_z
327 '0 1 0' * v_forward_y * trace_plane_normal_z
329 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y)
331 self.flags |= FL_ONGROUND;
335 // now set angles_x so that the car points forward, but is tilted in velocity direction
336 self.flags &= ~FL_ONGROUND;
339 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
340 self.movetype = MOVETYPE_NOCLIP;
344 rigvel_z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
345 self.velocity = rigvel;
346 self.movetype = MOVETYPE_FLY;
350 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
351 if(trace_fraction != 1)
353 self.angles = vectoangles2(
354 '1 0 0' * v_forward_x * trace_plane_normal_z
356 '0 1 0' * v_forward_y * trace_plane_normal_z
358 '0 0 1' * -(v_forward_x * trace_plane_normal_x + v_forward_y * trace_plane_normal_y),
366 vel_local_x = v_forward * self.velocity;
367 vel_local_y = v_right * self.velocity;
368 vel_local_z = v_up * self.velocity;
370 self.angles_x = racecar_angle(vel_local_x, vel_local_z);
371 self.angles_z = racecar_angle(-vel_local_y, vel_local_z);
375 vector vf1, vu1, smoothangles;
376 makevectors(self.angles);
377 f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
382 makevectors(angles_save);
383 vf1 = vf1 + v_forward * (1 - f);
384 vu1 = vu1 + v_up * (1 - f);
385 smoothangles = vectoangles2(vf1, vu1);
386 self.angles_x = -smoothangles_x;
387 self.angles_z = smoothangles_z;
390 float IsMoveInDirection(vector mv, float angle) // key mix factor
392 if(mv_x == 0 && mv_y == 0)
393 return 0; // avoid division by zero
394 angle -= RAD2DEG * atan2(mv_y, mv_x);
395 angle = remainder(angle, 360) / 45;
400 return 1 - fabs(angle);
403 float GeomLerp(float a, float lerp, float b)
419 return a * pow(fabs(b / a), lerp);
422 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
424 float zspeed, xyspeed, dot, k;
427 // this doesn't play well with analog input
428 if(self.movement_x == 0 || self.movement_y != 0)
429 return; // can't control movement if not moving forward or backward
432 k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
437 k *= bound(0, wishspeed / autocvar_sv_maxairspeed, 1);
439 zspeed = self.velocity_z;
441 xyspeed = vlen(self.velocity); self.velocity = normalize(self.velocity);
443 dot = self.velocity * wishdir;
445 if(dot > 0) // we can't change direction while slowing down
447 k *= pow(dot, autocvar_sv_aircontrol_power)*frametime;
448 xyspeed = max(0, xyspeed - autocvar_sv_aircontrol_penalty * sqrt(max(0, 1 - dot*dot)) * k/32);
449 k *= autocvar_sv_aircontrol;
450 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
453 self.velocity = self.velocity * xyspeed;
454 self.velocity_z = zspeed;
457 float AdjustAirAccelQW(float accelqw, float factor)
459 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
462 // example config for alternate speed clamping:
463 // sv_airaccel_qw 0.8
464 // sv_airaccel_sideways_friction 0
465 // prvm_globalset server speedclamp_mode 1
467 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
475 float vel_xy_current;
476 float vel_xy_backward, vel_xy_forward;
479 if(stretchfactor > 0)
480 speedclamp = stretchfactor;
482 speedclamp = 1; // full clamping, no stretch
484 speedclamp = -1; // no clamping
489 if(autocvar_sv_gameplayfix_q2airaccelerate)
490 wishspeed0 = wishspeed;
492 vel_straight = self.velocity * wishdir;
493 vel_z = self.velocity_z;
494 vel_xy = vec2(self.velocity);
495 vel_perpend = vel_xy - vel_straight * wishdir;
497 step = accel * frametime * wishspeed0;
499 vel_xy_current = vlen(vel_xy);
501 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
502 vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
503 vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
504 if(vel_xy_backward < 0)
505 vel_xy_backward = 0; // not that it REALLY occurs that this would cause wrong behaviour afterwards
507 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
509 if(sidefric < 0 && (vel_perpend*vel_perpend))
510 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
513 f = max(0, 1 + frametime * wishspeed * sidefric);
514 fminimum = (vel_xy_backward*vel_xy_backward - vel_straight*vel_straight) / (vel_perpend*vel_perpend);
515 // this cannot be > 1
517 vel_perpend = vel_perpend * max(0, f);
520 fminimum = sqrt(fminimum);
521 vel_perpend = vel_perpend * max(fminimum, f);
525 vel_perpend = vel_perpend * max(0, 1 - frametime * wishspeed * sidefric);
527 vel_xy = vel_straight * wishdir + vel_perpend;
531 float vel_xy_preclamp;
532 vel_xy_preclamp = vlen(vel_xy);
533 if(vel_xy_preclamp > 0) // prevent division by zero
535 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
536 if(vel_xy_current < vel_xy_preclamp)
537 vel_xy = vel_xy * (vel_xy_current / vel_xy_preclamp);
541 self.velocity = vel_xy + vel_z * '0 0 1';
544 void PM_AirAccelerate(vector wishdir, float wishspeed)
546 vector curvel, wishvel, acceldir, curdir;
547 float addspeed, accelspeed, curspeed, f;
553 curvel = self.velocity;
555 curspeed = vlen(curvel);
557 if(wishspeed > curspeed * 1.01)
559 wishspeed = min(wishspeed, curspeed + autocvar_sv_warsowbunny_airforwardaccel * self.stat_sv_maxspeed * frametime);
563 f = max(0, (autocvar_sv_warsowbunny_topspeed - curspeed) / (autocvar_sv_warsowbunny_topspeed - self.stat_sv_maxspeed));
564 wishspeed = max(curspeed, self.stat_sv_maxspeed) + autocvar_sv_warsowbunny_accel * f * self.stat_sv_maxspeed * frametime;
566 wishvel = wishdir * wishspeed;
567 acceldir = wishvel - curvel;
568 addspeed = vlen(acceldir);
569 acceldir = normalize(acceldir);
571 accelspeed = min(addspeed, autocvar_sv_warsowbunny_turnaccel * self.stat_sv_maxspeed * frametime);
573 if(autocvar_sv_warsowbunny_backtosideratio < 1)
575 curdir = normalize(curvel);
576 dot = acceldir * curdir;
578 acceldir = acceldir - (1 - autocvar_sv_warsowbunny_backtosideratio) * dot * curdir;
581 self.velocity += accelspeed * acceldir;
584 .vector movement_old;
587 .string lastclassname;
589 .float() PlayerPhysplug;
591 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
592 .float specialcommand_pos;
593 void SpecialCommand()
598 if(!CheatImpulse(99))
599 print("A hollow voice says \"Plugh\".\n");
603 float speedaward_speed;
604 string speedaward_holder;
605 string speedaward_uid;
606 void race_send_speedaward(float msg)
608 // send the best speed of the round
609 WriteByte(msg, SVC_TEMPENTITY);
610 WriteByte(msg, TE_CSQC_RACE);
611 WriteByte(msg, RACE_NET_SPEED_AWARD);
612 WriteInt24_t(msg, floor(speedaward_speed+0.5));
613 WriteString(msg, speedaward_holder);
616 float speedaward_alltimebest;
617 string speedaward_alltimebest_holder;
618 string speedaward_alltimebest_uid;
619 void race_send_speedaward_alltimebest(float msg)
621 // send the best speed
622 WriteByte(msg, SVC_TEMPENTITY);
623 WriteByte(msg, TE_CSQC_RACE);
624 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
625 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
626 WriteString(msg, speedaward_alltimebest_holder);
629 string GetMapname(void);
630 float speedaward_lastupdate;
631 float speedaward_lastsent;
632 void SV_PlayerPhysics()
634 vector wishvel, wishdir, v;
635 float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, buttons;
638 float not_allowed_to_move;
641 WarpZone_PlayerPhysics_FixVAngle();
646 maxspd_mod *= autocvar_g_keepaway_ballcarrier_highspeed;
648 maxspd_mod *= autocvar_g_movement_highspeed;
650 // fix physics stats for g_movement_highspeed
651 // TODO maybe rather use maxairspeed? needs testing
652 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
653 if(autocvar_sv_airstrafeaccel_qw)
654 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
656 self.stat_sv_airstrafeaccel_qw = 0;
657 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
658 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
660 if(self.PlayerPhysplug)
661 if(self.PlayerPhysplug())
664 self.race_movetime_frac += frametime;
665 f = floor(self.race_movetime_frac);
666 self.race_movetime_frac -= f;
667 self.race_movetime_count += f;
668 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
672 buttons = self.BUTTON_ATCK + 2 * self.BUTTON_JUMP + 4 * self.BUTTON_ATCK2 + 8 * self.BUTTON_ZOOM + 16 * self.BUTTON_CROUCH + 32 * self.BUTTON_HOOK + 64 * self.BUTTON_USE + 128 * (self.movement_x < 0) + 256 * (self.movement_x > 0) + 512 * (self.movement_y < 0) + 1024 * (self.movement_y > 0);
676 else if(buttons == 1)
678 else if(buttons == 2)
680 else if(buttons == 128)
682 else if(buttons == 256)
684 else if(buttons == 512)
686 else if(buttons == 1024)
691 if(c == substring(specialcommand, self.specialcommand_pos, 1))
693 self.specialcommand_pos += 1;
694 if(self.specialcommand_pos >= strlen(specialcommand))
696 self.specialcommand_pos = 0;
701 else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
702 self.specialcommand_pos = 0;
706 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
707 self.parm_idlesince = time;
709 buttons_prev = self.buttons_old;
710 self.buttons_old = buttons;
711 self.movement_old = self.movement;
712 self.v_angle_old = self.v_angle;
714 if(time < self.nickspamtime)
715 if(self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
717 // slight annoyance for nick change scripts
718 self.movement = -1 * self.movement;
719 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
721 if(self.nickspamcount >= autocvar_g_nick_flood_penalty_red) // if you are persistent and the slight annoyance above does not stop you, I'll show you!
723 self.angles_x = random() * 360;
724 self.angles_y = random() * 360;
725 // at least I'm not forcing retardedview by also assigning to angles_z
726 self.fixangle = TRUE;
730 if (self.punchangle != '0 0 0')
732 f = vlen(self.punchangle) - 10 * frametime;
734 self.punchangle = normalize(self.punchangle) * f;
736 self.punchangle = '0 0 0';
739 if (self.punchvector != '0 0 0')
741 f = vlen(self.punchvector) - 30 * frametime;
743 self.punchvector = normalize(self.punchvector) * f;
745 self.punchvector = '0 0 0';
748 if (IS_BOT_CLIENT(self))
750 if(playerdemo_read())
755 self.items &= ~IT_USING_JETPACK;
759 if(self.race_penalty)
760 if(time > self.race_penalty)
761 self.race_penalty = 0;
763 not_allowed_to_move = 0;
764 if(self.race_penalty)
765 not_allowed_to_move = 1;
766 if(!autocvar_sv_ready_restart_after_countdown)
767 if(time < game_starttime)
768 not_allowed_to_move = 1;
770 if(not_allowed_to_move)
772 self.velocity = '0 0 0';
773 self.movetype = MOVETYPE_NONE;
774 self.disableclientprediction = 2;
776 else if(self.disableclientprediction == 2)
778 if(self.movetype == MOVETYPE_NONE)
779 self.movetype = MOVETYPE_WALK;
780 self.disableclientprediction = 0;
784 if (self.movetype == MOVETYPE_NONE)
787 // when we get here, disableclientprediction cannot be 2
788 self.disableclientprediction = 0;
789 if(time < self.ladder_time)
790 self.disableclientprediction = 1;
792 if(time < self.spider_slowness)
794 self.stat_sv_maxspeed *= 0.5; // half speed while slow from spider
795 self.stat_sv_airspeedlimit_nonqw *= 0.5;
800 if(autocvar_sv_dodging_frozen && IS_REAL_CLIENT(self))
802 self.movement_x = bound(-5, self.movement_x, 5);
803 self.movement_y = bound(-5, self.movement_y, 5);
804 self.movement_z = bound(-5, self.movement_z, 5);
807 self.movement = '0 0 0';
808 self.disableclientprediction = 1;
810 vector midpoint = ((self.absmin + self.absmax) * 0.5);
811 if(pointcontents(midpoint) == CONTENT_WATER)
813 self.velocity = self.velocity * 0.5;
815 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
816 { self.velocity_z = 200; }
820 MUTATOR_CALLHOOK(PlayerPhysics);
822 if(self.player_blocked)
824 self.movement = '0 0 0';
825 self.disableclientprediction = 1;
832 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
835 // conveyors: first fix velocity
836 if(self.conveyor.state)
837 self.velocity -= self.conveyor.movedir;
839 if (!IS_PLAYER(self))
841 maxspd_mod = autocvar_sv_spectator_speed_multiplier;
842 if(!self.spectatorspeed)
843 self.spectatorspeed = maxspd_mod;
844 if(self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
846 if(self.lastclassname != "player")
848 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
849 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
850 else if(self.impulse == 11)
851 self.spectatorspeed = maxspd_mod;
852 else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
853 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
854 else if(self.impulse >= 1 && self.impulse <= 9)
855 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
856 } // otherwise just clear
859 maxspd_mod = self.spectatorspeed;
862 spd = max(self.stat_sv_maxspeed, autocvar_sv_maxairspeed) * maxspd_mod * swampspd_mod;
863 if(self.speed != spd)
867 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
868 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
869 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
870 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
873 maxspd_mod *= swampspd_mod; // only one common speed modder please!
876 // if dead, behave differently
880 if (!self.fixangle && !g_bugrigs)
883 self.angles_y = self.v_angle_y;
887 if(self.flags & FL_ONGROUND)
888 if(IS_PLAYER(self)) // no fall sounds for observers thank you very much
893 if(self.waterlevel < WATERLEVEL_SWIMMING)
894 if(time >= self.ladder_time)
897 self.nextstep = time + 0.3 + random() * 0.1;
898 trace_dphitq3surfaceflags = 0;
899 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
900 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
902 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
903 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
905 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
916 if (self.flags & FL_WATERJUMP )
918 self.velocity_x = self.movedir_x;
919 self.velocity_y = self.movedir_y;
920 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
922 self.flags &= ~FL_WATERJUMP;
923 self.teleport_time = 0;
926 else if (g_bugrigs && IS_PLAYER(self))
930 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
932 // noclipping or flying
933 self.flags &= ~FL_ONGROUND;
935 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
936 makevectors(self.v_angle);
937 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
938 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
940 wishdir = normalize(wishvel);
941 wishspeed = vlen(wishvel);
942 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
943 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
944 if (time >= self.teleport_time)
945 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
947 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
950 self.flags &= ~FL_ONGROUND;
952 makevectors(self.v_angle);
953 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
954 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
955 if (wishvel == '0 0 0')
956 wishvel = '0 0 -60'; // drift towards bottom
958 wishdir = normalize(wishvel);
959 wishspeed = vlen(wishvel);
960 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
961 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
962 wishspeed = wishspeed * 0.7;
965 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
967 // water acceleration
968 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
970 else if (time < self.ladder_time)
972 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
973 self.flags &= ~FL_ONGROUND;
976 g = autocvar_sv_gravity * frametime;
979 if(autocvar_sv_gameplayfix_gravityunaffectedbyticrate)
982 self.velocity_z += g;
985 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
986 makevectors(self.v_angle);
987 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
988 wishvel = v_forward * self.movement_x + v_right * self.movement_y + '0 0 1' * self.movement_z;
989 self.velocity_z += g;
990 if (self.ladder_entity.classname == "func_water")
993 if (f > self.ladder_entity.speed)
994 wishvel = wishvel * (self.ladder_entity.speed / f);
996 self.watertype = self.ladder_entity.skin;
997 f = self.ladder_entity.origin_z + self.ladder_entity.maxs_z;
998 if ((self.origin_z + self.view_ofs_z) < f)
999 self.waterlevel = WATERLEVEL_SUBMERGED;
1000 else if ((self.origin_z + (self.mins_z + self.maxs_z) * 0.5) < f)
1001 self.waterlevel = WATERLEVEL_SWIMMING;
1002 else if ((self.origin_z + self.mins_z + 1) < f)
1003 self.waterlevel = WATERLEVEL_WETFEET;
1006 self.waterlevel = WATERLEVEL_NONE;
1007 self.watertype = CONTENT_EMPTY;
1011 wishdir = normalize(wishvel);
1012 wishspeed = vlen(wishvel);
1013 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1014 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1015 if (time >= self.teleport_time)
1017 // water acceleration
1018 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1021 else if ((self.items & IT_JETPACK) && self.BUTTON_HOOK && (!autocvar_g_jetpack_fuel || self.ammo_fuel >= 0.01 || self.items & IT_UNLIMITED_WEAPON_AMMO) && !self.frozen)
1023 //makevectors(self.v_angle_y * '0 1 0');
1024 makevectors(self.v_angle);
1025 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1026 // add remaining speed as Z component
1027 maxairspd = autocvar_sv_maxairspeed*max(1, maxspd_mod);
1028 // fix speedhacks :P
1029 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1030 // add the unused velocity as up component
1033 // if(self.BUTTON_JUMP)
1034 wishvel_z = sqrt(max(0, 1 - wishvel * wishvel));
1036 // it is now normalized, so...
1037 float a_side, a_up, a_add, a_diff;
1038 a_side = autocvar_g_jetpack_acceleration_side;
1039 a_up = autocvar_g_jetpack_acceleration_up;
1040 a_add = autocvar_g_jetpack_antigravity * autocvar_sv_gravity;
1042 wishvel_x *= a_side;
1043 wishvel_y *= a_side;
1049 //////////////////////////////////////////////////////////////////////////////////////
1050 // finding the maximum over all vectors of above form
1051 // with wishvel having an absolute value of 1
1052 //////////////////////////////////////////////////////////////////////////////////////
1053 // we're finding the maximum over
1054 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1055 // for z in the range from -1 to 1
1056 //////////////////////////////////////////////////////////////////////////////////////
1057 // maximum is EITHER attained at the single extreme point:
1058 a_diff = a_side * a_side - a_up * a_up;
1061 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1062 if(f > -1 && f < 1) // can it be attained?
1064 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1065 //print("middle\n");
1068 // OR attained at z = 1:
1069 f = (a_up + a_add) * (a_up + a_add);
1075 // OR attained at z = -1:
1076 f = (a_up - a_add) * (a_up - a_add);
1080 //print("bottom\n");
1083 //////////////////////////////////////////////////////////////////////////////////////
1085 //print("best possible acceleration: ", ftos(best), "\n");
1088 fxy = bound(0, 1 - (self.velocity * normalize(wishvel_x * '1 0 0' + wishvel_y * '0 1 0')) / autocvar_g_jetpack_maxspeed_side, 1);
1089 if(wishvel_z - autocvar_sv_gravity > 0)
1090 fz = bound(0, 1 - self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1092 fz = bound(0, 1 + self.velocity_z / autocvar_g_jetpack_maxspeed_up, 1);
1096 wishvel_z = (wishvel_z - autocvar_sv_gravity) * fz + autocvar_sv_gravity;
1099 fvel = min(1, vlen(wishvel) / best);
1100 if(autocvar_g_jetpack_fuel && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1101 f = min(1, self.ammo_fuel / (autocvar_g_jetpack_fuel * frametime * fvel));
1105 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1107 if (f > 0 && wishvel != '0 0 0')
1109 self.velocity = self.velocity + wishvel * f * frametime;
1110 if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
1111 self.ammo_fuel -= autocvar_g_jetpack_fuel * frametime * fvel * f;
1112 self.flags &= ~FL_ONGROUND;
1113 self.items |= IT_USING_JETPACK;
1115 // jetpack also inhibits health regeneration, but only for 1 second
1116 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1119 else if (self.flags & FL_ONGROUND)
1121 // we get here if we ran out of ammo
1122 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1123 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1126 makevectors(self.v_angle_y * '0 1 0');
1127 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1129 if(!(self.lastflags & FL_ONGROUND))
1131 if(autocvar_speedmeter)
1132 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1133 if(self.lastground < time - 0.3)
1134 self.velocity = self.velocity * (1 - autocvar_sv_friction_on_land);
1135 if(self.jumppadcount > 1)
1136 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1137 self.jumppadcount = 0;
1145 if (f < autocvar_sv_stopspeed)
1146 f = 1 - frametime * (autocvar_sv_stopspeed / f) * autocvar_sv_friction;
1148 f = 1 - frametime * autocvar_sv_friction;
1150 self.velocity = self.velocity * f;
1152 self.velocity = '0 0 0';
1154 Mathematical analysis time!
1156 Our goal is to invert this mess.
1158 For the two cases we get:
1159 v = v0 * (1 - frametime * (autocvar_sv_stopspeed / v0) * autocvar_sv_friction)
1160 = v0 - frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1161 v0 = v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1163 v = v0 * (1 - frametime * autocvar_sv_friction)
1164 v0 = v / (1 - frametime * autocvar_sv_friction)
1166 These cases would be chosen ONLY if:
1167 v0 < autocvar_sv_stopspeed
1168 v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction < autocvar_sv_stopspeed
1169 v < autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1171 v0 >= autocvar_sv_stopspeed
1172 v / (1 - frametime * autocvar_sv_friction) >= autocvar_sv_stopspeed
1173 v >= autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1178 wishdir = normalize(wishvel);
1179 wishspeed = vlen(wishvel);
1180 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1181 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1183 wishspeed = wishspeed * 0.5;
1184 if (time >= self.teleport_time)
1185 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1190 // we get here if we ran out of ammo
1191 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1192 sprint(self, "You don't have any fuel for the ^2Jetpack\n");
1196 maxairspd = autocvar_sv_maxairspeed*maxspd_mod;
1197 airaccel = autocvar_sv_airaccelerate*maxspd_mod;
1201 maxairspd = autocvar_sv_maxairspeed;
1202 airaccel = autocvar_sv_airaccelerate;
1205 makevectors(self.v_angle_y * '0 1 0');
1206 wishvel = v_forward * self.movement_x + v_right * self.movement_y;
1208 wishdir = normalize(wishvel);
1209 wishspeed = wishspeed0 = vlen(wishvel);
1210 if (wishspeed0 > self.stat_sv_maxspeed*maxspd_mod)
1211 wishspeed0 = self.stat_sv_maxspeed*maxspd_mod;
1212 if (wishspeed > maxairspd)
1213 wishspeed = maxairspd;
1215 wishspeed = wishspeed * 0.5;
1216 if (time >= self.teleport_time)
1223 airaccelqw = self.stat_sv_airaccel_qw;
1224 accelerating = (self.velocity * wishdir > 0);
1225 wishspeed2 = wishspeed;
1228 if(autocvar_sv_airstopaccelerate)
1231 curdir = self.velocity;
1233 curdir = normalize(curdir);
1234 airaccel = airaccel + (autocvar_sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1236 // note that for straight forward jumping:
1237 // step = accel * frametime * wishspeed0;
1238 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1240 // dv/dt = accel * maxspeed (when slow)
1241 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1242 // log dv/dt = logaccel + logmaxspeed (when slow)
1243 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1244 strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1245 if(autocvar_sv_maxairstrafespeed)
1246 wishspeed = min(wishspeed, GeomLerp(autocvar_sv_maxairspeed*maxspd_mod, strafity, autocvar_sv_maxairstrafespeed*maxspd_mod));
1247 if(autocvar_sv_airstrafeaccelerate)
1248 airaccel = GeomLerp(airaccel, strafity, autocvar_sv_airstrafeaccelerate*maxspd_mod);
1249 if(self.stat_sv_airstrafeaccel_qw)
1250 airaccelqw = copysign(1-GeomLerp(1-fabs(self.stat_sv_airaccel_qw), strafity, 1-fabs(self.stat_sv_airstrafeaccel_qw)), ((strafity > 0.5) ? self.stat_sv_airstrafeaccel_qw : self.stat_sv_airaccel_qw));
1253 if(autocvar_sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement_x != 0)
1254 PM_AirAccelerate(wishdir, wishspeed);
1256 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, autocvar_sv_airaccel_qw_stretchfactor, autocvar_sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1258 if(autocvar_sv_aircontrol)
1259 CPM_PM_Aircontrol(wishdir, wishspeed2);
1263 if((g_cts || g_race) && !IS_OBSERVER(self))
1265 if(vlen(self.velocity - self.velocity_z * '0 0 1') > speedaward_speed)
1267 speedaward_speed = vlen(self.velocity - self.velocity_z * '0 0 1');
1268 speedaward_holder = self.netname;
1269 speedaward_uid = self.crypto_idfp;
1270 speedaward_lastupdate = time;
1272 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1274 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1275 race_send_speedaward(MSG_ALL);
1276 speedaward_lastsent = speedaward_speed;
1277 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1279 speedaward_alltimebest = speedaward_speed;
1280 speedaward_alltimebest_holder = speedaward_holder;
1281 speedaward_alltimebest_uid = speedaward_uid;
1282 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1283 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1284 race_send_speedaward_alltimebest(MSG_ALL);
1291 xyspeed = vlen('1 0 0' * self.velocity_x + '0 1 0' * self.velocity_y);
1292 if(self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1294 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1295 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1296 f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1297 // add the extra charge
1298 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * frametime);
1301 if(self.flags & FL_ONGROUND)
1302 self.lastground = time;
1304 // conveyors: then break velocity again
1305 if(self.conveyor.state)
1306 self.velocity += self.conveyor.movedir;
1308 self.lastflags = self.flags;
1309 self.lastclassname = self.classname;