From 556a20030a90c89eab9f5f459f466d4eec14384f Mon Sep 17 00:00:00 2001 From: Ben Gardner Date: Wed, 10 Jan 2024 21:25:23 -0600 Subject: [PATCH] save --- src/NetworkViewUi_test.lua | 4 +- src/ServiceEntity.lua | 24 +------- src/auto_player_request.lua | 114 ++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 src/auto_player_request.lua diff --git a/src/NetworkViewUi_test.lua b/src/NetworkViewUi_test.lua index 647d111..e800b96 100644 --- a/src/NetworkViewUi_test.lua +++ b/src/NetworkViewUi_test.lua @@ -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 = {} @@ -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) @@ -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) diff --git a/src/ServiceEntity.lua b/src/ServiceEntity.lua index 1d61782..8d248b7 100644 --- a/src/ServiceEntity.lua +++ b/src/ServiceEntity.lua @@ -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 diff --git a/src/auto_player_request.lua b/src/auto_player_request.lua new file mode 100644 index 0000000..eff0d69 --- /dev/null +++ b/src/auto_player_request.lua @@ -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