-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
301 lines (267 loc) · 7.65 KB
/
control.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
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
-- Functions --
local function calcRadius()
local drillTypes = game.get_filtered_entity_prototypes {
{
filter = 'type',
type = 'mining-drill'
}
}
local radius = 0
for _, prototype in pairs(drillTypes) do
local r = prototype.mining_drill_radius
if r > radius then
radius = r
end
end
global.radius = math.ceil(radius + 1)
end
local function miningDrill(drill, checks)
if not drill.valid then
return
end
if drill.status ~= defines.entity_status.no_minable_resources then
if checks < 5 then
table.insert(global.list, {
entity = drill,
checks = checks + 1
})
end
return
end
if drill.drop_target ~= nil then
table.insert(global.list, {
entity = drill.drop_target,
checks = 0
})
end
local position = drill.position
local name = drill.name
if drill.order_deconstruction(drill.force, drill.last_user) then
log('Marked '..name..' for Deconstruction | x: '..position.x..' | y: '..position.y)
else
log('Failed to Mark '..name..' for Deconstruction | x: '..position.x..' | y: '..position.y)
end
end
local function transportBelt(belt, checks)
if not belt.valid then
return
end
if #(belt.belt_neighbours.inputs) > 0 then
return
end
for _,drill in pairs(game.surfaces[belt.surface.index].find_entities_filtered {
type = 'mining-drill',
radius = global.radius,
position = belt.position,
to_be_deconstructed = false
}) do
if drill.valid and drill.drop_target ~= nil and drill.drop_target.position.x == belt.position.x and drill.drop_target.position.y == belt.position.y then
return
end
end
for i = 1, belt.get_max_transport_line_index(), 1 do
local contents = belt.get_transport_line(i).get_contents()
local key = next(contents)
if key then
if checks < 360 then
table.insert(global.list, {
entity = belt,
checks = checks + 1
})
end
return
end
end
for _,output in pairs(belt.belt_neighbours.outputs) do
table.insert(global.list, {
entity = output,
checks = 0
})
end
local position = belt.position
local name = belt.name
if belt.order_deconstruction(belt.force, belt.last_user) then
log('Marked ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
else
log('Failed to Mark ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
end
end
local function undergroundBelt(tunnel, checks)
if not tunnel.valid then
return
end
if #(tunnel.belt_neighbours.inputs) > 0 then
return
end
if tunnel.belt_to_ground_type == 'output' and tunnel.neighbours ~= nil then
return
end
for _,drill in pairs(game.surfaces[tunnel.surface.index].find_entities_filtered {
type = 'mining-drill',
radius = global.radius,
position = tunnel.position,
to_be_deconstructed = false
}) do
if drill.valid and drill.drop_target ~= nil and drill.drop_target.position.x == tunnel.position.x and drill.drop_target.position.y == tunnel.position.y then
return
end
end
for i = 1, tunnel.get_max_transport_line_index(), 1 do
local contents = tunnel.get_transport_line(i).get_contents()
local key = next(contents)
if key then
if checks < 360 then
table.insert(global.list, {
entity = tunnel,
checks = checks + 1
})
end
return
end
end
if tunnel.belt_to_ground_type == 'output' then
for _,output in pairs(tunnel.belt_neighbours.outputs) do
table.insert(global.list, {
entity = output,
checks = 0
})
end
else
table.insert(global.list, {
entity = tunnel.neighbours,
checks = 0
})
end
local position = tunnel.position
local name = tunnel.name
if tunnel.order_deconstruction(tunnel.force, tunnel.last_user) then
log('Marked ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
else
log('Failed to Mark ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
end
end
local function splitter(splitter, checks)
if not splitter.valid then
return
end
if #(splitter.belt_neighbours.inputs) > 0 then
return
end
for _, drill in pairs(game.surfaces[splitter.surface.index].find_entities_filtered {
type = 'mining-drill',
radius = global.radius,
position = splitter.position,
to_be_deconstructed = false
}) do
if drill.valid and drill.drop_target ~= nil and drill.drop_target.position.x == splitter.position.x and
drill.drop_target.position.y == splitter.position.y then
return
end
end
for i = 1, splitter.get_max_transport_line_index(), 1 do
local contents = splitter.get_transport_line(i).get_contents()
local key = next(contents)
if key then
if checks < 360 then
table.insert(global.list, {
entity = splitter,
checks = checks + 1
})
end
return
end
end
for _,output in pairs(splitter.belt_neighbours.outputs) do
table.insert(global.list, {
entity = output,
checks = 0
})
end
local position = splitter.position
local name = splitter.name
if splitter.order_deconstruction(splitter.force, splitter.last_user) then
log('Marked ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
else
log('Failed to Mark ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
end
end
local function container(chest, checks)
if not chest.valid then
return
end
for _,drill in pairs(game.surfaces[chest.surface.index].find_entities_filtered {
type = 'mining-drill',
radius = global.radius,
position = chest.position,
to_be_deconstructed = false
}) do
if drill.valid and drill.drop_target ~= nil and drill.drop_target.position.x == chest.position.x and drill.drop_target.position.y == chest.position.y then
return
end
end
if not chest.get_inventory(defines.inventory.chest).is_empty() then
if checks < 3600 then
table.insert(global.list, {
entity = chest,
checks = checks + 1
})
end
return
end
local position = chest.position
local name = chest.name
if chest.order_deconstruction(chest.force, chest.last_user) then
log('Marked ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
else
log('Failed to Mark ' .. name .. ' for Deconstruction | x: ' .. position.x .. ' | y: ' .. position.y)
end
end
-- Events --
script.on_init(function()
global.list = {}
calcRadius()
end)
script.on_configuration_changed(calcRadius)
script.on_event(defines.events.on_resource_depleted, function(event)
local prototype = event.entity.prototype
if prototype.infinite_resource then
return
end
if prototype.resource_category == 'basic-solid' then
local drills = game.surfaces[event.entity.surface.index].find_entities_filtered {
type = 'mining-drill',
radius = global.radius,
position = event.entity.position,
to_be_deconstructed = false
}
for _,drill in pairs(drills) do
table.insert(global.list, {
entity = drill,
checks = 0
})
end
end
end)
script.on_nth_tick(10, function()
local key, value = next(global.list)
if not key then
return
end
global.list[key] = nil
if value.entity == nil or not value.entity.valid then
return
end
if value.entity.type == 'mining-drill' then
miningDrill(value.entity, value.checks)
elseif value.entity.type == 'transport-belt' and settings.global['blackaslight-belts'].value then
transportBelt(value.entity, value.checks)
elseif value.entity.type == 'underground-belt' and settings.global['blackaslight-belts'].value then
undergroundBelt(value.entity, value.checks)
elseif value.entity.type == 'splitter' and settings.global['blackaslight-belts'].value then
splitter(value.entity, value.checks)
elseif value.entity.type == 'container' and settings.global['blackaslight-chests'].value then
container(value.entity, value.checks)
else
log('entity_name: ' .. value.entity.name .. ' | entity_type: ' .. value.entity.type)
end
end)