Skip to content

Commit

Permalink
Some more code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Oct 15, 2024
1 parent 91f76e6 commit aac7b6d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 167 deletions.
34 changes: 2 additions & 32 deletions scripts/ai/controllers/BalerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function BalerController:init(vehicle, baler)
self:debug('Baler controller initialized')
local additives = self.balerSpec.additives
if additives.available then
self.refillData.lastFillLevels[self.implement][additives.fillUnitIndex] = -1
self:addRefillImplementAndFillUnit(self.implement, additives.fillUnitIndex)
end
end

Expand All @@ -40,15 +40,7 @@ function BalerController:getDriveData()
end

function BalerController:update()
if self.settings.useAdditiveFillUnit:getValue() then
--- If the silage additive is empty, then stop the driver.
if self.balerSpec.additives.available then
if self.implement:getFillUnitFillLevelPercentage(self.balerSpec.additives.fillUnitIndex) <= 0 then
self:debug("Stopped Cp, as the additive fill unit is empty.")
self.vehicle:stopCurrentAIJob(AIMessageErrorOutOfFill.new())
end
end
end
self:updateAdditiveFillUnitEmpty(self.balerSpec.additives)
end

function BalerController:handleBaler()
Expand Down Expand Up @@ -176,26 +168,4 @@ function BalerController:canContinueWork()
return true
end
return spec.foldAnimTime == 0 or spec.foldAnimTime == 1
end

-------------------------
--- Refill handling
-------------------------

function BalerController:needsRefilling()
if self.balerSpec.additives.available then
if self.implement:getFillUnitFillLevelPercentage(self.balerSpec.additives.fillUnitIndex) <= 0 then
return ImplementController.needsRefilling(self)
end
end
return false
end

function BalerController:onStartRefilling()
if self:needsRefilling() then
if self.implement.aiPrepareLoading ~= nil then
self.implement:aiPrepareLoading(self.balerSpec.additives.fillUnitIndex)
end
end
ImplementController.onStartRefilling(self)
end
35 changes: 2 additions & 33 deletions scripts/ai/controllers/CombineController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,12 @@ function CombineController:init(vehicle, combine)
self.isWheeledImplement = ImplementUtil.isWheeledImplement(combine)
local additives = self.combineSpec.additives
if additives.available then
self.refillData.lastFillLevels[self.implement][additives.fillUnitIndex] = -1
self:addRefillImplementAndFillUnit(self.implement, additives.fillUnitIndex)
end
end

function CombineController:update()
if self.settings.useAdditiveFillUnit:getValue() then
--- If the silage additive is empty, then stop the driver.
local additives = self.combineSpec.additives
if additives.available then
if self.implement:getFillUnitFillLevelPercentage(additives.fillUnitIndex) <= 0 then
self:debug("Stopped Cp, as the additive fill unit is empty.")
self.vehicle:stopCurrentAIJob(AIMessageErrorOutOfFill.new())
end
end
end
self:updateAdditiveFillUnitEmpty(self.combineSpec.additives)
end

function CombineController:getDriveData()
Expand Down Expand Up @@ -229,25 +220,3 @@ function CombineController:updateChopperFillType()
end
end
end

-------------------------
--- Refill handling
-------------------------

function CombineController:needsRefilling()
if self.combineSpec.additives.available then
if self.implement:getFillUnitFillLevelPercentage(self.combineSpec.additives.fillUnitIndex) <= 0 then
return ImplementController.needsRefilling(self)
end
end
return false
end

function CombineController:onStartRefilling()
if self:needsRefilling() then
if self.implement.aiPrepareLoading ~= nil then
self.implement:aiPrepareLoading(self.combineSpec.additives.fillUnitIndex)
end
end
ImplementController.onStartRefilling(self)
end
35 changes: 2 additions & 33 deletions scripts/ai/controllers/ForageWagonController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function ForageWagonController:init(vehicle, forageWagon)
self.settings = vehicle:getCpSettings()
local additives = self.forageWagonSpec.additives
if additives.available then
self.refillData.lastFillLevels[self.implement][additives.fillUnitIndex] = -1
self:addRefillImplementAndFillUnit(self.implement, additives.fillUnitIndex)
end
end

Expand All @@ -20,16 +20,7 @@ function ForageWagonController:update()
self:debug("Stopped Cp, as the forage wagon is full.")
self.vehicle:stopCurrentAIJob(AIMessageErrorIsFull.new())
end
if self.settings.useAdditiveFillUnit:getValue() then
--- If the silage additive is empty, then stop the driver.
local additives = self.forageWagonSpec.additives
if additives.available then
if self.implement:getFillUnitFillLevelPercentage(additives.fillUnitIndex) <= 0 then
self:debug("Stopped Cp, as the additive fill unit is empty.")
self.vehicle:stopCurrentAIJob(AIMessageErrorOutOfFill.new())
end
end
end
self:updateAdditiveFillUnitEmpty(self.forageWagonSpec.additives)
end

function ForageWagonController:getDriveData()
Expand All @@ -41,26 +32,4 @@ function ForageWagonController:getDriveData()
maxSpeed = 5 + freeFillLevel / self.slowDownFillLevel * self.slowDownStartSpeed
end
return nil, nil, nil, maxSpeed
end

-------------------------
--- Refill handling
-------------------------

function ForageWagonController:needsRefilling()
if self.forageWagonSpec.additives.available then
if self.implement:getFillUnitFillLevelPercentage(self.forageWagonSpec.additives.fillUnitIndex) <= 0 then
return ImplementController.needsRefilling(self)
end
end
return false
end

function ForageWagonController:onStartRefilling()
if self:needsRefilling() then
if self.implement.aiPrepareLoading ~= nil then
self.implement:aiPrepareLoading(self.forageWagonSpec.additives.fillUnitIndex)
end
end
ImplementController.onStartRefilling(self)
end
85 changes: 63 additions & 22 deletions scripts/ai/controllers/ImplementController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ function ImplementController:init(vehicle, implement)
self.settings = vehicle:getCpSettings()
self.disabledStates = {}
self.refillData = {
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {
[self.implement] = {}
}
}
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {}
}
end

--- Get the controlled implement
Expand Down Expand Up @@ -72,7 +70,7 @@ function ImplementController:update(dt)
end

function ImplementController:delete()

end

--- Called by the drive strategy on lowering of the implements.
Expand All @@ -98,7 +96,8 @@ end
function ImplementController:onFinishRow(isHeadlandTurn)
end

function ImplementController:onTurnEndProgress(workStartNode, reversing, shouldLower, isLeftTurn)
function ImplementController:onTurnEndProgress(workStartNode, reversing,
shouldLower, isLeftTurn)
end

--- Any object this controller wants us to ignore, can register here a callback at the proximity controller
Expand All @@ -122,33 +121,73 @@ function ImplementController:canContinueWork()
return true
end

--- Stops the drive if the use additive fillunit setting is active and the tank is empty.
---@param additives table
function ImplementController:updateAdditiveFillUnitEmpty(additives)

if self.settings.useAdditiveFillUnit:getValue() then
--- If the silage additive is empty, then stop the driver.
if additives.available then
if self.implement:getFillUnitFillLevelPercentage(
additives.fillUnitIndex) <= 0 then
self:debug('Stopped Cp, as the additive fill unit is empty.')
self.vehicle:stopCurrentAIJob(AIMessageErrorOutOfFill.new())
end
end
end
end

-------------------------------------
--- Refill
-------------------------------------

--- Registers an implement and a fill unit for a possible refilling later.
---@param implement table
---@param fillUnitIndex number
function ImplementController:addRefillImplementAndFillUnit(implement,
fillUnitIndex)
if self.refillData.lastFillLevels[implement] == nil then
self.refillData.lastFillLevels[implement] = {}
end
self.refillData.lastFillLevels[implement][fillUnitIndex] = -1
end

function ImplementController:isRefillingAllowed()
return next(self.refillData.lastFillLevels[self.implement]) ~= nil
return next(self.refillData.lastFillLevels) ~= nil
end

function ImplementController:needsRefilling()
return self:isRefillingAllowed()
return self:isRefillingAllowed()
end

function ImplementController:onStartRefilling()
function ImplementController:onStartRefilling()
if self:isRefillingAllowed() then
if self:needsRefilling() then
if self:needsRefilling() then
for implement, data in pairs(self.refillData.lastFillLevels) do
for fillUnitIndex, _ in pairs(data) do
self:debug('Preparing %s for loading with fill unit index: %d',
CpUtil.getName(implement), fillUnitIndex)
if implement.aiPrepareLoading ~= nil then
implement:aiPrepareLoading(fillUnitIndex)
end
end
end
self.refillData.timer:set(false, 30 * 1000)
end
self.refillData.hasChanged = false
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels, true)
end
end

--- Checks if loading from a nearby fill trigger is possible or
--- if the fill level is currently being changed by for example an auger wagon.
---@return boolean dirty at least one fill unit is currently being filled.
---@return boolean changed at least one fill unit fill level has been changed since the start.
function ImplementController:onUpdateRefilling()
if self:isRefillingAllowed() then
if ImplementUtil.tryAndCheckRefillingFillUnits(self.refillData.lastFillLevels) or
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels) then
if ImplementUtil.tryAndCheckRefillingFillUnits(self.refillData.lastFillLevels) or
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels) then

self.refillData.timer:set(false, 10 * 1000)
self.refillData.hasChanged = true
end
Expand All @@ -159,12 +198,14 @@ end

function ImplementController:onStopRefilling()
if self:isRefillingAllowed() then
if self.implement.aiFinishLoading ~= nil then
self.implement:aiFinishLoading()
end
local spec = self.implement.spec_fillUnit
if spec and spec.fillTrigger.isFilling then
self.implement:setFillUnitIsFilling(false)
for implement, _ in pairs(self.refillData.lastFillLevels) do
if implement.aiFinishLoading ~= nil then
implement:aiFinishLoading()
end
local spec = implement.spec_fillUnit
if spec and spec.fillTrigger.isFilling then
implement:setFillUnitIsFilling(false)
end
end
end
end
end
1 change: 0 additions & 1 deletion scripts/ai/controllers/MotorController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ function MotorController:update()
end
end
local needsFuelLowInfo, needsFuelEmptyInfo = false, false
--- Only apply this if no refueling is active.
if self:isFuelLow(self.fuelThresholdSetting:getValue()) then
if not g_Courseplay.globalSettings.waitForRefueling:getValue() then
self.vehicle:stopCurrentAIJob(AIMessageErrorOutOfFuel.new())
Expand Down
11 changes: 1 addition & 10 deletions scripts/ai/controllers/SowingMachineController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SowingMachineController = CpObject(ImplementController)
function SowingMachineController:init(vehicle, implement)
ImplementController.init(self, vehicle, implement)
self.sowingMachineSpec = self.implement.spec_sowingMachine
self.refillData.lastFillLevels[self.implement][self.sowingMachineSpec.fillUnitIndex] = -1
self:addRefillImplementAndFillUnit(self.implement, self.sowingMachineSpec.fillUnitIndex)
end

function SowingMachineController:update()
Expand Down Expand Up @@ -51,13 +51,4 @@ function SowingMachineController:needsRefilling()
end
end
return false
end

function SowingMachineController:onStartRefilling()
if self:needsRefilling() then
if self.implement.aiPrepareLoading ~= nil then
self.implement:aiPrepareLoading(self.sowingMachineSpec.fillUnitIndex)
end
end
ImplementController.onStartRefilling(self)
end
38 changes: 2 additions & 36 deletions scripts/ai/controllers/SprayerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ function SprayerController:init(vehicle, sprayer)
self.sprayer = sprayer
self.sprayerSpec = sprayer.spec_sprayer
ImplementController.init(self, vehicle, self.sprayer)
self.refillData.lastFillLevels[self.implement][self.implement:getSprayerFillUnitIndex()] = -1
self:addRefillImplementAndFillUnit(self.implement, self.implement:getSprayerFillUnitIndex())
for _, supportedSprayType in ipairs(self.sprayerSpec.supportedSprayTypes) do
for _, src in ipairs(self.sprayerSpec.fillTypeSources[supportedSprayType]) do
self:debug("Found additional tank for refilling: %s|%d", src.vehicle, src.fillUnitIndex)
if not self.refillData.lastFillLevels[src.vehicle] then
self.refillData.lastFillLevels[src.vehicle] = {}
end
self.refillData.lastFillLevels[src.vehicle][src.fillUnitIndex] = -1
self:addRefillImplementAndFillUnit(src.vehicle, src.fillUnitIndex)
end
end
end
Expand All @@ -43,37 +40,6 @@ function SprayerController:needsRefilling()
return false
end

function SprayerController:onStartRefilling()
if self:needsRefilling() then
if self.implement.aiPrepareLoading ~= nil then
self.implement:aiPrepareLoading(self.implement:getSprayerFillUnitIndex())
end
for _, supportedSprayType in ipairs(self.sprayerSpec.supportedSprayTypes) do
for _, src in ipairs(self.sprayerSpec.fillTypeSources[supportedSprayType]) do
if src.vehicle.aiPrepareLoading ~= nil then
src.vehicle:aiPrepareLoading(src.fillUnitIndex)
end
end
end
end
ImplementController.onStartRefilling(self)
end

function SprayerController:onStopRefilling()
ImplementController.onStopRefilling(self)
for _, supportedSprayType in ipairs(self.sprayerSpec.supportedSprayTypes) do
for _, src in ipairs(self.sprayerSpec.fillTypeSources[supportedSprayType]) do
if src.vehicle.aiFinishLoading ~= nil then
src.vehicle:aiFinishLoading()
end
local spec = src.vehicle.spec_fillUnit
if spec.fillTrigger.isFilling then
src.vehicle:setFillUnitIsFilling(false)
end
end
end
end

local function processSprayerArea(sprayer, superFunc, ...)
local rootVehicle = sprayer.rootVehicle
if rootVehicle.getIsCpActive and rootVehicle:getIsCpActive() then
Expand Down

0 comments on commit aac7b6d

Please sign in to comment.