feat: udkørsel "Lever også til" (fodring med samme vogn) + låst traktor og vogn til flowet

This commit is contained in:
2026-10-20 12:29:17 +02:00
parent d074a0640a
commit 34f47dffc8
18 changed files with 340 additions and 27 deletions
+102 -3
View File
@@ -444,8 +444,8 @@ local function storeBuildings(adEnv, run, product, farmId)
return buildings
end
local function addStoreAsSource(adEnv, run, vehicle, product, capacity, sources, sourcesById, farmId, fullOnly, time)
if ADBuildings == nil or ADBuildings.getMarkers == nil then return end
-- Lagrenes læssemarkører med varen (ikke flowets egne kilder) -> storeSources, entries[wayPointId], names[bygning]
local function findStoreSources(adEnv, run, vehicle, product, sources, sourcesById, farmId, fullOnly, time)
local storeSources, entries, names = {}, {}, {}
for _, building in ipairs(storeBuildings(adEnv, run, product, farmId)) do
for _, marker in ipairs(ADBuildings.getMarkers(building, "load")) do
@@ -478,6 +478,12 @@ local function addStoreAsSource(adEnv, run, vehicle, product, capacity, sources,
for _, source in ipairs(storeSources) do if kept[source] then table.insert(survivors, source) end end
storeSources = survivors
end
return storeSources, entries, names
end
local function addStoreAsSource(adEnv, run, vehicle, product, capacity, sources, sourcesById, farmId, fullOnly, time)
if ADBuildings == nil or ADBuildings.getMarkers == nil then return end
local storeSources, entries, names = findStoreSources(adEnv, run, vehicle, product, sources, sourcesById, farmId, fullOnly, time)
local mainAvailable, storeAvailable = 0, 0
for _, entry in ipairs(product.sources) do mainAvailable = mainAvailable + (entry.available or 0) end
for _, source in ipairs(storeSources) do storeAvailable = storeAvailable + entries[source.wayPointId].available end
@@ -504,6 +510,80 @@ local function addStoreAsSource(adEnv, run, vehicle, product, capacity, sources,
end
end
-- "Lever også til" (20/10): et mål fodres når det har under så meget af varen
ADOutbound.FEED_BELOW_PERCENT = 50
-- Niveau og kapacitet for varen i bygningen (dens aflæsningsstationer) -> level, capacity | nil (tager ikke varen)
local function feedLevel(building, fillType, farmId)
local storage = g_currentMission ~= nil and g_currentMission.storageSystem or nil
for _, station in pairs(storage ~= nil and storage:getUnloadingStations() or {}) do
if station.owningPlaceable == building.placeable then
local ok, level, capacity = pcall(ADSmartPickup.getStationLevelAndCapacity, station, fillType, farmId)
if ok and level ~= nil and (capacity or 0) > 0 then return level, capacity end
end
end
return nil
end
ADOutbound.feedLevel = feedLevel -- flow-guiden: hvilke bygninger kan fodres med flowets varer
-- Ren: mest trængende mål under tærsklen. candidates = {{wayPointId, name, level, capacity}} -> {wayPointId, name, percent} | nil
function ADOutbound.pickFeedTarget(candidates, belowPercent)
local best = nil
for _, candidate in ipairs(candidates or {}) do
local capacity = candidate.capacity or 0
if capacity > 0 then
local percent = 100 * (candidate.level or 0) / capacity
if percent < (belowPercent or ADOutbound.FEED_BELOW_PERCENT) and (best == nil or percent < best.percent) then
best = {wayPointId = candidate.wayPointId, name = candidate.name, percent = percent}
end
end
end
return best
end
-- Fodringsmål for varen blandt flowets "Lever også til"-bygninger -> {wayPointId, name, percent, need} | nil.
-- Bygninger der selv er kilde for varen (sourcePlaceables) springes over; målet skal kunne nås (vehicle ~= nil).
local function findFeedTarget(adEnv, run, vehicle, fillType, sourcePlaceables, farmId)
local candidates = {}
for _, building in ipairs(ADRunsController.getFeedBuildings ~= nil and ADRunsController.getFeedBuildings(adEnv, run) or {}) do
local marker = building.unloadMarker
if marker ~= nil and not (sourcePlaceables or {})[building.placeable] then
local level, capacity = feedLevel(building, fillType, farmId)
if level ~= nil and (vehicle == nil or ADWaitPool.hasRouteTo(adEnv, vehicle, marker.id)) then
table.insert(candidates, {wayPointId = marker.id, name = building.name, level = level, capacity = capacity})
end
end
end
local target = ADOutbound.pickFeedTarget(candidates, ADOutbound.FEED_BELOW_PERCENT)
if target ~= nil then
for _, candidate in ipairs(candidates) do
if candidate.wayPointId == target.wayPointId then target.need = math.max(0, candidate.capacity - candidate.level) end
end
end
return target
end
-- Sætter product.feed (mest trængende "Lever også til"-mål) og product.feedSources (kilderne + lageret).
-- -> lagerkilderne, som kalderen lægger i sourcesById EFTER alle varer (ellers fandt den næste vare ikke skuret — review)
local function addFeedTarget(adEnv, run, vehicle, product, sources, sourcesById, farmId, fullOnly, time)
if #(run.feedBuildingIds or {}) == 0 then return {} end
local sourcePlaceables = {}
for _, entry in ipairs(product.sources) do
local source = sourcesById[entry.wayPointId]
if source ~= nil and source.placeable ~= nil then sourcePlaceables[source.placeable] = true end
end
local target = findFeedTarget(adEnv, run, vehicle, product.fillType, sourcePlaceables, farmId)
if target == nil then return {} end
product.feed = target
local feedSources = {}
for _, entry in ipairs(product.sources) do table.insert(feedSources, entry) end
-- lageret (palleskuret) er også kilde til fodring; kilderne pr. waypoint skal kendes af turen (navne, besøg)
local storeSources, entries = findStoreSources(adEnv, run, vehicle, product, sources, sourcesById, farmId, fullOnly, time)
for _, source in ipairs(storeSources) do table.insert(feedSources, entries[source.wayPointId]) end
product.feedSources = feedSources
return storeSources
end
-- input til ADOutboundPlanner.plan + kilder pr. waypoint-id.
function ADOutbound.buildPlanInput(adEnv, run, vehicle)
local farmId = ADSmartPickup.getFarmId(vehicle)
@@ -618,6 +698,19 @@ function ADOutbound.buildPlanInput(adEnv, run, vehicle)
if not okStore then Logging.warning(ADOutbound.LOG_PREFIX .. "'%s': lager som kilde fejlede: %s", tostring(ADRuns.vehicleLabel(vehicle)), tostring(errStore)) end
end
end
-- "Lever også til": fodring planlægges før salg/lager (ADOutboundPlanner.plan)
local feedStores = {}
for _, product in ipairs(products) do
local okFeed, found = pcall(addFeedTarget, adEnv, run, vehicle, product, sources, sourcesById, farmId, fullOnly, time)
if okFeed then
for _, source in ipairs(found or {}) do table.insert(feedStores, source) end
else
Logging.warning(ADOutbound.LOG_PREFIX .. "'%s': fodringsmål fejlede: %s", tostring(ADRuns.vehicleLabel(vehicle)), tostring(found))
end
end
for _, source in ipairs(feedStores) do
if sourcesById[source.wayPointId] == nil then sourcesById[source.wayPointId] = source end
end
local feedMode = ADRunsController ~= nil and ADRunsController.getStoreMarkers ~= nil and #ADRunsController.getStoreMarkers(adEnv, run) > 0
local input = {
feedMode = feedMode,
@@ -649,6 +742,12 @@ function ADOutbound.planCargoTarget(adEnv, run, vehicle, fillType, liters)
local source = ADSources.identify(adEnv, wayPointId, farmId)
if source ~= nil then table.insert(sources, source) end
end
-- "Lever også til": en vare et fodringsmål mangler, køres dertil — ikke tilbage på lager (review 20/10: fiskefoder
-- hentet i palleskuret ville efter gem/indlæs køre tilbage til skuret)
local sourcePlaceables = {}
for _, source in ipairs(sources) do if source.placeable ~= nil then sourcePlaceables[source.placeable] = true end end
local okFeed, feed = pcall(findFeedTarget, adEnv, run, vehicle, fillType, sourcePlaceables, farmId)
if okFeed and feed ~= nil then return feed.wayPointId end
local product = {fillType = fillType, name = name, policy = policy, isPallet = false, sources = {}}
if wantsSell(policy) then
local homeId = run.sourceWayPointIds ~= nil and run.sourceWayPointIds[1] or nil
@@ -713,7 +812,7 @@ local function applyTrip(adEnv, vehicle, run, trip, sourcesById)
for _, fillType in ipairs(trip.fillTypes) do table.insert(names, fillTypeName(fillType)) end
local sourceNames = {}
for _, id in ipairs(trip.sourceIds) do table.insert(sourceNames, sourcesById[id] ~= nil and sourcesById[id].name or tostring(id)) end
local why = trip.kind == "sell" and string.format("salg, %d %% af top", trip.percent or 0) or "lager"
local why = trip.kind == "sell" and string.format("salg, %d %% af top", trip.percent or 0) or (trip.kind == "feed" and "fodring" or "lager")
Logging.info(ADOutbound.LOG_PREFIX .. "'%s' udkørsel: %s fra %s → %s (%s)", tostring(ADRuns.vehicleLabel(vehicle)), table.concat(names, "+"), table.concat(sourceNames, ", "), tostring(trip.targetName), why)
end