2019-09-24 23:04:55 +02:00
--this adds a button that stashes/sorts your inventory into nearby chests in some kind of intelligent way - mewmew
2019-10-24 23:00:14 +02:00
-- modified by gerkiz
2019-09-24 23:04:55 +02:00
2020-06-07 23:25:29 +02:00
local Global = require ' utils.global '
2020-10-22 00:32:34 +02:00
local Event = require ' utils.event '
2021-05-23 17:03:52 +02:00
local BottomFrame = require ' comfy_panel.bottom_frame '
2021-03-16 21:09:21 +02:00
local floor = math.floor
2019-09-24 23:04:55 +02:00
local print_color = { r = 120 , g = 255 , b = 0 }
2020-10-22 17:39:23 +02:00
local this = {
2020-06-07 23:25:29 +02:00
floating_text_y_offsets = { } ,
2020-10-22 00:32:34 +02:00
whitelist = { } ,
2020-10-22 17:39:23 +02:00
insert_into_furnace = false ,
insert_into_wagon = false ,
2021-05-23 17:03:52 +02:00
bottom_button = false ,
2020-10-22 17:39:23 +02:00
small_radius = 2
2020-06-07 23:25:29 +02:00
}
local Public = { }
Global.register (
2020-10-22 17:39:23 +02:00
this ,
2020-06-07 23:25:29 +02:00
function ( t )
2020-10-22 17:39:23 +02:00
this = t
2020-06-07 23:25:29 +02:00
end
)
2020-06-10 00:11:35 +02:00
local function create_floaty_text ( surface , position , name , count )
2020-10-22 17:39:23 +02:00
if this.floating_text_y_offsets [ position.x .. ' _ ' .. position.y ] then
2020-11-29 03:23:42 +02:00
this.floating_text_y_offsets [ position.x .. ' _ ' .. position.y ] = this.floating_text_y_offsets [ position.x .. ' _ ' .. position.y ] - 0.5
2020-06-07 23:25:29 +02:00
else
2020-10-22 17:39:23 +02:00
this.floating_text_y_offsets [ position.x .. ' _ ' .. position.y ] = 0
2020-06-07 23:25:29 +02:00
end
surface.create_entity (
{
name = ' flying-text ' ,
position = {
position.x ,
2020-10-22 17:39:23 +02:00
position.y + this.floating_text_y_offsets [ position.x .. ' _ ' .. position.y ]
2020-06-07 23:25:29 +02:00
} ,
2020-06-30 15:55:29 +02:00
text = { ' ' , ' - ' , count , ' ' , game.item_prototypes [ name ] . localised_name } ,
2020-06-07 23:25:29 +02:00
color = { r = 255 , g = 255 , b = 255 }
}
)
2019-09-24 23:04:55 +02:00
end
2021-11-02 08:08:30 +02:00
local function prepare_floaty_text ( list , surface , position , name , count )
local str = surface.index .. ' , ' .. position.x .. ' , ' .. position.y
if not list [ str ] then
list [ str ] = { }
end
if not list [ str ] [ name ] then
list [ str ] [ name ] = { surface = surface , position = position , count = 0 }
end
list [ str ] [ name ] . count = list [ str ] [ name ] . count + count
end
2021-07-27 21:22:56 +02:00
local function chest_is_valid ( chest )
2020-06-07 23:25:29 +02:00
for _ , e in pairs (
chest.surface . find_entities_filtered (
{
type = { ' inserter ' , ' loader ' } ,
area = { { chest.position . x - 1 , chest.position . y - 1 } , { chest.position . x + 1 , chest.position . y + 1 } }
}
)
) do
if e.name ~= ' long-handed-inserter ' then
if e.position . x == chest.position . x then
if e.direction == 0 or e.direction == 4 then
return false
end
end
if e.position . y == chest.position . y then
if e.direction == 2 or e.direction == 6 then
return false
end
end
end
end
2020-10-22 00:32:34 +02:00
local i1 = chest.surface . find_entity ( ' long-handed-inserter ' , { chest.position . x - 2 , chest.position . y } )
if i1 then
if i1.direction == 2 or i1.direction == 6 then
2020-06-07 23:25:29 +02:00
return false
end
end
2020-10-22 00:32:34 +02:00
local i2 = chest.surface . find_entity ( ' long-handed-inserter ' , { chest.position . x + 2 , chest.position . y } )
if i2 then
if i2.direction == 2 or i2.direction == 6 then
2020-06-07 23:25:29 +02:00
return false
end
end
2020-10-22 00:32:34 +02:00
local i3 = chest.surface . find_entity ( ' long-handed-inserter ' , { chest.position . x , chest.position . y - 2 } )
if i3 then
if i3.direction == 0 or i3.direction == 4 then
2020-06-07 23:25:29 +02:00
return false
end
end
2020-10-22 00:32:34 +02:00
local i4 = chest.surface . find_entity ( ' long-handed-inserter ' , { chest.position . x , chest.position . y + 2 } )
if i4 then
if i4.direction == 0 or i4.direction == 4 then
2020-06-07 23:25:29 +02:00
return false
end
end
return true
2019-09-24 23:04:55 +02:00
end
2020-03-10 03:51:08 +02:00
local function sort_entities_by_distance ( position , entities )
2020-06-07 23:25:29 +02:00
local t = { }
local distance
local index
local size_of_entities = # entities
if size_of_entities < 2 then
return
end
for _ , entity in pairs ( entities ) do
distance = ( entity.position . x - position.x ) ^ 2 + ( entity.position . y - position.y ) ^ 2
2021-03-16 21:09:21 +02:00
index = floor ( distance ) + 1
2020-06-07 23:25:29 +02:00
if not t [ index ] then
t [ index ] = { }
end
table.insert ( t [ index ] , entity )
end
local i = 0
for _ , range in pairs ( t ) do
for _ , entity in pairs ( range ) do
i = i + 1
entities [ i ] = entity
end
end
2020-03-10 03:51:08 +02:00
end
2020-10-22 17:39:23 +02:00
local function get_nearby_chests ( player , a , furnace , wagon )
2020-06-07 23:25:29 +02:00
local r = player.force . character_reach_distance_bonus + 10
local r_square = r * r
2021-11-02 08:08:30 +02:00
local chests , inventories = { } , { }
2020-06-07 23:25:29 +02:00
local size_of_chests = 0
local area = { { player.position . x - r , player.position . y - r } , { player.position . x + r , player.position . y + r } }
2020-10-22 00:32:34 +02:00
area = a or area
2021-11-02 08:08:30 +02:00
local container_type = { ' container ' , ' logistic-container ' , ' linked-container ' }
local inventory_type = defines.inventory . chest
2020-06-07 23:25:29 +02:00
local containers = { }
local i = 0
2020-10-18 01:42:09 +02:00
2020-10-22 17:39:23 +02:00
if furnace then
container_type = { ' furnace ' }
2021-11-02 08:08:30 +02:00
inventory_type = defines.inventory . furnace_source
2020-10-22 17:39:23 +02:00
end
if wagon then
container_type = { ' cargo-wagon ' }
2021-11-02 08:08:30 +02:00
inventory_type = defines.inventory . cargo_wagon
2020-10-18 01:42:09 +02:00
end
2021-11-02 08:08:30 +02:00
for _ , e in pairs ( player.surface . find_entities_filtered ( { type = container_type , area = area , force = player.force } ) ) do
2020-06-07 23:25:29 +02:00
if ( ( player.position . x - e.position . x ) ^ 2 + ( player.position . y - e.position . y ) ^ 2 ) <= r_square then
i = i + 1
containers [ i ] = e
end
end
2021-02-15 23:36:20 +02:00
if # containers <= 0 then
if is_mod_loaded ( ' Krastorio2 ' ) then
2021-11-02 08:08:30 +02:00
for _ , e in pairs ( player.surface . find_entities_filtered ( { type = ' assembling-machine ' , area = area , force = player.force } ) ) do
2021-02-15 23:36:20 +02:00
if ( ( player.position . x - e.position . x ) ^ 2 + ( player.position . y - e.position . y ) ^ 2 ) <= r_square then
i = i + 1
containers [ i ] = e
end
end
end
end
2020-06-07 23:25:29 +02:00
sort_entities_by_distance ( player.position , containers )
for _ , entity in pairs ( containers ) do
size_of_chests = size_of_chests + 1
chests [ size_of_chests ] = entity
2021-11-02 08:08:30 +02:00
inventories [ size_of_chests ] = entity.get_inventory ( inventory_type )
2020-06-07 23:25:29 +02:00
end
2021-11-02 08:08:30 +02:00
return { chest = chests , inventory = inventories }
2019-09-24 23:04:55 +02:00
end
local function does_inventory_contain_item_type ( inventory , item_subgroup )
2020-10-22 00:32:34 +02:00
for name , _ in pairs ( inventory.get_contents ( ) ) do
local t = game.item_prototypes [ name ]
if t and t.subgroup . name == item_subgroup then
2020-06-07 23:25:29 +02:00
return true
end
end
return false
2019-09-24 23:04:55 +02:00
end
2021-11-02 08:08:30 +02:00
local function insert_to_furnace ( player_inventory , chests , name , count , floaty_text_list )
2021-03-16 21:09:21 +02:00
local try = 0
2021-11-02 09:45:14 +02:00
local to_insert = floor ( count / # chests.chest )
2021-06-06 20:35:04 +02:00
if to_insert <= 0 then
2021-06-07 14:04:44 +02:00
if count > 0 then
to_insert = count
else
return
end
2021-06-06 20:35:04 +02:00
end
2021-06-07 14:04:44 +02:00
2021-11-02 09:45:14 +02:00
local variate = count % # chests.chest
local chests_available = # chests.chest
local tries = # chests.chest
2021-03-16 21:09:21 +02:00
:: retry ::
2020-07-02 19:13:20 +02:00
2020-10-22 17:39:23 +02:00
--Attempt to store into furnaces.
2021-11-02 08:08:30 +02:00
for chestnr , chest in pairs ( chests.chest ) do
local chest_inventory = chests.inventory [ chestnr ]
2021-07-27 21:22:56 +02:00
local amount = to_insert
if variate > 0 then
amount = amount + 1
variate = variate - 1
end
if amount <= 0 then
return
end
2021-03-16 21:09:21 +02:00
2021-07-27 21:22:56 +02:00
if chest_inventory then
if ( chest.type == ' furnace ' or chest.type == ' assembling-machine ' ) then
if name == ' stone ' then
local valid_to_insert = ( amount % 2 == 0 )
if valid_to_insert then
2021-03-16 21:09:21 +02:00
if chest_inventory.can_insert ( { name = name , count = amount } ) then
2021-07-26 21:53:57 +02:00
local inserted_count = chest_inventory.insert ( { name = name , count = amount } )
2021-03-16 21:09:21 +02:00
player_inventory.remove ( { name = name , count = inserted_count } )
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-03-16 21:09:21 +02:00
count = count - inserted_count
if count <= 0 then
return
end
end
2021-07-27 21:22:56 +02:00
else
try = try + 1
if try <= tries then
chests_available = chests_available - 1
to_insert = floor ( count / chests_available )
variate = count % chests_available
goto retry
end
end
else
if chest_inventory.can_insert ( { name = name , count = amount } ) then
local inserted_count = chest_inventory.insert ( { name = name , count = amount } )
player_inventory.remove ( { name = name , count = inserted_count } )
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-27 21:22:56 +02:00
count = count - inserted_count
if count <= 0 then
return
end
2021-03-16 21:09:21 +02:00
end
2020-06-07 23:25:29 +02:00
end
end
end
2021-07-27 21:22:56 +02:00
end
2020-06-07 23:25:29 +02:00
2021-11-02 09:45:14 +02:00
to_insert = floor ( count / # chests.chest )
variate = count % # chests.chest
2021-03-16 21:09:21 +02:00
2021-11-02 08:08:30 +02:00
for _ , chest in pairs ( chests.chest ) do -- fuel
2021-07-27 21:22:56 +02:00
if chest.type == ' furnace ' or chest.type == ' assembling-machine ' then
local amount = to_insert
if variate > 0 then
amount = amount + 1
variate = variate - 1
end
if amount <= 0 then
return
end
local chest_inventory = chest.get_inventory ( defines.inventory . chest )
if chest_inventory and chest_inventory.can_insert ( { name = name , count = amount } ) then
local inserted_count = chest_inventory.insert ( { name = name , count = amount } )
player_inventory.remove ( { name = name , count = inserted_count } )
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-27 21:22:56 +02:00
count = count - inserted_count
if count <= 0 then
2020-10-18 01:42:09 +02:00
return
end
2021-07-27 21:22:56 +02:00
end
end
end
end
2021-11-02 08:08:30 +02:00
local function insert_into_wagon ( stack , chests , name , floaty_text_list )
2021-07-27 21:22:56 +02:00
-- Attempt to load filtered cargo wagon
2021-11-02 09:45:14 +02:00
for chestnr , chest in pairs ( chests.chest ) do
2021-07-27 21:22:56 +02:00
if chest.type == ' cargo-wagon ' then
2021-11-02 08:08:30 +02:00
local chest_inventory = chests.inventory [ chestnr ]
2021-07-27 21:22:56 +02:00
if chest_inventory.can_insert ( stack ) then
2021-07-28 22:32:50 +02:00
local inserted_count = chest_inventory.insert ( stack )
stack.count = stack.count - inserted_count
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-28 22:32:50 +02:00
if stack.count <= 0 then
2021-11-02 08:08:30 +02:00
return chestnr
2020-10-22 17:39:23 +02:00
end
2020-10-18 01:42:09 +02:00
end
end
end
2021-07-27 21:22:56 +02:00
end
2020-10-18 01:42:09 +02:00
2021-11-02 08:08:30 +02:00
local function insert_into_wagon_filtered ( stack , chests , name , floaty_text_list )
2021-07-26 21:53:57 +02:00
2021-07-27 21:22:56 +02:00
-- Attempt to load filtered cargo wagon
2021-11-02 08:08:30 +02:00
for chestnr , chest in pairs ( chests.chest ) do
2021-07-27 21:22:56 +02:00
if chest.type == ' cargo-wagon ' then
2021-11-02 08:08:30 +02:00
local chest_inventory = chests.inventory [ chestnr ]
2021-07-27 21:22:56 +02:00
for index = 1 , 40 do
if chest_inventory.can_insert ( stack ) then
if chest_inventory.get_filter ( index ) ~= nil then
local n = chest_inventory.get_filter ( index )
if n == name then
local inserted_count = chest_inventory.insert ( stack )
2021-07-28 22:32:50 +02:00
stack.count = stack.count - inserted_count
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-28 22:32:50 +02:00
if stack.count <= 0 then
2021-11-02 08:08:30 +02:00
return chestnr
2021-07-27 21:22:56 +02:00
end
2021-07-26 21:53:57 +02:00
end
2020-10-22 17:39:23 +02:00
end
2020-10-22 13:33:53 +02:00
end
2020-10-22 00:32:34 +02:00
end
end
2021-07-27 21:22:56 +02:00
end
end
2020-10-22 00:32:34 +02:00
2021-11-02 08:08:30 +02:00
local function insert_item_into_chest ( stack , chests , filtered_chests , name , floaty_text_list , previous_insert )
2021-07-27 21:22:56 +02:00
local container = {
[ ' container ' ] = true ,
2021-07-28 22:32:50 +02:00
[ ' logistic-container ' ] = true ,
2021-11-02 08:08:30 +02:00
[ ' linked-container ' ] = true
2021-07-27 21:22:56 +02:00
}
2021-11-02 08:08:30 +02:00
--Attemp to store in chest that stored last same item
if previous_insert.name == name and previous_insert.full ~= nil then
local chest_inventory = chests.inventory [ previous_insert.full ]
if chest_inventory and chest_inventory.can_insert ( stack ) then
local inserted_count = chest_inventory.insert ( stack )
stack.count = stack.count - inserted_count
prepare_floaty_text ( floaty_text_list , chests.chest [ previous_insert.full ] . surface , chests.chest [ previous_insert.full ] . position , name , inserted_count )
if stack.count <= 0 then
return previous_insert.full
end
end
end
2021-07-27 21:22:56 +02:00
--Attempt to store in chests that already have the same item.
2021-11-02 08:08:30 +02:00
for chestnr , chest in pairs ( chests.chest ) do
2021-07-27 21:22:56 +02:00
if container [ chest.type ] then
2021-11-02 08:08:30 +02:00
local chest_inventory = chests.inventory [ chestnr ]
2021-07-28 22:32:50 +02:00
if chest_inventory and chest_inventory.can_insert ( stack ) then
if chest_inventory.find_item_stack ( stack.name ) then
local inserted_count = chest_inventory.insert ( stack )
stack.count = stack.count - inserted_count
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-28 22:32:50 +02:00
if stack.count <= 0 then
2021-11-02 08:08:30 +02:00
return chestnr
2021-07-27 21:22:56 +02:00
end
end
end
end
end
--Attempt to store in empty chests.
2021-11-02 08:08:30 +02:00
for chestnr , chest in pairs ( filtered_chests.chest ) do
2021-07-27 21:22:56 +02:00
if container [ chest.type ] then
2021-11-02 08:08:30 +02:00
local chest_inventory = filtered_chests.inventory [ chestnr ]
2021-07-28 22:32:50 +02:00
if chest_inventory and chest_inventory.can_insert ( stack ) then
2021-07-27 21:22:56 +02:00
if chest_inventory.is_empty ( ) then
2021-07-28 22:32:50 +02:00
local inserted_count = chest_inventory.insert ( stack )
stack.count = stack.count - inserted_count
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-28 22:32:50 +02:00
if stack.count <= 0 then
2021-11-02 08:08:30 +02:00
return chestnr
2020-06-07 23:25:29 +02:00
end
end
end
end
2021-07-27 21:22:56 +02:00
end
2020-06-07 23:25:29 +02:00
2021-07-27 21:22:56 +02:00
--Attempt to store in chests with same item subgroup.
local item_subgroup = game.item_prototypes [ name ] . subgroup.name
if item_subgroup then
2021-11-02 08:08:30 +02:00
for chestnr , chest in pairs ( filtered_chests.chest ) do
2020-06-10 00:11:35 +02:00
if container [ chest.type ] then
2021-11-02 08:08:30 +02:00
local chest_inventory = filtered_chests.inventory [ chestnr ]
2021-07-28 22:32:50 +02:00
if chest_inventory and chest_inventory.can_insert ( stack ) then
2021-07-27 21:22:56 +02:00
if does_inventory_contain_item_type ( chest_inventory , item_subgroup ) then
2021-07-28 22:32:50 +02:00
local inserted_count = chest_inventory.insert ( stack )
stack.count = stack.count - inserted_count
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-28 22:32:50 +02:00
if stack.count <= 0 then
2021-11-02 08:08:30 +02:00
return chestnr
2020-06-07 23:25:29 +02:00
end
end
end
end
end
2021-07-27 21:22:56 +02:00
end
2020-06-07 23:25:29 +02:00
2021-07-27 21:22:56 +02:00
--Attempt to store in mixed chests.
2021-11-02 08:08:30 +02:00
for chestnr , chest in pairs ( filtered_chests.chest ) do
2021-07-27 21:22:56 +02:00
if container [ chest.type ] then
2021-11-02 08:08:30 +02:00
local chest_inventory = filtered_chests.inventory [ chestnr ]
2021-07-28 22:32:50 +02:00
if chest_inventory.can_insert ( stack ) then
local inserted_count = chest_inventory.insert ( stack )
stack.count = stack.count - inserted_count
2021-11-02 08:08:30 +02:00
prepare_floaty_text ( floaty_text_list , chest.surface , chest.position , name , inserted_count )
2021-07-28 22:32:50 +02:00
if stack.count <= 0 then
2021-11-02 08:08:30 +02:00
return chestnr
2020-06-07 23:25:29 +02:00
end
end
end
end
2019-09-24 23:04:55 +02:00
end
2019-10-24 23:00:14 +02:00
local function auto_stash ( player , event )
2020-06-07 23:25:29 +02:00
local button = event.button
2020-10-22 17:39:23 +02:00
local ctrl = event.control
2020-10-22 00:35:36 +02:00
local shift = event.shift
2020-06-07 23:25:29 +02:00
if not player.character then
player.print ( ' It seems that you are not in the realm of the living. ' , print_color )
return
end
if not player.character . valid then
player.print ( ' It seems that you are not in the realm of the living. ' , print_color )
return
end
2021-07-27 21:22:56 +02:00
local inventory = player.get_main_inventory ( )
2020-06-07 23:25:29 +02:00
if inventory.is_empty ( ) then
player.print ( ' Inventory is empty. ' , print_color )
return
end
2020-12-05 19:09:37 +02:00
2021-11-02 08:08:30 +02:00
local floaty_text_list = { }
2021-11-02 09:45:14 +02:00
local chests = { chest = { } , inventory = { } }
2020-10-22 17:39:23 +02:00
local r = this.small_radius
2020-10-22 00:32:34 +02:00
local area = { { player.position . x - r , player.position . y - r } , { player.position . x + r , player.position . y + r } }
2020-10-22 17:39:23 +02:00
if ctrl then
if button == defines.mouse_button_type . right and this.insert_into_furnace then
chests = get_nearby_chests ( player , nil , true , false )
end
elseif shift then
2020-12-04 02:09:59 +02:00
if button == defines.mouse_button_type . right and this.insert_into_wagon or button == defines.mouse_button_type . left and this.insert_into_wagon then
2020-10-22 17:39:23 +02:00
chests = get_nearby_chests ( player , area , false , true )
2020-10-22 00:32:34 +02:00
end
else
chests = get_nearby_chests ( player )
end
2021-11-02 08:08:30 +02:00
if not chests.chest or not chests.chest [ 1 ] then
2020-06-07 23:25:29 +02:00
player.print ( ' No valid nearby containers found. ' , print_color )
return
end
2021-07-27 21:22:56 +02:00
2021-11-02 08:08:30 +02:00
local filtered_chests = { chest = { } , inventory = { } }
for index , e in pairs ( chests.chest ) do
2021-07-27 21:22:56 +02:00
if chest_is_valid ( e ) then
2021-11-06 00:24:21 +02:00
filtered_chests.chest [ index ] = e
filtered_chests.inventory [ index ] = chests.inventory [ index ]
2020-06-07 23:25:29 +02:00
end
2020-12-05 19:09:37 +02:00
end
2020-06-07 23:25:29 +02:00
2020-12-05 19:09:37 +02:00
this.floating_text_y_offsets = { }
2020-06-07 23:25:29 +02:00
2020-12-05 19:09:37 +02:00
local hotbar_items = { }
for i = 1 , 100 , 1 do
local prototype = player.get_quick_bar_slot ( i )
if prototype then
hotbar_items [ prototype.name ] = true
2020-06-07 23:25:29 +02:00
end
2020-12-05 19:09:37 +02:00
end
2020-06-07 23:25:29 +02:00
2021-07-28 22:32:50 +02:00
local furnaceList = {
[ ' coal ' ] = 0 ,
[ ' iron-ore ' ] = 0 ,
[ ' copper-ore ' ] = 0 ,
[ ' stone ' ] = 0
}
2021-03-19 17:17:38 +02:00
2021-11-02 08:08:30 +02:00
local full_insert = { full = nil , name = nil }
2021-07-28 22:32:50 +02:00
for i = # inventory , 1 , - 1 do
if not inventory [ i ] . valid_for_read then
goto continue
end
local name = inventory [ i ] . name
2020-10-22 17:39:23 +02:00
local is_resource = this.whitelist [ name ]
2020-06-10 00:11:35 +02:00
2021-07-28 22:32:50 +02:00
if not hotbar_items [ name ] then
2020-10-22 17:39:23 +02:00
if ctrl and this.insert_into_furnace then
if button == defines.mouse_button_type . right then
2020-10-22 00:32:34 +02:00
if is_resource then
2021-07-28 22:32:50 +02:00
furnaceList [ name ] = ( furnaceList [ name ] or 0 ) + inventory [ i ] . count
2020-10-22 00:32:34 +02:00
end
end
2020-10-22 17:39:23 +02:00
elseif shift and this.insert_into_wagon then
2020-10-22 00:32:34 +02:00
if button == defines.mouse_button_type . right then
2020-10-22 17:39:23 +02:00
if is_resource then
2021-11-02 09:45:14 +02:00
full_insert = { full = insert_into_wagon ( inventory [ i ] , chests , name , floaty_text_list ) , name = name }
2020-10-22 17:39:23 +02:00
end
2021-11-02 09:45:14 +02:00
end
if button == defines.mouse_button_type . left then
full_insert = { full = insert_into_wagon_filtered ( inventory [ i ] , chests , name , floaty_text_list ) , name = name }
2020-06-07 23:25:29 +02:00
end
elseif button == defines.mouse_button_type . right then
2020-10-22 00:32:34 +02:00
if is_resource then
2021-11-02 08:08:30 +02:00
full_insert = { full = insert_item_into_chest ( inventory [ i ] , chests , filtered_chests , name , floaty_text_list , full_insert ) , name = name }
2020-06-07 23:25:29 +02:00
end
elseif button == defines.mouse_button_type . left then
2021-11-02 08:08:30 +02:00
full_insert = { full = insert_item_into_chest ( inventory [ i ] , chests , filtered_chests , name , floaty_text_list , full_insert ) , name = name }
end
if not full_insert.success then
hotbar_items [ # hotbar_items + 1 ] = name
2020-06-07 23:25:29 +02:00
end
end
2021-07-28 22:32:50 +02:00
:: continue ::
end
for furnaceName , furnaceCount in pairs ( furnaceList ) do
2021-11-02 08:08:30 +02:00
insert_to_furnace ( inventory , chests , furnaceName , furnaceCount , floaty_text_list )
end
for _ , texts in pairs ( floaty_text_list ) do
for name , text in pairs ( texts ) do
create_floaty_text ( text.surface , text.position , name , text.count )
end
2020-06-07 23:25:29 +02:00
end
2020-10-22 17:39:23 +02:00
local c = this.floating_text_y_offsets
2020-06-07 23:30:28 +02:00
for k , _ in pairs ( c ) do
2020-10-22 17:39:23 +02:00
this.floating_text_y_offsets [ k ] = nil
2020-06-07 23:30:28 +02:00
end
2019-09-24 23:04:55 +02:00
end
local function create_gui_button ( player )
2020-06-07 23:25:29 +02:00
if player.gui . top.auto_stash then
return
end
local tooltip
2020-10-22 17:39:23 +02:00
if this.insert_into_furnace and this.insert_into_wagon then
tooltip =
2021-02-01 21:01:07 +02:00
' Sort your inventory into nearby chests. \n LMB: Everything, excluding quickbar items. \n RMB: Only ores to nearby chests, excluding quickbar items. \n CTRL+RMB: Fill nearby furnaces. \n SHIFT+LMB: Everything onto filtered slots to wagon. \n SHIFT+RMB: Only ores to wagon '
2020-10-22 17:39:23 +02:00
elseif this.insert_into_furnace then
2021-02-01 21:01:07 +02:00
tooltip =
' Sort your inventory into nearby chests. \n LMB: Everything, excluding quickbar items. \n RMB: Only ores to nearby chests, excluding quickbar items. \n CTRL+RMB: Fill nearby furnaces. '
2020-10-22 17:39:23 +02:00
elseif this.insert_into_wagon then
2020-06-07 23:25:29 +02:00
tooltip =
2021-02-01 21:01:07 +02:00
' Sort your inventory into nearby chests. \n LMB: Everything, excluding quickbar items. \n RMB: Only ores to nearby chests, excluding quickbar items. \n SHIFT+LMB: Everything onto filtered slots to wagon. \n SHIFT+RMB: Only ores to wagon '
2020-06-07 23:25:29 +02:00
else
2021-02-01 21:01:07 +02:00
tooltip = ' Sort your inventory into nearby chests. \n LMB: Everything, excluding quickbar items. \n RMB: Only ores to nearby chests, excluding quickbar items. '
2020-06-07 23:25:29 +02:00
end
2021-05-23 17:03:52 +02:00
if this.bottom_button then
local data = BottomFrame.get ( ' bottom_quickbar_button ' )
-- save it for later use
data.tooltip = tooltip
data.sprite = ' item/wooden-chest '
if data [ player.index ] then
local f = data [ player.index ]
if f.frame and f.frame . valid then
f.frame . sprite = ' item/wooden-chest '
f.frame . tooltip = tooltip
end
end
else
local b =
player.gui . top.add (
{
type = ' sprite-button ' ,
sprite = ' item/wooden-chest ' ,
name = ' auto_stash ' ,
tooltip = tooltip
}
)
b.style . font_color = { r = 0.11 , g = 0.8 , b = 0.44 }
b.style . font = ' heading-1 '
b.style . minimal_height = 40
b.style . maximal_width = 40
b.style . minimal_width = 38
b.style . maximal_height = 38
b.style . padding = 1
b.style . margin = 0
end
2019-09-24 23:04:55 +02:00
end
2020-10-22 00:32:34 +02:00
local function do_whitelist ( )
local resources = game.entity_prototypes
2021-03-19 17:17:38 +02:00
local items = game.item_prototypes
2020-10-22 17:39:23 +02:00
this.whitelist = { }
2020-10-22 00:32:34 +02:00
for k , _ in pairs ( resources ) do
if resources [ k ] and resources [ k ] . type == ' resource ' and resources [ k ] . mineable_properties then
if resources [ k ] . mineable_properties.products [ 1 ] then
local r = resources [ k ] . mineable_properties.products [ 1 ] . name
2020-10-22 17:39:23 +02:00
this.whitelist [ r ] = true
2020-10-22 00:32:34 +02:00
elseif resources [ k ] . mineable_properties.products [ 2 ] then
local r = resources [ k ] . mineable_properties.products [ 2 ] . name
2020-10-22 17:39:23 +02:00
this.whitelist [ r ] = true
2020-10-22 00:32:34 +02:00
end
end
end
2021-03-19 17:17:38 +02:00
for k , _ in pairs ( items ) do
if items [ k ] and items [ k ] . group.name == ' resource-refining ' then
local r = items [ k ] . name
this.whitelist [ r ] = true
end
end
2020-10-22 00:32:34 +02:00
end
2019-09-24 23:04:55 +02:00
local function on_player_joined_game ( event )
2020-06-07 23:25:29 +02:00
create_gui_button ( game.players [ event.player_index ] )
2019-09-24 23:04:55 +02:00
end
local function on_gui_click ( event )
2020-06-07 23:25:29 +02:00
if not event.element then
return
end
if not event.element . valid then
return
end
2020-12-30 12:07:44 +02:00
local player = game.players [ event.player_index ]
2020-12-29 14:46:32 +02:00
local name = ' auto_stash '
2021-05-23 17:03:52 +02:00
if this.bottom_button then
local data = BottomFrame.get ( ' bottom_quickbar_button ' )
if data [ player.index ] then
data = data [ player.index ]
name = data.name
end
end
2020-12-29 14:46:32 +02:00
if event.element . name == name then
2020-12-30 12:07:44 +02:00
auto_stash ( player , event )
2020-06-07 23:25:29 +02:00
end
end
2019-10-24 23:00:14 +02:00
2020-10-22 17:39:23 +02:00
function Public . insert_into_furnace ( value )
if value then
this.insert_into_furnace = value
else
this.insert_into_furnace = false
end
end
2020-10-18 01:42:09 +02:00
function Public . insert_into_wagon ( value )
if value then
2020-10-22 17:39:23 +02:00
this.insert_into_wagon = value
else
this.insert_into_wagon = false
2020-10-18 01:42:09 +02:00
end
end
2021-05-23 17:03:52 +02:00
function Public . bottom_button ( value )
if value then
this.bottom_button = value
else
this.bottom_button = false
end
end
2021-03-16 23:44:40 +02:00
Event.on_configuration_changed ( do_whitelist )
2020-10-22 00:32:34 +02:00
Event.on_init ( do_whitelist )
Event.add ( defines.events . on_player_joined_game , on_player_joined_game )
Event.add ( defines.events . on_gui_click , on_gui_click )
2020-06-07 23:25:29 +02:00
return Public