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

164 lines
7.4 KiB
Lua

-- Smart Pickup — fanen Indstillinger
-- Moddens justeringer (ADSettings): én række pr. nøgle globalt, og pr. stald en række der kan afvige fra det globale
-- ("som globalt" = ingen egen værdi). Nye nøgler i ADSettings.KEYS (med choices) dukker op af sig selv.
-- Klik går til onClickSettingsRow.
SmartPickupFrame.SETTINGS_ROW_KINDS = {setGlobal = true, setBuilding = true, setBarnOnly = true, lockTractor = true}
local function choiceTexts(spec, i18n, withGlobal)
local texts = {}
if withGlobal then table.insert(texts, i18n:getText("spu_set_asGlobal")) end
for index, value in ipairs(spec.choices or {}) do
-- valg med navne (fx pop-up/statusvindue) i stedet for et tal
local labelKey = spec.labels ~= nil and spec.labels[index] or nil
table.insert(texts, labelKey ~= nil and i18n:getText(labelKey) or string.format(spec.format or "%s", tostring(value)))
end
return texts
end
local function choiceIndex(spec, value)
local best, bestDiff = 1, math.huge
for index, choice in ipairs(spec.choices or {}) do
local diff = math.abs(choice - (value or spec.default))
if diff < bestDiff then best, bestDiff = index, diff end
end
return best
end
local function keyTitle(i18n, key)
local text = i18n:getText("spu_set_" .. key)
if text == nil or text == "" or text == ("spu_set_" .. key) then return key end
return text
end
local function sortedKeys()
local keys = {}
for key, spec in pairs(ADSettings.KEYS) do
if spec.choices ~= nil then table.insert(keys, key) end
end
table.sort(keys)
return keys
end
function SmartPickupFrame:fillSettingsRows(adEnv)
local i18n = self.i18n
if ADSettings == nil then
self:addTextRow("ADSettings mangler")
return
end
local keys = sortedKeys()
self:addTextRow(i18n:getText("spu_set_globalTitle"))
for _, key in ipairs(keys) do
local spec = ADSettings.KEYS[key]
-- kun pr. stald (barnOnly): ingen global række
if not spec.barnOnly then
local option = self:addFieldSiloChoiceRow("setGlobal", keyTitle(i18n, key), choiceTexts(spec, i18n, false), choiceIndex(spec, ADSettings.get(key)))
self.rowData[option] = {kind = "setGlobal", key = key}
end
end
self:addTextRow(i18n:getText("spu_set_globalHelp"))
-- pr. stald: kun nøgler med perBuilding = true
local farmId = g_currentMission:getFarmId()
local barns, animalBarns = {}, {}
for _, building in ipairs(ADBuildings.list(adEnv, farmId) or {}) do
if building.kind == "husbandry" then table.insert(barns, building) end
-- stald med indbygget silo er klassificeret som silo, men har dyr (dyre-indstillinger)
if building.kind == "husbandry" or (building.placeable ~= nil and building.placeable.spec_husbandryAnimals ~= nil) then
table.insert(animalBarns, building)
end
end
-- fast rækkefølge også for ens navne (sted, så id — review)
local function byLabel(a, b)
local la, lb = ADBuildings.labelWithPlace(a), ADBuildings.labelWithPlace(b)
if la ~= lb then return la < lb end
return tostring(a.id) < tostring(b.id)
end
table.sort(barns, byLabel)
table.sort(animalBarns, byLabel)
for _, key in ipairs(keys) do
local spec = ADSettings.KEYS[key]
local barns = spec.animalBarns and animalBarns or barns
if spec.perBuilding and #barns > 0 then
self:addTextRow(string.format(i18n:getText("spu_set_perBarnTitle"), keyTitle(i18n, key)))
for _, barn in ipairs(barns) do
local own = ADSettings.getOwn(key, barn.id)
if spec.barnOnly then
-- kun staldens egen værdi (ingen "som global"): valgene direkte, intet valgt = standard (fra)
local option = self:addFieldSiloChoiceRow("setBuilding", ADBuildings.labelWithPlace(barn), choiceTexts(spec, i18n, false), choiceIndex(spec, own))
self.rowData[option] = {kind = "setBarnOnly", key = key, buildingId = barn.id}
else
local state = own ~= nil and (choiceIndex(spec, own) + 1) or 1
local option = self:addFieldSiloChoiceRow("setBuilding", ADBuildings.labelWithPlace(barn), choiceTexts(spec, i18n, true), state)
self.rowData[option] = {kind = "setBuilding", key = key, buildingId = barn.id}
end
end
end
end
self:fillLivestockTractorRows(adEnv, animalBarns)
end
-- Fast traktor til slagtedyr pr. stald (kun stalde med salget slået til): "ledig traktor" eller en bestemt traktor
function SmartPickupFrame:fillLivestockTractorRows(adEnv, animalBarns)
if ADLivestock == nil or ADLivestock.setLock == nil then return end
local i18n = self.i18n
local selling = {}
for _, barn in ipairs(animalBarns) do
if ADSettings.getOwn("sellReadyAnimals", barn.id) == 1 then table.insert(selling, barn) end
end
if #selling == 0 then return end
local tractors = {}
for _, vehicle in ipairs(ADRunsController.getFarmVehicles(adEnv) or {}) do
local id = ADRunsController.getVehicleId(vehicle)
if id ~= nil and vehicle.spec_attacherJoints ~= nil and vehicle.spec_combine == nil and vehicle.spec_dischargeable == nil then
table.insert(tractors, {id = id, name = tostring(ADRuns.vehicleLabel(vehicle))})
end
end
table.sort(tractors, function(a, b) return a.name < b.name end)
self:addTextRow(i18n:getText("spu_set_livestockTractorTitle"))
for _, barn in ipairs(selling) do
local texts, ids, state = {i18n:getText("spu_set_livestockTractorAny")}, {false}, 1
local locked = ADLivestock.getLock(barn.id)
for _, tractor in ipairs(tractors) do
table.insert(texts, tractor.name)
table.insert(ids, tractor.id)
if tractor.id == locked then state = #texts end
end
-- låst traktor findes ikke længere (solgt/ukendt): vis det, så låsen kan ses og slås fra (review)
if locked ~= nil and state == 1 then
table.insert(texts, i18n:getText("spu_set_livestockTractorMissing"))
table.insert(ids, locked)
state = #texts
end
local option = self:addFieldSiloChoiceRow("setBuilding", ADBuildings.labelWithPlace(barn), texts, state)
self.rowData[option] = {kind = "lockTractor", buildingId = barn.id, ids = ids}
end
end
function SmartPickupFrame:onClickSettingsRow(state, data)
self:safe(function(frame)
if data.kind == "lockTractor" then
local id = data.ids[state]
ADLivestock.setLock(data.buildingId, id ~= false and id or nil)
pcall(ADLivestock.save)
frame:requestRebuild()
return
end
local spec = ADSettings.KEYS[data.key]
if spec == nil then return end
if data.kind == "setBarnOnly" then
ADSettings.set(data.key, spec.choices[state], data.buildingId)
elseif data.kind == "setGlobal" then
ADSettings.set(data.key, spec.choices[state], nil)
elseif data.kind == "setBuilding" then
if state == 1 then
ADSettings.clear(data.key, data.buildingId)
else
ADSettings.set(data.key, spec.choices[state - 1], data.buildingId)
end
end
pcall(ADSettings.save)
frame:requestRebuild()
end)
end