Skip to content

Commit

Permalink
Rewrite the belt analyzer, and all the stuff that came up along the way
Browse files Browse the repository at this point in the history
This is not clean history; I don't have the inclination to split at the moment. THis introduces
a small UI framework thing, makers a bunch of small fixes, then uses all of this to rewrite the belt analyzer.

The punchline as it were is scripts/ui/belt-analyzer.lua
  • Loading branch information
ahicks92 committed Dec 3, 2024
1 parent f0274af commit dea1fbb
Show file tree
Hide file tree
Showing 15 changed files with 830 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
}
],
"Lua.workspace.userThirdParty": [
"c:\\Users\\Austin\\AppData\\Roaming\\Code\\User\\workspaceStorage\\5232b5213d87a49c56164541939376a4\\justarandomgeek.factoriomod-debug\\sumneko-3rd"
"c:\\Users\\Austin\\AppData\\Roaming\\Code\\User\\workspaceStorage\\63b7677c3b16e179985a791a1818f333\\justarandomgeek.factoriomod-debug\\sumneko-3rd"
]
}
137 changes: 7 additions & 130 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local fa_warnings = require("scripts.warnings")
local fa_circuits = require("scripts.circuit-networks")
local fa_kk = require("scripts.kruise-kontrol-wrapper")
local fa_quickbar = require("scripts.quickbar")
local BeltAnalyzer = require("scripts.ui.belt-analyzer")
local FaCommands = require("scripts.fa-commands")
local Consts = require("scripts.consts")
local Research = require("scripts.research")
Expand Down Expand Up @@ -1589,49 +1590,7 @@ function menu_cursor_up(pindex)
elseif players[pindex].menu == "technology" then
Research.menu_move_vertical(pindex, -1)
elseif players[pindex].menu == "belt" then
if players[pindex].belt.sector == 1 then
if
(players[pindex].belt.side == 1 and players[pindex].belt.line1.valid and players[pindex].belt.index > 1)
or (players[pindex].belt.side == 2 and players[pindex].belt.line2.valid and players[pindex].belt.index > 1)
then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = players[pindex].belt.index - 1
end
elseif players[pindex].belt.sector == 2 then
local max = 0
if players[pindex].belt.side == 1 then
max = #players[pindex].belt.network.combined.left
elseif players[pindex].belt.side == 2 then
max = #players[pindex].belt.network.combined.right
end
if players[pindex].belt.index > 1 then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = math.min(players[pindex].belt.index - 1, max)
end
elseif players[pindex].belt.sector == 3 then
local max = 0
if players[pindex].belt.side == 1 then
max = #players[pindex].belt.network.downstream.left
elseif players[pindex].belt.side == 2 then
max = #players[pindex].belt.network.downstream.right
end
if players[pindex].belt.index > 1 then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = math.min(players[pindex].belt.index - 1, max)
end
elseif players[pindex].belt.sector == 4 then
local max = 0
if players[pindex].belt.side == 1 then
max = #players[pindex].belt.network.upstream.left
elseif players[pindex].belt.side == 2 then
max = #players[pindex].belt.network.upstream.right
end
if players[pindex].belt.index > 1 then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = math.min(players[pindex].belt.index - 1, max)
end
end
fa_belts.read_belt_slot(pindex)
BeltAnalyzer.belt_analyzer:on_up(pindex)
elseif players[pindex].menu == "warnings" then
if players[pindex].warnings.category > 1 then
players[pindex].warnings.category = players[pindex].warnings.category - 1
Expand Down Expand Up @@ -1835,49 +1794,7 @@ function menu_cursor_down(pindex)
elseif players[pindex].menu == "technology" then
Research.menu_move_vertical(pindex, 1)
elseif players[pindex].menu == "belt" then
if players[pindex].belt.sector == 1 then
if
(players[pindex].belt.side == 1 and players[pindex].belt.line1.valid and players[pindex].belt.index < 4)
or (players[pindex].belt.side == 2 and players[pindex].belt.line2.valid and players[pindex].belt.index < 4)
then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = players[pindex].belt.index + 1
end
elseif players[pindex].belt.sector == 2 then
local max = 0
if players[pindex].belt.side == 1 then
max = #players[pindex].belt.network.combined.left
elseif players[pindex].belt.side == 2 then
max = #players[pindex].belt.network.combined.right
end
if players[pindex].belt.index < max then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = math.min(players[pindex].belt.index + 1, max)
end
elseif players[pindex].belt.sector == 3 then
local max = 0
if players[pindex].belt.side == 1 then
max = #players[pindex].belt.network.downstream.left
elseif players[pindex].belt.side == 2 then
max = #players[pindex].belt.network.downstream.right
end
if players[pindex].belt.index < max then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = math.min(players[pindex].belt.index + 1, max)
end
elseif players[pindex].belt.sector == 4 then
local max = 0
if players[pindex].belt.side == 1 then
max = #players[pindex].belt.network.upstream.left
elseif players[pindex].belt.side == 2 then
max = #players[pindex].belt.network.upstream.right
end
if players[pindex].belt.index < max then
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
players[pindex].belt.index = math.min(players[pindex].belt.index + 1, max)
end
end
fa_belts.read_belt_slot(pindex)
BeltAnalyzer.belt_analyzer:on_down(pindex)
elseif players[pindex].menu == "warnings" then
local warnings = {}
if players[pindex].warnings.sector == 1 then
Expand Down Expand Up @@ -2071,15 +1988,7 @@ function menu_cursor_left(pindex)
elseif players[pindex].menu == "technology" then
Research.menu_move_horizontal(pindex, -1)
elseif players[pindex].menu == "belt" then
if players[pindex].belt.side == 2 then
players[pindex].belt.side = 1
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
if not pcall(function()
fa_belts.read_belt_slot(pindex)
end) then
printout("Blank", pindex)
end
end
BeltAnalyzer.belt_analyzer:on_left(pindex)
elseif players[pindex].menu == "warnings" then
if players[pindex].warnings.index > 1 then
players[pindex].warnings.index = players[pindex].warnings.index - 1
Expand Down Expand Up @@ -2216,15 +2125,7 @@ function menu_cursor_right(pindex)
elseif players[pindex].menu == "technology" then
Research.menu_move_horizontal(pindex, 1)
elseif players[pindex].menu == "belt" then
if players[pindex].belt.side == 1 then
players[pindex].belt.side = 2
game.get_player(pindex).play_sound({ path = "Inventory-Move" })
if not pcall(function()
fa_belts.read_belt_slot(pindex)
end) then
printout("Blank", pindex)
end
end
BeltAnalyzer.belt_analyzer:on_right(pindex)
elseif players[pindex].menu == "warnings" then
local warnings = {}
if players[pindex].warnings.sector == 1 then
Expand Down Expand Up @@ -3691,19 +3592,7 @@ script.on_event("switch-menu-or-gun", function(event)
players[pindex].menu = "inventory"
read_inventory_slot(pindex, "Inventory, ")
elseif players[pindex].menu == "belt" then
players[pindex].belt.index = 1
players[pindex].belt.sector = players[pindex].belt.sector + 1
if players[pindex].belt.sector == 5 then players[pindex].belt.sector = 1 end
local sector = players[pindex].belt.sector
if sector == 1 then
printout("Local Lanes", pindex)
elseif sector == 2 then
printout("Total Lanes", pindex)
elseif sector == 3 then
printout("Downstream lanes", pindex)
elseif sector == 4 then
printout("Upstream Lanes", pindex)
end
BeltAnalyzer.belt_analyzer:on_next_tab(pindex)
elseif players[pindex].menu == "warnings" then
players[pindex].warnings.sector = players[pindex].warnings.sector + 1
if players[pindex].warnings.sector > 3 then players[pindex].warnings.sector = 1 end
Expand Down Expand Up @@ -3839,19 +3728,7 @@ script.on_event("reverse-switch-menu-or-gun", function(event)
players[pindex].menu = "inventory"
read_inventory_slot(pindex, "Inventory, ")
elseif players[pindex].menu == "belt" then
players[pindex].belt.index = 1
players[pindex].belt.sector = players[pindex].belt.sector - 1
if players[pindex].belt.sector == 0 then players[pindex].belt.sector = 4 end
local sector = players[pindex].belt.sector
if sector == 1 then
printout("Local Lanes", pindex)
elseif sector == 2 then
printout("Total Lanes", pindex)
elseif sector == 3 then
printout("Downstream lanes", pindex)
elseif sector == 4 then
printout("Upstream Lanes", pindex)
end
BeltAnalyzer.belt_analyzer:on_previous_tab(pindex)
elseif players[pindex].menu == "warnings" then
players[pindex].warnings.sector = players[pindex].warnings.sector - 1
if players[pindex].warnings.sector < 1 then players[pindex].warnings.sector = 3 end
Expand Down
17 changes: 17 additions & 0 deletions locale/en/ui-belt-analyzer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[fa]
# Titles for the tabs.
ui-belt-analyzer-tab-local=This Belt
ui-belt-analyzer-tab-total=Total Lanes
ui-belt-analyzer-tab-upstream=Upstream Lanes
ui-belt-analyzer-tab-downstream=Downstream Lanes

ui-belt-analyzer-lane=__1__ lane
# Up to 4, for each side of a belt.
ui-belt-analyzer-slot=__plural_for_parameter__1__{1=first|2=second|3=third|4=fourth}__ slot
ui-belt-analyzer-empty=empty

# For the total tabs, we don't have lane directions.
ui-belt-analyzer-left-lane=Left Lane
ui-belt-analyzer-right-lane=Right Lane

ui-belt-analyzer-aggregation=__1__, __2__ percent
4 changes: 4 additions & 0 deletions locale/en/ui-grid.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[fa]
ui-grid-empty=Empty grid
# We expect the child callbacks to provide "at", ""on", etc.
ui-grid-cell-with-pos=__1__ __2__
14 changes: 14 additions & 0 deletions math-helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@ function mod.mod1(index, length)
return ((index - 1) % length) + 1
end

---@param x number
---@param low number
---@param high number
---@return number
function mod.clamp(x, low, high)
if x < low then
return low
elseif x > high then
return high
else
return x
end
end

return mod
4 changes: 2 additions & 2 deletions scripts/building-vehicle-sectors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local fa_crafting = require("scripts.crafting")
local localising = require("scripts.localising")
local fa_belts = require("scripts.transport-belts")
local fa_blueprints = require("scripts.blueprints")
local BeltAnalyzer = require("scripts.ui.belt-analyzer")

local mod = {}

Expand Down Expand Up @@ -91,8 +92,7 @@ function mod.open_operable_building(ent, pindex)
players[pindex].menu_search_index = 0
players[pindex].menu_search_index_2 = 0
if ent.prototype.subgroup.name == "belt" then
players[pindex].in_menu = true
players[pindex].menu = "belt"
BeltAnalyzer.belt_analyzer:open(pindex, { entity = ent })
return
end
if ent.prototype.ingredient_count ~= nil then
Expand Down
2 changes: 1 addition & 1 deletion scripts/localising.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function mod.localise_item_or_fluid(what, protos)

local item_str = mod.get_localised_name_with_fallback(item_proto)
local quality_str = mod.get_localised_name_with_fallback(quality_proto)
local has_quality = (quality and quality.name ~= "normal") and 1 or 0
local has_quality = (quality and quality_proto.name ~= "normal") and 1 or 0
local has_count = count and 1 or 0

return { "fa.item-quantity-quality", item_str, has_quality, quality_str, has_count, count }
Expand Down
4 changes: 3 additions & 1 deletion scripts/message-builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,13 @@ function MessageBuilder:list_item(fragment)
end

-- It is an error to keep using this builder after building.
---@return LocalisedString Empty string if nothing was ever done.
---@return LocalisedString? Nil if nothing was done.
function MessageBuilder:build()
self:_check_not_built()
self.state = MESSAGE_BUILDER_STATE.BUILT

if not next(self.parts) then return nil end

return FaUtils.localise_cat_table(self.parts, "")
end

Expand Down
26 changes: 25 additions & 1 deletion scripts/table-helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ hit, but it's usually only a couple levels and for a function, which means in
context that's not too bad (plus, anything truly performance sensitive will
cache in a local anyway). See e.g. ds.work_queue, scanner.backends.simple.
Also, this is simpler Lua inheritance: list the most derived class first.
At least one table must be specified.
]]
function mod.nested_indexer(...)
Expand All @@ -154,7 +156,7 @@ function mod.nested_indexer(...)
local c = cache[key]
if c then return c end

for i = #args, 1, -1 do
for i = 1, #args do
local attempt = args[i][key]
if attempt then
cache[key] = attempt
Expand Down Expand Up @@ -288,4 +290,26 @@ function mod.max_counts2_tiebreak_quality(name1, qual1, name2, qual2)
return lev1 == lev2 and name1 < name2
end

--[[
When doing announcements we usually need to go from NQC to sorted data in
descending order. This does that, in a form that can be directly passed to
localisation.
]]
---@param tab fa.NQC
---@return ({ name: string, quality: string, count: number})[]
function mod.nqc_to_sorted_descending(tab)
local ret = {}

for n, quals in pairs(tab) do
for qual, c in pairs(quals) do
table.insert(ret, { name = n, quality = qual, count = c })
end
end

table.sort(ret, function(a, b)
return a.count > b.count
end)
return ret
end

return mod
Loading

0 comments on commit dea1fbb

Please sign in to comment.