98 lines
4.8 KiB
Lua
98 lines
4.8 KiB
Lua
-- Smart Pickup — fanen Marksiloer
|
|
-- Opsætning (til/fra, maks. vogne, mindste mængde, gårdlagre) og status for marksilo-flowet.
|
|
-- Klik går til onClickFieldSiloRow; reglerne ligger i ADFieldSilos/ADFieldSiloPlanner.
|
|
|
|
SmartPickupFrame.FIELDSILO_ROW_KINDS = {fsEnabled = true, fsMaxVehicles = true, fsMinLiters = true, fsStore = true}
|
|
|
|
local function numberTexts(from, to)
|
|
local texts = {}
|
|
for n = from, to do table.insert(texts, tostring(n)) end
|
|
return texts
|
|
end
|
|
|
|
local function literTexts()
|
|
local texts = {}
|
|
for _, liters in ipairs(ADFieldSiloConfig.MIN_LITERS_CHOICES) do table.insert(texts, string.format("%d l", liters)) end
|
|
return texts
|
|
end
|
|
|
|
local function indexOf(list, value)
|
|
for index, entry in ipairs(list) do if entry == value then return index end end
|
|
return 1
|
|
end
|
|
|
|
-- Valg med flere værdier: productPrefab (MultiTextOption). ingredientPrefab er en BinaryOption med kun to sider.
|
|
function SmartPickupFrame:addFieldSiloChoiceRow(kind, title, texts, state)
|
|
local element = self:addRow(self.productPrefab, {kind = kind})
|
|
local option = element:getDescendantByName("option")
|
|
option:setTexts(texts)
|
|
option:setState(state, false)
|
|
element:getDescendantByName("title"):setText(title)
|
|
local tooltip = option:getDescendantByName("ignore")
|
|
if tooltip ~= nil then tooltip:setText("") end
|
|
return option
|
|
end
|
|
|
|
function SmartPickupFrame:fillFieldSiloRows(adEnv)
|
|
local i18n, cfg = self.i18n, ADFieldSilos.config
|
|
local onOff = {i18n:getText("ui_no"), i18n:getText("ui_yes")}
|
|
self:addToggleRow("fsEnabled", i18n:getText("spu_fs_enabled"), onOff, cfg.enabled)
|
|
self:addFieldSiloChoiceRow("fsMaxVehicles", i18n:getText("spu_fs_maxVehicles"), numberTexts(1, ADFieldSiloConfig.MAX_VEHICLES), cfg.maxVehicles)
|
|
self:addFieldSiloChoiceRow("fsMinLiters", i18n:getText("spu_fs_minLiters"), literTexts(), indexOf(ADFieldSiloConfig.MIN_LITERS_CHOICES, cfg.minLiters))
|
|
|
|
local plan = ADFieldSilos.lastPlan
|
|
local waiting = 0
|
|
for _, entry in ipairs(plan ~= nil and plan.status or {}) do if entry.state == "ok" then waiting = waiting + 1 end end
|
|
self:addTextRow(string.format(i18n:getText("spu_fs_status"), ADFieldSilos.loanCount(), cfg.maxVehicles, waiting))
|
|
if #cfg.storeIds == 0 then self:addTextRow(i18n:getText("spu_fs_noStores")) end
|
|
local why = ADFieldSilos.lastVehicleReasons
|
|
if waiting > 0 and ADFieldSilos.loanCount() < cfg.maxVehicles and why ~= nil and why.eligible == 0 then
|
|
self:addTextRow(string.format(i18n:getText("spu_fs_noVehicle"), why.noPark, why.busy, why.controlled, why.notEnabled))
|
|
end
|
|
|
|
self:addTextRow(i18n:getText("spu_fs_silosTitle"))
|
|
if plan == nil or #plan.status == 0 then
|
|
self:addTextRow(" " .. i18n:getText("spu_fs_nothing"))
|
|
else
|
|
self:addCellRow(self.fieldPrefab, {i18n:getText("spu_fs_col_silo"), i18n:getText("spu_col_crop"), i18n:getText("spu_fs_col_liters"),
|
|
i18n:getText("spu_fs_col_onWay"), i18n:getText("spu_fs_col_state"), ""})
|
|
for _, entry in ipairs(plan.status) do
|
|
local desc = g_fillTypeManager:getFillTypeByIndex(entry.fillType)
|
|
local title = desc ~= nil and (desc.title or desc.name) or tostring(entry.fillType)
|
|
local state = i18n:getText("spu_fs_state_" .. entry.state)
|
|
if entry.state == "noFarmStore" then state = string.format(state, title) end
|
|
self:addCellRow(self.fieldPrefab, {tostring(entry.siloName), title, string.format("%d", math.floor(entry.liters)),
|
|
string.format("%d", math.floor(entry.onWay)), state, ""})
|
|
end
|
|
end
|
|
|
|
self:addTextRow(i18n:getText("spu_fs_storesTitle"))
|
|
local farmId = g_currentMission:getFarmId()
|
|
for _, building in ipairs(ADBuildings.list(adEnv, farmId)) do
|
|
if building.kind == "silo" then
|
|
local option = self:addToggleRow("fsStore", tostring(building.name), onOff, ADFieldSiloConfig.hasStore(cfg, building.id))
|
|
self.rowData[option] = {kind = "fsStore", buildingId = building.id}
|
|
end
|
|
end
|
|
end
|
|
|
|
function SmartPickupFrame:onClickFieldSiloRow(state, data)
|
|
self:safe(function(frame)
|
|
local cfg = ADFieldSilos.config
|
|
if data.kind == "fsEnabled" then
|
|
cfg = ADFieldSiloConfig.with(cfg, {enabled = state == SmartPickupFrame.STATE_ON})
|
|
elseif data.kind == "fsMaxVehicles" then
|
|
cfg = ADFieldSiloConfig.with(cfg, {maxVehicles = state})
|
|
elseif data.kind == "fsMinLiters" then
|
|
cfg = ADFieldSiloConfig.with(cfg, {minLiters = ADFieldSiloConfig.MIN_LITERS_CHOICES[state]})
|
|
elseif data.kind == "fsStore" then
|
|
cfg = ADFieldSiloConfig.toggleStore(cfg, data.buildingId)
|
|
end
|
|
ADFieldSilos.setConfig(cfg)
|
|
pcall(ADFieldSilos.save)
|
|
local adEnv = frame.getAdEnv()
|
|
if adEnv ~= nil then pcall(ADFieldSilos.planNow, adEnv) end
|
|
frame:requestRebuild()
|
|
end)
|
|
end
|