fix(flows): én læssemarkør pr. læssestation (mælk OG gylle på samme stald), naturlig navnesortering af bygninger (v1.13.1.0)

This commit is contained in:
2026-09-23 20:15:42 +02:00
parent d46ebbac04
commit e5cb48b011
6 changed files with 117 additions and 35 deletions
+76 -18
View File
@@ -68,13 +68,17 @@ end
-- Trigger-noder pr. bygning: læsse (loadTriggers.triggerNode, pallespawner, objektlager) og
-- aflæsning (unloadTriggers exactFillRootNode/triggerNode/aiNode).
-- loadGroups: én gruppe noder pr. læssestation (mælketank, gyllebeholder ...), så hver station får sin
-- egen markør; pallespawner/objektlager er en gruppe for sig.
local function collectNodes(placeable, loadStations, unloadStations)
local loadNodes, unloadNodes = {}, {}
local loadGroups, unloadNodes = {}, {}
for _, station in pairs(loadStations) do
if station.owningPlaceable == placeable then
local nodes = {}
for _, trigger in pairs(station.loadTriggers or {}) do
if trigger.triggerNode ~= nil then table.insert(loadNodes, trigger.triggerNode) end
if trigger.triggerNode ~= nil then table.insert(nodes, trigger.triggerNode) end
end
if #nodes > 0 then table.insert(loadGroups, nodes) end
end
end
for _, station in pairs(unloadStations) do
@@ -89,15 +93,52 @@ local function collectNodes(placeable, loadStations, unloadStations)
local productionPoint = getProductionPoint(placeable)
local spawner = productionPoint ~= nil and productionPoint.palletSpawner or nil
if spawner ~= nil then
local nodes = {}
for _, place in ipairs(spawner.spawnPlaces or {}) do
if place.startNode ~= nil then table.insert(loadNodes, place.startNode) end
if place.startNode ~= nil then table.insert(nodes, place.startNode) end
end
if #nodes > 0 then table.insert(loadGroups, nodes) end
end
if placeable.spec_objectStorage ~= nil and placeable.rootNode ~= nil then
table.insert(loadNodes, placeable.rootNode)
table.insert(loadGroups, {placeable.rootNode})
table.insert(unloadNodes, placeable.rootNode)
end
return loadNodes, unloadNodes
return loadGroups, unloadNodes
end
-- Naturlig sortering: tal i navne sammenlignes som tal ("Silo 9" < "Silo 10", "25 Silo" < "26 Silo").
local function naturalKey(text)
local parts = {}
for chunk in string.gmatch(tostring(text or ""), "%d+%.?%d*[^%d]*") do
local number, rest = string.match(chunk, "^(%d+%.?%d*)(.*)$")
table.insert(parts, {tonumber(number), string.lower(rest)})
end
local leading = string.match(tostring(text or ""), "^[^%d]*")
if leading ~= nil and leading ~= "" then
table.insert(parts, 1, {nil, string.lower(leading)})
end
return parts
end
function ADBuildings.naturalLess(a, b)
local ka, kb = naturalKey(a), naturalKey(b)
for index = 1, math.max(#ka, #kb) do
local pa, pb = ka[index], kb[index]
if pa == nil then return true end
if pb == nil then return false end
if pa[1] ~= nil and pb[1] ~= nil and pa[1] ~= pb[1] then return pa[1] < pb[1] end
if (pa[1] == nil) ~= (pb[1] == nil) then return pa[1] == nil end
if pa[2] ~= pb[2] then return pa[2] < pb[2] end
end
return false
end
-- Bygninger sorteres efter navn (naturligt), dernæst første læssemarkørs navn (ens navne, fx 40 Farma-siloer).
function ADBuildings.compare(a, b)
if a.name ~= b.name then return ADBuildings.naturalLess(a.name, b.name) end
local ma = a.loadMarkers ~= nil and a.loadMarkers[1] ~= nil and a.loadMarkers[1].name or (a.unloadMarker ~= nil and a.unloadMarker.name or "")
local mb = b.loadMarkers ~= nil and b.loadMarkers[1] ~= nil and b.loadMarkers[1].name or (b.unloadMarker ~= nil and b.unloadMarker.name or "")
return ADBuildings.naturalLess(ma, mb)
end
-- Nærmeste ikke-Wait-markør til en af noderne, ≤ MAX_MARKER_DISTANCE.
@@ -159,8 +200,6 @@ local function isSellPlaceable(placeable, unloadStations)
return false
end
local ORDER = {factory = 1, husbandry = 2, objectStorage = 3, silo = 4, sell = 5}
local function build(adEnv, farmId)
local placeables = g_currentMission ~= nil and g_currentMission.placeableSystem ~= nil and g_currentMission.placeableSystem.placeables or {}
local storage = g_currentMission ~= nil and g_currentMission.storageSystem or nil
@@ -174,16 +213,25 @@ local function build(adEnv, farmId)
if kind ~= nil and (kind == "sell" or isOwnedBy(placeable, farmId)) then
local id = ADBuildings.getId(placeable)
if id ~= nil then
local loadNodes, unloadNodes = collectNodes(placeable, loadStations, unloadStations)
local loadMarker
local loadGroups, unloadNodes = collectNodes(placeable, loadStations, unloadStations)
local loadMarkers, seen = {}, {}
if kind == "silo" then
loadMarker = siloLoadMarker(adEnv, farmId, placeable, loadStations)
local marker = siloLoadMarker(adEnv, farmId, placeable, loadStations)
if marker ~= nil then table.insert(loadMarkers, marker) end
else
loadMarker = nearestMarker(adEnv, loadNodes)
for _, nodes in ipairs(loadGroups) do
local marker = nearestMarker(adEnv, nodes)
if marker ~= nil and not seen[marker.id] then
seen[marker.id] = true
table.insert(loadMarkers, marker)
end
end
table.sort(loadMarkers, function(a, b) return ADBuildings.naturalLess(a.name, b.name) end)
end
table.insert(result, {
id = id, name = getName(placeable, id), kind = kind, placeable = placeable,
loadMarker = loadMarker,
loadMarker = loadMarkers[1],
loadMarkers = loadMarkers,
unloadMarker = nearestMarker(adEnv, unloadNodes),
canBeSource = kind ~= "sell",
canBeTarget = kind == "husbandry" or kind == "factory",
@@ -192,10 +240,7 @@ local function build(adEnv, farmId)
end
end
end
table.sort(result, function(a, b)
if ORDER[a.kind] ~= ORDER[b.kind] then return ORDER[a.kind] < ORDER[b.kind] end
return a.name < b.name
end)
table.sort(result, ADBuildings.compare)
return result
end
@@ -222,8 +267,21 @@ end
-- role "load": bygningen hvis læssemarkør er wayPointId; "unload": aflæsningsmarkør.
function ADBuildings.byMarker(adEnv, farmId, wayPointId, role)
for _, building in ipairs(ADBuildings.list(adEnv, farmId)) do
local marker = role == "unload" and building.unloadMarker or building.loadMarker
if marker ~= nil and marker.id == wayPointId then return building end
if role == "unload" then
if building.unloadMarker ~= nil and building.unloadMarker.id == wayPointId then return building end
else
for _, marker in ipairs(building.loadMarkers or {}) do
if marker.id == wayPointId then return building end
end
end
end
return nil
end
-- Alle markører for en rolle: læsning = én pr. læssestation; aflæsning = én.
function ADBuildings.getMarkers(building, role)
if role == "unload" then
return building.unloadMarker ~= nil and {building.unloadMarker} or {}
end
return building.loadMarkers or (building.loadMarker ~= nil and {building.loadMarker} or {})
end