1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2024-12-26 22:56:43 +02:00
This commit is contained in:
Gerkiz 2020-10-22 17:39:23 +02:00
parent 45127336a0
commit 736f556d82
4 changed files with 119 additions and 78 deletions

View File

@ -239,6 +239,7 @@ local function on_init()
T.main_caption_color = {r = 200, g = 100, b = 0}
T.sub_caption_color = {r = 0, g = 175, b = 175}
Autostash.insert_into_furnace(true)
Autostash.insert_into_wagon(true)
end

View File

@ -293,6 +293,7 @@ function Public.reset_map()
this.active_surface_index = CS.create_surface()
Autostash.insert_into_furnace(true)
Autostash.insert_into_wagon(true)
Poll.reset()

View File

@ -6,34 +6,36 @@ local Event = require 'utils.event'
local math_floor = math.floor
local print_color = {r = 120, g = 255, b = 0}
local autostash = {
local this = {
floating_text_y_offsets = {},
whitelist = {},
insert_into_wagon = false
insert_into_furnace = false,
insert_into_wagon = false,
small_radius = 2
}
local Public = {}
Global.register(
autostash,
this,
function(t)
autostash = t
this = t
end
)
local function create_floaty_text(surface, position, name, count)
if autostash.floating_text_y_offsets[position.x .. '_' .. position.y] then
autostash.floating_text_y_offsets[position.x .. '_' .. position.y] =
autostash.floating_text_y_offsets[position.x .. '_' .. position.y] - 0.5
if this.floating_text_y_offsets[position.x .. '_' .. position.y] then
this.floating_text_y_offsets[position.x .. '_' .. position.y] =
this.floating_text_y_offsets[position.x .. '_' .. position.y] - 0.5
else
autostash.floating_text_y_offsets[position.x .. '_' .. position.y] = 0
this.floating_text_y_offsets[position.x .. '_' .. position.y] = 0
end
surface.create_entity(
{
name = 'flying-text',
position = {
position.x,
position.y + autostash.floating_text_y_offsets[position.x .. '_' .. position.y]
position.y + this.floating_text_y_offsets[position.x .. '_' .. position.y]
},
text = {'', '-', count, ' ', game.item_prototypes[name].localised_name},
color = {r = 255, g = 255, b = 255}
@ -48,7 +50,12 @@ local function chest_is_valid(chest)
for index = 1, 40 do
if chest_inventory.get_filter(index) ~= nil then
local n = chest_inventory.get_filter(index)
t[n] = true
local item_stack = game.item_prototypes[n].stack_size
if (t[n] and t[n].valid) then
t[n].count = t[n].count + item_stack
else
t[n] = {count = item_stack, valid = true}
end
end
end
@ -137,7 +144,7 @@ local function sort_entities_by_distance(position, entities)
end
end
local function get_nearby_chests(player, a)
local function get_nearby_chests(player, a, furnace, wagon)
local r = player.force.character_reach_distance_bonus + 10
local r_square = r * r
local chests = {}
@ -146,12 +153,15 @@ local function get_nearby_chests(player, a)
area = a or area
local container_type = {'container', 'logistic-container', 'furnace'}
local container_type = {'container', 'logistic-container'}
local containers = {}
local i = 0
if autostash.insert_into_wagon then
table.insert(container_type, 'cargo-wagon')
if furnace then
container_type = {'furnace'}
end
if wagon then
container_type = {'cargo-wagon'}
end
for _, e in pairs(player.surface.find_entities_filtered({type = container_type, area = area, force = 'player'})) do
@ -179,7 +189,7 @@ local function does_inventory_contain_item_type(inventory, item_subgroup)
return false
end
local function insert_item_into_chest(player_inventory, chests, filtered_chests, name, count, wagon)
local function insert_item_into_chest(player_inventory, chests, filtered_chests, name, count, furnace, wagon)
local container = {
['container'] = true,
['logistic-container'] = true
@ -188,12 +198,49 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests,
local to_insert = math.floor(count / #chests)
local variator = count % #chests
--Attempt to store into furnaces.
if furnace then
for _, chest in pairs(chests) do
local chest_inventory = chest.get_inventory(defines.inventory.furnace_source)
if chest_inventory and chest.type == 'furnace' then
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
for _, chest in pairs(chests) do
if chest.type == 'furnace' then
local amount = to_insert
if variator > 0 then
amount = amount + 1
variator = variator - 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})
create_floaty_text(chest.surface, chest.position, name, inserted_count)
end
end
end
end
-- Attempt to load filtered cargo wagon
if wagon then
-- Attempt to load filtered cargo wagon
for _, chest in pairs(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
if chest_inventory and 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)
@ -206,48 +253,11 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests,
end
end
--Attempt to store into furnaces.
for _, chest in pairs(chests) do
local chest_inventory = chest.get_inventory(defines.inventory.furnace_source)
if chest_inventory and chest.type == 'furnace' then
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
for _, chest in pairs(chests) do
if chest.type == 'furnace' then
local amount = to_insert
if variator > 0 then
amount = amount + 1
variator = variator - 1
end
if amount <= 0 then
return
end
local chest_inventory = chest.get_inventory(defines.inventory.chest)
if chest_inventory then
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})
create_floaty_text(chest.surface, chest.position, name, inserted_count)
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
local chest_inventory = chest.get_inventory(defines.inventory.chest)
if chest_inventory.can_insert({name = name, count = count}) then
if chest_inventory and chest_inventory.can_insert({name = name, count = count}) then
if chest_inventory.find_item_stack(name) then
local inserted_count = chest_inventory.insert({name = name, count = count})
@ -266,7 +276,7 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests,
for _, chest in pairs(filtered_chests) do
if container[chest.type] then
local chest_inventory = chest.get_inventory(defines.inventory.chest)
if chest_inventory.can_insert({name = name, count = count}) then
if chest_inventory and chest_inventory.can_insert({name = name, count = count}) then
if chest_inventory.is_empty() then
local inserted_count = chest_inventory.insert({name = name, count = count})
player_inventory.remove({name = name, count = inserted_count})
@ -286,7 +296,7 @@ local function insert_item_into_chest(player_inventory, chests, filtered_chests,
for _, chest in pairs(filtered_chests) do
if container[chest.type] then
local chest_inventory = chest.get_inventory(defines.inventory.chest)
if chest_inventory.can_insert({name = name, count = count}) then
if chest_inventory and chest_inventory.can_insert({name = name, count = count}) then
if does_inventory_contain_item_type(chest_inventory, item_subgroup) then
local inserted_count = chest_inventory.insert({name = name, count = count})
player_inventory.remove({name = name, count = inserted_count})
@ -320,6 +330,7 @@ end
local function auto_stash(player, event)
local button = event.button
local ctrl = event.control
local shift = event.shift
if not player.character then
player.print('It seems that you are not in the realm of the living.', print_color)
@ -335,14 +346,18 @@ local function auto_stash(player, event)
return
end
local chests
local r = 1
local r = this.small_radius
local area = {{player.position.x - r, player.position.y - r}, {player.position.x + r, player.position.y + r}}
if shift then
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
if
button == defines.mouse_button_type.right or
button == defines.mouse_button_type.left and autostash.insert_into_wagon
button == defines.mouse_button_type.left and this.insert_into_wagon
then
chests = get_nearby_chests(player, area)
chests = get_nearby_chests(player, area, false, true)
end
else
chests = get_nearby_chests(player)
@ -363,7 +378,7 @@ local function auto_stash(player, event)
end
end
autostash.floating_text_y_offsets = {}
this.floating_text_y_offsets = {}
local hotbar_items = {}
for i = 1, 100, 1 do
@ -374,18 +389,25 @@ local function auto_stash(player, event)
end
for name, count in pairs(inventory.get_contents()) do
local is_resource = autostash.whitelist[name]
local is_resource = this.whitelist[name]
if not inventory.find_item_stack(name).grid and not hotbar_items[name] then
if shift and autostash.insert_into_wagon then
if button == defines.mouse_button_type.left then
if ctrl and this.insert_into_furnace then
if button == defines.mouse_button_type.right then
if is_resource then
insert_item_into_chest(inventory, chests, filtered_chests, name, count, true)
insert_item_into_chest(inventory, chests, filtered_chests, name, count, true, false)
end
end
elseif shift and this.insert_into_wagon then
if button == defines.mouse_button_type.right then
if filtered_allowed and is_resource and filtered_allowed[name] then
insert_item_into_chest(inventory, chests, filtered_chests, name, count, true)
if is_resource then
insert_item_into_chest(inventory, chests, filtered_chests, name, count, false, true)
end
end
if button == defines.mouse_button_type.left then
if filtered_allowed and filtered_allowed[name] and filtered_allowed[name].valid then
local c = filtered_allowed[name].count
insert_item_into_chest(inventory, chests, filtered_chests, name, c, false, true)
end
end
elseif button == defines.mouse_button_type.right then
@ -398,9 +420,9 @@ local function auto_stash(player, event)
end
end
local c = autostash.floating_text_y_offsets
local c = this.floating_text_y_offsets
for k, _ in pairs(c) do
autostash.floating_text_y_offsets[k] = nil
this.floating_text_y_offsets[k] = nil
end
end
@ -409,11 +431,18 @@ local function create_gui_button(player)
return
end
local tooltip
if autostash.insert_into_wagon then
if this.insert_into_furnace and this.insert_into_wagon then
tooltip =
'Sort your inventory into nearby chests.\nLMB: Everything, excluding quickbar items.\nRMB: Only ores.\nSHIFT+LMB: Only ores to wagon\nSHIFT+RMB: Only ores onto filtered slots to wagon.'
'Sort your inventory into nearby chests.\nLMB: Everything, excluding quickbar items.\nRMB: Only ores to nearby chests.\nCTRL+RMB: Fill nearby furnaces.\nSHIFT+LMB: Everything onto filtered slots to wagon.\nSHIFT+RMB: Only ores to wagon'
elseif this.insert_into_furnace then
tooltip =
'Sort your inventory into nearby chests.\nLMB: Everything, excluding quickbar items.\nRMB: Only ores to nearby chests.\nCTRL+RMB: Fill nearby furnaces.'
elseif this.insert_into_wagon then
tooltip =
'Sort your inventory into nearby chests.\nLMB: Everything, excluding quickbar items.\nRMB: Only ores to nearby chests.\nSHIFT+LMB: Everything onto filtered slots to wagon.\nSHIFT+RMB: Only ores to wagon'
else
tooltip = 'Sort your inventory into nearby chests.\nLMB: Everything, excluding quickbar items.\nRMB: Only ores.'
tooltip =
'Sort your inventory into nearby chests.\nLMB: Everything, excluding quickbar items.\nRMB: Only ores to nearby chests.'
end
local b =
player.gui.top.add(
@ -436,15 +465,15 @@ end
local function do_whitelist()
local resources = game.entity_prototypes
autostash.whitelist = {}
this.whitelist = {}
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
autostash.whitelist[r] = true
this.whitelist[r] = true
elseif resources[k].mineable_properties.products[2] then
local r = resources[k].mineable_properties.products[2].name
autostash.whitelist[r] = true
this.whitelist[r] = true
end
end
end
@ -466,9 +495,19 @@ local function on_gui_click(event)
end
end
function Public.insert_into_furnace(value)
if value then
this.insert_into_furnace = value
else
this.insert_into_furnace = false
end
end
function Public.insert_into_wagon(value)
if value then
autostash.insert_into_wagon = value or false
this.insert_into_wagon = value
else
this.insert_into_wagon = false
end
end

View File

@ -203,7 +203,7 @@ local function get_random_close_spawner()
end
local k = math_random(1, #spawners)
local spawner_2 = spawners[k]
if not spawner_2.valid then
if not spawner_2 or not spawner_2.valid then
this.nests[k] = nil
goto retry
end