This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
/
FieldSupplyAIDriver.lua
197 lines (170 loc) · 6.63 KB
/
FieldSupplyAIDriver.lua
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
--[[
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/>.
]]
--[[
Field Supply AI Driver to let fill tools with digestate or liquid manure on the field egde
Also known as mode 8
]]
--TODO: Have FieldSupplyAIDriver be derived from GrainTransportDriver and not FillableFieldworkAIDriver,
-- as there is no need for FieldworkAIDriver functions
---@class FieldSupplyAIDriver : FillableFieldworkAIDriver
FieldSupplyAIDriver = CpObject(FillableFieldworkAIDriver)
FieldSupplyAIDriver.myStates = {
ON_REFILL_COURSE = {},
WAITING_FOR_GETTING_UNLOADED = {}
}
--- Constructor
function FieldSupplyAIDriver:init(vehicle)
FillableFieldworkAIDriver.init(self, vehicle)
local settings = self.vehicle.cp.settings
self.triggerHandler.driveOnAtFillLevel = settings.driveOnAtFillLevel
self:initStates(FieldSupplyAIDriver.myStates)
self.supplyState = self.states.ON_REFILL_COURSE
self.mode=courseplay.MODE_FIELD_SUPPLY
self.debugChannel = courseplay.DBG_MODE_8
end
function FieldSupplyAIDriver:postInit()
---Refresh the Hud content here,as otherwise the moveable pipe is not
---detected the first time after loading a savegame.
self:setHudContent()
FillableFieldworkAIDriver.postInit(self)
end
function FieldSupplyAIDriver:setHudContent()
self:findPipe()
AIDriver.setHudContent(self)
courseplay.hud:setFieldSupplyAIDriverContent(self.vehicle)
end
--this one is should be better derived!!
function FieldSupplyAIDriver:start(startingPoint)
self.refillState = self.states.REFILL_DONE
self.supplyState = self.states.ON_REFILL_COURSE
AIDriver.start(self,startingPoint)
self.state = self.states.ON_UNLOAD_OR_REFILL_COURSE
end
function FieldSupplyAIDriver:stop(msgReference)
-- TODO: revise why FieldSupplyAIDriver is derived from FieldworkAIDriver, as it has no fieldwork course
-- so this override would not be necessary.
AIDriver.stop(self, msgReference)
end
function FieldSupplyAIDriver:shouldStopAtEndOfCourse()
return false
end
function FieldSupplyAIDriver:onEndCourse()
AIDriver.onEndCourse(self)
end
function FieldSupplyAIDriver:isProximitySwerveEnabled()
return self.state == self.states.ON_UNLOAD_OR_REFILL_COURSE or
self.state == self.states.RETURNING_TO_FIRST_POINT or
self.supplyState == self.states.ON_REFILL_COURSE
end
function FieldSupplyAIDriver:drive(dt)
-- update current waypoint/goal point
if self.supplyState == self.states.ON_REFILL_COURSE then
FillableFieldworkAIDriver.driveUnloadOrRefill(self)
self.unloadingText = nil
elseif self.supplyState == self.states.WAITING_FOR_GETTING_UNLOADED then
self:holdWithFuelSave()
self:updateInfoText()
if self.objectWithPipe then
self.triggerHandler:enableFillTypeUnloadingAugerWagon()
---TODO: Currently it's not possible to open the pipe after the mode 4 driver arrives,
--- as there is not trigger to detect it, while the pipe is closed.
-- if AIDriverUtil.isTrailerUnderPipe(self.pipe,true) then
--- Open the pipe if there is a trailer under the pipe and standing still.
self.objectWithPipe:setPipeState(AIDriverUtil.PIPE_STATE_OPEN)
self:isWorkingToolPositionReached(dt,1)
-- else
-- self.objectWithPipe:setPipeState(AIDriverUtil.PIPE_STATE_CLOSED)
-- end
else
self.triggerHandler:enableFillTypeUnloading()
end
self.triggerHandler:disableFillTypeLoading()
--if i'm empty or fillLevel is below threshold then drive to get new stuff
if self:isFillLevelToContinueReached() then
self:continue()
self.triggerHandler:resetLoadingState()
end
end
AIDriver.drive(self, dt)
end
function FieldSupplyAIDriver:enableFillTypeLoading(isInWaitPointRange)
if not isInWaitPointRange then
FillableFieldworkAIDriver.enableFillTypeLoading(self)
end
end
function FieldSupplyAIDriver:continue()
self:changeSupplyState(self.states.ON_REFILL_COURSE )
AIDriver.continue(self)
end
function FieldSupplyAIDriver:onWaypointPassed(ix)
if self.course:isWaitAt(ix) then
self:changeSupplyState(self.states.WAITING_FOR_GETTING_UNLOADED)
self:setInfoText('REACHED_OVERLOADING_POINT')
self:refreshHUD()
end
AIDriver.onWaypointPassed(self,ix)
end
function FieldSupplyAIDriver:changeSupplyState(newState)
self.supplyState = newState;
end
function FieldSupplyAIDriver:isFillLevelToContinueReached()
local fillTypeData, fillTypeDataSize= self.triggerHandler:getSiloSelectedFillTypeData()
if fillTypeData == nil then
return
end
--pipe still opening wait!
if self.objectWithPipe and not self.objectWithPipe:getIsPipeStateChangeAllowed(AIDriverUtil.PIPE_STATE_CLOSED) then
return
end
local fillLevelInfo = {}
self:getAllFillLevels(self.vehicle, fillLevelInfo)
for fillType, info in pairs(fillLevelInfo) do
for _,data in ipairs(fillTypeData) do
if data.fillType == fillType then
local fillLevelPercentage = info.fillLevel/info.capacity*100
if fillLevelPercentage <= self.vehicle.cp.settings.moveOnAtFillLevel:get() and self:levelDidNotChange(fillLevelPercentage) then
return true
end
end
end
end
end
function FieldSupplyAIDriver:needsFillTypeLoading()
if not self.isInWaitPointRange then
return true
end
end
function FieldSupplyAIDriver:findPipe()
local implementWithPipe = AIDriverUtil.getImplementWithSpecialization(self.vehicle, Pipe) or self.vehicle.spec_pipe and self.vehicle
if implementWithPipe then
self.objectWithPipe = implementWithPipe
self.pipe = implementWithPipe.spec_pipe
end
end
function FieldSupplyAIDriver:closePipeIfNeeded(isInWaitPointRange)
if self.objectWithPipe and not self.isInWaitPointRange then
self.objectWithPipe:setPipeState(AIDriverUtil.PIPE_STATE_CLOSED)
end
end
function FieldSupplyAIDriver:getSiloSelectedFillTypeSetting()
return self.vehicle.cp.settings.siloSelectedFillTypeFieldSupplyDriver
end
function FieldSupplyAIDriver:getCanShowDriveOnButton()
return AIDriver.getCanShowDriveOnButton(self) or self.refillState == self.states.WAITING_FOR_GETTING_UNLOADED
end
function FieldSupplyAIDriver:getWorkingToolPositionsSetting()
local setting = self.settings.pipeToolPositions
return setting:getHasMoveablePipe() and setting:hasValidToolPositions() and setting
end