162 lines
7.4 KiB
Lua
162 lines
7.4 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).
|
|
|
|
-- "Sugar mill (fabrik): *Sukker 150.0k/180.0k · Roer 356.8k/360.0k" (* = output)
|
|
function SmartPickupFrame:describeInventory(entry)
|
|
local parts = {}
|
|
for _, item in ipairs(entry.entries) do
|
|
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
|
|
table.insert(parts, string.format("%s%s %s", item.isOutput and "*" or "", tostring(item.title), amount))
|
|
end
|
|
local kindText = self.i18n:getText("spu_source_kind_" .. tostring(entry.kind))
|
|
return string.format("%s (%s): %s", tostring(entry.name), kindText, table.concat(parts, " · "))
|
|
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
|
|
|
|
-- "Sukker — 150.0k/180.0k (83 %) · nu 240.300 kr · top 300.400 kr · gården 412.0k = 3.2 døgn · forbrug 130.0k/døgn · prod. 48.0k/døgn"
|
|
function SmartPickupFrame:describeStat(stat)
|
|
local i18n = self.i18n
|
|
local fc = SmartPickupFrame.formatCompact
|
|
local parts = {string.format("%s — %s/%s%s", self:fillTypeTitle(stat.fillType), fc(stat.level), fc(stat.capacity), stat.percent ~= nil and string.format(" (%d %%)", stat.percent) or "")}
|
|
if stat.valueNow ~= nil then
|
|
table.insert(parts, string.format(i18n:getText("spu_stat_now"), SmartPickupFrame.formatMoney(stat.valueNow)))
|
|
table.insert(parts, string.format(i18n:getText("spu_stat_top"), SmartPickupFrame.formatMoney(stat.valueTop)))
|
|
else
|
|
table.insert(parts, i18n:getText("spu_stat_noSell"))
|
|
end
|
|
local days = stat.days ~= nil and string.format(i18n:getText("spu_stat_days"), stat.days) or i18n:getText("spu_stat_noUse")
|
|
table.insert(parts, string.format(i18n:getText("spu_stat_farm"), fc(stat.farmTotal), days))
|
|
if stat.consumption > 0 then table.insert(parts, string.format(i18n:getText("spu_stat_use"), fc(stat.consumption))) end
|
|
if stat.production > 0 then table.insert(parts, string.format(i18n:getText("spu_stat_prod"), fc(stat.production))) end
|
|
return " " .. table.concat(parts, " · ")
|
|
end
|
|
|
|
function SmartPickupFrame:describeStatTotals(stats)
|
|
local totals = ADStorageStats.buildingTotals(stats)
|
|
return string.format(" " .. 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 stats = self:computeBuildingStats(adEnv, entry, inventory)
|
|
for _, stat in ipairs(stats) do
|
|
table.insert(self.storageDetailRows, self:addTextRow(self:describeStat(stat)))
|
|
end
|
|
table.insert(self.storageDetailRows, self:addTextRow(self:describeStatTotals(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
|
|
self.storageDetailRows[index]:setText(self:describeStat(stat))
|
|
end
|
|
self.storageDetailRows[#stats + 1]:setText(self:describeStatTotals(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
|
|
table.insert(self.storageRows, {placeable = entry.placeable, title = self:addTextRow(self:describeInventory(entry))})
|
|
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]
|
|
if entry ~= nil then
|
|
row.title:setText(self:describeInventory(entry))
|
|
end
|
|
end
|
|
end
|