-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathammo.lua
440 lines (361 loc) · 14.3 KB
/
ammo.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
-- Cache commonly used functions
local FIND_BY_CLASS = entities.FindByClass
local WORLD_TO_SCREEN = client.WorldToScreen
local COLOR = draw.Color
local LINE = draw.Line
local TRACE_LINE = engine.TraceLine
local CUR_TIME = globals.CurTime
local POLYGON = draw.TexturedPolygon
local CROSS = (function(a, b, c) return (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]) end)
local VECTOR_LENGTH = (function(v1, v2) return math.sqrt((v2.x - v1.x) ^ 2 + (v2.y - v1.y) ^ 2 + (v2.z - v1.z) ^ 2) end)
local TICK_COUNT = globals.TickCount
local floor = math.floor
local min = math.min
local max = math.max
local cos = math.cos
local sin = math.sin
local rad = math.rad
local format = string.format
-- Track initialization state
local initialized = false
local textures = {
clock = nil,
health = nil,
ammo = nil
}
-- Add update interval control
local lastUpdateTick = 0
local UPDATE_INTERVAL = 2 -- Update positions every 2 ticks
-- Optimization: Add caches
local nextVisCheck = {}
local visibilityCache = {}
local modelCache = {}
local vecCache = {
eyePos = Vector3(0, 0, 0),
viewOffset = Vector3(0, 0, 0)
}
-- Debug mode
local DEBUG = false
-- Configuration (all RGB/Alpha values are 0-255)
local config = {
-- Toggles
showGroundPieChart = false,
showScreenPieChart = true,
showSeconds = false,
showMilliseconds = false,
scaleWithDistance = true,
scaleTextWithDistance = true,
-- Distances
maxDistances = {
groundPieChart = 1000,
screenPieChart = 1000,
text = 1000
},
-- Sizes and segments
pieChartSize = 20,
pieChartSegments = 32,
minScreenChartSize = 5,
maxScreenChartSize = 20,
maxScaleDistance = 1000,
-- Text sizes
textSize = {
min = 12,
max = 20,
default = 16
},
-- Colors
healthColor = {
r = 155, g = 200, b = 155,
polygonAlpha = 200
},
ammoColor = {
r = 255, g = 255, b = 155,
polygonAlpha = 200
},
secondsColor = {
r = 255, g = 255, b = 255,
alpha = 255
}
}
-- Font variables
local font = nil
local fontCache = {}
-- Standard respawn time
local RESPAWN_TIME = 10.0
-- Store initial positions of supplies
local supplyPositions = {}
-- Safe font creation
local function CreateFonts()
if not font then
font = draw.CreateFont("Verdana", config.textSize.default, 800)
end
end
-- Safe texture creation
local function CreateTextures()
if not textures.clock then
textures.clock = draw.CreateTextureRGBA(string.char(255, 255, 255, 255), 1, 1)
end
if not textures.health then
textures.health = draw.CreateTextureRGBA(string.char(
config.healthColor.r, config.healthColor.g, config.healthColor.b, config.healthColor.polygonAlpha,
config.healthColor.r, config.healthColor.g, config.healthColor.b, config.healthColor.polygonAlpha,
config.healthColor.r, config.healthColor.g, config.healthColor.b, config.healthColor.polygonAlpha,
config.healthColor.r, config.healthColor.g, config.healthColor.b, config.healthColor.polygonAlpha
), 2, 2)
end
if not textures.ammo then
textures.ammo = draw.CreateTextureRGBA(string.char(
config.ammoColor.r, config.ammoColor.g, config.ammoColor.b, config.ammoColor.polygonAlpha,
config.ammoColor.r, config.ammoColor.g, config.ammoColor.b, config.ammoColor.polygonAlpha,
config.ammoColor.r, config.ammoColor.g, config.ammoColor.b, config.ammoColor.polygonAlpha,
config.ammoColor.r, config.ammoColor.g, config.ammoColor.b, config.ammoColor.polygonAlpha
), 2, 2)
end
end
-- Initialize resources safely
local function Initialize()
if initialized then return end
-- Only initialize if we're in game
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return end
CreateFonts()
CreateTextures()
initialized = true
end
-- Safe cleanup
local function Cleanup()
if textures.clock then
draw.DeleteTexture(textures.clock)
textures.clock = nil
end
if textures.health then
draw.DeleteTexture(textures.health)
textures.health = nil
end
if textures.ammo then
draw.DeleteTexture(textures.ammo)
textures.ammo = nil
end
fontCache = {}
nextVisCheck = nil
visibilityCache = nil
modelCache = nil
vecCache = nil
supplyPositions = {}
initialized = false
end
-- Cache fonts for different sizes
local function GetFont(size)
size = math.floor(size)
if not fontCache[size] then
fontCache[size] = draw.CreateFont("Verdana", size, 800)
end
return fontCache[size]
end
-- Optimization: Cached model check
local function GetPickupType(entity)
if not entity then return nil end
local model = entity:GetModel()
if not model then return nil end
local modelName = models.GetModelName(model)
if not modelName then return nil end
if modelCache[modelName] then
return modelCache[modelName]
end
modelName = string.lower(modelName)
if string.find(modelName, "ammopack") then
modelCache[modelName] = "ammo"
return "ammo"
elseif string.find(modelName, "medkit") or string.find(modelName, "healthkit") then
modelCache[modelName] = "health"
return "health"
end
modelCache[modelName] = nil
return nil
end
-- Calculate screen chart size
local function GetScreenChartSize(distance)
if not config.scaleWithDistance then
return config.pieChartSize
end
local scale = 1 - math.min(distance / config.maxScaleDistance, 1)
local size = config.minScreenChartSize + (config.maxScreenChartSize - config.minScreenChartSize) * scale
return math.max(config.minScreenChartSize, math.min(config.maxScreenChartSize, size))
end
-- Get text size
local function GetTextSize(distance)
if not config.scaleTextWithDistance then
return config.textSize.default
end
local scale = 1 - math.min(distance / config.maxScaleDistance, 1)
return math.max(config.textSize.min,
math.min(config.textSize.max,
config.textSize.min + (config.textSize.max - config.textSize.min) * scale))
end
-- Function to create a filled clock polygon
local function DrawClockFill(centerX, centerY, radius, percentage, vertices, colorTable)
if percentage <= 0 or percentage > 1 then return end
local points = {}
table.insert(points, {centerX, centerY, 0, 0})
local startAngle = -90
local endAngle = startAngle + (360 * percentage)
local step = (endAngle - startAngle) / vertices
for i = 0, vertices do
local angle = rad(startAngle + (i * step))
local x = centerX + cos(angle) * radius
local y = centerY + sin(angle) * radius
table.insert(points, {x, y, 0, 0})
end
COLOR(colorTable.r, colorTable.g, colorTable.b, colorTable.polygonAlpha)
POLYGON(textures.clock, points)
end
-- Ground polygon drawing
local function DrawFilledProgress(pos, radius, percentage, segments, colorTable, fillTexture)
local positions = {}
local screenPositions = {}
local offsetZ = 1
for i = 1, segments do
local ang = i * (2 * math.pi / segments)
local worldPos = Vector3(
pos.x + radius * cos(ang),
pos.y + radius * sin(ang),
pos.z + offsetZ
)
local screenPos = WORLD_TO_SCREEN(worldPos)
if screenPos then
table.insert(positions, screenPos)
else
return
end
end
local centerScreen = WORLD_TO_SCREEN(Vector3(pos.x, pos.y, pos.z + offsetZ))
if not centerScreen then return end
local visibleVerts = floor((1 - percentage) * segments)
if visibleVerts <= 0 then return end
local coords = {}
local reverseCoords = {}
local totalVerts = visibleVerts + 1
table.insert(coords, {centerScreen[1], centerScreen[2], 0, 0})
reverseCoords[totalVerts] = {centerScreen[1], centerScreen[2], 0, 0}
for i = 1, visibleVerts do
local pos = positions[i]
local coordEntry = {pos[1], pos[2], 0, 0}
table.insert(coords, coordEntry)
reverseCoords[totalVerts - i] = coordEntry
end
COLOR(colorTable.r, colorTable.g, colorTable.b, colorTable.polygonAlpha)
local sum = 0
for i = 1, #coords - 1 do
sum = sum + CROSS(coords[i], coords[i + 1], coords[1])
end
POLYGON(fillTexture, sum < 0 and reverseCoords or coords, true)
end
-- Optimization: Cached visibility check
local function IsVisible(pos)
local id = math.floor(pos.x) .. math.floor(pos.y) .. math.floor(pos.z)
local curTick = TICK_COUNT()
if nextVisCheck[id] and curTick < nextVisCheck[id] then
return visibilityCache[id]
end
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return false end
vecCache.viewOffset = localPlayer:GetPropVector("localdata", "m_vecViewOffset[0]")
vecCache.eyePos = localPlayer:GetAbsOrigin()
vecCache.eyePos.x = vecCache.eyePos.x + vecCache.viewOffset.x
vecCache.eyePos.y = vecCache.eyePos.y + vecCache.viewOffset.y
vecCache.eyePos.z = vecCache.eyePos.z + vecCache.viewOffset.z
local trace = TRACE_LINE(vecCache.eyePos, pos, MASK_SHOT_HULL)
visibilityCache[id] = trace.fraction > 0.99
nextVisCheck[id] = curTick + 3
return visibilityCache[id]
end
-- Update supply positions
local function UpdateSupplyPositions()
local currentTick = TICK_COUNT()
if currentTick - lastUpdateTick < UPDATE_INTERVAL then return end
lastUpdateTick = currentTick
local currentSupplies = {}
local currentTime = CUR_TIME()
local entities = FIND_BY_CLASS("CBaseAnimating")
for _, entity in pairs(entities) do
if entity:IsValid() and not entity:IsDormant() then
local pickupType = GetPickupType(entity)
if pickupType then
local pos = entity:GetAbsOrigin()
local key = format("%.0f_%.0f_%.0f", pos.x, pos.y, pos.z)
currentSupplies[key] = true
if not supplyPositions[key] then
supplyPositions[key] = {
pos = pos,
type = pickupType,
respawning = false,
disappearTime = 0
}
end
end
end
end
for key, info in pairs(supplyPositions) do
if not currentSupplies[key] and not info.respawning then
info.respawning = true
info.disappearTime = currentTime
elseif currentSupplies[key] and info.respawning then
info.respawning = false
info.disappearTime = 0
end
end
end
-- Main ESP callback
callbacks.Register("Draw", "SupplyRespawnESP", function()
-- Initialize resources if needed
Initialize()
-- Don't proceed if not initialized or game UI is visible
if not initialized or engine.Con_IsVisible() or engine.IsGameUIVisible() then
return
end
local localPlayer = entities.GetLocalPlayer()
if not localPlayer or not localPlayer:IsAlive() then
return
end
draw.SetFont(font)
UpdateSupplyPositions()
local currentTime = CUR_TIME()
local playerPos = localPlayer:GetAbsOrigin()
for _, info in pairs(supplyPositions) do
if info.respawning and IsVisible(info.pos) then
local screenPos = WORLD_TO_SCREEN(info.pos)
if screenPos then
local timeLeft = math.max(0, RESPAWN_TIME - (currentTime - info.disappearTime))
local timePercent = timeLeft / RESPAWN_TIME
local colorConfig = info.type == "health" and config.healthColor or config.ammoColor
local fillTexture = info.type == "health" and textures.health or textures.ammo
local distance = VECTOR_LENGTH(playerPos, info.pos)
if config.showGroundPieChart and distance <= config.maxDistances.groundPieChart then
DrawFilledProgress(info.pos, config.pieChartSize, timePercent, config.pieChartSegments, colorConfig, fillTexture)
end
if config.showScreenPieChart and distance <= config.maxDistances.screenPieChart then
local screenChartSize = GetScreenChartSize(distance)
DrawClockFill(screenPos[1], screenPos[2], screenChartSize, timePercent, config.pieChartSegments, colorConfig)
end
if config.showSeconds and distance <= config.maxDistances.text then
local timerText = config.showMilliseconds and
string.format("%.1fs", timeLeft) or
string.format("%ds", math.ceil(timeLeft))
local scaledFont = GetFont(GetTextSize(distance))
draw.SetFont(scaledFont)
local width = select(1, draw.GetTextSize(timerText)) or 0
COLOR(
config.secondsColor.r,
config.secondsColor.g,
config.secondsColor.b,
config.secondsColor.alpha
)
draw.TextShadow(math.floor(screenPos[1] - width/2), math.floor(screenPos[2] - 15), timerText)
draw.SetFont(font)
end
end
end
end
end)
-- Cleanup on unload
callbacks.Register("Unload", Cleanup)