Skip to content

Commit

Permalink
Added missing Refill controllers and a few code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Oct 14, 2024
1 parent 6252ab0 commit 449b4f1
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 67 deletions.
26 changes: 26 additions & 0 deletions scripts/ai/controllers/BalerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function BalerController:init(vehicle, baler)
self.baleWrapperSpec = self.baler.spec_baleWrapper
self.lastDroppedBale = CpTemporaryObject()
self:debug('Baler controller initialized')
local additives = self.balerSpec.additives
if additives.available then
self.refillData[self.implement][additives.fillUnitIndex] = -1
end
end

function BalerController:getDriveData()
Expand Down Expand Up @@ -172,4 +176,26 @@ 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
26 changes: 26 additions & 0 deletions scripts/ai/controllers/CombineController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function CombineController:init(vehicle, combine)
self.beaconLightsActive = false
self.hasPipe = SpecializationUtil.hasSpecialization(Pipe, combine.specializations)
self.isWheeledImplement = ImplementUtil.isWheeledImplement(combine)
local additives = self.combineSpec.additives
if additives.available then
self.refillData[self.implement][additives.fillUnitIndex] = -1
end
end

function CombineController:update()
Expand Down Expand Up @@ -225,3 +229,25 @@ 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
25 changes: 25 additions & 0 deletions scripts/ai/controllers/ForageWagonController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function ForageWagonController:init(vehicle, forageWagon)
ImplementController.init(self, vehicle, forageWagon)
self.forageWagonSpec = forageWagon.spec_forageWagon
self.settings = vehicle:getCpSettings()
local additives = self.forageWagonSpec.additives
if additives.available then
self.refillData[self.implement][additives.fillUnitIndex] = -1
end
end

function ForageWagonController:update()
Expand Down Expand Up @@ -39,3 +43,24 @@ function ForageWagonController:getDriveData()
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
54 changes: 54 additions & 0 deletions scripts/ai/controllers/ImplementController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ function ImplementController:init(vehicle, implement)
self.implement = implement
self.settings = vehicle:getCpSettings()
self.disabledStates = {}
self.refillData = {
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {
[self.implement] = {}
}
}
end

--- Get the controlled implement
Expand Down Expand Up @@ -113,4 +120,51 @@ end

function ImplementController:canContinueWork()
return true
end

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

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

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

function ImplementController:onStartRefilling()
if self:isRefillingAllowed() then
if self:needsRefilling() then
self.refillData.timer:set(false, 30 * 1000)
end
self.refillData.hasChanged = false
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels, true)
end
end

function ImplementController:onUpdateRefilling()
if self:isRefillingAllowed() 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
return self.refillData.timer:get(), self.refillData.hasChanged
end
return true, false
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)
end
end
end
36 changes: 4 additions & 32 deletions scripts/ai/controllers/SowingMachineController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ SowingMachineController = CpObject(ImplementController)
function SowingMachineController:init(vehicle, implement)
ImplementController.init(self, vehicle, implement)
self.sowingMachineSpec = self.implement.spec_sowingMachine
self.refillData = {
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {
[self.implement] = {
[self.sowingMachineSpec.fillUnitIndex] = -1
}
}
}
self.refillData.lastFillLevels[self.implement][self.sowingMachineSpec.fillUnitIndex] = -1
end

function SowingMachineController:update()
Expand Down Expand Up @@ -55,37 +47,17 @@ end
function SowingMachineController:needsRefilling()
if not g_currentMission.missionInfo.helperBuySeeds then
if self.implement:getFillUnitFillLevel(self.sowingMachineSpec.fillUnitIndex) <= 0 then
return true
return ImplementController.needsRefilling(self)
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
self.refillData.timer:set(false, 30 * 1000)
end
self.refillData.hasChanged = false
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels, true)
end

function SowingMachineController:onUpdateRefilling()
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
return self.refillData.timer:get(), self.refillData.hasChanged
end

function SowingMachineController:onStopRefilling()
if self.implement.aiFinishLoading ~= nil then
self.implement:aiFinishLoading()
end
local spec = self.implement.spec_fillUnit
if spec.fillTrigger.isFilling then
self.implement:setFillUnitIsFilling(false)
end
ImplementController.onStartRefilling(self)
end
37 changes: 6 additions & 31 deletions scripts/ai/controllers/SprayerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ function SprayerController:init(vehicle, sprayer)
self.sprayer = sprayer
self.sprayerSpec = sprayer.spec_sprayer
ImplementController.init(self, vehicle, self.sprayer)
self.refillData = {
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {
[self.implement] = {
[self.implement:getSprayerFillUnitIndex()] = -1
}
}
}
self.refillData.lastFillLevels[self.implement][self.implement:getSprayerFillUnitIndex()] = -1
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)
Expand Down Expand Up @@ -48,9 +40,10 @@ function SprayerController:needsRefilling()
end
end
end
return false
end

function SprayerController:onStartRefilling(ignore)
function SprayerController:onStartRefilling()
if self:needsRefilling() then
if self.implement.aiPrepareLoading ~= nil then
self.implement:aiPrepareLoading(self.implement:getSprayerFillUnitIndex())
Expand All @@ -62,36 +55,18 @@ function SprayerController:onStartRefilling(ignore)
end
end
end
self.refillData.timer:set(false, 10 * 1000)
end
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels, true)
self.refillData.hasChanged = false
end

function SprayerController:onUpdateRefilling()
if ImplementUtil.tryAndCheckRefillingFillUnits(self.refillData.lastFillLevels) or
ImplementUtil.hasFillLevelChanged(self.refillData.lastFillLevels) then
self:debugSparse("Waiting for refilling to finish ..")
self.refillData.timer:set(false, 10 * 1000)
self.refillData.hasChanged = true
end
return self.refillData.timer:get(), self.refillData.hasChanged
ImplementController.onStartRefilling(self)
end

function SprayerController:onStopRefilling()
if self.implement.aiFinishLoading ~= nil then
self.implement:aiFinishLoading()
end
local spec = self.implement.spec_fillUnit
if spec.fillTrigger.isFilling then
self.implement:setFillUnitIsFilling(false)
end
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
spec = src.vehicle.spec_fillUnit
local spec = src.vehicle.spec_fillUnit
if spec.fillTrigger.isFilling then
src.vehicle:setFillUnitIsFilling(false)
end
Expand Down
8 changes: 4 additions & 4 deletions scripts/ai/controllers/TreePlanterController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ TreePlanterController = CpObject(ImplementController)
function TreePlanterController:init(vehicle, implement)
ImplementController.init(self, vehicle, implement)
self.treePlanterSpec = implement.spec_treePlanter
self.refillData = {
timer = CpTemporaryObject(true),
hasChanged = false
}
end

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

function TreePlanterController:isRefillingAllowed()
return true
end

function TreePlanterController:needsRefilling()
return not g_currentMission.missionInfo.helperBuySeeds
and self.treePlanterSpec.mountedSaplingPallet == nil
Expand Down

0 comments on commit 449b4f1

Please sign in to comment.