diff --git a/scripts/ai/SelfUnloadHelper.lua b/scripts/ai/SelfUnloadHelper.lua index 69d56988..01e65eeb 100644 --- a/scripts/ai/SelfUnloadHelper.lua +++ b/scripts/ai/SelfUnloadHelper.lua @@ -35,6 +35,10 @@ SelfUnloadHelper.maxDistanceFromField = 20 ---@return table|nil best fill node of trailer ---@return number|nil distance of trailer from myVehicle function SelfUnloadHelper:findBestTrailer(fieldPolygon, myVehicle, implementWithPipe, pipeOffsetX) + if fieldPolygon == nil or #fieldPolygon == 0 then + CpUtil.errorVehicle(myVehicle, 'Field polygon is nil or empty, can\'t find a trailer to unload to') + return nil + end local bestTrailer, bestFillUnitIndex, bestFillType local minDistance = math.huge for _, otherVehicle in pairs(g_currentMission.vehicleSystem.vehicles) do @@ -125,7 +129,6 @@ end ---@param fillRootNode number|nil optional fill node for the trailer, must not be nil if bestTrailer is not nil function SelfUnloadHelper:getTargetParameters(fieldPolygon, myVehicle, implementWithPipe, objectWithPipeAttributes, bestTrailer, fillRootNode) - if not bestTrailer then -- no trailer passed in, let's find one bestTrailer, fillRootNode = SelfUnloadHelper:findBestTrailer(fieldPolygon, diff --git a/scripts/ai/strategies/AIDriveStrategyCombineCourse.lua b/scripts/ai/strategies/AIDriveStrategyCombineCourse.lua index 841d1a56..dda0eeda 100644 --- a/scripts/ai/strategies/AIDriveStrategyCombineCourse.lua +++ b/scripts/ai/strategies/AIDriveStrategyCombineCourse.lua @@ -76,10 +76,6 @@ AIDriveStrategyCombineCourse.isAAIDriveStrategyCombineCourse = true function AIDriveStrategyCombineCourse:init(task, job) AIDriveStrategyFieldWorkCourse.init(self, task, job) AIDriveStrategyCourse.initStates(self, AIDriveStrategyCombineCourse.myStates) - --- Combine needs a field polygon for the self-unload to work. Although it is a user setting, but it can be - --- changed during the work, so we always require the field polygon, regardless of the setting, as it is checked - --- only after starting the CP driver. - self.state = self.states.WAITING_FOR_FIELD_BOUNDARY_DETECTION self.fruitLeft, self.fruitRight = 0, 0 self.litersPerMeter = 0 self.litersPerSecond = 0 @@ -243,17 +239,20 @@ function AIDriveStrategyCombineCourse:getDriveData(dt, vX, vY, vZ) self:setMaxSpeed(0) end - if self.state == self.states.INITIAL and self.vehicle:cpGetFieldPolygon() == nil then - self:setMaxSpeed(0) - self.state = self.states.WAITING_FOR_FIELD_BOUNDARY_DETECTION + --- Combine may need the field polygon for the self-unload to work, so if by now we don't have one + --- and the field boundary detection is not running, start it. This can happen if the combine wasn't started + --- directly, such as as it was driving to the field and then switched strategy + if not self.vehicle:cpIsFieldBoundaryDetectionRunning() and self.vehicle:cpGetFieldPolygon() == nil then self:startFieldBoundaryDetection() end - if self.state == self.states.WAITING_FOR_FIELD_BOUNDARY_DETECTION then - self:setMaxSpeed(0) + if self.vehicle:cpIsFieldBoundaryDetectionRunning() then + if self.settings.selfUnload:getValue() then + -- don't have the field boundary yet, and self unload is selected, don't move + self:setMaxSpeed(0) + end if self:waitForFieldBoundary() then self:debug('Have field boundary now.') - self.state = self.states.INITIAL end elseif self.state == self.states.WORKING then -- Harvesting