-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetterscope.lua
More file actions
344 lines (298 loc) · 14 KB
/
betterscope.lua
File metadata and controls
344 lines (298 loc) · 14 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
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
-- Global variables for materials and rendering
local ScopeRenderer = {
materials = {
texture = nil,
material = nil
},
screen = {
width = 0,
height = 0
},
zoom = {
base_fov = client.GetConVar("fov_desired") or 90,
offset = 0,
step = 2,
step_new = 1.75,
locked = false,
warning = {
alpha = 0,
fade_start = 0
}
},
view = {
custom = nil,
new_style = true
},
fonts = {
display = nil, -- Store font reference
},
lbox = {
noscope = gui.GetValue("No Scope") or 0,
sens = client.GetConVar("zoom_sensitivity_ratio") or 1,
},
settings = {
show_friendly = true, -- Toggle for friendly hitboxes
}
}
-- Team colors for ESP
local TEAM_COLORS = {
[2] = {r = 87, g = 160, b = 211, a = 100}, -- BLU
[3] = {r = 237, g = 84, b = 84, a = 100} -- RED
}
-- Calculate zoom sensitivity ratio based on FOVs
function ScopeRenderer:CalculateZoomSensitivityRatio(hipFOV, zoomFOV)
-- Formula: (tan(zoom_fov/2) * hip_fov) / (tan(hip_fov/2) * zoom_fov)
local zoomRad = math.rad(zoomFOV/2)
local hipRad = math.rad(hipFOV/2)
return (math.tan(zoomRad) * hipFOV) / (math.tan(hipRad) * zoomFOV)
end
-- Initialize materials and fonts only once when needed
function ScopeRenderer:Initialize()
-- Initialize font if not already done
if not self.fonts.display then
self.fonts.display = draw.CreateFont("Arial", 20, 800)
-- Set initial zoom sensitivity ratio for 90 FOV
client.SetConVar("zoom_sensitivity_ratio", ScopeRenderer.lbox.sens)
gui.SetValue("No Scope", 1)
end
-- Check if we need to create/update materials
if self.materials.material then
-- Update screen size if needed
local new_width, new_height = draw.GetScreenSize()
if new_width ~= self.screen.width or new_height ~= self.screen.height then
self.screen.width = new_width
self.screen.height = new_height
-- Recreate texture with new size
if self.materials.texture then
self.materials.texture = nil
end
self.materials.texture = materials.CreateTextureRenderTarget("scope_tex_persistent", self.screen.width, self.screen.height)
if not self.materials.texture then return false end
end
return true
end
-- First time initialization
self.screen.width, self.screen.height = draw.GetScreenSize()
-- Create fullscreen texture
local texture_name = "scope_tex_persistent"
self.materials.texture = materials.CreateTextureRenderTarget(texture_name, self.screen.width, self.screen.height)
if not self.materials.texture then return false end
-- Create persistent material
local material_kv = string.format([[
UnlitGeneric
{
$basetexture "%s"
$ignorez 1
$nofog 1
}
]], texture_name)
self.materials.material = materials.Create("scope_material_persistent", material_kv)
if not self.materials.material then return false end
return true
end
-- Check if player is scoped
function ScopeRenderer:IsScoped()
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return false end
local activeWeapon = localPlayer:GetPropEntity("m_hActiveWeapon")
if not activeWeapon then return false end
if activeWeapon:GetClass() ~= "CTFSniperRifle" then return false end
local chargedDamage = activeWeapon:GetPropFloat("SniperRifleLocalData", "m_flChargedDamage")
return chargedDamage > 0
end
-- Draw scope overlay and hitboxes
callbacks.Register("Draw", function()
-- Initialize local player first
local localPlayer = entities.GetLocalPlayer()
if not localPlayer then return end
-- Early exit conditions
if not ScopeRenderer:IsScoped() or
engine.Con_IsVisible() or
engine.IsGameUIVisible() or
input.IsButtonDown(KEY_ESCAPE) then return end
-- Ensure we have proper screen dimensions
ScopeRenderer.screen.width, ScopeRenderer.screen.height = draw.GetScreenSize()
-- Set default color state
draw.Color(255, 255, 255, 255)
-- Set color for scope lines
draw.Color(255, 255, 255, 255) -- White color for better visibility
-- Draw scope lines
draw.FilledRect(0, ScopeRenderer.screen.height/2, ScopeRenderer.screen.width, ScopeRenderer.screen.height/2 + 1)
draw.FilledRect(ScopeRenderer.screen.width/2, 0, ScopeRenderer.screen.width/2 + 1, ScopeRenderer.screen.height)
-- Draw current FOV and sensitivity ratio
draw.Color(255, 255, 255, 255)
draw.SetFont(ScopeRenderer.fonts.display) -- Use stored font
local currentZoomFOV = ScopeRenderer.zoom.base_fov + ScopeRenderer.zoom.offset
-- Calculate the ratio using the same formula we use to set it
local currentSensRatio = ScopeRenderer:CalculateZoomSensitivityRatio(ScopeRenderer.zoom.base_fov, currentZoomFOV)
local fovText = string.format("FOV: %.1f°", currentZoomFOV)
local sensText = string.format("Sens Ratio: %.6f", currentSensRatio)
local fov_w, fov_h = draw.GetTextSize(fovText)
local sens_w, sens_h = draw.GetTextSize(sensText)
-- Draw FOV and sensitivity info - ensure color is set before each draw
draw.Color(255, 255, 255, 255)
draw.Text(20, ScopeRenderer.screen.height - fov_h - sens_h - 20, fovText)
draw.Color(255, 255, 255, 255)
draw.Text(20, ScopeRenderer.screen.height - sens_h - 15, sensText)
-- Draw FOV and sensitivity info - ensure color is set before each draw
draw.Color(255, 255, 255, 255)
draw.Text(20, ScopeRenderer.screen.height - fov_h - sens_h - 20, fovText)
draw.Color(255, 255, 255, 255)
draw.Text(20, ScopeRenderer.screen.height - sens_h - 15, sensText)
-- Draw zoom lock warning
if ScopeRenderer.zoom.warning.alpha > 0 then
local current_time = globals.RealTime()
local time_since_warning = current_time - ScopeRenderer.zoom.warning.fade_start
if time_since_warning < 1.0 then
-- Ensure alpha never goes below 2
ScopeRenderer.zoom.warning.alpha = math.floor(math.max(2, 255 * (1.0 - time_since_warning)))
draw.Color(255, 0, 0, ScopeRenderer.zoom.warning.alpha)
draw.SetFont(ScopeRenderer.fonts.display) -- Use stored font
local warning_text = "Zoom Locked!"
local text_w, text_h = draw.GetTextSize(warning_text)
-- Reset color before text draw
draw.Color(255, 0, 0, ScopeRenderer.zoom.warning.alpha)
draw.Text(ScopeRenderer.screen.width - text_w - 20, ScopeRenderer.screen.height - text_h - 20, warning_text)
else
ScopeRenderer.zoom.warning.alpha = 2 -- Set to 2 instead of 0
end
end
-- Draw hitboxes
local players = entities.FindByClass("CTFPlayer")
local localPlayer = entities.GetLocalPlayer()
if localPlayer and ScopeRenderer.view.custom then
local localTeam = localPlayer:GetTeamNumber()
for _, player in pairs(players) do
if player:IsAlive() and not player:IsDormant() and player:GetIndex() ~= localPlayer:GetIndex() then
-- Skip friendly players if show_friendly is false
if not ScopeRenderer.settings.show_friendly and player:GetTeamNumber() == localTeam then return end
local hitboxes = player:GetHitboxes()
if hitboxes then
local headHitbox = hitboxes[1]
if headHitbox then
local mins = headHitbox[1]
local maxs = headHitbox[2]
local corners = {
Vector3(mins.x, mins.y, mins.z),
Vector3(maxs.x, mins.y, mins.z),
Vector3(mins.x, maxs.y, mins.z),
Vector3(maxs.x, maxs.y, mins.z),
Vector3(mins.x, mins.y, maxs.z),
Vector3(maxs.x, mins.y, maxs.z),
Vector3(mins.x, maxs.y, maxs.z),
Vector3(maxs.x, maxs.y, maxs.z)
}
local screenCorners = {}
local allCornersVisible = true
for _, corner in ipairs(corners) do
local screenPos = client.WorldToScreen(corner, ScopeRenderer.view.custom)
if not screenPos then
allCornersVisible = false
break
end
table.insert(screenCorners, screenPos)
end
if allCornersVisible then
local left = math.huge
local right = -math.huge
local top = math.huge
local bottom = -math.huge
for _, corner in ipairs(screenCorners) do
left = math.min(left, corner[1])
right = math.max(right, corner[1])
top = math.min(top, corner[2])
bottom = math.max(bottom, corner[2])
end
local teamColor = TEAM_COLORS[player:GetTeamNumber()]
if teamColor then
-- Ensure color is set before drawing with minimum alpha of 2
draw.Color(teamColor.r, teamColor.g, teamColor.b, math.max(2, teamColor.a))
if left and right and top and bottom then -- Make sure we have valid coordinates
-- Reset color before rect draw to ensure it's set
draw.Color(teamColor.r, teamColor.g, teamColor.b, math.max(2, teamColor.a))
draw.FilledRect(left, top, right, bottom)
end
end
end
end
end
end
end
end
end)
-- Block scroll wheel when scoped
callbacks.Register("SendStringCmd", function(cmd)
if ScopeRenderer:IsScoped() and (cmd:Get() == "invprev" or cmd:Get() == "invnext") then
return false
end
end)
-- Handle zoom controls
callbacks.Register("CreateMove", function(cmd)
if not ScopeRenderer:IsScoped() then return end
if ScopeRenderer.view.new_style then
if input.IsButtonPressed(113) then -- MWHEEL_DOWN
ScopeRenderer.zoom.locked = true
ScopeRenderer.zoom.warning.alpha = 255
ScopeRenderer.zoom.warning.fade_start = globals.RealTime()
elseif input.IsButtonPressed(112) then -- MWHEEL_UP
ScopeRenderer.zoom.locked = false
end
if not ScopeRenderer.zoom.locked then
if (cmd.buttons & IN_FORWARD) ~= 0 and (cmd.buttons & IN_MOVERIGHT) ~= 0 then
ScopeRenderer.zoom.offset = math.max(ScopeRenderer.zoom.offset - ScopeRenderer.zoom.step_new, -80)
end
if (cmd.buttons & IN_BACK) ~= 0 and (cmd.buttons & IN_MOVELEFT) ~= 0 then
ScopeRenderer.zoom.offset = math.min(ScopeRenderer.zoom.offset + ScopeRenderer.zoom.step_new, 30)
end
elseif ((cmd.buttons & IN_FORWARD) ~= 0 and (cmd.buttons & IN_MOVERIGHT) ~= 0) or
((cmd.buttons & IN_BACK) ~= 0 and (cmd.buttons & IN_MOVELEFT) ~= 0) then
ScopeRenderer.zoom.warning.alpha = 255
ScopeRenderer.zoom.warning.fade_start = globals.RealTime()
end
else
if input.IsButtonPressed(112) or input.IsButtonDown(112) then -- MWHEEL_UP
cmd.buttons = cmd.buttons & ~MOUSE_WHEEL_UP
ScopeRenderer.zoom.offset = math.max(ScopeRenderer.zoom.offset - ScopeRenderer.zoom.step, -10)
elseif input.IsButtonPressed(113) or input.IsButtonDown(113) then -- MWHEEL_DOWN
cmd.buttons = cmd.buttons & ~MOUSE_WHEEL_DOWN
ScopeRenderer.zoom.offset = math.min(ScopeRenderer.zoom.offset + ScopeRenderer.zoom.step, 120)
end
end
end)
-- Main render callback
callbacks.Register("PostRenderView", function(view)
if engine.Con_IsVisible() or engine.IsGameUIVisible() or input.IsButtonDown(KEY_ESCAPE) then return end
if not ScopeRenderer:IsScoped() then return end
if not ScopeRenderer:Initialize() then return end
-- Set up custom view with zoom FOV
local customView = view
customView.angles = engine.GetViewAngles()
local currentZoomFOV = ScopeRenderer.zoom.base_fov + ScopeRenderer.zoom.offset
customView.fov = currentZoomFOV
ScopeRenderer.view.custom = customView
-- Update zoom sensitivity based on our actual rendered FOV
local hipFOV = ScopeRenderer.zoom.base_fov
local newRatio = ScopeRenderer:CalculateZoomSensitivityRatio(hipFOV, currentZoomFOV)
client.SetConVar("zoom_sensitivity_ratio", tostring(newRatio))
-- Render the scene to our texture
render.Push3DView(customView, E_ClearFlags.VIEW_CLEAR_COLOR | E_ClearFlags.VIEW_CLEAR_DEPTH, ScopeRenderer.materials.texture)
render.ViewDrawScene(true, true, customView)
render.PopView()
-- Draw the texture to the screen
render.DrawScreenSpaceRectangle(
ScopeRenderer.materials.material,
0, 0,
ScopeRenderer.screen.width, ScopeRenderer.screen.height,
0, 0,
ScopeRenderer.screen.width, ScopeRenderer.screen.height,
ScopeRenderer.screen.width, ScopeRenderer.screen.height
)
end)
-- Clean up properly on unload
callbacks.Register("Unload", function()
-- We don't destroy materials anymore, they persist between script reloads
ScopeRenderer.view.custom = nil
-- Reset zoom sensitivity ratio to default TF2 sniper value on unload
client.SetConVar("zoom_sensitivity_ratio", ScopeRenderer.lbox.sens)
gui.SetValue("No Scope", ScopeRenderer.lbox.noscope)
end)