Skip to content

Commit

Permalink
fix: plow rotation on headland
Browse files Browse the repository at this point in the history
  • Loading branch information
pvaiko committed Sep 22, 2024
1 parent e89bac9 commit 4318a6a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
16 changes: 16 additions & 0 deletions scripts/Course.lua
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,22 @@ function Course:isOnHeadland(ix, n, boundaryId)
end
end

---@param ix number
---@return boolean|nil true if ix is on a clockwise headland (around the field, or around an island). False if
--- it is on a counterclockwise headland, and nil, if is is not on a headland.
function Course:isOnClockwiseHeadland(ix)
local boundaryId = self.waypoints[ix].attributes:getBoundaryId()
if boundaryId == nil then
return nil
end
if CourseGenerator.isHeadland(boundaryId) then
return self.headlandClockwise
elseif CourseGenerator.isIslandHeadland(boundaryId) then
return self.islandHeadlandClockwise
end
return nil
end

function Course:isHeadlandTransition(ix)
return self.waypoints[ix]:isHeadlandTransition()
end
Expand Down
5 changes: 3 additions & 2 deletions scripts/ai/AIDriveStrategyPlowCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ function AIDriveStrategyPlowCourse:rotatePlows()
local ix = self.ppc:getCurrentWaypointIx()
local plowShouldBeOnTheLeft
if self.course:isOnHeadland(ix) then
plowShouldBeOnTheLeft = self.course:isLeftSideWorked(ix)
self:debug('On a headland, plow should be on the left %s', tostring(plowShouldBeOnTheLeft))
local clockwise = self.course:isOnClockwiseHeadland(ix)
plowShouldBeOnTheLeft = not clockwise
self:debug('On a headland (clockwise %s), plow should be on the left %s', tostring(clockwise), tostring(plowShouldBeOnTheLeft))
else
local isNextTurnLeft = self.course:isNextTurnLeft(ix)
if isNextTurnLeft == nil then
Expand Down
12 changes: 12 additions & 0 deletions scripts/courseGenerator/CourseGenerator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ CourseGenerator.cDefaultHeadlandOverlapPercentage = 5
CourseGenerator.debugPoints = {}
CourseGenerator.debugPolylines = {}

---@param boundaryId string boundary ID from the waypoint attributes
---@return boolean true if this boundary ID is associated with a headland around the field
function CourseGenerator.isHeadland(boundaryId)
return string.sub(boundaryId, 1) == CourseGenerator.Headland.boundaryIdPrefix
end

---@param boundaryId string boundary ID from the waypoint attributes
---@return boolean true if this boundary ID is associated with a headland around an island
function CourseGenerator.isIslandHeadland(boundaryId)
return string.sub(boundaryId, 1) == CourseGenerator.IslandHeadland.boundaryIdPrefix
end

--- Return true when running in the game
-- used by file and log functions to determine how exactly to do things,
-- for example, io.flush is not available from within the game.
Expand Down
10 changes: 8 additions & 2 deletions scripts/courseGenerator/Headland.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local Headland = CpObject()

--- Headlands around the field will always have a boundary ID starting with 'F' (for field)
Headland.boundaryIdPrefix = 'F'

--- Create a headland from a base polygon. The headland is a new polygon, offset by width, that is, inside
--- of the base polygon.
---
Expand Down Expand Up @@ -251,7 +254,7 @@ end
--- A short ID to identify the boundary this headland is based on when serializing/deserializing. By default, this
--- is the field boundary.
function Headland:getBoundaryId()
return 'F'
return Headland.boundaryIdPrefix
end

function Headland:__tostring()
Expand All @@ -265,6 +268,9 @@ CourseGenerator.Headland = Headland
---@class IslandHeadland
local IslandHeadland = CpObject(CourseGenerator.Headland)

--- Headlands around the field will always have a boundary ID starting with 'I' (for island)
IslandHeadland.boundaryIdPrefix = 'I'

--- Create an island headland around a base polygon. The headland is a new polygon, offset by width, that is, outside
--- of the base polygon.
---
Expand Down Expand Up @@ -305,7 +311,7 @@ end

--- A short ID in the form I<island ID> to identify the boundary this headland is based on when serializing/deserializing
function IslandHeadland:getBoundaryId()
return 'I' .. self.island:getId()
return IslandHeadland.boundaryIdPrefix .. self.island:getId()
end

function IslandHeadland:__tostring()
Expand Down

0 comments on commit 4318a6a

Please sign in to comment.