Skip to content

Commit

Permalink
belt-analyzer fix: we are presenting the percent of the belt, not the…
Browse files Browse the repository at this point in the history
… percent of an item relative to other items
  • Loading branch information
ahicks92 committed Dec 7, 2024
1 parent 5ed0437 commit abe5691
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion locale/en/ui-belt-analyzer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ ui-belt-analyzer-empty=empty
ui-belt-analyzer-left-lane=Left Lane
ui-belt-analyzer-right-lane=Right Lane

ui-belt-analyzer-aggregation=__1__, __2__ percent
ui-belt-analyzer-aggregation=__1__, __2__ percent of the belt
41 changes: 39 additions & 2 deletions scripts/transport-belts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,29 @@ end
---@class fa.TransportBelts.BeltAnalyzerData
---@field left { upstream: fa.NQC, downstream: fa.NQC, total: fa.NQC }
---@field right { upstream: fa.NQC, downstream: fa.NQC, total: fa.NQC }
---@field upstream_length number in "slots", always a multiple of 4.
---@field downstream_length number
---@field total_length number

---@param ent LuaEntity
---@return number
local function belt_length_in_slots(ent)
-- Underground belt exits are also special: they have 0.5-length lines which are used, and two lines left empty.
if ent.type == "underground-belt" and ent.belt_to_ground_type == "output" then return 2 end

-- Underground belt entrances are where we get the length of the underground part from.
if ent.type == "underground-belt" and ent.belt_to_ground_type == "input" then
local l1 = ent.get_transport_line(1).line_length
local l2 = ent.get_transport_line(3).line_length
return (l1 + l2) * 4
end

if ent.type == "loader" then return 0 end

-- Everything else is 4: transport belts for the obvious reason and splitters
-- because they are complicated and 4 is a good enough approximation.
return 4
end

--[[
Run the belt analyzer algorithm: collect the left and right lane contents for
Expand All @@ -575,6 +598,10 @@ function Node:belt_analyzer_algo()
total = {},
},
right = { upstream = {}, downstream = {}, total = {} },
upstream_length = 0,
downstream_length = 0,
-- There is always at least this belt.
total_length = 0,
}

---@param tab fa.NQC
Expand Down Expand Up @@ -629,13 +656,23 @@ function Node:belt_analyzer_algo()

self:walk_single_parents(function(e)
local n = Node.create(e)
return add_node(n, "upstream")
if add_node(n, "upstream") then
ret.upstream_length = ret.upstream_length + belt_length_in_slots(e)
return true
end
return false
end)

self:walk_single_child(function(e)
return add_node(Node.create(e), "downstream")
if add_node(Node.create(e), "downstream") then
ret.downstream_length = ret.downstream_length + belt_length_in_slots(e)
return true
end
return false
end)

ret.total_length = belt_length_in_slots(self.entity) + ret.upstream_length + ret.downstream_length

return ret
end

Expand Down
23 changes: 13 additions & 10 deletions scripts/ui/belt-analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ local mod = {}
---@field upstream fa.BeltAnalyzer.SortedEntries[]
---@field downstream fa.BeltAnalyzer.SortedEntries[]
---@field total fa.BeltAnalyzer.SortedEntries[]
---@field upstream_length number
---@field downstream_length number
---@field total_length number

---@class fa.ui.BeltAnalyzer.SharedState
---@field node fa.TransportBelts.Node
Expand Down Expand Up @@ -97,7 +100,9 @@ local LocalTab = Grid.declare_grid(LocalGrid)
-- The only difference between the upstream/total/downstream tabs is which
-- field of the analysis they reference, so this returns the grids referencing
-- the proper field. All other logic is 100% identical.
local function aggregate_grid_builder(field)
---@param field "upstream"|"downstream"|"total"
---@param length_field "upstream_length" | "downstream_length" | "total_length"
local function aggregate_grid_builder(field, length_field)
---@class fa.BeltAnalyzer.AggregateTabCallbacks: fa.ui.GridCallbacks
local TabCallbacks = {}

Expand All @@ -119,22 +124,17 @@ local function aggregate_grid_builder(field)
local ent = ctx.shared_state.analysis[field][x][y]
if not ent then return { "fa.ui-belt-analyzer-empty" } end

local total = 0
for i = 1, #ctx.shared_state.analysis[field][x] do
local c = ctx.shared_state.analysis[field][x][i].count
total = total + c
end
local percent = string.format("%.1f", 100 * ent.count / total)
local percent = string.format("%.1f", 100 * ent.count / ctx.shared_state.analysis[length_field])

return { "fa.ui-belt-analyzer-aggregation", Localising.localise_item(ent), percent }
end

return Grid.declare_grid(TabCallbacks)
end

local TotalTab = aggregate_grid_builder("total")
local UpstreamTab = aggregate_grid_builder("upstream")
local DownstreamTab = aggregate_grid_builder("downstream")
local TotalTab = aggregate_grid_builder("total", "total_length")
local UpstreamTab = aggregate_grid_builder("upstream", "upstream_length")
local DownstreamTab = aggregate_grid_builder("downstream", "downstream_length")

---@param params fa.ui.BeltAnalyzer.Parameters
---@return fa.ui.BeltAnalyzer.SharedState
Expand All @@ -154,6 +154,9 @@ local function state_setup(_pindex, params)
upstream = { up_left, up_right },
total = { tot_left, tot_right },
downstream = { down_left, down_right },
upstream_length = ad.upstream_length,
downstream_length = ad.downstream_length,
total_length = ad.total_length,
},
}
end
Expand Down

0 comments on commit abe5691

Please sign in to comment.