-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathgmod_wire_adv_emarker.lua
More file actions
160 lines (127 loc) · 3.75 KB
/
gmod_wire_adv_emarker.lua
File metadata and controls
160 lines (127 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
AddCSLuaFile()
DEFINE_BASECLASS("base_wire_entity")
ENT.PrintName = "Adv Wire Entity Marker"
ENT.WireDebugName = "Adv EMarker"
if CLIENT then return end
function ENT:Initialize()
self:PhysicsInit(SOLID_VPHYSICS)
self.Marks = {}
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)"
})
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
if IsValid(value) then
self.Target = value
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 value ~= 0 and IsValid(self.Target) and self:CheckEnt(self.Target) then
self:UnlinkEnt(self.Target)
end
elseif name == "Clear Entities" then
self:ClearEntities()
end
end
function ENT:UpdateOutputs()
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(checkent)
for index, ent in ipairs(self.Marks) do
if checkent == ent then
return true, index
end
end
return false, 0
end
function ENT:LinkEnt(ent)
if self:CheckEnt(ent) then return false end
table.insert(self.Marks, ent)
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)
ent:RemoveCallOnRemove("AdvEMarker.Unlink" .. self:EntIndex())
self:UpdateOutputs()
end
return bool
end
function ENT:ClearEntities()
for index, ent in ipairs(self.Marks) do
ent:RemoveCallOnRemove("AdvEMarker.Unlink" .. self:EntIndex())
end
self.Marks = {}
self:UpdateOutputs()
end
function ENT:OnRemove()
self:ClearEntities()
end
function ENT:BuildDupeInfo()
local info = BaseClass.BuildDupeInfo(self)
if #self.Marks > 0 then
local tab = {}
for index, ent in ipairs(self.Marks) do
tab[index] = ent:EntIndex()
end
info.marks = tab
end
return info
end
function ENT:ApplyDupeInfo(ply, ent, info, GetEntByID)
BaseClass.ApplyDupeInfo(self, ply, ent, info, GetEntByID)
if info.marks then
for index, entid in ipairs(info.marks) do
local ent = GetEntByID(entid)
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")