Skip to content

Commit

Permalink
speed: Move logic to SPEED class & use moveData
Browse files Browse the repository at this point in the history
Sprinting is now signalled via the IN_BULLRUSH key, which seems to be
unused. This should ensure that sprinting is predicted properly, instead
of waiting on net messages to start / stop the sprinting
logic.
  • Loading branch information
saibotk committed Apr 24, 2023
1 parent 2c20a48 commit 54e33ec
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
21 changes: 10 additions & 11 deletions gamemodes/terrortown/gamemode/shared/sh_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TTT2ShopFallbackInitialized = false
local function TTT2RegisterSWEP(equipment, name, initialize)
local doHotreload = TTT2ShopFallbackInitialized

-- Handle first initialization or do hotreload
-- Handle first initialization or do hotreload
if initialize then
equipment = weapons.GetStored(name)
doHotreload = false
Expand Down Expand Up @@ -239,19 +239,18 @@ end
-- @realm shared
-- @ref https://wiki.facepunch.com/gmod/GM:Move
function GM:Move(ply, moveData)
SPEED:HandleSpeedCalculation(ply, moveData)

local mul = ply:GetSpeedMultiplier()
if client and client.isSprinting then
-- We abuse IN_BULLRUSH here to still be able to use our own binding system
moveData:AddKeys(IN_BULLRUSH)
end

if ply.sprintMultiplier and (ply.sprintProgress or 0) > 0 then
local sprintMultiplierModifier = {1}
if server and moveData:KeyDown(IN_BULLRUSH) then
ply.isSprinting = true
end

---
-- @realm shared
hook.Run("TTT2PlayerSprintMultiplier", ply, sprintMultiplierModifier)
SPEED:HandleSpeedCalculation(ply, moveData)

mul = mul * ply.sprintMultiplier * sprintMultiplierModifier[1]
end
local mul = ply:GetSpeedMultiplier()

moveData:SetMaxClientSpeed(moveData:GetMaxClientSpeed() * mul)
moveData:SetMaxSpeed(moveData:GetMaxSpeed() * mul)
Expand Down
8 changes: 8 additions & 0 deletions gamemodes/terrortown/gamemode/shared/sh_speed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ function SPEED:HandleSpeedCalculation(ply, moveData)
if IsValid(wep) and wep.GetIronsights and wep:GetIronsights() then
baseMultiplier = 120 / 220
isSlowed = true
elseif ply.isSprinting and (ply.sprintProgress or 0) > 0 then
local sprintMultiplierModifier = {1}

---
-- @realm shared
hook.Run("TTT2PlayerSprintMultiplier", ply, sprintMultiplierModifier)

baseMultiplier = (1 + GetGlobalFloat("ttt2_sprint_max", 0)) * sprintMultiplierModifier[1]
end

local speedMultiplierModifier = {1}
Expand Down
26 changes: 3 additions & 23 deletions gamemodes/terrortown/gamemode/shared/sh_sprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ local function PlayerSprint(trySprinting, moveKey)

local client = LocalPlayer()

if trySprinting and not GetGlobalBool("ttt2_sprint_enabled", true) then return end
if not GetGlobalBool("ttt2_sprint_enabled", true) then return end
if not trySprinting and not client.isSprinting or trySprinting and client.isSprinting then return end
if client.isSprinting and (client.moveKey and not moveKey or not client.moveKey and moveKey) then return end

client.oldSprintProgress = client.sprintProgress
client.sprintMultiplier = trySprinting and (1 + GetGlobalFloat("ttt2_sprint_max", 0)) or nil
client.isSprinting = trySprinting
client.moveKey = moveKey

net.Start("TTT2SprintToggle")
net.WriteBool(trySprinting)
net.SendToServer()
end

if SERVER then
util.AddNetworkString("TTT2SprintToggle")

-- Set ConVars

---
Expand Down Expand Up @@ -69,16 +61,6 @@ if SERVER then
cvars.AddChangeCallback(showCrosshair:GetName(), function(name, old, new)
SetGlobalBool(name, tobool(new))
end, "TTT2SprintCHChange")

net.Receive("TTT2SprintToggle", function(_, ply)
if not sprintEnabled:GetBool() or not IsValid(ply) then return end

local bool = net.ReadBool()

ply.oldSprintProgress = ply.sprintProgress
ply.sprintMultiplier = bool and (1 + maxSprintMul:GetFloat()) or nil
ply.isSprinting = bool
end)
else -- CLIENT
---
-- @realm client
Expand Down Expand Up @@ -172,15 +154,13 @@ function UpdateSprint()
-- @realm shared
hook.Run("TTT2StaminaRegen", ply, modifier)

ply.sprintProgress = math.min((ply.oldSprintProgress or 0) + FrameTime() * modifier[1] * GetGlobalFloat("ttt2_sprint_stamina_regeneration"), 1)
ply.oldSprintProgress = ply.sprintProgress
ply.sprintProgress = math.min((ply.sprintProgress or 0) + FrameTime() * modifier[1] * GetGlobalFloat("ttt2_sprint_stamina_regeneration"), 1)
elseif wantsToMove then
---
-- @realm shared
hook.Run("TTT2StaminaDrain", ply, modifier)

ply.sprintProgress = math.max((ply.oldSprintProgress or 0) - FrameTime() * modifier[1] * GetGlobalFloat("ttt2_sprint_stamina_consumption"), 0)
ply.oldSprintProgress = ply.sprintProgress
ply.sprintProgress = math.max((ply.sprintProgress or 0) - FrameTime() * modifier[1] * GetGlobalFloat("ttt2_sprint_stamina_consumption"), 0)
end
end
end
Expand Down

0 comments on commit 54e33ec

Please sign in to comment.