Files
fs25-adsmartpickup/FS25_ADSmartPickup/adLoadDiag.lua
T
masterdraco 58edfcd548 feat: Alt automatisk laver planen om ved ændret behov (>10 %, hvert 5. min); pålæsnings-diagnose i traktorens retning — 1.62.292
Brugerspørgsmål 17/10: flere dyr -> genberegnes forbruget? Nu: tickNeeds
sammenligner behov i ha pr. vare mod sidste plan; >10 % eller nyt/forsvundet
behov -> applyAll(enableIds={}, replan) — spillerens Fra-marker og egne mål
røres ikke. Diagnose: triggerens midte (bounding-sfære) set fra tankens
exactFillRootNode i traktorens retning (+X = venstre i Giants); adGates
genindsat efter uheld i patch (review).
2026-10-17 06:58:14 +02:00

240 lines
14 KiB
Lua

-- AD Load Diag
-- Diagnose når AutoDrive står ved et LOAD-mål uden at fylde (in-game 30/9: Fendt + MEGA-sprøjte ved "Market Price
-- Liquid Fill Station"). AutoDrive fylder kun, når en fyldenhed står i triggerens boks (trigger.fillableObjects) og
-- triggeren har varen. Efter REPORT_MS ved målet uden fremgang skrives én linje: markør, vare, og for hver vogn den
-- nærmeste AD-læssetrigger (afstand, har varen?, står vognen i boksen?). Kun når en trigger med varen er inden for
-- NEAR_M (holder ved Wait-punkter logges ikke). Ingen indgreb — kun log.
ADLoadDiag = {}
ADLoadDiag.LOG_PREFIX = "ADSmartPickup: pålæsning: "
-- før udkørslens "intet at læsse" (30 s), ellers når beskeden aldrig ud (in-game 10/10)
ADLoadDiag.REPORT_MS = 25000
ADLoadDiag.NEAR_M = 20 -- m: trigger med varen herinden for = køretøjet står ved stationen (ellers Wait/mark: ingen log)
-- ---- ren logik ----
-- state (pr. opgave), dt, reached (ved målet), level (liter på vognene) -> true når der skal rapporteres (én gang pr.
-- ankomst). Fremgang eller kørsel nulstiller.
function ADLoadDiag.tick(state, dt, reached, level)
if not reached then
state.ms, state.reported, state.level = 0, false, nil
return false
end
if state.level == nil or level > state.level then
state.level, state.ms = level, 0
return false
end
state.ms = (state.ms or 0) + (dt or 0)
if state.reported or state.ms < ADLoadDiag.REPORT_MS then return false end
state.reported = true
return true
end
-- distances = afstande fra vognene til nærmeste AD-trigger med varen -> står ved stationen?
function ADLoadDiag.atStation(distances)
for _, d in ipairs(distances or {}) do
if d <= ADLoadDiag.NEAR_M then return true end
end
return false
end
-- ---- spil ----
local fillTypeName = ADUtil.fillTypeName
local function inBox(trigger, trailer)
for key, entry in pairs(trigger.fillableObjects or {}) do
if key == trailer or (type(entry) == "table" and entry.object == trailer) then return true end
end
return false
end
-- Ren: hvor triggerens midte ligger set fra vognens påfyldningspunkt i TRAKTORENS retning (x til siden, y op/ned,
-- z frem/tilbage) -> læsbar tekst, der siger hvilken vej sporet/markøren skal rykkes (in-game 17/10: Supercis 800 ved
-- 'Slurry Tank' stod 0,4 m fra triggeren langs vejen, men 1,5 m til siden — ikke i boksen)
function ADLoadDiag.describeOffset(x, y, z, radius)
if x == nil or y == nil or z == nil then return nil end
local parts = {}
-- Giants: Y op, Z frem, højrehåndet -> +X peger til VENSTRE for traktoren (venstre hjul har positiv x i køretøjs-XML)
if math.abs(x) >= 0.05 then table.insert(parts, string.format("%.1f m til %s", math.abs(x), x > 0 and "venstre" or "højre")) end
if math.abs(z) >= 0.05 then table.insert(parts, string.format("%.1f m %s", math.abs(z), z < 0 and "bagud" or "frem")) end
if math.abs(y) >= 0.05 then table.insert(parts, string.format("%.1f m %s", math.abs(y), y < 0 and "lavere" or "højere")) end
local where = #parts > 0 and table.concat(parts, ", ") or "lige dér"
return string.format("triggerens midte ligger %s i forhold til tankens påfyldningspunkt%s", where,
radius ~= nil and string.format(" (triggerens radius %.1f m)", radius) or "")
end
-- Triggerens midte (bounding-sfærens centrum, ellers nodens nulpunkt) set fra den første fyldenheds exactFillRootNode,
-- i traktorens retning.
local function fillOffset(trigger, trailer, fillType, vehicle)
local node = trigger.triggerNode
if node == nil or trailer.getFillUnits == nil or vehicle == nil or vehicle.rootNode == nil then return nil end
for i = 1, #(trailer:getFillUnits() or {}) do
if trailer.getFillUnitSupportsFillType == nil or trailer:getFillUnitSupportsFillType(i, fillType) then
local fillNode = trailer.getFillUnitExactFillRootNode ~= nil and trailer:getFillUnitExactFillRootNode(i) or nil
if fillNode ~= nil then
local fx, fy, fz = getWorldTranslation(fillNode)
local cx, cy, cz, radius = 0, 0, 0, nil
if getShapeBoundingSphere ~= nil then
local okR, sx, sy, sz, r = pcall(getShapeBoundingSphere, node)
if okR and type(r) == "number" then
radius = r
if type(sx) == "number" and type(sy) == "number" and type(sz) == "number" then cx, cy, cz = sx, sy, sz end
end
end
local tx, ty, tz = localToWorld(node, cx, cy, cz)
local x, _, z = worldDirectionToLocal(vehicle.rootNode, tx - fx, 0, tz - fz)
return ADLoadDiag.describeOffset(x, ty - fy, z, radius)
end
end
end
return nil
end
-- AD's egne porte for pålæsning ved triggeren (AutoDrive.getTriggerAndTrailerPairs + tryLoadingAtTrigger): pr. fyldenhed
-- understøtter/tillader varen, fri plads, varevalget matcher, vognen i triggerens liste, kildens niveau, trigger i gang.
local function adGates(adEnv, vehicle, trailer, trigger, fillType)
local autoDrive = adEnv.AutoDrive
local parts = {}
local farmId = vehicle.getActiveFarm ~= nil and vehicle:getActiveFarm() or nil
local sourceLevel = nil
if trigger.source ~= nil and trigger.source.getAllFillLevels ~= nil then
local ok, levels = pcall(trigger.source.getAllFillLevels, trigger.source, farmId)
if ok and type(levels) == "table" then sourceLevel = levels[fillType] end
end
table.insert(parts, string.format("kilde(%s)=%s", tostring(farmId), tostring(sourceLevel)))
table.insert(parts, "trigger.isLoading=" .. tostring(trigger.isLoading))
-- spillets LoadTrigger starter kun med et gyldigt fyldobjekt (onFillTypeSelection -> setIsLoading)
local valid = trigger.validFillableObject
table.insert(parts, string.format("valid=%s/%s autoStart=%s", tostring(valid ~= nil and (valid.getName ~= nil and valid:getName() or "ja") or nil),
tostring(trigger.validFillableFillUnitIndex), tostring(trigger.autoStart)))
local okA, activatable = pcall(function() return trigger.getIsActivatable ~= nil and trigger:getIsActivatable(trailer) end)
table.insert(parts, "aktiverbar=" .. (okA and tostring(activatable) or "fejl"))
local selected = vehicle.ad.stateModule.getSelectedFillTypes ~= nil and vehicle.ad.stateModule:getSelectedFillTypes() or {}
for i = 1, #(trailer:getFillUnits() or {}) do
local supports = trailer:getFillUnitSupportsFillType(i, fillType)
local allows = trailer.getFillUnitAllowsFillType == nil or trailer:getFillUnitAllowsFillType(i, fillType)
local okM, match = pcall(autoDrive.fillTypesMatch, vehicle, trigger, trailer, selected, i)
local okL, inList = pcall(autoDrive.trailerIsInTriggerList, trailer, trigger, i)
local okT, viaTrigger = pcall(function() return ToolType ~= nil and trailer.getFillUnitSupportsToolType ~= nil and trailer:getFillUnitSupportsToolType(i, ToolType.TRIGGER) end)
table.insert(parts, string.format("enhed%d: understøtter=%s tillader=%s fri=%.0f varevalg=%s iListe=%s trigger-værktøj=%s", i, tostring(supports), tostring(allows),
trailer:getFillUnitFreeCapacity(i) or -1, okM and tostring(match) or "fejl", okL and tostring(inList) or "fejl", okT and tostring(viaTrigger) or "fejl"))
end
local tm = vehicle.ad.trailerModule
if tm ~= nil then
table.insert(parts, string.format("AD: isLoading=%s trigger=%s activeAtTrigger=%s startforsøg=%s", tostring(tm.isLoading), tostring(tm.trigger ~= nil),
tostring(tm.isActiveAtTrigger ~= nil and tm:isActiveAtTrigger()), tostring(tm.countLoading)))
end
-- LoadAtDestinationTask afslutter straks ved rotation "kun pålæsning"/"fortsæt ved tom silo" — medmindre modden holder vognen
local okR, rotate = pcall(autoDrive.getSetting, "rotateTargets", vehicle)
local okC, continueEmpty = pcall(autoDrive.getSetting, "continueOnEmptySilo", vehicle)
local okW, wait = pcall(function() return ADSmartPickup ~= nil and ADSmartPickup.shouldWaitForLoading ~= nil and ADSmartPickup.shouldWaitForLoading(vehicle) end)
table.insert(parts, string.format("rotér=%s fortsætTom=%s smartHolder=%s", okR and tostring(rotate) or "?", okC and tostring(continueEmpty) or "?", okW and tostring(wait) or "?"))
-- AD's egen beslutningskæde: autoload-gren (aldrig trigger), "giv videre til CP", og hvad AD selv finder af par
local stateModule = vehicle.ad.stateModule
local okH, canRestart = pcall(function() return stateModule.getCanRestartHelper ~= nil and stateModule:getCanRestartHelper() end)
table.insert(parts, string.format("hasAL=%s cpOvertag=%s mode=%s", tostring(vehicle.ad.hasAL), okH and tostring(canRestart) or "?",
tostring(stateModule.getMode ~= nil and stateModule:getMode() or "?")))
local okP, pairs_ = pcall(autoDrive.getTriggerAndTrailerPairs, vehicle, 1000)
if okP and type(pairs_) == "table" then
local found = {}
for _, pair in pairs(pairs_) do
table.insert(found, string.format("%s/enhed%s fyld=%s", tostring(pair.trailer ~= nil and pair.trailer.getName ~= nil and pair.trailer:getName() or "?"),
tostring(pair.fillUnitIndex), tostring(pair.hasFill)))
end
table.insert(parts, "ADpar=" .. (#found > 0 and table.concat(found, ",") or "ingen"))
else
table.insert(parts, "ADpar=fejl: " .. tostring(pairs_))
end
return table.concat(parts, " ")
end
local function describeTrailer(adEnv, trailer, fillType, vehicle)
local node = trailer.components ~= nil and trailer.components[1] ~= nil and trailer.components[1].node or trailer.rootNode
if node == nil then return nil end
local tx, _, tz = getWorldTranslation(node)
local nearest, nearestOk = nil, nil
for _, trigger in pairs(adEnv.ADTriggerManager.getLoadTriggers() or {}) do
local x, _, z = adEnv.ADTriggerManager.getTriggerPos(trigger)
if x ~= nil and (x ~= 0 or z ~= 0) then
local d = math.sqrt((x - tx) ^ 2 + (z - tz) ^ 2)
local ok = adEnv.ADTriggerManager.triggerSupportsFillType(trigger, fillType)
if nearest == nil or d < nearest.d then nearest = {d = d, ok = ok, trigger = trigger} end
if ok and (nearestOk == nil or d < nearestOk.d) then nearestOk = {d = d, trigger = trigger} end
end
end
local name = trailer.getName ~= nil and trailer:getName() or "?"
local parts = {}
if nearest ~= nil then
table.insert(parts, string.format("nærmeste AD-trigger %.1f m (%s varen, %s)", nearest.d, nearest.ok and "har" or "UDEN",
inBox(nearest.trigger, trailer) and "vognen i boksen" or "vognen IKKE i boksen"))
end
if nearestOk ~= nil and (nearest == nil or nearestOk.trigger ~= nearest.trigger) then
table.insert(parts, string.format("nærmeste med varen %.1f m (%s)", nearestOk.d,
inBox(nearestOk.trigger, trailer) and "i boksen" or "ikke i boksen"))
end
if nearestOk == nil then table.insert(parts, "INGEN AD-læssetrigger med varen") end
if nearestOk ~= nil and not inBox(nearestOk.trigger, trailer) then
local okO, offset = pcall(fillOffset, nearestOk.trigger, trailer, fillType, vehicle)
if okO and offset ~= nil then table.insert(parts, offset) end
end
if nearestOk ~= nil and vehicle ~= nil and trailer.getFillUnits ~= nil then
local okG, gates = pcall(adGates, adEnv, vehicle, trailer, nearestOk.trigger, fillType)
table.insert(parts, "[" .. (okG and gates or ("fejl: " .. tostring(gates))) .. "]")
end
return string.format("'%s': %s", tostring(name), table.concat(parts, ", ")), nearestOk ~= nil and nearestOk.d or nil
end
local function report(adEnv, task)
local vehicle = task.vehicle
local fillType = vehicle.ad.stateModule.getFillType ~= nil and vehicle.ad.stateModule:getFillType() or nil
local marker = task.destinationID ~= nil and adEnv.ADGraphManager:getMapMarkerByWayPointId(task.destinationID) or nil
local lines, distances = {}, {}
for _, trailer in ipairs(task.trailers or {}) do
if trailer.getFillUnits ~= nil and #trailer:getFillUnits() > 0 then
local ok, line, distance = pcall(describeTrailer, adEnv, trailer, fillType, vehicle)
if ok and line ~= nil then table.insert(lines, line) end
if ok and distance ~= nil then table.insert(distances, distance) end
end
end
-- holder ved Wait/mark (ingen trigger med varen tæt på): det er ikke en fejlet pålæsning
if not ADLoadDiag.atStation(distances) then return end
local maxDistance = adEnv.AutoDrive.getMaxTriggerDistance ~= nil and adEnv.AutoDrive.getMaxTriggerDistance(vehicle) or nil
Logging.warning(ADLoadDiag.LOG_PREFIX .. "'%s' står ved '%s' uden at fylde %s i %d s (AD-triggerafstand %s m): %s",
tostring(ADRuns.vehicleLabel(vehicle)), tostring(marker ~= nil and marker.name or task.destinationID), fillTypeName(fillType),
math.floor(ADLoadDiag.REPORT_MS / 1000), tostring(maxDistance), #lines > 0 and table.concat(lines, "; ") or "ingen vogne med fyldenheder")
end
local states = setmetatable({}, {__mode = "k"})
function ADLoadDiag.after(adEnv, task, dt)
local vehicle = task.vehicle
if vehicle == nil or vehicle.ad == nil or vehicle.ad.drivePathModule == nil then return end
local state = states[task]
if state == nil then
state = {}
states[task] = state
end
local reached = vehicle.ad.drivePathModule:isTargetReached() and not vehicle.ad.trailerModule:isActiveAtTrigger()
local level = adEnv.AutoDrive.getAllFillLevels(task.trailers) or 0
if ADLoadDiag.tick(state, dt, reached, level) then report(adEnv, task) end
end
local isInstalled = false
function ADLoadDiag.install(adEnv)
local task = adEnv ~= nil and adEnv.LoadAtDestinationTask or nil
if isInstalled or task == nil or task.update == nil or adEnv.ADTriggerManager == nil then return end
isInstalled = true
local original = task.update
task.update = function(self, dt)
local result = original(self, dt)
local ok, err = pcall(ADLoadDiag.after, adEnv, self, dt)
if not ok and not self.spuDiagFailed then
self.spuDiagFailed = true
Logging.warning(ADLoadDiag.LOG_PREFIX .. "diagnose fejlede: %s", tostring(err))
end
return result
end
end