mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-30 12:31:41 +09:00
fix(common): remove wrong move_items
Have only copy_items_stack and move_items_stack
This commit is contained in:
@@ -540,7 +540,7 @@ end
|
|||||||
|
|
||||||
--[[-- Moves items to the position and stores them in the closest entity of the type given
|
--[[-- 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
|
-- 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 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=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={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=32] number radius the radius in which the items are allowed to be placed
|
||||||
@@ -551,49 +551,52 @@ end
|
|||||||
move_items(game.player.get_main_inventory().get_contents())
|
move_items(game.player.get_main_inventory().get_contents())
|
||||||
|
|
||||||
]]
|
]]
|
||||||
function Common.move_items(items, surface, position, radius, chest_type)
|
function Common.copy_items_stack(items, surface, position, radius, chest_type)
|
||||||
chest_type = chest_type or 'iron-chest'
|
chest_type = chest_type or 'iron-chest'
|
||||||
surface = surface or game.surfaces[1]
|
surface = surface or game.surfaces[1]
|
||||||
if position and type(position) ~= 'table' then return end
|
if position and type(position) ~= 'table' then return end
|
||||||
if type(items) ~= 'table' then return end
|
if type(items) ~= 'table' then return end
|
||||||
-- Finds all entities of the given type
|
-- Finds all entities of the given type
|
||||||
local p = position or {x=0, y=0}
|
local p = position or {x=0, y=0}
|
||||||
local r = radius or 32
|
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 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 count = #entities
|
||||||
local current = 1
|
local current = 1
|
||||||
-- Makes a new empty chest when it is needed
|
-- Makes a new empty chest when it is needed
|
||||||
local function make_new_chest()
|
local function make_new_chest()
|
||||||
local pos = surface.find_non_colliding_position(chest_type, position, 32, 1)
|
local pos = surface.find_non_colliding_position(chest_type, position, 32, 1)
|
||||||
local chest = surface.create_entity{name=chest_type, position=pos, force='neutral'}
|
local chest = surface.create_entity{name=chest_type, position=pos, force='neutral'}
|
||||||
table.insert(entities, chest)
|
table.insert(entities, chest)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
return chest
|
return chest
|
||||||
end
|
end
|
||||||
-- Function used to round robin the items into all chests
|
-- Function used to round robin the items into all chests
|
||||||
local function next_chest(item)
|
local function next_chest(item)
|
||||||
local chest = entities[current]
|
local chest = entities[current]
|
||||||
if count == 0 then return make_new_chest() end
|
if count == 0 then return make_new_chest() end
|
||||||
if chest.get_inventory(defines.inventory.chest).can_insert(item) then
|
if chest.get_inventory(defines.inventory.chest).can_insert(item) then
|
||||||
-- If the item can be inserted then the chest is returned
|
-- If the item can be inserted then the chest is returned
|
||||||
current = current+1
|
current = current+1
|
||||||
if current > count then current = 1 end
|
if current > count then current = 1 end
|
||||||
return chest
|
return chest
|
||||||
else
|
else
|
||||||
-- Other wise it is removed from the list
|
-- Other wise it is removed from the list
|
||||||
table.remove(entities, current)
|
table.remove(entities, current)
|
||||||
count = count - 1
|
count = count - 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Inserts the items into the chests
|
-- Inserts the items into the chests
|
||||||
local last_chest
|
local last_chest
|
||||||
for item_name, item_count in pairs(items) do
|
for i=1,#items do
|
||||||
local chest = next_chest{name=item_name, count=item_count}
|
local item = items[i]
|
||||||
if not chest 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
|
if item.valid_for_read then
|
||||||
Util.insert_safe(chest, {[item_name]=item_count})
|
local chest = next_chest(item)
|
||||||
last_chest = chest
|
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
|
||||||
end
|
chest.insert(item)
|
||||||
return last_chest
|
last_chest = chest
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return last_chest
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[-- Moves items to the position and stores them in the closest entity of the type given
|
--[[-- Moves items to the position and stores them in the closest entity of the type given
|
||||||
@@ -650,7 +653,9 @@ function Common.move_items_stack(items, surface, position, radius, chest_type)
|
|||||||
if item.valid_for_read then
|
if item.valid_for_read then
|
||||||
local chest = next_chest(item)
|
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
|
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)
|
local empty_stack = chest.get_inventory(defines.inventory.chest).find_empty_stack(item.name)
|
||||||
|
if not empty_stack 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
|
||||||
|
empty_stack.transfer_stack(item)
|
||||||
last_chest = chest
|
last_chest = chest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user