-- credits: drip
-- cache
local entity_get_local_player = entity.get_local_player
local entity_get_origin = entity.get_origin
local globals_tickinterval = globals.tickinterval
local math_floor = math.floor
local renderer_text = renderer.text
-- state variables
local prev_origins = {}
local teleport_distance, dist_broken = 0, 0
local lagcomp_break_time = nil
-- constants
local LAGCOMP_DISTANCE_THRESHOLD = 4096
local LAGCOMP_DISPLAY_DURATION = 2 -- seconds
-- math utilities
local vec_substract = function(a, b) return {a[1] - b[1], a[2] - b[2], a[3] - b[3] } end
local vec_lenght = function(x, y) return (x*x + y*y) end
-- main logic
client.set_event_callback("setup_command", function(cmd)
local me = entity_get_local_player()
local origin = {entity_get_origin(me) }
if prev_origins[me] == nil then
prev_origins[me] = origin
return
end
-- calculate movement data
local diff_origin = vec_substract(origin, prev_origins[me])
teleport_distance = vec_lenght(diff_origin[1], diff_origin[2])
prev_origins[me] = origin
-- detect lagcomp break
if teleport_distance >= LAGCOMP_DISTANCE_THRESHOLD then
dist_broken = teleport_distance
lagcomp_break_time = globals.realtime()
end
end)
-- display lagcomp break info
client.set_event_callback("paint", function()
if lagcomp_break_time == nil then return end
local time_since_break = globals.realtime() - lagcomp_break_time
if time_since_break > LAGCOMP_DISPLAY_DURATION then
lagcomp_break_time = nil
return
end
local screen_width, screen_height = client.screen_size()
local alpha = 255 * (1 - (time_since_break/LAGCOMP_DISPLAY_DURATION))
alpha = math.max(0, math.min(255, alpha))
renderer_text(
screen_width/2, screen_height - 80,
255, 0, 0, alpha,
"c", 0,
string.format("Lag Compensation Broken: %.2f units", dist_broken)
)
end)