-- 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 -- 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 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