-
Notifications
You must be signed in to change notification settings - Fork 19
/
Aimbot.lua
313 lines (254 loc) · 9.04 KB
/
Aimbot.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
--[[
Custom Aimbot for Lmaobox
Author: github.com/lnx00
]]
if UnloadLib then UnloadLib() end
---@alias AimTarget { entity : Entity, angles : EulerAngles, factor : number }
---@type boolean, lnxLib
local libLoaded, lnxLib = pcall(require, "lnxLib")
assert(libLoaded, "lnxLib not found, please install it!")
assert(lnxLib.GetVersion() >= 0.987, "lnxLib version is too old, please update it!")
local Math, Conversion = lnxLib.Utils.Math, lnxLib.Utils.Conversion
local WPlayer, WWeapon = lnxLib.TF2.WPlayer, lnxLib.TF2.WWeapon
local Helpers = lnxLib.TF2.Helpers
local Prediction = lnxLib.TF2.Prediction
local Fonts = lnxLib.UI.Fonts
local Hitbox = {
Head = 1,
Neck = 2,
Pelvis = 4,
Body = 5,
Chest = 7
}
local options = {
AimKey = KEY_LSHIFT,
AutoShoot = true,
Silent = true,
AimPos = {
Hitscan = Hitbox.Head,
Projectile = Hitbox.Head
},
AimFov = 40,
PredTicks = 60,
StrafePrediction = true,
StrafeSamples = 1,
DebugInfo = true
}
local latency = 0
local lerp = 0
local lastAngles = {} ---@type EulerAngles[]
local strafeAngles = {} ---@type number[]
---@param me WPlayer
local function CalcStrafe(me)
local players = entities.FindByClass("CTFPlayer")
for idx, entity in ipairs(players) do
if entity:IsDormant() or not entity:IsAlive() then
lastAngles[idx] = nil
strafeAngles[idx] = nil
goto continue
end
-- Ignore teammates (for now)
if entity:GetTeamNumber() == me:GetTeamNumber() then
goto continue
end
local v = entity:EstimateAbsVelocity()
local angle = v:Angles()
-- Play doesn't have a last angle
if lastAngles[idx] == nil then
lastAngles[idx] = angle
goto continue
end
-- Calculate the delta angle
if angle.y ~= lastAngles[idx].y then
local delta = Math.NormalizeAngle(angle.y - lastAngles[idx].y)
strafeAngles[idx] = math.clamp(delta, -5, 5)
end
lastAngles[idx] = angle
::continue::
end
end
-- Finds the best position for hitscan weapons
---@param me WPlayer
---@param weapon WWeapon
---@param player WPlayer
---@return AimTarget?
local function CheckHitscanTarget(me, weapon, player)
-- FOV Check
local aimPos = player:GetHitboxPos(options.AimPos.Hitscan)
if not aimPos then return nil end
local angles = Math.PositionAngles(me:GetEyePos(), aimPos)
local fov = Math.AngleFov(angles, engine.GetViewAngles())
-- Visiblity Check
if not Helpers.VisPos(player:Unwrap(), me:GetEyePos(), aimPos) then return nil end
-- The target is valid
local target = { entity = player, angles = angles, factor = fov }
return target
end
-- Finds the best position for projectile weapons
---@param me WPlayer
---@param weapon WWeapon
---@param player WPlayer
---@return AimTarget?
local function CheckProjectileTarget(me, weapon, player)
local projInfo = weapon:GetProjectileInfo()
if not projInfo then return nil end
local speed = projInfo[1]
local shootPos = me:GetEyePos() -- TODO: Add weapon offset
local aimPos = player:GetHitboxPos(options.AimPos.Projectile)
local aimOffset = aimPos - player:GetAbsOrigin()
--local aimOffset = Vector3()
-- Distance check
local maxDistance = options.PredTicks * speed
if me:DistTo(player) > maxDistance then return nil end
-- Visiblity Check
if not Helpers.VisPos(player:Unwrap(), shootPos, player:GetAbsOrigin()) then
return nil
end
-- Predict the player
local strafeAngle = options.StrafePrediction and strafeAngles[player:GetIndex()] or nil
local predData = Prediction.Player(player, options.PredTicks, strafeAngle)
if not predData then return nil end
-- Find a valid prediction
local targetAngles = nil
for i = 0, options.PredTicks do
local pos = predData.pos[i] + aimOffset
local solution = Math.SolveProjectile(shootPos, pos, projInfo[1], projInfo[2])
if not solution then goto continue end
-- Time check
local time = solution.time + latency + lerp
local ticks = Conversion.Time_to_Ticks(time) + 1
if ticks > i then goto continue end
-- Visiblity Check
if not Helpers.VisPos(player:Unwrap(), shootPos, pos) then
goto continue
end
-- The prediction is valid
targetAngles = solution.angles
break
-- TODO: FOV Check
::continue::
end
-- We didn't find a valid prediction
if not targetAngles then return nil end
-- Calculate the fov
local fov = Math.AngleFov(targetAngles, engine.GetViewAngles())
-- The target is valid
local target = { entity = player, angles = targetAngles, factor = fov }
return target
end
-- Checks the given target for the given weapon
---@param me WPlayer
---@param weapon WWeapon
---@param entity Entity
---@return AimTarget?
local function CheckTarget(me, weapon, entity)
if not entity then return nil end
if not entity:IsAlive() then return nil end
if entity:GetTeamNumber() == me:GetTeamNumber() then return nil end
local player = WPlayer.FromEntity(entity)
-- FOV check
local angles = Math.PositionAngles(me:GetEyePos(), player:GetAbsOrigin())
local fov = Math.AngleFov(angles, engine.GetViewAngles())
if fov > options.AimFov then return nil end
if weapon:IsShootingWeapon() then
-- TODO: Improve this
local projType = weapon:GetWeaponProjectileType()
if projType == 1 then
-- Hitscan weapon
return CheckHitscanTarget(me, weapon, player)
else
-- Projectile weapon
return CheckProjectileTarget(me, weapon, player)
end
elseif weapon:IsMeleeWeapon() then
-- TODO: Melee Aimbot
end
return nil
end
-- Returns the best target for the given weapon
---@param me WPlayer
---@param weapon WWeapon
---@return AimTarget? target
local function GetBestTarget(me, weapon)
local players = entities.FindByClass("CTFPlayer")
local bestTarget = nil
local bestFactor = math.huge
-- Check all players
for _, entity in pairs(players) do
local target = CheckTarget(me, weapon, entity)
if not target then goto continue end
-- Add valid target
if target.factor < bestFactor then
bestFactor = target.factor
bestTarget = target
end
-- TODO: Continue searching
break
::continue::
end
return bestTarget
end
---@param userCmd UserCmd
local function OnCreateMove(userCmd)
if not input.IsButtonDown(options.AimKey) then return end
local me = WPlayer.GetLocal()
if not me or not me:IsAlive() then return end
local weapon = me:GetActiveWeapon()
if not weapon then return end
-- Calculate strafe angles (optional)
if options.StrafePrediction then
CalcStrafe(me)
end
-- Check if we can shoot
--[[local flCurTime = globals.CurTime()
local canShoot = weapon:GetNextPrimaryAttack() <= flCurTime and me:GetNextAttack() <= flCurTime
if not canShoot then return end]]
-- Get current latency
local latIn, latOut = clientstate.GetLatencyIn(), clientstate.GetLatencyOut()
latency = (latIn or 0) + (latOut or 0)
-- Get current lerp
lerp = client.GetConVar("cl_interp") or 0
-- Get the best target
local currentTarget = GetBestTarget(me, weapon)
if not currentTarget then return end
-- Aim at the target
userCmd:SetViewAngles(currentTarget.angles:Unpack())
if not options.Silent then
engine.SetViewAngles(currentTarget.angles)
end
-- Auto Shoot
if options.AutoShoot then
if weapon:GetWeaponID() == TF_WEAPON_COMPOUND_BOW
or weapon:GetWeaponID() == TF_WEAPON_PIPEBOMBLAUNCHER then
-- Huntsman
if weapon:GetChargeBeginTime() > 0 then
userCmd.buttons = userCmd.buttons & ~IN_ATTACK
else
userCmd.buttons = userCmd.buttons | IN_ATTACK
end
else
-- Normal weapon
userCmd.buttons = userCmd.buttons | IN_ATTACK
end
end
end
local function OnDraw()
if not options.DebugInfo then return end
draw.SetFont(Fonts.Verdana)
draw.Color(255, 255, 255, 255)
-- Draw current latency and lerp
draw.Text(20, 140, string.format("Latency: %.2f", latency))
draw.Text(20, 160, string.format("Lerp: %.2f", lerp))
local me = WPlayer.GetLocal()
if not me or not me:IsAlive() then return end
local weapon = me:GetActiveWeapon()
if not weapon then return end
-- Draw current weapon
draw.Text(20, 180, string.format("Weapon: %s", weapon:GetName()))
draw.Text(20, 200, string.format("Weapon ID: %d", weapon:GetWeaponID()))
draw.Text(20, 220, string.format("Weapon DefIndex: %d", weapon:GetDefIndex()))
end
callbacks.Unregister("CreateMove", "LNX.Aimbot.CreateMove")
callbacks.Register("CreateMove", "LNX.Aimbot.CreateMove", OnCreateMove)
callbacks.Unregister("Draw", "LNX.Aimbot.Draw")
callbacks.Register("Draw", "LNX.Aimbot.Draw", OnDraw)