-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathentitycontrol.lua
More file actions
302 lines (229 loc) · 6.65 KB
/
entitycontrol.lua
File metadata and controls
302 lines (229 loc) · 6.65 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
-- This part of the wire map interface entity controls
-- the adding and removing of its in-/outputs entities.
function ENT:HandleWireEntNameUpdated()
if not self.WireEntNameUpdated then
return
end
self:AddEntitiesByName(self.WireEntName)
self.WireEntNameUpdated = nil
end
function ENT:HandleWireEntsUpdated()
if not self.WireEntsUpdated then
return
end
local ready = false
if self.WireEntsRemoved then
self:TriggerHammerOutputSafe("OnWireEntsRemoved", self)
self.WireEntsRemoved = nil
ready = true
end
if self.WireEntsAdded then
if self:GetWiredEntityCount() > 0 then
-- Avoid triggering "created" event if the list if entity is actually empty.
self:TriggerHammerOutputSafe("OnWireEntsCreated", self)
ready = true
end
self.WireEntsAdded = nil
end
if ready then
-- Only trigger "ready" if one of the other both had been triggered.
self:TriggerHammerOutputSafe("OnWireEntsReady", self)
end
self.NextNetworkTime = math.max(self.NextNetworkTime or 0, CurTime() + self.MIN_THINK_TIME * 2)
self.WireEntsUpdated = nil
end
-- Entity add functions
function ENT:AddEntitiesByName(name)
local entities = self:GetEntitiesByTargetnameOrClass(name)
self:AddEntitiesByTable(entities)
end
function ENT:AddEntitiesByTable(entitiesToAdd)
if not entitiesToAdd then return end
local tmp = {}
for key, value in pairs(entitiesToAdd) do
if isentity(key) and IsValid(key) then
tmp[key] = key
end
if isentity(value) and IsValid(value) then
tmp[value] = value
end
end
for _, wireEnt in pairs(tmp) do
self:AddSingleEntity(wireEnt)
end
end
function ENT:AddSingleEntity(wireEnt)
if not self:IsWireableEntity(wireEnt) then
return
end
local hardLimit = self.WireEntsHardLimit or 0
if hardLimit >= self:GetMaxSubEntities() * 3 then
-- Stop adding more things until cleaned up.
return
end
local id = wireEnt:EntIndex()
local wireEnts = self.WireEntsRegister
local item = wireEnts[id]
local oldWireEnt = item and item.ent
local isInList = IsValid(oldWireEnt) and oldWireEnt == wireEnt
if isInList and wireEnt._WireMapInterfaceEnt_HasPorts then
return
end
if not isInList then
if not self.WireEntsUpdated then
self:TriggerHammerOutputSafe("OnWireEntsStartChanging", self)
end
self:OverrideEnt(wireEnt)
end
if wireEnt._WMI_AddPorts then
wireEnt:_WMI_AddPorts(self.WireInputRegister, self.WireOutputRegister)
end
wireEnts[id] = {
ent = wireEnt,
cid = wireEnt:GetCreationID(),
}
if not isInList then
self.WireEntsHardLimit = hardLimit + 1
self.WireEntsUpdated = true
self.WireEntsAdded = true
self:RequestNetworkEntities()
self.WireEntsSorted = nil
self.WireEntsCount = nil
end
self:ApplyWireOutputBufferSingle(wireEnt)
-- Sometimes CurTime() may get lag compensated and "time travels" when the entity is being added by duplicator/gun trigger.
-- So we delay the think call past the predicted event time to avoid any race conditions.
self:NextThink(CurTime() + self.MIN_THINK_TIME)
return wireEnt
end
-- Entity remove functions
function ENT:RemoveAllEntities()
local wireEnts = self.WireEntsRegister
for id, item in pairs(wireEnts) do
self:RemoveSingleEntity(item.ent)
wireEnts[id] = nil
end
self.WireEntsSorted = nil
self.WireEntsCount = nil
self.WireEntsHardLimit = nil
end
function ENT:RemoveEntitiesByName(name)
local entities = self:GetEntitiesByTargetnameOrClass(name)
self:RemoveEntitiesByTable(entities)
end
function ENT:RemoveEntitiesByTable(entitiesToRemove)
if not entitiesToRemove then return end
local tmp = {}
for key, value in pairs(entitiesToRemove) do
if isentity(key) and IsValid(key) then
tmp[key] = key
end
if isentity(value) and IsValid(value) then
tmp[value] = value
end
end
for _, wireEnt in pairs(tmp) do
self:RemoveSingleEntity(wireEnt)
end
end
function ENT:RemoveSingleEntity(wireEnt)
if not IsValid(wireEnt) then return end
if not IsValid(wireEnt._WireMapInterfaceEnt) then return end
if wireEnt._WireMapInterfaceEnt ~= self then return end
local id = wireEnt:EntIndex()
local wireEnts = self.WireEntsRegister
if not wireEnts[id] then
return
end
if wireEnt._WMI_RemoveOverrides then
wireEnt:_WMI_RemoveOverrides(self)
end
wireEnts[id] = nil
self.WireEntsSorted = nil
self.WireEntsCount = nil
end
function ENT:UnregisterWireEntityInternal(wireEnt)
if not IsValid(wireEnt) then
return
end
local wireEnts = self.WireEntsRegister
local id = wireEnt:EntIndex()
if not wireEnts[id] then
return
end
if not self.WireEntsUpdated then
self:TriggerHammerOutputSafe("OnWireEntsStartChanging", self)
end
wireEnts[id] = nil
wireEnt._WireMapInterfaceEnt = nil
local hardLimit = self.WireEntsHardLimit or 0
self.WireEntsHardLimit = math.max(hardLimit - 1, 0)
self.WireEntsUpdated = true
self.WireEntsRemoved = true
self:RequestNetworkEntities()
self.WireEntsSorted = nil
self.WireEntsCount = nil
-- Sometimes CurTime() may get lag compensated and "time travels" when the entity is being destoryed by gun fire.
-- So we delay the think past the predicted event time to avoid any race conditions.
self:NextThink(CurTime() + self.MIN_THINK_TIME)
end
function ENT:SanitizeAndSortWiredEntities()
local wireEnts = self.WireEntsRegister
if not wireEnts or table.IsEmpty(wireEnts) then
self.WireEntsSorted = {}
self.WireEntsCount = 0
self.WireEntsHardLimit = nil
return
end
for id, item in pairs(wireEnts) do
local wireEnt = item.ent
if not self:IsWireableEntity(wireEnt) or
not wireEnt._IsWireMapInterfaceSubEntity or
not wireEnt._WireMapInterfaceEnt_Data or
not wireEnt._WMI_GetSpawnId
then
-- Remove invalid/broken wire entities.
if IsValid(wireEnt) then
if wireEnt._WMI_RemoveOverrides then
wireEnt:_WMI_RemoveOverrides(self)
end
end
wireEnts[id] = nil
end
end
local count = 0
local wireEntsSorted = {}
for id, item in SortedPairsByMemberValue(wireEnts, "cid", false) do
local wireEnt = item.ent
if self:CheckEntLimit(count, wireEnt) then
count = count + 1
wireEntsSorted[count] = wireEnt
else
-- Remove newest wire entities first if limit is exhausted.
if wireEnt._WMI_RemoveOverrides then
wireEnt:_WMI_RemoveOverrides(self)
end
wireEnts[id] = nil
end
end
self.WireEntsSorted = wireEntsSorted
self.WireEntsCount = count
self.WireEntsHardLimit = nil
end
function ENT:GetWiredEntities()
if not self.WireEntsSorted then
self:SanitizeAndSortWiredEntities()
end
return self.WireEntsSorted
end
function ENT:GetWiredEntityCount()
if not self.WireEntsCount then
self:SanitizeAndSortWiredEntities()
end
return self.WireEntsCount
end
function ENT:SetWiredEntities(entities)
if not entities then return end
self:RemoveAllEntities()
self:AddEntitiesByTable(entities)
end