363 lines
17 KiB
Lua
363 lines
17 KiB
Lua
-- AD Flow Wagons
|
|
-- Flows ejer deres vogne, men traktor og vogn er ikke låst sammen: den vogn der hænger på en flow-traktor huskes
|
|
-- (run.wagons = {[traktorId] = vognId}). Starter flowet med en solo-traktor, henter den sin vogn ved "Connect <vognens
|
|
-- parkering>". Stoppes/slettes flowet, eller er dets bygning væk, sættes vognen på plads og traktoren kører hjem —
|
|
-- så er begge ledige i maskinpuljen (en stoppet flows vogn må lånes; en kørende flows aldrig).
|
|
|
|
ADFlowWagons = {}
|
|
ADFlowWagons.LOG_PREFIX = "ADSmartPickup: flow-vogne: "
|
|
ADFlowWagons.REMEMBER_MS = 10000 -- husk vogne / prøv ventende starter igen
|
|
ADFlowWagons.BUILDING_CHECK_MS = 60000
|
|
ADFlowWagons.MISSING_CHECKS = 2 -- så mange tjek i træk før en bygning regnes for væk
|
|
|
|
-- ---- ren logik ----
|
|
|
|
ADFlowWagons.serialize = function(wagons) return ADRuns.serializeWagons(wagons) end
|
|
ADFlowWagons.parse = function(text) return ADRuns.parseWagons(text) end
|
|
|
|
-- Husk traktorens vogn: ny tabel ved ændring, samme tabel når intet ændres.
|
|
function ADFlowWagons.remember(wagons, tractorId, wagonId)
|
|
if (wagons or {})[tractorId] == wagonId then return wagons end
|
|
local copy = ADRuns.copyWagons(wagons)
|
|
copy[tractorId] = wagonId
|
|
return copy
|
|
end
|
|
|
|
-- s = {hasRear, wagonId, wagonExists, hasConnect, wagonAttached, wagonBusy} -> "start" | "fetch" | "wait"
|
|
function ADFlowWagons.startAction(s)
|
|
-- lånt ud af puljen (markarbejde, marksilo, på hjemtur): vent til den er fri
|
|
if s.tractorBusy then return "wait" end
|
|
if s.hasRear or s.wagonId == nil or not s.wagonExists or not s.hasConnect then return "start" end
|
|
if s.wagonAttached or s.wagonBusy then return "wait" end
|
|
return "fetch"
|
|
end
|
|
|
|
-- Ingen husket vogn: gæt ud fra parkeringsnavnet — en ledig vogn hvis parkering indeholder flowets navn, helst med
|
|
-- traktorens nummer til sidst ("Cow Food 3" -> "P Cow Food vogn 3"). candidates = {{id, parkName}}, taken = {[id]=true}
|
|
function ADFlowWagons.guessWagon(runName, tractorNumber, candidates, taken)
|
|
local wanted = tostring(runName or ""):lower()
|
|
if wanted == "" then return nil end
|
|
local matching = {}
|
|
for _, candidate in ipairs(candidates or {}) do
|
|
local park = tostring(candidate.parkName or ""):lower()
|
|
if not (taken or {})[candidate.id] and park:find(wanted, 1, true) ~= nil then table.insert(matching, candidate) end
|
|
end
|
|
table.sort(matching, function(a, b) return tostring(a.parkName) < tostring(b.parkName) end)
|
|
for _, candidate in ipairs(matching) do
|
|
if tractorNumber ~= nil and tostring(candidate.parkName):match("%s(%d+)$") == tostring(tractorNumber) then return candidate.id end
|
|
end
|
|
return matching[1] ~= nil and matching[1].id or nil
|
|
end
|
|
|
|
function ADFlowWagons.ownerOf(runs, wagonId)
|
|
for _, run in ipairs(runs or {}) do
|
|
for _, id in pairs(run.wagons or {}) do
|
|
if id == wagonId then return run end
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
-- Holdes vognen af et flow der ikke er stoppet? (så må puljen ikke låne den)
|
|
function ADFlowWagons.isHeld(runs, wagonId)
|
|
for _, run in ipairs(runs or {}) do
|
|
if not run.stopped then
|
|
for _, id in pairs(run.wagons or {}) do
|
|
if id == wagonId then return true end
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- Et tjek af om bygningen mangler: -> ny tæller, væk?
|
|
function ADFlowWagons.missingStep(count, missingNow)
|
|
if not missingNow then return 0, false end
|
|
local next = (count or 0) + 1
|
|
return next, next >= ADFlowWagons.MISSING_CHECKS
|
|
end
|
|
|
|
-- Flowets bygnings-id'er der ikke findes i exists = {[id] = true}. Kun bygninger der er set i denne session
|
|
-- (seen) tæller: et forældet id i et gammelt flow må ikke stoppe et flow der virker.
|
|
function ADFlowWagons.missingBuildings(run, exists, seen)
|
|
local missing = {}
|
|
for _, key in ipairs({"sourceBuildingIds", "targetBuildingIds", "storeBuildingIds"}) do
|
|
for _, id in ipairs(run[key] or {}) do
|
|
if not exists[id] and (seen or {})[id] then table.insert(missing, id) end
|
|
end
|
|
end
|
|
return missing
|
|
end
|
|
|
|
-- ---- spil ----
|
|
|
|
local waiting = setmetatable({}, {__mode = "k"}) -- traktor -> runId (venter på sin vogn)
|
|
local fetching = setmetatable({}, {__mode = "k"}) -- traktor -> runId (henter sin vogn)
|
|
local missingCount = {} -- runId -> tjek i træk
|
|
local seenBuildings = {} -- bygnings-id'er set i denne session
|
|
local rememberTimer, buildingTimer = 0, 0
|
|
|
|
local function runs()
|
|
return ADRunsController.store ~= nil and ADRunsController.store.runs or {}
|
|
end
|
|
|
|
local function findAnyVehicle(id)
|
|
for _, vehicle in pairs(g_currentMission.vehicleSystem.vehicles) do
|
|
if ADRunsController.getVehicleId(vehicle) == id then return vehicle end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function connectOf(adEnv, wagon)
|
|
local index = wagon.advd ~= nil and wagon.advd.parkDestination or nil
|
|
local park = type(index) == "number" and index >= 1 and adEnv.ADGraphManager:getMapMarkerById(index) or nil
|
|
if park == nil then return nil end
|
|
local marker = ADPool.connectFor(park.name, adEnv.ADGraphManager:getMapMarkers())
|
|
return marker ~= nil and marker.id or nil
|
|
end
|
|
|
|
-- Traktorer i stoppede flows er ledige i puljen; traktorer i kørende flows aldrig.
|
|
function ADFlowWagons.isFreeTractor(vehicle)
|
|
local run = ADRunsController.getRunForVehicle(vehicle)
|
|
return run == nil or run.stopped == true
|
|
end
|
|
|
|
-- Holder flowet noget: en traktor henter/venter på sin vogn eller har en vogn bagpå (til Stop-knappen).
|
|
function ADFlowWagons.hasActivity(run, vehiclesById)
|
|
for _, vehicleId in ipairs(run.vehicleIds or {}) do
|
|
local vehicle = vehiclesById ~= nil and vehiclesById[vehicleId] or nil
|
|
if vehicle ~= nil and (waiting[vehicle] ~= nil or fetching[vehicle] ~= nil or #ADHitch.rearImplementsOf(vehicle) > 0) then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function ADFlowWagons.hasWagonFor(run, tractorId)
|
|
return (run.wagons or {})[tractorId] ~= nil
|
|
end
|
|
|
|
function ADFlowWagons.isHeldWagon(wagonId)
|
|
return ADFlowWagons.isHeld(runs(), wagonId)
|
|
end
|
|
|
|
-- Frakoblede vogne med parkering, som intet kørende flow holder, og som flowet ikke allerede har givet en traktor.
|
|
local function guessFor(adEnv, run, vehicle, tractorId)
|
|
local taken = {}
|
|
for _, id in pairs(run.wagons or {}) do taken[id] = true end
|
|
local candidates = {}
|
|
local farmId = g_currentMission:getFarmId()
|
|
for _, any in pairs(g_currentMission.vehicleSystem.vehicles) do
|
|
local index = any.advd ~= nil and any.advd.parkDestination or nil
|
|
local attached = any.getAttacherVehicle ~= nil and any:getAttacherVehicle() ~= nil
|
|
if any.spec_motorized == nil and any.spec_attachable ~= nil and not attached and type(index) == "number" and index >= 1
|
|
and any.getOwnerFarmId ~= nil and any:getOwnerFarmId() == farmId then
|
|
local id = ADRunsController.getVehicleId(any)
|
|
local park = adEnv.ADGraphManager:getMapMarkerById(index)
|
|
if id ~= nil and park ~= nil and not ADFlowWagons.isHeld(runs(), id) then
|
|
table.insert(candidates, {id = id, parkName = park.name})
|
|
end
|
|
end
|
|
end
|
|
local number = nil
|
|
for index, id in ipairs(run.vehicleIds or {}) do
|
|
if id == tractorId then number = index end
|
|
end
|
|
local label = tostring(ADRuns.vehicleLabel(vehicle))
|
|
number = tonumber(label:match("%s(%d+)$")) or number
|
|
local wagonId = ADFlowWagons.guessWagon(run.name, number, candidates, taken)
|
|
if wagonId ~= nil then
|
|
Logging.info(ADFlowWagons.LOG_PREFIX .. "flow '%s': '%s' får vognen ved parkeringen der matcher flowets navn", tostring(run.name), label)
|
|
end
|
|
return wagonId
|
|
end
|
|
|
|
-- Lånt ud af puljen: markarbejde, marksilo-lån, på hjemtur eller Courseplay kører den.
|
|
function ADFlowWagons.isPoolBusy(vehicle, tractorId)
|
|
return (ADFieldJobs ~= nil and ADFieldJobs.state.assignments[tractorId] ~= nil)
|
|
or (ADFieldSilos ~= nil and ADFieldSilos.isLoaned(vehicle))
|
|
or (ADPool ~= nil and ADPool.isReturning(vehicle))
|
|
or (ADCpBridge ~= nil and ADCpBridge.isWorking(vehicle))
|
|
end
|
|
|
|
-- Før en flow-traktor startes: hent sin vogn / vent på den. -> håndteret, resultat (som startVehicle)
|
|
function ADFlowWagons.beforeStart(adEnv, run, vehicle)
|
|
local tractorId = ADRunsController.getVehicleId(vehicle)
|
|
if fetching[vehicle] ~= nil then return true, {vehicle = vehicle, ok = true, reason = "fetchingWagon"} end
|
|
local wagonId = (run.wagons or {})[tractorId]
|
|
if wagonId == nil and #ADHitch.rearImplementsOf(vehicle) == 0 then
|
|
wagonId = guessFor(adEnv, run, vehicle, tractorId)
|
|
if wagonId ~= nil then run.wagons = ADFlowWagons.remember(run.wagons, tractorId, wagonId) end
|
|
end
|
|
local wagon = wagonId ~= nil and findAnyVehicle(wagonId) or nil
|
|
local connectId = wagon ~= nil and connectOf(adEnv, wagon) or nil
|
|
local action = ADFlowWagons.startAction({
|
|
tractorBusy = ADFlowWagons.isPoolBusy(vehicle, tractorId),
|
|
hasRear = #ADHitch.rearImplementsOf(vehicle) > 0, wagonId = wagonId, wagonExists = wagon ~= nil,
|
|
hasConnect = connectId ~= nil,
|
|
wagonAttached = wagon ~= nil and wagon.getAttacherVehicle ~= nil and wagon:getAttacherVehicle() ~= nil,
|
|
wagonBusy = wagonId ~= nil and ADPool.isReserved(wagonId),
|
|
})
|
|
if action == "start" then return false end
|
|
ADRunsController.runningRuns[run.id] = true
|
|
if action == "wait" then
|
|
if waiting[vehicle] == nil then
|
|
Logging.info(ADFlowWagons.LOG_PREFIX .. "flow '%s': '%s' venter på sin vogn (i brug andetsteds)", tostring(run.name),
|
|
tostring(ADRuns.vehicleLabel(vehicle)))
|
|
end
|
|
waiting[vehicle] = run.id
|
|
return true, {vehicle = vehicle, ok = true, reason = "waitingWagon"}
|
|
end
|
|
waiting[vehicle] = nil
|
|
fetching[vehicle] = run.id
|
|
local runId = run.id
|
|
local okFetch, errFetch = pcall(ADHitch.fetch, adEnv, vehicle, connectId, function(tractor, ok, detail)
|
|
fetching[tractor] = nil
|
|
local current = ADRuns.getRun(ADRunsController.store, runId)
|
|
if current == nil or current.stopped then
|
|
ADHitch.returnHome(adEnv, tractor)
|
|
return
|
|
end
|
|
if ok then
|
|
local result = ADRunsController.startVehicle(adEnv, current, tractor)
|
|
if result ~= nil and not result.ok then
|
|
-- flowet kan ikke startes (markør, bygning …): ikke efterladt ved Connect med vognen
|
|
Logging.warning(ADFlowWagons.LOG_PREFIX .. "flow '%s': '%s' kunne ikke starte (%s) — vognen sættes tilbage",
|
|
tostring(current.name), tostring(ADRuns.vehicleLabel(tractor)), tostring(result.reason))
|
|
ADHitch.returnHome(adEnv, tractor)
|
|
end
|
|
else
|
|
Logging.warning(ADFlowWagons.LOG_PREFIX .. "flow '%s': '%s' kunne ikke hente sin vogn (%s)", tostring(current.name),
|
|
tostring(ADRuns.vehicleLabel(tractor)), tostring(detail))
|
|
end
|
|
end)
|
|
if not okFetch then
|
|
fetching[vehicle] = nil
|
|
return true, {vehicle = vehicle, ok = false, reason = "error"}
|
|
end
|
|
Logging.info(ADFlowWagons.LOG_PREFIX .. "flow '%s': '%s' henter sin vogn", tostring(run.name), tostring(ADRuns.vehicleLabel(vehicle)))
|
|
return true, {vehicle = vehicle, ok = true, reason = "fetchingWagon"}
|
|
end
|
|
|
|
-- Stop/slet/bygning væk: vognen på plads, traktoren hjem.
|
|
function ADFlowWagons.sendHome(adEnv, run)
|
|
for _, vehicleId in ipairs(run.vehicleIds or {}) do
|
|
local vehicle = ADRunsController.findVehicleById(adEnv, vehicleId)
|
|
if vehicle ~= nil then
|
|
waiting[vehicle] = nil
|
|
if fetching[vehicle] ~= nil then
|
|
fetching[vehicle] = nil
|
|
ADHitch.cancel(vehicle)
|
|
end
|
|
local okHome, pooled = pcall(ADHitch.returnHome, adEnv, vehicle)
|
|
if not (okHome and pooled) then
|
|
local park = ADFieldWork.parkMarkerId(adEnv, vehicle)
|
|
if park ~= nil then pcall(ADFieldWork.startAd, adEnv, vehicle, adEnv.AutoDrive.MODE_DRIVETO, park, nil) end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function rememberAll(adEnv)
|
|
for _, run in ipairs(runs()) do
|
|
if not run.stopped then
|
|
for _, vehicleId in ipairs(run.vehicleIds or {}) do
|
|
local vehicle = ADRunsController.findVehicleById(adEnv, vehicleId)
|
|
local rear = vehicle ~= nil and ADHitch.rearImplementsOf(vehicle) or {}
|
|
local wagonId = rear[1] ~= nil and ADRunsController.getVehicleId(rear[1].object) or nil
|
|
if wagonId ~= nil then
|
|
local updated = ADFlowWagons.remember(run.wagons, vehicleId, wagonId)
|
|
if updated ~= run.wagons then
|
|
run.wagons = updated
|
|
Logging.info(ADFlowWagons.LOG_PREFIX .. "flow '%s': '%s' har vognen '%s'", tostring(run.name),
|
|
tostring(ADRuns.vehicleLabel(vehicle)), tostring(ADRuns.vehicleLabel(rear[1].object)))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function retryWaiting(adEnv)
|
|
local list = {}
|
|
for vehicle, runId in pairs(waiting) do table.insert(list, {vehicle = vehicle, runId = runId}) end
|
|
for _, item in ipairs(list) do
|
|
waiting[item.vehicle] = nil
|
|
local run = ADRuns.getRun(ADRunsController.store, item.runId)
|
|
if run ~= nil and not run.stopped then pcall(ADRunsController.startVehicle, adEnv, run, item.vehicle) end
|
|
end
|
|
end
|
|
|
|
local function checkBuildings(adEnv)
|
|
local exists = {}
|
|
local placeables = g_currentMission.placeableSystem ~= nil and g_currentMission.placeableSystem.placeables or {}
|
|
for _, placeable in pairs(placeables) do
|
|
local id = ADBuildings.getId(placeable)
|
|
if id ~= nil then exists[id] = true end
|
|
end
|
|
if next(exists) == nil then return end -- listen er ikke klar: sig intet
|
|
for id in pairs(exists) do seenBuildings[id] = true end
|
|
for _, run in ipairs(runs()) do
|
|
if not run.stopped then
|
|
local missing = ADFlowWagons.missingBuildings(run, exists, seenBuildings)
|
|
local count, gone = ADFlowWagons.missingStep(missingCount[run.id], #missing > 0)
|
|
missingCount[run.id] = count
|
|
if gone then
|
|
missingCount[run.id] = 0
|
|
Logging.warning(ADFlowWagons.LOG_PREFIX .. "flow '%s': bygning findes ikke længere (%s) — stoppet, traktorer kørt hjem",
|
|
tostring(run.name), table.concat(missing, ", "))
|
|
ADRunsController.stopRun(adEnv, run.id)
|
|
if ADFieldJobs ~= nil and ADFieldJobs.notify ~= nil then
|
|
pcall(ADFieldJobs.notify, string.format("Flow '%s': bygning findes ikke længere — stoppet, traktorer kørt hjem", tostring(run.name)), true)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- Én gang efter indlæsning: et stoppet flows traktor der holder stille med sin vogn (gemt midt i hjemturen — AD
|
|
-- kørte den færdig, men afkoblingen er glemt) køres hjem. Ikke løbende: spilleren kan selv have koblet på.
|
|
ADFlowWagons.AFTER_LOAD_MS = 120000
|
|
local sinceLoad, cleanedUp = 0, false
|
|
|
|
local function cleanupAfterLoad(adEnv)
|
|
for _, run in ipairs(runs()) do
|
|
if run.stopped then
|
|
for _, vehicleId in ipairs(run.vehicleIds or {}) do
|
|
local vehicle = ADRunsController.findVehicleById(adEnv, vehicleId)
|
|
local module = vehicle ~= nil and vehicle.ad ~= nil and vehicle.ad.stateModule or nil
|
|
local controlled = vehicle ~= nil and vehicle.getIsControlled ~= nil and vehicle:getIsControlled()
|
|
if module ~= nil and not module:isActive() and not controlled and #ADHitch.poolParkIds(adEnv, vehicle) > 0
|
|
and not ADFlowWagons.isPoolBusy(vehicle, vehicleId) then
|
|
Logging.info(ADFlowWagons.LOG_PREFIX .. "flow '%s' er stoppet: '%s' sætter sin vogn på plads", tostring(run.name),
|
|
tostring(ADRuns.vehicleLabel(vehicle)))
|
|
pcall(ADHitch.returnHome, adEnv, vehicle)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function ADFlowWagons.tick(adEnv, dt)
|
|
if adEnv == nil or ADRunsController.store == nil then return end
|
|
if not cleanedUp then
|
|
sinceLoad = sinceLoad + dt
|
|
if sinceLoad >= ADFlowWagons.AFTER_LOAD_MS then
|
|
cleanedUp = true
|
|
local ok, err = pcall(cleanupAfterLoad, adEnv)
|
|
if not ok then Logging.warning(ADFlowWagons.LOG_PREFIX .. "oprydning efter indlæsning: %s", tostring(err)) end
|
|
end
|
|
end
|
|
rememberTimer = rememberTimer + dt
|
|
buildingTimer = buildingTimer + dt
|
|
if rememberTimer >= ADFlowWagons.REMEMBER_MS then
|
|
rememberTimer = 0
|
|
rememberAll(adEnv)
|
|
retryWaiting(adEnv)
|
|
end
|
|
if buildingTimer >= ADFlowWagons.BUILDING_CHECK_MS then
|
|
buildingTimer = 0
|
|
checkBuildings(adEnv)
|
|
end
|
|
end
|