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

Commit

Permalink
Some MP, mode9 and baler improvements (#6381)
Browse files Browse the repository at this point in the history
* Some MP, mode9 and baler improvements

-fix mode 2/3 in MP
-reduce network load by only sending updated values
-improved mode 9 raycast
-improved baler handling
-diff small changes
-enabled fueling by trailer
-mode 1/4 wait while they get loaded
-fixes mode 4
-improved shovelPositions
-mode 3 code cleanUp
-StateModule only for TriggerHandler for now
  • Loading branch information
schwiti6190 authored Nov 27, 2020
1 parent 638c571 commit e0382c4
Show file tree
Hide file tree
Showing 26 changed files with 680 additions and 516 deletions.
50 changes: 42 additions & 8 deletions AIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,42 @@ end

function AIDriver:writeUpdateStream(streamId, connection, dirtyMask)
self.triggerHandler:writeUpdateStream(streamId)
streamWriteString(streamId,self.state.name)
if self.active then
if self.state ~= self.stateSend then
streamWriteBool(streamId,true)
streamWriteString(streamId,self.state.name)
self.stateSend = self.state
else
streamWriteBool(streamId,false)
end
-- streamWriteBool(streamId,self.vehicle.cp.isDriving)
if self.active ~= self.activeSend then
streamWriteBool(streamId,true)
streamWriteBool(streamId,self.active or false)
self.activeSend = self.active
else
streamWriteBool(streamId,false)
end
end

function AIDriver:readUpdateStream(streamId, timestamp, connection)
self.triggerHandler:readUpdateStream(streamId)
local nameState = streamReadString(streamId)
self.state = self.states[nameState]
self.active = streamReadBool(streamId)
-- self.vehicle.cp.isDriving = streamReadBool(streamId)
if streamReadBool(streamId) then
local nameState = streamReadString(streamId)
self.state = self.states[nameState]
end
if streamReadBool(streamId) then
self.active = streamReadBool(streamId)
end
end

function AIDriver:postSync()
function AIDriver:onWriteStream(streamId)
streamWriteString(streamId,self.state.name)
streamWriteBool(streamId,self.active or false)
end

function AIDriver:onReadStream(streamId)
local nameState = streamReadString(streamId)
self.state = self.states[nameState]
self.active = streamReadBool(streamId)
end

function AIDriver:setHudContent()
Expand Down Expand Up @@ -518,6 +535,13 @@ function AIDriver:driveVehicleToLocalPosition(dt, allowedToDrive, moveForwards,
AIVehicleUtil.driveToPoint(self.vehicle, dt, self.acceleration, allowedToDrive, moveForwards, ax, az, maxSpeed, false)
end

--[[ emergency brake, maybe for mode 4/8 refill at trigger ??
local oldFunc = self.vehicle.getBrakeForce
self.vehicle.getBrakeForce = function () return 10000000 end
AIVehicleUtil.driveToPoint(self.vehicle, dt, self.acceleration, allowedToDrive, moveForwards, ax, az, maxSpeed, false)
self.vehicle.getBrakeForce = oldFunc
]]--

-- many courseplay modes control the vehicle through the lx/lz normalized local directions.
-- this is an interface for those modes to drive the vehicle.
function AIDriver:driveVehicleInDirection(dt, allowedToDrive, moveForwards, lx, lz, maxSpeed)
Expand Down Expand Up @@ -2109,3 +2133,13 @@ end
function AIDriver:getCanShowDriveOnButton()
return self.triggerHandler:isLoading() or self.triggerHandler:isUnloading() or self:isWaiting()
end

--disable detaching, while CP is driving
function AIDriver:isDetachAllowed(superFunc,preSuperFunc)
local rootVehicle = self:getRootVehicle()
if courseplay:isAIDriverActive(rootVehicle) then
return false
end
return superFunc(self,preSuperFunc)
end
AttacherJoints.isDetachAllowed = Utils.overwrittenFunction(AttacherJoints.isDetachAllowed, AIDriver.isDetachAllowed)
88 changes: 49 additions & 39 deletions BalerAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ function BalerAIDriver:init(vehicle)
courseplay.debugVehicle(11,vehicle,'BalerAIDriver:init()')
UnloadableFieldworkAIDriver.init(self, vehicle)
self.baler = AIDriverUtil.getAIImplementWithSpecialization(vehicle, Baler)
self.balerSpec = self.baler.spec_baler
self.slowDownFillLevel = 200
self.slowDownStartSpeed = 20
end

function BalerAIDriver:driveFieldwork(dt)
Expand All @@ -34,6 +37,19 @@ function BalerAIDriver:driveFieldwork(dt)
return UnloadableFieldworkAIDriver.driveFieldwork(self, dt)
end

function BalerAIDriver:start(startingPoint)
UnloadableFieldworkAIDriver.start(self,startingPoint)
--use giants automaticDrop, so we don't have to do it
self.oldAutomaticDrop = self.balerSpec.automaticDrop
self.balerSpec.automaticDrop = true
end

function BalerAIDriver:dismiss()
UnloadableFieldworkAIDriver.dismiss(self)
--revert possible change for the player to default
self.balerSpec.automaticDrop = self.oldAutomaticDrop
end

function BalerAIDriver:allFillLevelsOk()
-- always fine, we'll stop when needed in driveFieldwork()
return true
Expand All @@ -50,48 +66,42 @@ end
function BalerAIDriver:handleBaler()
-- turn.lua will raise/lower as needed, don't touch the balers while the turn maneuver is executed or while on temporary alignment / connecting track
if not self:isHandlingAllowed() then return end
--if vehicle.cp.waypointIndex >= vehicle.cp.startWork + 1 and vehicle.cp.waypointIndex < vehicle.cp.stopWork and vehicle.cp.turnStage == 0 then
-- vehicle, self.baler, unfold, lower, turnOn, allowedToDrive, cover, unload, ridgeMarker,forceSpeedLimit,workSpeed)
local specialTool, allowedToDrive, stoppedForReason = courseplay:handleSpecialTools(self.vehicle, self.baler, true, true, true, true, nil, nil, nil);
if not specialTool then
-- automatic opening for balers
local capacity = self.baler.cp.capacity
local fillLevel = self.baler.cp.fillLevel
if self.baler.spec_baler ~= nil then
--print(string.format("if courseplay:isRoundbaler(self.baler)(%s) and fillLevel(%s) > capacity(%s) * 0.9 and fillLevel < capacity and self.baler.spec_baler.unloadingState(%s) == Baler.UNLOADING_CLOSED(%s) then",
--tostring(courseplay:isRoundbaler(self.baler)),tostring(fillLevel),tostring(capacity),tostring(self.baler.spec_baler.unloadingState),tostring(Baler.UNLOADING_CLOSED)))
if courseplay:isRoundbaler(self.baler) and fillLevel > capacity * 0.9 and fillLevel < capacity and self.baler.spec_baler.unloadingState == Baler.UNLOADING_CLOSED then
if not self.baler.spec_turnOnVehicle.isTurnedOn and not stoppedForReason then
self.baler:setIsTurnedOn(true, false)
end
self:setSpeed(self.vehicle.cp.speeds.turn)
elseif fillLevel >= capacity and self.baler.spec_baler.unloadingState == Baler.UNLOADING_CLOSED then
allowedToDrive = false;
if #(self.baler.spec_baler.bales) > 0 and self.baler.spec_baleWrapper == nil then --Ensures the baler wrapper combo is empty before unloading
self.baler:setIsUnloadingBale(true, false)
end
elseif self.baler.spec_baler.unloadingState ~= Baler.UNLOADING_CLOSED 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
self.baler:setIsTurnedOn(true, false);

if not self.baler:getIsTurnedOn() then
if self.baler:getCanBeTurnedOn() then
self.baler:setIsTurnedOn(true, false);
else --maybe this line is enough to handle bale dropping and waiting ?
self:setSpeed(0)
--baler needs refilling of some sort (net,...)
if self.balerSpec.unloadingState == Baler.UNLOADING_CLOSED then
CpManager:setGlobalInfoText(self.vehicle, 'NEEDS_REFILLING');
end
end
if self.baler.setPickupState ~= nil then
if self.baler.spec_pickup ~= nil and not self.baler.spec_pickup.isLowered then
self.baler:setPickupState(true, false)
courseplay:debug('lowering baler pickup')
end
end

if self.baler.setPickupState ~= nil then -- lower pickup after unloading
if self.baler.spec_pickup ~= nil and not self.baler.spec_pickup.isLowered then
self.baler:setPickupState(true, false)
self:debug('lowering baler pickup')
end
end
if not allowedToDrive then
self:setSpeed(0)

local fillLevel = self.baler:getFillUnitFillLevel(self.balerSpec.fillUnitIndex)
local capacity = self.baler:getFillUnitCapacity(self.balerSpec.fillUnitIndex)

if not self.balerSpec.nonStopBaling and (self.balerSpec.baleUnloadAnimationName ~= nil or self.balerSpec.allowsBaleUnloading) then
self:debugSparse("baleUnloadAnimationName: %s, allowsBaleUnloading: %s, nonStopBaling:%s",tostring(self.balerSpec.baleUnloadAnimationName),tostring(self.balerSpec.allowsBaleUnloading),tostring(self.balerSpec.nonStopBaling))
--copy of giants code: AIDriveStrategyBaler:getDriveData(dt, vX,vY,vZ) to avoid leftover when full
local freeFillLevel = capacity - fillLevel
if freeFillLevel < self.slowDownFillLevel then
maxSpeed = 2 + (freeFillLevel / self.slowDownFillLevel) * self.slowDownStartSpeed
self:setSpeed(maxSpeed)
end

--baler is full or is unloading so wait!
if fillLevel == capacity or self.balerSpec.unloadingState ~= Baler.UNLOADING_CLOSED then
self:setSpeed(0)
end
end

return true
end
end
4 changes: 0 additions & 4 deletions CombineAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,6 @@ function CombineAIDriver:getCombine()
return self.combine
end

function CombineAIDriver:postSync()
--TODO: figure out if we need this or not for multiplayer ??
end

function CombineAIDriver:start(startingPoint)
self:clearAllUnloaderInformation()
self:addBackwardProximitySensor()
Expand Down
14 changes: 10 additions & 4 deletions CombineUnloadAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ function CombineUnloadAIDriver:init(vehicle)
self.combineToUnloadReversing = 0
end

function CombineUnloadAIDriver:postSync()
self.assignedCombinesSetting:sendPostSyncRequestEvent()
end

function CombineUnloadAIDriver:getAssignedCombines()
return self.assignedCombinesSetting:getData()
end
Expand All @@ -138,6 +134,16 @@ function CombineUnloadAIDriver:setHudContent()
courseplay.hud:setCombineUnloadAIDriverContent(self.vehicle,self.assignedCombinesSetting)
end

function CombineUnloadAIDriver:onWriteStream(streamId)
self.assignedCombinesSetting:onWriteStream(streamId)
AIDriver.onWriteStream(self,streamId)
end

function CombineUnloadAIDriver:onReadStream(streamId)
self.assignedCombinesSetting:onReadStream(streamId)
AIDriver.onReadStream(self,streamId)
end

function CombineUnloadAIDriver:debug(...)
local combineName = self.combineToUnload and (' -> ' .. nameNum(self.combineToUnload)) or '(unassigned)'
courseplay.debugVehicle(self.debugChannel, self.vehicle, combineName .. ': ' .. string.format( ... ))
Expand Down
2 changes: 1 addition & 1 deletion CombineUnloadManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function CombineUnloadManager:addNewCombines()
-- this isn't needed as combines will be added when an CombineAIDriver is created for them
-- but we want to be able to reload this file on the fly when developing/troubleshooting
for _, vehicle in pairs(g_currentMission.vehicles) do
if vehicle.cp.driver and vehicle.cp.driver:is_a(CombineAIDriver) and not self.combines[vehicle] then
if vehicle.cp.driver and vehicle.cp.driver.is_a and vehicle.cp.driver:is_a(CombineAIDriver) and not self.combines[vehicle] then
self:addCombineToList(vehicle, vehicle.cp.driver)
end
end
Expand Down
55 changes: 39 additions & 16 deletions CpManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,12 @@ end;
function CpManager:devAddMoney()
if g_server ~= nil then
g_currentMission:addMoney(5000000,1, MoneyType.OTHER,true);
return ('Added %s to your bank account'):format(g_i18n:formatMoney(5000000));
end;
else
CommandEvents.sendEvent("devAddMoney")
end
return ('Added %s to your bank account'):format(g_i18n:formatMoney(5000000));
end;

function CpManager:devAddFillLevels()
--[[ Ryan TODO FillUtil.NUM_FILLTYPES doesn't have exist in g_fillTypeManager. Also the set and get functions might not exist there any more
if g_server ~= nil then
Expand All @@ -500,14 +503,19 @@ function CpManager:devStopAll()
for _,vehicle in pairs (self.activeCoursePlayers) do
courseplay:stop(vehicle);
end

return ('stopped all Courseplayers');
end;
else
CommandEvents.sendEvent("devStopAll")
end
return ('stopped all Courseplayers');
end;

function CpManager:devSaveAllFields()
courseplay.fields.saveAllFields()
return( 'All fields saved' )
if g_server then
courseplay.fields.saveAllFields()
else
CommandEvents.sendEvent("devSaveAllFields")
end
return( 'All fields saved' )
end

--- Print a global variable
Expand Down Expand Up @@ -673,28 +681,43 @@ function CpManager:loadAIDriver()
end

function CpManager:saveVehiclePositions()
DevHelper.saveAllVehiclePositions()
if g_server then
DevHelper.saveAllVehiclePositions()
else
CommandEvents.sendEvent("saveVehiclePositions")
end
end

function CpManager:restoreVehiclePositions()
DevHelper.restoreAllVehiclePositions()
if g_server then
DevHelper.restoreAllVehiclePositions()
else
CommandEvents.sendEvent("restoreVehiclePositions")
end
end

function CpManager:restartSaveGame(saveGameNumber)
restartApplication(" -autoStartSavegameId " .. saveGameNumber)
if g_server then
restartApplication(" -autoStartSavegameId " .. saveGameNumber)
end
end

function CpManager:showCombineUnloadManagerStatus()
g_combineUnloadManager:printStatus()
end

function CpManager:setLookaheadDistance(d)
local vehicle = g_currentMission.controlledVehicle
if vehicle and vehicle.cp and vehicle.cp.ppc then
vehicle.cp.ppc:setLookaheadDistance(d)
print('Look ahead distance for ' .. vehicle.name .. ' changed to ' .. tostring(d))
else
print('No vehicle or has no PPC.')
if g_server then
local vehicle = g_currentMission.controlledVehicle
if vehicle and vehicle.cp and vehicle.cp.ppc then
vehicle.cp.ppc:setLookaheadDistance(d)
print('Look ahead distance for ' .. vehicle.name .. ' changed to ' .. tostring(d))
else
print('No vehicle or has no PPC.')
end
else
CommandEvents.sendEvent("setLookaheadDistance",d)
print('trying to change LookaheadDistance.')
end
end

Expand Down
Loading

0 comments on commit e0382c4

Please sign in to comment.