mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Update move item stack (#317)
* Update common.lua * Update common.lua * Update common.lua * Update common.lua * Update common.lua * Update expcore/common.lua Co-authored-by: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> * Update expcore/common.lua Co-authored-by: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> * Update expcore/common.lua Co-authored-by: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> * Update expcore/common.lua --------- Co-authored-by: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com>
This commit is contained in:
@@ -612,53 +612,139 @@ move_items_stack(game.player.get_main_inventory())
|
|||||||
|
|
||||||
]]
|
]]
|
||||||
function Common.move_items_stack(items, surface, position, radius, chest_type)
|
function Common.move_items_stack(items, surface, position, radius, chest_type)
|
||||||
chest_type = chest_type or 'iron-chest'
|
chest_type = chest_type or 'steel-chest'
|
||||||
surface = surface or game.surfaces[1]
|
surface = surface or game.surfaces[1]
|
||||||
if position and type(position) ~= 'table' then return end
|
|
||||||
if type(items) ~= 'table' then return end
|
if position and type(position) ~= '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, 'iron-chest'}} or {}
|
||||||
local count = #entities
|
local count = #entities
|
||||||
local current = 1
|
local current = 0
|
||||||
|
local last_entity = nil
|
||||||
|
|
||||||
|
-- ipairs does not work on LuaInventory
|
||||||
|
for i = 1, #items do
|
||||||
|
local item = items[i]
|
||||||
|
if item.valid_for_read then
|
||||||
|
local inserted = false
|
||||||
|
|
||||||
|
-- Attempt to insert the items
|
||||||
|
for j = 1, count do
|
||||||
|
local entity = entities[((current + j - 1) % count) + 1]
|
||||||
|
|
||||||
|
if entity.can_insert(item) then
|
||||||
|
last_entity = entity
|
||||||
|
current = current + 1
|
||||||
|
entity.insert(item)
|
||||||
|
inserted = true
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- If it was not inserted then a new entity is needed
|
||||||
|
if not inserted then
|
||||||
|
--[[
|
||||||
|
if not options.allow_creation then
|
||||||
|
error('Unable to insert items into a valid entity, consider enabling allow_creation')
|
||||||
|
end
|
||||||
|
|
||||||
|
if options.name == nil then
|
||||||
|
error('Name must be provided to allow creation of new entities')
|
||||||
|
end
|
||||||
|
|
||||||
|
if options.position then
|
||||||
|
pos = surface.find_non_colliding_position(chest_type, p, r, 1, true)
|
||||||
|
|
||||||
|
elseif options.area then
|
||||||
|
pos = surface.find_non_colliding_position_in_box(chest_type, options.area, 1, true)
|
||||||
|
|
||||||
|
else
|
||||||
|
pos = surface.find_non_colliding_position(chest_type, {0,0}, 0, 1, true)
|
||||||
|
end
|
||||||
|
]]
|
||||||
|
|
||||||
|
local pos = surface.find_non_colliding_position(chest_type, p, r, 1, true)
|
||||||
|
last_entity = surface.create_entity{name=chest_type, position=pos, force='neutral'}
|
||||||
|
|
||||||
|
count = count + 1
|
||||||
|
entities[count] = last_entity
|
||||||
|
|
||||||
|
last_entity.insert(item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
-- 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 i=1,#items do
|
for i=1,#items do
|
||||||
local item = items[i]
|
local item = items[i]
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
local empty_stack = chest.get_inventory(defines.inventory.chest).find_empty_stack(item.name)
|
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
|
|
||||||
|
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)
|
empty_stack.transfer_stack(item)
|
||||||
last_chest = chest
|
last_chest = chest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return last_chest
|
return last_chest
|
||||||
|
]]
|
||||||
|
|
||||||
|
return last_entity
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[-- Prints a colored value on a location, color is based on the value.
|
--[[-- Prints a colored value on a location, color is based on the value.
|
||||||
|
|||||||
Reference in New Issue
Block a user