Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
bengardner committed Jan 11, 2024
1 parent bd21ba0 commit 556a200
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/NetworkViewUi_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local UiCharacterInventory = require "src.UiCharacterInventory"
local UiNetworkItems = require "src.UiNetworkItems"
local UiChestInventory = require "src.UiChestInventory"
local clog = require("src.log_console").log
local auto_player_request = require'src.auto_player_request'

local M = {}

Expand Down Expand Up @@ -367,7 +368,7 @@ Event.on_event("in_open_test_view", function (event)


Event.on_event("debug-network-item", function (event)
GlobalState.log_queue_info()
--GlobalState.log_queue_info()
-- log_ammo_stuff()
--[[ player_index, input_name, cursor_position, ]]
local player = game.get_player(event.player_index)
Expand All @@ -379,6 +380,7 @@ Event.on_event("debug-network-item", function (event)
if info ~= nil then
clog(" - %s", serpent.line(info))
end
auto_player_request.doit(player)
end
end)

Expand Down
24 changes: 2 additions & 22 deletions src/ServiceEntity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,19 @@ local function transfer_item_to_inv_level(entity, inv, name, count)
end
end

local fuel_list = {
--"processed-fuel",
"coal",
"wood",
}

local function service_refuel(entity, inv)
if inv.is_empty() then
local fuel_name, n_avail = GlobalState.get_best_available_fuel(entity)
if fuel_name == nil then
if fuel_name == nil or n_avail == nil then
return
end
--clog("best fuel for %s is %s and we have %s", entity.name, fuel_name, n_avail)
local prot = game.item_prototypes[fuel_name]
local n_avail = GlobalState.get_item_count(fuel_name)
if n_avail > 0 then
local prot = game.item_prototypes[fuel_name]
local n_add = (#inv * prot.stack_size) -- - 12
transfer_item_to_inv(entity, inv, fuel_name, math.min(n_avail, n_add))
end
return
--[[
for _, fuel_name in ipairs(fuel_list) do
local prot = game.item_prototypes[fuel_name]
local n_avail = GlobalState.get_item_count(fuel_name)
if prot ~= nil and n_avail > 0 then
-- add some if if empty
-- need to NOT fill it to the top or we can cause a deadlock
local n_add = (#inv * prot.stack_size) - 12
transfer_item_to_inv(entity, inv, fuel_name, math.min(n_avail, n_add))
return
end
end
]]
else
-- try to top off the fuel(s)
for fuel, _ in ipairs(inv.get_contents()) do
Expand Down
114 changes: 114 additions & 0 deletions src/auto_player_request.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
--[[
Automatically adds a 1-stack request for any researched items.
]]
local Event = require('__stdlib__/stdlib/event/event')
local item_utils = require("src.item_utils")
local clog = require("src.log_console").log

local function set_character_requests(player, character, items)
local requests = {}
for idx=1, character.request_slot_count do
local rr = character.get_request_slot(idx)
if rr ~= nil then
--print(string.format("old [%s] = %s", idx, serpent.line(rr)))
requests[rr.name] = { count=rr.count, slot=idx }
end
end

--print("\nitems:")
--print(serpent.block(items))

-- always add to the end for now
-- TODO: restructure to match the inventory list (break on groups)
local add_idx = 1
for _, info in ipairs(items) do
if info == 'break' then
local old_idx = add_idx
add_idx = 1 + math.floor((add_idx + 8) / 10) * 10
for idx=old_idx, add_idx -1 do
character.clear_request_slot(idx)
end
else
local item = info.item
local count = info.count
local rr = requests[item]
if rr ~= nil then
-- request exists -- do not touch if in the same slot
if rr.slot ~= add_idx then
if rr.slot > add_idx then
character.clear_request_slot(rr.slot)
end
character.clear_request_slot(add_idx)
character.set_request_slot({ name=item, count=math.max(1, rr.count) }, add_idx)
end
else
character.clear_request_slot(add_idx)
character.set_request_slot({name=item, count=count}, add_idx)
end
add_idx = add_idx + 1
end
end
for idx=add_idx, character.request_slot_count do
character.clear_request_slot(idx)
end
end

local function force_request_everything(force)
-- get all items that have a valid recipe and the stack size for each
local items_tab = {}
local items = {}
local function add_item(name, recipe, what)
if items_tab[name] == nil then
-- this filters out fluids
local prot = game.item_prototypes[name]
if prot ~= nil then
items_tab[name] = true
table.insert(items, { item = name, count = prot.stack_size })
-- print(string.format(" -- Added %s due to %s [%s]", name, recipe.name, what))
end
end
end
for _, recipe in pairs(force.recipes) do
--print(string.format("RECIPE: %s enabled=%s hidden=%s products=%s inf=%s", recipe.name, recipe.enabled, recipe.hidden, serpent.line(recipe.products), serpent.line(recipe.ingredients)))
if recipe.enabled == true and recipe.hidden ~= true then
for _, pp in ipairs(recipe.products) do
add_item(pp.name, recipe, 'product')
end
--[[ this gets too many items, especially if there is a 'scrap' recipe
for _, pp in ipairs(recipe.ingredients) do
if pp.type == "item" then
add_item(pp.name, recipe, 'ingredients')
end
end
]]
end
end
table.sort(items, item_utils.entry_compare_items)

items = item_utils.entry_list_split_by_group(items)

for _, player in ipairs(force.players) do
local character = player.character
if character ~= nil and player.character_personal_logistic_requests_enabled then
set_character_requests(player, character, items)
end
end
end

local function on_research_stuff(event)
local research = event.research
if research ~= nil and research.force ~= nil then
force_request_everything(research.force)
end
end

Event.on_event(defines.events.on_research_finished, on_research_stuff)
Event.on_event(defines.events.on_research_reversed, on_research_stuff)

local M = {}

function M.doit(player)
force_request_everything(player.force)
end

return M

0 comments on commit 556a200

Please sign in to comment.