feat: flows kan låne en ledig traktor + aflæsnings-skub ved trigger undervejs (1.62.364)
This commit is contained in:
@@ -38,8 +38,35 @@ function ADFlowSleep.track(since, isIdle, nowMs)
|
||||
return since or nowMs
|
||||
end
|
||||
|
||||
function ADFlowSleep.shouldSleep(since, nowMs)
|
||||
return since ~= nil and nowMs - since >= ADFlowSleep.SLEEP_AFTER_MS
|
||||
function ADFlowSleep.shouldSleep(since, nowMs, afterMs)
|
||||
return since ~= nil and nowMs - since >= (afterMs or ADFlowSleep.SLEEP_AFTER_MS)
|
||||
end
|
||||
|
||||
-- Flow med lånt traktor (brugerønske 19/10: "burde der ikke være en option ... lån et ledigt køretøj, specielt med
|
||||
-- sådan en som honning" — for at spare på økonomien): flowet ejer kun sin vogn (run.borrowWagonId). Har det en tur,
|
||||
-- låner det en ledig traktor, der henter vognen ved Connect; efter turen sover det hurtigt, vognen sættes på plads, og
|
||||
-- traktoren slippes igen (ikke låst til flowet som en fast traktor).
|
||||
ADFlowSleep.BORROW_SLEEP_AFTER_MS = 2 * 60 * 1000
|
||||
|
||||
function ADFlowSleep.sleepAfterMs(run)
|
||||
return (run ~= nil and run.borrowWagonId ~= nil) and ADFlowSleep.BORROW_SLEEP_AFTER_MS or ADFlowSleep.SLEEP_AFTER_MS
|
||||
end
|
||||
|
||||
-- s = {borrow, stopped, sleeping, hasTractor, tractorBusy} -> "await" (startet uden traktor: vent i dvale på en tur) |
|
||||
-- "release" (stoppet/sovende, og traktoren er færdig med vognen) | nil
|
||||
function ADFlowSleep.borrowAction(s)
|
||||
if not s.borrow then return nil end
|
||||
if not s.hasTractor then return (not s.stopped) and "await" or nil end
|
||||
if (s.stopped or s.sleeping) and not s.tractorBusy then return "release" end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Traktoren der lånes. Sidder vognen allerede på en traktor, er det den — hvis den er ledig, ellers ingen (en anden
|
||||
-- kan ikke hente vognen). Ellers bedst egnede ledige (som ved opvågning). s = {attacher = {id, free} | nil, candidates,
|
||||
-- wagon, bestFit} -> {id, …} eller nil
|
||||
function ADFlowSleep.chooseBorrowed(s)
|
||||
if s.attacher ~= nil then return s.attacher.free and s.attacher or nil end
|
||||
return ADFlowSleep.chooseTractor({candidates = s.candidates, wagon = s.wagon, bestFit = s.bestFit})
|
||||
end
|
||||
|
||||
-- Holdes et salgs-flow tilbage af gødningsreserven ("… reserveret til markerne (183 ha)")? Så er der intet at
|
||||
@@ -319,6 +346,8 @@ local function cargoVehicleOf(run, byId, onlyTractorId)
|
||||
if tractor ~= nil and (hasCargoWagon(tractor) or hasOwnBody(tractor)) then return tractor end
|
||||
end
|
||||
end
|
||||
-- lånt traktor: ingen traktor endnu — planen regner med flowets vogn
|
||||
if onlyTractorId == nil and #(run.vehicleIds or {}) == 0 and run.borrowWagonId ~= nil then return findVehicle(run.borrowWagonId) end
|
||||
return nil
|
||||
end
|
||||
|
||||
@@ -488,7 +517,69 @@ local function lentOutWithWagon(run, byId)
|
||||
return nil
|
||||
end
|
||||
|
||||
local function carriesWagon(vehicle, wagonId)
|
||||
for _, implement in ipairs(vehicle ~= nil and ADHitch.rearImplementsOf(vehicle) or {}) do
|
||||
if ADRunsController.getVehicleId(implement.object) == wagonId then return true end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
-- Lånt traktor til flowets vogn: sættes på flowet (vehicleIds + vogn) — opvågningen starter den så som en solo-traktor
|
||||
-- med husket vogn (henter vognen ved Connect). -> true når der er en traktor
|
||||
local function staffBorrowed(adEnv, run, now)
|
||||
local wagon = findVehicle(run.borrowWagonId)
|
||||
if wagon == nil then
|
||||
noteNoWork(run, string.format("flowets vogn (%s) findes ikke længere", tostring(run.borrowWagonId)), now)
|
||||
return false
|
||||
end
|
||||
local wagonName = tostring(ADRuns.vehicleLabel(wagon))
|
||||
local attacher = wagon.getAttacherVehicle ~= nil and wagon:getAttacherVehicle() or nil
|
||||
local attacherInfo = nil
|
||||
if attacher ~= nil then
|
||||
local id = ADRunsController.getVehicleId(attacher)
|
||||
local module = attacher.ad ~= nil and attacher.ad.stateModule or nil
|
||||
local free = id ~= nil and ADRunsController.getRunForVehicle(attacher) == nil and not (module ~= nil and module:isActive())
|
||||
and not (attacher.getIsControlled ~= nil and attacher:getIsControlled()) and not ADFieldWork.isBusyVehicle(attacher, id)
|
||||
attacherInfo = {id = id, name = tostring(ADRuns.vehicleLabel(attacher)), free = free}
|
||||
end
|
||||
local candidates = {}
|
||||
if attacherInfo == nil then
|
||||
local okFree, list = pcall(freeTractors, adEnv, {})
|
||||
candidates = okFree and list or {}
|
||||
end
|
||||
local x, z = positionOf(wagon)
|
||||
local okHp, neededHp = pcall(ADPool.readNeededHp, wagon)
|
||||
local chosen = ADFlowSleep.chooseBorrowed({attacher = attacherInfo, candidates = candidates,
|
||||
wagon = {x = x, z = z, neededHp = okHp and neededHp or nil},
|
||||
bestFit = function(list, implement) return ADPool.bestFit(list, implement, "unloader") end})
|
||||
if chosen == nil then
|
||||
noteNoWork(run, attacherInfo ~= nil and string.format("vognen '%s' sidder på '%s', som er optaget", wagonName, attacherInfo.name)
|
||||
or string.format("ingen ledig traktor kan trække '%s'", wagonName), now)
|
||||
return false
|
||||
end
|
||||
run.vehicleIds = {chosen.id}
|
||||
run.wagons = {[chosen.id] = run.borrowWagonId}
|
||||
run.spareWagons = {}
|
||||
Logging.info(ADFlowSleep.LOG_PREFIX .. "'%s' låner '%s' til vognen '%s'", tostring(run.name), tostring(chosen.name or chosen.id), wagonName)
|
||||
return true
|
||||
end
|
||||
|
||||
local borrowRetryAt = {} -- runId -> ms: lånt traktor kunne ikke starte — ikke låne igen før (review 19/10: ellers blev en
|
||||
-- ny puljetraktor hentet og sendt hjem ved hvert vågn-tjek)
|
||||
|
||||
local function wake(adEnv, run, byId, why)
|
||||
if run.borrowWagonId ~= nil and #(run.vehicleIds or {}) == 0 then
|
||||
local now = nowMs()
|
||||
if borrowRetryAt[run.id] ~= nil and now < borrowRetryAt[run.id] then
|
||||
noteNoWork(run, string.format("lånt traktor kunne ikke starte — prøver igen om %d min",
|
||||
math.ceil((borrowRetryAt[run.id] - now) / 60000)), now)
|
||||
return
|
||||
end
|
||||
borrowRetryAt[run.id] = nil
|
||||
local okStaff, staffed = pcall(staffBorrowed, adEnv, run, nowMs())
|
||||
if not okStaff then logOnce("borrow:" .. tostring(run.id), string.format("'%s': lån af traktor fejlede: %s", tostring(run.name), tostring(staffed))) end
|
||||
if not (okStaff and staffed) then return end
|
||||
end
|
||||
local busySet = lentOutWithWagon(run, byId)
|
||||
if busySet ~= nil then
|
||||
local text = string.format("sættet '%s' er lånt ud (gøder)", tostring(busySet))
|
||||
@@ -539,6 +630,7 @@ local function wake(adEnv, run, byId, why)
|
||||
for _, result in ipairs(results or {}) do table.insert(reasons, tostring(result.reason)) end
|
||||
ADRunsController.stopRun(adEnv, run.id)
|
||||
run.sleeping = true
|
||||
if run.borrowWagonId ~= nil then borrowRetryAt[run.id] = nowMs() + ADFlowSleep.RESTART_FAILED_BACKOFF_MS end
|
||||
Logging.info(ADFlowSleep.LOG_PREFIX .. "'%s' kunne ikke starte (%s) — sover videre", tostring(run.name), table.concat(reasons, ", "))
|
||||
end
|
||||
end
|
||||
@@ -569,7 +661,7 @@ local function sleep(adEnv, run, byId, why, keepWagons)
|
||||
sleepReason[run.id] = nil
|
||||
reasonLogged[run.id] = nil
|
||||
Logging.info(ADFlowSleep.LOG_PREFIX .. "'%s' sover — %s; %s%s",
|
||||
tostring(run.name), why or string.format("intet at lave i %d min", math.floor(ADFlowSleep.SLEEP_AFTER_MS / 60000)),
|
||||
tostring(run.name), why or string.format("intet at lave i %d min", math.floor(ADFlowSleep.sleepAfterMs(run) / 60000)),
|
||||
keepWagons and "sættet bliver samlet og er ledigt til markflåden (gødning)" or "traktor og vogn er ledige til andet", falseWakes > 0
|
||||
and string.format(" (vågnede uden tur %d gang(e) i træk — næste tjek om %d min)", falseWakes, math.floor(ADFlowSleep.wakeBackoffMs(falseWakes) / 60000)) or "")
|
||||
end
|
||||
@@ -710,7 +802,33 @@ local function cancelSleepIfPaused()
|
||||
end
|
||||
end
|
||||
|
||||
-- Flow med lånt traktor: startet uden traktor -> dvale (vågn-tjekket låner en, når der er en tur); stoppet/sovende
|
||||
-- og traktoren færdig med vognen (sat på plads, ingen tømning/hjemtur fra flow-vognene) -> traktoren slippes.
|
||||
local function tickBorrow(run, byId)
|
||||
if run.borrowWagonId == nil then return end
|
||||
local ids = run.vehicleIds or {}
|
||||
local busy = false
|
||||
for _, id in ipairs(ids) do
|
||||
local vehicle = byId[id]
|
||||
local status = vehicle ~= nil and ADFlowWagons ~= nil and ADFlowWagons.statusOf ~= nil and ADFlowWagons.statusOf(vehicle) or nil
|
||||
if status ~= nil or carriesWagon(vehicle, run.borrowWagonId) then busy = true end
|
||||
end
|
||||
local action = ADFlowSleep.borrowAction({borrow = true, stopped = run.stopped, sleeping = run.sleeping, hasTractor = #ids > 0, tractorBusy = busy})
|
||||
if action == "await" then
|
||||
run.stopped, run.sleeping = true, true
|
||||
lastCheck[run.id], idleSince[run.id] = nil, nil
|
||||
Logging.info(ADFlowSleep.LOG_PREFIX .. "'%s' venter på en tur — en ledig traktor lånes, når der er en", tostring(run.name))
|
||||
elseif action == "release" then
|
||||
local names = {}
|
||||
for _, id in ipairs(ids) do table.insert(names, byId[id] ~= nil and tostring(ADRuns.vehicleLabel(byId[id])) or tostring(id)) end
|
||||
run.vehicleIds, run.wagons, run.spareWagons = {}, {}, {}
|
||||
Logging.info(ADFlowSleep.LOG_PREFIX .. "'%s' slipper den lånte traktor '%s' — vognen står på sin plads, traktoren er ledig",
|
||||
tostring(run.name), table.concat(names, ", "))
|
||||
end
|
||||
end
|
||||
|
||||
local function tickRun(adEnv, run, byId, now)
|
||||
tickBorrow(run, byId)
|
||||
if run.sleeping and run.stopped then
|
||||
if not ADFlowSleep.isCheckDue(lastCheck[run.id], now, (wakeState[run.id] or {}).falseWakes) then return end
|
||||
local hasParked = next(run.parkedLoaded or {}) ~= nil
|
||||
@@ -777,7 +895,7 @@ local function tickRun(adEnv, run, byId, now)
|
||||
end
|
||||
local idle = ADFlowSleep.isIdle({stopped = run.stopped, vehicles = vehicles})
|
||||
idleSince[run.id] = ADFlowSleep.track(idleSince[run.id], idle, now)
|
||||
if ADFlowSleep.shouldSleep(idleSince[run.id], now) then
|
||||
if ADFlowSleep.shouldSleep(idleSince[run.id], now, ADFlowSleep.sleepAfterMs(run)) then
|
||||
sleep(adEnv, run, byId)
|
||||
return
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user