fix(udsendelse): retfærdig kø i udsendelsesporten — længst ventende sender næste maskine ud (v1.62.239)

This commit is contained in:
2026-10-16 02:20:18 +02:00
parent db89110b02
commit 97d56c1322
7 changed files with 82 additions and 25 deletions
+6 -3
View File
@@ -768,11 +768,14 @@ local function stepField(adEnv, info, env)
elseif decision.action == "dispatch" then
-- én udsendelse ad gangen på tværs af markerne: marken prøver igen ved næste tick
-- fælles port: også "Start alle", marksiloer og flows tæller med (in-game 16/10)
if ADLaunchGate ~= nil and not ADLaunchGate.isOpen() then return end
if ADLaunchGate ~= nil and not ADLaunchGate.isOpen("fieldWork") then return end
local before = ADFieldJobs.assignmentCount()
ADFieldWork.dispatch(adEnv, field, info, decision)
if ADFieldJobs.assignmentCount() > before then
if ADLaunchGate ~= nil then ADLaunchGate.markLaunched() end
local launched = ADFieldJobs.assignmentCount() > before
if ADLaunchGate ~= nil then
if launched then ADLaunchGate.markLaunched("fieldWork") else ADLaunchGate.done("fieldWork") end
end
if launched then
ADFieldJobs.waitingSince[info.id] = nil
elseif ADFieldJobs.waitingSince[info.id] == nil then
ADFieldJobs.waitingSince[info.id] = ADFieldJobs.now
+10 -5
View File
@@ -648,13 +648,16 @@ function ADFieldSilos.planNow(adEnv)
ADFieldSilos.lastFeed = {reserve = feedReserve, stock = farmStock}
ADFieldSilos.lastPlan = plan
local paused = ADFieldJobs ~= nil and ADFieldJobs.state ~= nil and ADFieldJobs.state.paused
if not cfg.enabled or paused then return plan end
if not cfg.enabled or paused then
if ADLaunchGate ~= nil then ADLaunchGate.done("fieldSilos") end
return plan
end
local silosById, storesById = {}, {}
for _, silo in ipairs(silos) do silosById[silo.id] = silo end
for _, store in ipairs(stores) do storesById[store.id] = store end
for _, order in ipairs(plan.dispatch) do
-- fælles udsendelsesport: én maskine ad gangen; resten planlægges igen om lidt
if ADLaunchGate ~= nil and not ADLaunchGate.isOpen() then
if ADLaunchGate ~= nil and not ADLaunchGate.isOpen("fieldSilos") then
gateBlocked = true
break
end
@@ -665,20 +668,22 @@ function ADFieldSilos.planNow(adEnv)
local pair = poolById ~= nil and poolById[order.vehicleId] or nil
if vehicle ~= nil and pair ~= nil then
local ok, err = pcall(dispatchPool, adEnv, order, pair, silosById, storesById)
if ok and ADLaunchGate ~= nil then ADLaunchGate.markLaunched() end
if ok and ADLaunchGate ~= nil then ADLaunchGate.markLaunched("fieldSilos") end
if not ok then
loans[vehicle] = nil
Logging.warning(ADFieldSilos.LOG_PREFIX .. "kunne ikke sende '%s' efter puljevogn: %s", tostring(ADRuns.vehicleLabel(vehicle)), tostring(err))
end
elseif vehicle ~= nil then
local ok, err = pcall(dispatchOne, adEnv, order, vehicle, silosById, storesById)
if ok and ADLaunchGate ~= nil then ADLaunchGate.markLaunched() end
if ok and ADLaunchGate ~= nil then ADLaunchGate.markLaunched("fieldSilos") end
if not ok then
loans[vehicle] = nil
Logging.warning(ADFieldSilos.LOG_PREFIX .. "kunne ikke sende '%s': %s", tostring(ADRuns.vehicleLabel(vehicle)), tostring(err))
end
end
end
-- ikke blokeret: intet mere at sende ud nu — ud af portens kø
if not gateBlocked and ADLaunchGate ~= nil then ADLaunchGate.done("fieldSilos") end
return plan
end
@@ -692,7 +697,7 @@ function ADFieldSilos.tick(adEnv, dt)
checkLoans(adEnv)
end
planTimerMs = planTimerMs + (dt or 0)
local gateReady = gateBlocked and ADLaunchGate ~= nil and ADLaunchGate.isOpen()
local gateReady = gateBlocked and ADLaunchGate ~= nil and ADLaunchGate.isOpen("fieldSilos")
if planTimerMs >= ADFieldSilos.PLAN_INTERVAL_MS or gateReady then
planTimerMs, gateBlocked = 0, false
ADFieldSilos.planNow(adEnv)
+21 -7
View File
@@ -343,6 +343,7 @@ local function farmVehiclesById(adEnv)
end
local restartDue = {} -- runId -> ms hvor næste tjek for ledige traktorer må køre
local restartWaiting = {} -- runId -> true mens en ledig traktor med arbejde står i udsendelsesportens kø
local lastControlled = setmetatable({}, {__mode = "k"}) -- køretøj -> ms hvor spilleren sidst sad i den
local function restartIdleMember(adEnv, run, byId, now)
@@ -353,9 +354,10 @@ local function restartIdleMember(adEnv, run, byId, now)
end
if restartDue[run.id] == nil then restartDue[run.id] = now + ADFlowSleep.RESTART_STARTUP_DELAY_MS end
if now < restartDue[run.id] then return end
-- fælles udsendelsesport lukket: prøv igen ved næste tick (uden at skubbe minut-tjekket)
if ADLaunchGate ~= nil and not ADLaunchGate.isOpen() then return end
restartDue[run.id] = now + ADFlowSleep.WAKE_CHECK_MS
local gateName = "flow:" .. tostring(run.id)
-- står allerede i portens kø: regn ikke det hele igen hvert sekund, før det er vores tur
if restartWaiting[run.id] and ADLaunchGate ~= nil and not ADLaunchGate.isOpen(gateName) then return end
restartWaiting[run.id] = nil
for _, id in ipairs(run.vehicleIds or {}) do
local vehicle = byId[id]
if vehicle ~= nil then
@@ -370,13 +372,25 @@ local function restartIdleMember(adEnv, run, byId, now)
end
end
local id = ADFlowSleep.pickIdleMember(run, members)
if id == nil then return end
local hasWork, why = ADFlowSleep.hasWork(adEnv, run, byId, id)
if not hasWork then return end
local hasWork, why = false, nil
if id ~= nil then hasWork, why = ADFlowSleep.hasWork(adEnv, run, byId, id) end
if not hasWork then
restartDue[run.id] = now + ADFlowSleep.WAKE_CHECK_MS
if ADLaunchGate ~= nil then ADLaunchGate.done(gateName) end
return
end
-- fælles udsendelsesport: står i kø og prøver igen ved næste tick (uden at skubbe minut-tjekket)
if ADLaunchGate ~= nil and not ADLaunchGate.isOpen(gateName) then
restartWaiting[run.id] = true
return
end
restartDue[run.id] = now + ADFlowSleep.WAKE_CHECK_MS
Logging.info(ADFlowSleep.LOG_PREFIX .. "'%s': '%s' holder stille i det kørende flow — startes (%s)", tostring(run.name),
tostring(ADRuns.vehicleLabel(byId[id])), tostring(why))
local result = ADRunsController.startVehicle(adEnv, run, byId[id])
if ADLaunchGate ~= nil and result ~= nil and result.ok then ADLaunchGate.markLaunched() end
if ADLaunchGate ~= nil then
if result ~= nil and result.ok then ADLaunchGate.markLaunched(gateName) else ADLaunchGate.done(gateName) end
end
if ADFlowSleep.restartNeedsBackoff(result) then restartDue[run.id] = now + ADFlowSleep.RESTART_FAILED_BACKOFF_MS end
end
+37 -6
View File
@@ -6,11 +6,16 @@
ADLaunchGate = {}
ADLaunchGate.DEFAULT_MS = 10000
-- en ventende der ikke har spurgt i så lang tid, har opgivet (fx flowet faldt i dvale); alle spørger mindst hvert sekund
ADLaunchGate.STALE_MS = 3000
-- Retfærdig kø: den der har ventet længst får porten. Uden kø tog markarbejdet (tjek hvert sekund, før flowene i
-- hovedløkken) porten hver gang den åbnede, og ledige flow-traktorer kom aldrig til (in-game 16/10: Cow Food 2/3).
local clockMs, lastLaunchMs = 0, nil
local waiting = {} -- navn -> {since, seen}
function ADLaunchGate.reset()
clockMs, lastLaunchMs = 0, nil
clockMs, lastLaunchMs, waiting = 0, nil, {}
end
function ADLaunchGate.tick(dt)
@@ -23,12 +28,38 @@ function ADLaunchGate.intervalMs()
return seconds * 1000
end
-- Må der sendes en maskine ud nu?
function ADLaunchGate.isOpen()
local function intervalPassed()
return lastLaunchMs == nil or clockMs - lastLaunchMs >= ADLaunchGate.intervalMs()
end
-- Kaldes når en maskine faktisk er sendt ud.
function ADLaunchGate.markLaunched()
lastLaunchMs = clockMs
-- Må `who` sende en maskine ud nu? `who` stiller sig i kø (eller bliver stående) ved hvert kald; porten gives til
-- den der har ventet længst, når intervallet er gået. Uden `who`: kun intervallet.
function ADLaunchGate.isOpen(who)
if who == nil then return intervalPassed() end
local entry = waiting[who]
if entry == nil then
entry = {since = clockMs}
waiting[who] = entry
end
entry.seen = clockMs
if not intervalPassed() then return false end
for name, other in pairs(waiting) do
if clockMs - other.seen > ADLaunchGate.STALE_MS then
waiting[name] = nil
elseif name ~= who and (other.since < entry.since or (other.since == entry.since and tostring(name) < tostring(who))) then
return false
end
end
return true
end
-- Kaldes når en maskine faktisk er sendt ud; `who` forlader køen.
function ADLaunchGate.markLaunched(who)
lastLaunchMs = clockMs
if who ~= nil then waiting[who] = nil end
end
-- `who` fik porten (eller ventede) men har intet at sende ud: forlader køen, så de næste kommer til.
function ADLaunchGate.done(who)
if who ~= nil then waiting[who] = nil end
end
+6 -3
View File
@@ -1407,10 +1407,13 @@ end
-- Starter næste traktor i køen, når der er gået staggerMs() siden forrige — og den fælles port er åben
-- (markarbejde/marksiloer kan have sendt en maskine ud imens).
function ADRunsController.processStartQueue(adEnv, dt)
if #ADRunsController.startQueue == 0 then return end
if #ADRunsController.startQueue == 0 then
if ADLaunchGate ~= nil then ADLaunchGate.done("startAll") end
return
end
staggerTimerMs = staggerTimerMs + (dt or 0)
if ADLaunchGate ~= nil then
if not ADLaunchGate.isOpen() then return end
if not ADLaunchGate.isOpen("startAll") then return end
elseif staggerTimerMs < ADRunsController.staggerMs() then
return
end
@@ -1421,7 +1424,7 @@ function ADRunsController.processStartQueue(adEnv, dt)
if run ~= nil and vehicle ~= nil and not isVehicleActive(vehicle) then
local result = ADRunsController.startVehicle(adEnv, run, vehicle)
-- en sprunget/fejlet start lukker ikke porten for de andre
if ADLaunchGate ~= nil and result ~= nil and result.ok then ADLaunchGate.markLaunched() end
if ADLaunchGate ~= nil and result ~= nil and result.ok then ADLaunchGate.markLaunched("startAll") end
end
end
+1 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<modDesc descVersion="107">
<author>masterdraco</author>
<version>1.62.238.0</version>
<version>1.62.239.0</version>
<title>
<en>AD Smart Pickup</en>
</title>