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

Markervision: Generalize for all placeable entities #1423

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to TTT2 will be documented here. Inspired by [keep a changel

### Added

- Added all entities inheriting from ttt_base_placeable to get markervision
- Added migrations between TTT2-versions, some breaking changes could now be migrated instead
- Added a new markerVision module that adds information to a specific point in space to replace the old C4 radar; it is currently used by these builtin weapons (by @TimGoll)
- C4
Expand Down
101 changes: 99 additions & 2 deletions gamemodes/terrortown/entities/entities/ttt_base_placeable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ ENT.isDestructible = true

ENT.pickupWeaponClass = nil

-- MarkerVision related
ENT.markerIconMaterial = Material("vgui/ttt/tid/tid_big_role_not_known")
ENT.markerVisibility = VISIBLE_FOR_PLAYER

---
-- @realm shared
function ENT:Initialize()
Expand All @@ -24,6 +28,11 @@ function ENT:Initialize()

if SERVER then
self:PrecacheGibs()

local mvObject = self:AddMarkerVision("ttt_base_placeable_owner")
mvObject:SetOwner(self:GetMarkerOwner())
mvObject:SetVisibleFor(self.markerVisibility)
mvObject:SyncToClients()
end

local phys = self:GetPhysicsObject()
Expand Down Expand Up @@ -74,6 +83,12 @@ if SERVER then
AccessorFunc(ENT, "hitNormal", "HitNormal", FORCE_VECTOR)
AccessorFunc(ENT, "stickRotation", "StickRotation", FORCE_ANGLE)

---
-- @realm server
function ENT:OnRemove()
self:RemoveMarkerVision("ttt_base_placeable_owner")
end

---
-- @param CTakeDamageInfo dmgInfo
-- @realm server
Expand Down Expand Up @@ -232,7 +247,7 @@ 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.UseOverride has to be called if
-- the entity pickup system should be used.
-- @param Player activator The player that used their use key
-- @hook
Expand Down Expand Up @@ -392,4 +407,86 @@ if SERVER then

return true
end
end

---
-- Gets the current markerOwner
-- @note Override if not the originator should own the marker
-- @return Player|Role The Player or role be marker owner
-- @realm server
function ENT:GetMarkerOwner()
return self:GetOriginator()
end
end -- SERVER

if CLIENT then
local TryT = LANG.TryTranslation
local ParT = LANG.GetParamTranslation

---
-- Override to handle activation of marker overlay
-- @param MARKER_VISION_DATA mvData The @{MARKER_VISION_DATA} data object
-- @realm client
function ENT:ShouldDrawMarkerVision(mvData)
return true
end

---
-- Override to customize markervision on drawcalls
-- @param MARKER_VISION_DATA mvData The @{MARKER_VISION_DATA} data object
-- @realm client
function ENT:CustomizeMarkerVision(mvData) end

---
-- Override if not the markerIconMaterial should be used
-- @param MARKER_VISION_DATA mvData The @{MARKER_VISION_DATA} data object
-- @return table Containing the icon Materials to show
-- @realm client
function ENT:GetMarkerVisionIcons(mvData)
return { self.markerIconMaterial }
end

---
-- Override if not the default color for the icons should be used
-- @param MARKER_VISION_DATA mvData The @{MARKER_VISION_DATA} data object
-- @return table Containing the icon colors to show
-- @realm client
function ENT:GetMarkerVisionIconColors(mvData)
return { COLOR_WHITE }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a color table? Is this one color for each icon? One for each line of text?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see this is for the icon, this is a bit confusing, because the text can have colors as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. Will adjust.

end

-- Handle markervision for ttt_base_placeables
hook.Add("TTT2RenderMarkerVisionInfo", "HUDDrawMarkerVisionBasePlaceable", function(mvData)
local ent = mvData:GetEntity()
local mvObject = mvData:GetMarkerVisionObject()

if
not mvObject:IsObjectFor(ent, "ttt_base_placeable_owner")
or not ent:ShouldDrawMarkerVision(mvData)
then
return
end

local owner = ent:GetOriginator()
local nick = IsValid(owner) and owner:Nick() or "---"

local distance = math.Round(util.HammerUnitsToMeters(mvData:GetEntityDistance()), 1)

mvData:EnableText()
mvData:SetTitle(TryT(ent.PrintName))

local icons = ent:GetMarkerVisionIcons(mvData)
local colors = ent:GetMarkerVisionIconColors(mvData)
local size = math.min(#icons, #colors)

for i = 1, size do
mvData:AddIcon(icons[i], colors[i])
end

mvData:AddDescriptionLine(ParT("marker_vision_owner", { owner = nick }))
mvData:AddDescriptionLine(ParT("marker_vision_distance", { distance = distance }))

mvData:AddDescriptionLine(TryT(mvObject:GetVisibleForTranslationKey()), COLOR_SLATEGRAY)

ent:CustomizeMarkerVision(mvData)
end)
end -- CLIENT
37 changes: 5 additions & 32 deletions gamemodes/terrortown/entities/entities/ttt_beacon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ ENT.pickupWeaponClass = "weapon_ttt_beacon"
ENT.timeLastBeep = CurTime()
ENT.lastPlysFound = {}

ENT.markerIconMaterial = Material("vgui/ttt/marker_vision/beacon")
ENT.markerVisibility = VISIBLE_FOR_PLAYER

local beaconDetectionRange = 135

---
Expand All @@ -42,11 +45,6 @@ function ENT:Initialize()
if SERVER then
self:SetUseType(SIMPLE_USE)
self:NextThink(CurTime() + 1)

local mvObject = self:AddMarkerVision("beacon_owner")
mvObject:SetOwner(ROLE_DETECTIVE)
mvObject:SetVisibleFor(VISIBLE_FOR_ROLE)
mvObject:SyncToClients()
end
end

Expand Down Expand Up @@ -124,7 +122,7 @@ if SERVER then
ply:RemoveMarkerVision("beacon_player")
end

self:RemoveMarkerVision("beacon_owner")
BaseClass.OnRemove(self)
end

---
Expand Down Expand Up @@ -164,7 +162,7 @@ if SERVER then
end

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

Expand Down Expand Up @@ -203,7 +201,6 @@ if CLIENT then
local baseOpacity = 35
local factorRenderDistance = 3

local materialBeacon = Material("vgui/ttt/marker_vision/beacon")
local materialPlayer = Material("vgui/ttt/tid/tid_big_role_not_known")

-- handle looking at Beacon
Expand Down Expand Up @@ -240,30 +237,6 @@ if CLIENT then
tData:AddDescriptionLine(TryT("beacon_short_desc"))
end)

hook.Add("TTT2RenderMarkerVisionInfo", "HUDDrawMarkerVisionBeacon", function(mvData)
local ent = mvData:GetEntity()
local mvObject = mvData:GetMarkerVisionObject()

if not mvObject:IsObjectFor(ent, "beacon_owner") then
return
end

local owner = ent:GetOriginator()
local nick = IsValid(owner) and owner:Nick() or "---"

local distance = math.Round(util.HammerUnitsToMeters(mvData:GetEntityDistance()), 1)

mvData:EnableText()

mvData:AddIcon(materialBeacon)
mvData:SetTitle(TryT(ent.PrintName))

mvData:AddDescriptionLine(ParT("marker_vision_owner", { owner = nick }))
mvData:AddDescriptionLine(ParT("marker_vision_distance", { distance = distance }))

mvData:AddDescriptionLine(TryT(mvObject:GetVisibleForTranslationKey()), COLOR_SLATEGRAY)
end)

hook.Add("TTT2RenderMarkerVisionInfo", "HUDDrawMarkerVisionBeaconPlys", function(mvData)
local ent = mvData:GetEntity()
local mvObject = mvData:GetMarkerVisionObject()
Expand Down
77 changes: 31 additions & 46 deletions gamemodes/terrortown/entities/entities/ttt_c4/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ ENT.Avoidable = true

ENT.isDestructible = false

ENT.markerIconMaterial = Material("vgui/ttt/marker_vision/c4")
ENT.markerVisibility = VISIBLE_FOR_TEAM

---
-- @accessor Entity
-- @realm shared
Expand Down Expand Up @@ -426,12 +429,6 @@ end
-- Timer configuration handling

if SERVER then
---
-- @realm server
function ENT:OnRemove()
self:RemoveMarkerVision("c4_owner")
end

---
-- @param Player ply
-- @realm server
Expand All @@ -447,8 +444,6 @@ if SERVER then
self:SetExplodeTime(0)
self:SetArmed(false)

self:RemoveMarkerVision("c4_owner")

self.DisarmCausedExplosion = false
end

Expand Down Expand Up @@ -518,11 +513,6 @@ if SERVER then
end

events.Trigger(EVENT_C4PLANT, ply)

local mvObject = self:AddMarkerVision("c4_owner")
mvObject:SetOwner(ply)
mvObject:SetVisibleFor(VISIBLE_FOR_TEAM)
mvObject:SyncToClients()
end

---
Expand Down Expand Up @@ -773,8 +763,6 @@ if SERVER then
-- @realm server
function GAMEMODE:TTTC4Destroyed(bomb, ply) end
else -- CLIENT
local materialC4 = Material("vgui/ttt/marker_vision/c4")

local TryT = LANG.TryTranslation
local ParT = LANG.GetParamTranslation

Expand Down Expand Up @@ -883,35 +871,17 @@ else -- CLIENT
tData:AddDescriptionLine(TryT("c4_short_desc"))
end)

hook.Add("TTT2RenderMarkerVisionInfo", "HUDDrawMarkerVisionC4", function(mvData)
local ent = mvData:GetEntity()
local mvObject = mvData:GetMarkerVisionObject()

if not mvObject:IsObjectFor(ent, "c4_owner") then
return
end

local owner = ent:GetOriginator()
local nick = IsValid(owner) and owner:Nick() or "---"

local time = util.SimpleTime(ent:GetExplodeTime() - CurTime(), "%02i:%02i")
local distance = math.Round(util.HammerUnitsToMeters(mvData:GetEntityDistance()), 1)

mvData:EnableText()

mvData:SetTitle(TryT(ent.PrintName))

mvData:AddDescriptionLine(ParT("marker_vision_owner", { owner = nick }))
mvData:AddDescriptionLine(ParT("c4_marker_vision_time", { time = time }))
mvData:AddDescriptionLine(ParT("marker_vision_distance", { distance = distance }))

mvData:AddDescriptionLine(TryT(mvObject:GetVisibleForTranslationKey()), COLOR_SLATEGRAY)

---
-- Overriden to change colors with distance to armed c4
-- @param MARKER_VISION_DATA mvData The @{MARKER_VISION_DATA} data object
-- @return table Containing the icon colors to show
-- @realm client
function ENT:GetMarkerVisionIconColors(mvData)
local color = COLOR_WHITE

if mvData:GetEntityDistance() > ent:GetRadius() then
if mvData:GetEntityDistance() > self:GetRadius() then
mvData:AddDescriptionLine(TryT("c4_marker_vision_safe_zone"), COLOR_GREEN)
elseif mvData:GetEntityDistance() > ent:GetRadiusInner() then
elseif mvData:GetEntityDistance() > self:GetRadiusInner() then
mvData:AddDescriptionLine(TryT("c4_marker_vision_damage_zone"), COLOR_ORANGE)

color = COLOR_ORANGE
Expand All @@ -921,13 +891,28 @@ else -- CLIENT
color = COLOR_RED
end

mvData:AddIcon(
materialC4,
(mvData:IsOffScreen() or not mvData:IsOnScreenCenter()) and color
)
if mvData:IsOffScreen() or not mvData:IsOnScreenCenter() or not self:GetArmed() then
color = COLOR_WHITE
end

return { color }
end

---
-- Overriden to add time to marker
-- @param MARKER_VISION_DATA mvData The @{MARKER_VISION_DATA} data object
-- @realm client
function ENT:CustomizeMarkerVision(mvData)
if not self:GetArmed() then
return
end

local time = util.SimpleTime(self:GetExplodeTime() - CurTime(), "%02i:%02i")

mvData:AddDescriptionLine(ParT("c4_marker_vision_time", { time = time }))

mvData:SetCollapsedLine(time)
end)
end
end

---
Expand Down
6 changes: 6 additions & 0 deletions gamemodes/terrortown/entities/entities/ttt_decoy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ ENT.CanHavePrints = false
ENT.CanUseKey = true
ENT.pickupWeaponClass = "weapon_ttt_decoy"

if CLIENT then
ENT.Icon = "vgui/ttt/icon_decoy"
ENT.PrintName = "decoy_name"
ENT.markerIconMaterial = Material("vgui/ttt/icon_decoy")
end

---
-- @realm shared
function ENT:Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ DEFINE_BASECLASS("ttt_base_placeable")
if CLIENT then
ENT.Icon = "vgui/ttt/icon_health"
ENT.PrintName = "hstation_name"
ENT.markerIconMaterial = Material("vgui/ttt/icon_health")
end

ENT.Base = "ttt_base_placeable"
Expand Down
Loading
Loading