update_enemies = function(self)
local localplayer = EntityList.GetClientEntity(EngineClient.GetLocalPlayer())
if not localplayer then return end
local my_index = EntityList.GetClientEntity(EngineClient.GetLocalPlayer()):GetPlayer()
if not my_index then return end
if EngineClient.IsConnected() and my_index:IsAlive() then
local players = EntityList.GetEntitiesByName("CCSPlayer")
local fov_enemy, maximum_fov = nil, 180
for i = 1, #players do
local enemy = players[i]:GetPlayer()
if enemy ~= my_index and not enemy:IsTeamMate() and enemy:IsAlive() then
local my_origin = my_index:GetRenderOrigin()
local enemy_origin = enemy:GetRenderOrigin()
local world_to_screen = (
my_origin.x - enemy_origin.x == 0 and my_origin.y - enemy_origin.z == 0
) and 0 or math.deg(
math.atan2(
my_origin.y - enemy_origin.y, my_origin.x - enemy_origin.x
)
) - EngineClient.GetViewAngles().yaw + 180
local calculated_fov = math.abs(self.normalize_yaw(world_to_screen))
if not fov_enemy or calculated_fov <= maximum_fov then
fov_enemy = enemy
maximum_fov = calculated_fov
end
end
end
return ({
enemy = fov_enemy,
fov = maximum_fov
})
end
end
local data, current_enemy = update_enemies(), nil
if data ~= nil then
current_enemy = data.enemy
end
local target_name = (current_enemy ~= nil and current_enemy:IsAlive()) and current_enemy:GetName() or "nil"