Skip to content

Commit 518c024

Browse files
committed
Add scanner fallback for coarse field boundaries
1 parent c3ce493 commit 518c024

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

scripts/field/FieldBoundaryDetector.lua

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ FieldBoundaryDetector = CpObject()
1919
function FieldBoundaryDetector:init(x, z, vehicle)
2020
self.logger = Logger('FieldBoundaryDetector', Logger.level.debug, CpDebug.DBG_COURSES)
2121
self.vehicle = vehicle
22+
self.x = x
23+
self.z = z
2224
self.updates = 0
2325
local customField = g_customFieldManager:getCustomField(x, z)
2426
if customField and g_Courseplay.globalSettings.preferCustomFields:getValue() then
@@ -35,6 +37,7 @@ function FieldBoundaryDetector:init(x, z, vehicle)
3537
self.logger:info('Field boundary detection successful after %d updates, %d boundary points and %d islands',
3638
self.updates, #courseField.fieldRootBoundary.boundaryLine, #courseField.islands)
3739
self.fieldPolygon = self:_getAsVertices(courseField.fieldRootBoundary.boundaryLine)
40+
self:_tryLowResolutionBoundaryFallback()
3841
self.islandPolygons = {}
3942
for i, island in ipairs(courseField.islands) do
4043
local islandBoundary = self:_getAsVertices(island.rootBoundary.boundaryLine)
@@ -49,6 +52,7 @@ function FieldBoundaryDetector:init(x, z, vehicle)
4952
else
5053
self.logger:info('Field boundary detection failed after %d updates and no custom field found at %.1f %.1f',
5154
self.updates, x, z)
55+
self:_tryGiantsFailedBoundaryFallback()
5256
return
5357
end
5458
end
@@ -95,4 +99,37 @@ end
9599
function FieldBoundaryDetector:_useCustomField(customField)
96100
self.fieldPolygon = customField:getVertices()
97101
self.useCustomField = true
98-
end
102+
end
103+
104+
function FieldBoundaryDetector:_tryLowResolutionBoundaryFallback()
105+
local minimumGiantsBoundaryPoints = 60
106+
if not self.fieldPolygon or #self.fieldPolygon >= minimumGiantsBoundaryPoints then
107+
return
108+
end
109+
if not g_fieldScanner or not g_fieldScanner.findContour then
110+
return
111+
end
112+
local valid, scannerPolygon = g_fieldScanner:findContour(self.x, self.z)
113+
if valid and scannerPolygon and #scannerPolygon > #self.fieldPolygon * 2 then
114+
self.logger:info('Giants boundary only has %d points, using Courseplay field scanner result with %d points instead',
115+
#self.fieldPolygon, #scannerPolygon)
116+
self.fieldPolygon = scannerPolygon
117+
else
118+
self.logger:info('Giants boundary only has %d points, but Courseplay field scanner fallback did not improve it',
119+
#self.fieldPolygon)
120+
end
121+
end
122+
123+
function FieldBoundaryDetector:_tryGiantsFailedBoundaryFallback()
124+
if not g_fieldScanner or not g_fieldScanner.findContour then
125+
return
126+
end
127+
local valid, scannerPolygon = g_fieldScanner:findContour(self.x, self.z)
128+
if valid and scannerPolygon and #scannerPolygon > 0 then
129+
self.logger:info('Giants boundary detection failed, using Courseplay field scanner result with %d points instead',
130+
#scannerPolygon)
131+
self.fieldPolygon = scannerPolygon
132+
else
133+
self.logger:info('Giants boundary detection failed, and Courseplay field scanner fallback did not find a boundary')
134+
end
135+
end

0 commit comments

Comments
 (0)