-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetwork.lua
192 lines (176 loc) · 4.17 KB
/
Network.lua
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
local devices = require "devices"
---@class Network
local Network = {}
Network.__index = Network
---@param map Map
---@param name string
function Network.new(map, name)
if name == nil then
local i = 0
name = "network_" .. i
while map.networks[name] ~= nil do
i = i + 1
name = "network_" .. i
end
end
local self = setmetatable({
name=name,
remark="No Remark.",
---@type table<string, string|number>
fields=setmetatable({_fields={}}, {
__index=function(self, key)
if key == "_fields" then return rawget(self, key) end
local fields = rawget(self, "_fields")
for fieldName, _ in pairs(fields) do
if fieldName:lower() == key:lower() then
return fields[fieldName]
end
end
return nil
end,
__newindex=function(self, key, value)
local fields = rawget(self, "_fields")
for fieldName, _ in pairs(fields) do
if fieldName:lower() == key:lower() then
fields[fieldName] = value
return
end
end
fields[key] = value
end
}),
color={math.random(0, 255)/255, math.random(0, 255)/255, math.random(0, 255)/255},
---@type Device[]
objects={},
---@type Map
map=map,
---@type number[]
hull={}
}, Network)
return self
end
function Network:destroy()
self.map:removeNetwork(self)
-- Invalidate the object in-case of left over references we WANT it to error so we can fix
for _, obj in pairs(self.objects) do
obj:destroy()
end
self.objects = nil
self.hull = nil
self.map = nil
self.color = nil
self.name = nil
self.remark = nil
self.fields = nil
end
function Network.deserialize(map, save)
local self = Network.new(map, save.name)
self.color = save.extensions.color or self.color
if save.extensions and save.extensions.fields then
for fieldName, fieldValue in pairs(save.extensions.fields) do
self.fields[fieldName] = fieldValue
end
end
for _, deviceSave in pairs(save.devices) do
deviceSave._tagged_device:deserialize(self, deviceSave)
end
return self
end
function Network:serialize()
local objects = {}
for _, object in pairs(self.objects) do
if getmetatable(object) == devices.DeviceMeta then
table.insert(objects, setmetatable(object:serialize(), {__yaml_tag="!" .. object:GetSaveName()}))
end
end
return setmetatable({
name=self.name,
remark=self.remark,
devices=objects,
extensions={
fields=self.fields._fields,
color=self.color
}
}, {__yaml_anchor=self.name})
end
function Network:generateHull()
local points = {}
for _, obj in pairs(self.objects) do
for _, point in pairs(obj:getBounds()) do
table.insert(points, point)
end
end
self.hull = MonotoneChain(points)
end
function Network:update(dt)
local drawableObjectCount = 0
for _, obj in pairs(self.objects) do
if getmetatable(obj) == devices.DeviceMeta then
drawableObjectCount = drawableObjectCount + 1
end
end
if drawableObjectCount <= 0 then
self:destroy()
else
for _, obj in pairs(self.objects) do
obj:update(dt)
end
end
end
---@param obj Device
function Network:addObject(obj)
table.insert(self.objects, obj)
self:generateHull()
end
---@param obj Device
function Network:removeObject(obj)
for i, v in pairs(self.objects) do
if v == obj then
table.remove(self.objects, i)
if #self.objects <= 0 then
self.map:removeNetwork(self)
else
self:generateHull()
end
return true
end
end
return false
end
---@param x number
---@param y number
---@return Device|nil
function Network:getObjectAt(x, y)
for _, obj in pairs(self.objects) do
if getmetatable(obj) == devices.DeviceMeta then
local ox, oy, ow, oh = obj.x, obj.y, obj:getSize()
if IsInside(ox, oy, ox+ow, oy+oh, x, y) then
return obj
end
end
end
end
---@param x number
---@param y number
---@return boolean
function Network:withinBounds(x, y)
return InsidePolygon(self.hull, {x, y})
end
function Network:getField(name)
return self.fields[name] or 0
end
function Network:setField(name, value)
self.fields[name] = value
end
function Network:changeField(oldName, newName)
if self.fields[newName] == nil then
self.fields[newName] = self:getField(oldName)
end
for _, obj in pairs(self.objects) do
if obj:hasFieldWithName(oldName) then
return
end
end
self.fields[oldName] = nil
end
return Network