Skip to content

Commit 4406007

Browse files
v4.2.0
1 parent c0810f2 commit 4406007

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

Swing_Prediction.lua

+36-25
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,38 @@ local vPlayerFuture
7676
local pLocalFuture
7777
local ping = 0
7878

79-
function GetClosestEnemy(pLocal, pLocalOrigin)
80-
local players = entities.FindByClass("CTFPlayer") -- Create a table of all players in the game
81-
closestDistance = 2000
82-
local maxDistance = 2000
83-
closestPlayer = nil
84-
-- find closest enemy
85-
for _, vPlayer in ipairs(players) do
86-
if vPlayer ~= nil and vPlayer:IsAlive() and vPlayer:GetTeamNumber() ~= pLocal:GetTeamNumber() then
87-
local vPlayerOrigin = vPlayer:GetAbsOrigin()
88-
local distanceX = math.abs(vPlayerOrigin.x - pLocalOrigin.x)
89-
local distanceY = math.abs(vPlayerOrigin.y - pLocalOrigin.y)
90-
local distanceZ = math.abs(vPlayerOrigin.z - pLocalOrigin.z)
91-
local distance = math.sqrt(distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ)
92-
if distance < closestDistance and distance <= maxDistance then
93-
closestPlayer = vPlayer
94-
closestDistance = distance
95-
end
79+
---@param me WPlayer
80+
---@return AimTarget? target
81+
local function GetBestTarget(me)
82+
local players = entities.FindByClass("CTFPlayer")
83+
local target = nil
84+
local lastFov = math.huge
85+
86+
for _, entity in pairs(players) do
87+
if not entity then goto continue end
88+
if not entity:IsAlive() then goto continue end
89+
if entity:GetTeamNumber() == entities.GetLocalPlayer():GetTeamNumber() then goto continue end
90+
91+
local player = WPlayer.FromEntity(entity)
92+
93+
local aimPos = entity:GetAbsOrigin()
94+
local angles = Math.PositionAngles(pLocalOrigin, aimPos)
95+
local fov = Math.AngleFov(angles, engine.GetViewAngles())
96+
if fov > 360 then goto continue end
97+
98+
-- Visiblity Check
99+
if not Helpers.VisPos(entity, pLocalOrigin, aimPos) then goto continue end
100+
101+
-- Add valid target
102+
if fov < lastFov then
103+
lastFov = fov
104+
target = { entity = entity, pos = aimPos, angles = angles, factor = fov }
96105
end
106+
107+
::continue::
97108
end
98-
if closestDistance < 2000 then
99-
return closestPlayer
100-
else
101-
return nil
102-
end
109+
110+
return target
103111
end
104112

105113
function TargetPositionPrediction(targetLastPos, tickRate, time, targetEntity)
@@ -215,6 +223,7 @@ local Hitbox = {
215223
Chest = 7
216224
}
217225

226+
218227
--[[ Code needed to run 66 times a second ]]--
219228
local function OnCreateMove(pCmd)
220229
if not Swingpred:GetValue() then goto continue end -- enable or distable script
@@ -276,8 +285,10 @@ local function OnCreateMove(pCmd)
276285
pLocalOrigin = (pLocal:GetAbsOrigin() + Vheight)
277286
end
278287

279-
--[-----Get closestPlayer------------------]
280-
closestPlayer = GetClosestEnemy(pLocal, pLocalOrigin, players)
288+
--[-----Get best target------------------]
289+
if GetBestTarget(pLocal).entity ~= nil then
290+
closestPlayer = GetBestTarget(pLocal).entity --GetClosestEnemy(pLocal, pLocalOrigin, players)
291+
end
281292
--[-----Refil and skip code when alone-----]
282293

283294
if closestPlayer == nil then
@@ -286,7 +297,7 @@ if closestPlayer == nil then
286297
end goto continue
287298
end
288299

289-
vPlayerOrigin = closestPlayer:GetAbsOrigin()
300+
vPlayerOrigin = closestPlayer:GetAbsOrigin() -- get closest player origin
290301

291302
--[--------------Prediction-------------------] -- predict both players position after swing
292303

0 commit comments

Comments
 (0)