Skip to content

Commit 1cf7f1f

Browse files
committed
fix inventory full checks
store data by player.index instead name unregister on_tick if not needed
1 parent 6355721 commit 1cf7f1f

File tree

3 files changed

+730
-732
lines changed

3 files changed

+730
-732
lines changed

control.lua

+112-76
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ function entityKey(ent)
3737
return false
3838
end
3939

40+
function count_keys(hashmap)
41+
local result = 0
42+
for _, __ in pairs(hashmap) do
43+
result = result + 1
44+
end
45+
return result
46+
end
47+
4048
--/c game.player.print(serpent.dump(game.player.surface.find_logistic_network_by_position(game.player.position, game.player.force.name).find_cell_closest_to(game.player.position)))
4149
function hasPocketBots(player)
4250
local logisticCell = player.character.logistic_cell
@@ -47,6 +55,55 @@ function hasPocketBots(player)
4755
return port
4856
end
4957

58+
function on_tick(event)
59+
if global.removeTicks[event.tick] then
60+
local status, err = pcall(function()
61+
for _, g in pairs(global.removeTicks[event.tick]) do
62+
if not g.g.valid then
63+
if g.p.get_item_count("module-inserter-proxy") > 0 then
64+
g.p.remove_item{name="module-inserter-proxy", count = 1}
65+
end
66+
global.entitiesToInsert[g.key] = nil
67+
end
68+
end
69+
if count_keys(global.removeTicks[event.tick]) == 0 then
70+
global.removeTicks[event.tick] = nil
71+
end
72+
if count_keys(global.removeTicks) == 0 then
73+
script.on_event(defines.events.on_tick, nil)
74+
end
75+
end)
76+
if not status then
77+
debugDump(err, true)
78+
end
79+
end
80+
end
81+
82+
function add_ghost(key, data)
83+
global.removeTicks[key] = global.removeTicks[key] or {}
84+
global.removeTicks[key][key] = data
85+
script.on_event(defines.events.on_tick, on_tick)
86+
end
87+
88+
function remove_ghost(key)
89+
local toDelete = false
90+
for tick, t in pairs(global.removeTicks) do
91+
if t[key] then
92+
toDelete = {t=tick, k=key}
93+
break
94+
end
95+
end
96+
if toDelete then
97+
global.removeTicks[toDelete.t][toDelete.k] = nil
98+
if count_keys(global.removeTicks[toDelete.t]) == 0 then
99+
global.removeTicks[toDelete.t] = nil
100+
end
101+
if count_keys(global.removeTicks) == 0 then
102+
script.on_event(defines.events.on_tick, nil)
103+
end
104+
end
105+
end
106+
50107
script.on_event(defines.events.on_marked_for_deconstruction, function(event)
51108
local status, err = pcall(function()
52109
local entity = event.entity
@@ -88,23 +145,17 @@ script.on_event(defines.events.on_marked_for_deconstruction, function(event)
88145

89146
if not player then return end
90147

91-
if not global["config"][player.name] then
148+
if not global["config"][player.index] then
92149

93150
-- Config for this player does not exist yet, so we have nothing to do.
94151
-- We can create it now for later usage.
95-
global["config"][player.name] = {}
152+
global["config"][player.index] = {}
96153
entity.cancel_deconstruction(entity.force)
97154
return
98155
end
99156

100-
local config = global["config"][player.name]
101-
102-
-- Check if player has space for proxy item
103-
--/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}))
157+
local config = global["config"][player.index]
104158

105-
local proxy = {name="module-inserter-proxy", count=1}
106-
107-
--if player.get_inventory(defines.inventory.player_main).can_insert(proxy) or player.get_inventory(defines.inventory.player_quickbar).can_insert(proxy) then
108159
-- Check if entity is valid and stored in config as a source.
109160
local index = 0
110161
for i = 1, #config do
@@ -117,16 +168,13 @@ script.on_event(defines.events.on_marked_for_deconstruction, function(event)
117168
entity.cancel_deconstruction(entity.force)
118169
return
119170
end
120-
local freeSlots = 0
121-
for i=1,#player.get_inventory(defines.inventory.player_quickbar) do
122-
if not player.get_inventory(defines.inventory.player_quickbar)[i].valid_for_read then
123-
freeSlots = freeSlots + 1
124-
end
125-
end
126-
127-
if player.get_inventory(defines.inventory.player_main).can_insert(proxy) or
128-
(freeSlots > 1 and player.cursor_stack.valid_for_read) or
129-
(freeSlots > 0 and not player.cursor_stack.valid_for_read) then
171+
172+
local proxy = {name="module-inserter-proxy", count=1}
173+
174+
local can_insert_main = player.get_inventory(defines.inventory.player_main).can_insert(proxy)
175+
local can_insert_quick = player.get_inventory(defines.inventory.player_quickbar).can_insert(proxy)
176+
177+
if can_insert_main or can_insert_quick then
130178
if entity.type == "assembling-machine" and not entity.recipe then
131179
player.print("Can't insert modules in assembler without recipe")
132180
entity.cancel_deconstruction(entity.force)
@@ -178,30 +226,19 @@ script.on_event(defines.events.on_marked_for_deconstruction, function(event)
178226
if player.get_item_count("module-inserter-proxy") > 0 then
179227
player.remove_item(proxy)
180228
end
181-
local toDelete = false
182-
for tick, t in pairs(global.removeTicks) do
183-
for k, g in pairs(t) do
184-
if g.key == key then
185-
toDelete = {t=tick, k=k}
186-
break
187-
end
188-
end
189-
if toDelete then
190-
break
191-
end
192-
end
193-
if toDelete then
194-
global.removeTicks[toDelete.t][toDelete.k] = nil
195-
end
229+
remove_ghost(key)
196230
end
197231
if not global.entitiesToInsert[key] then -- or (global.entitiesToInsert[key].ghost and not global.entitiesToInsert[key].ghost.valid) then
198232
local ghost = entity.surface.create_entity(new_entity)
199233
global.entitiesToInsert[key] = {entity = entity, player = player, modules = modules, ghost = ghost}
200234
--ghost.time_to_live = 60*30
201235
local delTick = game.tick + ghost.time_to_live + 2
202-
global.removeTicks[delTick] = global.removeTicks[delTick] or {}
203-
table.insert(global.removeTicks[delTick], {p=player,g=ghost, key = key})
204-
player.insert{name="module-inserter-proxy", count=1}
236+
add_ghost(key, {p=player,g=ghost})
237+
if can_insert_main then
238+
player.get_inventory(defines.inventory.player_main).insert(proxy)
239+
elseif can_insert_quick then
240+
player.get_inventory(defines.inventory.player_quickbar).insert(proxy)
241+
end
205242
end
206243
end
207244
end
@@ -291,7 +328,7 @@ local function init_global()
291328
end
292329

293330
local function init_player(player)
294-
global.settings[player.name] = global.settings[player.name] or {}
331+
global.settings[player.index] = global.settings[player.index] or {}
295332
end
296333

297334
local function init_players()
@@ -318,28 +355,38 @@ end
318355

319356
local function on_load()
320357
-- set metatables, register conditional event handlers, local references to global
358+
if count_keys(global.removeTicks) == 0 then
359+
script.on_event(defines.events.on_tick, nil)
360+
else
361+
script.on_event(defines.events.on_tick, on_tick)
362+
end
321363
end
322364

323365
local function cleanup(show)
324366
local count = 0
325367
for tick, data in pairs(global.removeTicks) do
326-
if tick < game.tick then
368+
if data then
327369
for i=#data,1,-1 do
328370
local proxyData = data[i]
329-
if proxyData.g and not proxyData.g.valid then
371+
if proxyData and proxyData.g and not proxyData.g.valid then
330372
table.remove(data, i)
331373
count = count + 1
332374
end
333375
end
376+
if count_keys(global.removeTicks[tick]) == 0 then
377+
global.removeTicks[tick] = nil
378+
end
334379
end
335-
if #global.removeTicks[tick] == 0 then
336-
global.removeTicks[tick] = nil
337-
end
338380
end
339-
381+
if count_keys(global.removeTicks) == 0 then
382+
script.on_event(defines.events.on_tick, nil)
383+
else
384+
script.on_event(defines.events.on_tick, on_tick)
385+
end
386+
340387
if show then
341388
debugDump("Removed "..count.." entries", true)
342-
log(count)
389+
log("ModuleInserter: Removed "..count.." entries")
343390
end
344391
end
345392

@@ -364,7 +411,20 @@ local function on_configuration_changed(data)
364411
update_gui()
365412
end
366413
if oldVersion < "0.1.34" then
367-
--cleanup(true)
414+
local tmp = {}
415+
tmp.config = util.table.deepcopy(global["config"])
416+
tmp["config-tmp"] = util.table.deepcopy(global["config-tmp"])
417+
tmp.storage = util.table.deepcopy(global["storage"])
418+
tmp.settings = util.table.deepcopy(global.settings)
419+
for key, data in pairs(tmp) do
420+
global[key] = {}
421+
for pi, player in pairs(game.players) do
422+
if player.name and data[player.name] then
423+
global[key][player.index] = data[player.name]
424+
end
425+
end
426+
end
427+
cleanup(true)
368428
end
369429
--mod was updated
370430
-- update/change gui for all players via game.players.gui ?
@@ -393,40 +453,15 @@ script.on_event(defines.events.on_player_created, on_player_created)
393453
script.on_event(defines.events.on_force_created, on_force_created)
394454
script.on_event(defines.events.on_forces_merging, on_forces_merging)
395455

396-
function count_keys(hashmap)
397-
local result = 0
398-
for _, __ in pairs(hashmap) do
399-
result = result + 1
400-
end
401-
return result
402-
end
403-
404456
function get_config_item(player, index, type1)
405-
if not global["config-tmp"][player.name]
406-
or index > #global["config-tmp"][player.name]
407-
or global["config-tmp"][player.name][index][type1] == "" or type(global["config-tmp"][player.name][index][type1]) == "table" then
457+
if not global["config-tmp"][player.index]
458+
or index > #global["config-tmp"][player.index]
459+
or global["config-tmp"][player.index][index][type1] == "" or type(global["config-tmp"][player.index][index][type1]) == "table" then
408460

409461
return {"upgrade-planner-item-not-set"}
410462

411463
end
412-
return game.item_prototypes[global["config-tmp"][player.name][index][type1]].localised_name
413-
end
414-
415-
function on_tick(event)
416-
if global.removeTicks[event.tick] then
417-
local status, err = pcall(function()
418-
for _, g in pairs(global.removeTicks[event.tick]) do
419-
if not g.g.valid and g.p.get_item_count("module-inserter-proxy") > 0 then
420-
g.p.remove_item{name="module-inserter-proxy", count = 1}
421-
global.entitiesToInsert[g.key] = nil
422-
end
423-
end
424-
global.removeTicks[event.tick] = nil
425-
end)
426-
if not status then
427-
debugDump(err, true)
428-
end
429-
end
464+
return game.item_prototypes[global["config-tmp"][player.index][index][type1]].localised_name
430465
end
431466

432467
script.on_event(defines.events.on_robot_built_entity, function(event)
@@ -472,6 +507,7 @@ script.on_event(defines.events.on_robot_built_entity, function(event)
472507
end
473508
local key = entityKey(entity)
474509
global.entitiesToInsert[key] = nil
510+
remove_ghost(key)
475511
end
476512
entity.destroy()
477513
end
@@ -504,7 +540,7 @@ script.on_event(defines.events.on_gui_click, function(event)
504540
for _,k in pairs(global.removeTicks) do
505541
c = c+#k
506542
end
507-
debugDump("#config "..#global.config[player.name],true)
543+
debugDump("#config "..#global.config[player.index],true)
508544
debugDump("#Remove "..c,true)
509545
elseif element.name == "module-inserter-storage-store" then
510546
gui_store(player)

0 commit comments

Comments
 (0)