Files
fs25-adsmartpickup/FS25_ADSmartPickup/gui/SmartPickupStorageTab.lua
T

203 lines
8.9 KiB
Lua

-- Smart Pickup — fanen Lagre
-- Alt gården har liggende, pr. bygning: fabrikker (outputs først), stalde, palleskure, siloer.
-- Kun bygninger med indhold vises; teksterne opdateres hvert sekund uden genopbygning.
-- Funktionerne lægges på SmartPickupFrame (indlæses efter SmartPickupFrame.lua).
SmartPickupFrame.GRID_CELLS = 3
-- Én celle i gitteret: "*Ensilage 1.7M/20.0M" eller "Kurve 12 paller" (* = fabrikkens output).
function SmartPickupFrame:describeInventoryItem(item)
local amount
if item.count ~= nil then
amount = string.format(self.i18n:getText("spu_storage_pallets"), item.count)
else
amount = string.format("%s/%s", SmartPickupFrame.formatCompact(item.level or 0), SmartPickupFrame.formatCompact(item.capacity or 0))
end
return string.format("%s%s %s", item.isOutput and "*" or "", tostring(item.title), amount)
end
-- Gitterlinjer for en bygning: {{navn, celle, celle, celle}, {"", celle, ...}, ...}.
function SmartPickupFrame:inventoryLines(entry)
local cells = {}
for _, item in ipairs(entry.entries) do table.insert(cells, self:describeInventoryItem(item)) end
local kindText = self.i18n:getText("spu_source_kind_" .. tostring(entry.kind))
local lines = {}
for index, chunk in ipairs(ADStorageStats.chunk(cells, SmartPickupFrame.GRID_CELLS)) do
local line = {index == 1 and string.format("%s (%s)", tostring(entry.name), kindText) or ""}
for cell = 1, SmartPickupFrame.GRID_CELLS do line[cell + 1] = chunk[cell] or "" end
table.insert(lines, line)
end
return lines
end
-- Kloner en tabelrække og sætter cellerne c1..cN; returnerer celle-elementerne.
function SmartPickupFrame:addCellRow(prefab, texts)
local row = self:addRow(prefab, {kind = "text"})
local cells = {}
for index = 1, #texts do
cells[index] = row:getDescendantByName("c" .. index)
end
SmartPickupFrame.setCells(cells, texts)
return cells
end
function SmartPickupFrame.setCells(cells, texts)
for index, cell in ipairs(cells) do
if cell ~= nil then cell:setText(tostring(texts[index] or "")) end
end
end
function SmartPickupFrame:getInventory(adEnv)
if ADSources == nil or ADSources.getFarmInventory == nil then
return {}
end
return ADSources.getFarmInventory(adEnv, g_currentMission:getFarmId())
end
-- ---- detaljer for den valgte bygning ----
function SmartPickupFrame.formatMoney(value)
if value == nil then return "-" end
if g_i18n ~= nil and g_i18n.formatMoney ~= nil then
local ok, text = pcall(g_i18n.formatMoney, g_i18n, value, 0, true, true)
if ok and text ~= nil then return text end
end
return string.format("%d", math.floor(value + 0.5))
end
function SmartPickupFrame:fillTypeTitle(fillType)
local title = g_fillTypeManager.getFillTypeTitleByIndex ~= nil and g_fillTypeManager:getFillTypeTitleByIndex(fillType) or nil
return tostring(title or g_fillTypeManager:getFillTypeNameByIndex(fillType))
end
-- Beregner stats for alle varer i bygningen (gårdens totaler og rater regnes én gang pr. opdatering).
function SmartPickupFrame:computeBuildingStats(adEnv, entry, inventory)
local farmId = g_currentMission:getFarmId()
local totals = ADStorageStats.farmTotals(inventory)
local rates = ADSources.getFarmRates ~= nil and ADSources.getFarmRates(adEnv, farmId) or {}
local stats = {}
for _, item in ipairs(entry.entries) do
local quote = ADSources.getMarketQuote ~= nil and ADSources.getMarketQuote(item.fillType) or nil
table.insert(stats, ADStorageStats.itemStats(item, totals[item.fillType], rates[item.fillType], quote))
end
return stats
end
-- Tabelkolonner: vare · i bygningen · værdi nu · ved toppris · gården · rækker til · forbrug/prod. pr. døgn
function SmartPickupFrame:statHeader()
local i18n = self.i18n
return {i18n:getText("spu_col_item"), i18n:getText("spu_col_here"), i18n:getText("spu_col_now"), i18n:getText("spu_col_top"),
i18n:getText("spu_col_farm"), i18n:getText("spu_col_lasts"), i18n:getText("spu_col_rate")}
end
function SmartPickupFrame:statCells(stat)
local i18n = self.i18n
local fc = SmartPickupFrame.formatCompact
local here = string.format("%s/%s", fc(stat.level), fc(stat.capacity))
if stat.percent ~= nil then here = here .. string.format(" %d %%", stat.percent) end
local now, top = i18n:getText("spu_stat_noSell"), ""
if stat.valueNow ~= nil then
now, top = SmartPickupFrame.formatMoney(stat.valueNow), SmartPickupFrame.formatMoney(stat.valueTop)
end
local days = stat.days ~= nil and string.format(i18n:getText("spu_stat_days"), stat.days) or "-"
return {self:fillTypeTitle(stat.fillType), here, now, top, fc(stat.farmTotal), days, ADStorageStats.rateText(stat.consumption, stat.production, fc)}
end
function SmartPickupFrame:statTotalCells(stats)
local totals = ADStorageStats.buildingTotals(stats)
return {self.i18n:getText("spu_stat_total"), "", SmartPickupFrame.formatMoney(totals.valueNow), SmartPickupFrame.formatMoney(totals.valueTop), "", "", ""}
end
-- Navne til vælgeren; ens navne (40 Farma-siloer) får løbenummer.
function SmartPickupFrame:storageLabels(inventory)
local counts, seen, labels = {}, {}, {}
for _, entry in ipairs(inventory) do counts[entry.name] = (counts[entry.name] or 0) + 1 end
for index, entry in ipairs(inventory) do
seen[entry.name] = (seen[entry.name] or 0) + 1
labels[index] = counts[entry.name] > 1 and string.format("%s #%d", entry.name, seen[entry.name]) or entry.name
end
return labels
end
function SmartPickupFrame:fillStorageDetail(adEnv, inventory)
self.storageDetailRows = {}
if #inventory == 0 then
return
end
self.storageIndex = math.max(1, math.min(self.storageIndex or 1, #inventory))
local row = self:addRow(self.productPrefab, {kind = "storageBuilding"})
local option = row:getDescendantByName("option")
option:setTexts(self:storageLabels(inventory))
option:setState(self.storageIndex, false)
row:getDescendantByName("title"):setText(self.i18n:getText("spu_storage_select"))
local entry = inventory[self.storageIndex]
self.storageDetailPlaceable = entry.placeable
local tooltip = option:getDescendantByName("ignore")
local tooltipText = self.i18n:getText("spu_storage_select_tooltip")
if tooltip ~= nil then tooltip:setText(tooltipText) end
option.toolTipText = tooltipText
local stats = self:computeBuildingStats(adEnv, entry, inventory)
self:addCellRow(self.statPrefab, self:statHeader())
for _, stat in ipairs(stats) do
table.insert(self.storageDetailRows, self:addCellRow(self.statPrefab, self:statCells(stat)))
end
table.insert(self.storageDetailRows, self:addCellRow(self.statPrefab, self:statTotalCells(stats)))
end
function SmartPickupFrame:refreshStorageDetail(adEnv, inventory)
local entry = nil
for _, candidate in ipairs(inventory) do
if candidate.placeable == self.storageDetailPlaceable then entry = candidate end
end
if entry == nil or #(self.storageDetailRows or {}) ~= #entry.entries + 1 then
return
end
local stats = self:computeBuildingStats(adEnv, entry, inventory)
for index, stat in ipairs(stats) do
SmartPickupFrame.setCells(self.storageDetailRows[index], self:statCells(stat))
end
SmartPickupFrame.setCells(self.storageDetailRows[#stats + 1], self:statTotalCells(stats))
end
function SmartPickupFrame:onClickStorageBuilding(state)
self.storageIndex = state
self:requestRebuild()
end
function SmartPickupFrame:fillStorageRows(adEnv)
self.storageRows = {}
local inventory = self:getInventory(adEnv)
self:fillStorageDetail(adEnv, inventory)
self:addTextRow(self.i18n:getText("spu_section_storage"))
for _, entry in ipairs(inventory) do
local rows = {}
for _, line in ipairs(self:inventoryLines(entry)) do
table.insert(rows, self:addCellRow(self.gridPrefab, line))
end
table.insert(self.storageRows, {placeable = entry.placeable, rows = rows})
end
if #inventory == 0 then
self:addTextRow(self.i18n:getText("spu_storage_empty"))
end
end
function SmartPickupFrame:refreshStorageRows(adEnv)
if self.currentTab ~= SmartPickupFrame.TAB_STORAGE or #(self.storageRows or {}) == 0 then
return
end
local inventory = self:getInventory(adEnv)
local byPlaceable = {}
for _, entry in ipairs(inventory) do
byPlaceable[entry.placeable] = entry
end
pcall(self.refreshStorageDetail, self, adEnv, inventory)
for _, row in ipairs(self.storageRows) do
local entry = byPlaceable[row.placeable]
local lines = entry ~= nil and self:inventoryLines(entry) or nil
-- flere/færre linjer end ved opbygningen (ny vare) vises ved næste genopbygning
if lines ~= nil and #lines == #row.rows then
for index, line in ipairs(lines) do SmartPickupFrame.setCells(row.rows[index], line) end
end
end
end