diff --git a/src/GlobalState.lua b/src/GlobalState.lua index d1dbe91..d5fc140 100644 --- a/src/GlobalState.lua +++ b/src/GlobalState.lua @@ -9,7 +9,7 @@ local M = {} M.get_default_limit = require("src.DefaultLimits").get_default_limit -local infinite_supply = true +local infinite_supply = false local setup_has_run = false function M.setup() diff --git a/src/NetworkViewUi_test.lua b/src/NetworkViewUi_test.lua index e800b96..0caea2d 100644 --- a/src/NetworkViewUi_test.lua +++ b/src/NetworkViewUi_test.lua @@ -13,6 +13,8 @@ local M = {} M.container_width = 1424 M.container_height = 836 +M.super_debug = false + function M.get_gui(player_index) return GlobalState.get_ui_state(player_index).test_view end @@ -366,6 +368,47 @@ Event.on_event("in_open_test_view", function (event) M.create_gui(event.player_index) end) +local function request_everything(player) + local character = player.character + if character == nil or not player.character_personal_logistic_requests_enabled then + return + end + + local requests = {} + for idx=1, character.request_slot_count do + local rr = character.get_request_slot(idx) + if rr ~= nil then + requests[rr.name] = { count=rr.count, slot=idx } + end + end + + local items = {} + for name, recipe in pairs(player.force.recipes) do + --print(string.format("RECIPE: %s enabled=%s hidden=%s products=%s", recipe.name, recipe.enabled, recipe.hidden, serpent.line(recipe.products))) + if recipe.enabled and not recipe.hidden then + for _, pp in ipairs(recipe.products) do + local prot = game.item_prototypes[pp.name] + if prot ~= nil and items[pp.name] == nil then + items[pp.name] = prot.stack_size + end + end + end + end + + local add_idx = character.request_slot_count + 1 + for item, count in pairs(items) do + local rr = requests[item] + if rr == nil then + player.print(string.format("Added Reqeust for %s in slot %s", item, add_idx)) + character.set_request_slot({name=item, count=count}, add_idx) + add_idx = add_idx + 1 + else + if rr.count > 0 and rr.count < count then + character.set_request_slot({name=item, count=count}, rr.slot) + end + end + end +end Event.on_event("debug-network-item", function (event) --GlobalState.log_queue_info() @@ -380,8 +423,187 @@ Event.on_event("debug-network-item", function (event) if info ~= nil then clog(" - %s", serpent.line(info)) end + if ent.type == "rocket-silo" then + clog("%s recipe=%s allow+copy=%s", ent.name, serpent.line(ent.get_recipe().ingredients), ent.prototype.allow_copy_paste) + end auto_player_request.doit(player) + --request_everything(player) end end) +local function update_player_selected(player) + if player == nil then + return + end + + local info + local ent = player.selected + if ent ~= nil and ent.unit_number ~= nil then + info = GlobalState.entity_info_get(ent.unit_number) + end + + local gname = "MYSUPERTEST" + local parent = player.gui.left + local frame = parent[gname] + if frame ~= nil then + frame.destroy() + end + + if info == nil then + return + end + + -- create the window/frame + frame = parent.add { + type = "frame", + name = gname, + --caption = "This is a test", + style = "quick_bar_window_frame", + --style = "tooltip_heading_label", + --style = "tooltip_generated_from_description_frame", + ignored_by_interaction = true, + } + + -- create the main vertical flow + local vflow = frame.add { + type = "flow", + direction = "vertical", + } + + -- add the header + local hdr_frame = vflow.add { + type = "frame", + name = gname, + style = "tooltip_title_frame_light", + ignored_by_interaction = true, + } + + hdr_frame.add { + type="label", + name="MYSUPERTEST-text", + caption = ent.localised_name, + style = "tooltip_heading_label", + ignored_by_interaction = true, + } + + -- start the description area + local desc_flow = vflow.add { + type = "flow", + direction = "vertical", + } + + --[[ + vflow.add { + type="label", + name="MYSUPERTEST-text", + caption = "This is a test", + --style = "tooltip_heading_title", + --style = "tooltip_title_label", + style = "tooltip_heading_label", + ignored_by_interaction = true, + } + ]] + --[[ + flow = vflow.add { + type = "flow", + name="MYSUPERTEST-flow", + direction = "vertical", + --style = "tooltip_panel_background", + } + ]] + if M.super_debug == true then + -- debug: log info + local xi = {} + for k, v in pairs(info) do + if k ~= "entity" then + xi[k] = v + end + end + desc_flow.add { + type="label", + caption = serpent.line(xi), + ignored_by_interaction = true, + } + else + desc_flow.add { + type="label", + caption = string.format("unit_number: %s", info.unit_number), + ignored_by_interaction = true, + } + end + if info.service_type ~= nil then + desc_flow.add { + type="label", + caption = string.format("Service type: %s", info.service_type), + ignored_by_interaction = true, + } + end + if info.service_priority ~= nil then + desc_flow.add { + type="label", + caption = string.format("Service priority: %s", info.service_priority), + ignored_by_interaction = true, + } + end + if info.service_tick_delta ~= nil then + desc_flow.add { + type="label", + caption = string.format("Service period: %.0f seconds", info.service_tick_delta / 60), + ignored_by_interaction = true, + } + end + if info.service_tick ~= nil then + desc_flow.add { + type="label", + caption = string.format("Service tick: %s", info.service_tick), + ignored_by_interaction = true, + } + end + if info.ore_name ~= nil then + --[[ + local hflow = desc_flow.add { + type="flow", + ignored_by_interaction = true, + } + hflow.add { + type="label", + caption = string.format("Ore: %s", info.ore_name), + ignored_by_interaction = true, + } + ]] + desc_flow.add { + type = "sprite-button", + sprite = "item/" .. info.ore_name, + } +end + if info.requests ~= nil then + local take_hflow = desc_flow.add { + type="flow", + ignored_by_interaction = true, + } + for _, r in ipairs(info.requests) do + if r.type == "take" then + take_hflow.add { + type = "sprite-button", + sprite = "item/" .. r.item, + number = r.buffer or 0, + } + end + end + end + --string.format("[%s] %s", ent.unit_number, ent.localised_name) +end + +local function on_selected_entity_changed(event) + update_player_selected(game.get_player(event.player_index)) +end +Event.on_event(defines.events.on_selected_entity_changed, on_selected_entity_changed) + +local function update_all_players_selected() + for _, player in pairs(game.players) do + update_player_selected(player) + end +end +Event.on_nth_tick(60, update_all_players_selected) + return M diff --git a/src/auto_player_request.lua b/src/auto_player_request.lua index eff0d69..d802ca6 100644 --- a/src/auto_player_request.lua +++ b/src/auto_player_request.lua @@ -5,16 +5,64 @@ local Event = require('__stdlib__/stdlib/event/event') local item_utils = require("src.item_utils") local clog = require("src.log_console").log +local function sort_character_requests(player, character, items) + local requests = {} + for idx=1, character.request_slot_count do + local ls = character.get_personal_logistic_slot(idx) + if ls ~= nil and ls.name ~= nil then + table.insert(requests, { slot=idx, item=ls.name, min=ls.min, max=ls.max }) + end + end + + table.sort(requests, item_utils.entry_compare_items) + requests = item_utils.entry_list_split_by_group(requests) + + local add_idx = 1 + for _, rr in ipairs(requests) do + if rr == '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_personal_logistic_slot(idx) + end + else + -- request exists -- do not touch if in the same slot + if rr.slot ~= add_idx then + if rr.slot > add_idx then + character.clear_personal_logistic_slot(rr.slot) + end + character.clear_personal_logistic_slot(add_idx) + character.set_personal_logistic_slot(add_idx, { name=rr.item, min=rr.min, max=rr.max }) + else + -- same index (no change) + --character.clear_personal_logistic_slot(add_idx) + --character.set_personal_logistic_slot(add_idx, { name=rr.item, min=rr.min, max=rr.max }) + end + add_idx = add_idx + 1 + end + end + for idx=add_idx, character.request_slot_count do + character.clear_personal_logistic_slot(idx) + end +end + local function set_character_requests(player, character, items) local requests = {} + for name, count in pairs(items) do + requests[name] = { slot=0, name=name, min=count, max=count } + end for idx=1, character.request_slot_count do - local rr = character.get_request_slot(idx) - if rr ~= nil then + local ls = character.get_personal_logistic_slot(idx) + if ls ~= nil and ls.name ~= nil then --print(string.format("old [%s] = %s", idx, serpent.line(rr))) - requests[rr.name] = { count=rr.count, slot=idx } + requests[ls.name] = { slot=idx, name=ls.name, min=ls.min, max=ls.max } -- fields: name, min, max end end + table.sort(items, item_utils.entry_compare_items) + + local thelist = item_utils.entry_list_split_by_group(items) + --print("\nitems:") --print(serpent.block(items)) @@ -26,30 +74,37 @@ local function set_character_requests(player, character, items) 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) + character.clear_personal_logistic_slot(idx) end else local item = info.item local count = info.count local rr = requests[item] if rr ~= nil then + requests[item] = nil -- 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) + character.clear_personal_logistic_slot(rr.slot) end - character.clear_request_slot(add_idx) - character.set_request_slot({ name=item, count=math.max(1, rr.count) }, add_idx) + character.clear_personal_logistic_slot(add_idx) + character.set_personal_logistic_slot(add_idx, { name=item, min=rr.min, max=rr.max }) end else - character.clear_request_slot(add_idx) - character.set_request_slot({name=item, count=count}, add_idx) + character.clear_personal_logistic_slot(add_idx) + character.set_personal_logistic_slot(add_idx, { name=item, min=count, max=count }) end add_idx = add_idx + 1 end end + for _, rr in pairs(requests) do + print(string.format("ADD: %s @ %s", serpent.line(rr), add_idx)) + character.clear_personal_logistic_slot(add_idx) + character.set_personal_logistic_slot(add_idx, { name=rr.name, min=rr.min, max=rr.max }) + add_idx = add_idx + 1 + end for idx=add_idx, character.request_slot_count do - character.clear_request_slot(idx) + character.clear_personal_logistic_slot(idx) end end @@ -64,7 +119,7 @@ local function force_request_everything(force) 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)) + --print(string.format(" -- Added %s due to %s [%s]", name, recipe.name, what)) end end end @@ -74,7 +129,7 @@ local function force_request_everything(force) 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 + --[[ this gets too many items, especially if there is a 'scrap' recipea for _, pp in ipairs(recipe.ingredients) do if pp.type == "item" then add_item(pp.name, recipe, 'ingredients') @@ -85,12 +140,13 @@ local function force_request_everything(force) end table.sort(items, item_utils.entry_compare_items) - items = item_utils.entry_list_split_by_group(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) + --set_character_requests(player, character, items) + sort_character_requests(player, character, items) end end end