Skip to content

Commit

Permalink
Predicted FOV changes: Made FOV calls shared (#1394)
Browse files Browse the repository at this point in the history
The recently added dynamic FOV is really choppy on some servers with
high ping or low tickrate. To mitigate this issue, I made everything
related to dynamic FOV shared.
  • Loading branch information
TimGoll authored Feb 8, 2024
1 parent 5680c36 commit 98900c7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
7 changes: 7 additions & 0 deletions gamemodes/terrortown/gamemode/client/cl_player_ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,19 @@ function plymeta:SetSettingOnServer(identifier, value)
return
end

local oldValue = self.playerSettings[identifier]

self.playerSettings[identifier] = value

net.Start("ttt2_set_player_setting")
net.WriteString(identifier)
net.WriteString(tostring(value))
net.SendToServer()

---
-- @realm shared
-- stylua: ignore
hook.Run("TTT2PlayerSettingChanged", self, identifier, oldValue, self.playerSettings[identifier])
end

local airtime = 0
Expand Down
41 changes: 1 addition & 40 deletions gamemodes/terrortown/gamemode/server/sv_player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,6 @@ CreateConVar("ttt_killer_dna_basetime", "100", {FCVAR_NOTIFY, FCVAR_ARCHIVE})
util.AddNetworkString("ttt2_damage_received")
util.AddNetworkString("ttt2_set_player_setting")

---
-- Update the sprinting FOV on the player if the setting is enabled.
-- @realm server
function plymeta:UpdateSprintingFOV()
local mul = self:GetSpeedMultiplier() * SPRINT:HandleSpeedMultiplierCalculation(self)

if not self:GetPlayerSetting("enable_dynamic_fov") then
return
end

local newFOV = (self:GetPlayerSetting("fov_desired") or 85) * mul ^ (1 / 6)

if self.lastFOV ~= newFOV then
self.lastFOV = newFOV

self:SetFOV(newFOV, 0.25, nil, true)
end
end

---
-- First spawn on the server.
-- Called when the @{Player} spawns for the first time.
Expand Down Expand Up @@ -1514,7 +1495,7 @@ net.Receive("ttt2_set_player_setting", function(_, ply)
end

---
-- @realm server
-- @realm shared
-- stylua: ignore
hook.Run("TTT2PlayerSettingChanged", ply, identifier, oldValue, ply.playerSettings[identifier])
end)
Expand Down Expand Up @@ -1671,23 +1652,3 @@ function GM:AllowPVP()

return rs ~= ROUND_PREP and (rs ~= ROUND_POST or ttt_postdm:GetBool())
end

---
-- A hook that is called on change of a player setting on the server.
-- @param Player ply The player whose setting was changed
-- @param string identifier The setting's identifier
-- @param any oldValue The old value of the setting
-- @param any newValue The new value of the settings
-- @hook
-- @realm server
function GM:TTT2PlayerSettingChanged(ply, identifier, oldValue, newValue)
if IsValid(ply) and identifier == "enable_dynamic_fov" then
if newValue then
ply:UpdateSprintingFOV()
else
ply.lastFOV = 0

ply:SetFOV(0, 0.25, nil, true)
end
end
end
4 changes: 1 addition & 3 deletions gamemodes/terrortown/gamemode/shared/sh_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,7 @@ function GM:Move(ply, moveData)
moveData:SetMaxClientSpeed(moveData:GetMaxClientSpeed() * mul)
moveData:SetMaxSpeed(moveData:GetMaxSpeed() * mul)

if SERVER then
ply:UpdateSprintingFOV()
end
ply:UpdateSprintingFOV()
end

-- @param Player ply The player
Expand Down
39 changes: 39 additions & 0 deletions gamemodes/terrortown/gamemode/shared/sh_player_ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,45 @@ function plymeta:GetPlayerSetting(identifier)
return self.playerSettings and self.playerSettings[identifier]
end

---
-- Update the sprinting FOV on the player if the setting is enabled.
-- @realm shared
function plymeta:UpdateSprintingFOV()
local mul = self:GetSpeedMultiplier() * SPRINT:HandleSpeedMultiplierCalculation(self)

if not self:GetPlayerSetting("enable_dynamic_fov") then
return
end

local newFOV = (self:GetPlayerSetting("fov_desired") or 85) * mul ^ (1 / 6)

if self.lastFOV ~= newFOV then
self.lastFOV = newFOV

self:SetFOV(newFOV, 0.25, nil, true)
end
end

---
-- A hook that is called on change of a player setting on the server.
-- @param Player ply The player whose setting was changed
-- @param string identifier The setting's identifier
-- @param any oldValue The old value of the setting
-- @param any newValue The new value of the settings
-- @hook
-- @realm shared
function GM:TTT2PlayerSettingChanged(ply, identifier, oldValue, newValue)
if IsValid(ply) and identifier == "enable_dynamic_fov" then
if newValue then
ply:UpdateSprintingFOV()
else
ply.lastFOV = 0

ply:SetFOV(0, 0.25, nil, true)
end
end
end

---
-- A hook that is called on the change of a role. It is called once for the old role
-- and once for the new role if some criteria are met.
Expand Down

0 comments on commit 98900c7

Please sign in to comment.