Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENTITY: New Use workflow #1458

Merged
merged 14 commits into from
Jun 2, 2024
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel

### Changed

- Placeable Entities are now checked for pickup clientside first
- Visualizer can now only be picked up by the originator
- C4 UI is not routed over the server anymore

### Fixed

- Fixed the AFK timer accumulating while player not fully joined (by @EntranceJew)
-
### Removed

- Removed radio tab in shop UI
- Removed all uses of UseOverride-Hook inside TTT2 (It's still available for addons!)

## [v0.13.1b](https://github.com/TTT-2/TTT2/tree/v0.13.1b) (2024-02-27)

Expand Down
53 changes: 37 additions & 16 deletions gamemodes/terrortown/entities/entities/ttt_base_placeable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ENT.isDestructible = true

ENT.pickupWeaponClass = nil

local soundDeny = Sound("HL2Player.UseDeny")

---
-- @realm shared
function ENT:Initialize()
Expand Down Expand Up @@ -41,6 +43,38 @@ function ENT:SetupDataTables()
self:NetworkVar("Entity", 0, "Originator")
end

---
-- Run if a valid player tries to pick up this entity to check if this pickup is accepted.
-- @param Player activator The player that used their use key
-- @return[default=true] boolean Return true to allow pickup
-- @hook
-- @realm shared
function ENT:PlayerCanPickupWeapon(activator)
return true
end

if CLIENT then
---
-- Hook that is called if a player uses their use key while focusing on the entity.
-- Implement this to predict early if entity can be picked up
-- @return bool True to prevent pickup
-- @realm client
function ENT:ClientUse()
local client = LocalPlayer()
if not IsValid(client) or not client:IsTerror() or not self.pickupWeaponClass then
TimGoll marked this conversation as resolved.
Show resolved Hide resolved
return true
end

if not self:PlayerCanPickupWeapon(client) then
LANG.Msg(client, "pickup_fail", nil, MSG_MSTACK_WARN)

self:EmitSound(soundDeny)
TimGoll marked this conversation as resolved.
Show resolved Hide resolved

return true
end
end
end -- CLIENT

if SERVER then
local soundRumble = {
Sound("physics/concrete/concrete_break2.wav"),
Expand All @@ -67,8 +101,6 @@ if SERVER then

local soundThrow = Sound("Weapon_SLAM.SatchelThrow")

local soundDeny = Sound("HL2Player.UseDeny")

local soundWeaponPickup = Sound("items/ammo_pickup.wav")

AccessorFunc(ENT, "hitNormal", "HitNormal", FORCE_VECTOR)
Expand Down Expand Up @@ -232,12 +264,11 @@ if SERVER then

---
-- Hook that is called if a player uses their use key while focusing on the entity.
-- @note When overwriting this function BaseClass.UseOverwrite has to be called if
-- @note When overwriting this function BaseClass.Use has to be called if
-- the entity pickup system should be used.
-- @param Player activator The player that used their use key
-- @hook
-- @realm server
function ENT:UseOverride(activator)
function ENT:Use(activator)
if not IsValid(activator) or not activator:IsTerror() or not self.pickupWeaponClass then
return
end
Expand Down Expand Up @@ -281,16 +312,6 @@ if SERVER then
self:Remove()
end

---
-- Run if a valid player tries to pick up this entity to check if this pickup is accepted.
-- @param Player activator The player that used their use key
-- @return[default=true] boolean Return true to allow pickup
-- @hook
-- @realm server
function ENT:PlayerCanPickupWeapon(activator)
return true
end

---
-- Called when this entity is picked up and about to be removed.
-- @param Player activator The player that used their use key
Expand Down Expand Up @@ -392,4 +413,4 @@ if SERVER then

return true
end
end
end -- SERVER
17 changes: 8 additions & 9 deletions gamemodes/terrortown/entities/entities/ttt_beacon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ ENT.Model = "models/props_lab/reciever01a.mdl"

ENT.CanHavePrints = true

ENT.CanUseKey = true
ENT.pickupWeaponClass = "weapon_ttt_beacon"

ENT.timeLastBeep = CurTime()
Expand Down Expand Up @@ -50,6 +49,13 @@ function ENT:Initialize()
end
end

---
-- @param Player activator
-- @realm shared
function ENT:PlayerCanPickupWeapon(activator)
return self:GetOriginator() == activator
end

if SERVER then
local soundBeep = Sound("weapons/c4/cc4_beep1.wav")

Expand Down Expand Up @@ -127,13 +133,6 @@ if SERVER then
self:RemoveMarkerVision("beacon_owner")
end

---
-- @param Player activator
-- @realm server
function ENT:PlayerCanPickupWeapon(activator)
return self:GetOriginator() == activator
end

---
-- @realm server
function ENT:UpdateTransmitState()
Expand Down Expand Up @@ -164,7 +163,7 @@ if SERVER then
end

---
-- @realm server
-- @realm server
-- stylua: ignore
if hook.Run("TTT2BeaconDeathNotify", victim, beacon) == false then continue end

Expand Down
13 changes: 0 additions & 13 deletions gamemodes/terrortown/entities/entities/ttt_c4/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -494,19 +494,6 @@ end

---- Communication

local function C4ConfigHook()
local bomb = net.ReadEntity()

if IsValid(bomb) then
if not bomb:GetArmed() then
ShowC4Config(bomb)
else
ShowC4Disarm(bomb)
end
end
end
net.Receive("TTT_C4Config", C4ConfigHook)

local function C4DisarmResultHook()
local bomb = net.ReadEntity()
local correct = net.ReadBit() == 1
Expand Down
37 changes: 17 additions & 20 deletions gamemodes/terrortown/entities/entities/ttt_c4/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ ENT.Base = "ttt_base_placeable"
ENT.Model = "models/weapons/w_c4_planted.mdl"

ENT.CanHavePrints = true
ENT.CanUseKey = true
ENT.Avoidable = true

ENT.isDestructible = false
Expand Down Expand Up @@ -125,15 +124,6 @@ function ENT:SetDetonateTimer(length)
self:SetExplodeTime(CurTime() + length)
end

---
-- @param Entity activator
-- @realm shared
function ENT:UseOverride(activator)
if IsValid(activator) and activator:IsPlayer() then
self:ShowC4Config(activator)
end
end

---
-- @param number t
-- @return number
Expand Down Expand Up @@ -525,16 +515,6 @@ if SERVER then
mvObject:SyncToClients()
end

---
-- @param Player ply
-- @realm server
function ENT:ShowC4Config(ply)
-- show menu to player to configure or disarm us
net.Start("TTT_C4Config")
net.WriteEntity(self)
net.Send(ply)
end

local function ReceiveC4Config(ply, cmd, args)
if not (IsValid(ply) and ply:IsTerror() and #args == 2) then
return
Expand Down Expand Up @@ -791,6 +771,23 @@ else -- CLIENT
antialias = false,
})

---
-- Hook that is called if a player uses their use key while focusing on the entity.
-- Shows C4 UI
-- @return bool True to prevent pickup
-- @realm client
function ENT:ClientUse()
if IsValid(self) then
if not self:GetArmed() then
ShowC4Config(self)
else
ShowC4Disarm(self)
end
end

return true
end

---
-- @return table pos
-- @realm client
Expand Down
25 changes: 10 additions & 15 deletions gamemodes/terrortown/entities/entities/ttt_cse_proj.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ENT.MaxScenesPerPulse = 3
ENT.SceneDuration = 10
ENT.PulseDelay = 10

ENT.CanUseKey = true
ENT.pickupWeaponClass = "weapon_ttt_cse"

---
Expand All @@ -38,6 +37,13 @@ function ENT:Initialize()
self:SetHealth(50)
end

---
-- @param Player activator
-- @realm shared
function ENT:PlayerCanPickupWeapon(activator)
return self:GetOriginator() == activator
end

---
-- @realm shared
function ENT:GetNearbyCorpses()
Expand Down Expand Up @@ -186,15 +192,6 @@ if SERVER then

return true
end

---
-- @param Player activator
-- @realm server
function ENT:PlayerCanPickupWeapon(activator)
local roleDataActivator = activator:GetSubRoleData()

return roleDataActivator.isPolicingRole and roleDataActivator.isPublicRole
end
end

if CLIENT then
Expand All @@ -214,7 +211,6 @@ if CLIENT then
hook.Add("TTTRenderEntityInfo", "HUDDrawTargetIDVisualizer", function(tData)
local client = LocalPlayer()
local ent = tData:GetEntity()
local roleData = client:GetSubRoleData()

if
not IsValid(client)
Expand All @@ -233,14 +229,13 @@ if CLIENT then

tData:SetTitle(TryT("vis_name"))

if roleData.isPublicRole and roleData.isPolicingRole then
if ent:GetOriginator() == client then
tData:SetSubtitle(ParT("target_pickup", { usekey = Key("+use", "USE") }))
tData:SetKeyBinding("+use")
else
tData:SetSubtitle(TryT("vis_no_pickup"))
tData:AddIcon(roles.DETECTIVE.iconMaterial)
tData:SetSubtitle(TryT("entity_pickup_owner_only"))
end

tData:SetKeyBinding("+use")
TimGoll marked this conversation as resolved.
Show resolved Hide resolved
tData:AddDescriptionLine(TryT("vis_short_desc"))
end)
end
17 changes: 8 additions & 9 deletions gamemodes/terrortown/entities/entities/ttt_decoy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ENT.Model = "models/props_lab/reciever01b.mdl"

ENT.CanHavePrints = false

ENT.CanUseKey = true
ENT.pickupWeaponClass = "weapon_ttt_decoy"

---
Expand Down Expand Up @@ -47,6 +46,14 @@ function ENT:OnRemove()
self:GetOriginator().decoy = nil
end

---
-- @param Player activator
-- @realm shared
function ENT:PlayerCanPickupWeapon(activator)
return activator:HasTeam()
and self:GetNWString("decoy_owner_team", "none") == activator:GetTeam()
end

if SERVER then
---
-- @realm server
Expand All @@ -59,14 +66,6 @@ if SERVER then

LANG.Msg(originator, "decoy_broken", nil, MSG_MSTACK_WARN)
end

---
-- @param Player activator
-- @realm server
function ENT:PlayerCanPickupWeapon(activator)
return activator:HasTeam()
and self:GetNWString("decoy_owner_team", "none") == activator:GetTeam()
end
end

if CLIENT then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ENT.Base = "base_anim"
ENT.PrintName = "hat_deerstalker_name"
ENT.Model = Model("models/ttt/deerstalker.mdl")
ENT.CanHavePrints = false
ENT.CanUseKey = true

---
-- @realm shared
Expand Down Expand Up @@ -131,7 +130,7 @@ if SERVER then
---
-- @param Player ply
-- @realm server
function ENT:UseOverride(ply)
function ENT:Use(ply)
if not ttt_hats_reclaim:GetBool() then
return
end
Expand Down