mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2024-12-30 23:17:53 +02:00
Merge pull request #204 from zzh8829/cargo-autostash
Add autostash to filtered cargo wagon
This commit is contained in:
commit
931c175051
@ -295,6 +295,7 @@ function Public.reset_map()
|
||||
this.active_surface_index = CS.create_surface()
|
||||
|
||||
Autostash.insert_into_furnace(true)
|
||||
Autostash.insert_into_wagon(true)
|
||||
|
||||
Poll.reset()
|
||||
ICW.reset()
|
||||
|
@ -8,7 +8,8 @@ local print_color = {r = 120, g = 255, b = 0}
|
||||
|
||||
local autostash = {
|
||||
floating_text_y_offsets = {},
|
||||
insert_into_furnace = false
|
||||
insert_into_furnace = false,
|
||||
insert_into_wagon = false
|
||||
}
|
||||
|
||||
local Public = {}
|
||||
@ -48,6 +49,16 @@ local function create_floaty_text(surface, position, name, count)
|
||||
end
|
||||
|
||||
local function chest_is_valid(chest)
|
||||
if chest.type == 'cargo-wagon' then
|
||||
local chest_inventory = chest.get_inventory(defines.inventory.cargo_wagon)
|
||||
for index=1, 40 do
|
||||
if chest_inventory.get_filter(index) == nil then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
for _, e in pairs(
|
||||
chest.surface.find_entities_filtered(
|
||||
{
|
||||
@ -133,15 +144,17 @@ local function get_nearby_chests(player)
|
||||
local size_of_chests = 0
|
||||
local area = {{player.position.x - r, player.position.y - r}, {player.position.x + r, player.position.y + r}}
|
||||
|
||||
local type
|
||||
|
||||
local type = {'container', 'logistic-container'}
|
||||
local containers = {}
|
||||
local i = 0
|
||||
|
||||
if autostash.insert_into_furnace then
|
||||
type = {'furnace', 'container', 'logistic-container'}
|
||||
else
|
||||
type = {'container', 'logistic-container'}
|
||||
table.insert(type, 'furnace')
|
||||
end
|
||||
if autostash.insert_into_wagon then
|
||||
table.insert(type, 'cargo-wagon')
|
||||
end
|
||||
|
||||
for _, e in pairs(player.surface.find_entities_filtered({type = type, area = area, force = 'player'})) do
|
||||
if ((player.position.x - e.position.x) ^ 2 + (player.position.y - e.position.y) ^ 2) <= r_square then
|
||||
i = i + 1
|
||||
@ -212,6 +225,22 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests,
|
||||
end
|
||||
end
|
||||
|
||||
-- Attempt to load filtered cargo wagon
|
||||
for _, chest in pairs(filtered_chests) do
|
||||
if chest.type == 'cargo-wagon' then
|
||||
local chest_inventory = chest.get_inventory(defines.inventory.cargo_wagon)
|
||||
if chest_inventory.can_insert({name = name, count = count}) then
|
||||
local inserted_count = chest_inventory.insert({name = name, count = count})
|
||||
player_inventory.remove({name = name, count = inserted_count})
|
||||
create_floaty_text(chest.surface, chest.position, name, inserted_count)
|
||||
count = count - inserted_count
|
||||
if count <= 0 then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--Attempt to store in chests that already have the same item.
|
||||
for _, chest in pairs(chests) do
|
||||
if container[chest.type] then
|
||||
@ -403,6 +432,12 @@ function Public.insert_into_furnace(value)
|
||||
end
|
||||
end
|
||||
|
||||
function Public.insert_into_wagon(value)
|
||||
if value then
|
||||
autostash.insert_into_wagon = value or false
|
||||
end
|
||||
end
|
||||
|
||||
local event = require 'utils.event'
|
||||
event.add(defines.events.on_player_joined_game, on_player_joined_game)
|
||||
event.add(defines.events.on_gui_click, on_gui_click)
|
||||
|
Loading…
Reference in New Issue
Block a user