Skip to content

Commit

Permalink
Added waiting for refueling on the field
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Oct 11, 2024
1 parent 92fbb8e commit 7ccc935
Show file tree
Hide file tree
Showing 16 changed files with 120 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Courseplay.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

--- Global class
---@class Courseplay
---@operator call:Courseplay
Courseplay = CpObject()
Courseplay.MOD_NAME = g_currentModName
Courseplay.BASE_DIRECTORY = g_currentModDirectory
Expand Down
3 changes: 2 additions & 1 deletion config/GlobalSettingsSetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<Text>always</Text>
</Texts>
</Setting>
<Setting classType="AIParameterSettingList" name="fuelThreshold" min="0" max="100" incremental="5" default="5" unit="4"/>
<Setting classType="AIParameterSettingList" name="fuelThreshold" min="0" max="100" incremental="5" default="5" unit="4" onChangeCallback="onFuelSettingChanged"/>
<Setting classType="AIParameterBooleanSetting" name="waitForRefueling" defaultBool="false" isExpertModeOnly="true" onChangeCallback="onFuelSettingChanged"/>
<Setting classType="AIParameterSettingList" name="brokenThreshold" min="0" max="100" incremental="5" default="100" unit="4"/>
<Setting classType="AIParameterBooleanSetting" name="stopThreshingDuringRain" defaultBool="true"/>
<Setting classType="AIParameterSettingList" name="fruitDestruction">
Expand Down
8 changes: 8 additions & 0 deletions config/MasterTranslations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,14 @@
<Text language="de"><![CDATA[Fahrer wird entlassen, wenn Kraftstoff unter diesem Wert liegt.]]></Text>
<Text language="en"><![CDATA[Driver will be released, if the fuel percentage is less.]]></Text>
</Translation>
<Translation name="CP_global_setting_waitForRefueling_title">
<Text language="de"><![CDATA[Wartet auf auftanken]]></Text>
<Text language="en"><![CDATA[Waits for refueling]]></Text>
</Translation>
<Translation name="CP_global_setting_waitForRefueling_tooltip">
<Text language="de"><![CDATA[Fahrzeug wartet vor Ord, um aufgefüllt zu werden.]]></Text>
<Text language="en"><![CDATA[Vehicle waits until it is refueled.]]></Text>
</Translation>
<Translation name="CP_global_setting_brokenThreshold_title">
<Text language="de"><![CDATA[Minimum Zustand]]></Text>
<Text language="en"><![CDATA[Broken threshold]]></Text>
Expand Down
5 changes: 5 additions & 0 deletions scripts/CpGlobalSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ end
--- Loads settings setup form an xmlFile.
function CpGlobalSettings:loadSettingsSetup()
MessageType.CP_DISTANCE_UNIT_CHANGED = nextMessageTypeId()
MessageType.CP_FUEL_SETTING_CHANGED = nextMessageTypeId()

local filePath = Utils.getFilename("config/GlobalSettingsSetup.xml", g_Courseplay.BASE_DIRECTORY)
CpSettingsUtil.loadSettingsFromSetup(self,filePath)
Expand Down Expand Up @@ -101,6 +102,10 @@ function CpGlobalSettings:onUnitChanged()
end
end

function CpGlobalSettings:onFuelSettingChanged()
g_messageCenter:publish(MessageType.CP_FUEL_SETTING_CHANGED)
end

function CpGlobalSettings:debug(str,...)
CpUtil.debugFormat(CpDebug.DBG_HUD,"Global settings: "..str,...)
end
Expand Down
2 changes: 1 addition & 1 deletion scripts/ai/AIDriveStrategyBunkerSilo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function AIDriveStrategyBunkerSilo:initializeImplementControllers(vehicle)
end

function AIDriveStrategyBunkerSilo:isFuelSaveAllowed()
return self.state.properties.fuelSaveAllowed
return self.state.properties.fuelSaveAllowed or AIDriveStrategyCourse.isFuelSaveAllowed(self)
end

-----------------------------------------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion scripts/ai/AIDriveStrategyCombineCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,8 @@ function AIDriveStrategyCombineCourse:isFuelSaveAllowed()
if self.combine:getIsThreshingDuringRain() then
return true
end
return self:isWaitingForUnload() or self:isChopperWaitingForUnloader()
return self:isWaitingForUnload() or self:isChopperWaitingForUnloader()
or AIDriveStrategyCourse.isFuelSaveAllowed(self)
end

--- Check if the vehicle should stop during a turn for example while it
Expand Down
4 changes: 4 additions & 0 deletions scripts/ai/AIDriveStrategyCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ AIDriveStrategyCourse.myStates = {
}

--- Implement controller events.
--- TODO_25 a more generic implementation
AIDriveStrategyCourse.onRaisingEvent = "onRaising"
AIDriveStrategyCourse.onLoweringEvent = "onLowering"
AIDriveStrategyCourse.onFinishedEvent = "onFinished"
AIDriveStrategyCourse.onStartEvent = "onStart"
AIDriveStrategyCourse.onStartRefillingEvent = "onStartRefilling"
AIDriveStrategyCourse.onStopRefillingEvent = "onStopRefilling"
AIDriveStrategyCourse.onUpdateRefillingEvent = "onUpdateRefilling"
AIDriveStrategyCourse.onStartRefuellingEvent = "onStartRefuelling"
AIDriveStrategyCourse.onStopRefuellingEvent = "onStopRefuelling"
AIDriveStrategyCourse.onUpdateRefuellingEvent = "onUpdateRefuelling"
AIDriveStrategyCourse.updateEvent = "update"
AIDriveStrategyCourse.deleteEvent = "delete"
--- A row has just been finished, implements are being raised and about to start the actual turn
Expand Down
2 changes: 1 addition & 1 deletion scripts/ai/AIDriveStrategyShovelSiloLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ end

--- Fuel save only allowed when no trailer is there to unload into.
function AIDriveStrategyShovelSiloLoader:isFuelSaveAllowed()
return self.state == self.states.WAITING_FOR_TRAILER
return self.state == self.states.WAITING_FOR_TRAILER or AIDriveStrategyCourse.isFuelSaveAllowed(self)
end

-----------------------------------------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions scripts/ai/AIDriveStrategySiloLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ end
--- Fuel save only allowed when no trailer is there to unload into.
function AIDriveStrategySiloLoader:isFuelSaveAllowed()
return self.state == self.states.WORKING and not self.conveyorController:canDischargeToObject()
or AIDriveStrategyCourse.isFuelSaveAllowed(self)
end

-----------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion scripts/ai/AIDriveStrategyUnloadCombine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ end
------------------------------------------------------------------------------------------------------------------------

function AIDriveStrategyUnloadCombine:isFuelSaveAllowed()
return self.state.properties.fuelSaveAllowed
return self.state.properties.fuelSaveAllowed or AIDriveStrategyCourse.isFuelSaveAllowed(self)
end

function AIDriveStrategyUnloadCombine:isCoverOpeningAllowed()
Expand Down
36 changes: 36 additions & 0 deletions scripts/ai/controllers/MotorController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ function MotorController:init(vehicle, implement)
self.vehicle.spec_cpAIWorker.motorDisabled = false
self.isValid = true
self.fuelThresholdSetting = g_Courseplay.globalSettings.fuelThreshold
self.refuelData = {
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {
[self.implement] = {}
}
}
for _, fillUnitIndex in ipairs(self.motorSpec.propellantFillUnitIndices) do
self.refuelData.lastFillLevels[self.implement][fillUnitIndex] = -1
end
end

function MotorController:update()
Expand Down Expand Up @@ -82,4 +92,30 @@ function MotorController:stopMotor()
self.implement:stopMotor()
self.vehicle.spec_cpAIWorker.motorDisabled = true
self:debug("Stopped motor for fuel save.")
end

function MotorController:onStartRefuelling()
ImplementUtil.hasFillLevelChanged(self.refuelData.lastFillLevels, true)
self.refuelData.hasChanged = false
self.refuelData.timer:set(false, 10 * 1000)
end

function MotorController:onUpdateRefuelling()
if ImplementUtil.tryAndCheckRefillingFillUnits(self.refuelData.lastFillLevels) or
ImplementUtil.hasFillLevelChanged(self.refuelData.lastFillLevels) then
self.refuelData.timer:set(false, 10 * 1000)
self.refuelData.hasChanged = true
end
return self.refuelData.timer:get(), self.refuelData.hasChanged
end

function MotorController:onStopRefuelling()
local spec = self.implement.spec_fillUnit
if spec.fillTrigger.isFilling then
self.implement:setFillUnitIsFilling(false)
end
end

function MotorController:onFinished()
self:onStopRefuelling()
end
1 change: 1 addition & 0 deletions scripts/ai/controllers/SprayerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ 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
Expand Down
55 changes: 55 additions & 0 deletions scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ function CpAIJob.new(isServer, customMt)

self:setupJobParameters()
self:setupTasks(isServer)

self.isRefuelingActive = false

g_messageCenter:subscribe(MessageType.CP_FUEL_SETTING_CHANGED, self.stopRefuelling, self)

return self
end

Expand Down Expand Up @@ -75,9 +80,58 @@ end
---@param message table Stop reason can be used to reverse engineer the cause.
---@return boolean
function CpAIJob:isFinishingAllowed(message)
--- TODO_25 Move the refuel logic into a task ...
--- For this we need to seperate strategy,task logic first ..
if message:isa(AIMessageErrorOutOfFuel) then
if g_Courseplay.globalSettings.waitForRefueling:getValue() then
local strategy = self.vehicle:getCpDriveStrategy()
if strategy then
if not self.isRefuelingActive then
self.isRefuelingActive = true
strategy:raiseControllerEvent(
AIDriveStrategyCourse.onStartRefuellingEvent)
self:debug("Starts to wait for refueling")
end
return false
end
end
end
return true
end

function CpAIJob:update(dt)
CpAIJob:superClass().update(self, dt)
if self.isRefuelingActive then
local vehicle = self:getVehicle()
local strategy = vehicle:getCpDriveStrategy()
vehicle:cpHold(1500, true)
vehicle:setCpInfoTextActive(InfoTextManager.FUEL_IS_EMPTY)

local readyToContinue, fillLevelHasChanged = true, false
strategy:raiseControllerEventWithLambda(
AIDriveStrategyCourse.onUpdateRefuellingEvent,
function(timerHasFinished, hasChanged)
readyToContinue = readyToContinue and timerHasFinished
fillLevelHasChanged = fillLevelHasChanged or hasChanged
end)
if readyToContinue and fillLevelHasChanged then
self:stopRefuelling()
end
end
end

function CpAIJob:stopRefuelling()
if self.isRefuelingActive then
local vehicle = self:getVehicle()
local strategy = vehicle:getCpDriveStrategy()
self:debug("Finished the refueling")
strategy:raiseControllerEvent(
AIDriveStrategyCourse.onStopRefuellingEvent)
self.isRefuelingActive = false
vehicle:resetCpActiveInfoText(InfoTextManager.FUEL_IS_EMPTY)
end
end

--- Gets the first task to start with.
function CpAIJob:getStartTaskIndex()
if self.currentTaskIndex ~= 0 or self.isDirectStart or self:isTargetReached() then
Expand Down Expand Up @@ -167,6 +221,7 @@ function CpAIJob:stop(aiMessage)
if driveStrategy then
driveStrategy:onFinished(hasFinished)
end
g_messageCenter:unsubscribeAll(self)
end

--- Updates the parameter values.
Expand Down
2 changes: 1 addition & 1 deletion scripts/ai/jobs/CpAIJobFieldWork.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function CpAIJobFieldWork:isFinishingAllowed(message)
end
return false
end
return true
return CpAIJob.isFinishingAllowed(self, message)
end

---@param vehicle table
Expand Down
4 changes: 0 additions & 4 deletions scripts/specializations/CpAIBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ function CpAIBaleFinder:applyCpBaleFinderJobParameters(job)
spec.cpJob:copyFrom(job)
end

function CpAIBaleFinder:getCpDriveStrategy(superFunc)
return superFunc(self) or self.spec_cpAIBaleFinder.driveStrategy
end

--- Is the bale finder allowed?
function CpAIBaleFinder:getCanStartCpBaleFinder()
return (AIUtil.hasImplementWithSpecialization(self, BaleWrapper) and not AIUtil.hasImplementWithSpecialization(self, Baler)) or
Expand Down
4 changes: 0 additions & 4 deletions scripts/specializations/CpAICombineUnloader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ function CpAICombineUnloader:getCpCombineUnloaderJobParameters()
return spec.cpJob:getCpJobParameters()
end

function CpAICombineUnloader:getCpDriveStrategy(superFunc)
return superFunc(self) or self.spec_cpAICombineUnloader.driveStrategy
end

--- Makes sure only trailers with discharge nodes are used.
function CpAICombineUnloader:isValidTrailer(trailer)
local spec = trailer.spec_dischargeable
Expand Down

0 comments on commit 7ccc935

Please sign in to comment.