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 #2 from Courseplay/master
Browse files Browse the repository at this point in the history
Rebase
  • Loading branch information
Axel32019 authored Jul 28, 2019
2 parents d87e4bf + af52629 commit 07f984b
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 63 deletions.
16 changes: 10 additions & 6 deletions AIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ function AIDriver:init(vehicle)
-- same for allowedToDrive, is reset at the end of each loop to true and needs to be set to false
-- if someone wants to stop by calling hold()
self.allowedToDrive = true
self.collisionDetectionEnabled = false
self.collisionDetector = CollisionDetector(self.vehicle)
self.collisionDetectionEnabled = true
self.collisionDetector = nil
-- list of active messages to display
self.activeMsgReferences = {}
self.pathfinder = Pathfinder()
Expand Down Expand Up @@ -196,7 +196,9 @@ end
function AIDriver:beforeStart()
self.turnIsDriving = false
self.nextCourse = nil
-- self:deleteCollisionDetector() -- makes no sense to delete the detector here
if self.collisionDetector == nil then
self.collisionDetector = CollisionDetector(self.vehicle)
end
self:startEngineIfNeeded()
end

Expand All @@ -216,8 +218,8 @@ end

--- Dismiss the driver
function AIDriver:dismiss()
if self.collisionDetector then -- restore the default direction of the colli boxes
self.collisionDetector:reset()
if self.collisionDetector then
self.collisionDetector:reset() -- restore the default direction of the colli boxes
end
self.vehicle:deactivateLights()
self:clearAllInfoTexts()
Expand All @@ -227,6 +229,7 @@ end
--- Stop the driver
-- @param reason as defined in globalInfoText.msgReference
function AIDriver:stop(msgReference)
self:deleteCollisionDetector()
-- not much to do here, see the derived classes
self:setInfoText(msgReference)
self.state = self.states.STOPPED
Expand Down Expand Up @@ -738,6 +741,7 @@ function AIDriver:detectCollision(dt)
self:clearInfoText('TRAFFIC')
end

return self.allowedToDrive
end

function AIDriver:areBeaconLightsEnabled()
Expand Down Expand Up @@ -1354,4 +1358,4 @@ end

function AIDriver:onUnBlocked()
self:debug('Unblocked...')
end
end
21 changes: 20 additions & 1 deletion BaleLoaderAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ function BaleLoaderAIDriver.register()
BaleLoader.onAIImplementEnd = Utils.overwrittenFunction(BaleLoader.onAIImplementEnd,
function(self, superFunc)
if superFunc ~= nil then superFunc(self) end
self:doStateChange(BaleLoader.CHANGE_MOVE_TO_TRANSPORT);
local spec = self.spec_baleLoader
if not spec.grabberIsMoving and spec.grabberMoveState == nil and spec.isInWorkPosition then
self:doStateChange(BaleLoader.CHANGE_MOVE_TO_TRANSPORT);
end
end)

local baleLoaderRegisterEventListeners = function(vehicleType)
Expand All @@ -57,6 +60,13 @@ function BaleLoaderAIDriver:init(vehicle)
courseplay.debugVehicle(11,vehicle,'BaleLoaderAIDriver:init()')
UnloadableFieldworkAIDriver.init(self, vehicle)
self.baleLoader = FieldworkAIDriver.getImplementWithSpecialization(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
self.baleLoader.getAIMarkers = function(self)
return UnloadableFieldworkAIDriver.getAIMarkersFromGrabberNode(self, self.spec_baleLoader)
end

self:initStates(BaleLoaderAIDriver.myStates)
self.manualUnloadNode = WaypointNode(self.vehicle:getName() .. 'unloadNode')
self:debug('Initialized, bale loader: %s', self.baleLoader:getName())
Expand Down Expand Up @@ -132,6 +142,15 @@ function BaleLoaderAIDriver:driveUnloadOrRefill(dt)
end
end
end
else
-- for some bale loaders the onAIImplementEnd() does not work seemingly because the grabber is still busy
-- with the last bale when switching to the unload course. Make sure here that it is moved to transport
-- position on the unload course
local spec = self.baleLoader.spec_baleLoader
if not spec.grabberIsMoving and spec.grabberMoveState == nil and spec.isInWorkPosition then
self:debug('Move grabber to transport position')
self.baleLoader:doStateChange(BaleLoader.CHANGE_MOVE_TO_TRANSPORT);
end
end
return false
end
Expand Down
8 changes: 8 additions & 0 deletions BaleWrapperAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ function BaleWrapperAIDriver:init(vehicle)
-- 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)

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
-- to get the markers
self.baleWrapper.getAIMarkers = function(self)
return UnloadableFieldworkAIDriver.getAIMarkersFromGrabberNode(self, self.spec_baleWrapper)
end
end
end

function BaleWrapperAIDriver:driveFieldwork()
Expand Down
3 changes: 2 additions & 1 deletion CombineAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function CombineAIDriver:init(vehicle)
self.pullBackDistanceEnd = self.pullBackDistanceStart + 10
-- when making a pocket, how far to back up before changing to forward
self.pocketReverseDistance = 25
self.lastEmptyTimestamp = 0

if self.vehicle.spec_combine then
self.combine = self.vehicle.spec_combine
Expand Down Expand Up @@ -621,7 +622,7 @@ end
function CombineAIDriver:shouldStopForUnloading(pc)
local stop = false
if self.vehicle.cp.stopWhenUnloading and self.pipe then
if self.pipe.currentState == UnloadableFieldworkAIDriver.PIPE_STATE_OPEN and
if self.pipe.currentState == CombineAIDriver.PIPE_STATE_OPEN and
g_updateLoopIndex > self.lastEmptyTimestamp + 600 then
-- stop only if the pipe is open AND we have been emptied more than 1000 cycles ago.
-- this makes sure the combine will start driving after it is emptied but the trailer
Expand Down
48 changes: 41 additions & 7 deletions FieldworkAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ end

function FieldworkAIDriver:changeToFieldwork()
self:debug('change to fieldwork')
self:disableCollisionDetection()
self.state = self.states.ON_FIELDWORK_COURSE
self.fieldworkState = self.states.WAITING_FOR_LOWER
self:startWork()
Expand Down Expand Up @@ -972,9 +971,10 @@ function FieldworkAIDriver:setMarkers()
-- work areas of the vehicle itself
addMarkers(self.vehicle, referenceNode)
-- and then the work areas of all the implements
for _, implement in pairs(self.vehicle:getAttachedImplements()) do
for _, implement in pairs( self:getAllAIImplements(self.vehicle)) do
addMarkers(implement.object, referenceNode)
end

if #markers == 0 then
-- make sure we always have a default front/back marker, placed on the direction node if nothing else found
table.insert(markers, 0)
Expand Down Expand Up @@ -1028,7 +1028,7 @@ function FieldworkAIDriver:shouldLowerImplements(turnEndNode, reversing)
doLower = true
end
-- and then check all implements
for _, implement in ipairs(self.vehicle:getAttachedAIImplements()) do
for _, implement in ipairs(self:getAllAIImplements(self.vehicle)) do
if reversing then
-- when driving backward, all implements must reach the turn end node before lowering, hence the 'and'
doLower = doLower and self:shouldLowerThisImplement(implement.object, turnEndNode, reversing)
Expand Down Expand Up @@ -1073,7 +1073,7 @@ function FieldworkAIDriver:shouldRaiseImplements(turnStartNode)
-- see if the vehicle has AI markers -> has work areas (built-in implements like a mower or cotton harvester)
local doRaise = self:shouldRaiseThisImplement(self.vehicle, turnStartNode)
-- and then check all implements
for _, implement in ipairs(self.vehicle:getAttachedAIImplements()) do
for _, implement in ipairs(self:getAllAIImplements(self.vehicle)) do
-- only when _all_ implements can be raised will we raise them all, hence the 'and'
doRaise = doRaise and self:shouldRaiseThisImplement(implement.object, turnStartNode)
end
Expand All @@ -1095,9 +1095,32 @@ function FieldworkAIDriver:shouldRaiseThisImplement(object, turnStartNode)
end

function FieldworkAIDriver:onDraw()
if self.frontMarker and self.backMarker then
DebugUtil.drawDebugNode(self.frontMarker:getNode(), getName(self.frontMarker:getNode()))
DebugUtil.drawDebugNode(self.backMarker:getNode(), getName(self.backMarker:getNode()))

if not courseplay.debugChannels[6] then return end

local function showAIMarkersOfObject(object)
if object.getAIMarkers then
local aiLeftMarker, aiRightMarker, aiBackMarker = object:getAIMarkers()
if aiLeftMarker then
DebugUtil.drawDebugNode(aiLeftMarker, object:getName() .. ' AI Left')
end
if aiRightMarker then
DebugUtil.drawDebugNode(aiRightMarker, object:getName() .. ' AI Right')
end
if aiBackMarker then
DebugUtil.drawDebugNode(aiBackMarker, object:getName() .. ' AI Back')
end
DebugUtil.drawDebugNode(object.cp.DirectionNode or object.rootNode, object:getName() .. ' root')
end
end

showAIMarkersOfObject(self.vehicle)
-- draw the Giant's supplied AI markers for all implements
local implements = self:getAllAIImplements(self.vehicle)
if implements then
for _, implement in ipairs(implements) do
showAIMarkersOfObject(implement.object)
end
end
AIDriver.onDraw(self)
end
Expand All @@ -1123,3 +1146,14 @@ function FieldworkAIDriver:getAIMarkersFromWorkAreas(object)
end
end

function FieldworkAIDriver:getAllAIImplements(object, implements)
if not implements then implements = {} end
for _, implement in ipairs(object:getAttachedImplements()) do
-- ignore everything which has no work area
if courseplay:hasWorkAreas(implement.object) then
table.insert(implements, implement)
end
self:getAllAIImplements(implement.object, implements)
end
return implements
end
37 changes: 20 additions & 17 deletions TrafficCollision.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function CollisionDetector:init(vehicle, course)
self.ignoredNodes = {}
self:addToIgnoreList(self.vehicle)
self.trafficCollisionTriggers = {}
self.trafficCollisionTriggers[1] = nil
self:createTriggers()
self:adaptCollisHeight()
end
Expand All @@ -47,7 +48,6 @@ function CollisionDetector:delete()
self.collidingObjects = {} -- clear all detected collisions
self.nCollidingObjects = 0 -- clear all detected collisions
self.ignoredNodes = {} -- clear all detected collisions
self:addToIgnoreList(self.vehicle)
end
end

Expand All @@ -71,18 +71,20 @@ function CollisionDetector:createTriggers()
end
self.vehicle.cp.trafficCollisionTriggerToTriggerIndex = {}
-- self.vehicle.aiTrafficCollisionTrigger = self.aiTrafficCollisionTrigger
for i = 1, self.vehicle.cp.numTrafficCollisionTriggers do
local newTrigger = clone(self.vehicle.aiTrafficCollisionTrigger, true)
self.trafficCollisionTriggers[i] = newTrigger
self.vehicle.cp.trafficCollisionTriggerToTriggerIndex[newTrigger] = i;
setName(newTrigger, 'cpAiCollisionTrigger ' .. tostring(i))
if i > 1 then
unlink(newTrigger)
link(self.trafficCollisionTriggers[i - 1], newTrigger)
setTranslation(newTrigger, 0, 0, 4)
if self.trafficCollisionTriggers[1] == nil then
for i = 1, self.vehicle.cp.numTrafficCollisionTriggers do
local newTrigger = clone(self.vehicle.aiTrafficCollisionTrigger, true)
self.trafficCollisionTriggers[i] = newTrigger
self.vehicle.cp.trafficCollisionTriggerToTriggerIndex[newTrigger] = i;
setName(newTrigger, 'cpAiCollisionTrigger ' .. tostring(i))
if i > 1 then
unlink(newTrigger)
link(self.trafficCollisionTriggers[i - 1], newTrigger)
setTranslation(newTrigger, 0, 0, 4)
end;
addTrigger(newTrigger, 'onCollision', self)
end;
addTrigger(newTrigger, 'onCollision', self)
end;
end
end


Expand All @@ -98,6 +100,7 @@ function CollisionDetector:deleteTriggers()
delete(node)
end
end
self.trafficCollisionTriggers[i] = nil
end

end
Expand All @@ -110,10 +113,10 @@ function CollisionDetector:addToIgnoreList(object)
self.ignoredNodes[object.rootNode] = true;
-- add the vehicle or implement's own collision trigger to the ignore list
-- local aiCollisionTrigger = courseplay:findAiCollisionTrigger(object)
if not courseplay:findAiCollisionTrigger(object) then return end
if object.aiCollisionTrigger then
self:debug('-- %q', getName(object.aiCollisionTrigger))
self.ignoredNodes[object.aiCollisionTrigger] = true
courseplay:findAiCollisionTrigger(object) -- get aiTrafficCollisionTrigger for vehicles
if object.aiTrafficCollisionTrigger then
self:debug('-- %q', getName(object.aiTrafficCollisionTrigger))
self.ignoredNodes[object.aiTrafficCollisionTrigger] = true
end
if object.components then
self:debug('will ignore collisions with %q (%q) components', nameNum(object), tostring(object.cp.xmlFileName))
Expand Down Expand Up @@ -164,7 +167,7 @@ function CollisionDetector:getStatus(dt)
if collidingVehicle.isCpPathVehicle then
self:setPathVehiclesSpeed(collidingVehicle, dt)
end
if collidingVehicle.lastSpeedReal == nil or collidingVehicle.lastSpeedReal*3600 == 0 or not self:doesVehicleGoMyDirection(collidingVehicleId) then
if collidingVehicle.lastSpeedReal == nil or collidingVehicle.lastSpeedReal*3600 < 0.01 then -- collidingVehicle not moving -> STOP
isInTraffic = true
else
trafficSpeed = collidingVehicle.lastSpeedReal*3600
Expand Down
12 changes: 10 additions & 2 deletions UnloadableFieldworkAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ function UnloadableFieldworkAIDriver:init(vehicle)
self:initStates(UnloadableFieldworkAIDriver.myStates)
self.mode = courseplay.MODE_FIELDWORK
self.stopImplementsWhileUnloadOrRefillOnField = false
self.lastEmptyTimestamp = 0

end

function UnloadableFieldworkAIDriver:setHudContent()
Expand All @@ -65,6 +63,16 @@ function UnloadableFieldworkAIDriver.create(vehicle)
end
end

-- Bale loaders / wrappers have no AI markers
function UnloadableFieldworkAIDriver.getAIMarkersFromGrabberNode(object, spec)
-- use the grabber node for all markers if exists
if spec.baleGrabber and spec.baleGrabber.grabNode then
return spec.baleGrabber.grabNode, spec.baleGrabber.grabNode, spec.baleGrabber.grabNode
else
return object.rootNode, object.rootNode, object.rootNode
end
end

function UnloadableFieldworkAIDriver:drive(dt)
-- only reason we need this is to update the totalFillLevel for reverse.lua so it will
-- do a raycast for tip triggers (side effects, side effects all over the place, killing me...)
Expand Down
26 changes: 0 additions & 26 deletions debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -486,32 +486,6 @@ end

function courseplay:showAIMarkers(vehicle)

if not courseplay.debugChannels[6] then return end

local function showAIMarkersOfObject(object)
if object.getAIMarkers then
local aiLeftMarker, aiRightMarker, aiBackMarker = object:getAIMarkers()
if aiLeftMarker then
DebugUtil.drawDebugNode(aiLeftMarker, object:getName() .. ' AI Left')
end
if aiRightMarker then
DebugUtil.drawDebugNode(aiRightMarker, object:getName() .. ' AI Right')
end
if aiBackMarker then
DebugUtil.drawDebugNode(aiBackMarker, object:getName() .. ' AI Back')
end
DebugUtil.drawDebugNode(object.cp.DirectionNode or object.rootNode, object:getName() .. ' root')
end
end

showAIMarkersOfObject(vehicle)
-- draw the Giant's supplied AI markers for all implements
local implements = vehicle:getAttachedImplements()
if implements then
for _, implement in ipairs(implements) do
showAIMarkersOfObject(implement.object)
end
end
end

--------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function courseplay:nilOrBool(variable, bool)
end;

function nameNum(vehicle, hideNum)
if vehicle == nil then
if vehicle == nil or not vehicle.getName then
return 'nil';
end;

Expand Down
2 changes: 1 addition & 1 deletion modDesc.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<modDesc descVersion="40">
<version>6.01.00246</version>
<version>6.01.00254</version>
<author><![CDATA[Courseplay.devTeam]]></author>
<title>
<br>CoursePlay SIX</br>
Expand Down
8 changes: 8 additions & 0 deletions turn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,14 @@ function courseplay:turn(vehicle, dt, turnContext)
----------------------------------------------------------
--allowedToDrive = allowedToDrive and not courseplay:needToWaitForTools(vehicle)


if vehicle.cp.drivingMode:get() == DrivingModeSetting.DRIVING_MODE_AIDRIVER then
-- in AIDriver based modes do not call / use legacy collision code
allowedToDrive = vehicle.cp.driver:detectCollision(dt)
else
-- allowedToDrive = courseplay:checkTraffic(self, true, allowedToDrive)
end

----------------------------------------------------------
-- allowedToDrive false -> SLOW DOWN TO STOP
----------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion vehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ function courseplay:findAiCollisionTrigger(vehicle)
end;
end;

if vehicle.aiTrafficCollisionTrigger == nil then
if vehicle.aiTrafficCollisionTrigger == nil and SpecializationUtil.hasSpecialization(AIVehicle, vehicle.specializations) then
print(string.format('## Courseplay: aiTrafficCollisionTrigger missing. Traffic collision prevention will not work! vehicle %s', nameNum(vehicle)));
end;

Expand Down

0 comments on commit 07f984b

Please sign in to comment.