feat: stald-silo som mål, retfærdig fordeling, ADSettings + spuSet (v1.62.139)
This commit is contained in:
@@ -507,7 +507,7 @@ local function build(adEnv, farmId)
|
||||
loadMarkers = loadMarkers,
|
||||
unloadMarker = unloadMarker,
|
||||
canBeSource = kind ~= "sell" and not isFillOnlySilo,
|
||||
canBeTarget = kind == "husbandry" or kind == "factory" or isFillOnlySilo,
|
||||
canBeTarget = kind == "husbandry" or kind == "factory" or kind == "silo" or isFillOnlySilo,
|
||||
canSell = kind == "sell",
|
||||
})
|
||||
end
|
||||
|
||||
@@ -344,9 +344,12 @@ function ADRunsController.getTargetFillTypes(adEnv, run, vehicle)
|
||||
stations = getTargetStations(adEnv, run, farmId)
|
||||
end
|
||||
local result, seen = {}, {}
|
||||
-- stald-silo som mål (brugerønske 10/10): kun det foder, staldene ved siloen bruger — ikke alt siloen kan rumme
|
||||
local stableOnly = ADSmartPickup.getStableSiloFillTypes ~= nil and ADSmartPickup.getStableSiloFillTypes(adEnv, stations, farmId) or nil
|
||||
for _, fillType in ipairs(ADRunsController.getCarriableFillTypes(adEnv, vehicle)) do
|
||||
for _, entry in ipairs(stations) do
|
||||
if not seen[fillType] and ADSmartPickup.getStationLevelAndCapacity(entry.station, fillType, farmId) ~= nil then
|
||||
if not seen[fillType] and (stableOnly == nil or stableOnly[fillType])
|
||||
and ADSmartPickup.getStationLevelAndCapacity(entry.station, fillType, farmId) ~= nil then
|
||||
seen[fillType] = true
|
||||
table.insert(result, fillType)
|
||||
end
|
||||
@@ -1579,6 +1582,7 @@ end
|
||||
|
||||
-- Køretøjs-referencer der ikke findes (solgt, andet savegame) logges; run'et beholdes.
|
||||
function ADRunsController.load(adEnv)
|
||||
if ADSettings ~= nil then pcall(ADSettings.load) end
|
||||
local ok, store = pcall(ADRunsStorage.load)
|
||||
if not ok then
|
||||
Logging.warning(ADRunsController.LOG_PREFIX .. "kunne ikke indlæse runs: %s", tostring(store))
|
||||
|
||||
@@ -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
|
||||
@@ -423,6 +423,57 @@ local function mayUseSource(station, fillTypes, level, loadCapacity, need)
|
||||
return true
|
||||
end
|
||||
|
||||
-- Stald-silo som mål: de varer staldene inden for stableSiloRange af siloen bruger (foder + halm). nil når målet ikke
|
||||
-- er en ren silo, eller ingen stald står ved den (så fyldes siloen med alt, den tager — et almindeligt lager).
|
||||
function ADSmartPickup.getStableSiloFillTypes(adEnv, stations, farmId)
|
||||
local placeables = {}
|
||||
for _, entry in ipairs(stations or {}) do
|
||||
local placeable = entry.station ~= nil and entry.station.owningPlaceable or nil
|
||||
if placeable == nil or placeable.spec_silo == nil or getHusbandry(entry.station) ~= nil or getProductionPoint(entry.station) ~= nil then return nil end
|
||||
table.insert(placeables, placeable)
|
||||
end
|
||||
if #placeables == 0 or ADBuildings == nil then return nil end
|
||||
local result, any = {}, false
|
||||
local strawIndex = g_fillTypeManager ~= nil and g_fillTypeManager:getFillTypeIndexByName("STRAW") or nil
|
||||
for _, silo in ipairs(placeables) do
|
||||
local siloId = ADBuildings.getId ~= nil and ADBuildings.getId(silo) or nil
|
||||
local range = ADSettings ~= nil and ADSettings.get("stableSiloRange", siloId) or ADSupplyPlanner.STABLE_SILO_M
|
||||
local okS, sx, _, sz = pcall(getWorldTranslation, silo.rootNode)
|
||||
if okS then
|
||||
for _, building in ipairs(ADBuildings.list(adEnv, farmId) or {}) do
|
||||
local husbandry = building.kind == "husbandry" and building.placeable or nil
|
||||
if husbandry ~= nil and husbandry.rootNode ~= nil then
|
||||
local okH, hx, _, hz = pcall(getWorldTranslation, husbandry.rootNode)
|
||||
if okH and math.sqrt((hx - sx) ^ 2 + (hz - sz) ^ 2) <= range then
|
||||
for fillType in pairs(getHusbandryFoodFillTypes(husbandry) or {}) do result[fillType], any = true, true end
|
||||
if strawIndex ~= nil and husbandry.spec_husbandryStraw ~= nil then result[strawIndex], any = true, true end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return any and result or nil
|
||||
end
|
||||
|
||||
-- Behov pr. vare lige nu (til retfærdig fordeling): køretøj -> {free, at}
|
||||
local demandRegistry = {}
|
||||
ADSmartPickup.DEMAND_FRESH_MS = 10 * 60 * 1000
|
||||
local function registerDemand(fillType, driver, freeSpace)
|
||||
local now = g_currentMission ~= nil and g_currentMission.time or 0
|
||||
demandRegistry[fillType] = demandRegistry[fillType] or {}
|
||||
demandRegistry[fillType][driver] = {free = freeSpace or 0, at = now}
|
||||
local total, others = 0, 0
|
||||
for vehicle, entry in pairs(demandRegistry[fillType]) do
|
||||
if now - entry.at > ADSmartPickup.DEMAND_FRESH_MS then
|
||||
demandRegistry[fillType][vehicle] = nil
|
||||
else
|
||||
total = total + entry.free
|
||||
if vehicle ~= driver then others = others + 1 end
|
||||
end
|
||||
end
|
||||
return total, others
|
||||
end
|
||||
|
||||
-- Kun gårdens lagre eller en silo ved stalden (ADSupplyPlanner.filterSupplySources); marksiloer udelades.
|
||||
function ADSmartPickup.restrictSupplySources(adEnv, markerStations, targetPlaceable)
|
||||
local fieldSiloIds = {}
|
||||
@@ -444,7 +495,9 @@ function ADSmartPickup.restrictSupplySources(adEnv, markerStations, targetPlacea
|
||||
table.insert(entries, {marker = entry.marker, station = entry.station, isFieldSilo = id ~= nil and fieldSiloIds[id] == true,
|
||||
isSilo = placeable ~= nil and placeable.spec_silo ~= nil, distance = distance})
|
||||
end
|
||||
local near, farm = ADSupplyPlanner.filterSupplySources(entries)
|
||||
local targetId = targetPlaceable ~= nil and ADBuildings ~= nil and ADBuildings.getId ~= nil and ADBuildings.getId(targetPlaceable) or nil
|
||||
local range = ADSettings ~= nil and ADSettings.get("stableSiloRange", targetId) or nil
|
||||
local near, farm = ADSupplyPlanner.filterSupplySources(entries, range)
|
||||
local all = {}
|
||||
for _, entry in ipairs(near) do table.insert(all, entry) end
|
||||
for _, entry in ipairs(farm) do table.insert(all, entry) end
|
||||
@@ -1006,12 +1059,23 @@ function ADSmartPickup.findSupplyPickup(adEnv, driver, ingredients, targetWayPoi
|
||||
local choice = not held[need] and (ADSupplyPlanner.isFoodAllowed == nil or ADSupplyPlanner.isFoodAllowed(need, bestFood))
|
||||
and findBestSourceTiered(sourceTiers, {need.fillType}, farmId, loadCapacity, need) or nil
|
||||
if choice ~= nil then
|
||||
-- retfærdig fordeling af gårdens lager mellem alle, der mangler varen lige nu (brugerønske 10/10)
|
||||
local pending = math.min(loadCapacity or math.huge, choice.level or math.huge, need.freeSpace or math.huge)
|
||||
local totalFree, others = registerDemand(need.fillType, driver, need.freeSpace)
|
||||
if choice.isOwn and others > 0 then
|
||||
local share = ADSupplyPlanner.fairShare(choice.level or 0, need.freeSpace or 0, totalFree)
|
||||
if share < pending then
|
||||
Logging.info(ADSmartPickup.LOG_PREFIX .. "'%s': %s deles med %d andre behov — tager %.0f l af %.0f l i %s",
|
||||
tostring(ADRuns.vehicleLabel(driver)), tostring(g_fillTypeManager:getFillTypeNameByIndex(need.fillType)), others, share, choice.level or 0, tostring(choice.markerName))
|
||||
pending = math.max(share, ADSmartPickup.MIN_USEFUL_LITERS or 0)
|
||||
end
|
||||
end
|
||||
return {
|
||||
markerIndex = choice.markerIndex,
|
||||
markerId = choice.markerId,
|
||||
targetWayPointId = wayPointId,
|
||||
-- det der reelt kommer af: vognen, kildens beholdning og målets fri plads sætter grænsen
|
||||
pendingLiters = math.min(loadCapacity or math.huge, choice.level or math.huge, need.freeSpace or math.huge),
|
||||
pendingLiters = pending,
|
||||
markerName = choice.markerName,
|
||||
group = choice.group,
|
||||
level = choice.level,
|
||||
|
||||
@@ -410,11 +410,12 @@ end
|
||||
-- Forsyningens kilder: kun gårdens lagre — eller en silo der står ved stalden (stald-silo, inden for STABLE_SILO_M af
|
||||
-- målet). Marksiloer (siloer ved markerne) bruges ikke: dem tømmer marksilo-flowet hjem til gården (brugerønske 10/10).
|
||||
-- entries = {{isFieldSilo = bool, isSilo = bool, distance = m}}; -> stald-siloer (prøves først), gårdens lagre (ellers).
|
||||
ADSupplyPlanner.STABLE_SILO_M = 80
|
||||
function ADSupplyPlanner.filterSupplySources(entries)
|
||||
ADSupplyPlanner.STABLE_SILO_M = 80 -- standard; justeres med ADSettings "stableSiloRange" (globalt/pr. stald)
|
||||
function ADSupplyPlanner.filterSupplySources(entries, range)
|
||||
range = range or ADSupplyPlanner.STABLE_SILO_M
|
||||
local near, farm = {}, {}
|
||||
for _, entry in ipairs(entries or {}) do
|
||||
local isNear = entry.distance ~= nil and entry.distance <= ADSupplyPlanner.STABLE_SILO_M
|
||||
local isNear = entry.distance ~= nil and entry.distance <= range
|
||||
if isNear and entry.isSilo then
|
||||
table.insert(near, entry)
|
||||
elseif not entry.isFieldSilo then
|
||||
@@ -423,3 +424,12 @@ function ADSupplyPlanner.filterSupplySources(entries)
|
||||
end
|
||||
return near, farm
|
||||
end
|
||||
|
||||
-- Retfærdig fordeling (brugerønske 10/10): skal flere stalde have den samme vare fra gårdens lager, får hver sin andel
|
||||
-- efter behov (tomt rum i truget), ikke først til mølle. available = gårdens beholdning, myFree = eget behov,
|
||||
-- totalFree = alle aktuelle behov (inkl. eget). -> liter denne tur højst må tage.
|
||||
function ADSupplyPlanner.fairShare(available, myFree, totalFree)
|
||||
if (totalFree or 0) <= 0 or (myFree or 0) <= 0 then return available end
|
||||
if myFree >= totalFree then return available end
|
||||
return available * (myFree / totalFree)
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8" standalone="no"?>
|
||||
<modDesc descVersion="107">
|
||||
<author>masterdraco</author>
|
||||
<version>1.62.138.0</version>
|
||||
<version>1.62.139.0</version>
|
||||
<title>
|
||||
<en>AD Smart Pickup</en>
|
||||
</title>
|
||||
@@ -16,6 +16,7 @@
|
||||
<sourceFile filename="adNaming.lua"/>
|
||||
<sourceFile filename="adNames.lua"/>
|
||||
<sourceFile filename="adStuckDiag.lua"/>
|
||||
<sourceFile filename="adSettings.lua"/>
|
||||
<sourceFile filename="adSupplyPlanner.lua"/>
|
||||
<sourceFile filename="adRuns.lua"/>
|
||||
<sourceFile filename="adRunsStorage.lua"/>
|
||||
|
||||
Reference in New Issue
Block a user