forked from mspielberg/factorio-miniloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
381 lines (340 loc) · 11.7 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
local blueprint = require("lualib.blueprint")
local circuit = require("circuit")
local configchange = require("configchange")
local event = require("lualib.event")
local miniloader = require("lualib.miniloader")
local _ = require("gui")
local snapping = require("snapping")
local util = require("lualib.util")
local compat_pickerextended = require("compat.pickerextended")
local use_snapping = settings.global["miniloader-snapping"].value
--[[
loader_type = "input"
+------------------+
| |
| P |
| |
| | |
| | | chest dir
| | |
| | v
| |
+------------------+
D D
--
loader_type = "output"
+------------------+
| |
| D D |
| |
| | |
| | | chest dir
| | |
| | v
| |
+------------------+
P
--
D: drop positions
P: pickup position
]]
local function register_bobs_blacklist()
for _, interface_name in ipairs{"bobinserters", "boblogistics"} do
local interface = remote.interfaces[interface_name]
if interface and interface["blacklist_inserter"] then
for entity_name in pairs(game.entity_prototypes) do
if util.is_miniloader_inserter_name(entity_name) then
remote.call(interface_name, "blacklist_inserter", entity_name)
end
end
end
end
end
-- Event Handlers
local function on_init()
circuit.on_init()
compat_pickerextended.on_load()
end
local function on_load()
circuit.on_load()
compat_pickerextended.on_load()
end
local function on_configuration_changed(configuration_changed_data)
local mod_change = configuration_changed_data.mod_changes["miniloader"]
if mod_change and mod_change.old_version and mod_change.old_version ~= mod_change.new_version then
configchange.on_mod_version_changed(mod_change.old_version)
end
register_bobs_blacklist()
configchange.fix_inserter_counts()
end
local function on_built_miniloader(entity, orientation)
if not orientation then
orientation = {direction = util.opposite_direction(entity.direction), type = "input"}
end
return miniloader.fixup(entity, orientation)
end
local function on_robot_built(ev)
local entity = ev.created_entity
if util.is_miniloader_inserter(entity) then
on_built_miniloader(entity, util.orientation_from_inserters(entity))
end
end
local function on_script_built(ev)
local entity = ev.entity
if entity and util.is_miniloader_inserter(entity) then
on_built_miniloader(entity, util.orientation_from_inserters(entity))
end
end
local function on_script_revive(ev)
local entity = ev.entity
if entity and util.is_miniloader_inserter(entity) then
on_built_miniloader(entity, util.orientation_from_inserters(entity))
end
end
local miniloader_mined
local function on_player_built(ev)
local entity = ev.created_entity
if ev.mod_name then
-- might be circuit connected or have filter settings
on_robot_built(ev)
return
end
if util.is_miniloader_inserter(entity) then
local orientation
if miniloader_mined and
miniloader_mined.tick == ev.tick and
miniloader_mined.player_index == ev.player_index then
orientation = miniloader_mined.orientation
end
local loader = on_built_miniloader(entity, orientation)
if use_snapping and not orientation then
-- adjusts direction & loader_type
snapping.snap_loader(loader)
end
else
snapping.check_for_loaders(ev)
end
end
local function on_rotated(ev)
local entity = ev.entity
if util.is_miniloader_inserter(entity) then
local miniloader = util.find_miniloaders{
surface = entity.surface,
position = entity.position,
force = entity.force,
}[1]
miniloader.rotate{ by_player = game.players[ev.player_index] }
util.update_inserters(miniloader)
elseif util.is_miniloader(entity) then
util.update_inserters(entity)
elseif use_snapping then
snapping.check_for_loaders(ev)
end
end
local function on_miniloader_mined(ev)
local entity = ev.entity
local buffer = ev.buffer and ev.buffer.valid and ev.buffer
local inserters = util.get_loader_inserters(entity)
if buffer and inserters[1] then
local _, item_to_place = next(inserters[1].prototype.items_to_place_this)
buffer.insert{desired_count=1, name=item_to_place.name}
end
for i=1,#inserters do
-- return items to player / robot if mined
if buffer and inserters[i] ~= entity and inserters[i].held_stack.valid_for_read then
buffer.insert(inserters[i].held_stack)
end
inserters[i].destroy()
end
local chest = entity.surface.find_entity("miniloader-target-chest", entity.position)
if chest then
chest.destroy()
end
end
local function on_miniloader_inserter_mined(ev)
local entity = ev.entity
local buffer = ev.buffer and ev.buffer.valid and ev.buffer
local loader = entity.surface.find_entities_filtered{
position = entity.position,
type = "loader-1x1",
}[1]
if loader then
if buffer then
for i=1,2 do
local tl = loader.get_transport_line(i)
for j=1,math.min(#tl, 256) do
buffer.insert(tl[j])
end
tl.clear()
end
end
loader.destroy()
end
local inserters = util.get_loader_inserters(entity)
for i=1,#inserters do
if inserters[i] ~= entity then
-- return items in inserter hand to player / robot if mined
if buffer and inserters[i].held_stack.valid_for_read then
buffer.insert(inserters[i].held_stack)
end
inserters[i].destroy()
end
end
local chest = entity.surface.find_entity("miniloader-target-chest", entity.position)
if chest then
chest.destroy()
end
end
local function on_mined(ev)
local entity = ev.entity
if util.is_miniloader(entity) then
on_miniloader_mined(ev)
elseif util.is_miniloader_inserter(entity) then
on_miniloader_inserter_mined(ev)
end
end
local function on_placed_blueprint(ev, player, bp)
local surface = player.surface
local bp_area = blueprint.bounding_box(bp)
local surface_area = util.move_box(
util.rotate_box(bp_area, ev.direction),
ev.position
)
event.on_next_tick(function()
if not surface.valid then return end
local inserter_entities = surface.find_entities_filtered{
area = surface_area,
type = "inserter",
}
for _, e in pairs(inserter_entities) do
if util.is_miniloader_inserter(e) then
circuit.sync_behavior(e)
circuit.sync_filters(e)
circuit.sync_partner_connections(e)
end
end
end)
end
local function on_put_item(ev)
local player_index = ev.player_index
local player = game.players[player_index]
local cursor_stack = player.cursor_stack
if cursor_stack and blueprint.is_setup_bp(cursor_stack) then
return on_placed_blueprint(ev, player, cursor_stack)
end
local entities = player.surface.find_entities_filtered{position = ev.position}
for _, entity in ipairs(entities) do
if entity.name == "entity-ghost" and util.is_miniloader_inserter_name(entity.ghost_name) then
-- building over a ghost
miniloader_mined = {
tick = ev.tick,
player_index = player_index,
orientation = util.orientation_from_inserters(entity),
}
return
end
end
end
local function on_player_mined_entity(ev)
local entity = ev.entity
if util.is_miniloader_inserter(entity) then
-- might be a fast replace, so store current orientation
miniloader_mined = {
tick = ev.tick,
player_index = ev.player_index,
orientation = util.orientation_from_inserters(entity),
}
end
on_mined(ev)
end
local function on_robot_pre_mined(ev)
if ev.instant_deconstruction then
on_mined(ev)
end
end
local function on_entity_settings_pasted(ev)
local src = ev.source
local dst = ev.destination
if util.is_miniloader_inserter(src) and util.is_miniloader_inserter(dst)
or util.is_miniloader(src) and util.is_miniloader(dst) then
circuit.sync_behavior(dst)
circuit.sync_filters(dst)
local src_loader = src.surface.find_entities_filtered{type="loader-1x1",position=src.position}[1]
local dst_loader = dst.surface.find_entities_filtered{type="loader-1x1",position=dst.position}[1]
if src_loader and dst_loader then
dst_loader.loader_type = src_loader.loader_type
util.update_inserters(dst_loader)
end
end
end
local function on_setup_blueprint(ev)
local player = game.players[ev.player_index]
local bp = player.blueprint_to_setup
if not bp or not bp.valid_for_read then
bp = player.cursor_stack
end
blueprint.filter_miniloaders(bp)
end
local function on_marked_for_deconstruction(ev)
local entity = ev.entity
if not (util.is_miniloader(entity) or util.is_miniloader_inserter(entity)) then return end
for _, ent in ipairs(entity.surface.find_entities_filtered{position=entity.position}) do
-- order_deconstruction() causes event handlers to be fired which may invalidate entities
if ent.valid and (util.is_miniloader(ent) or util.is_miniloader_inserter(ent)) then
if not ent.to_be_deconstructed(ent.force) then
ent.order_deconstruction(ent.force)
end
end
end
end
local function on_canceled_deconstruction(ev)
local entity = ev.entity
for _, ent in ipairs(entity.surface.find_entities_filtered{position=entity.position}) do
if util.is_miniloader(ent) or util.is_miniloader_inserter(ent) then
if ent.to_be_deconstructed(ent.force) then
ent.cancel_deconstruction(ent.force)
end
end
end
end
local function on_marked_for_upgrade(ev)
local entity = ev.entity
if not util.is_miniloader_inserter(entity) then return end
local main_inserter = entity.surface.find_entity(entity.name, entity.position)
if entity == main_inserter then return end
local force = ev.player_index and game.get_player(ev.player_index).force or entity.force
entity.cancel_upgrade(force)
end
-- lifecycle events
script.on_init(on_init)
script.on_load(on_load)
script.on_configuration_changed(on_configuration_changed)
-- entity events
event.register(defines.events.on_built_entity, on_player_built)
event.register(defines.events.on_robot_built_entity, on_robot_built)
event.register(defines.events.on_player_rotated_entity, on_rotated)
event.register(defines.events.on_player_mined_entity, on_player_mined_entity)
event.register(defines.events.on_robot_pre_mined, on_robot_pre_mined)
event.register(defines.events.on_robot_mined_entity, on_mined)
event.register(defines.events.on_entity_died, on_mined)
event.register(defines.events.script_raised_built, on_script_built)
event.register(defines.events.script_raised_revive, on_script_revive)
event.register(defines.events.script_raised_destroy, on_mined)
event.register(defines.events.on_entity_settings_pasted, on_entity_settings_pasted)
event.register(defines.events.on_put_item, on_put_item)
event.register(defines.events.on_player_setup_blueprint, on_setup_blueprint)
event.register(defines.events.on_marked_for_deconstruction, on_marked_for_deconstruction)
event.register(defines.events.on_canceled_deconstruction, on_canceled_deconstruction)
event.register(defines.events.on_marked_for_upgrade, on_marked_for_upgrade)
event.register(defines.events.on_runtime_mod_setting_changed, function(ev)
if ev.setting == "miniloader-snapping" then
use_snapping = settings.global["miniloader-snapping"].value
elseif ev.setting == "miniloader-lock-stack-sizes" then
local size = settings.global["miniloader-lock-stack-sizes"].value and 1 or 0
miniloader.forall(function(surface, miniloader)
for _, inserter in pairs(util.get_loader_inserters(miniloader)) do
inserter.inserter_stack_size_override = size
end
end)
end
end)