Skip to content

Instantly share code, notes, and snippets.

@modeco80
Created July 30, 2023 23:03
Show Gist options
  • Save modeco80/eb6576f4db3808f5490053584abbdcfb to your computer and use it in GitHub Desktop.
Save modeco80/eb6576f4db3808f5490053584abbdcfb to your computer and use it in GitHub Desktop.
A really shoddy antilag hook for Garry's Mod
--[[
(c) 2023 Lily Tsuru - feel free to use this in whatever
credit me though :(
This is a pretty meh antilag hook using the "recent"
physenv.GetLastSimulationTime() API.
This API was very poorly documented by Rubat (the programmer who added it),
so I figure I'll explain it here to *actually* document it, because I don't
personally think people should need RE experience to figure out what things do in this game:
In the Server DLL's PhysFrame function, there is code similar to
```cpp
float startTime = engine->Time();
///1 very expensive super physics later
g_PhysLastSimTime = engine->Time() - startTime;
```
This code calculates a delta time in microseconds (at least on Linux)
that tells how long the Server PhysFrame() function took to execute.
The newly added physenv.GetLastSimulationTime() API simply returns that calculated delta value.
Anyhow, documentation aside, this function can be used to create antilag hooks pretty easily!
]]
local server_lag_data = {
maybe_printed = false,
definitely_printed = false
}
function server_lag_data:Reset()
self.maybe_printed = false
self.definitely_printed = false
end
function server_lag_data:Tick(physms)
if physms > 10 then
if !self.maybe_printed then
print("Server might be lagging.")
self.maybe_printed = true
end
-- The server is definitely lagging. Log the delta that made us trip this branch
-- and then take action from there
if physms > 100 then
if !self.definitely_printed then
print(string.format("SERVER IS DEFINITELY LAGGING. Delta %.3f ms", physms))
-- replace this with something better (i.e: cleanup al propes of the player who created potentional problems)
RunConsoleCommand("gmod_admin_cleanup")
self.definitely_printed = true
end
end
else
if self.maybe_printed or self.definitely_printed then
print("Lag seemingly under control now.")
self:Reset()
end
end
end
hook.Add("Think", "__LILY_ANTILAG_TEST", function()
server_lag_data:Tick(physenv.GetLastSimulationTime() * 1000)
return nil -- let other think hooks run
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment