Skip to content

Commit

Permalink
speed: Use NetworkVar for sprinting
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotk committed Apr 24, 2023
1 parent 54e33ec commit f51ae62
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gamemodes/terrortown/entities/weapons/weapon_tttbase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ if CLIENT then

local client = LocalPlayer()

if not enable_crosshair:GetBool() or not IsValid(client) or client.isSprinting and not GetGlobalBool("ttt2_sprint_crosshair", false) then return end
if not enable_crosshair:GetBool() or not IsValid(client) or client:GetSprintingPredicted() and not GetGlobalBool("ttt2_sprint_crosshair", false) then return end

local sights = not self.NoSights and self:GetIronsights()
local x = math.floor(ScrW() * 0.5)
Expand Down
9 changes: 0 additions & 9 deletions gamemodes/terrortown/gamemode/shared/sh_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,6 @@ end
-- @realm shared
-- @ref https://wiki.facepunch.com/gmod/GM:Move
function GM:Move(ply, moveData)
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 server and moveData:KeyDown(IN_BULLRUSH) then
ply.isSprinting = true
end

SPEED:HandleSpeedCalculation(ply, moveData)

local mul = ply:GetSpeedMultiplier()
Expand Down
23 changes: 23 additions & 0 deletions gamemodes/terrortown/gamemode/shared/sh_player_ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ if not plymeta then
return
end

---
-- Dummy functions that will be replaced when SetupDataTables runs. These are
-- here for when that does not happen (due to e.g. stacking base classes)
-- @return[default=false] boolean
-- @realm shared
function plymeta:GetSprintingPredicted()
return false
end

---
-- Dummy functions that will be replaced when SetupDataTables runs. These are
-- here for when that does not happen (due to e.g. stacking base classes)
-- @realm shared
function plymeta:SetSprintingPredicted()

end

function plymeta:SetupDataTables()
self:NetworkVar("Bool", 3, "SprintingPredicted")

plymeta.SetupDataTables(self)
end

---
-- Checks whether a player is a available terrorist (not a spectator)
-- @return boolean
Expand Down
2 changes: 1 addition & 1 deletion gamemodes/terrortown/gamemode/shared/sh_speed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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
elseif ply:GetSprintingPredicted() and (ply.sprintProgress or 0) > 0 then
local sprintMultiplierModifier = {1}

---
Expand Down
17 changes: 9 additions & 8 deletions gamemodes/terrortown/gamemode/shared/sh_sprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ local function PlayerSprint(trySprinting, moveKey)
if SERVER then return end

local client = LocalPlayer()
local isSprinting = client:GetSprintingPredicted()

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
if not trySprinting and not isSprinting or trySprinting and isSprinting then return end
if isSprinting and (client.moveKey and not moveKey or not client.moveKey and moveKey) then return end

client.isSprinting = trySprinting
client:SetSprintingPredicted(trySprinting)
client.moveKey = moveKey
end

Expand Down Expand Up @@ -80,7 +81,7 @@ else -- CLIENT
-- @realm client
function UpdateInputSprint(ply, key, pressed)
if pressed then
if ply.isSprinting or not enable_doubletap_sprint:GetBool() or ply.preventSprint then return end
if ply:GetSprintingPredicted() or not enable_doubletap_sprint:GetBool() or ply.preventSprint then return end

local time = CurTime()

Expand All @@ -91,7 +92,7 @@ else -- CLIENT
lastPressedMoveKey = key
lastPress = time
else
if not ply.isSprinting then return end
if not ply:GetSprintingPredicted() then return end

local moveKey = ply.moveKey
local wantsToMove = ply:KeyDown(IN_FORWARD) or ply:KeyDown(IN_BACK) or ply:KeyDown(IN_MOVERIGHT) or ply:KeyDown(IN_MOVELEFT)
Expand Down Expand Up @@ -133,8 +134,8 @@ function UpdateSprint()

local wantsToMove = ply:KeyDown(IN_FORWARD) or ply:KeyDown(IN_BACK) or ply:KeyDown(IN_MOVERIGHT) or ply:KeyDown(IN_MOVELEFT)

if ply.sprintProgress == 1 and (not ply.isSprinting or not wantsToMove) then continue end
if ply.sprintProgress == 0 and ply.isSprinting and wantsToMove then
if ply.sprintProgress == 1 and (not ply:GetSprintingPredicted() or not wantsToMove) then continue end
if ply.sprintProgress == 0 and ply:GetSprintingPredicted() and wantsToMove then
ply.sprintResetDelayCounter = ply.sprintResetDelayCounter + FrameTime()

-- If the player keeps sprinting even though they have no stamina, start refreshing stamina after 1.5 seconds automatically
Expand All @@ -149,7 +150,7 @@ function UpdateSprint()

local modifier = {1} -- Multiple hooking support

if not ply.isSprinting or not wantsToMove then
if not ply:GetSprintingPredicted() or not wantsToMove then
---
-- @realm shared
hook.Run("TTT2StaminaRegen", ply, modifier)
Expand Down

0 comments on commit f51ae62

Please sign in to comment.