Skip to content

Commit

Permalink
Div adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Oct 14, 2024
1 parent 53d4109 commit 91f76e6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 94 deletions.
4 changes: 2 additions & 2 deletions config/GlobalSettingsSetup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
<Text>always</Text>
</Texts>
</Setting>
<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="fuelThreshold" min="0" max="100" incremental="5" default="5" unit="4"/>
<Setting classType="AIParameterBooleanSetting" name="waitForRefueling" defaultBool="false" isExpertModeOnly="true"/>
<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
5 changes: 0 additions & 5 deletions scripts/CpGlobalSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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 @@ -102,10 +101,6 @@ 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
3 changes: 0 additions & 3 deletions scripts/ai/AIDriveStrategyCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ 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
62 changes: 32 additions & 30 deletions scripts/ai/controllers/MotorController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ function MotorController:init(vehicle, implement)
self.isValid = true
self.fuelThresholdSetting = g_Courseplay.globalSettings.fuelThreshold
self.refuelData = {
active = false,
timer = CpTemporaryObject(true),
hasChanged = false,
lastFillLevels = {
[self.implement] = {}
}
Expand Down Expand Up @@ -56,14 +54,21 @@ function MotorController:update()
end
end
end
local needsFuelLowInfo = false
if not self.refuelData.active then
--- Only apply this if no refueling is active.
if self:isFuelLow(self.fuelThresholdSetting:getValue()) then
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())
elseif self:isFuelLow(self.fuelThresholdSetting:getValue() + 5) then
needsFuelLowInfo = true
else
needsFuelEmptyInfo = true
end
elseif self:isFuelLow(self.fuelThresholdSetting:getValue() + 5) then
needsFuelLowInfo = true
end
if needsFuelEmptyInfo then
self:setInfoText(InfoTextManager.FUEL_IS_EMPTY)
else
self:clearInfoText(InfoTextManager.FUEL_IS_EMPTY)
end
if needsFuelLowInfo then
self:setInfoText(InfoTextManager.FUEL_IS_LOW)
Expand All @@ -72,6 +77,24 @@ function MotorController:update()
end
end

function MotorController:getDriveData()
local maxSpeed
if ImplementUtil.tryAndCheckRefillingFillUnits(self.refuelData.lastFillLevels) or
ImplementUtil.hasFillLevelChanged(self.refuelData.lastFillLevels) then
self.refuelData.timer:set(false, 10 * 1000)
end
if not self.refuelData.timer:get() then
maxSpeed = 0
end
if g_Courseplay.globalSettings.waitForRefueling:getValue() and
self:isFuelLow(self.fuelThresholdSetting:getValue()) then

maxSpeed = 0
end

return nil, nil, nil, maxSpeed
end

--- There is a time problem with the release of the driver, when no player is entered,
--- so we use this flag to make sure the :update() isn't used after :delete() was called.
function MotorController:delete()
Expand Down Expand Up @@ -103,30 +126,9 @@ function MotorController:stopMotor()
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)
self.refuelData.active = true
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()
function MotorController:onFinished()
local spec = self.implement.spec_fillUnit
if spec.fillTrigger.isFilling then
self.implement:setFillUnitIsFilling(false)
end
self.refuelData.active = false
end

function MotorController:onFinished()
self:onStopRefuelling()
end
53 changes: 0 additions & 53 deletions scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ 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 @@ -80,58 +76,9 @@ 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
3 changes: 2 additions & 1 deletion scripts/specializations/CpVehicleSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ end
function CpVehicleSettings:isRefillOnTheFieldSettingVisible()
return AIUtil.hasChildVehicleWithSpecialization(self, Sprayer) or
AIUtil.hasChildVehicleWithSpecialization(self, SowingMachine) or
AIUtil.hasChildVehicleWithSpecialization(self, TreePlanter)
AIUtil.hasChildVehicleWithSpecialization(self, TreePlanter) or
CpVehicleSettings.isAdditiveFillUnitSettingVisible(self)
end

---------------------------------------------
Expand Down

0 comments on commit 91f76e6

Please sign in to comment.