-- AD CP Field Guard -- Courseplays tømmevogn (combine unloader) starter med at vente på markens grænse (WAITING_FOR_FIELD_BOUNDARY_DETECTION). -- Ligger punktet, grænsen måles fra, uden for marken (fx F-markøren ved vejen), findes der ingen mark, og vognen venter for -- evigt (in-game 19/10 00:14-00:26: 'Ballesamler 2' på F6 stod i "Field detection", mens Mejetærsker 2 meldte "Needs to -- be unloaded"). Efter STUCK_MS måles grænsen igen fra et punkt inde på marken (CP's egen cpDetectFieldBoundary); hjælper -- det ikke, får spilleren besked. Ren logik øverst, spil-adapter nederst. -- info-log bag "Detaljeret log" (ADLog i adSettings.lua; uden ADLog — tests — direkte til Logging) local function logInfo(...) return (ADLog or Logging).info(...) end ADCpFieldGuard = {} ADCpFieldGuard.LOG_PREFIX = "ADSmartPickup: markgrænse: " ADCpFieldGuard.TICK_MS = 5000 ADCpFieldGuard.STUCK_MS = 120000 ADCpFieldGuard.GRID = 8 -- søgenet over omridsets kasse, når midtpunktet ligger uden for marken -- ---- ren logik ---- -- verts = {{x, z}}; inside(x, z, verts) -> bool. Et punkt inde på marken: midtpunktet, ellers første punkt i et net. function ADCpFieldGuard.interiorPoint(verts, inside) if verts == nil or #verts < 3 or inside == nil then return nil end local sx, sz, minX, maxX, minZ, maxZ = 0, 0, math.huge, -math.huge, math.huge, -math.huge for _, v in ipairs(verts) do sx, sz = sx + v.x, sz + v.z minX, maxX, minZ, maxZ = math.min(minX, v.x), math.max(maxX, v.x), math.min(minZ, v.z), math.max(maxZ, v.z) end local cx, cz = sx / #verts, sz / #verts if inside(cx, cz, verts) then return cx, cz end local n = ADCpFieldGuard.GRID for i = 1, n - 1 do for j = 1, n - 1 do local x, z = minX + (maxX - minX) * i / n, minZ + (maxZ - minZ) * j / n if inside(x, z, verts) then return x, z end end end -- smalle marker (strimler): 2 m ind fra midten af hver kant for k = 1, #verts do local a, b = verts[k], verts[k % #verts + 1] local mx, mz = (a.x + b.x) / 2, (a.z + b.z) / 2 local dx, dz = b.x - a.x, b.z - a.z local len = math.sqrt(dx * dx + dz * dz) if len > 0 then for _, sign in ipairs({1, -1}) do local x, z = mx - sign * dz / len * 2, mz + sign * dx / len * 2 if inside(x, z, verts) then return x, z end end end end return nil end -- s = {waitingMs, retried, running, told} -> "detect" | "notify" | "none" function ADCpFieldGuard.step(s) if s.running or (s.waitingMs or 0) < ADCpFieldGuard.STUCK_MS then return "none" end if not s.retried then return "detect" end if not s.told then return "notify" end return "none" end -- ---- spil-adapter ---- local timerMs = 0 local watch = setmetatable({}, {__mode = "k"}) -- køretøj -> {since, retried, told} local function isWaitingForBoundary(vehicle) if vehicle.getCpDriveStrategy == nil then return false end local strategy = vehicle:getCpDriveStrategy() return strategy ~= nil and strategy.states ~= nil and strategy.state ~= nil and strategy.state == strategy.states.WAITING_FOR_FIELD_BOUNDARY_DETECTION end local function checkVehicle(vehicle, fieldId, now) if not isWaitingForBoundary(vehicle) then watch[vehicle] = nil return end local w = watch[vehicle] if w == nil then w = {since = now} watch[vehicle] = w end local running = vehicle.cpIsFieldBoundaryDetectionRunning ~= nil and vehicle:cpIsFieldBoundaryDetectionRunning() local action = ADCpFieldGuard.step({waitingMs = now - w.since, retried = w.retried, running = running, told = w.told}) local label = ADRuns ~= nil and ADRuns.vehicleLabel(vehicle) or "?" if action == "detect" then local info = ADFields.cache ~= nil and ADFields.cache[fieldId] or nil local x, z = ADCpFieldGuard.interiorPoint(info ~= nil and info.verts or nil, ADFieldJobs.insidePolygon) w.retried, w.since = true, now if x == nil or vehicle.cpDetectFieldBoundary == nil then logInfo(ADCpFieldGuard.LOG_PREFIX .. "'%s' venter på F%s's grænse — kan ikke måle igen: %s", label, tostring(fieldId), x == nil and "intet punkt inde på marken kendes" or "Courseplays cpDetectFieldBoundary findes ikke") return end vehicle:cpDetectFieldBoundary(x, z) logInfo(ADCpFieldGuard.LOG_PREFIX .. "'%s' har ventet %d s på F%s's grænse — måles igen fra et punkt inde på marken (%.0f, %.0f)", label, math.floor(ADCpFieldGuard.STUCK_MS / 1000), tostring(fieldId), x, z) elseif action == "notify" then w.told = true local text = string.format("'%s' kan ikke finde F%s's grænse (Courseplay) — kør den ind på marken eller tøm høsteren selv", label, tostring(fieldId)) local polygon = vehicle.cpGetFieldPolygon ~= nil and vehicle:cpGetFieldPolygon() or nil logInfo(ADCpFieldGuard.LOG_PREFIX .. "%s (ny måling: kører=%s, grænse fundet=%s)", text, tostring(running), tostring(polygon ~= nil)) if ADFieldJobs.notify ~= nil then ADFieldJobs.notify(text, true) end end end function ADCpFieldGuard.tick(dt) timerMs = timerMs + (dt or 0) if timerMs < ADCpFieldGuard.TICK_MS then return end local elapsed = timerMs timerMs = 0 if ADFieldJobs == nil or ADFieldJobs.state == nil or ADFields == nil then return end ADCpFieldGuard.clock = (ADCpFieldGuard.clock or 0) + elapsed for _, assignment in pairs(ADFieldJobs.state.assignments or {}) do local vehicle = assignment.vehicle if vehicle ~= nil and assignment.fieldId ~= nil then local ok, err = pcall(checkVehicle, vehicle, assignment.fieldId, ADCpFieldGuard.clock) if not ok then Logging.warning(ADCpFieldGuard.LOG_PREFIX .. "%s", tostring(err)) end end end end