forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverloaderAIDriver.lua
More file actions
117 lines (105 loc) · 4.41 KB
/
OverloaderAIDriver.lua
File metadata and controls
117 lines (105 loc) · 4.41 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
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2020 Peter Vaiko
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/>.
]]--
---@class OverloaderAIDriver : CombineUnloadAIDriver
OverloaderAIDriver = CpObject(CombineUnloadAIDriver)
OverloaderAIDriver.myStates = {
ENROUTE = {},
WAITING_FOR_TRAILER = {},
WAITING_FOR_OVERLOAD_TO_START = {},
OVERLOADING = {},
}
function OverloaderAIDriver:init(vehicle)
CombineUnloadAIDriver.init(self, vehicle)
self:initStates(OverloaderAIDriver.myStates)
self:debug('OverloaderAIDriver:init()')
self.mode = courseplay.MODE_OVERLOADER
self.unloadCourseState = self.states.ENROUTE
self:findPipeAndTrailer()
end
function OverloaderAIDriver:findPipeAndTrailer()
local implementWithPipe = AIDriverUtil.getImplementWithSpecialization(self.vehicle, Pipe)
if implementWithPipe then
self.pipe = implementWithPipe.spec_pipe
self.objectWithPipe = implementWithPipe
self:debug('Overloader found its pipe')
else
self:debug('Overloader has no implement with pipe')
end
self.trailer = AIDriverUtil.getImplementWithSpecialization(self.vehicle, Trailer)
end
function OverloaderAIDriver:start(startingPoint)
--- Looks like the implements are not attached at onLoad() when the game loads so we end up
--- with no pipe after game start. So make another attempt to find it when missing
--- TODO: this should be fixed properly, just like the other hack in start_stop.lua for the bale loader
if not self.pipe or not self.trailer then self:findPipeAndTrailer() end
self.unloadCourseState = self.states.ENROUTE
CombineUnloadAIDriver.start(self, startingPoint)
end
function OverloaderAIDriver:isTrailerUnderPipe()
if not self.pipe then return end
for trailer, value in pairs(self.pipe.objectsInTriggers) do
if value > 0 then
return true
end
end
return false
end
function OverloaderAIDriver:driveUnloadCourse(dt)
if self.unloadCourseState == self.states.ENROUTE then
elseif self.unloadCourseState == self.states.WAITING_FOR_TRAILER then
self:hold()
if self:isTrailerUnderPipe() then
self:debug('Trailer is here, opening pipe')
if self.pipe then self.objectWithPipe:setPipeState(AIDriverUtil.PIPE_STATE_OPEN) end
self.unloadCourseState = self.states.WAITING_FOR_OVERLOAD_TO_START
end
elseif self.unloadCourseState == self.states.WAITING_FOR_OVERLOAD_TO_START then
self:setSpeed(0)
if self.pipe:getDischargeState() == Dischargeable.DISCHARGE_STATE_OBJECT then
self:debug('Overloading started')
self.unloadCourseState = self.states.OVERLOADING
end
elseif self.unloadCourseState == self.states.OVERLOADING then
self:setSpeed(0)
if self.pipe:getDischargeState() == Dischargeable.DISCHARGE_STATE_OFF then
self:debug('Overloading finished, closing pipe')
if self.pipe then self.objectWithPipe:setPipeState(AIDriverUtil.PIPE_STATE_CLOSED) end
self.unloadCourseState = self.states.ENROUTE
end
end
AIDriver.drive(self, dt)
end
function OverloaderAIDriver:onWaypointPassed(ix)
if self.course:isWaitAt(ix) then
if self:isTrailerEmpty() then
self:debug('Wait point reached but my trailer is empty, continuing')
else
self:debug('Wait point reached, wait for trailer.')
self.unloadCourseState = self.states.WAITING_FOR_TRAILER
end
else
CombineUnloadAIDriver.onWaypointPassed(self, ix)
end
end
function OverloaderAIDriver:isTrailerEmpty()
if self.trailer and self.trailer.getFillUnits then
for _, fillUnit in pairs(self.trailer:getFillUnits()) do
if fillUnit.fillLevel > 0 then
return false
end
end
end
return true
end