Skip to content

Commit

Permalink
Small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Mar 26, 2022
1 parent e64d20e commit 7de298c
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions scripts/ai/AIDriveStrategyFindBales.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,6 @@ function AIDriveStrategyFindBales:startWithoutCourse()

self:info('Starting bale collect/wrap')

-- We either collect bales on a normal field or a custom field.
-- If the vehicle is on a custom field, find all bales which are in the field polygon of the custom field.
local x, _, z = getWorldTranslation(self.vehicle.rootNode)
---@type CustomField
self.customField = g_customFieldManager:getCustomField(x, z)
-- Custom field takes precedence over normal field.
if self.customField then
self.fieldName = self.customField:getName()
self:info(' - map: %s, custom field %s', g_currentMission.missionInfo.mapTitle, self.customField:getName())
else
-- On a normal field, we use the field ID under the position of the bale to find bales.
local myField = CpFieldUtil.getFieldNumUnderVehicle(self.vehicle)
if myField == 0 then
self:error('Vehicle not on field, cannot start bale collect/wrap job.')
self.vehicle:stopCurrentAIJob(AIMessageErrorNoFieldFound.new())
return
end
self.fieldId = myField
self.fieldName = tostring(myField)
self:info(' - map: %s, field %s', g_currentMission.missionInfo.mapTitle, self.fieldName)
end

for _, implement in pairs(self.vehicle:getAttachedImplements()) do
self:info(' - %s', CpUtil.getName(implement.object))
end
Expand All @@ -98,7 +76,7 @@ function AIDriveStrategyFindBales:collectNextBale()
self:findPathToNextBale()
else
self:info('No bales found, scan the field once more before leaving for the unload course.')
self.bales = self:findBales(self.fieldId)
self.bales = self:findBales()
if #self.bales > 0 then
self:info('Found more bales, collecting them')
self:findPathToNextBale()
Expand Down Expand Up @@ -162,7 +140,6 @@ end
--- Find bales on field
---@return BaleToCollect[] list of bales found
function AIDriveStrategyFindBales:findBales()
self:debug('Finding bales on field %s...', self.fieldName)
local balesFound = {}
for _, object in pairs(g_currentMission.nodeToObject) do
if BaleToCollect.isValidBale(object, self.baleWrapper, self.baleLoader) then
Expand All @@ -180,7 +157,7 @@ function AIDriveStrategyFindBales:findBales()
for _, bale in pairs(balesFound) do
table.insert(bales, bale)
end
self:debug('Found %d bales on field %s', #bales, self.fieldName)
self:debug('Found %d bales', #bales)
return bales
end

Expand Down Expand Up @@ -210,7 +187,7 @@ function AIDriveStrategyFindBales:findClosestBale(bales)
self:debug('%d. bale (%d, %s) INVALID', i, bale:getId(), bale:getBaleObject())
invalidBales = invalidBales + 1
self:debug('Found an invalid bales, rescanning field', invalidBales)
self.bales = self:findBales(self.fieldId)
self.bales = self:findBales()
-- return empty, next time this is called everything should be ok
return
end
Expand Down Expand Up @@ -271,8 +248,8 @@ function AIDriveStrategyFindBales:startPathfindingToBale(bale)
local done, path, goalNodeInvalid
-- use no off-field penalty if we are on a custom field
self.pathfinder, done, path, goalNodeInvalid =
PathfinderUtil.startPathfindingFromVehicleToGoal(self.vehicle, goal, false, self.fieldId,
{}, self.lastBale and {self.lastBale} or {}, nil, self.fieldId and nil or 0)
PathfinderUtil.startPathfindingFromVehicleToGoal(self.vehicle, goal, false, nil,
{}, self.lastBale and {self.lastBale} or {}, nil, nil)
if done then
return self:onPathfindingDoneToNextBale(path, goalNodeInvalid)
else
Expand Down Expand Up @@ -363,7 +340,7 @@ function AIDriveStrategyFindBales:onWaypointPassed(ix, course)
self:collectNextBale()
elseif self.state == self.states.APPROACHING_BALE then
self:debug('looks like somehow we missed a bale, rescanning field')
self.bales = self:findBales(self.fieldId)
self.bales = self:findBales()
self:collectNextBale()
elseif self.state == self.states.REVERSING_AFTER_PATHFINDER_FAILURE then
self:debug('backed up after pathfinder failed, trying again')
Expand Down

0 comments on commit 7de298c

Please sign in to comment.