Skip to content

Commit

Permalink
Merge pull request #2842 from Courseplay/PathfinderController
Browse files Browse the repository at this point in the history
Added Pathfinder controller to unify the pathfinder handling
  • Loading branch information
schwiti6190 committed Oct 18, 2023
2 parents 9a25a98 + cdf3c22 commit 2f4e683
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 3 deletions.
1 change: 1 addition & 0 deletions modDesc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ Changelog 7.1.0.0:

<sourceFile filename="scripts/ai/AIUtil.lua"/>
<sourceFile filename="scripts/ai/ImplementUtil.lua"/>
<sourceFile filename="scripts/ai/PathfinderController.lua"/>
<sourceFile filename="scripts/ai/ProximityController.lua"/>
<sourceFile filename="scripts/ai/FieldWorkerProximityController.lua"/>
<sourceFile filename="scripts/ai/CollisionAvoidanceController.lua"/>
Expand Down
37 changes: 37 additions & 0 deletions scripts/CpObject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,47 @@ function CpObject(base, init)
end
return false
end
c.__tostring = function (self)
-- Default tostring function for printing all attributes and assigned functions.
local str = '[ '
for attribute, value in pairs(self) do
str = str .. string.format('%s: %s ', attribute, value)
end
str = str .. ']'
return str
end

setmetatable(c, mt)
return c
end

---@class CpObjectUtil
CpObjectUtil = {
BUILDER_API_NIL = "nil"
}

--- Registers a builder api for a class.
--- The attributes are set as private variables with "_" before the variable name
--- and the builder functions are named like the attribute.
--- Nil values have to be replaced with CpObjectUtil.BUILDER_API_NIL !!
---@param class table
---@param attributesToDefault table<attributeName, any>
function CpObjectUtil.registerBuilderAPI(class, attributesToDefault)
for attributeName, default in pairs(attributesToDefault) do
if default == CpObjectUtil.BUILDER_API_NIL then
default = nil
end
--- Applies the default value to the private variable
class["_" .. attributeName] = default
--- Creates the builder functions/ setters with the public variable name
class[attributeName] = function(self, value)
self["_" .. attributeName] = value
return self
end
end
end


--- Object that holds a value temporarily. You can tell when to set the value and how long it should keep that
--- value, in milliseconds. Great for timers.
---@class CpTemporaryObject
Expand Down
25 changes: 24 additions & 1 deletion scripts/ai/AIDriveStrategyCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ function AIDriveStrategyCourse:setAIVehicle(vehicle, jobParameters)
self:initializeImplementControllers(vehicle)
self.ppc = PurePursuitController(vehicle)
self.ppc:registerListeners(self, 'onWaypointPassed', 'onWaypointChange')

self.pathfinderController = PathfinderController(vehicle)
self.pathfinderController:registerListeners(self, self.onPathfindingFinished, self.onPathfindingRetry)

self.storage = vehicle.spec_cpAIWorker

self.settings = vehicle:getCpSettings()
Expand Down Expand Up @@ -428,8 +432,9 @@ function AIDriveStrategyCourse:getCurrentCourse()
return self.ppc:getCourse() or self.course
end

function AIDriveStrategyCourse:update()
function AIDriveStrategyCourse:update(dt)
self.ppc:update()
self.pathfinderController:update(dt)
self:updatePathfinding()
self:updateInfoTexts()
end
Expand Down Expand Up @@ -579,6 +584,24 @@ end
function AIDriveStrategyCourse:onWaypointPassed(ix, course)
end

--- Pathfinding has finished
---@param controller PathfinderController
---@param success boolean
---@param course Course|nil
---@param goalNodeInvalid boolean|nil
function AIDriveStrategyCourse:onPathfindingFinished(controller, success, course, goalNodeInvalid)
-- override
end

--- Pathfinding failed, but a retry attempt is leftover.
---@param controller PathfinderController
---@param lastContext PathfinderControllerContext
---@param wasLastRetry boolean
---@param currentRetryAttempt number
function AIDriveStrategyCourse:onPathfindingRetry(controller, lastContext, wasLastRetry, currentRetryAttempt)
-- override
end

------------------------------------------------------------------------------------------------------------------------
--- Pathfinding
---------------------------------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 2f4e683

Please sign in to comment.