4 #include "../dpdefs/progsdefs.qh"
5 #include "../dpdefs/dpextensions.qh"
6 #include "../warpzonelib/mathlib.qh"
7 #include "../warpzonelib/server.qh"
8 #include "../common/constants.qh"
9 #include "../common/util.qh"
10 #include "../common/animdecide.qh"
11 #include "../common/monsters/sv_monsters.qh"
12 #include "../common/weapons/weapons.qh"
14 #include "autocvars.qh"
16 #include "../common/notifications.qh"
17 #include "mutators/mutators_include.qh"
18 #include "../common/mapinfo.qh"
19 #include "../csqcmodellib/sv_model.qh"
20 #include "anticheat.qh"
24 #include "playerdemo.qh"
31 .entity ladder_entity;
33 .float swamp_slowdown;
37 .float spectatorspeed;
43 When you press the jump key
44 returns true if handled
47 float PlayerJump (void)
50 return true; // no jumping in freezetag when frozen
52 if(self.player_blocked)
53 return true; // no jumping while blocked
55 float doublejump = false;
56 float mjumpheight = autocvar_sv_jumpvelocity;
58 player_multijump = doublejump;
59 player_jumpheight = mjumpheight;
60 if(MUTATOR_CALLHOOK(PlayerJump))
63 doublejump = player_multijump;
64 mjumpheight = player_jumpheight;
66 if (autocvar_sv_doublejump)
68 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
69 if (trace_fraction < 1 && trace_plane_normal.z > 0.7)
73 // we MUST clip velocity here!
75 f = self.velocity * trace_plane_normal;
77 self.velocity -= f * trace_plane_normal;
81 if (self.waterlevel >= WATERLEVEL_SWIMMING)
83 self.velocity_z = self.stat_sv_maxspeed * 0.7;
88 if (!(self.flags & FL_ONGROUND))
89 return !(self.flags & FL_JUMPRELEASED);
91 if(self.cvar_cl_movement_track_canjump)
92 if (!(self.flags & FL_JUMPRELEASED))
95 // sv_jumpspeedcap_min/sv_jumpspeedcap_max act as baseline
96 // velocity bounds. Final velocity is bound between (jumpheight *
97 // min + jumpheight) and (jumpheight * max + jumpheight);
99 if(autocvar_sv_jumpspeedcap_min != "")
103 minjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_min);
105 if (self.velocity.z < minjumpspeed)
106 mjumpheight += minjumpspeed - self.velocity.z;
109 if(autocvar_sv_jumpspeedcap_max != "")
111 // don't do jump speedcaps on ramps to preserve old xonotic ramjump style
112 tracebox(self.origin + '0 0 0.01', self.mins, self.maxs, self.origin - '0 0 0.01', MOVE_NORMAL, self);
114 if(!(trace_fraction < 1 && trace_plane_normal.z < 0.98 && autocvar_sv_jumpspeedcap_max_disable_on_ramps))
118 maxjumpspeed = mjumpheight * stof(autocvar_sv_jumpspeedcap_max);
120 if (self.velocity.z > maxjumpspeed)
121 mjumpheight -= self.velocity.z - maxjumpspeed;
125 if(!(self.lastflags & FL_ONGROUND))
127 if(autocvar_speedmeter)
128 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
129 if(self.lastground < time - 0.3)
131 self.velocity_x *= (1 - autocvar_sv_friction_on_land);
132 self.velocity_y *= (1 - autocvar_sv_friction_on_land);
134 if(self.jumppadcount > 1)
135 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
136 self.jumppadcount = 0;
139 self.velocity_z = self.velocity.z + mjumpheight;
140 self.oldvelocity_z = self.velocity.z;
142 self.flags &= ~FL_ONGROUND;
143 self.flags &= ~FL_JUMPRELEASED;
145 animdecide_setaction(self, ANIMACTION_JUMP, true);
147 if(autocvar_g_jump_grunt)
148 PlayerSound(playersound_jump, CH_PLAYER, VOICETYPE_PLAYERSOUND);
150 self.restart_jump = -1; // restart jump anim next time
151 // value -1 is used to not use the teleport bit (workaround for tiny hitch when re-jumping)
154 void CheckWaterJump()
158 // check for a jump-out-of-water
159 makevectors (self.angles);
161 start.z = start.z + 8;
163 normalize(v_forward);
164 end = start + v_forward*24;
165 traceline (start, end, true, self);
166 if (trace_fraction < 1)
168 start.z = start.z + self.maxs.z - 8;
169 end = start + v_forward*24;
170 self.movedir = trace_plane_normal * -50;
171 traceline (start, end, true, self);
172 if (trace_fraction == 1)
173 { // open at eye level
174 self.flags |= FL_WATERJUMP;
175 self.velocity_z = 225;
176 self.flags &= ~FL_JUMPRELEASED;
177 self.teleport_time = time + 2; // safety net
183 .float jetpack_stopped;
184 // Hack: shouldn't need to know about this
185 .float multijump_count;
186 void CheckPlayerJump()
188 float was_flying = self.items & IT_USING_JETPACK;
190 if (self.cvar_cl_jetpack_jump < 2)
191 self.items &= ~IT_USING_JETPACK;
193 if (self.BUTTON_JUMP || self.BUTTON_JETPACK)
195 float air_jump = !PlayerJump() || self.multijump_count > 0; // PlayerJump() has important side effects
196 float activate = self.cvar_cl_jetpack_jump && air_jump && self.BUTTON_JUMP || self.BUTTON_JETPACK;
197 float has_fuel = !autocvar_g_jetpack_fuel || self.ammo_fuel || self.items & IT_UNLIMITED_WEAPON_AMMO;
198 if (!(self.items & IT_JETPACK)) { }
199 else if (self.jetpack_stopped) { }
202 if (was_flying) // TODO: ran out of fuel message
203 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
205 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
206 self.jetpack_stopped = true;
207 self.items &= ~IT_USING_JETPACK;
209 else if (activate && !self.frozen)
210 self.items |= IT_USING_JETPACK;
214 self.jetpack_stopped = false;
215 self.items &= ~IT_USING_JETPACK;
217 if (!self.BUTTON_JUMP)
218 self.flags |= FL_JUMPRELEASED;
220 if (self.waterlevel == WATERLEVEL_SWIMMING)
224 float racecar_angle(float forward, float down)
226 float ret, angle_mult;
234 ret = vectoyaw('0 1 0' * down + '1 0 0' * forward);
236 angle_mult = forward / (800 + forward);
239 return ret * angle_mult + 360 * (1 - angle_mult);
241 return ret * angle_mult;
244 void RaceCarPhysics()
246 // using this move type for "big rigs"
247 // the engine does not push the entity!
249 float accel, steer, f, myspeed, steerfactor;
250 vector angles_save, rigvel;
252 angles_save = self.angles;
253 accel = bound(-1, self.movement.x / self.stat_sv_maxspeed, 1);
254 steer = bound(-1, self.movement.y / self.stat_sv_maxspeed, 1);
256 if(g_bugrigs_reverse_speeding)
260 // back accel is DIGITAL
261 // to prevent speedhack
271 makevectors(self.angles); // new forward direction!
273 if(self.flags & FL_ONGROUND || g_bugrigs_air_steering)
275 float upspeed, accelfactor;
277 myspeed = self.velocity * v_forward;
278 upspeed = self.velocity * v_up;
280 // responsiveness factor for steering and acceleration
281 f = 1 / (1 + pow(max(-myspeed, myspeed) / g_bugrigs_speed_ref, g_bugrigs_speed_pow));
282 //MAXIMA: f(v) := 1 / (1 + (v / g_bugrigs_speed_ref) ^ g_bugrigs_speed_pow);
284 if(myspeed < 0 && g_bugrigs_reverse_spinning)
285 steerfactor = -myspeed * g_bugrigs_steer;
287 steerfactor = -myspeed * f * g_bugrigs_steer;
289 if(myspeed < 0 && g_bugrigs_reverse_speeding)
290 accelfactor = g_bugrigs_accel;
292 accelfactor = f * g_bugrigs_accel;
293 //MAXIMA: accel(v) := f(v) * g_bugrigs_accel;
299 myspeed = max(0, myspeed - frametime * (g_bugrigs_friction_floor - g_bugrigs_friction_brake * accel));
303 if(!g_bugrigs_reverse_speeding)
304 myspeed = min(0, myspeed + frametime * g_bugrigs_friction_floor);
311 myspeed = max(0, myspeed - frametime * g_bugrigs_friction_floor);
315 if(g_bugrigs_reverse_stopping)
318 myspeed = min(0, myspeed + frametime * (g_bugrigs_friction_floor + g_bugrigs_friction_brake * accel));
321 // terminal velocity = velocity at which 50 == accelfactor, that is, 1549 units/sec
322 //MAXIMA: friction(v) := g_bugrigs_friction_floor;
324 self.angles_y += steer * frametime * steerfactor; // apply steering
325 makevectors(self.angles); // new forward direction!
327 myspeed += accel * accelfactor * frametime;
329 rigvel = myspeed * v_forward + '0 0 1' * upspeed;
333 myspeed = vlen(self.velocity);
335 // responsiveness factor for steering and acceleration
336 f = 1 / (1 + pow(max(0, myspeed / g_bugrigs_speed_ref), g_bugrigs_speed_pow));
337 steerfactor = -myspeed * f;
338 self.angles_y += steer * frametime * steerfactor; // apply steering
340 rigvel = self.velocity;
341 makevectors(self.angles); // new forward direction!
344 rigvel = rigvel * max(0, 1 - vlen(rigvel) * g_bugrigs_friction_air * frametime);
345 //MAXIMA: airfriction(v) := v * v * g_bugrigs_friction_air;
346 //MAXIMA: total_acceleration(v) := accel(v) - friction(v) - airfriction(v);
347 //MAXIMA: solve(total_acceleration(v) = 0, v);
349 if(g_bugrigs_planar_movement)
351 vector rigvel_xy, neworigin, up;
354 rigvel.z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
355 rigvel_xy = vec2(rigvel);
357 if(g_bugrigs_planar_movement_car_jumping)
360 mt = MOVE_NOMONSTERS;
362 tracebox(self.origin, self.mins, self.maxs, self.origin + '0 0 1024', mt, self);
363 up = trace_endpos - self.origin;
365 // BUG RIGS: align the move to the surface instead of doing collision testing
367 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos + rigvel_xy * frametime, mt, self);
370 tracebox(trace_endpos, self.mins, self.maxs, trace_endpos - up + '0 0 1' * rigvel.z * frametime, mt, self);
372 if(trace_fraction < 0.5)
375 neworigin = self.origin;
378 neworigin = trace_endpos;
380 if(trace_fraction < 1)
382 // now set angles_x so that the car points parallel to the surface
383 self.angles = vectoangles(
384 '1 0 0' * v_forward.x * trace_plane_normal.z
386 '0 1 0' * v_forward.y * trace_plane_normal.z
388 '0 0 1' * -(v_forward.x * trace_plane_normal.x + v_forward.y * trace_plane_normal.y)
390 self.flags |= FL_ONGROUND;
394 // now set angles_x so that the car points forward, but is tilted in velocity direction
395 self.flags &= ~FL_ONGROUND;
398 self.velocity = (neworigin - self.origin) * (1.0 / frametime);
399 self.movetype = MOVETYPE_NOCLIP;
403 rigvel.z -= frametime * autocvar_sv_gravity; // 4x gravity plays better
404 self.velocity = rigvel;
405 self.movetype = MOVETYPE_FLY;
409 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 4', MOVE_NORMAL, self);
410 if(trace_fraction != 1)
412 self.angles = vectoangles2(
413 '1 0 0' * v_forward.x * trace_plane_normal.z
415 '0 1 0' * v_forward.y * trace_plane_normal.z
417 '0 0 1' * -(v_forward.x * trace_plane_normal.x + v_forward.y * trace_plane_normal.y),
425 vel_local.x = v_forward * self.velocity;
426 vel_local.y = v_right * self.velocity;
427 vel_local.z = v_up * self.velocity;
429 self.angles_x = racecar_angle(vel_local.x, vel_local.z);
430 self.angles_z = racecar_angle(-vel_local.y, vel_local.z);
434 vector vf1, vu1, smoothangles;
435 makevectors(self.angles);
436 f = bound(0, frametime * g_bugrigs_angle_smoothing, 1);
441 makevectors(angles_save);
442 vf1 = vf1 + v_forward * (1 - f);
443 vu1 = vu1 + v_up * (1 - f);
444 smoothangles = vectoangles2(vf1, vu1);
445 self.angles_x = -smoothangles.x;
446 self.angles_z = smoothangles.z;
449 float IsMoveInDirection(vector mv, float angle) // key mix factor
451 if(mv.x == 0 && mv.y == 0)
452 return 0; // avoid division by zero
453 angle -= RAD2DEG * atan2(mv.y, mv.x);
454 angle = remainder(angle, 360) / 45;
459 return 1 - fabs(angle);
462 float GeomLerp(float a, float lerp, float b)
478 return a * pow(fabs(b / a), lerp);
481 void CPM_PM_Aircontrol(vector wishdir, float wishspeed)
483 float zspeed, xyspeed, dot, k;
486 // this doesn't play well with analog input
487 if(self.movement_x == 0 || self.movement.y != 0)
488 return; // can't control movement if not moving forward or backward
491 k = 32 * (2 * IsMoveInDirection(self.movement, 0) - 1);
496 k *= bound(0, wishspeed / autocvar_sv_maxairspeed, 1);
498 zspeed = self.velocity.z;
500 xyspeed = vlen(self.velocity); self.velocity = normalize(self.velocity);
502 dot = self.velocity * wishdir;
504 if(dot > 0) // we can't change direction while slowing down
506 k *= pow(dot, autocvar_sv_aircontrol_power)*frametime;
507 xyspeed = max(0, xyspeed - autocvar_sv_aircontrol_penalty * sqrt(max(0, 1 - dot*dot)) * k/32);
508 k *= autocvar_sv_aircontrol;
509 self.velocity = normalize(self.velocity * xyspeed + wishdir * k);
512 self.velocity = self.velocity * xyspeed;
513 self.velocity_z = zspeed;
516 float AdjustAirAccelQW(float accelqw, float factor)
518 return copysign(bound(0.000001, 1 - (1 - fabs(accelqw)) * factor, 1), accelqw);
521 // example config for alternate speed clamping:
522 // sv_airaccel_qw 0.8
523 // sv_airaccel_sideways_friction 0
524 // prvm_globalset server speedclamp_mode 1
526 void PM_Accelerate(vector wishdir, float wishspeed, float wishspeed0, float accel, float accelqw, float stretchfactor, float sidefric, float speedlimit)
534 float vel_xy_current;
535 float vel_xy_backward, vel_xy_forward;
538 if(stretchfactor > 0)
539 speedclamp = stretchfactor;
541 speedclamp = 1; // full clamping, no stretch
543 speedclamp = -1; // no clamping
548 if(autocvar_sv_gameplayfix_q2airaccelerate)
549 wishspeed0 = wishspeed;
551 vel_straight = self.velocity * wishdir;
552 velZ = self.velocity.z;
553 vel_xy = vec2(self.velocity);
554 vel_perpend = vel_xy - vel_straight * wishdir;
556 step = accel * frametime * wishspeed0;
558 vel_xy_current = vlen(vel_xy);
560 accelqw = AdjustAirAccelQW(accelqw, (speedlimit - bound(wishspeed, vel_xy_current, speedlimit)) / max(1, speedlimit - wishspeed));
561 vel_xy_forward = vel_xy_current + bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
562 vel_xy_backward = vel_xy_current - bound(0, wishspeed + vel_xy_current, step) * accelqw - step * (1 - accelqw);
563 if(vel_xy_backward < 0)
564 vel_xy_backward = 0; // not that it REALLY occurs that this would cause wrong behaviour afterwards
566 vel_straight = vel_straight + bound(0, wishspeed - vel_straight, step) * accelqw + step * (1 - accelqw);
568 if(sidefric < 0 && (vel_perpend*vel_perpend))
569 // negative: only apply so much sideways friction to stay below the speed you could get by "braking"
572 f = max(0, 1 + frametime * wishspeed * sidefric);
573 fminimum = (vel_xy_backward*vel_xy_backward - vel_straight*vel_straight) / (vel_perpend*vel_perpend);
574 // this cannot be > 1
576 vel_perpend = vel_perpend * max(0, f);
579 fminimum = sqrt(fminimum);
580 vel_perpend = vel_perpend * max(fminimum, f);
584 vel_perpend = vel_perpend * max(0, 1 - frametime * wishspeed * sidefric);
586 vel_xy = vel_straight * wishdir + vel_perpend;
590 float vel_xy_preclamp;
591 vel_xy_preclamp = vlen(vel_xy);
592 if(vel_xy_preclamp > 0) // prevent division by zero
594 vel_xy_current += (vel_xy_forward - vel_xy_current) * speedclamp;
595 if(vel_xy_current < vel_xy_preclamp)
596 vel_xy = vel_xy * (vel_xy_current / vel_xy_preclamp);
600 self.velocity = vel_xy + velZ * '0 0 1';
603 void PM_AirAccelerate(vector wishdir, float wishspeed)
605 vector curvel, wishvel, acceldir, curdir;
606 float addspeed, accelspeed, curspeed, f;
612 curvel = self.velocity;
614 curspeed = vlen(curvel);
616 if(wishspeed > curspeed * 1.01)
618 wishspeed = min(wishspeed, curspeed + autocvar_sv_warsowbunny_airforwardaccel * self.stat_sv_maxspeed * frametime);
622 f = max(0, (autocvar_sv_warsowbunny_topspeed - curspeed) / (autocvar_sv_warsowbunny_topspeed - self.stat_sv_maxspeed));
623 wishspeed = max(curspeed, self.stat_sv_maxspeed) + autocvar_sv_warsowbunny_accel * f * self.stat_sv_maxspeed * frametime;
625 wishvel = wishdir * wishspeed;
626 acceldir = wishvel - curvel;
627 addspeed = vlen(acceldir);
628 acceldir = normalize(acceldir);
630 accelspeed = min(addspeed, autocvar_sv_warsowbunny_turnaccel * self.stat_sv_maxspeed * frametime);
632 if(autocvar_sv_warsowbunny_backtosideratio < 1)
634 curdir = normalize(curvel);
635 dot = acceldir * curdir;
637 acceldir = acceldir - (1 - autocvar_sv_warsowbunny_backtosideratio) * dot * curdir;
640 self.velocity += accelspeed * acceldir;
643 .vector movement_old;
646 .string lastclassname;
648 .float() PlayerPhysplug;
650 string specialcommand = "xwxwxsxsxaxdxaxdx1x ";
651 .float specialcommand_pos;
652 void SpecialCommand()
657 if(!CheatImpulse(99))
658 print("A hollow voice says \"Plugh\".\n");
662 float speedaward_speed;
663 string speedaward_holder;
664 string speedaward_uid;
665 void race_send_speedaward(float msg)
667 // send the best speed of the round
668 WriteByte(msg, SVC_TEMPENTITY);
669 WriteByte(msg, TE_CSQC_RACE);
670 WriteByte(msg, RACE_NET_SPEED_AWARD);
671 WriteInt24_t(msg, floor(speedaward_speed+0.5));
672 WriteString(msg, speedaward_holder);
675 float speedaward_alltimebest;
676 string speedaward_alltimebest_holder;
677 string speedaward_alltimebest_uid;
678 void race_send_speedaward_alltimebest(float msg)
680 // send the best speed
681 WriteByte(msg, SVC_TEMPENTITY);
682 WriteByte(msg, TE_CSQC_RACE);
683 WriteByte(msg, RACE_NET_SPEED_AWARD_BEST);
684 WriteInt24_t(msg, floor(speedaward_alltimebest+0.5));
685 WriteString(msg, speedaward_alltimebest_holder);
688 string GetMapname(void);
689 float speedaward_lastupdate;
690 float speedaward_lastsent;
691 void SV_PlayerPhysics()
693 vector wishvel, wishdir, v;
694 float wishspeed, f, maxspd_mod, spd, maxairspd, airaccel, swampspd_mod, buttons;
697 float not_allowed_to_move;
700 WarpZone_PlayerPhysics_FixVAngle();
705 maxspd_mod *= autocvar_g_keepaway_ballcarrier_highspeed;
707 maxspd_mod *= autocvar_g_movement_highspeed;
709 // fix physics stats for g_movement_highspeed
710 // TODO maybe rather use maxairspeed? needs testing
711 self.stat_sv_airaccel_qw = AdjustAirAccelQW(autocvar_sv_airaccel_qw, maxspd_mod);
712 if(autocvar_sv_airstrafeaccel_qw)
713 self.stat_sv_airstrafeaccel_qw = AdjustAirAccelQW(autocvar_sv_airstrafeaccel_qw, maxspd_mod);
715 self.stat_sv_airstrafeaccel_qw = 0;
716 self.stat_sv_airspeedlimit_nonqw = autocvar_sv_airspeedlimit_nonqw * maxspd_mod;
717 self.stat_sv_maxspeed = autocvar_sv_maxspeed * maxspd_mod; // also slow walking
719 if(self.PlayerPhysplug)
720 if(self.PlayerPhysplug())
723 self.race_movetime_frac += frametime;
724 f = floor(self.race_movetime_frac);
725 self.race_movetime_frac -= f;
726 self.race_movetime_count += f;
727 self.race_movetime = self.race_movetime_frac + self.race_movetime_count;
731 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);
735 else if(buttons == 1)
737 else if(buttons == 2)
739 else if(buttons == 128)
741 else if(buttons == 256)
743 else if(buttons == 512)
745 else if(buttons == 1024)
750 if(c == substring(specialcommand, self.specialcommand_pos, 1))
752 self.specialcommand_pos += 1;
753 if(self.specialcommand_pos >= strlen(specialcommand))
755 self.specialcommand_pos = 0;
760 else if(self.specialcommand_pos && (c != substring(specialcommand, self.specialcommand_pos - 1, 1)))
761 self.specialcommand_pos = 0;
765 if(buttons != self.buttons_old || self.movement != self.movement_old || self.v_angle != self.v_angle_old)
766 self.parm_idlesince = time;
768 buttons_prev = self.buttons_old;
769 self.buttons_old = buttons;
770 self.movement_old = self.movement;
771 self.v_angle_old = self.v_angle;
773 if(time < self.nickspamtime)
774 if(self.nickspamcount >= autocvar_g_nick_flood_penalty_yellow)
776 // slight annoyance for nick change scripts
777 self.movement = -1 * self.movement;
778 self.BUTTON_ATCK = self.BUTTON_JUMP = self.BUTTON_ATCK2 = self.BUTTON_ZOOM = self.BUTTON_CROUCH = self.BUTTON_HOOK = self.BUTTON_USE = 0;
780 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!
782 self.angles_x = random() * 360;
783 self.angles_y = random() * 360;
784 // at least I'm not forcing retardedview by also assigning to angles_z
785 self.fixangle = true;
789 if (self.punchangle != '0 0 0')
791 f = vlen(self.punchangle) - 10 * frametime;
793 self.punchangle = normalize(self.punchangle) * f;
795 self.punchangle = '0 0 0';
798 if (self.punchvector != '0 0 0')
800 f = vlen(self.punchvector) - 30 * frametime;
802 self.punchvector = normalize(self.punchvector) * f;
804 self.punchvector = '0 0 0';
807 if (IS_BOT_CLIENT(self))
809 if(playerdemo_read())
816 if(self.race_penalty)
817 if(time > self.race_penalty)
818 self.race_penalty = 0;
820 not_allowed_to_move = 0;
821 if(self.race_penalty)
822 not_allowed_to_move = 1;
823 if(time < game_starttime)
824 not_allowed_to_move = 1;
826 if(not_allowed_to_move)
828 self.velocity = '0 0 0';
829 self.movetype = MOVETYPE_NONE;
830 self.disableclientprediction = 2;
832 else if(self.disableclientprediction == 2)
834 if(self.movetype == MOVETYPE_NONE)
835 self.movetype = MOVETYPE_WALK;
836 self.disableclientprediction = 0;
840 if (self.movetype == MOVETYPE_NONE)
843 // when we get here, disableclientprediction cannot be 2
844 self.disableclientprediction = 0;
845 if(time < self.ladder_time)
846 self.disableclientprediction = 1;
848 if(time < self.spider_slowness)
850 self.stat_sv_maxspeed *= 0.5; // half speed while slow from spider
851 self.stat_sv_airspeedlimit_nonqw *= 0.5;
856 if(autocvar_sv_dodging_frozen && IS_REAL_CLIENT(self))
858 self.movement_x = bound(-5, self.movement.x, 5);
859 self.movement_y = bound(-5, self.movement.y, 5);
860 self.movement_z = bound(-5, self.movement.z, 5);
863 self.movement = '0 0 0';
864 self.disableclientprediction = 1;
866 vector midpoint = ((self.absmin + self.absmax) * 0.5);
867 if(pointcontents(midpoint) == CONTENT_WATER)
869 self.velocity = self.velocity * 0.5;
871 if(pointcontents(midpoint + '0 0 16') == CONTENT_WATER)
872 { self.velocity_z = 200; }
876 MUTATOR_CALLHOOK(PlayerPhysics);
878 if(self.player_blocked)
880 self.movement = '0 0 0';
881 self.disableclientprediction = 1;
888 swampspd_mod = self.swamp_slowdown; //cvar("g_balance_swamp_moverate");
891 // conveyors: first fix velocity
892 if(self.conveyor.state)
893 self.velocity -= self.conveyor.movedir;
895 if (!IS_PLAYER(self))
897 maxspd_mod = autocvar_sv_spectator_speed_multiplier;
898 if(!self.spectatorspeed)
899 self.spectatorspeed = maxspd_mod;
900 if(self.impulse && self.impulse <= 19 || (self.impulse >= 200 && self.impulse <= 209) || (self.impulse >= 220 && self.impulse <= 229))
902 if(self.lastclassname != "player")
904 if(self.impulse == 10 || self.impulse == 15 || self.impulse == 18 || (self.impulse >= 200 && self.impulse <= 209))
905 self.spectatorspeed = bound(1, self.spectatorspeed + 0.5, 5);
906 else if(self.impulse == 11)
907 self.spectatorspeed = maxspd_mod;
908 else if(self.impulse == 12 || self.impulse == 16 || self.impulse == 19 || (self.impulse >= 220 && self.impulse <= 229))
909 self.spectatorspeed = bound(1, self.spectatorspeed - 0.5, 5);
910 else if(self.impulse >= 1 && self.impulse <= 9)
911 self.spectatorspeed = 1 + 0.5 * (self.impulse - 1);
912 } // otherwise just clear
915 maxspd_mod = self.spectatorspeed;
918 spd = max(self.stat_sv_maxspeed, autocvar_sv_maxairspeed) * maxspd_mod * swampspd_mod;
919 if(self.speed != spd)
923 stuffcmd(self, strcat("cl_forwardspeed ", temps, "\n"));
924 stuffcmd(self, strcat("cl_backspeed ", temps, "\n"));
925 stuffcmd(self, strcat("cl_sidespeed ", temps, "\n"));
926 stuffcmd(self, strcat("cl_upspeed ", temps, "\n"));
929 maxspd_mod *= swampspd_mod; // only one common speed modder please!
932 // if dead, behave differently
936 if (!self.fixangle && !g_bugrigs)
939 self.angles_y = self.v_angle.y;
943 if(self.flags & FL_ONGROUND)
944 if(IS_PLAYER(self)) // no fall sounds for observers thank you very much
949 if(self.waterlevel < WATERLEVEL_SWIMMING)
950 if(time >= self.ladder_time)
953 self.nextstep = time + 0.3 + random() * 0.1;
954 trace_dphitq3surfaceflags = 0;
955 tracebox(self.origin, self.mins, self.maxs, self.origin - '0 0 1', MOVE_NOMONSTERS, self);
956 if (!(trace_dphitq3surfaceflags & Q3SURFACEFLAG_NOSTEPS))
958 if(trace_dphitq3surfaceflags & Q3SURFACEFLAG_METALSTEPS)
959 GlobalSound(globalsound_metalfall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
961 GlobalSound(globalsound_fall, CH_PLAYER, VOICETYPE_PLAYERSOUND);
972 if (self.flags & FL_WATERJUMP )
974 self.velocity_x = self.movedir.x;
975 self.velocity_y = self.movedir.y;
976 if (time > self.teleport_time || self.waterlevel == WATERLEVEL_NONE)
978 self.flags &= ~FL_WATERJUMP;
979 self.teleport_time = 0;
982 else if (g_bugrigs && IS_PLAYER(self))
986 else if (self.movetype == MOVETYPE_NOCLIP || self.movetype == MOVETYPE_FLY || self.movetype == MOVETYPE_FLY_WORLDONLY)
988 // noclipping or flying
989 self.flags &= ~FL_ONGROUND;
991 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
992 makevectors(self.v_angle);
993 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
994 wishvel = v_forward * self.movement.x + v_right * self.movement.y + '0 0 1' * self.movement.z;
996 wishdir = normalize(wishvel);
997 wishspeed = vlen(wishvel);
998 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
999 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1000 if (time >= self.teleport_time)
1001 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1003 else if (self.waterlevel >= WATERLEVEL_SWIMMING)
1006 self.flags &= ~FL_ONGROUND;
1008 makevectors(self.v_angle);
1009 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1010 wishvel = v_forward * self.movement.x + v_right * self.movement.y + '0 0 1' * self.movement.z;
1011 if (wishvel == '0 0 0')
1012 wishvel = '0 0 -60'; // drift towards bottom
1014 wishdir = normalize(wishvel);
1015 wishspeed = vlen(wishvel);
1016 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1017 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1018 wishspeed = wishspeed * 0.7;
1021 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
1023 // water acceleration
1024 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1026 else if (time < self.ladder_time)
1028 // on a spawnfunc_func_ladder or swimming in spawnfunc_func_water
1029 self.flags &= ~FL_ONGROUND;
1032 g = autocvar_sv_gravity * frametime;
1035 if(autocvar_sv_gameplayfix_gravityunaffectedbyticrate)
1038 self.velocity_z += g;
1041 self.velocity = self.velocity * (1 - frametime * autocvar_sv_friction);
1042 makevectors(self.v_angle);
1043 //wishvel = v_forward * self.movement_x + v_right * self.movement_y + v_up * self.movement_z;
1044 wishvel = v_forward * self.movement.x + v_right * self.movement.y + '0 0 1' * self.movement.z;
1045 self.velocity_z += g;
1046 if (self.ladder_entity.classname == "func_water")
1049 if (f > self.ladder_entity.speed)
1050 wishvel = wishvel * (self.ladder_entity.speed / f);
1052 self.watertype = self.ladder_entity.skin;
1053 f = self.ladder_entity.origin.z + self.ladder_entity.maxs.z;
1054 if ((self.origin.z + self.view_ofs.z) < f)
1055 self.waterlevel = WATERLEVEL_SUBMERGED;
1056 else if ((self.origin.z + (self.mins.z + self.maxs.z) * 0.5) < f)
1057 self.waterlevel = WATERLEVEL_SWIMMING;
1058 else if ((self.origin.z + self.mins.z + 1) < f)
1059 self.waterlevel = WATERLEVEL_WETFEET;
1062 self.waterlevel = WATERLEVEL_NONE;
1063 self.watertype = CONTENT_EMPTY;
1067 wishdir = normalize(wishvel);
1068 wishspeed = vlen(wishvel);
1069 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1070 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1071 if (time >= self.teleport_time)
1073 // water acceleration
1074 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1077 else if (self.items & IT_USING_JETPACK)
1079 //makevectors(self.v_angle_y * '0 1 0');
1080 makevectors(self.v_angle);
1081 wishvel = v_forward * self.movement.x + v_right * self.movement.y;
1082 // add remaining speed as Z component
1083 maxairspd = autocvar_sv_maxairspeed*max(1, maxspd_mod);
1084 // fix speedhacks :P
1085 wishvel = normalize(wishvel) * min(vlen(wishvel) / maxairspd, 1);
1086 // add the unused velocity as up component
1089 // if(self.BUTTON_JUMP)
1090 wishvel.z = sqrt(max(0, 1 - wishvel * wishvel));
1092 // it is now normalized, so...
1093 float a_side, a_up, a_add, a_diff;
1094 a_side = autocvar_g_jetpack_acceleration_side;
1095 a_up = autocvar_g_jetpack_acceleration_up;
1096 a_add = autocvar_g_jetpack_antigravity * autocvar_sv_gravity;
1098 wishvel.x *= a_side;
1099 wishvel.y *= a_side;
1105 //////////////////////////////////////////////////////////////////////////////////////
1106 // finding the maximum over all vectors of above form
1107 // with wishvel having an absolute value of 1
1108 //////////////////////////////////////////////////////////////////////////////////////
1109 // we're finding the maximum over
1110 // f(a_side, a_up, a_add, z) := a_side * (1 - z^2) + (a_add + a_up * z)^2;
1111 // for z in the range from -1 to 1
1112 //////////////////////////////////////////////////////////////////////////////////////
1113 // maximum is EITHER attained at the single extreme point:
1114 a_diff = a_side * a_side - a_up * a_up;
1117 f = a_add * a_up / a_diff; // this is the zero of diff(f(a_side, a_up, a_add, z), z)
1118 if(f > -1 && f < 1) // can it be attained?
1120 best = (a_diff + a_add * a_add) * (a_diff + a_up * a_up) / a_diff;
1121 //print("middle\n");
1124 // OR attained at z = 1:
1125 f = (a_up + a_add) * (a_up + a_add);
1131 // OR attained at z = -1:
1132 f = (a_up - a_add) * (a_up - a_add);
1136 //print("bottom\n");
1139 //////////////////////////////////////////////////////////////////////////////////////
1141 //print("best possible acceleration: ", ftos(best), "\n");
1144 fxy = bound(0, 1 - (self.velocity * normalize(wishvel.x * '1 0 0' + wishvel.y * '0 1 0')) / autocvar_g_jetpack_maxspeed_side, 1);
1145 if(wishvel.z - autocvar_sv_gravity > 0)
1146 fz = bound(0, 1 - self.velocity.z / autocvar_g_jetpack_maxspeed_up, 1);
1148 fz = bound(0, 1 + self.velocity.z / autocvar_g_jetpack_maxspeed_up, 1);
1152 wishvel.z = (wishvel.z - autocvar_sv_gravity) * fz + autocvar_sv_gravity;
1155 fvel = min(1, vlen(wishvel) / best);
1156 if(autocvar_g_jetpack_fuel && !(self.items & IT_UNLIMITED_WEAPON_AMMO))
1157 f = min(1, self.ammo_fuel / (autocvar_g_jetpack_fuel * frametime * fvel));
1161 //print("this acceleration: ", ftos(vlen(wishvel) * f), "\n");
1163 if (f > 0 && wishvel != '0 0 0')
1165 self.velocity = self.velocity + wishvel * f * frametime;
1166 if (!(self.items & IT_UNLIMITED_WEAPON_AMMO))
1167 self.ammo_fuel -= autocvar_g_jetpack_fuel * frametime * fvel * f;
1168 self.flags &= ~FL_ONGROUND;
1169 self.items |= IT_USING_JETPACK;
1171 // jetpack also inhibits health regeneration, but only for 1 second
1172 self.pauseregen_finished = max(self.pauseregen_finished, time + autocvar_g_balance_pause_fuel_regen);
1175 else if (self.flags & FL_ONGROUND)
1177 // we get here if we ran out of ammo
1178 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1179 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
1182 makevectors(self.v_angle.y * '0 1 0');
1183 wishvel = v_forward * self.movement.x + v_right * self.movement.y;
1185 if(!(self.lastflags & FL_ONGROUND))
1187 if(autocvar_speedmeter)
1188 dprint(strcat("landing velocity: ", vtos(self.velocity), " (abs: ", ftos(vlen(self.velocity)), ")\n"));
1189 if(self.lastground < time - 0.3)
1190 self.velocity = self.velocity * (1 - autocvar_sv_friction_on_land);
1191 if(self.jumppadcount > 1)
1192 dprint(strcat(ftos(self.jumppadcount), "x jumppad combo\n"));
1193 self.jumppadcount = 0;
1201 if (f < autocvar_sv_stopspeed)
1202 f = 1 - frametime * (autocvar_sv_stopspeed / f) * autocvar_sv_friction;
1204 f = 1 - frametime * autocvar_sv_friction;
1206 self.velocity = self.velocity * f;
1208 self.velocity = '0 0 0';
1210 Mathematical analysis time!
1212 Our goal is to invert this mess.
1214 For the two cases we get:
1215 v = v0 * (1 - frametime * (autocvar_sv_stopspeed / v0) * autocvar_sv_friction)
1216 = v0 - frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1217 v0 = v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction
1219 v = v0 * (1 - frametime * autocvar_sv_friction)
1220 v0 = v / (1 - frametime * autocvar_sv_friction)
1222 These cases would be chosen ONLY if:
1223 v0 < autocvar_sv_stopspeed
1224 v + frametime * autocvar_sv_stopspeed * autocvar_sv_friction < autocvar_sv_stopspeed
1225 v < autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1227 v0 >= autocvar_sv_stopspeed
1228 v / (1 - frametime * autocvar_sv_friction) >= autocvar_sv_stopspeed
1229 v >= autocvar_sv_stopspeed * (1 - frametime * autocvar_sv_friction)
1234 wishdir = normalize(wishvel);
1235 wishspeed = vlen(wishvel);
1236 if (wishspeed > self.stat_sv_maxspeed*maxspd_mod)
1237 wishspeed = self.stat_sv_maxspeed*maxspd_mod;
1239 wishspeed = wishspeed * 0.5;
1240 if (time >= self.teleport_time)
1241 PM_Accelerate(wishdir, wishspeed, wishspeed, autocvar_sv_accelerate*maxspd_mod, 1, 0, 0, 0);
1246 // we get here if we ran out of ammo
1247 if((self.items & IT_JETPACK) && self.BUTTON_HOOK && !(buttons_prev & 32) && self.ammo_fuel < 0.01)
1248 Send_Notification(NOTIF_ONE, self, MSG_INFO, INFO_JETPACK_NOFUEL);
1252 maxairspd = autocvar_sv_maxairspeed*maxspd_mod;
1253 airaccel = autocvar_sv_airaccelerate*maxspd_mod;
1257 maxairspd = autocvar_sv_maxairspeed;
1258 airaccel = autocvar_sv_airaccelerate;
1261 makevectors(self.v_angle.y * '0 1 0');
1262 wishvel = v_forward * self.movement.x + v_right * self.movement.y;
1264 wishdir = normalize(wishvel);
1265 wishspeed = wishspeed0 = vlen(wishvel);
1266 if (wishspeed0 > self.stat_sv_maxspeed*maxspd_mod)
1267 wishspeed0 = self.stat_sv_maxspeed*maxspd_mod;
1268 if (wishspeed > maxairspd)
1269 wishspeed = maxairspd;
1271 wishspeed = wishspeed * 0.5;
1272 if (time >= self.teleport_time)
1279 airaccelqw = self.stat_sv_airaccel_qw;
1280 accelerating = (self.velocity * wishdir > 0);
1281 wishspeed2 = wishspeed;
1284 if(autocvar_sv_airstopaccelerate)
1287 curdir = self.velocity;
1289 curdir = normalize(curdir);
1290 airaccel = airaccel + (autocvar_sv_airstopaccelerate*maxspd_mod - airaccel) * max(0, -(curdir * wishdir));
1292 // note that for straight forward jumping:
1293 // step = accel * frametime * wishspeed0;
1294 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
1296 // dv/dt = accel * maxspeed (when slow)
1297 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
1298 // log dv/dt = logaccel + logmaxspeed (when slow)
1299 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
1300 strafity = IsMoveInDirection(self.movement, -90) + IsMoveInDirection(self.movement, +90); // if one is nonzero, other is always zero
1301 if(autocvar_sv_maxairstrafespeed)
1302 wishspeed = min(wishspeed, GeomLerp(autocvar_sv_maxairspeed*maxspd_mod, strafity, autocvar_sv_maxairstrafespeed*maxspd_mod));
1303 if(autocvar_sv_airstrafeaccelerate)
1304 airaccel = GeomLerp(airaccel, strafity, autocvar_sv_airstrafeaccelerate*maxspd_mod);
1305 if(self.stat_sv_airstrafeaccel_qw)
1306 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));
1309 if(autocvar_sv_warsowbunny_turnaccel && accelerating && self.movement_y == 0 && self.movement.x != 0)
1310 PM_AirAccelerate(wishdir, wishspeed);
1312 PM_Accelerate(wishdir, wishspeed, wishspeed0, airaccel, airaccelqw, autocvar_sv_airaccel_qw_stretchfactor, autocvar_sv_airaccel_sideways_friction / maxairspd, self.stat_sv_airspeedlimit_nonqw);
1314 if(autocvar_sv_aircontrol)
1315 CPM_PM_Aircontrol(wishdir, wishspeed2);
1319 if((g_cts || g_race) && !IS_OBSERVER(self))
1321 if(vlen(self.velocity - self.velocity.z * '0 0 1') > speedaward_speed)
1323 speedaward_speed = vlen(self.velocity - self.velocity.z * '0 0 1');
1324 speedaward_holder = self.netname;
1325 speedaward_uid = self.crypto_idfp;
1326 speedaward_lastupdate = time;
1328 if(speedaward_speed > speedaward_lastsent && time - speedaward_lastupdate > 1)
1330 string rr = (g_cts) ? CTS_RECORD : RACE_RECORD;
1331 race_send_speedaward(MSG_ALL);
1332 speedaward_lastsent = speedaward_speed;
1333 if (speedaward_speed > speedaward_alltimebest && speedaward_uid != "")
1335 speedaward_alltimebest = speedaward_speed;
1336 speedaward_alltimebest_holder = speedaward_holder;
1337 speedaward_alltimebest_uid = speedaward_uid;
1338 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/speed"), ftos(speedaward_alltimebest));
1339 db_put(ServerProgsDB, strcat(GetMapname(), rr, "speed/crypto_idfp"), speedaward_alltimebest_uid);
1340 race_send_speedaward_alltimebest(MSG_ALL);
1347 xyspeed = vlen('1 0 0' * self.velocity.x + '0 1 0' * self.velocity.y);
1348 if(self.weapon == WEP_VORTEX && WEP_CVAR(vortex, charge) && WEP_CVAR(vortex, charge_velocity_rate) && xyspeed > WEP_CVAR(vortex, charge_minspeed))
1350 // add a maximum of charge_velocity_rate when going fast (f = 1), gradually increasing from minspeed (f = 0) to maxspeed
1351 xyspeed = min(xyspeed, WEP_CVAR(vortex, charge_maxspeed));
1352 f = (xyspeed - WEP_CVAR(vortex, charge_minspeed)) / (WEP_CVAR(vortex, charge_maxspeed) - WEP_CVAR(vortex, charge_minspeed));
1353 // add the extra charge
1354 self.vortex_charge = min(1, self.vortex_charge + WEP_CVAR(vortex, charge_velocity_rate) * f * frametime);
1357 if(self.flags & FL_ONGROUND)
1358 self.lastground = time;
1360 // conveyors: then break velocity again
1361 if(self.conveyor.state)
1362 self.velocity += self.conveyor.movedir;
1364 self.lastflags = self.flags;
1365 self.lastclassname = self.classname;