void Movement::Strafe() {
vec3_t velocity;
float delta, abs_delta, velocity_delta, correct;
// don't strafe while noclipping or on ladders..
if (g_cl.m_local->m_MoveType() == MOVETYPE_NOCLIP || g_cl.m_local->m_MoveType() == MOVETYPE_LADDER)
return;
// get networked velocity ( maybe absvelocity better here? ).
// meh, should be predicted anyway? ill see.
velocity = g_cl.m_local->m_vecAbsVelocity();
// get the velocity len2d ( speed ).
m_speed = velocity.length_2d();
// compute the ideal strafe angle for our velocity.
m_ideal = (m_speed > 0.f) ? math::rad_to_deg(std::asin(15.f / m_speed)) : 90.f;
m_ideal2 = (m_speed > 0.f) ? math::rad_to_deg(std::asin(30.f / m_speed)) : 90.f;
// some additional sanity.
math::clamp(m_ideal, 0.f, 90.f);
math::clamp(m_ideal2, 0.f, 90.f);
// save entity bounds ( used much in circle-strafer ).
m_mins = g_cl.m_local->m_vecMins();
m_maxs = g_cl.m_local->m_vecMaxs();
// save our origin
m_origin = g_cl.m_local->m_vecOrigin();
// disable strafing while pressing shift.
if ((g_cl.m_buttons & IN_SPEED) || (g_cl.m_flags & FL_ONGROUND))
return;
// for changing direction.
// we want to change strafe direction every call.
m_switch_value *= -1.f;
// for allign strafer.
++m_strafe_index;
if (g_cl.m_pressing_move && g_menu.main.movement.autostrafe.get()) {
enum EDirections {
FORWARDS = 0,
BACKWARDS = 180,
LEFT = 90,
RIGHT = -90,
BACK_LEFT = 135,
BACK_RIGHT = -135
};
float wish_dir{ };
// get our key presses.
bool holding_w = g_cl.m_buttons & IN_FORWARD;
bool holding_a = g_cl.m_buttons & IN_MOVELEFT;
bool holding_s = g_cl.m_buttons & IN_BACK;
bool holding_d = g_cl.m_buttons & IN_MOVERIGHT;
// move in the appropriate direction.
if (holding_w) {
// forward left
if (holding_a) {
wish_dir += (EDirections::LEFT / 2);
}
// forward right
else if (holding_d) {
wish_dir += (EDirections::RIGHT / 2);
}
// forward
else {
wish_dir += EDirections::FORWARDS;
}
}
else if (holding_s) {
// back left
if (holding_a) {
wish_dir += EDirections::BACK_LEFT;
}
// back right
else if (holding_d) {
wish_dir += EDirections::BACK_RIGHT;
}
// back
else {
wish_dir += EDirections::BACKWARDS;
}
g_cl.m_cmd->m_forward_move = 0;
}
else if (holding_a) {
// left
wish_dir += EDirections::LEFT;
}
else if (holding_d) {
// right
wish_dir += EDirections::RIGHT;
}
g_cl.m_strafe_angles.y += math::NormalizeYaw(wish_dir);
}
// cancel out any forwardmove values.
g_cl.m_cmd->m_forward_move = 0.f;
if (!g_menu.main.movement.autostrafe.get())
return;
// get our viewangle change.
delta = math::NormalizedAngle(g_cl.m_strafe_angles.y - m_old_yaw);
// convert to absolute change.
abs_delta = std::abs(delta);
// save old yaw for next call.
m_circle_yaw = m_old_yaw = g_cl.m_strafe_angles.y;
// set strafe direction based on mouse direction change.
if (delta > 0.f)
g_cl.m_cmd->m_side_move = -450.f;
else if (delta < 0.f)
g_cl.m_cmd->m_side_move = 450.f;
// we can accelerate more, because we strafed less then needed
// or we got of track and need to be retracked.
if (abs_delta <= m_ideal || abs_delta >= 30.f) {
// compute angle of the direction we are traveling in.
ang_t velocity_angle;
math::VectorAngles(velocity, velocity_angle);
// get the delta between our direction and where we are looking at.
velocity_delta = math::NormalizeYaw(g_cl.m_strafe_angles.y - velocity_angle.y);
// correct our strafe amongst the path of a circle.
correct = m_ideal;
if (velocity_delta <= correct || m_speed <= 15.f) {
// not moving mouse, switch strafe every tick.
if (-correct <= velocity_delta || m_speed <= 15.f) {
g_cl.m_strafe_angles.y += (m_ideal * m_switch_value);
g_cl.m_cmd->m_side_move = 450.f * m_switch_value;
}
else {
g_cl.m_strafe_angles.y = velocity_angle.y - correct;
g_cl.m_cmd->m_side_move = 450.f;
}
}
else {
g_cl.m_strafe_angles.y = velocity_angle.y + correct;
g_cl.m_cmd->m_side_move = -450.f;
}
}
}