BEHOLD, the magical duplicating inventory

This commit is contained in:
Will Berry
2023-03-05 23:07:13 -05:00
parent f8756efbae
commit 7e745232bc
3 changed files with 193 additions and 46 deletions

View File

@@ -1,12 +1,14 @@
local misc = require("__flib__.misc")
local gui = require("__flib__.gui-lite")
local templates = require("templates")
local templates = require("scripts.gui.templates")
local misc = require("__flib__.misc")
local inventory_tab = {}
function inventory_tab.build()
function inventory_tab.create()
return {
tab = {
name = "manager_inventory_tab",
type = "tab",
caption = { "gui.ltnm-inventory" },
ref = { "inventory", "tab" },
@@ -15,6 +17,7 @@ function inventory_tab.build()
},
},
content = {
name = "manager_inventory_content_frame",
type = "flow",
style_mods = { horizontal_spacing = 12 },
direction = "horizontal",
@@ -26,62 +29,204 @@ function inventory_tab.build()
}
end
local function update_table(self, name, color)
local translations = self.player_table.dictionaries.materials
---@param map_data MapData
---@param player_data PlayerData
function inventory_tab.build(map_data, player_data)
local state = self.state
local refs = self.refs.inventory
local refs = player_data.refs
local search_query = state.search_query
local search_network_id = state.network_id
local search_surface = state.surface
local search_item = player_data.search_item
local search_network_name = player_data.search_network_name
local search_network_mask = player_data.search_network_mask
local search_surface_idx = player_data.search_surface_idx
local inventory_provided = {}
local inventory_in_transit = {}
local inventory_requested = {}
local ltn_inventory = state.ltn_data.inventory[name][search_surface]
local stations_sorted = {}
local i = 0
for id, station in pairs(map_data.stations) do
local entity = station.entity_stop
if not entity.valid then
goto continue
end
local table = refs[name].table
local children = table.children
if search_network_name then
if search_network_name ~= station.network_name then
goto continue
end
local train_flag = get_network_flag(station, search_network_name)
if not bit32.btest(search_network_mask, train_flag) then
goto continue
end
elseif search_network_mask ~= -1 then
if station.network_name == NETWORK_EACH then
local masks = station.network_flag--[[@as {}]]
for _, network_flag in pairs(masks) do
if bit32.btest(search_network_mask, network_flag) then
goto has_match
end
end
goto continue
::has_match::
elseif not bit32.btest(search_network_mask, station.network_flag) then
goto continue
end
end
for name, count_by_network_id in pairs(ltn_inventory or {}) do
if
bit32.btest(count_by_network_id.combined_id, search_network_id)
and string.find(string.lower(translations[name]), string.lower(search_query))
then
local running_count = 0
for network_id, count in pairs(count_by_network_id) do
if network_id ~= "combined_id" and bit32.btest(network_id, search_network_id) then
running_count = running_count + count
if search_surface_idx then
if entity.surface.index ~= search_surface_idx then
goto continue
end
end
if search_item then
if not station.deliveries then
goto continue
end
for item_name, _ in pairs(station.deliveries) do
if item_name == search_item then
goto has_match
end
end
goto continue
::has_match::
end
stations_sorted[#stations_sorted + 1] = id
::continue::
end
for i, station_id in pairs(stations_sorted) do
--- @class Station
local station = map_data.stations[station_id]
local comb1_signals, _ = get_signals(station)
if comb1_signals then
for _, v in pairs(comb1_signals) do
local item = v.signal
local count = v.count
if item.type ~= "virtual" then
if count > 0 then
if inventory_provided[item.name] == nil then
inventory_provided[item.name] = count
else
inventory_provided[item.name] = inventory_provided[item.name] + count
end
else
if inventory_requested[item.name] == nil then
inventory_requested[item.name] = count
else
inventory_requested[item.name] = inventory_requested[item.name] + count
end
end
end
end
end
if running_count > 0 then
i = i + 1
local button = children[i]
if not button then
button = table.add({ type = "sprite-button", style = "flib_slot_button_" .. color, enabled = false })
local deliveries = station.deliveries
if deliveries then
for item, count in pairs(deliveries) do
if count > 0 then
if inventory_in_transit[item] == nil then
inventory_in_transit[item] = 0
else
inventory_in_transit[item] = inventory_in_transit[item] + count
end
end
button.sprite = string.gsub(name, ",", "/")
button.number = running_count
button.tooltip = "[img="
.. string.gsub(name, ",", "/")
.. "] [font=default-semibold]"
.. translations[name]
.. "[/font]\n"
.. misc.delineate_number(running_count)
end
end
end
for j = i + 1, #children do
children[j].destroy()
end
end
local provided_children = {}
local i = 0
for item, count in pairs(inventory_provided) do
i = i + 1
local sprite = ""
if game.is_valid_sprite_path("item/" .. item) then
sprite = "item/" .. item
elseif game.is_valid_sprite_path("fluid/" .. item) then
sprite = "fluid/" .. item
end
if game.is_valid_sprite_path(sprite) then
provided_children[#provided_children+1] = {
type = "sprite-button",
style = "flib_slot_button_green",
enabled = false,
sprite = sprite,
number = count,
tooltip = "[img="
.. string.gsub(item, ",", "/")
.. "] [font=default-semibold]"
.. item
.. "[/font]\n"
.. misc.delineate_number(count)
}
end
end
local requested_children = {}
local i = 0
for item, count in pairs(inventory_requested) do
i = i + 1
local sprite = ""
if game.is_valid_sprite_path("item/" .. item) then
sprite = "item/" .. item
elseif game.is_valid_sprite_path("fluid/" .. item) then
sprite = "fluid/" .. item
end
if game.is_valid_sprite_path(sprite) then
requested_children[#requested_children+1] = {
type = "sprite-button",
style = "flib_slot_button_red",
enabled = false,
sprite = sprite,
number = count,
tooltip = "[img="
.. string.gsub(item, ",", "/")
.. "] [font=default-semibold]"
.. item
.. "[/font]\n"
.. misc.delineate_number(count)
}
end
end
local in_transit_children = {}
local i = 0
for item, count in pairs(inventory_in_transit) do
i = i + 1
local sprite = ""
if game.is_valid_sprite_path("item/" .. item) then
sprite = "item/" .. item
elseif game.is_valid_sprite_path("fluid/" .. item) then
sprite = "fluid/" .. item
end
if game.is_valid_sprite_path(sprite) then
in_transit_children[#in_transit_children+1] = {
type = "sprite-button",
style = "flib_slot_button_blue",
enabled = false,
sprite = sprite,
number = count,
tooltip = "[img="
.. string.gsub(item, ",", "/")
.. "] [font=default-semibold]"
.. item
.. "[/font]\n"
.. misc.delineate_number(count)
}
end
end
gui.add(refs.inventory_provided_table, provided_children)
gui.add(refs.inventory_requested_table, requested_children)
gui.add(refs.inventory_in_transit_table, in_transit_children)
function inventory_tab.update(self)
update_table(self, "provided", "green")
update_table(self, "in_transit", "blue")
update_table(self, "requested", "red")
end
return inventory_tab

View File

@@ -8,7 +8,7 @@ local templates = require("scripts.gui.templates")
local stations_tab = require("scripts.gui.stations")
--local trains_tab = require("scripts.gui.trains")
--local depots_tab = require("scripts.gui.depots")
--local inventory_tab = require("scripts.gui.inventory")
local inventory_tab = require("scripts.gui.inventory")
--local history_tab = require("scripts.gui.history")
--local alerts_tab = require("scripts.gui.alerts")
@@ -93,6 +93,7 @@ function manager.create(player)
type = "tabbed-pane",
style = "ltnm_tabbed_pane",
stations_tab.create(widths),
inventory_tab.create(),
selected_tab_index = 1,
},
},
@@ -113,6 +114,7 @@ end
--- @param player_data PlayerData
function manager.update(map_data, player_data)
stations_tab.build(map_data, player_data)
inventory_tab.build(map_data, player_data)
end

View File

@@ -43,7 +43,7 @@ function templates.inventory_slot_table(name, columns)
vertical_scroll_policy = "auto-and-reserve-space",
-- vertical_scroll_policy = "always",
ref = { "inventory", name, "scroll_pane" },
{ type = "table", style = "slot_table", column_count = columns, ref = { "inventory", name, "table" } },
{ type = "table", name = "inventory_" .. name .. "_table", style = "slot_table", column_count = columns, ref = { "inventory", name, "table" } },
},
},
}