Skip to content

Commit 2942ec2

Browse files
committed
updated for Factorio 0.12.11
1 parent 58f7514 commit 2942ec2

3 files changed

Lines changed: 65 additions & 51 deletions

File tree

control.lua

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ typeToSlot["furnace"] = defines.inventory.assembling_machine_modules
1818
typeToSlot["rocket-silo"] = defines.inventory.assembling_machine_modules
1919
typeToSlot["beacon"] = 1
2020

21-
nameToSlots = {}
22-
local metaitem = game.forces.player.recipes["mi-meta"].ingredients
23-
24-
for i, ent in pairs(metaitem) do
25-
nameToSlots[ent.name] = ent.amount
26-
end
27-
28-
game.forces.player.technologies["mi-meta-productivityRecipes"].reload()
29-
productivityRecipes = game.forces.player.technologies["mi-meta-productivityRecipes"].effects
30-
productivityAllowed = #productivityRecipes > 0 and {} or false
31-
for _, recipe in pairs(productivityRecipes) do
32-
productivityAllowed[recipe.recipe] = true
33-
end
34-
3521
function subPos(p1,p2)
3622
local p2 = p2 or {x=0,y=0}
3723
return {x=p1.x-p2.x, y=p1.y-p2.y}
@@ -60,7 +46,7 @@ function hasPocketBots(player)
6046
return port
6147
end
6248

63-
game.on_event(defines.events.on_marked_for_deconstruction, function(event)
49+
script.on_event(defines.events.on_marked_for_deconstruction, function(event)
6450
local status, err = pcall(function()
6551
local entity = event.entity
6652
local deconstruction = false
@@ -163,8 +149,8 @@ game.on_event(defines.events.on_marked_for_deconstruction, function(event)
163149
entity.cancel_deconstruction(entity.force)
164150
return
165151
end
166-
if productivityAllowed and entity.type == "assembling-machine" then
167-
if entity.recipe and not productivityAllowed[entity.recipe.name] == true then
152+
if global.productivityAllowed and entity.type == "assembling-machine" then
153+
if entity.recipe and not global.productivityAllowed[entity.recipe.name] == true then
168154
player.print("Can't use "..module.." with recipe: " .. entity.recipe.name)
169155
entity.cancel_deconstruction(entity.force)
170156
return
@@ -229,7 +215,6 @@ game.on_event(defines.events.on_marked_for_deconstruction, function(event)
229215
end)
230216

231217
local function initGlob()
232-
233218
if not global.version or global.version < "0.0.2" then
234219
global.config = {}
235220
global["config-tmp"] = {}
@@ -245,20 +230,41 @@ local function initGlob()
245230
global["config-tmp"] = global["config-tmp"] or {}
246231
global["storage"] = global["storage"] or {}
247232
global.guiVersion = global.guiVersion or {}
233+
global.nameToSlots = global.nameToSlots or {}
234+
global.productivityAllowed = global.productivityAllowed or {}
248235

249-
if global.version < "0.0.7" then
250-
global["storage"] = {}
251-
global.version = "0.0.7"
236+
global.version = "0.1.1"
237+
end
238+
239+
local function getMetaItemData()
240+
local metaitem = game.forces.player.recipes["mi-meta"].ingredients
241+
242+
for i, ent in pairs(metaitem) do
243+
global.nameToSlots[ent.name] = ent.amount
252244
end
253245

254-
if global.version < "0.0.9" then
255-
global.entitiesToInsert = {}
256-
global.removeTicks = {}
257-
global.guiVersion = {}
258-
global.version = "0.0.9"
246+
game.forces.player.technologies["mi-meta-productivityRecipes"].reload()
247+
productivityRecipes = game.forces.player.technologies["mi-meta-productivityRecipes"].effects
248+
global.productivityAllowed = #productivityRecipes > 0 and global.productivityAllowed or false
249+
for _, recipe in pairs(productivityRecipes) do
250+
global.productivityAllowed[recipe.recipe] = true
259251
end
252+
end
260253

261-
--hanndle removed items here
254+
local function oninit()
255+
initGlob()
256+
getMetaItemData()
257+
script.on_event(defines.events.on_tick, function() update_gui() end)
258+
end
259+
260+
local function onload()
261+
initGlob()
262+
script.on_event(defines.events.on_tick, function() update_gui() end)
263+
end
264+
265+
local function on_configuration_changed(data)
266+
getMetaItemData()
267+
--handle removed items
262268
local items = game.item_prototypes
263269
for name, p in pairs(global.config) do
264270
for i=#p,1,-1 do
@@ -294,17 +300,6 @@ local function initGlob()
294300
end
295301
end
296302
end
297-
global.version = "0.0.9"
298-
end
299-
300-
local function oninit()
301-
initGlob()
302-
game.on_event(defines.events.on_tick, function() update_gui() end)
303-
end
304-
305-
local function onload()
306-
initGlob()
307-
game.on_event(defines.events.on_tick, function() update_gui() end)
308303
end
309304

310305
function update_gui(player)
@@ -319,6 +314,24 @@ function update_gui(player)
319314
end
320315
gui_init(player)
321316
else
317+
for player, store in pairs(global.storage) do
318+
for name, p in pairs(store) do
319+
for i=#p,1,-1 do
320+
if p[i].from ~= "" and not items[p[i].from] then
321+
global.storage[player][name][i].from = ""
322+
global.storage[player][name][i].to = ""
323+
end
324+
if type(p[i].to) == "table" then
325+
for k, m in pairs(p[i].to) do
326+
if m and not items[m] then
327+
global.storage[player][name][i].to[k] = false
328+
end
329+
end
330+
end
331+
end
332+
end
333+
end
334+
322335
for i,player in pairs(game.players) do
323336
if player.valid and player.connected then
324337
if not global.guiVersion[player.name] then
@@ -332,7 +345,7 @@ function update_gui(player)
332345
end
333346
end
334347
end
335-
game.on_event(defines.events.on_tick, on_tick)
348+
script.on_event(defines.events.on_tick, on_tick)
336349
end)
337350
if not status then
338351
debugDump(err, true)
@@ -358,8 +371,9 @@ function get_config_item(player, index, type1)
358371
return game.get_localised_item_name(global["config-tmp"][player.name][index][type1])
359372
end
360373

361-
game.on_init(oninit)
362-
game.on_load(onload)
374+
script.on_init(oninit)
375+
script.on_load(onload)
376+
script.on_configuration_changed(on_configuration_changed)
363377

364378
function on_tick(event)
365379
if global.removeTicks[event.tick] then
@@ -378,7 +392,7 @@ function on_tick(event)
378392
end
379393
end
380394

381-
game.on_event(defines.events.on_robot_built_entity, function(event)
395+
script.on_event(defines.events.on_robot_built_entity, function(event)
382396
local status, err = pcall(function()
383397
local entity = event.created_entity
384398
if entity.name == "module-inserter-proxy" then
@@ -430,7 +444,7 @@ game.on_event(defines.events.on_robot_built_entity, function(event)
430444
end
431445
end)
432446

433-
game.on_event(defines.events.on_gui_click, function(event)
447+
script.on_event(defines.events.on_gui_click, function(event)
434448
local element = event.element
435449
--debugDump(element.name, true)
436450
local player = game.get_player(event.player_index)
@@ -481,7 +495,7 @@ game.on_event(defines.events.on_gui_click, function(event)
481495
end
482496
end)
483497

484-
game.on_event(defines.events.on_research_finished, function(event)
498+
script.on_event(defines.events.on_research_finished, function(event)
485499
if event.research.name == 'automated-construction' then
486500
for _, player in pairs(event.research.force.players) do
487501
gui_init(player, true)
@@ -506,7 +520,7 @@ end
506520
function saveVar(var, name)
507521
local var = var or global
508522
local n = name or ""
509-
game.makefile("module"..n..".lua", serpent.block(var, {name="glob"}))
523+
game.write_file("module"..n..".lua", serpent.block(var, {name="glob"}))
510524
end
511525

512526
remote.add_interface("mi",

gui.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ function gui_set_rule(player, type1, index)
416416
end
417417
end
418418
end
419-
if stack.type ~= "empty" and not nameToSlots[stack.name] then
419+
if stack.type ~= "empty" and not global.nameToSlots[stack.name] then
420420
gui_display_message(frame, false, "module-inserter-item-no-slots")
421421
return
422422
end
@@ -430,7 +430,7 @@ function gui_set_rule(player, type1, index)
430430
ruleset_grid["module-inserter-" .. type1 .. "-" .. index].style = style
431431
ruleset_grid["module-inserter-" .. type1 .. "-" .. index].state = false
432432
if type1 == "from" then
433-
--local slots = nameToSlots[global["config-tmp"][player.name][index].from] or "-"
433+
--local slots = global.nameToSlots[global["config-tmp"][player.name][index].from] or "-"
434434
--ruleset_grid["module-inserter-slots-" .. index].caption = slots
435435
gui_update_modules(player, index)
436436
end
@@ -453,7 +453,7 @@ function gui_set_modules(player, index, slot)
453453
local type1 = "to"
454454
local config = global["config-tmp"][player.name][index]
455455
local modules = type(config[type1]) == "table" and config[type1] or {}
456-
local maxSlots = nameToSlots[config.from]
456+
local maxSlots = global.nameToSlots[config.from]
457457
if stack.type == "module" then
458458
if game.entity_prototypes[config.from].type == "beacon" and game.item_prototypes[stack.name].module_effects and game.item_prototypes[stack.name].module_effects["productivity"] then
459459
if game.item_prototypes[stack.name].module_effects["productivity"] ~= 0 then
@@ -475,7 +475,7 @@ end
475475

476476
function gui_update_modules(player, index)
477477
local frame = player.gui.left["module-inserter-config-frame"]
478-
local slots = nameToSlots[global["config-tmp"][player.name][index].from] or 1
478+
local slots = global.nameToSlots[global["config-tmp"][player.name][index].from] or 1
479479
local modules = global["config-tmp"][player.name][index].to
480480
local flow = frame["module-inserter-ruleset-grid"]["module-inserter-slotflow-" .. index]
481481
for i=#flow.children_names,1,-1 do

info.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "ModuleInserter",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"title": "Module Inserter",
55
"author": "Choumiko",
66
"homepage": "",
7-
"dependencies": ["base >= 0.12.6"],
7+
"dependencies": ["base >= 0.12.11"],
88
"description": "Mass insert modules into machines with pocket bots"
99
}

0 commit comments

Comments
 (0)