forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFillableFieldworkAIDriver.lua
More file actions
263 lines (237 loc) · 9.44 KB
/
FillableFieldworkAIDriver.lua
File metadata and controls
263 lines (237 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2018 Peter Vajko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
--[[
Fieldwork AI Driver for seeding, spraying, etc. where the tool needs to be filled
with some material
Also known as mode 4
]]
---@class FillableFieldworkAIDriver : FieldworkAIDriver
FillableFieldworkAIDriver = CpObject(FieldworkAIDriver)
FillableFieldworkAIDriver.myStates = {
TO_BE_REFILLED = {},
REFILL_DONE = {}
}
function FillableFieldworkAIDriver:init(vehicle)
courseplay.debugVehicle(11,vehicle,'FillableFieldworkAIDriver:init()')
FieldworkAIDriver.init(self, vehicle)
self:initStates(FillableFieldworkAIDriver.myStates)
self.mode = courseplay.MODE_SEED_FERTILIZE
self.refillState = self.states.TO_BE_REFILLED
end
function FillableFieldworkAIDriver:start(startingPoint)
self:getSiloSelectedFillTypeSetting():cleanUpOldFillTypes()
FieldworkAIDriver.start(self,startingPoint)
end
function FillableFieldworkAIDriver:setHudContent()
FieldworkAIDriver.setHudContent(self)
courseplay.hud:setFillableFieldworkAIDriverContent(self.vehicle)
end
function FillableFieldworkAIDriver:changeToUnloadOrRefill()
self.refillState = self.states.TO_BE_REFILLED
self:refreshHUD()
FieldworkAIDriver.changeToUnloadOrRefill(self)
end
--- Out of seeds/fertilizer/whatever
function FillableFieldworkAIDriver:changeToFieldworkUnloadOrRefill()
self:debug('change to fieldwork refilling')
self:setInfoText(self:getFillLevelInfoText())
FieldworkAIDriver.changeToFieldworkUnloadOrRefill(self)
end
--- Drive the refill part of the course
function FillableFieldworkAIDriver:driveUnloadOrRefill()
if self:getSiloSelectedFillTypeSetting():isEmpty() then
self:setSpeed(0)
self:setInfoText('NO_SELECTED_FILLTYPE')
return
else
self:clearInfoText('NO_SELECTED_FILLTYPE')
end
local isNearWaitPoint, waitPointIx = self.course:hasWaitPointWithinDistance(self.ppc:getCurrentWaypointIx(), 10)
self.waitPointIx = waitPointIx
--this one is used to disable loading at the unloading stations,
--might be better to disable the triggerID for loading
self.isInWaitPointRange = self.waitPointIx and self.waitPointIx+8 >self.ppc:getCurrentWaypointIx() or isNearWaitPoint
self:enableFillTypeLoading(self.isInWaitPointRange)
if self.course:isTemporary() then
-- use the courseplay speed limit until we get to the actual unload corse fields (on alignment/temporary)
self:setSpeed(self.vehicle.cp.speeds.field)
elseif self.refillState == self.states.TO_BE_REFILLED and isNearWaitPoint then
local distanceToWait = self.course:getDistanceBetweenVehicleAndWaypoint(self.vehicle, waitPointIx)
self:setSpeed(MathUtil.clamp(distanceToWait,self.vehicle.cp.speeds.crawl,self:getRecordedSpeed()))
if distanceToWait < 1 then
self:fillAtWaitPoint()
end
else
if self.triggerHandler:isLoading() then
self:fillAtWaitPoint()
else
self:clearInfoText('REACHED_REFILLING_POINT')
end
-- just drive normally
self:setSpeed(self:getRecordedSpeed())
self:closePipeIfNeeded()
end
end
function FillableFieldworkAIDriver:enableFillTypeLoading(isInWaitPointRange)
self.triggerHandler:enableFillTypeLoading()
self.triggerHandler:disableFillTypeUnloading()
end
function FillableFieldworkAIDriver:needsFillTypeLoading()
if self.state == self.states.ON_UNLOAD_OR_REFILL_COURSE then
return true
end
end
function FillableFieldworkAIDriver:closePipeIfNeeded(isInWaitPointRange)
--override
end
function FillableFieldworkAIDriver:fillAtWaitPoint()
local fillLevelInfo = {}
self:getAllFillLevels(self.vehicle, fillLevelInfo)
local fillTypeData, fillTypeDataSize= self.triggerHandler:getSiloSelectedFillTypeData()
if fillTypeData == nil then
return
end
self:setSpeed(0)
local minFillLevelIsOk = true
local newTotalFillLevel = 0
for _,data in ipairs(fillTypeData) do
for fillType, info in pairs(fillLevelInfo) do
if data.fillType == fillType then
newTotalFillLevel = newTotalFillLevel+info.fillLevel
if info.fillLevel/info.capacity*100 < data.minFillLevel then
minFillLevelIsOk = false
end
end
end
end
if self:levelDidNotChange(newTotalFillLevel) and self:areFillLevelsOk(fillLevelInfo) and minFillLevelIsOk then
self:continue()
end
self:setInfoText('REACHED_REFILLING_POINT')
end
--TODO might change this one
function FillableFieldworkAIDriver:levelDidNotChange(fillLevelPercent)
--fillLevel changed in last loop-> start timer
if self.prevFillLevelPct == nil or self.prevFillLevelPct ~= fillLevelPercent then
self.prevFillLevelPct = fillLevelPercent
courseplay:setCustomTimer(self.vehicle, "fillLevelChange", 3)
end
--if time is up and no fillLevel change happend, return true
if courseplay:timerIsThrough(self.vehicle, "fillLevelChange",false) then
if self.prevFillLevelPct == fillLevelPercent then
return true
end
courseplay:resetCustomTimer(self.vehicle, "fillLevelChange",nil)
end
end
function FillableFieldworkAIDriver:continue()
AIDriver.continue(self)
self.refillState = self.states.REFILL_DONE
self.state = self.states.ON_UNLOAD_OR_REFILL_COURSE
end
-- is the fill level ok to continue? With fillable tools we need to stop working when we are out
-- of material (seed, fertilizer, etc.)
function FillableFieldworkAIDriver:areFillLevelsOk(fillLevelInfo)
local allOk = true
local hasSeeds, hasNoFertilizer = false, false
if self:getSiloSelectedFillTypeSetting():isEmpty() and AIDriverUtil.hasAIImplementWithSpecialization(self.vehicle, Cultivator) then
courseplay:setInfoText(self.vehicle, "skipping loading Seeds/Fertilizer and continue with Cultivator !!!")
return true
end
for fillType, info in pairs(fillLevelInfo) do
if self:isValidFillType(fillType) and info.fillLevel == 0 and info.capacity > 0 and not self:helperBuysThisFillType(fillType) then
allOk = false
if fillType == FillType.FERTILIZER or fillType == FillType.LIQUIDFERTILIZER then hasNoFertilizer = true end
else
if fillType == FillType.SEEDS then hasSeeds = true end
end
end
-- special handling for sowing machines with fertilizer
if not allOk and self.vehicle.cp.settings.sowingMachineFertilizerEnabled:is(false) and hasNoFertilizer and hasSeeds then
self:debugSparse('Has no fertilizer but has seeds so keep working.')
allOk = true
end
return allOk
end
--- Do we need to check this fill unit at all?
--- AIR and DEF are currently don't seem to be used in the game and some mods come with empty tank. Most stock
--- vehicles don't seem to consume any air or adblue.
function FillableFieldworkAIDriver:isValidFillType(fillType)
return fillType ~= FillType.DEF and fillType ~= FillType.AIR
end
--- Does the helper buy this fill unit (according to the game settings)? If yes, we don't have to stop or refill when empty.
function FillableFieldworkAIDriver:helperBuysThisFillType(fillType)
if g_currentMission.missionInfo.helperBuySeeds and fillType == FillType.SEEDS then
return true
end
if g_currentMission.missionInfo.helperBuyFertilizer and
(fillType == FillType.FERTILIZER or fillType == FillType.LIQUIDFERTILIZER) then
return true
end
-- Check for source as in Sprayer:getExternalFill()
-- Source 1 - helper refill off, 2 - helper buys, > 2 - farm sources (manure heap, etc.)
if fillType == FillType.MANURE then
if g_currentMission.missionInfo.helperManureSource == 2 then
-- helper buys
return true
elseif g_currentMission.missionInfo.helperManureSource > 2 then
else
-- maure heaps
local info = g_currentMission.manureHeaps[g_currentMission.missionInfo.helperManureSource - 2]
if info ~= nil then -- Can be nil if pen was removed
if info.manureHeap:getManureLevel() > 0 then
return true
end
end
return false
end
elseif fillType == FillType.LIQUIDMANURE or fillType == FillType.DIGESTATE then
if g_currentMission.missionInfo.helperSlurrySource == 2 then
-- helper buys
return true
elseif g_currentMission.missionInfo.helperSlurrySource > 2 then
--
local info = g_currentMission.liquidManureTriggers[g_currentMission.missionInfo.helperSlurrySource - 2]
if info ~= nil then -- Can be nil if pen was removed
if info.silo:getFillLevel(FillType.LIQUIDMANURE) > 0 then
return true
end
end
return true
end
end
if g_currentMission.missionInfo.helperBuyFuel and
(fillType == FillType.DIESEL or fillType == FillType.FUEL) then
return true
end
return false
end
function FillableFieldworkAIDriver:getFillLevelInfoText()
return 'NEEDS_REFILLING'
end
function FillableFieldworkAIDriver:setLightsMask(vehicle)
local x,y,z = getWorldTranslation(vehicle.rootNode);
if not courseplay:isField(x, z) and self.state == self.states.ON_UNLOAD_OR_REFILL_COURSE then
vehicle:setLightsTypesMask(courseplay.lights.HEADLIGHT_STREET)
else
vehicle:setLightsTypesMask(courseplay.lights.HEADLIGHT_FULL)
end
end
function FillableFieldworkAIDriver:getSiloSelectedFillTypeSetting()
return self.vehicle.cp.settings.siloSelectedFillTypeFillableFieldWorkDriver
end
function FillableFieldworkAIDriver:notAllowedToLoadNextFillType()
return true
end