Skip to content

Commit 3788014

Browse files
committed
remove invalid items from config/storage
fix DarkMatter replicators not working
1 parent cba9408 commit 3788014

2 files changed

Lines changed: 173 additions & 123 deletions

File tree

control.lua

Lines changed: 172 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ for _, recipe in pairs(productivityRecipes) do
3131
productivityAllowed[recipe.recipe] = true
3232
end
3333

34+
function subPos(p1,p2)
35+
local p2 = p2 or {x=0,y=0}
36+
return {x=p1.x-p2.x, y=p1.y-p2.y}
37+
end
3438

3539
function expandPos(pos, range)
3640
local range = range or 0.5
@@ -56,161 +60,170 @@ function hasPocketBots(player)
5660
end
5761

5862
game.on_event(defines.events.on_marked_for_deconstruction, function(event)
59-
local entity = event.entity
60-
local deconstruction = false
61-
local upgrade = false
62-
local module = false
63-
local player = nil
64-
-- Determine which player used upgrade planner.
65-
-- If more than one player has upgrade planner in their hand or one
66-
-- player has a upgrade planner and other has deconstruction planner,
67-
-- we can't determine it, so we have to discard deconstruction order.
68-
for i = 1, #game.players do
69-
if game.players[i].cursor_stack.valid_for_read then
70-
if game.players[i].cursor_stack.name == "upgrade-planner" then
71-
if upgrade or deconstruction or module then
72-
--debugDump("Upgrade planner used", true)
73-
return
74-
end
75-
upgrade = true
76-
elseif game.players[i].cursor_stack.name == "deconstruction-planner" then
77-
if upgrade or module then
78-
--debugDump("Deconstruction/Module planner used", true)
79-
return
80-
end
81-
deconstruction = true
82-
elseif game.players[i].cursor_stack.name == "module-inserter" then
83-
if upgrade or deconstruction then
84-
--debugDump("Deconstruction/Upgrade planner used", true)
85-
return
63+
local status, err = pcall(function()
64+
local entity = event.entity
65+
local deconstruction = false
66+
local upgrade = false
67+
local module = false
68+
local player = nil
69+
-- Determine which player used upgrade planner.
70+
-- If more than one player has upgrade planner in their hand or one
71+
-- player has a upgrade planner and other has deconstruction planner,
72+
-- we can't determine it, so we have to discard deconstruction order.
73+
for _, p in pairs(game.players) do
74+
local stack = p.cursor_stack
75+
if stack.valid_for_read then
76+
if stack.name == "upgrade-planner" then
77+
if upgrade or deconstruction or module then
78+
--debugDump("Upgrade planner used", true)
79+
return
80+
end
81+
upgrade = true
82+
elseif stack.name == "deconstruction-planner" then
83+
if upgrade or module then
84+
--debugDump("Deconstruction/Module planner used", true)
85+
return
86+
end
87+
deconstruction = true
88+
elseif stack.name == "module-inserter" then
89+
if upgrade or deconstruction then
90+
--debugDump("Deconstruction/Upgrade planner used", true)
91+
return
92+
end
93+
player = p
94+
module = true
8695
end
87-
player = game.players[i]
88-
module = true
8996
end
9097
end
91-
end
9298

93-
if not player then return end
99+
if not player then return end
94100

95-
if not global["config"][player.name] then
101+
if not global["config"][player.name] then
96102

97-
-- Config for this player does not exist yet, so we have nothing to do.
98-
-- We can create it now for later usage.
99-
global["config"][player.name] = {}
100-
entity.cancel_deconstruction(entity.force)
101-
return
102-
end
103+
-- Config for this player does not exist yet, so we have nothing to do.
104+
-- We can create it now for later usage.
105+
global["config"][player.name] = {}
106+
entity.cancel_deconstruction(entity.force)
107+
return
108+
end
103109

104-
local config = global["config"][player.name]
110+
local config = global["config"][player.name]
105111

106-
if entity.type ~= "rocket-silo" then
112+
if entity.type ~= "rocket-silo" then
107113

108-
-- Check if player has space for proxy item
109-
--/c game.player.print(serpent.dump(game.player.get_inventory(defines.inventory.player_main).can_insert{name="module-inserter-proxy", count=1} or game.player.get_inventory(defines.inventory.player_quickbar).can_insert{name="module-inserter-proxy", count=1}))
114+
-- Check if player has space for proxy item
115+
--/c game.player.print(serpent.dump(game.player.get_inventory(defines.inventory.player_main).can_insert{name="module-inserter-proxy", count=1} or game.player.get_inventory(defines.inventory.player_quickbar).can_insert{name="module-inserter-proxy", count=1}))
110116

111-
local proxy = {name="module-inserter-proxy", count=1}
117+
local proxy = {name="module-inserter-proxy", count=1}
112118

113-
--if player.get_inventory(defines.inventory.player_main).can_insert(proxy) or player.get_inventory(defines.inventory.player_quickbar).can_insert(proxy) then
114-
-- Check if entity is valid and stored in config as a source.
115-
local index = 0
116-
for i = 1, #config do
117-
if config[i].from == entity.name then
118-
index = i
119-
break
119+
--if player.get_inventory(defines.inventory.player_main).can_insert(proxy) or player.get_inventory(defines.inventory.player_quickbar).can_insert(proxy) then
120+
-- Check if entity is valid and stored in config as a source.
121+
local index = 0
122+
for i = 1, #config do
123+
if config[i].from == entity.name then
124+
index = i
125+
break
126+
end
120127
end
121-
end
122-
if index == 0 then
123-
entity.cancel_deconstruction(entity.force)
124-
return
125-
end
126-
local freeSlots = 0
127-
for i=1,#player.get_inventory(defines.inventory.player_quickbar) do
128-
if not player.get_inventory(defines.inventory.player_quickbar)[i].valid_for_read then
129-
freeSlots = freeSlots + 1
128+
if index == 0 then
129+
entity.cancel_deconstruction(entity.force)
130+
return
130131
end
131-
end
132-
133-
if player.get_inventory(defines.inventory.player_main).can_insert(proxy) or
134-
(freeSlots > 1 and player.cursor_stack.valid_for_read) or
135-
(freeSlots > 0 and not player.cursor_stack.valid_for_read) then
136-
local modules = util.table.deepcopy(config[index].to)
137-
local cTable = {}
138-
for i, module in pairs(modules) do
139-
if module then
140-
if not cTable[module] then
141-
cTable[module] = 1
142-
else
143-
cTable[module] = cTable[module] + 1
144-
end
132+
local freeSlots = 0
133+
for i=1,#player.get_inventory(defines.inventory.player_quickbar) do
134+
if not player.get_inventory(defines.inventory.player_quickbar)[i].valid_for_read then
135+
freeSlots = freeSlots + 1
145136
end
146-
if module and game.item_prototypes[module].module_effects and game.item_prototypes[module].module_effects["productivity"] then
147-
if game.item_prototypes[module].module_effects["productivity"] ~= 0 then
148-
if entity.type == "beacon" then
149-
player.print("Can't insert "..module.." in "..entity.name)
150-
entity.cancel_deconstruction(entity.force)
151-
return
137+
end
138+
139+
if player.get_inventory(defines.inventory.player_main).can_insert(proxy) or
140+
(freeSlots > 1 and player.cursor_stack.valid_for_read) or
141+
(freeSlots > 0 and not player.cursor_stack.valid_for_read) then
142+
local modules = util.table.deepcopy(config[index].to)
143+
local cTable = {}
144+
for i, module in pairs(modules) do
145+
if module then
146+
if not cTable[module] then
147+
cTable[module] = 1
148+
else
149+
cTable[module] = cTable[module] + 1
152150
end
153-
if entity.type == "assembling-machine" then
154-
if entity.recipe and not productivityAllowed[entity.recipe.name] then
155-
player.print("Can't use "..module.." with recipe: " .. entity.recipe.name)
151+
end
152+
if module and game.item_prototypes[module].module_effects and game.item_prototypes[module].module_effects["productivity"] then
153+
if game.item_prototypes[module].module_effects["productivity"] ~= 0 then
154+
if entity.type == "beacon" then
155+
player.print("Can't insert "..module.." in "..entity.name)
156156
entity.cancel_deconstruction(entity.force)
157157
return
158158
end
159+
if entity.type == "assembling-machine" then
160+
if entity.recipe and not productivityAllowed[entity.recipe.name] then
161+
player.print("Can't use "..module.." with recipe: " .. entity.recipe.name)
162+
entity.cancel_deconstruction(entity.force)
163+
return
164+
end
165+
end
159166
end
160167
end
161168
end
162-
end
163-
if entity.type == "assembling-machine" and not entity.recipe then
164-
player.print("Can't insert modules in assembler without recipe")
165-
entity.cancel_deconstruction(entity.force)
166-
return
167-
end
168-
local inventory = entity.get_inventory(typeToSlot[entity.type])
169-
local contents = inventory.get_contents()
170-
if not util.table.compare(cTable,contents) then
171-
-- proxy entity that the robots fly to
172-
local new_entity = {
173-
name = "entity-ghost",
174-
inner_name = "module-inserter-proxy",
175-
position = entity.position,
176-
direction = entity.direction,
177-
force = entity.force
178-
}
179-
local key = entityKey(new_entity)
180-
if global.entitiesToInsert[key] then
181-
global.entitiesToInsert[key] = nil
182-
if player.get_item_count("module-inserter-proxy") > 0 then
183-
player.remove_item(proxy)
169+
if entity.type == "assembling-machine" and not entity.recipe then
170+
player.print("Can't insert modules in assembler without recipe")
171+
entity.cancel_deconstruction(entity.force)
172+
return
173+
end
174+
local inventory = entity.get_inventory(typeToSlot[entity.type])
175+
local contents = inventory.get_contents()
176+
if not util.table.compare(cTable,contents) then
177+
-- proxy entity that the robots fly to
178+
local new_entity = {
179+
name = "entity-ghost",
180+
inner_name = "module-inserter-proxy",
181+
position = entity.position,
182+
direction = entity.direction,
183+
force = entity.force
184+
}
185+
if string.find(entity.name, "replicator%-%d") then
186+
new_entity.position = subPos(new_entity.position, {x=0.5,y=0.5})
184187
end
185-
local toDelete = false
186-
for tick, t in pairs(global.removeTicks) do
187-
for k, g in pairs(t) do
188-
if g.key == key then
189-
toDelete = {t=tick, k=k}
188+
local key = entityKey(new_entity)
189+
if global.entitiesToInsert[key] then
190+
global.entitiesToInsert[key] = nil
191+
if player.get_item_count("module-inserter-proxy") > 0 then
192+
player.remove_item(proxy)
193+
end
194+
local toDelete = false
195+
for tick, t in pairs(global.removeTicks) do
196+
for k, g in pairs(t) do
197+
if g.key == key then
198+
toDelete = {t=tick, k=k}
199+
break
200+
end
201+
end
202+
if toDelete then
190203
break
191204
end
192205
end
193206
if toDelete then
194-
break
207+
global.removeTicks[toDelete.t][toDelete.k] = nil
195208
end
196209
end
197-
if toDelete then
198-
global.removeTicks[toDelete.t][toDelete.k] = nil
210+
if not global.entitiesToInsert[key] then -- or (global.entitiesToInsert[key].ghost and not global.entitiesToInsert[key].ghost.valid) then
211+
local ghost = entity.surface.create_entity(new_entity)
212+
global.entitiesToInsert[key] = {entity = entity, player = player, modules = modules, ghost = ghost}
213+
--ghost.time_to_live = 60*30
214+
local delTick = game.tick + ghost.time_to_live + 2
215+
global.removeTicks[delTick] = global.removeTicks[delTick] or {}
216+
table.insert(global.removeTicks[delTick], {p=player,g=ghost, key = key})
217+
player.insert{name="module-inserter-proxy", count=1}
199218
end
200219
end
201-
if not global.entitiesToInsert[key] then -- or (global.entitiesToInsert[key].ghost and not global.entitiesToInsert[key].ghost.valid) then
202-
local ghost = entity.surface.create_entity(new_entity)
203-
global.entitiesToInsert[key] = {entity = entity, player = player, modules = modules, ghost = ghost}
204-
--ghost.time_to_live = 60*30
205-
local delTick = game.tick + ghost.time_to_live + 2
206-
global.removeTicks[delTick] = global.removeTicks[delTick] or {}
207-
table.insert(global.removeTicks[delTick], {p=player,g=ghost, key = key})
208-
player.insert{name="module-inserter-proxy", count=1}
209-
end
210220
end
211221
end
222+
entity.cancel_deconstruction(entity.force)
223+
end)
224+
if not status then
225+
debugDump(err, true)
212226
end
213-
entity.cancel_deconstruction(entity.force)
214227
end)
215228

216229
local function initGlob()
@@ -242,6 +255,43 @@ local function initGlob()
242255
global.guiVersion = {}
243256
global.version = "0.0.9"
244257
end
258+
259+
--hanndle removed items here
260+
local items = game.item_prototypes
261+
for name, p in pairs(global.config) do
262+
for i=#p,1,-1 do
263+
if p[i].from ~= "" and not items[p[i].from] then
264+
global.config[name][i].from = ""
265+
global.config[name][i].to = ""
266+
debugDump(p[i].from,true)
267+
end
268+
if type(p[i].to) == "table" then
269+
for k, m in pairs(p[i].to) do
270+
if m and not items[m] then
271+
global.config[name][i].to[k] = false
272+
end
273+
end
274+
end
275+
end
276+
end
277+
278+
for player, store in pairs(global.storage) do
279+
for name, p in pairs(store) do
280+
for i=#p,1,-1 do
281+
if p[i].from ~= "" and not items[p[i].from] then
282+
global.storage[player][name][i].from = ""
283+
global.storage[player][name][i].to = ""
284+
end
285+
if type(p[i].to) == "table" then
286+
for k, m in pairs(p[i].to) do
287+
if m and not items[m] then
288+
global.storage[player][name][i].to[k] = false
289+
end
290+
end
291+
end
292+
end
293+
end
294+
end
245295
global.version = "0.0.9"
246296
end
247297

info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ModuleInserter",
3-
"version": "0.0.9",
3+
"version": "0.0.91",
44
"title": "Module Inserter",
55
"author": "Choumiko",
66
"homepage": "",

0 commit comments

Comments
 (0)