feat: stald-silo som mål, retfærdig fordeling, ADSettings + spuSet (v1.62.139)
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
-- AD Settings
|
||||
-- Justerbare indstillinger, globalt og pr. bygning (fremtidig indstillingsside, brugerønske 10/10-2026).
|
||||
-- Gemmes i savegamet som ADSmartPickup_settings.xml. Konsol: spuSet <nøgle> <værdi> [bygningsnavn]; spuSet uden
|
||||
-- argumenter viser nøgler og værdier. Ren logik øverst (testes med luajit), spil-adapter nederst.
|
||||
|
||||
ADSettings = {}
|
||||
ADSettings.FILE_NAME = "ADSmartPickup_settings.xml"
|
||||
ADSettings.ROOT = "ADSmartPickupSettings"
|
||||
ADSettings.LOG_PREFIX = "ADSmartPickup: indstilling: "
|
||||
|
||||
-- nøgle -> {default, min, max, text}. Nye indstillinger tilføjes her, så de får standardværdi, grænser og hjælpetekst.
|
||||
ADSettings.KEYS = {
|
||||
stableSiloRange = {default = 80, min = 10, max = 500, text = "m: en silo inden for denne afstand af en stald er staldens egen silo (forsyning henter der først)"},
|
||||
}
|
||||
|
||||
local values = {global = {}, byBuilding = {}}
|
||||
|
||||
-- ---- ren logik ----
|
||||
|
||||
function ADSettings.reset()
|
||||
values = {global = {}, byBuilding = {}}
|
||||
end
|
||||
|
||||
function ADSettings.clamp(key, value)
|
||||
local spec = ADSettings.KEYS[key]
|
||||
local number = tonumber(value)
|
||||
if spec == nil or number == nil then return nil end
|
||||
return math.max(spec.min or -math.huge, math.min(spec.max or math.huge, number))
|
||||
end
|
||||
|
||||
-- Værdien for nøglen: bygningens egen, ellers den globale, ellers standard.
|
||||
function ADSettings.get(key, buildingId)
|
||||
local spec = ADSettings.KEYS[key]
|
||||
if spec == nil then return nil end
|
||||
local own = buildingId ~= nil and values.byBuilding[buildingId] or nil
|
||||
if own ~= nil and own[key] ~= nil then return own[key] end
|
||||
if values.global[key] ~= nil then return values.global[key] end
|
||||
return spec.default
|
||||
end
|
||||
|
||||
-- Sætter (buildingId = nil: globalt). Returnerer den gemte værdi eller nil ved ukendt nøgle/værdi.
|
||||
function ADSettings.set(key, value, buildingId)
|
||||
local clamped = ADSettings.clamp(key, value)
|
||||
if clamped == nil then return nil end
|
||||
if buildingId ~= nil then
|
||||
values.byBuilding[buildingId] = values.byBuilding[buildingId] or {}
|
||||
values.byBuilding[buildingId][key] = clamped
|
||||
else
|
||||
values.global[key] = clamped
|
||||
end
|
||||
return clamped
|
||||
end
|
||||
|
||||
function ADSettings.snapshot() return values end
|
||||
function ADSettings.restore(snapshot) values = snapshot or {global = {}, byBuilding = {}} end
|
||||
|
||||
-- ---- lagring ----
|
||||
|
||||
function ADSettings.getPath()
|
||||
local runsPath = ADRunsStorage ~= nil and ADRunsStorage.getPath ~= nil and ADRunsStorage.getPath() or nil
|
||||
if runsPath == nil then return nil end
|
||||
return (string.gsub(runsPath, ADRunsStorage.FILE_NAME .. "$", ADSettings.FILE_NAME))
|
||||
end
|
||||
|
||||
function ADSettings.save()
|
||||
local path = ADSettings.getPath()
|
||||
if path == nil or XMLFile == nil then return false end
|
||||
local xmlFile = XMLFile.create("ADSmartPickupSettings", path, ADSettings.ROOT)
|
||||
if xmlFile == nil then return false end
|
||||
local index = 0
|
||||
for key, value in pairs(values.global) do
|
||||
local node = string.format("%s.setting(%d)", ADSettings.ROOT, index)
|
||||
xmlFile:setString(node .. "#key", key)
|
||||
xmlFile:setFloat(node .. "#value", value)
|
||||
index = index + 1
|
||||
end
|
||||
for buildingId, keys in pairs(values.byBuilding) do
|
||||
for key, value in pairs(keys) do
|
||||
local node = string.format("%s.setting(%d)", ADSettings.ROOT, index)
|
||||
xmlFile:setString(node .. "#key", key)
|
||||
xmlFile:setFloat(node .. "#value", value)
|
||||
xmlFile:setString(node .. "#building", buildingId)
|
||||
index = index + 1
|
||||
end
|
||||
end
|
||||
xmlFile:save()
|
||||
xmlFile:delete()
|
||||
return true
|
||||
end
|
||||
|
||||
function ADSettings.load()
|
||||
ADSettings.reset()
|
||||
local path = ADSettings.getPath()
|
||||
if path == nil or fileExists == nil or not fileExists(path) or XMLFile == nil then return false end
|
||||
local xmlFile = XMLFile.load("ADSmartPickupSettings", path)
|
||||
if xmlFile == nil then return false end
|
||||
xmlFile:iterate(ADSettings.ROOT .. ".setting", function(_, node)
|
||||
local key = xmlFile:getString(node .. "#key")
|
||||
local value = xmlFile:getFloat(node .. "#value")
|
||||
local building = xmlFile:getString(node .. "#building")
|
||||
if key ~= nil and value ~= nil then ADSettings.set(key, value, building ~= "" and building or nil) end
|
||||
end)
|
||||
xmlFile:delete()
|
||||
return true
|
||||
end
|
||||
|
||||
-- ---- konsol ----
|
||||
|
||||
local function findBuilding(name)
|
||||
local adEnv = ADFieldSilos ~= nil and ADFieldSilos.adEnv or nil
|
||||
if adEnv == nil or ADBuildings == nil or g_currentMission == nil then return nil end
|
||||
local wanted = string.lower(name)
|
||||
for _, building in ipairs(ADBuildings.list(adEnv, g_currentMission:getFarmId()) or {}) do
|
||||
if string.lower(tostring(building.name)) == wanted then return building end
|
||||
end
|
||||
for _, building in ipairs(ADBuildings.list(adEnv, g_currentMission:getFarmId()) or {}) do
|
||||
if string.find(string.lower(tostring(building.name)), wanted, 1, true) then return building end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function ADSettings:consoleSet(key, value, ...)
|
||||
if key == nil or key == "" then
|
||||
local lines = {}
|
||||
for name, spec in pairs(ADSettings.KEYS) do
|
||||
table.insert(lines, string.format("%s = %s (standard %s) — %s", name, tostring(ADSettings.get(name)), tostring(spec.default), spec.text))
|
||||
end
|
||||
table.sort(lines)
|
||||
return "spuSet <nøgle> <værdi> [bygningsnavn]\n" .. table.concat(lines, "\n")
|
||||
end
|
||||
if ADSettings.KEYS[key] == nil then return "spuSet: ukendt nøgle '" .. tostring(key) .. "'" end
|
||||
local buildingName = table.concat({...}, " ")
|
||||
local building = nil
|
||||
if buildingName ~= "" then
|
||||
building = findBuilding(buildingName)
|
||||
if building == nil then return "spuSet: ingen bygning hedder '" .. buildingName .. "'" end
|
||||
end
|
||||
local saved = ADSettings.set(key, value, building ~= nil and building.id or nil)
|
||||
if saved == nil then return "spuSet: ugyldig værdi '" .. tostring(value) .. "'" end
|
||||
ADSettings.save()
|
||||
local text = string.format("%s = %s%s", key, tostring(saved), building ~= nil and (" for '" .. tostring(building.name) .. "'") or " (globalt)")
|
||||
Logging.info(ADSettings.LOG_PREFIX .. "%s", text)
|
||||
return "spuSet: " .. text
|
||||
end
|
||||
|
||||
if addConsoleCommand ~= nil then
|
||||
addConsoleCommand("spuSet", "Indstilling: spuSet <nøgle> <værdi> [bygningsnavn]; uden argumenter: liste", "consoleSet", ADSettings)
|
||||
end
|
||||
Reference in New Issue
Block a user