Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from Courseplay/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
polosubtil authored Aug 2, 2020
2 parents 1c91e6f + fa2e345 commit acba8f0
Show file tree
Hide file tree
Showing 68 changed files with 3,910 additions and 1,622 deletions.
59 changes: 35 additions & 24 deletions AIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ AIDriver.proximityMinLimitedSpeed = 2
-- if anything closer than this, we stop
AIDriver.proximityLimitLow = 1

AIDriver.APPROACH_AUGER_TRIGGER_SPEED = 3

-- we use this as an enum
AIDriver.myStates = {
TEMPORARY = {}, -- Temporary course, dynamically generated, for example alignment or fruit avoidance
Expand Down Expand Up @@ -227,6 +229,12 @@ function AIDriver:beforeStart()
self.firstReversingWheeledWorkTool = courseplay:getFirstReversingWheeledWorkTool(self.vehicle)
-- for now, pathfinding generated courses can't be driven by towed tools
self.allowReversePathfinding = self.firstReversingWheeledWorkTool == nil
if self.vehicle:getAINeedsTrafficCollisionBox() then
courseplay.debugVehicle(3,self.vehicle,"Making sure cars won't stop around us")
-- something deep inside the Giants vehicle sets the translation of this box to whatever
-- is in aiTrafficCollisionTranslation, if you do a setTranslation() it won't remain there...
self.vehicle.spec_aiVehicle.aiTrafficCollisionTranslation[2] = -1000
end
end

--- Start driving
Expand All @@ -240,7 +248,8 @@ function AIDriver:start(startingPoint)
-- main course is the one generated/loaded/recorded
self.mainCourse = Course(self.vehicle, self.vehicle.Waypoints)
local ix = self.mainCourse:getStartingWaypointIx(AIDriverUtil.getDirectionNode(self.vehicle), startingPoint)
self:debug('AI driver in mode %d starting at %d/%d waypoints', self:getMode(), ix, self.mainCourse:getNumberOfWaypoints())
self:info('AI driver in mode %d starting at %d/%d waypoints (%s)',
self:getMode(), ix, self.mainCourse:getNumberOfWaypoints(), tostring(startingPoint))
self:startCourseWithAlignment(self.mainCourse, ix)
end

Expand Down Expand Up @@ -351,7 +360,6 @@ function AIDriver:drive(dt)
self:hold()
self:continueIfWaitTimeIsOver()
end

self:driveCourse(dt)
self:drawTemporaryCourse()
end
Expand Down Expand Up @@ -383,7 +391,7 @@ function AIDriver:driveCourse(dt)
if self:getIsInFilltrigger() then
self:setSpeed(self.vehicle.cp.speeds.approach)
end

self:slowDownForWaitPoints()

self:stopEngineIfNotNeeded()
Expand Down Expand Up @@ -571,7 +579,7 @@ function AIDriver:onEndCourse()
local parkDestination = self.vehicle.spec_autodrive:GetParkDestination(self.vehicle)
self.vehicle.spec_autodrive:StartDrivingWithPathFinder(self.vehicle, parkDestination, -3, nil, nil, nil)
end
elseif self.vehicle.cp.stopAtEnd then
elseif self.vehicle.cp.settings.stopAtEnd:is(true) then
if self.state ~= self.states.STOPPED then
self:stop('END_POINT')
end
Expand Down Expand Up @@ -742,10 +750,15 @@ function AIDriver:getTotalLength()
return self.vehicle.cp.totalLength
end

--- Get waypoint closest to the current position of the vehicle
function AIDriver:getRelevantWaypointIx()
return self.ppc:getRelevantWaypointIx()
end

function AIDriver:getRecordedSpeed()
-- default is the street speed (reduced in corners)
local speed = self:getDefaultStreetSpeed(self.ppc:getCurrentWaypointIx()) or self.vehicle.cp.speeds.street
if self.vehicle.cp.speeds.useRecordingSpeed then
if self.vehicle.cp.settings.useRecordingSpeed:is(true) then
-- use default street speed if there's no recorded speed.
speed = math.min(self.course:getAverageSpeed(self.ppc:getCurrentWaypointIx(), 4) or speed, speed)
end
Expand Down Expand Up @@ -869,25 +882,11 @@ end
function AIDriver:enableCollisionDetection()
courseplay.debugVehicle(3,self.vehicle,'Collision detection enabled')
self.collisionDetectionEnabled = true
-- move the big collision box around the vehicle underground because this will stop
-- traffic (not CP drivers though) around us otherwise
if self.vehicle:getAINeedsTrafficCollisionBox() then
courseplay.debugVehicle(3,self.vehicle,"Making sure cars won't stop around us")
-- something deep inside the Giants vehicle sets the translation of this box to whatever
-- is in aiTrafficCollisionTranslation, if you do a setTranslation() it won't remain there...
self.vehicle.spec_aiVehicle.aiTrafficCollisionTranslation[2] = -1000
end
end

function AIDriver:disableCollisionDetection()
courseplay.debugVehicle(3,self.vehicle,'Collision detection disabled')
self.collisionDetectionEnabled = false
-- move the big collision box around the vehicle back over the ground so
-- game traffic around us will stop while we are working on the field
if self.vehicle:getAINeedsTrafficCollisionBox() then
courseplay.debugVehicle(3,self.vehicle,'Cars will stop around us again.')
self.vehicle.spec_aiVehicle.aiTrafficCollisionTranslation[2] = 0
end
end

function AIDriver:detectCollision(dt)
Expand Down Expand Up @@ -920,7 +919,7 @@ function AIDriver:detectCollision(dt)
end

function AIDriver:areBeaconLightsEnabled()
return self.vehicle.cp.warningLightsMode > courseplay.lights.WARNING_LIGHTS_NEVER
return self.vehicle.cp.settings.warningLightsMode:get() > WarningLightsModeSetting.WARNING_LIGHTS_NEVER
end

function AIDriver:updateLights()
Expand Down Expand Up @@ -1410,7 +1409,7 @@ end
--- pathfinding considers any collision-free path valid, also outside of the field.
---@return boolean true when a pathfinding successfully started
function AIDriver:driveToPointWithPathfinding(waypoint, zOffset, course, ix, fieldNum)
if self.vehicle.cp.realisticDriving then
if self.vehicle.cp.settings.useRealisticDriving:is(true) then
if not self.pathfinder or not self.pathfinder:isActive() then
self.courseAfterPathfinding = course
self.waypointIxAfterPathfinding = ix
Expand Down Expand Up @@ -1527,7 +1526,7 @@ end;
--- Is auto stop engine enabled?
function AIDriver:isEngineAutoStopEnabled()
-- do not auto stop engine when auto motor start is enabled as it'll try to restart the engine on each update tick.
return self.vehicle.cp.saveFuelOptionActive and not g_currentMission.missionInfo.automaticMotorStartEnabled
return self.vehicle.cp.settings.saveFuelOption:is(true) and not g_currentMission.missionInfo.automaticMotorStartEnabled
end

--- Check the engine state and stop if we have the fuel save option and been stopped too long
Expand Down Expand Up @@ -1564,13 +1563,13 @@ function AIDriver:onDraw()
end

end

--TODO: do we want to continue using this setter/getter for driveUnloadNow??
function AIDriver:setDriveUnloadNow(driveUnloadNow)
courseplay:setDriveUnloadNow(self.vehicle, driveUnloadNow or false)
end

function AIDriver:getDriveUnloadNow()
return self.vehicle.cp.driveUnloadNow
return self.vehicle.cp.settings.driveUnloadNow:get()
end

function AIDriver:refreshHUD()
Expand Down Expand Up @@ -1796,4 +1795,16 @@ function AIDriver:checkProximitySensor(maxSpeed, allowedToDrive, moveForwards)
local newSpeed = AIDriver.proximityMinLimitedSpeed + normalizedD * deltaV
self:debugSparse('proximity: d = %.1f (%d %%), speed = %.1f', d, 100 * normalizedD, newSpeed)
return newSpeed, allowedToDrive
end

function AIDriver:isLoadingTriggerCallbackEnabled()
return false
end

function AIDriver:isUnloadingTriggerCallbackEnabled()
return false
end

function AIDriver:isOverloadingTriggerCallbackEnabled()
return false
end
6 changes: 3 additions & 3 deletions AITurn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function AITurn.canMakeKTurn(vehicle, turnContext)
courseplay.debugVehicle(AITurn.debugChannel, vehicle, 'Not all attached implements allow for reversing, use generated course turn')
return false
end
if vehicle.cp.turnOnField and not AITurn.canTurnOnField(turnContext, vehicle) then
if vehicle.cp.settings.turnOnField:is(true) and not AITurn.canTurnOnField(turnContext, vehicle) then
courseplay.debugVehicle(AITurn.debugChannel, vehicle, 'Turn on field is on but there is not enough space, use generated course turn')
return false
end
Expand Down Expand Up @@ -534,7 +534,7 @@ end

function CourseTurn:generateCalculatedTurn()
-- TODO: fix ugly dependency on global variables, there should be one function to create the turn maneuver
self.vehicle.cp.turnStage = 1
self.vehicle.cp.settings.turnStage:set(true)
-- call turn() with stage 1 which will generate the turn waypoints (dt isn't used by that part)
courseplay:turn(self.vehicle, 1, self.turnContext)
-- they waypoints should now be in turnTargets, create a course based on that
Expand All @@ -549,7 +549,7 @@ function CourseTurn:generatePathfinderTurn()
local done, path
local turnEndNode, startOffset, goalOffset = self.turnContext:getTurnEndNodeAndOffsets()
local canTurnOnField, distanceToReverse = AITurn.canTurnOnField(self.turnContext, self.vehicle)
if not canTurnOnField and self.vehicle.cp.turnOnField then
if not canTurnOnField and self.vehicle.cp.settings.turnOnField:is(true) then
self:debug('Turn on field is on, generating reverse course before turning.')
self.reverseBeforeStartingTurnWaypoints = self.turnContext:createReverseWaypointsBeforeStartingTurn(self.vehicle, distanceToReverse)
startOffset = startOffset - distanceToReverse
Expand Down
2 changes: 1 addition & 1 deletion BaleLoaderAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end
function BaleLoaderAIDriver:init(vehicle)
courseplay.debugVehicle(11,vehicle,'BaleLoaderAIDriver:init()')
UnloadableFieldworkAIDriver.init(self, vehicle)
self.baleLoader = FieldworkAIDriver.getImplementWithSpecialization(vehicle, BaleLoader)
self.baleLoader = AIDriverUtil.getAIImplementWithSpecialization(vehicle, BaleLoader)

-- Bale loaders have no AI markers (as they are not AIImplements according to Giants) so add a function here
-- to get the markers
Expand Down
2 changes: 1 addition & 1 deletion BaleWrapperAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function BaleWrapperAIDriver:init(vehicle)
-- derived classes may not fly when there are multiple specializations to handle, if we had a bale loader
-- which is also a bale wrapper then we would probably have to put everything back into the baler.
BalerAIDriver.init(self, vehicle)
self.baleWrapper = FieldworkAIDriver.getImplementWithSpecialization(vehicle, BaleWrapper)
self.baleWrapper = AIDriverUtil.getAIImplementWithSpecialization(vehicle, BaleWrapper)

if not self.baler then
-- Bale wrappers which aren't balers have no AI markers as they have no pick up so add a function here
Expand Down
9 changes: 6 additions & 3 deletions BalerAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BalerAIDriver = CpObject(UnloadableFieldworkAIDriver)
function BalerAIDriver:init(vehicle)
courseplay.debugVehicle(11,vehicle,'BalerAIDriver:init()')
UnloadableFieldworkAIDriver.init(self, vehicle)
self.baler = FieldworkAIDriver.getImplementWithSpecialization(vehicle, Baler)
self.baler = AIDriverUtil.getAIImplementWithSpecialization(vehicle, Baler)
end

function BalerAIDriver:driveFieldwork(dt)
Expand Down Expand Up @@ -71,8 +71,11 @@ function BalerAIDriver:handleBaler()
self.baler:setIsUnloadingBale(true, false)
end
elseif self.baler.spec_baler.unloadingState ~= Baler.UNLOADING_CLOSED then
allowedToDrive = false
if self.baler.spec_baler.unloadingState == Baler.UNLOADING_OPEN then
if fillLevel >= capacity then -- Only stop if capacity is full. Allowing for continuous balers such as the ViconFastBale
allowedToDrive = false
elseif fillLevel == 0 and (self.baler.spec_baler.unloadingState == Baler.UNLOADING_CLOSING or self.baler.spec_baler.unloadingState == Baler.UNLOADING_OPENING) then
allowedToDrive = false
elseif self.baler.spec_baler.unloadingState == Baler.UNLOADING_OPEN then
self.baler:setIsUnloadingBale(false)
end
elseif fillLevel >= 0 and not self.baler:getIsTurnedOn() and self.baler.spec_baler.unloadingState == Baler.UNLOADING_CLOSED then
Expand Down
Loading

0 comments on commit acba8f0

Please sign in to comment.