Skip to content

Commit

Permalink
refactor: do not detect again if position is same
Browse files Browse the repository at this point in the history
Peter Vaiko committed Jan 24, 2025
1 parent 172dcba commit ae56aef
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 7 additions & 1 deletion scripts/courseGenerator/CourseGeneratorInterface.lua
Original file line number Diff line number Diff line change
@@ -22,7 +22,13 @@ function CourseGeneratorInterface:startGeneration(startPosition, vehicle, settin
self.settings = settings
self.object = object
self.onFinishedFunc = onFinishedFunc
vehicle:cpDetectFieldBoundary(startPosition.x, startPosition.z, self, self.onFieldDetectionFinished)
local x, z = vehicle:cpGetFieldPosition()
if x == startPosition.x and z == startPosition.z and vehicle:cpGetFieldPolygon() then
self.logger:debug(vehicle, 'Same start position, using existing field polygon')
self:onFieldDetectionFinished(vehicle, vehicle:cpGetFieldPolygon(), vehicle:cpGetIslandPolygons())
else
vehicle:cpDetectFieldBoundary(startPosition.x, startPosition.z, self, self.onFieldDetectionFinished)
end
end

function CourseGeneratorInterface:onFieldDetectionFinished(vehicle, fieldPolygon, islandPolygons)
9 changes: 8 additions & 1 deletion scripts/specializations/CpCourseGenerator.lua
Original file line number Diff line number Diff line change
@@ -29,14 +29,15 @@ function CpCourseGenerator.registerFunctions(vehicleType)
SpecializationUtil.registerFunction(vehicleType, 'cpIsFieldBoundaryDetectionRunning', CpCourseGenerator.cpIsFieldBoundaryDetectionRunning)
SpecializationUtil.registerFunction(vehicleType, 'cpGetFieldPosition', CpCourseGenerator.cpGetFieldPosition)
SpecializationUtil.registerFunction(vehicleType, 'cpGetFieldPolygon', CpCourseGenerator.cpGetFieldPolygon)
SpecializationUtil.registerFunction(vehicleType, 'cpGetIslandPolygons', CpCourseGenerator.cpGetIslandPolygons)
SpecializationUtil.registerFunction(vehicleType, 'cpDrawFieldPolygon', CpCourseGenerator.cpDrawFieldPolygon)
end

function CpCourseGenerator:onLoad(savegame)
-- create shortcut to this spec
self.spec_cpCourseGenerator = self["spec_" .. CpCourseGenerator.SPEC_NAME]
self.spec_cpCourseGenerator.logger = Logger(CpCourseGenerator.SPEC_NAME, nil, CpDebug.DBG_COURSES)
-- make sure cpGetFieldPosition always returns at least an empty table
-- make sure cpGetFieldPosition always has spec.position
self.spec_cpCourseGenerator.position = {}
end

@@ -91,10 +92,16 @@ function CpCourseGenerator:onUpdate(dt)
end
end

---@return table|nil [{x, y, z}] field polygon with game vertices
function CpCourseGenerator:cpGetFieldPolygon()
return self.spec_cpCourseGenerator.fieldPolygon
end

---@return table|nil [[{x, y, z}]] array of island polygons with game vertices (x, y, z)
function CpCourseGenerator:cpGetIslandPolygons()
return self.spec_cpCourseGenerator.islandPolygons
end

-- For debug, if there is a field polygon or island polygons, draw them
function CpCourseGenerator:cpDrawFieldPolygon()
local spec = self.spec_cpCourseGenerator

0 comments on commit ae56aef

Please sign in to comment.