Skip to content
166 changes: 87 additions & 79 deletions lua/entities/gmod_wire_adv_emarker.lua
Original file line number Diff line number Diff line change
@@ -1,80 +1,84 @@
AddCSLuaFile()
DEFINE_BASECLASS( "base_wire_entity" )
ENT.PrintName = "Adv Wire Entity Marker"
ENT.Author = "Divran"

DEFINE_BASECLASS("base_wire_entity")
ENT.PrintName = "Adv Wire Entity Marker"
ENT.WireDebugName = "Adv EMarker"

if CLIENT then return end -- No more client
if CLIENT then return end

function ENT:Initialize()
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )

self:PhysicsInit(SOLID_VPHYSICS)
self.Marks = {}
local outputs = {"Entities [ARRAY]", "Nr"}
for i=3,12 do
outputs[i] = "Entity" .. (i-2) .. " [ENTITY]"
end
self.Inputs = WireLib.CreateInputs( self, {

WireLib.CreateInputs(self, {
"Entity (This entity will be added or removed once the other two inputs are changed) [ENTITY]",
"Add Entity (Change to non-zero value to add the entity specified by the 'Entity' input)",
"Remove Entity (Change to non-zero value to remove the entity specified by the 'Entity' input)",
"Clear Entities (Removes all entities from the marker)"
} )
self.Outputs = WireLib.CreateOutputs( self, outputs )
self:SetOverlayText( "Number of entities linked: 0" )
})

WireLib.CreateOutputs(self, {
"Entities [ARRAY]",
"Nr (Number of entities linked)",
"Entity1 [ENTITY]",
"Entity2 [ENTITY]",
"Entity3 [ENTITY]",
"Entity4 [ENTITY]",
"Entity5 [ENTITY]",
"Entity6 [ENTITY]",
"Entity7 [ENTITY]",
"Entity8 [ENTITY]",
"Entity9 [ENTITY]",
"Entity10 [ENTITY]"
})

self:SetOverlayText("Number of entities linked: 0")
end

function ENT:TriggerInput( name, value )
if (name == "Entity") then
function ENT:TriggerInput(name, value)
if name == "Entity" then
if IsValid(value) then
self.Target = value
end
elseif (name == "Add Entity") then
if IsValid(self.Target) then
if (value ~= 0) then
local bool, index = self:CheckEnt( self.Target )
if (not bool) then
self:LinkEnt( self.Target )
end
end
elseif name == "Add Entity" then
if value ~= 0 and IsValid(self.Target) and not self:CheckEnt(self.Target) then
self:LinkEnt(self.Target)
end
elseif (name == "Remove Entity") then
if IsValid(self.Target) then
if (value ~= 0) then
local bool, index = self:CheckEnt( self.Target )
if (bool) then
self:UnlinkEnt( self.Target )
end
end
elseif name == "Remove Entity" then
if value ~= 0 and IsValid(self.Target) and self:CheckEnt(self.Target) then
self:UnlinkEnt(self.Target)
end
elseif (name == "Clear Entities") then
elseif name == "Clear Entities" then
self:ClearEntities()
end
end

function ENT:UpdateOutputs()
-- Trigger regular outputs
WireLib.TriggerOutput( self, "Entities", self.Marks )
WireLib.TriggerOutput( self, "Nr", #self.Marks )

-- Trigger special outputs
for i=3,12 do
WireLib.TriggerOutput( self, "Entity" .. (i-2), self.Marks[i-2] )
end

-- Overlay text
self:SetOverlayText( "Number of entities linked: " .. #self.Marks )

-- Yellow lines information
local marks = self.Marks
WireLib.TriggerOutput(self, "Entities", marks)
WireLib.TriggerOutput(self, "Nr", #marks)
WireLib.TriggerOutput(self, "Entity1", marks[1])
WireLib.TriggerOutput(self, "Entity2", marks[2])
WireLib.TriggerOutput(self, "Entity3", marks[3])
WireLib.TriggerOutput(self, "Entity4", marks[4])
WireLib.TriggerOutput(self, "Entity5", marks[5])
WireLib.TriggerOutput(self, "Entity6", marks[6])
WireLib.TriggerOutput(self, "Entity7", marks[7])
WireLib.TriggerOutput(self, "Entity8", marks[8])
WireLib.TriggerOutput(self, "Entity9", marks[9])
WireLib.TriggerOutput(self, "Entity10", marks[10])

self:SetOverlayText("Number of entities linked: " .. #marks)
WireLib.SendMarks(self)
end

function ENT:CheckEnt( ent )
for index, e in pairs( self.Marks ) do
if (e == ent) then return true, index end
function ENT:CheckEnt(checkent)
for index, ent in ipairs(self.Marks) do
if checkent == ent then
return true, index
end
end

return false, 0
end

Expand All @@ -83,32 +87,32 @@ function ENT:LinkEnt(ent)

table.insert(self.Marks, ent)

ent:CallOnRemove("AdvEMarker.Unlink", function(ent)
if self:IsValid() then
self:UnlinkEnt(ent)
end
ent:CallOnRemove("AdvEMarker.Unlink" .. self:EntIndex(), function(ent)
self:UnlinkEnt(ent)
end)

self:UpdateOutputs()

return true
end

function ENT:UnlinkEnt( ent )
local bool, index = self:CheckEnt( ent )
if (bool) then
table.remove( self.Marks, index )
function ENT:UnlinkEnt(ent)
local bool, index = self:CheckEnt(ent)

if bool then
table.remove(self.Marks, index)
ent:RemoveCallOnRemove("AdvEMarker.Unlink" .. self:EntIndex())
self:UpdateOutputs()
end

return bool
end

function ENT:ClearEntities()
for i=1,#self.Marks do
if self.Marks[i]:IsValid() then
self.Marks[i]:RemoveCallOnRemove( "AdvEMarker.Unlink" )
end
for index, ent in ipairs(self.Marks) do
ent:RemoveCallOnRemove("AdvEMarker.Unlink" .. self:EntIndex())
end

self.Marks = {}
self:UpdateOutputs()
end
Expand All @@ -117,18 +121,17 @@ function ENT:OnRemove()
self:ClearEntities()
end

duplicator.RegisterEntityClass( "gmod_wire_adv_emarker", WireLib.MakeWireEnt, "Data" )

function ENT:BuildDupeInfo()
local info = BaseClass.BuildDupeInfo(self) or {}
local info = BaseClass.BuildDupeInfo(self)

if #self.Marks > 0 then
local tab = {}

if next(self.Marks) then
local tbl = {}
for index, e in pairs( self.Marks ) do
tbl[index] = e:EntIndex()
for index, ent in ipairs(self.Marks) do
tab[index] = ent:EntIndex()
end

info.marks = tbl
info.marks = tab
end

return info
Expand All @@ -137,16 +140,21 @@ end
function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID)

if (info.marks) then
self.Marks = self.Marks or {}

for index, entid in pairs(info.marks) do
if info.marks then
for index, entid in ipairs(info.marks) do
local ent = GetEntByID(entid)
self.Marks[index] = ent
ent:CallOnRemove("AdvEMarker.Unlink", function(ent)
if IsValid(self) then self:UnlinkEnt(ent) end
end)

if ent:IsValid() then
table.insert(self.Marks, ent)

ent:CallOnRemove("AdvEMarker.Unlink" .. self:EntIndex(), function(ent)
self:UnlinkEnt(ent)
end)
end
end

self:UpdateOutputs()
end
end

duplicator.RegisterEntityClass("gmod_wire_adv_emarker", WireLib.MakeWireEnt, "Data")
84 changes: 59 additions & 25 deletions lua/entities/gmod_wire_emarker.lua
Original file line number Diff line number Diff line change
@@ -1,47 +1,81 @@
AddCSLuaFile()
DEFINE_BASECLASS( "base_wire_entity" )
ENT.PrintName = "Wire Entity Marker"

DEFINE_BASECLASS("base_wire_entity")
ENT.PrintName = "Wire Entity Marker"
ENT.WireDebugName = "EMarker"

if CLIENT then return end -- No more client
if CLIENT then return end

function ENT:Initialize()
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:PhysicsInit(SOLID_VPHYSICS)

self.Outputs = WireLib.CreateSpecialOutputs(self, { "Entity" }, { "ENTITY" })
self:SetOverlayText( "No Mark selected" )
WireLib.CreateOutputs(self, { "Entity [ENTITY]" })
self:SetOverlayText("No linked mark")
end

function ENT:LinkEMarker(mark)
if mark then self.mark = mark end
if not IsValid(self.mark) then self:SetOverlayText( "No Mark selected" ) return end
self.mark:CallOnRemove("EMarker.UnLink", function(ent)
if IsValid(self) and self.mark == ent then self:UnLinkEMarker() end
end)
Wire_TriggerOutput(self, "Entity", self.mark)
self:SetOverlayText( "Linked - " .. self.mark:GetModel() )
function ENT:LinkEnt(ent)
if ent ~= self.Mark then
if self.Mark then
self.Mark:RemoveCallOnRemove("EMarker.UnLink" .. self:EntIndex())
self.Mark = nil
end

ent:CallOnRemove("EMarker.UnLink" .. self:EntIndex(), function(ent)
self:UnlinkEnt()
end)

WireLib.SendMarks(self, { ent })
WireLib.TriggerOutput(self, "Entity", ent)
self:SetOverlayText("Linked to: " .. ent:GetClass())
self.Mark = ent

return true
end

return false
end

function ENT:UnLinkEMarker()
self.mark = NULL
Wire_TriggerOutput(self, "Entity", NULL)
self:SetOverlayText( "No Mark selected" )
function ENT:UnlinkEnt()
if self.Mark then
WireLib.SendMarks(self, {})
WireLib.TriggerOutput(self, "Entity", NULL)
self:SetOverlayText("No linked mark")
self.Mark:RemoveCallOnRemove("EMarker.UnLink" .. self:EntIndex())
self.Mark = nil

return true
end

return false
end

duplicator.RegisterEntityClass( "gmod_wire_emarker", WireLib.MakeWireEnt, "Data" )
function ENT:OnRemove()
if self.Mark then
self.Mark:RemoveCallOnRemove("EMarker.UnLink" .. self:EntIndex())
self.Mark = nil
end
end

function ENT:BuildDupeInfo()
local info = BaseClass.BuildDupeInfo(self) or {}
if ( self.mark ) and ( self.mark:IsValid() ) then
info.mark = self.mark:EntIndex()
local info = BaseClass.BuildDupeInfo(self)

if self.Mark then
info.mark = self.Mark:EntIndex()
end

return info
end

function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID)

self:LinkEMarker(GetEntByID(info.mark))
if info.mark then
local ent = GetEntByID(info.mark)

if ent:IsValid() then
self:LinkEnt(ent)
end
end
end

duplicator.RegisterEntityClass("gmod_wire_emarker", WireLib.MakeWireEnt, "Data")
Loading
Loading