@@ -76,30 +76,38 @@ local vPlayerFuture
76
76
local pLocalFuture
77
77
local ping = 0
78
78
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 }
96
105
end
106
+
107
+ :: continue::
97
108
end
98
- if closestDistance < 2000 then
99
- return closestPlayer
100
- else
101
- return nil
102
- end
109
+
110
+ return target
103
111
end
104
112
105
113
function TargetPositionPrediction (targetLastPos , tickRate , time , targetEntity )
@@ -215,6 +223,7 @@ local Hitbox = {
215
223
Chest = 7
216
224
}
217
225
226
+
218
227
--[[ Code needed to run 66 times a second ]] --
219
228
local function OnCreateMove (pCmd )
220
229
if not Swingpred :GetValue () then goto continue end -- enable or distable script
@@ -276,8 +285,10 @@ local function OnCreateMove(pCmd)
276
285
pLocalOrigin = (pLocal :GetAbsOrigin () + Vheight )
277
286
end
278
287
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
281
292
-- [-----Refil and skip code when alone-----]
282
293
283
294
if closestPlayer == nil then
@@ -286,7 +297,7 @@ if closestPlayer == nil then
286
297
end goto continue
287
298
end
288
299
289
- vPlayerOrigin = closestPlayer :GetAbsOrigin ()
300
+ vPlayerOrigin = closestPlayer :GetAbsOrigin () -- get closest player origin
290
301
291
302
-- [--------------Prediction-------------------] -- predict both players position after swing
292
303
0 commit comments