From 19f1133585b71e88dcbfabe8c2ad562582f5aebe Mon Sep 17 00:00:00 2001 From: schwiti6190 <58079399+schwiti6190@users.noreply.github.com> Date: Fri, 3 Nov 2023 21:53:02 +0100 Subject: [PATCH] WIP --- scripts/ai/jobs/CpAIJob.lua | 17 ++++++++ scripts/ai/jobs/CpAIJobBaleFinder.lua | 31 ++++---------- scripts/ai/jobs/CpAIJobCombineUnloader.lua | 44 +++++++------------- scripts/ai/jobs/CpAIJobFieldWork.lua | 16 +++---- scripts/ai/tasks/CpAITaskBaleFinder.lua | 11 +---- scripts/ai/tasks/CpAITaskCombineUnloader.lua | 11 +---- scripts/ai/tasks/CpAITaskFieldWork.lua | 7 +--- 7 files changed, 50 insertions(+), 87 deletions(-) diff --git a/scripts/ai/jobs/CpAIJob.lua b/scripts/ai/jobs/CpAIJob.lua index 781199629..aae57cf0f 100644 --- a/scripts/ai/jobs/CpAIJob.lua +++ b/scripts/ai/jobs/CpAIJob.lua @@ -276,6 +276,12 @@ end function CpAIJob:writeStream(streamId, connection) CpAIJob:superClass().writeStream(self, streamId, connection) + if self.fieldPolygon then + streamWriteBool(streamId, true) + CustomField.writeStreamVertices(self.fieldPolygon, streamId, connection) + else + streamWriteBool(streamId, false) + end if self.cpJobParameters then self.cpJobParameters:writeStream(streamId, connection) end @@ -283,6 +289,9 @@ end function CpAIJob:readStream(streamId, connection) CpAIJob:superClass().readStream(self, streamId, connection) + if streamReadBool(streamId) then + self.fieldPolygon = CustomField.readStreamVertices(streamId, connection) + end if self.cpJobParameters then self.cpJobParameters:validateSettings() self.cpJobParameters:readStream(streamId, connection) @@ -310,6 +319,14 @@ function CpAIJob:getCpJobParameters() return self.cpJobParameters end +function CpAIJob:getFieldPolygon() + return self.fieldPolygon +end + +function CpAIJob:setFieldPolygon(polygon) + self.fieldPolygon = polygon +end + --- Can the job be started? function CpAIJob:getCanStartJob() return true diff --git a/scripts/ai/jobs/CpAIJobBaleFinder.lua b/scripts/ai/jobs/CpAIJobBaleFinder.lua index fe4fc6c3b..4e509d8ec 100644 --- a/scripts/ai/jobs/CpAIJobBaleFinder.lua +++ b/scripts/ai/jobs/CpAIJobBaleFinder.lua @@ -74,7 +74,7 @@ function CpAIJobBaleFinder:validate(farmId) -------------------------------------------------------------- isValid, errorMessage = self:validateFieldPosition(isValid, errorMessage) - + local fieldPolygon = self:getFieldPolygon() -------------------------------------------------------------- --- Validate start distance to field, if started with the hud -------------------------------------------------------------- @@ -82,8 +82,8 @@ function CpAIJobBaleFinder:validate(farmId) --- Checks the distance for starting with the hud, as a safety check. --- Firstly check, if the vehicle is near the field. local x, _, z = getWorldTranslation(vehicle.rootNode) - isValid = CpMathUtil.isPointInPolygon(self.fieldPolygon, x, z) or - CpMathUtil.getClosestDistanceToPolygonEdge(self.fieldPolygon, x, z) < self.minStartDistanceToField + isValid = CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or + CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < self.minStartDistanceToField if not isValid then return false, g_i18n:getText("CP_error_vehicle_too_far_away_from_field") end @@ -98,11 +98,11 @@ function CpAIJobBaleFinder:validateFieldPosition(isValid, errorMessage) if tx == nil or tz == nil then return false, g_i18n:getText("CP_error_not_on_field") end - local _ - self.fieldPolygon, _ = CpFieldUtil.getFieldPolygonAtWorldPosition(tx, tz) - self.hasValidPosition = self.fieldPolygon ~= nil + local fieldPolygon, _ = CpFieldUtil.getFieldPolygonAtWorldPosition(tx, tz) + self:setFieldPolygon(fieldPolygon) + self.hasValidPosition = fieldPolygon ~= nil if self.hasValidPosition then - self.selectedFieldPlot:setWaypoints(self.fieldPolygon) + self.selectedFieldPlot:setWaypoints(fieldPolygon) self.selectedFieldPlot:setVisible(true) else return false, g_i18n:getText("CP_error_not_on_field") @@ -130,20 +130,3 @@ function CpAIJobBaleFinder:getDescription() end return desc end - -function CpAIJobBaleFinder:readStream(streamId, connection) - CpAIJob.readStream(self, streamId, connection) - if streamReadBool(streamId) then - self.fieldPolygon = CustomField.readStreamVertices(streamId, connection) - end -end - -function CpAIJobBaleFinder:writeStream(streamId, connection) - CpAIJob.writeStream(self, streamId, connection) - if self.fieldPolygon then - streamWriteBool(streamId, true) - CustomField.writeStreamVertices(self.fieldPolygon, streamId, connection) - else - streamWriteBool(streamId, false) - end -end \ No newline at end of file diff --git a/scripts/ai/jobs/CpAIJobCombineUnloader.lua b/scripts/ai/jobs/CpAIJobCombineUnloader.lua index 57ff79fc0..26f03c52d 100644 --- a/scripts/ai/jobs/CpAIJobCombineUnloader.lua +++ b/scripts/ai/jobs/CpAIJobCombineUnloader.lua @@ -65,7 +65,7 @@ function CpAIJobCombineUnloader:getCanStartJob() return self.hasValidPosition end ----@param vehicle Vehicle +---@param vehicle table ---@param mission Mission ---@param farmId number ---@param isDirectStart boolean disables the drive to by giants @@ -124,10 +124,11 @@ function CpAIJobCombineUnloader:validateFieldPosition(isValid, errorMessage) return false, g_i18n:getText("CP_error_not_on_field") end local _ - self.fieldPolygon, _ = CpFieldUtil.getFieldPolygonAtWorldPosition(tx, tz) - self.hasValidPosition = self.fieldPolygon ~= nil + local fieldPolygon, _ = CpFieldUtil.getFieldPolygonAtWorldPosition(tx, tz) + self:setFieldPolygon(fieldPolygon) + self.hasValidPosition = fieldPolygon ~= nil if self.hasValidPosition then - self.selectedFieldPlot:setWaypoints(self.fieldPolygon) + self.selectedFieldPlot:setWaypoints(fieldPolygon) self.selectedFieldPlot:setVisible(true) else return false, g_i18n:getText("CP_error_not_on_field") @@ -156,6 +157,7 @@ function CpAIJobCombineUnloader:validate(farmId) if not isValid then return isValid, errorMessage end + local fieldPolygon = self:getFieldPolygon() ------------------------------------ --- Validate start distance to field ------------------------------------- @@ -167,13 +169,13 @@ function CpAIJobCombineUnloader:validate(farmId) --- Checks the distance for starting with the hud, as a safety check. --- Firstly check, if the vehicle is near the field. local x, _, z = getWorldTranslation(vehicle.rootNode) - isValid = CpMathUtil.isPointInPolygon(self.fieldPolygon, x, z) or - CpMathUtil.getClosestDistanceToPolygonEdge(self.fieldPolygon, x, z) < self.minStartDistanceToField + isValid = CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or + CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < self.minStartDistanceToField if not isValid and useGiantsUnload then --- Alternatively check, if the start marker is close to the field and giants unload is active. local x, z = self.cpJobParameters.startPosition:getPosition() - isValid = CpMathUtil.isPointInPolygon(self.fieldPolygon, x, z) or - CpMathUtil.getClosestDistanceToPolygonEdge(self.fieldPolygon, x, z) < self.minStartDistanceToField + isValid = CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or + CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < self.minStartDistanceToField if not isValid then return false, g_i18n:getText("CP_error_start_position_to_far_away_from_field") end @@ -209,8 +211,8 @@ function CpAIJobCombineUnloader:validate(farmId) if useFieldUnload then local x, z = self.cpJobParameters.fieldUnloadPosition:getPosition() - isValid = CpMathUtil.isPointInPolygon(self.fieldPolygon, x, z) or - CpMathUtil.getClosestDistanceToPolygonEdge(self.fieldPolygon, x, z) < self.minFieldUnloadDistanceToField + isValid = CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or + CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < self.minFieldUnloadDistanceToField if not isValid then return false, g_i18n:getText("CP_error_fieldUnloadPosition_too_far_away_from_field") end @@ -233,23 +235,6 @@ function CpAIJobCombineUnloader:drawSelectedField(map) self.heapPlot:draw(map) end -function CpAIJobCombineUnloader:readStream(streamId, connection) - CpAIJob.readStream(self, streamId, connection) - if streamReadBool(streamId) then - self.fieldPolygon = CustomField.readStreamVertices(streamId, connection) - end -end - -function CpAIJobCombineUnloader:writeStream(streamId, connection) - CpAIJob.writeStream(self, streamId, connection) - if self.fieldPolygon then - streamWriteBool(streamId, true) - CustomField.writeStreamVertices(self.fieldPolygon, streamId, connection) - else - streamWriteBool(streamId, false) - end -end - ------------------------------------ --- Giants unload ------------------------------------ @@ -344,9 +329,10 @@ function CpAIJobCombineUnloader:getStartTaskIndex() return startTask end local vehicle = self:getVehicle() + local fieldPolygon = self:getFieldPolygon() local x, _, z = getWorldTranslation(vehicle.rootNode) - if CpMathUtil.isPointInPolygon(self.fieldPolygon, x, z) or - CpMathUtil.getClosestDistanceToPolygonEdge(self.fieldPolygon, x, z) < 2*self.minStartDistanceToField then + if CpMathUtil.isPointInPolygon(fieldPolygon, x, z) or + CpMathUtil.getClosestDistanceToPolygonEdge(fieldPolygon, x, z) < 2*self.minStartDistanceToField then CpUtil.debugVehicle(CpDebug.DBG_FIELDWORK, vehicle, "Close to the field, start cp drive strategy.") return startTask end diff --git a/scripts/ai/jobs/CpAIJobFieldWork.lua b/scripts/ai/jobs/CpAIJobFieldWork.lua index 2d86750ca..639b2fea5 100644 --- a/scripts/ai/jobs/CpAIJobFieldWork.lua +++ b/scripts/ai/jobs/CpAIJobFieldWork.lua @@ -36,7 +36,7 @@ function CpAIJobFieldWork:setupJobParameters() self:setupCpJobParameters(CpJobParameters(self)) end ----@param vehicle Vehicle +---@param vehicle table ---@param mission Mission ---@param farmId number ---@param isDirectStart boolean disables the drive to by giants @@ -81,18 +81,17 @@ function CpAIJobFieldWork:validateFieldSetup(isValid, errorMessage) end self.hasValidPosition = false self.foundVines = nil - local isCustomField - self.fieldPolygon, isCustomField = CpFieldUtil.getFieldPolygonAtWorldPosition(tx, tz) - - if self.fieldPolygon then + local fieldPolygon, isCustomField = CpFieldUtil.getFieldPolygonAtWorldPosition(tx, tz) + self:setFieldPolygon(fieldPolygon) + if fieldPolygon then self.hasValidPosition = true - self.foundVines = g_vineScanner:findVineNodesInField(self.fieldPolygon, tx, tz, self.customField ~= nil) + self.foundVines = g_vineScanner:findVineNodesInField(fieldPolygon, tx, tz, self.customField ~= nil) if self.foundVines then CpUtil.debugVehicle(CpDebug.DBG_FIELDWORK, vehicle, "Found vine nodes, generating a vine field border.") self.fieldPolygon = g_vineScanner:getCourseGeneratorVertices(0, tx, tz) end - self.selectedFieldPlot:setWaypoints(self.fieldPolygon) + self.selectedFieldPlot:setWaypoints(fieldPolygon) self.selectedFieldPlot:setVisible(true) self.selectedFieldPlot:setBrightColor(true) if isCustomField then @@ -177,6 +176,7 @@ end --- Button callback to generate a field work course. function CpAIJobFieldWork:onClickGenerateFieldWorkCourse() local vehicle = self.vehicleParameter:getVehicle() + local fieldPolygon = self:getFieldPolygon() local settings = vehicle:getCourseGeneratorSettings() local tx, tz = self.cpJobParameters.fieldPosition:getPosition() local ok, course @@ -196,7 +196,7 @@ function CpAIJobFieldWork:onClickGenerateFieldWorkCourse() ) else - ok, course = CourseGeneratorInterface.generate(self.fieldPolygon, + ok, course = CourseGeneratorInterface.generate(fieldPolygon, { x = tx, z = tz }, settings.isClockwise:getValue(), settings.workWidth:getValue(), diff --git a/scripts/ai/tasks/CpAITaskBaleFinder.lua b/scripts/ai/tasks/CpAITaskBaleFinder.lua index b0454c8ad..97228f1e3 100644 --- a/scripts/ai/tasks/CpAITaskBaleFinder.lua +++ b/scripts/ai/tasks/CpAITaskBaleFinder.lua @@ -2,20 +2,11 @@ ---@class CpAIJobBaleFinder : CpAITask CpAITaskBaleFinder = CpObject(CpAITask) -function CpAITaskBaleFinder:reset() - CpAITask.reset(self) - self.fieldPolygon = nil -end - -function CpAIJobBaleFinder:setFieldPolygon(polygon) - self.fieldPolygon = polygon -end - function CpAITaskBaleFinder:start() if self.isServer then self:debug("CP bale finder task started.") local strategy = AIDriveStrategyFindBales(self, self.job) - strategy:setFieldPolygon(self.job.fieldPolygon) + strategy:setFieldPolygon(self.job:getFieldPolygon()) strategy:setAIVehicle(self.vehicle, self.job:getCpJobParameters()) end CpAITask.start(self) diff --git a/scripts/ai/tasks/CpAITaskCombineUnloader.lua b/scripts/ai/tasks/CpAITaskCombineUnloader.lua index ecde06b39..76dcacee2 100644 --- a/scripts/ai/tasks/CpAITaskCombineUnloader.lua +++ b/scripts/ai/tasks/CpAITaskCombineUnloader.lua @@ -2,20 +2,11 @@ ---@class CpAITaskCombineUnloader : CpAITask CpAITaskCombineUnloader = CpObject(CpAITask) -function CpAITaskCombineUnloader:reset() - CpAITask.reset(self) - self.fieldPolygon = nil -end - -function CpAITaskCombineUnloader:setFieldPolygon(polygon) - self.fieldPolygon = polygon -end - function CpAITaskCombineUnloader:start() if self.isServer then self:debug("CP combine unloader task started.") local strategy = AIDriveStrategyUnloadCombine(self, self.job) - strategy:setFieldPolygon(self.fieldPolygon) + strategy:setFieldPolygon(self.job:getFieldPolygon()) strategy:setAIVehicle(self.vehicle, self.job:getCpJobParameters()) self.vehicle:startCpWithStrategy(strategy) end diff --git a/scripts/ai/tasks/CpAITaskFieldWork.lua b/scripts/ai/tasks/CpAITaskFieldWork.lua index b83e6402a..5aa69c031 100644 --- a/scripts/ai/tasks/CpAITaskFieldWork.lua +++ b/scripts/ai/tasks/CpAITaskFieldWork.lua @@ -4,14 +4,9 @@ CpAITaskFieldWork = CpObject(CpAITask) function CpAITaskFieldWork:reset() self.startPosition = nil - self.fieldPolygon = nil CpAITask.reset(self) end -function CpAITaskFieldWork:setFieldPolygon(polygon) - self.fieldPolygon = polygon -end - function CpAITaskFieldWork:setStartPosition(startPosition) self.startPosition = startPosition end @@ -54,7 +49,7 @@ function CpAITaskFieldWork:start() cpDriveStrategy = AIDriveStrategyFieldWorkCourse(self, self.job) end end - cpDriveStrategy:setFieldPolygon(self.fieldPolygon) + cpDriveStrategy:setFieldPolygon(self.job:getFieldPolygon()) cpDriveStrategy:setAIVehicle(self.vehicle, self.job:getCpJobParameters()) cpSpec.driveStrategy = cpDriveStrategy --- Only the last driving strategy can stop the helper, while it is running.