Skip to content

Commit

Permalink
TargetID: Make ttt_idenfity_body_woconfirm replicated; move and cle…
Browse files Browse the repository at this point in the history
…an up additional ConVars surrounding corpses/ragdolls (#1692)

In #1684, `targetid.HUDDrawTargetIDRagdolls` was updated to check if the
`ttt_idenfity_body_woconfirm` ConVar was enabled to determine what hint
gets displayed.

Problem is, this ConVar only exists on the server, and the TargetID
library is entirely clientside. This resulted in Lua errors being thrown
on every frame the function was called and managed to reach that check.

This PR adds an additional table to the `CORPSE` namespace for the
purpose of storing ConVar objects related to corpses and bodysearching.
The following `CreateConVar` calls have been moved from `sv_main`:
- `ttt_idenfity_body_woconfirm` (to `sh_corpse`, with `FCVAR_REPLICATED`
added)
- `ttt2_confirm_team` (to `sv_corpse`)
- `ttt2_confirm_killlist` (to `sv_corpse`)

I've also moved the `CreateConVar` calls for `ttt_announce_body_found`
and `ttt_ragdoll_collide` into the new table for consistency, and so
scripts for other parts of the gamemode can access the ConVar objects
directly without having to store their own separate copies (or calling
`GetConVar` repeatedly). I've also already adjusted all existing checks
for these ConVars across the gamemode accordingly.

The changelog entry is worded the way it is because I just can't think
of any other way to describe the results of the changes without going
into a level of detail that'd be unnecessary for an end-user, but still
useful information for addon developers.
  • Loading branch information
Wryyyong authored and Histalek committed Dec 10, 2024
1 parent df1d1da commit f590bb8
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel
- Updated old TTT HUD to show name of spectated player (by @somefnfplayerlol)
- Changes to the enabled map prefixes will not be announced to players anymore (by @Histalek)
- By default only `ttt` and `ttt2` map prefixes are enabled (by @Histalek)
- Updated `ttt_identify_body_woconfirm` to be replicated across the server and client (by @Wryyyong)

## [v0.14.0b](https://github.com/TTT-2/TTT2/tree/v0.14.0b) (2024-09-20)

Expand Down
56 changes: 38 additions & 18 deletions gamemodes/terrortown/gamemode/server/sv_corpse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,6 @@ local IsValid = IsValid
local ConVarExists = ConVarExists
local hook = hook

---
-- @realm server
local cvBodyfound = CreateConVar(
"ttt_announce_body_found",
"1",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"If detective mode, announce when someone's body is found"
)

---
-- @realm server
local cvRagCollide = CreateConVar("ttt_ragdoll_collide", "0", { FCVAR_NOTIFY, FCVAR_ARCHIVE })

local soundsSearch = {
Sound("player/footsteps/snow1.wav"),
Sound("player/footsteps/snow2.wav"),
Expand All @@ -60,8 +47,41 @@ ttt_include("sh_corpse")

util.AddNetworkString("TTT2SendConfirmMsg")

---
-- @realm server
CORPSE.cv.announce_body_found = CreateConVar(
"ttt_announce_body_found",
"1",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"If detective mode, announce when someone's body is found"
)

---
-- @realm server
CORPSE.cv.confirm_team = CreateConVar(
"ttt2_confirm_team",
"0",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"Show team of confirmed player"
)

---
-- @realm server
CORPSE.cv.confirm_killlist = CreateConVar(
"ttt2_confirm_killlist",
"1",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"Confirm players in kill list"
)

---
-- @realm server
CORPSE.cv.ragdoll_collide =
CreateConVar("ttt_ragdoll_collide", "0", { FCVAR_NOTIFY, FCVAR_ARCHIVE })

-- networked data abstraction layer
local dti = CORPSE.dti
local config = CORPSE.cv

---
-- Sets a CORPSE found state
Expand Down Expand Up @@ -160,13 +180,13 @@ function CORPSE.IdentifyBody(ply, rag, searchUID)
end

-- Announce body
if cvBodyfound:GetBool() and notConfirmed then
if config.announce_body_found:GetBool() and notConfirmed then
local subrole = rag.was_role
local team = rag.was_team
local rd = roles.GetByIndex(subrole)
local roletext = "body_found_" .. rd.abbr
local clr = rag.role_color
local bool = GetGlobalBool("ttt2_confirm_team")
local bool = config.confirm_team:GetBool()

net.Start("TTT2SendConfirmMsg")

Expand Down Expand Up @@ -200,7 +220,7 @@ function CORPSE.IdentifyBody(ply, rag, searchUID)
net.Broadcast()
end

if GetConVar("ttt2_confirm_killlist"):GetBool() then
if config.confirm_killlist:GetBool() then
-- Handle kill list
local ragKills = rag.kills

Expand Down Expand Up @@ -278,7 +298,7 @@ function CORPSE.ShowSearch(ply, rag, isCovert, isLongRange)
end

if
GetConVar("ttt_identify_body_woconfirm"):GetBool()
config.identify_body_woconfirm:GetBool()
and gameloop.IsDetectiveMode()
and not isCovert
then
Expand Down Expand Up @@ -640,7 +660,7 @@ function CORPSE.GetPlayerTeam(rag)
end

hook.Add("ShouldCollide", "TTT2RagdollCollide", function(ent1, ent2)
if cvRagCollide:GetBool() then
if config.ragdoll_collide:GetBool() then
return
end

Expand Down
29 changes: 0 additions & 29 deletions gamemodes/terrortown/gamemode/server/sv_main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,6 @@ local map_switch_delay = CreateConVar(
0
)

---
-- @realm server
CreateConVar(
"ttt_identify_body_woconfirm",
"1",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"Toggles whether ragdolls should be confirmed in gameloop.IsDetectiveMode() without clicking on confirm espacially"
)

---
-- @realm server
local confirm_team = CreateConVar(
"ttt2_confirm_team",
"0",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"Show team of confirmed player"
)

---
-- @realm server
CreateConVar(
"ttt2_confirm_killlist",
"1",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE },
"Confirm players in kill list"
)

---
-- @realm server
CreateConVar(
Expand Down Expand Up @@ -505,8 +478,6 @@ function GM:SyncGlobals()
)
end

SetGlobalBool("ttt2_confirm_team", confirm_team:GetBool())

---
-- @realm server
hook.Run("TTT2SyncGlobals")
Expand Down
11 changes: 11 additions & 0 deletions gamemodes/terrortown/gamemode/shared/sh_corpse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ CORPSE.dti = {
INT_CREDITS = 0,
}

CORPSE.cv = {
---
-- @realm shared
identify_body_woconfirm = CreateConVar(
"ttt_identify_body_woconfirm",
"1",
{ FCVAR_NOTIFY, FCVAR_ARCHIVE, FCVAR_REPLICATED },
"Toggles whether ragdolls should be confirmed in gameloop.IsDetectiveMode() without clicking on confirm espacially"
),
}

local dti = CORPSE.dti

---
Expand Down
12 changes: 5 additions & 7 deletions lua/autorun/gs_crazyphysics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ local tEntitiesToCheck = {

local bTTT
local tIdentifyEntities
local cv_ttt_announce_body_found
local cv_ttt2_confirm_killlist
local corpseConfig

hook.Add("Initialize", "TTT2GSCrazyPhysics", function()
-- Change check if your terrortown folder is named something different
Expand All @@ -180,8 +179,7 @@ hook.Add("Initialize", "TTT2GSCrazyPhysics", function()
prop_ragdoll = true,
}

cv_ttt_announce_body_found = GetConVar("ttt_announce_body_found")
cv_ttt2_confirm_killlist = GetConVar("ttt2_confirm_killlist")
corpseConfig = CORPSE.cv
end
end)

Expand Down Expand Up @@ -285,8 +283,8 @@ local function IdentifyCorpse(pCorpse)
end
end

if cv_ttt_announce_body_found:GetBool() then
if GetGlobalBool("ttt2_confirm_team") then -- TODO: adjust the new messages
if corpseConfig.announce_body_found:GetBool() then
if corpseConfig.confirm_team:GetBool() then -- TODO: adjust the new messages
LANG.Msg("body_found", {
finder = "The Server",
victim = CORPSE.GetPlayerNick(pCorpse, nil) or pPlayer:GetName(),
Expand All @@ -302,7 +300,7 @@ local function IdentifyCorpse(pCorpse)
end
end

if cv_ttt2_confirm_killlist:GetBool() then
if corpseConfig.confirm_killlist:GetBool() then
local tKills = pCorpse.kills
if tKills then
for i = 1, #tKills do
Expand Down
11 changes: 10 additions & 1 deletion lua/ttt2/libraries/bodysearch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ if SERVER then
local mathRound = math.Round
local mathFloor = math.floor

local cv_ttt2_confirm_killlist

hook.Add("Initialize", "TTT2BodySearch", function()
-- Change check if your terrortown folder is named something different
if engine.ActiveGamemode():lower() == "terrortown" and TTT2 and istable(CORPSE) then
cv_ttt2_confirm_killlist = CORPSE.cv.confirm_killlist
end
end)

util.AddNetworkString("ttt2_client_reports_corpse")
util.AddNetworkString("ttt2_client_confirm_corpse")
util.AddNetworkString("ttt2_credits_were_taken")
Expand Down Expand Up @@ -356,7 +365,7 @@ if SERVER then

-- build list of people this player killed, but only if convar is enabled
sceneData.killEntityIDList = {}
if GetConVar("ttt2_confirm_killlist"):GetBool() then
if cv_ttt2_confirm_killlist:GetBool() then
local ragKills = rag.kills or {}

for i = 1, #ragKills do
Expand Down
11 changes: 10 additions & 1 deletion lua/ttt2/libraries/targetid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ local materialDestructible = Material("vgui/ttt/tid/tid_destructible")
local materialDNATargetID = Material("vgui/ttt/dnascanner/dna_hud")
local materialFire = Material("vgui/ttt/tid/tid_onfire")

local cv_ttt_identify_body_woconfirm

hook.Add("Initialize", "TTT2TargetID", function()
-- Change check if your terrortown folder is named something different
if engine.ActiveGamemode():lower() == "terrortown" and TTT2 and istable(CORPSE) then
cv_ttt_identify_body_woconfirm = CORPSE.cv.identify_body_woconfirm
end
end)

---
-- This function makes sure local variables, which use other libraries that are not yet initialized, are initialized later.
-- It gets called after all libraries are included and `cl_targetid.lua` gets included.
Expand Down Expand Up @@ -562,7 +571,7 @@ function targetid.HUDDrawTargetIDRagdolls(tData)
tData:AddDescriptionLine(TryT("corpse_hint_inspect_limited_details"))
elseif
bodysearch.GetInspectConfirmMode() == 0
and not GetConVar("ttt_identify_body_woconfirm"):GetBool()
and not cv_ttt_identify_body_woconfirm:GetBool()
then
tData:SetSubtitle(ParT("corpse_hint_without_confirm", key_params))
else
Expand Down

0 comments on commit f590bb8

Please sign in to comment.