forked from mspielberg/factorio-railloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configchange.lua
442 lines (411 loc) · 12.6 KB
/
configchange.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
local delaydestroy = require "delaydestroy"
local inserter_config = require "inserterconfig"
local util = require "util"
local version = require "version"
local M = {}
local all_migrations = {}
local function add_migration(migration)
all_migrations[#all_migrations+1] = migration
end
function M.on_mod_version_changed(old)
old = version.parse(old)
for _, migration in ipairs(all_migrations) do
if version.between(old, migration.low, migration.high) then
log("running world migration "..migration.name)
migration.task()
end
end
end
add_migration{
name = "v0_3_0_change_work_queue_name",
low = {0,0,0},
high = {0,3,0},
task = function()
global.unconfigured_loaders = {}
local t = global.unconfigured_loaders
for _, e in ipairs(global.unconfigured_inserters) do
if e.valid then
local loader = e.surface.find_entities_filtered{
type = "container",
position = e.position,
force = e.force,
}[1]
t[#t+1] = loader
end
end
global.unconfigured_inserters = nil
global.unconfigured_inserters_iter = nil
inserter_config.on_load()
end,
}
add_migration{
name = "v0_3_0_add_additional_inserters",
low = {0,0,0},
high = {0,3,0},
task = function()
for _, s in pairs(game.surfaces) do
for _, e in ipairs(s.find_entities_filtered{type = "inserter"}) do
if string.find(e.name, "railu?n?loader%-.*inserter") then
local new_inserter = s.create_entity{
name = e.name,
position = e.position,
direction = e.direction,
force = e.force
}
new_inserter.destructible = false
for i=1,e.filter_slot_count do
new_inserter.set_filter(i, e.get_filter(i))
end
end
end
end
end,
}
add_migration{
name = "v0_3_7_add_ghost_registry",
low = {0,0,0},
high = {0,3,7},
task = function()
global.ghosts = {}
end,
}
add_migration{
name = "v0_4_0_add_delayed_destroy_queue",
low = {0,0,0},
high = {0,4,0},
task = function()
global.entities_to_destroy = {}
end,
}
add_migration{
name = "v0_4_0_change_unconfigured_loader_queue_indexing",
low = {0,0,0},
high = {0,4,0},
task = function()
local new = {}
for _, v in pairs(global.unconfigured_loaders) do
if v.valid then
new[v.unit_number] = v
end
end
global.unconfigured_loaders = new
global.unconfigured_loaders_iter = nil
end,
}
local function update_proxy_direction(entity)
local is_ns = entity.direction == defines.direction.east or defines.direction.west
entity.direction = is_ns and defines.direction.east or defines.direction.north
end
add_migration{
name = "v0_4_0_relocate_proxy_ghosts",
low = {0,0,0},
high = {0,4,0},
task = function()
for _, s in pairs(game.surfaces) do
local ghosts = s.find_entities_filtered{
name = "entity-ghost",
}
for _, g in ipairs(ghosts) do
if g.valid and g.ghost_name:find("^railu?n?loader%-placement%-proxy$") then
local old_position_key = util.position_key(g)
--re-orient
local new_position = util.moveposition(g.position, util.offset(g.direction, 1.5, 0))
g.teleport(new_position)
update_proxy_direction(g)
-- fix up any recorded circuit connections
local new_position_key = util.position_key(g)
local connections = global.ghosts[old_position_key]
if connections then
global.ghosts[new_position_key] = connections
global.ghosts[old_position_key] = nil
end
-- remove any underlying rail ghosts
local rail_ghosts = s.find_entities_filtered{
name = "entity-ghost",
ghost_type = "straight-rail",
area = g.bounding_box,
}
for _, rg in ipairs(rail_ghosts) do
rg.destroy()
end
end
end
end
end
}
--[[
foreach player
foreach inventory
foreach slot
foreach surface
foreach entity
foreach inventory
foreach slot
]]
local function all_blueprints()
local next_player
do
local f, s, var = pairs(game.players)
next_player = function()
local player
var, player = f(s, var)
return player
end
end
local next_surface
do
local f, s, var = pairs(game.surfaces)
next_surface = function()
local surface
var, surface = f(s, var)
return surface
end
end
local max_inventory_id = 1
for _, inventory_id in pairs(defines.inventory) do
if inventory_id > max_inventory_id then
max_inventory_id = inventory_id
end
end
local done_with_players = false
local entity = next_player()
local entities
local entities_iter
local inventories_iter = 0
local inventory
local inventory_iter -- starting slot of inventory to examine on next execution
local item_inventory
local item_inventory_iter
local iter
iter = function()
if item_inventory then
for i=item_inventory_iter,#item_inventory do
local stack = item_inventory[i]
if stack.valid_for_read then
log("item-with-inventory slot " .. i .. " contains " .. stack.name)
end
if stack.valid_for_read and stack.name == "blueprint" then
item_inventory_iter = i + 1
return stack
end
end
item_inventory = nil
end
if inventory then
for i=inventory_iter,#inventory do
local stack = inventory[i]
if stack.valid_for_read then
if stack.name == "blueprint" then
log("inventory slot " .. i .. " contains " .. stack.name)
inventory_iter = i + 1
return stack
elseif stack.is_item_with_inventory then
log("examining item-with-inventory " .. stack.name .. " in slot " .. i)
inventory_iter = i + 1
item_inventory = stack.get_inventory(defines.inventory.item_main)
item_inventory_iter = 1
return iter()
end
end
end
end
-- search for next inventory
if entity then
while true do
inventories_iter = inventories_iter + 1
if inventories_iter > max_inventory_id then
break
end
inventory = entity.get_inventory(inventories_iter)
if inventory and not inventory.is_empty() then
log("examining inventory " .. inventories_iter .. " of entity " .. entity.name .. " (" .. util.position_key(entity) .. ")")
inventory_iter = 1
return iter()
end
end
end
-- ran out of inventories, look for next entity
if not done_with_players then
entity = next_player()
if not entity then
done_with_players = true
return iter()
end
end
if entities then
entities_iter, entity = next(entities, entities_iter)
if entity then
if entity.name == "item-on-ground" and entity.stack.name == "blueprint" then
log("found blueprint on ground (" .. util.position_key(entity) .. ")")
return entity.stack
else
inventories_iter = 0
return iter()
end
end
end
local surface = next_surface()
if not surface then
return nil
end
entities = surface.find_entities()
return iter()
end
return iter
end
add_migration{
name = "v0_4_0_relocate_blueprint_proxies",
low = {0,0,0},
high = {0,4,0},
task = function()
for bp in all_blueprints() do
if bp.is_blueprint_setup() then
local entities = bp.get_blueprint_entities()
if entities then
for _, e in ipairs(entities) do
if e.name:find("^railu?n?%loader%-placement%-proxy") then
log("updating " .. e.name .. " at " .. e.position.x .. "," .. e.position.y)
e.position = util.moveposition(e.position, util.offset(e.direction, 1.5, 0))
update_proxy_direction(e)
for k, e2 in ipairs(entities) do
local prototype = game.entity_prototypes[e2.name]
if prototype.type == "straight-rail" then
if (e.direction == defines.direction.north
and e2.position.x == e.position.x
and e2.position.y <= e.position.y + 2
and e2.position.y >= e.position.y - 2)
or (e.direction == defines.direction.east
and e2.position.y == e.position.y
and e2.position.x <= e.position.x + 2
and e2.position.x >= e.position.x - 2) then
log("removing " .. e2.name .. " at " .. e2.position.x .. "," .. e2.position.y)
entities[k] = nil
end
end
end
end
end
bp.set_blueprint_entities(entities)
end
end
end
end
}
add_migration{
name = "v0_5_0_circuit_connect_inserters",
low = {0,0,0},
high = {0,5,0},
task = function()
for _,s in pairs(game.surfaces) do
for _, chest in ipairs(s.find_entities_filtered{type="container"}) do
local type = util.railloader_type(chest.name)
if type then
for _, inserter_name in ipairs{"rail"..type.."-inserter", "rail"..type.."-universal-inserter"} do
for _, inserter in ipairs(s.find_entities_filtered{name = inserter_name, position = chest.position}) do
for _, wire_type in ipairs{"red", "green"} do
inserter.connect_neighbour{
target_entity = chest,
wire = defines.wire_type[wire_type],
}
end
local behavior = inserter.get_or_create_control_behavior()
behavior.circuit_condition = {
condition = {
comparator = "=",
first_signal = {type = "virtual", name = "railloader-disable"},
}
}
end
end
end
end
end
end
}
add_migration{
name = "v0_5_2_circuit_connect_interface_inserters",
low = {0,0,0},
high = {0,5,2},
task = function()
for _,s in pairs(game.surfaces) do
for _, chest in ipairs(s.find_entities_filtered{type="container"}) do
local type = util.railloader_type(chest.name)
if type then
local inserter_name = "rail"..type.."-interface-inserter"
for _, inserter in ipairs(s.find_entities_filtered{name = inserter_name, position = chest.position}) do
for _, wire_type in ipairs{"red", "green"} do
inserter.connect_neighbour{
target_entity = chest,
wire = defines.wire_type[wire_type],
}
end
local behavior = inserter.get_or_create_control_behavior()
behavior.circuit_condition = {
condition = {
comparator = "=",
first_signal = {type = "virtual", name = "railloader-disable"},
}
}
end
end
end
end
end
}
add_migration{
name = "v1_0_6_remove_ghost_registry",
low = {0,0,0},
high = {1,0,6},
task = function()
global.ghosts = nil
end
}
add_migration{
name = "v1_1_0_add_previous_blueprint_global",
low = {0,0,0},
high = {1,1,0},
task = function()
global.previous_opened_blueprint_for = {}
end
}
add_migration{
name = "remove_orphan_structures",
low = {0,0,0},
high = {99,0,0},
task = function()
for _,s in pairs(game.surfaces) do
for _,e in ipairs(s.find_entities_filtered{type="simple-entity"}) do
local type = util.railloader_type(e.name)
if type then
local proto = game.entity_prototypes["rail" .. type .. "-chest"]
local brl = s.find_entity(proto.name, e.position)
if not brl then
local area = {
left_top = {
e.position.x + proto.collision_box.left_top.x,
e.position.y + proto.collision_box.left_top.y,
},
right_bottom = {
e.position.x + proto.collision_box.right_bottom.x,
e.position.y + proto.collision_box.right_bottom.y,
},
}
for _, ent in ipairs(s.find_entities_filtered{area = area}) do
if ent.type == "inserter" then
ent.destroy()
elseif string.find(ent.name, "^railu?n?loader%-structure") then
ent.destroy()
elseif ent.type == "straight-rail" then
local success = ent.destroy()
if not success then
delaydestroy.register_to_destroy(ent)
end
end
end
end
end
end
end
end,
}
return M