-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsticky.lua
More file actions
247 lines (200 loc) · 8.03 KB
/
sticky.lua
File metadata and controls
247 lines (200 loc) · 8.03 KB
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
local enemy_only = true
local max_dist = 2800 -- hammer units
local box_3d = false -- Toggle for 3D box ESP
local box_2d = true -- Toggle for 2D box ESP
local box_color_visible = { 0, 255, 0, 255 } -- Green for visible stickies
local box_color_invisible = { 255, 0, 0, 255 } -- Red for not visible stickies
local box_2d_size = 20 -- Size of the 2D box in pixels
local boxOnlyWhenVisible = true
-- Chams settings
local chamsOn = false -- Disabled by default because of chams being bugged in lbox
local chamsOnlyWhenVisible = false
-- Cache system configuration
local visibilityCheckInterval = 0.1 -- Check visibility every 0.1 seconds
local cacheCleanupInterval = 1.0 -- Clean caches every 1.0 seconds
local nextCleanupTime = 0
local hitboxCacheLifetime = 0.2 -- Hitbox cache lifetime in seconds
-- Centralized cache tables
local visibilityCache = {}
local hitboxCache = {}
local cachedStickies = {}
local lastStickyUpdate = 0
local stickyUpdateInterval = 0.1
-- Cham material definitions
local visibleMaterial = materials.Create("VisibleSticky", [[
"UnlitGeneric"
{
$basetexture "vgui/white_additive"
$model "1"
$color2 "[0 1 0]"
$ignorez "0"
}
]])
local invisibleMaterial = materials.Create("InvisibleSticky", [[
"UnlitGeneric"
{
$basetexture "vgui/white_additive"
$model "1"
$color2 "[1 0 0]"
$ignorez "1"
}
]])
local function cleanCaches()
local currentTime = globals.RealTime()
if currentTime < nextCleanupTime then return end
for entityIndex, data in pairs(visibilityCache) do
if (currentTime - data.time) > visibilityCheckInterval * 2 then
visibilityCache[entityIndex] = nil
end
end
for entityIndex, data in pairs(hitboxCache) do
if (currentTime - data.time) > hitboxCacheLifetime * 2 then
hitboxCache[entityIndex] = nil
end
end
nextCleanupTime = currentTime + cacheCleanupInterval
end
local function IsVisible(entity, localPlayer)
if not entity or not localPlayer then return false end
local currentTime = globals.RealTime()
local entityIndex = entity:GetIndex()
if visibilityCache[entityIndex] and
(currentTime - visibilityCache[entityIndex].time) < visibilityCheckInterval then
return visibilityCache[entityIndex].visible
end
local source = localPlayer:GetAbsOrigin() + localPlayer:GetPropVector("localdata", "m_vecViewOffset[0]")
local targetPos = entity:GetAbsOrigin()
local trace = engine.TraceLine(source, targetPos, MASK_SHOT)
local isVisible = trace.fraction > 0.99 or trace.entity == entity
visibilityCache[entityIndex] = {
time = currentTime,
visible = isVisible
}
return isVisible
end
local function getHitboxWithCache(entity)
if not entity then return nil end
local currentTime = globals.RealTime()
local entityIndex = entity:GetIndex()
if hitboxCache[entityIndex] and
(currentTime - hitboxCache[entityIndex].time) < hitboxCacheLifetime then
return hitboxCache[entityIndex].hitbox
end
local hitbox = entity:HitboxSurroundingBox()
if hitbox then
hitboxCache[entityIndex] = {
time = currentTime,
hitbox = hitbox
}
end
return hitbox
end
local function draw_3d_box(vertices, color)
if #vertices < 8 then return end
draw.Color(table.unpack(color))
local edges = {
{1,2}, {2,3}, {3,4}, {4,1},
{5,6}, {6,7}, {7,8}, {8,5},
{1,5}, {2,6}, {3,7}, {4,8}
}
for _, edge in ipairs(edges) do
local v1, v2 = vertices[edge[1]], vertices[edge[2]]
if v1 and v2 then
draw.Line(v1.x, v1.y, v2.x, v2.y)
end
end
end
local function draw_2d_box(x, y, size, color)
draw.Color(table.unpack(color))
local half_size = size / 2
local x1, y1 = x - half_size, y - half_size
local x2, y2 = x + half_size, y + half_size
draw.Line(x1, y1, x2, y1)
draw.Line(x1, y2, x2, y2)
draw.Line(x1, y1, x1, y2)
draw.Line(x2, y1, x2, y2)
end
local function stickybomb_esp()
local currentTime = globals.RealTime()
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return end
if (currentTime - lastStickyUpdate) > stickyUpdateInterval then
cachedStickies = entities.FindByClass("CTFGrenadePipebombProjectile")
lastStickyUpdate = currentTime
end
cleanCaches()
for _, projectile in pairs(cachedStickies) do
if projectile:IsValid() and projectile:GetPropInt("m_iType") == 1 and not projectile:IsDormant() then
local projectile_pos = projectile:GetAbsOrigin()
if vector.Distance(localPlayer:GetAbsOrigin(), projectile_pos) > max_dist then
goto continue
end
if enemy_only and projectile:GetTeamNumber() == localPlayer:GetTeamNumber() then
goto continue
end
local projectile_screen = client.WorldToScreen(projectile_pos)
if not projectile_screen then goto continue end
local isVisible = IsVisible(projectile, localPlayer)
if boxOnlyWhenVisible and not isVisible then
goto continue
end
local color = isVisible and box_color_visible or box_color_invisible
if box_2d then
draw_2d_box(projectile_screen[1], projectile_screen[2], box_2d_size, color)
end
if box_3d then
local hitboxes = getHitboxWithCache(projectile)
if hitboxes then
local min, max = hitboxes[1], hitboxes[2]
local vertices = {
Vector3(min.x, min.y, min.z), Vector3(min.x, max.y, min.z),
Vector3(max.x, max.y, min.z), Vector3(max.x, min.y, min.z),
Vector3(min.x, min.y, max.z), Vector3(min.x, max.y, max.z),
Vector3(max.x, max.y, max.z), Vector3(max.x, min.y, max.z)
}
local screenVertices = {}
local allValid = true
for j, vertex in ipairs(vertices) do
local screenPos = client.WorldToScreen(vertex)
if screenPos then
screenVertices[j] = {x = screenPos[1], y = screenPos[2]}
else
allValid = false
break
end
end
if allValid then
draw_3d_box(screenVertices, color)
end
end
end
end
::continue::
end
end
local function onDrawModel(ctx)
if not chamsOn then return end
local entity = ctx:GetEntity()
if entity and entity:IsValid() and entity:GetClass() == "CTFGrenadePipebombProjectile" and entity:GetPropInt("m_iType") == 1 then
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return end
if enemy_only and entity:GetTeamNumber() == localPlayer:GetTeamNumber() then
ctx:ForcedMaterialOverride(nil)
return
end
local isVisible = IsVisible(entity, localPlayer)
if isVisible then
ctx:ForcedMaterialOverride(visibleMaterial)
else
if not chamsOnlyWhenVisible then
ctx:ForcedMaterialOverride(invisibleMaterial)
else
ctx:ForcedMaterialOverride(nil)
end
end
else
ctx:ForcedMaterialOverride(nil)
end
end
callbacks.Register("Draw", "stickybomb_esp", stickybomb_esp)
callbacks.Register("DrawModel", "StickyChams", onDrawModel)