diff --git a/expcore/common.lua b/expcore/common.lua index 64886316..38d58e52 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -539,6 +539,7 @@ end -- @section factorio --[[-- Moves items to the position and stores them in the closest entity of the type given +-- Copies the items by prototype name, but keeps them in the original inventory @tparam table items items which are to be added to the chests, ['name']=count @tparam[opt=navies] LuaSurface surface the surface that the items will be moved to @tparam[opt={0, 0}] table position the position that the items will be moved to {x=100, y=100} @@ -595,6 +596,67 @@ function Common.move_items(items, surface, position, radius, chest_type) return last_chest end +--[[-- Moves items to the position and stores them in the closest entity of the type given +-- Differs from move_items by accepting a table of LuaItemStack and transferring them into the inventory - not copying +@tparam table items items which are to be added to the chests, an array of LuaItemStack +@tparam[opt=navies] LuaSurface surface the surface that the items will be moved to +@tparam[opt={0, 0}] table position the position that the items will be moved to {x=100, y=100} +@tparam[opt=32] number radius the radius in which the items are allowed to be placed +@tparam[opt=iron-chest] string chest_type the chest type that the items should be moved into +@treturn LuaEntity the last chest that had items inserted into it + +@usage-- Copy all the items in a players inventory and place them in chests at {0, 0} +move_items(game.player.get_main_inventory()) + +]] +function Common.move_items_stack(items, surface, position, radius, chest_type) + chest_type = chest_type or 'iron-chest' + surface = surface or game.surfaces[1] + if position and type(position) ~= 'table' then return end + if type(items) ~= 'table' then return end + -- Finds all entities of the given type + local p = position or {x=0, y=0} + local r = radius or 32 + local entities = surface.find_entities_filtered{area={{p.x-r, p.y-r}, {p.x+r, p.y+r}}, name=chest_type} or {} + local count = #entities + local current = 1 + -- Makes a new empty chest when it is needed + local function make_new_chest() + local pos = surface.find_non_colliding_position(chest_type, position, 32, 1) + local chest = surface.create_entity{name=chest_type, position=pos, force='neutral'} + table.insert(entities, chest) + count = count + 1 + return chest + end + -- Function used to round robin the items into all chests + local function next_chest(item) + local chest = entities[current] + if count == 0 then return make_new_chest() end + if chest.get_inventory(defines.inventory.chest).can_insert(item) then + -- If the item can be inserted then the chest is returned + current = current+1 + if current > count then current = 1 end + return chest + else + -- Other wise it is removed from the list + table.remove(entities, current) + count = count - 1 + end + end + -- Inserts the items into the chests + local last_chest + for i=1,#items do + local item = items[i] + if item.valid_for_read then + local chest = next_chest(item) + if not chest or not chest.valid then return error(string.format('Cant move item %s to %s{%s, %s} no valid chest in radius', item.name, surface.name, p.x, p.y)) end + chest.insert(item) + last_chest = chest + end + end + return last_chest +end + --[[-- Prints a colored value on a location, color is based on the value. nb: src is below but the gradent has been edited https://github.com/Refactorio/RedMew/blob/9184b2940f311d8c9c891e83429fc57ec7e0c4a2/map_gen/maps/diggy/debug.lua#L31 diff --git a/modules/addons/death-logger.lua b/modules/addons/death-logger.lua index 97c057fe..092bcd4a 100644 --- a/modules/addons/death-logger.lua +++ b/modules/addons/death-logger.lua @@ -4,7 +4,7 @@ local Event = require 'utils.event' --- @dep utils.event local Global = require 'utils.global' --- @dep utils.global local config = require 'config.death_logger' --- @dep config.death_logger -local format_time, move_items = _C.format_time, _C.move_items --- @dep expcore.common +local format_time, move_items = _C.format_time, _C.move_items_stack --- @dep expcore.common -- Max amount of ticks a corpse can be alive local corpse_lifetime = 60*60*15 @@ -64,7 +64,7 @@ Event.add(defines.events.on_player_died, function(event) local player = game.players[event.player_index] local corpse = player.surface.find_entity('character-corpse', player.position) if config.use_chests_as_bodies then - local items = corpse.get_inventory(defines.inventory.character_corpse).get_contents() + local items = corpse.get_inventory(defines.inventory.character_corpse) local chest = move_items(items, corpse.surface, corpse.position) chest.destructible = false corpse.destroy() @@ -140,7 +140,7 @@ end if config.auto_collect_bodies then Event.add(defines.events.on_character_corpse_expired, function(event) local corpse = event.corpse - local items = corpse.get_inventory(defines.inventory.character_corpse).get_contents() + local items = corpse.get_inventory(defines.inventory.character_corpse) move_items(items, corpse.surface, {x=0, y=0}) end) end diff --git a/modules/addons/inventory-clear.lua b/modules/addons/inventory-clear.lua index 57ae4958..51dc18e2 100644 --- a/modules/addons/inventory-clear.lua +++ b/modules/addons/inventory-clear.lua @@ -3,13 +3,13 @@ local Event = require 'utils.event' --- @dep utils.event local events = require 'config.inventory_clear' --- @dep config.inventory_clear -local move_items = _C.move_items --- @dep expcore.common +local move_items_stack = _C.move_items_stack --- @dep expcore.common local function clear_items(event) local player = game.players[event.player_index] local inv = player.get_main_inventory() - move_items(inv.get_contents()) + move_items_stack(inv) inv.clear() end -for _, event_name in ipairs(events) do Event.add(event_name, clear_items) end \ No newline at end of file +for _, event_name in ipairs(events) do Event.add(event_name, clear_items) end diff --git a/modules/commands/clear-inventory.lua b/modules/commands/clear-inventory.lua index 04155978..ceb6b3e9 100644 --- a/modules/commands/clear-inventory.lua +++ b/modules/commands/clear-inventory.lua @@ -4,7 +4,7 @@ ]] local Commands = require 'expcore.commands' --- @dep expcore.commands -local move_items = _C.move_items --- @dep expcore.common +local move_items_stack = _C.move_items_stack --- @dep expcore.common require 'config.expcore.command_role_parse' --- Clears a players inventory @@ -14,10 +14,10 @@ Commands.new_command('clear-inventory', 'Clears a players inventory') :add_param('player', false, 'player-role') :add_alias('clear-inv', 'move-inventory', 'move-inv') :register(function(_, player) - local inv = player.get_main_inventory() - if not inv then - return Commands.error{'expcore-commands.reject-player-alive'} - end - move_items(inv.get_contents()) - inv.clear() -end) \ No newline at end of file + local inv = player.get_main_inventory() + if not inv then + return Commands.error{'expcore-commands.reject-player-alive'} + end + move_items_stack(inv) + inv.clear() +end)