feat: indstillinger i sektioner; stald med indbygget silo er aldrig marksilo eller tømmested

This commit is contained in:
2026-10-20 01:11:28 +02:00
parent 1b82b50e0e
commit 8c89e159d5
10 changed files with 149 additions and 44 deletions
@@ -1,9 +1,10 @@
-- 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.
-- Moddens justeringer (ADSettings), delt i sektioner (ADSettings.SECTIONS, vælger øverst): é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 — i sektionen fra ADSettings.SECTION_OF, ellers i den første.
-- Klik går til onClickSettingsRow.
SmartPickupFrame.SETTINGS_ROW_KINDS = {setGlobal = true, setBuilding = true, setBarnOnly = true, lockTractor = true}
SmartPickupFrame.SETTINGS_ROW_KINDS = {setGlobal = true, setBuilding = true, setBarnOnly = true, lockTractor = true, settingsSection = true}
local function choiceTexts(spec, i18n, withGlobal)
local texts = {}
@@ -31,44 +32,16 @@ local function keyTitle(i18n, key)
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()
-- Stalde til pr.-stald-rækkerne: rene stalde, og stalde med dyr (også dem med indbygget silo, der er klassificeret som
-- silo). Fast rækkefølge også for ens navne (sted, så id — review)
local function collectBarns(adEnv)
local barns, animalBarns = {}, {}
for _, building in ipairs(ADBuildings.list(adEnv, farmId) or {}) do
for _, building in ipairs(ADBuildings.list(adEnv, g_currentMission:getFarmId()) 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
@@ -76,12 +49,53 @@ function SmartPickupFrame:fillSettingsRows(adEnv)
end
table.sort(barns, byLabel)
table.sort(animalBarns, byLabel)
return barns, animalBarns
end
-- Sektionsvælgeren øverst (brugerønske 20/10: fanen deles op)
function SmartPickupFrame:getSettingsSection()
return ADSettings.validSection(self.settingsSection)
end
function SmartPickupFrame:addSettingsSectionRow(section)
local texts, state = {}, 1
for index, name in ipairs(ADSettings.SECTIONS) do
table.insert(texts, self.i18n:getText("spu_set_section_" .. name))
if name == section then state = index end
end
local option = self:addFieldSiloChoiceRow("settingsSection", self.i18n:getText("spu_set_sectionTitle"), texts, state)
self.rowData[option] = {kind = "settingsSection"}
end
-- Gælder hele gården: sektionens nøgler uden barnOnly
function SmartPickupFrame:addSettingsGlobalRows(keys)
local i18n, anyGlobal, anyOverride = self.i18n, false, false
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
if not spec.barnOnly then
if not anyGlobal then self:addTextRow(i18n:getText("spu_set_globalTitle")) end
anyGlobal = true
anyOverride = anyOverride or spec.perBuilding == true
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
-- hjælpen om "som globalt" kun når sektionen har pr.-stald-rækker der kan afvige
if anyOverride then self:addTextRow(i18n:getText("spu_set_globalHelp")) end
end
-- Pr. stald: sektionens nøgler med perBuilding. Spredningen vises kun for stalde, der sælger (skjul det irrelevante)
function SmartPickupFrame:addSettingsBarnRows(keys, barns, animalBarns)
local i18n = self.i18n
for _, key in ipairs(keys) do
local spec = ADSettings.KEYS[key]
local list = {}
for _, barn in ipairs(spec.animalBarns and animalBarns or barns) do
if key ~= "sellReadySpreadMonths" or ADSettings.getOwn("sellReadyAnimals", barn.id) == 1 then table.insert(list, barn) end
end
if spec.perBuilding and #list > 0 then
self:addTextRow(string.format(i18n:getText("spu_set_perBarnTitle"), keyTitle(i18n, key)))
for _, barn in ipairs(barns) do
for _, barn in ipairs(list) 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)
@@ -95,7 +109,20 @@ function SmartPickupFrame:fillSettingsRows(adEnv)
end
end
end
self:fillLivestockTractorRows(adEnv, animalBarns)
end
function SmartPickupFrame:fillSettingsRows(adEnv)
if ADSettings == nil then
self:addTextRow("ADSettings mangler")
return
end
local section = self:getSettingsSection()
self:addSettingsSectionRow(section)
local keys = ADSettings.sectionKeys(section)
self:addSettingsGlobalRows(keys)
local barns, animalBarns = collectBarns(adEnv)
self:addSettingsBarnRows(keys, barns, animalBarns)
if section == "animals" then self:fillLivestockTractorRows(adEnv, animalBarns) end
end
-- Fast traktor til slagtedyr pr. stald (kun stalde med salget slået til): "ledig traktor" eller en bestemt traktor
@@ -137,6 +164,11 @@ end
function SmartPickupFrame:onClickSettingsRow(state, data)
self:safe(function(frame)
if data.kind == "settingsSection" then
frame.settingsSection = ADSettings.SECTIONS[state]
frame:requestRebuild()
return
end
if data.kind == "lockTractor" then
local id = data.ids[state]
ADLivestock.setLock(data.buildingId, id ~= false and id or nil)