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 Base: Add remote triggerable #1436

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel

- Added hook ENTITY:ClientUse(), which is triggered clientside if an entity is used
- Return true to prevent also using this on the server for clientside only usecases
- Added hook ENTITY:RemoteUse(ply), which is shared
- Return true if only clientside should be used

### Changed

Expand Down
40 changes: 25 additions & 15 deletions gamemodes/terrortown/gamemode/client/cl_keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function GM:PlayerBindPress(ply, bindName, pressed)
return true
end
elseif bindName == "+use" and pressed then
-- Do old traitor button check
if TBHUD:PlayerIsFocused() then
if ply:KeyDown(IN_WALK) then
-- Try to change the access to the button for your current role or team
Expand All @@ -76,29 +77,38 @@ function GM:PlayerBindPress(ply, bindName, pressed)
return TBHUD:UseFocused()
end

local tr = util.TraceLine({
start = ply:GetShootPos(),
endpos = ply:GetShootPos() + ply:GetAimVector() * 100,
filter = ply,
mask = MASK_SHOT,
})

if not tr.Hit or not IsValid(tr.Entity) then
return
end

-- Find out if a marker is focussed otherwise check normal use
local isClientOnly = false
if isfunction(tr.Entity.ClientUse) then
isClientOnly = tr.Entity:ClientUse()
local useEnt = markerVision.GetFocussedEntity()
local isRemote = IsValid(useEnt)
TimGoll marked this conversation as resolved.
Show resolved Hide resolved
if not isRemote then
local tr = util.TraceLine({
start = ply:GetShootPos(),
endpos = ply:GetShootPos() + ply:GetAimVector() * 100,
filter = ply,
mask = MASK_SHOT,
})

useEnt = tr.Entity
if not tr.Hit or not IsValid(useEnt) then
return
end

if isfunction(useEnt.ClientUse) then
isClientOnly = useEnt:ClientUse()
end
elseif isfunction(useEnt.RemoteUse) then
isClientOnly = useEnt:RemoteUse(ply)
TimGoll marked this conversation as resolved.
Show resolved Hide resolved
Histalek marked this conversation as resolved.
Show resolved Hide resolved
end

-- If returned true by ClientUse, then dont call Use and UseOverride serverside
-- If returned true by ClientUse or RemoteUse, then dont call Use and UseOverride or RemoteUse serverside
if isClientOnly then
return
end

net.Start("TTT2PlayerUseEntity")
net.WriteEntity(tr.Entity)
net.WriteEntity(useEnt)
net.WriteBool(isRemote)
net.SendToServer()
elseif string.sub(bindName, 1, 4) == "slot" and pressed then
local idx = tonumber(string.sub(bindName, 5, -1)) or 1
Expand Down
9 changes: 9 additions & 0 deletions gamemodes/terrortown/gamemode/server/sv_player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,20 @@ end
-- @internal
net.Receive("TTT2PlayerUseEntity", function(len, ply)
local ent = net.ReadEntity()
local isRemote = net.ReadBool()

if not IsValid(ent) then
return
end

if isRemote then
if isfunction(ent.RemoteUse) then
ent:RemoteUse(ply)
end

return
end

-- Check if the use interaction is possible
-- Add the bounding radius to compensate for center position
local distance = ply:GetShootPos():Distance(ent:GetPos())
Expand Down
36 changes: 31 additions & 5 deletions lua/ttt2/libraries/marker_vision.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ VISIBLE_FOR_BITS = 3
markerVision = {}

markerVision.registry = {}
markerVision.focussedMarkers = {}

---
-- Creates a new marker vision object for the entity.
Expand Down Expand Up @@ -227,6 +228,24 @@ if CLIENT then
{ font = "Tahoma", size = 14, weight = 300, extended = true }
)

---
-- This gets the currently focussed closest entity.
-- @realm client
function markerVision.GetFocussedEntity()
local closestEntTbl = markerVision.focussedMarkers[1] or {}

for i = 2, #markerVision.focussedMarkers do
local entTbl = markerVision.focussedMarkers[i]
if entTbl.screenDistanceSquared < closestEntTbl.screenDistanceSquared then
closestEntTbl = entTbl
end
end

markerVision.focussedMarkers = { closestEntTbl }

return closestEntTbl.entity
end
TimGoll marked this conversation as resolved.
Show resolved Hide resolved

---
-- The draw function of the radar vision module.
-- @internal
Expand Down Expand Up @@ -254,6 +273,8 @@ if CLIENT then
local xScreenCenter = 0.5 * widthScreen
local yScreenCenter = 0.5 * heightScreen

markerVision.focussedMarkers = {}

for i = 1, #markerVision.registry do
local mvObject = markerVision.registry[i]
local ent = mvObject.data.ent
Expand All @@ -265,11 +286,9 @@ if CLIENT then
local posEnt = ent:GetPos() + ent:OBBCenter()
local screenPos = posEnt:ToScreen()
local isOffScreen = util.IsOffScreen(screenPos)
local isOnScreenCenter = not isOffScreen
and screenPos.x > xScreenCenter - offsetIcon
and screenPos.x < xScreenCenter + offsetIcon
and screenPos.y > yScreenCenter - offsetIcon
and screenPos.y < yScreenCenter + offsetIcon
local screenDistSquared = (screenPos.x - xScreenCenter) ^ 2
+ (screenPos.y - yScreenCenter) ^ 2
local isOnScreenCenter = not isOffScreen and screenDistSquared < offsetIcon ^ 2
local distanceEntity = posEnt:Distance(client:EyePos())

-- call internal targetID functions first so the data can be modified by addons
Expand All @@ -294,6 +313,13 @@ if CLIENT then
continue
end

if isOnScreenCenter then
markerVision.focussedMarkers[#markerVision.focussedMarkers + 1] = {
entity = ent,
screenDistanceSquared = screenDistSquared,
}
TimGoll marked this conversation as resolved.
Show resolved Hide resolved
end

local amountIcons = #params.displayInfo.icon

if isOffScreen or not isOnScreenCenter then
Expand Down
Loading