Skip to content

Commit

Permalink
6.01.00112: moved tipper unloading to AIDriver
Browse files Browse the repository at this point in the history
BGA reverse handling: record the course reverse into the silo. the last rev waypoint will be the unload point for the tipper. then the unloading will be handled as with the  reverse unload point.
  • Loading branch information
ThomasGaertner committed Mar 3, 2019
1 parent 410592e commit 906d414
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 61 deletions.
94 changes: 91 additions & 3 deletions AIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ function AIDriver:driveCourse(dt)
end

-- use the recorded speed by default
self:setSpeed(self:getRecordedSpeed())

if not self:hasTipTrigger() then
self:setSpeed(self:getRecordedSpeed())
end

if self:getIsInFilltrigger() then
self:setSpeed(self.vehicle.cp.speeds.approach)
end
Expand Down Expand Up @@ -501,6 +503,9 @@ function AIDriver:isWaiting()
return self.state == self.states.STOPPED
end

function AIDriver:hasTipTrigger()
return self.vehicle.cp.currentTipTrigger ~= nil
end

--- Set the speed. The idea is that self.speed is reset at the beginning of every loop and
-- every function calls setSpeed() and the speed will be set to the minimum
Expand Down Expand Up @@ -717,7 +722,7 @@ function AIDriver:dischargeAtUnloadPoint(dt,unloadPointIx)
z = courseplay:isNodeTurnedWrongWay(vehicle,tipRefpoint)and -z or z

local foundHeap = self:checkForHeapBehindMe(tipper)
print(string.format("foundHeap(%s) or z(%s) >= 0",tostring(foundHeap),tostring(z)))
--print(string.format("foundHeap(%s) or z(%s) >= 0",tostring(foundHeap),tostring(z)))

--when we reached the unload point, stop the tractor and inhibit any action from ppc till the trailer is empty
if (foundHeap or z >= 0) and tipper.cp.fillLevel ~= 0 or tipper:getTipState() ~= Trailer.TIPSTATE_CLOSED then
Expand Down Expand Up @@ -800,3 +805,86 @@ function AIDriver:checkForHeapBehindMe(tipper)
return true;
end
end

function AIDriver:dischargeAtTipTrigger(dt)
local trigger = self.vehicle.cp.currentTipTrigger
local allowedToDrive = true
if trigger ~= nil then
local isBGA = trigger.bunkerSilo ~= nil;
if isBGA then
if not self.ppc:isReversing() then
--we are going forward into the BGA silo, so tip when I'm in and adjust the speed
self:tipIntoBGASiloTipTrigger(dt)
else
--we are reversing into the BGA Silo. We are taking the last rev waypoint as virtual unloadpoint and start tipping there the same way as on unload point
allowedToDrive, takeOverSteering = self:dischargeAtUnloadPoint(dt,self.course:getLastReverseAt(self.ppc:getCurrentWaypointIx()))
end
else
--using all standard tip triggers
allowedToDrive = self:tipIntoStandardTipTrigger()
end;
end
return allowedToDrive,takeOverSteering
end

function AIDriver:tipIntoStandardTipTrigger()
local stopForTipping = false
for _, tipper in pairs(self.vehicle.cp.workTools) do
if tipper.spec_dischargeable ~= nil then
for i=1,#tipper.spec_dischargeable.dischargeNodes do
if tipper:getCanDischargeToObject(tipper.spec_dischargeable.dischargeNodes[i])then
tipper:setDischargeState(Dischargeable.DISCHARGE_STATE_OBJECT)
stopForTipping = true
end
end
end
end

return not stopForTipping
end

function AIDriver:tipIntoBGASiloTipTrigger(dt)
local trigger = self.vehicle.cp.currentTipTrigger
for _, tipper in pairs (self.vehicle.cp.workTools) do
if tipper.spec_dischargeable ~= nil and trigger ~= nil then
--figure out , when i'm in the silo area
local currentDischargeNode = tipper:getCurrentDischargeNode().node
local x,y,z = getWorldTranslation(currentDischargeNode)
local tx,ty,tz = x,y,z+1
local x1,z1 = trigger.bunkerSiloArea.sx,trigger.bunkerSiloArea.sz
local x2,z2 = trigger.bunkerSiloArea.wx,trigger.bunkerSiloArea.wz
local x3,z3 = trigger.bunkerSiloArea.hx,trigger.bunkerSiloArea.hz
local trailerInTipRange = MathUtil.hasRectangleLineIntersection2D(x1,z1,x2-x1,z2-z1,x3-x1,z3-z1,x,z,tx-x,tz-z)

--tip when I'm inside the Silo
if trailerInTipRange then
if not self.unloadSpeed then
--calculate the speed needed to unload in this silo
local sx, sy, sz = worldToLocal(trigger.triggerStartId, x, y, z);
local ex, ey, ez = worldToLocal(trigger.triggerEndId, x, y, z);
local totalLength = courseplay:distance3D(sx, sy, sz, ex, ey, ez)
local fillDelta = self.vehicle.cp.totalFillLevel / self.vehicle.cp.totalCapacity;

local animation;
if tipper.spec_animatedVehicle.animations['tipAnimationBack'] ~= nil then
animation = tipper.spec_animatedVehicle.animations['tipAnimationBack'];
else
animation = {["duration"] = 15000, ["currentTime"] = 0} --Set some defaults, so in case a weird anim name was used, at least we are not throwing an error
end
local totalTipDuration = (animation.duration- animation.currentTime)/1*fillDelta / 1000;
local meterPrSeconds = totalLength / totalTipDuration;
self.unloadSpeed = meterPrSeconds*3.6
end

local tipState = tipper:getTipState()
if tipState == Trailer.TIPSTATE_CLOSED or tipState == Trailer.TIPSTATE_CLOSING then
tipper:setDischargeState(Dischargeable.DISCHARGE_STATE_GROUND)
end
else
self.unloadSpeed = nil
end
self.speed = self.unloadSpeed or self.speed
end
end

end
44 changes: 21 additions & 23 deletions FillableFieldworkAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,29 @@ function FillableFieldworkAIDriver:searchForRefillTriggers()
-- look straight ahead for now. The rest of CP looks into the direction of the 'current waypoint'
-- but we don't have that information (lx/lz) here. See if we can get away with this, should only
-- be a problem if we have a sharp curve around the trigger
if not self:getIsInFilltrigger() then
if not self.ppc:isReversing() then
local x, y, z = localToWorld(self.vehicle.cp.DirectionNode, 0, 1, 3)
local nx, ny, nz = localDirectionToWorld(self.vehicle.cp.DirectionNode, 0, -0.1, 1)
-- raycast start point in front of vehicle
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'fwd', true, x, y, z, nx, ny, nz)
if not self.ppc:isReversing() then
local x, y, z = localToWorld(self.vehicle.cp.DirectionNode, 0, 1, 3)
local nx, ny, nz = localDirectionToWorld(self.vehicle.cp.DirectionNode, 0, -0.1, 1)
-- raycast start point in front of vehicle
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'fwd', true, x, y, z, nx, ny, nz)

--create a hammerhead racast to get small triggerStartId
local x, y, z = localToWorld(self.vehicle.cp.DirectionNode, -1.5, 1, 10)
local nx, ny, nz = localDirectionToWorld(self.vehicle.cp.DirectionNode, 1, 0, 0)
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'fwd', false, x, y, z, nx, ny, nz,3)

else
for _,workTool in pairs(self.vehicle.cp.workTools) do
local node = workTool.cp.realTurningNode or workTool.rootNode ;
local x, y, z = localToWorld(node, 0, 2, 3)
local nx, ny, nz = localDirectionToWorld(node, 0, -0.1, -1)
-- raycast start point behind the workTool
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'rev', false, x, y, z, nx, ny, nz)

--create a hammerhead racast to get small triggerStartId
local x, y, z = localToWorld(self.vehicle.cp.DirectionNode, -1.5, 1, 10)
local nx, ny, nz = localDirectionToWorld(self.vehicle.cp.DirectionNode, 1, 0, 0)
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'fwd', false, x, y, z, nx, ny, nz,3)

else
for _,workTool in pairs(self.vehicle.cp.workTools) do
local node = workTool.cp.realTurningNode or workTool.rootNode ;
local x, y, z = localToWorld(node, 0, 2, 3)
local nx, ny, nz = localDirectionToWorld(node, 0, -0.1, -1)
-- raycast start point behind the workTool
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'rev', false, x, y, z, nx, ny, nz)

--create a hammerhead racast to get small triggerStartId
local x, y, z = localToWorld(node, -1.5, 1, -10)
local nx, ny, nz = localDirectionToWorld(node, 1, 0, 0)
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'rev', false, x, y, z, nx, ny, nz,3)
end
local x, y, z = localToWorld(node, -1.5, 1, -10)
local nx, ny, nz = localDirectionToWorld(node, 1, 0, 0)
courseplay:doTriggerRaycasts(self.vehicle, 'specialTrigger', 'rev', false, x, y, z, nx, ny, nz,3)
end
end
end
Expand Down
27 changes: 3 additions & 24 deletions GrainTransportAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,6 @@ function GrainTransportAIDriver:onWaypointChange(newIx)

end

function GrainTransportAIDriver:hasTipTrigger()
-- TODO: come up with something better?
return self.vehicle.cp.currentTipTrigger ~= nil
end

function GrainTransportAIDriver:getSpeed()
if self:hasTipTrigger() then
-- slow down around the tip trigger
if self:getIsInBunksiloTrigger() then
return self.vehicle.cp.speeds.reverse
else
return 10
end
else
return AIDriver.getSpeed(self)
end
end

function GrainTransportAIDriver:getIsInBunksiloTrigger()
return self.vehicle.cp.backupUnloadSpeed ~= nil
end

function GrainTransportAIDriver:checkLastWaypoint()
local allowedToDrive = true
if self.ppc:reachedLastWaypoint() then
Expand Down Expand Up @@ -207,12 +185,13 @@ function GrainTransportAIDriver:unLoad(allowedToDrive, dt)
if self.vehicle.cp.totalFillLevel > 0
and self:hasTipTrigger()
and not self:isNearFillPoint() then
allowedToDrive, takeOverSteering = courseplay:unload_tippers(self.vehicle, allowedToDrive, dt);
self:setSpeed(self.vehicle.cp.speeds.approach)
allowedToDrive, takeOverSteering = self:dischargeAtTipTrigger(dt)
courseplay:setInfoText(self.vehicle, "COURSEPLAY_TIPTRIGGER_REACHED");
end
-- tractor reaches unloadPoint
if isNearUnloadPoint then
self:setSpeed(self.vehicle.cp.speeds.turn)
self:setSpeed(self.vehicle.cp.speeds.approach)
courseplay:setInfoText(self.vehicle, "COURSEPLAY_TIPTRIGGER_REACHED");
allowedToDrive, takeOverSteering = self:dischargeAtUnloadPoint(dt,unloadPointIx)
end
Expand Down
9 changes: 3 additions & 6 deletions UnloadableFieldworkAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ function UnloadableFieldworkAIDriver:driveUnloadOrRefill(dt)
if self.vehicle.cp.totalFillLevel > 0 then
if self:hasTipTrigger() then
-- unload at tip trigger
allowedToDrive, takeOverSteering = courseplay:unload_tippers(self.vehicle, allowedToDrive);
self:setSpeed(self.vehicle.cp.speeds.approach)
allowedToDrive, takeOverSteering = self:dischargeAtTipTrigger(dt)
courseplay:setInfoText(self.vehicle,"COURSEPLAY_TIPTRIGGER_REACHED");
self:setSpeed(self.vehicle.cp.speeds.turn)
end
end

-- tractor reaches unloadPoint
if isNearUnloadPoint then
self:setSpeed(self.vehicle.cp.speeds.turn)
self:setSpeed(self.vehicle.cp.speeds.approach)
courseplay:setInfoText(self.vehicle, "COURSEPLAY_TIPTRIGGER_REACHED");
allowedToDrive, takeOverSteering = self:dischargeAtUnloadPoint(dt,unloadPointIx)
end
Expand All @@ -105,10 +106,6 @@ function UnloadableFieldworkAIDriver:driveUnloadOrRefill(dt)
return takeOverSteering
end

function UnloadableFieldworkAIDriver:hasTipTrigger()
return self.vehicle.cp.currentTipTrigger ~= nil
end

function UnloadableFieldworkAIDriver:handlePipe()
if self.vehicle.spec_pipe then
if self.vehicle.cp.isChopper then
Expand Down
8 changes: 8 additions & 0 deletions Waypoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ function Course:isReverseAt(ix)
return self.waypoints[math.min(math.max(1, ix), #self.waypoints)].rev
end

function Course:getLastReverseAt(ix)
for i=ix,#self.waypoints do
if not self.waypoints[i].rev then
return i-1
end
end
end

function Course:isTurnStartAtIx(ix)
return self.waypoints[math.min(math.max(1, ix), #self.waypoints)].turnStart
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.00111</version>
<version>6.01.00112</version>
<author><![CDATA[Courseplay.devTeam]]></author>
<title>
<br>CoursePlay SIX</br>
Expand Down
4 changes: 2 additions & 2 deletions reverse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function courseplay:goReverse(vehicle,lx,lz,mode2)
elseif vehicle.cp.mode ~= 9 then
-- Start: Fixes issue #525
local tx, ty, tz = localToWorld(vehicle.cp.DirectionNode, 0, 1, -3);
local nx, ny, nz = localDirectionToWorld(vehicle.cp.DirectionNode, lx, 0, lz);
local nx, ny, nz = localDirectionToWorld(vehicle.cp.DirectionNode, lx, -0,1, lz);
courseplay:doTriggerRaycasts(vehicle, 'tipTrigger', 'rev', false, tx, ty, tz, nx, ny, nz);
-- End: Fixes issue #525
end
Expand Down Expand Up @@ -266,7 +266,7 @@ function courseplay:goReverse(vehicle,lx,lz,mode2)
end;

if (vehicle.cp.mode == courseplay.MODE_GRAIN_TRANSPORT or vehicle.cp.mode == courseplay.MODE_COMBI or vehicle.cp.mode == courseplay.MODE_FIELDWORK) and vehicle.cp.currentTipTrigger == nil and (vehicle.cp.totalFillLevel ~= nil and vehicle.cp.totalFillLevel > 0) then
local nx, ny, nz = localDirectionToWorld(node, lxTipper, 0, lzTipper);
local nx, ny, nz = localDirectionToWorld(node, lxTipper, -0.1, lzTipper);
courseplay:doTriggerRaycasts(vehicle, 'tipTrigger', 'rev', false, xTipper, yTipper + 1, zTipper, nx, ny, nz);
end;
courseplay:showDirection(vehicle.cp.DirectionNode,lx,lz, 0.7, 0, 1);
Expand Down
3 changes: 1 addition & 2 deletions toolManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ function courseplay:unload_tippers(vehicle, allowedToDrive,dt)
end;]]


local isBGA = ctt.bunkerSilo ~= nil and vehicle.cp.handleAsOneSilo ~= true;
local isBGA = ctt.bunkerSilo ~= nil;
local bgaIsFull = isBGA and (ctt.fillLevel >= ctt.capacity);
if isBGA then
vehicle.cp.isBGATipping = true;
Expand Down Expand Up @@ -1263,7 +1263,6 @@ function courseplay:resetTipTrigger(vehicle, changeToForward)
vehicle.cp.isUnloaded = true;
end
vehicle.cp.currentTipTrigger = nil;
vehicle.cp.handleAsOneSilo = nil; -- Used for BGA tipping
vehicle.cp.isReverseBGATipping = nil; -- Used for reverse BGA tipping
vehicle.cp.isBGATipping = false;

Expand Down

0 comments on commit 906d414

Please sign in to comment.