Skip to content

Instantly share code, notes, and snippets.

@meepen
Last active February 15, 2018 01:13
Show Gist options
  • Save meepen/bac76877c0fae52c9508cbf396ae92cf to your computer and use it in GitHub Desktop.
Save meepen/bac76877c0fae52c9508cbf396ae92cf to your computer and use it in GitHub Desktop.
Predicted TTT sprinting code (double tap W)
local WAIT_TIME = 0.2 -- seconds between w presses
local SPEED_MULT = 2
local STAMINA_REGEN_PER_SECOND = 0.1
local STAMINA_LOSS_PER_SECOND = 0.2
local sprinttime = function(ply, val)
if (val ~= nil) then
ply:SetNW2Float("SprintTime", val)
end
return ply:GetNW2Float("SprintTime", 0)
end
local sprintmode = function(ply, val)
if (val ~= nil) then
ply:SetNW2Bool("SprintMode", val)
end
return ply:GetNW2Bool("SprintMode", false)
end
local sprintstamina = function(ply, val)
if (val ~= nil) then
ply:SetNW2Float("SprintStaminaTime", CurTime())
ply:SetNW2Float("SprintStaminaPercent", val)
end
local time = ply:GetNW2Float("SprintStaminaTime", -math.huge)
local percent = ply:GetNW2Float("SprintStaminaPercent", 1)
return math.Clamp(percent + (CurTime() - time) * (sprintmode(ply) and -STAMINA_LOSS_PER_SECOND or STAMINA_REGEN_PER_SECOND), 0, 1)
end
hook.Add("TTTPlayerSpeedModifier", "Sprinter", function(ply, slowed, mv)
-- no need to override
if (sprintstamina(ply) <= 0 or slowed or not mv:KeyDown(IN_FORWARD)) then
-- reset if was running
if (sprintmode(ply)) then
sprintstamina(ply, sprintstamina(ply)) -- update time
sprinttime(ply, 0)
sprintmode(ply, false)
end
return
end
if (mv:KeyPressed(IN_FORWARD)) then
if (sprinttime(ply) > CurTime() - WAIT_TIME) then
sprintstamina(ply, sprintstamina(ply)) -- update time
sprinttime(ply, 0)
sprintmode(ply, true)
else
sprinttime(ply, CurTime())
end
end
if (sprintmode(ply)) then
return SPEED_MULT
end
end)
@mcd1992
Copy link

mcd1992 commented Feb 15, 2018

Wow, copy minecraft mucH!? I'm telling Notch, ur gettging DMCAd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment