Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. void c_misc::autostrafe(C_CSPlayer* local)
  2. {
  3.     if (!config::get<bool>(g_cfg.movement_autostrafe)) return;
  4.  
  5.     enum directions : int
  6.     {
  7.         FORWARDS = 0,
  8.         BACKWARDS = 180,
  9.         LEFT = 90,
  10.         RIGHT = -90
  11.     };
  12.  
  13.     is_bhopping = g_cl.m_cmd->m_buttons & IN_JUMP;
  14.  
  15.     if (!is_bhopping && local->flags() & FL_ONGROUND)
  16.     {
  17.         calculated_direction = directions::FORWARDS;
  18.         in_transition = false;
  19.         return;
  20.     }
  21.  
  22.     vec3_t base;
  23.     g_csgo.m_engine->GetViewAngles(base);
  24.  
  25.     auto get_rough_direction = [&](float true_direction) -> float
  26.     {
  27.         std::array<float, 4> minimum = { directions::FORWARDS, directions::BACKWARDS, directions::LEFT, directions::RIGHT };
  28.         float best_angle, best_delta = 181.f;
  29.  
  30.         for (size_t i = 0; i < minimum.size(); ++i)
  31.         {
  32.             float rough_direction = base.y + minimum.at(i);
  33.             float delta = fabsf(math::normalize_angle(true_direction - rough_direction));
  34.  
  35.             if (delta < best_delta)
  36.             {
  37.                 best_angle = rough_direction;
  38.                 best_delta = delta;
  39.             }
  40.         }
  41.  
  42.         return best_angle;
  43.     };
  44.  
  45.     vec3_t angles;
  46.     math::vector_angle(local->velocity(), angles);
  47.  
  48.     true_direction = angles.y;
  49.  
  50.     if (g_cl.m_cmd->m_buttons & IN_FORWARD)
  51.     {
  52.         wish_direction = base.y + directions::FORWARDS;
  53.     }
  54.     else if (g_cl.m_cmd->m_buttons & IN_BACK)
  55.     {
  56.         wish_direction = base.y + directions::BACKWARDS;
  57.     }
  58.     else if (g_cl.m_cmd->m_buttons & IN_MOVELEFT)
  59.     {
  60.         wish_direction = base.y + directions::LEFT;
  61.     }
  62.     else if (g_cl.m_cmd->m_buttons & IN_MOVERIGHT)
  63.     {
  64.         wish_direction = base.y + directions::RIGHT;
  65.     }
  66.     else
  67.     {
  68.         g_cl.m_cmd->m_buttons |= IN_FORWARD;
  69.         wish_direction = base.y + directions::FORWARDS;
  70.     }
  71.  
  72.     float speed_rotation = math::min(math::rad_to_deg(std::asinf(30.f / local->velocity().length_2d())) * 0.5f, 45.f);
  73.  
  74.     if (in_transition)
  75.     {
  76.         float ideal_step = speed_rotation + calculated_direction;
  77.         step = fabsf(math::normalize_angle(calculated_direction - ideal_step));
  78.  
  79.         if (fabsf(math::normalize_angle(wish_direction - calculated_direction)) > step)
  80.         {
  81.             float add = math::normalize_angle(calculated_direction + step);
  82.             float sub = math::normalize_angle(calculated_direction - step);
  83.  
  84.             if (fabsf(math::normalize_angle(wish_direction - add)) >= fabsf(math::normalize_angle(wish_direction - sub)))
  85.             {
  86.                 calculated_direction -= step;
  87.             }
  88.             else
  89.             {
  90.                 calculated_direction += step;
  91.             }
  92.         }
  93.         else
  94.         {
  95.             in_transition = false;
  96.         }
  97.     }
  98.     else
  99.     {
  100.         rough_direction = get_rough_direction(true_direction);
  101.         calculated_direction = rough_direction;
  102.  
  103.         if (rough_direction != wish_direction)
  104.         {
  105.             in_transition = true;
  106.         }
  107.     }
  108.  
  109.     g_cl.m_cmd->m_forwardmove = 0.f;
  110.     g_cl.m_cmd->m_sidemove = g_cl.m_cmd->m_command_number % 2 ? 450.f : -450.f;
  111.  
  112.     float direction = (g_cl.m_cmd->m_command_number % 2 ? speed_rotation : -speed_rotation) + calculated_direction;
  113.     float rotation = math::deg_to_rad(base.y - direction);
  114.  
  115.     float cos_rot = cos(rotation);
  116.     float sin_rot = sin(rotation);
  117.  
  118.     float forwardmove = (cos_rot * g_cl.m_cmd->m_forwardmove) - (sin_rot * g_cl.m_cmd->m_sidemove);
  119.     float sidemove = (sin_rot * g_cl.m_cmd->m_forwardmove) + (cos_rot * g_cl.m_cmd->m_sidemove);
  120.  
  121.     g_cl.m_cmd->m_forwardmove = forwardmove;
  122.     g_cl.m_cmd->m_sidemove = sidemove;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement