Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Nov 3, 2023
1 parent 4542eb3 commit 19f1133
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 87 deletions.
17 changes: 17 additions & 0 deletions scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,22 @@ 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
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)
Expand Down Expand Up @@ -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
Expand Down
31 changes: 7 additions & 24 deletions scripts/ai/jobs/CpAIJobBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ function CpAIJobBaleFinder:validate(farmId)
--------------------------------------------------------------

isValid, errorMessage = self:validateFieldPosition(isValid, errorMessage)

local fieldPolygon = self:getFieldPolygon()
--------------------------------------------------------------
--- Validate start distance to field, if started with the hud
--------------------------------------------------------------
if isValid and self.isDirectStart then
--- 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
Expand All @@ -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")
Expand Down Expand Up @@ -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
44 changes: 15 additions & 29 deletions scripts/ai/jobs/CpAIJobCombineUnloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
-------------------------------------
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
------------------------------------
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions scripts/ai/jobs/CpAIJobFieldWork.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down
11 changes: 1 addition & 10 deletions scripts/ai/tasks/CpAITaskBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions scripts/ai/tasks/CpAITaskCombineUnloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions scripts/ai/tasks/CpAITaskFieldWork.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 19f1133

Please sign in to comment.