Skip to content

Commit

Permalink
Voice Chat: Added module for battery drain and bar to UI (#1536)
Browse files Browse the repository at this point in the history
The voice battery drain was previously removed from the UI in the voice
rework. It is now added back to the UI. While doing so I moved all
convars and functions to a new module called `voicebattery`. That way I
was able to remove the global bools and replace them with with
replicated convars.


![image](https://github.com/TTT-2/TTT2/assets/13639408/5052f54c-11b1-4197-b6f1-0678951fb45f)

I'm not sure that I'm a fan of the voice battery being client only. This
seems like a bad idea to me. But I wanted to keep it that way for now,
we might want to redo it at some point.
  • Loading branch information
TimGoll authored May 31, 2024
1 parent 6076978 commit 93ab9ef
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 122 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
- Added a submenu to issue basic commands
- Added a new `gameloop` module that contains all functions related to the round structure (by @Tim Goll)
- Added a loadingscreen that hides the visible and audible lag introduced by the map cleanup on round change (by @TimGoll)
- Added a voicebattery module that handles the voice battery (by @TimGoll)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion gamemodes/terrortown/gamemode/client/cl_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ function GM:ClearClientState()

client:SetTargetPlayer(nil)

VOICE.InitBattery()
voicebattery.InitBattery()

local plys = playerGetAll()

Expand Down
74 changes: 7 additions & 67 deletions gamemodes/terrortown/gamemode/client/cl_voice.lua
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,6 @@ function VOICE.CycleMuteState(force_state)
return mute_state
end

VOICE.battery_max = 100
VOICE.battery_min = 10

---
-- Scales a linear volume into a Power 4 value.
-- @param number volume
Expand Down Expand Up @@ -614,44 +611,12 @@ function VOICE.UpdatePlayerVoiceVolume(ply)
end

---
-- Initializes the voice battery
-- Checks if a player is using the role/team voice chat. This is not the global
-- voice chat.
-- @param player ply The player to check
-- @return boolean Returns true if the player is using the role voice chat
-- @realm client
function VOICE.InitBattery()
LocalPlayer().voice_battery = VOICE.battery_max
end

local function GetRechargeRate()
local r = GetGlobalFloat("ttt_voice_drain_recharge", 0.05)

if LocalPlayer().voice_battery < VOICE.battery_min then
r = r * 0.5
end

return r
end

local function GetDrainRate()
local ply = LocalPlayer()

if
not IsValid(ply)
or ply:IsSpec()
or not GetGlobalBool("ttt_voice_drain", false)
or gameloop.GetRoundState() ~= ROUND_ACTIVE
then
return 0
end

local plyRoleData = ply:GetSubRoleData()

if ply:IsAdmin() or (plyRoleData.isPublicRole and plyRoleData.isPolicingRole) then
return GetGlobalFloat("ttt_voice_drain_admin", 0)
else
return GetGlobalFloat("ttt_voice_drain_normal", 0)
end
end

local function IsRoleChatting(ply)
function VOICE.IsRoleChatting(ply)
local plyTeam = ply:GetTeam()
local plyRoleData = ply:GetSubRoleData()

Expand All @@ -664,31 +629,6 @@ local function IsRoleChatting(ply)
and not ply[plyTeam .. "_gvoice"]
end

---
-- Updates the voice battery
-- @note Called every @{GM:Tick}
-- @realm client
-- @internal
function VOICE.Tick()
if not GetGlobalBool("ttt_voice_drain", false) then
return
end

local client = LocalPlayer()

if VOICE.IsSpeaking() and not IsRoleChatting(client) then
client.voice_battery = client.voice_battery - GetDrainRate()

if not VOICE.CanSpeak() then
client.voice_battery = 0

permissions.EnableVoiceChat(false)
end
elseif client.voice_battery < VOICE.battery_max then
client.voice_battery = client.voice_battery + GetRechargeRate()
end
end

---
-- Returns whether a @{Player} is speaking
-- @note @{Player:IsSpeaking} does not work for local @{Player}
Expand Down Expand Up @@ -720,13 +660,13 @@ function VOICE.CanSpeak()
return false
end

if not GetGlobalBool("ttt_voice_drain", false) then
if not voicebattery.IsEnabled() then
return true
end

local client = LocalPlayer()

return client.voice_battery > VOICE.battery_min or IsRoleChatting(client)
return voicebattery.IsCharged() or VOICE.IsRoleChatting(client)
end

---
Expand Down
41 changes: 0 additions & 41 deletions gamemodes/terrortown/gamemode/server/sv_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,6 @@ local idle_enabled = CreateConVar("ttt_idle", "1", {FCVAR_NOTIFY, FCVAR_ARCHIVE}
-- stylua: ignore
local idle_time = CreateConVar("ttt_idle_limit", "180", {FCVAR_NOTIFY, FCVAR_ARCHIVE})

---
-- @realm server
-- stylua: ignore
local voice_drain = CreateConVar("ttt_voice_drain", "0", {FCVAR_NOTIFY, FCVAR_ARCHIVE})

---
-- @realm server
-- stylua: ignore
local voice_drain_normal = CreateConVar("ttt_voice_drain_normal", "0.2", {FCVAR_NOTIFY, FCVAR_ARCHIVE})

---
-- @realm server
-- stylua: ignore
local voice_drain_admin = CreateConVar("ttt_voice_drain_admin", "0.05", {FCVAR_NOTIFY, FCVAR_ARCHIVE})

---
-- @realm server
-- stylua: ignore
local voice_drain_recharge = CreateConVar("ttt_voice_drain_recharge", "0.05", {FCVAR_NOTIFY, FCVAR_ARCHIVE})

---
-- @realm server
-- stylua: ignore
Expand Down Expand Up @@ -499,11 +479,6 @@ function GM:SyncGlobals()
SetGlobalInt(idle_time:GetName(), idle_time:GetInt())
SetGlobalBool(idle_enabled:GetName(), idle_enabled:GetBool())

SetGlobalBool(voice_drain:GetName(), voice_drain:GetBool())
SetGlobalFloat(voice_drain_normal:GetName(), voice_drain_normal:GetFloat())
SetGlobalFloat(voice_drain_admin:GetName(), voice_drain_admin:GetFloat())
SetGlobalFloat(voice_drain_recharge:GetName(), voice_drain_recharge:GetFloat())

local rlsList = roles.GetList()

for i = 1, #rlsList do
Expand Down Expand Up @@ -531,22 +506,6 @@ cvars.AddChangeCallback(idle_enabled:GetName(), function(cv, old, new)
SetGlobalBool(idle_enabled:GetName(), tobool(tonumber(new)))
end)

cvars.AddChangeCallback(voice_drain:GetName(), function(cv, old, new)
SetGlobalBool(voice_drain:GetName(), tobool(tonumber(new)))
end)

cvars.AddChangeCallback(voice_drain_normal:GetName(), function(cv, old, new)
SetGlobalFloat(voice_drain_normal:GetName(), tonumber(new))
end)

cvars.AddChangeCallback(voice_drain_admin:GetName(), function(cv, old, new)
SetGlobalFloat(voice_drain_admin:GetName(), tonumber(new))
end)

cvars.AddChangeCallback(voice_drain_recharge:GetName(), function(cv, old, new)
SetGlobalFloat(voice_drain_recharge:GetName(), tonumber(new))
end)

---
-- This @{function} is used to load the shop equipments
-- @realm server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,24 @@ if CLIENT then
local heightBar = h - 2 * self.padding
local wNick = w - h - self.padding

local offset = 3 * self.scale
local heightBatteryBar = 2 * self.scale

draw.Box(xPos + self.padding, yPos + self.padding, w - self.padding, heightBar, color)
self:DrawLines(
xPos + self.padding,
yPos + self.padding,
w - self.padding,
heightBar,
color.a
)

for i = 1, #data do
local yValue = math.floor(data[i] * 0.5 * heightBar - 4 * self.scale)

draw.Box(
xPos + h + 3 + (i - 1) * (widthBar + 1),
xPos + h + offset + (i - 1) * (widthBar + 1),
yPos + 0.5 * h - yValue - 1,
widthBar,
yValue,
colorVoiceBar
)
if yValue > 1 then
draw.Box(
xPos + h + 3 + (i - 1) * (widthBar + 1),
xPos + h + offset + (i - 1) * (widthBar + 1),
yPos + 0.5 * h - yValue - 2,
widthBar,
self.scale,
Expand All @@ -84,23 +80,23 @@ if CLIENT then
end

draw.Box(
xPos + h + 3 + (i - 1) * (widthBar + 1),
xPos + h + offset + (i - 1) * (widthBar + 1),
yPos + 0.5 * h,
widthBar,
self.scale,
colorVoiceDevider
)

draw.Box(
xPos + h + 3 + (i - 1) * (widthBar + 1),
xPos + h + offset + (i - 1) * (widthBar + 1),
yPos + 0.5 * h + 2,
widthBar,
yValue,
colorVoiceBar
)
if yValue > 1 then
draw.Box(
xPos + h + 3 + (i - 1) * (widthBar + 1),
xPos + h + offset + (i - 1) * (widthBar + 1),
yPos + 0.5 * h + 2 + yValue,
widthBar,
self.scale,
Expand Down Expand Up @@ -131,6 +127,36 @@ if CLIENT then
true,
self.scale
)

if
voicebattery.IsEnabled()
and ply == LocalPlayer()
and VOICE.GetVoiceMode(ply) == VOICE_MODE_GLOBAL
then
draw.Box(
xPos + h,
yPos + h - self.padding - heightBatteryBar,
w - h,
heightBatteryBar,
colorVoiceBar
)

draw.Box(
xPos + h,
yPos + h - self.padding - heightBatteryBar,
voicebattery.GetChargePercent() * (w - h),
heightBatteryBar,
colorVoiceLine
)
end

self:DrawLines(
xPos + self.padding,
yPos + self.padding,
w - self.padding,
heightBar,
color.a
)
end

function HUDELEMENT:Draw()
Expand Down
1 change: 1 addition & 0 deletions gamemodes/terrortown/gamemode/shared/sh_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ include("ttt2/libraries/loadingscreen.lua")
include("ttt2/libraries/marker_vision.lua")
include("ttt2/libraries/weaponrenderer.lua")
include("ttt2/libraries/game_effects.lua")
include("ttt2/libraries/voicebattery.lua")

-- include ttt required files
ttt_include("sh_decal")
Expand Down
2 changes: 1 addition & 1 deletion gamemodes/terrortown/gamemode/shared/sh_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ function GM:Tick()
RADIO:StoreTarget()
end

VOICE.Tick()
voicebattery.Tick()
end
end

Expand Down
Loading

0 comments on commit 93ab9ef

Please sign in to comment.