Updated aim.c

This commit is contained in:
Wizzard 2023-09-20 09:17:50 -04:00
parent c5e985bc42
commit e43cea56f8
1 changed files with 29 additions and 28 deletions

View File

@ -1,5 +1,6 @@
#include <math.h> #include <math.h>
#include <cfloat>
#include "features.h" #include "features.h"
#include "../include/sdk.h" #include "../include/sdk.h"
@ -12,26 +13,20 @@
/* Scale factor for aim punch */ /* Scale factor for aim punch */
#define AIM_PUNCH_MULT 2 #define AIM_PUNCH_MULT 2
static float vec_length(vec3_t v) {
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
static bool is_visible(vec3_t start, vec3_t end) { static bool is_visible(vec3_t start, vec3_t end) {
/* Syntax: PM_TraceLine(start, end, flags, usehulll, ignore_pe); */ pmtrace_t* tr = i_engine->PM_TraceLine(start, end, PM_TRACELINE_PHYSENTSONLY, 2, -1);
pmtrace_t* tr = if (tr->ent <= 0) return false;
i_engine->PM_TraceLine(start, end, PM_TRACELINE_PHYSENTSONLY, 2, -1);
/* We didn't hit a valid entity */
if (tr->ent <= 0)
return false;
/* Get entity index from physents, check if we can't get a valid player */
const int ent_idx = i_pmove->physents[tr->ent].info; const int ent_idx = i_pmove->physents[tr->ent].info;
if (!get_player(ent_idx)) return get_player(ent_idx) != NULL;
return false;
/* We hit a valid player */
return true;
} }
static vec3_t get_closest_delta(vec3_t viewangles) { static vec3_t get_closest_delta(vec3_t viewangles) {
/* Compensate aim punch. We get g_punchAngles from CalcRefdef hook */ // Compensate for aim punch
viewangles.x += g_punchAngles.x * AIM_PUNCH_MULT; viewangles.x += g_punchAngles.x * AIM_PUNCH_MULT;
viewangles.y += g_punchAngles.y * AIM_PUNCH_MULT; viewangles.y += g_punchAngles.y * AIM_PUNCH_MULT;
viewangles.z += g_punchAngles.z * AIM_PUNCH_MULT; viewangles.z += g_punchAngles.z * AIM_PUNCH_MULT;
@ -40,38 +35,44 @@ static vec3_t get_closest_delta(vec3_t viewangles) {
i_engine->pEventAPI->EV_LocalPlayerViewheight(view_height); i_engine->pEventAPI->EV_LocalPlayerViewheight(view_height);
vec3_t local_eyes = vec_add(localplayer->origin, view_height); vec3_t local_eyes = vec_add(localplayer->origin, view_height);
/* These 2 vars are used to store the best target across iterations. float min_distance = FLT_MAX; // For tracking the closest player
* NOTE: The default value of best_fov will be the aimbot fov */
float best_fov = dz_aimbot->value; float best_fov = dz_aimbot->value;
vec3_t best_delta = { 0, 0, 0 }; vec3_t best_delta = { 0, 0, 0 };
for (int i = 1; i <= i_engine->GetMaxClients(); i++) { for (int i = 1; i <= i_engine->GetMaxClients(); i++) {
cl_entity_t* ent = get_player(i); cl_entity_t* ent = get_player(i);
if (!is_alive(ent) || is_friend(ent)) if (!is_alive(ent) || is_friend(ent)) {
continue; continue; // Skip if not alive or is a friend
}
/* TODO: Get bones origin instead of calculating from ent origin */
vec3_t head_pos = ent->origin; vec3_t head_pos = ent->origin;
if (ent->curstate.usehull != 1) /* Get head if not crouched */ if (ent->curstate.usehull != 1) { // Get head if not crouched
head_pos.z += HEAD_OFFSET; head_pos.z += HEAD_OFFSET;
}
if (!is_visible(local_eyes, head_pos)) /* We can't see player */ float distance = vec_length(vec_sub(ent->origin, local_eyes));
continue; if (distance > min_distance) {
continue; // Skip players that are further than the current closest target
}
const vec3_t enemy_angle = vec_to_ang(vec_sub(head_pos, local_eyes)); const vec3_t enemy_angle = vec_to_ang(vec_sub(head_pos, local_eyes));
const vec3_t delta = vec_sub(enemy_angle, viewangles); const vec3_t delta = vec_sub(enemy_angle, viewangles);
vec_norm(delta); vec_norm(delta);
float fov = hypotf(delta.x, delta.y); float fov = hypotf(delta.x, delta.y);
if (fov > 360.0f) if (fov > 360.0f) {
fov = remainderf(fov, 360.0f); fov = remainderf(fov, 360.0f);
if (fov > 180.0f) }
if (fov > 180.0f) {
fov = 360.0f - fov; fov = 360.0f - fov;
}
if (fov < best_fov) { // Only check visibility for potential targets
if (fov < best_fov && is_visible(local_eyes, head_pos)) {
best_fov = fov; best_fov = fov;
vec_copy(best_delta, delta); vec_copy(best_delta, delta);
min_distance = distance; // Update the closest target's distance
} }
} }