Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathCompactingAIDriver.lua
More file actions
Latest commit
210 lines (184 loc) · 7.42 KB
/
Copy pathCompactingAIDriver.lua
File metadata and controls
210 lines (184 loc) · 7.42 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
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2021 Courseplay Dev team
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/>.
]]
---@classCompactingAIDriver:BunkerSiloAIDriver
CompactingAIDriver=CpObject(BunkerSiloAIDriver)
CompactingAIDriver.myStates= {
WAITING_FOR_UNLOADERS= {},
NOTHING= {}
}
functionCompactingAIDriver:init(vehicle)
BunkerSiloAIDriver.init(self,vehicle)
self:initStates(CompactingAIDriver.myStates)
self.debugChannel=10
self.compactingState=self.states.NOTHING
self.mode=courseplay.MODE_BUNKERSILO_COMPACTER
end
functionCompactingAIDriver:onDraw()
AIDriver.onDraw(self)
--- TODO: this needs improvements, as currently vehicle.Waypoints is needed.
ifself.vehicle.Waypointsandself.vehicle.cp.canDrivethen
localsearchRadiusSetting=self.settings.levelCompactSearchRadius
searchRadiusSetting:update()
ifsearchRadiusSetting:isShowRadiusActive() then
locallastIx=#self.vehicle.Waypoints
localwps=self.vehicle.Waypoints
localx,y,z=wps[lastIx].cx,wps[lastIx].cy,wps[lastIx].cz
localr=searchRadiusSetting:get()
cpDebug:drawCircle(x, y+3, z,r,math.ceil(r/2), 1, 1, 1)
end
end
end
functionCompactingAIDriver:setHudContent()
BunkerSiloAIDriver.setHudContent(self)
courseplay.hud:setLevelCompactAIDriverContent(self.vehicle)
end
functionCompactingAIDriver:start(startingPoint)
--- Clean up old waypoint node.
self.relevantWaypointNode=nil
BunkerSiloAIDriver.start(self,startingPoint)
end
functionCompactingAIDriver:drive(dt)
--- Gets the search radius.
localnormalRadius=self.settings.levelCompactSearchRadius:get()
--- Enlarge the searchRadius for waiting at waitpoint to avoid traffic problems
localsearchRadius=self:isWaitingForUnloaders() andnormalRadius+10ornormalRadius
--- Searches for unloaders and restart/hold them in place.
ifself:foundUnloaderInRadius(searchRadius,notself:isWaitingForUnloaders()) then
self.hasFoundUnloaders=true
else
self.hasFoundUnloaders=false
end
--- If the wait point is reached, wait until the unloader has finished.
ifself:isWaitingForUnloaders() then
self:hold()
ifnotself.hasFoundUnloadersthen
self.compactingState=self.states.NOTHING
self:clearInfoText('WAITING_FOR_UNLOADERS')
else
self:setInfoText('WAITING_FOR_UNLOADERS')
end
end
BunkerSiloAIDriver.drive(self,dt)
end
---search for unloaders nearby
---@paramrnumber radius to search from the last main course waypoint
---@paramsetWaitingboolean if true then set the cp driver to wait else reset waiting if needed
---@returntrue if valid unloader is nearby
functionCompactingAIDriver:foundUnloaderInRadius(r,setWaiting)
ifnotg_currentMissionthen
return
end
ifself.relevantWaypointNode==nilthen
self.relevantWaypointNode=WaypointNode('relevantWaypointNode')
self.relevantWaypointNode:setToWaypoint(self.course,self.course:getNumberOfWaypoints() , true)
end
ifself:isSiloDebugActive() then
localx,y,z=getTranslation(self.relevantWaypointNode.node)
DebugUtil.drawDebugCircle(x,y+2,z, r, math.ceil(r/2))
end
for_, vehicleinpairs(g_currentMission.vehicles) do
ifvehicle~=self.vehiclethen
locald=calcDistanceFrom(self.relevantWaypointNode.node, vehicle.rootNode)
ifd<rthen
localautodriveSpec=vehicle.spec_autodrive
ifcourseplay:isAIDriverActive(vehicle) andvehicle.cp.driver.triggerHandler:isAllowedToUnloadAtBunkerSilo() then
--CombineUnloadAIDriver,GrainTransportAIDriver,UnloadableFieldworkAIDriver
localisOkayToStop=self:isTheUnloaderAllowedToStop(vehicle)
ifsetWaitingandisOkayToStopthen
vehicle.cp.driver:hold()
vehicle.cp.driver:setInfoText("WAITING_FOR_LEVELCOMPACTAIDRIVER")
vehicle.cp.driver:disableTrafficConflictDetection()
else
vehicle.cp.driver:clearInfoText("WAITING_FOR_LEVELCOMPACTAIDRIVER")
vehicle.cp.driver:enableTrafficConflictDetection()
end
self:debugSparse("found cp driver : %s",nameNum(vehicle))
returnisOkayToStop
elseifautodriveSpecandautodriveSpec.HoldDrivingandvehicle.ad.stateModuleandvehicle.ad.stateModule:isActive() then
--- Autodrive
localisOkayToStop=self:isTheUnloaderAllowedToStop(vehicle)
ifsetWaitingandisOkayToStopthen
autodriveSpec:HoldDriving(vehicle)
end
self:debugSparse("found autodrive driver : %s",nameNum(vehicle))
returnisOkayToStop
elseifvehicle.getIsEnteredand (vehicle:getIsEntered() orvehicle:getIsControlled()) and (AIDriverUtil.hasImplementWithSpecialization(vehicle, Trailer) orvehicle.spec_trailer) then
--Player controlled vehicle
ifself.settings.levelCompactSearchOnlyAutomatedDriver:is(false) then
--Player controlled vehicle is allowed to lookup
self:debugSparse("found player driven vehicle : %s",nameNum(vehicle))
returntrue
end
end
end
end
end
end
functionCompactingAIDriver:isTheUnloaderAllowedToStop(unloader)
localonlyStopFilledDrivers=self.settings.levelCompactSiloTyp:get()
ifonlyStopFilledDriversthen
localtotalFillLevel=AIDriverUtil.getTotalFillLevelAndCapacity(unloader)
iftotalFillLevel<0.02then
returnfalse
end
end
returntrue
end
functionCompactingAIDriver:beforeDriveIntoSilo()
self:lowerImplements()
BunkerSiloAIDriver.beforeDriveIntoSilo(self)
end
functionCompactingAIDriver:beforeDriveOutOfSilo()
self:raiseImplements()
BunkerSiloAIDriver.beforeDriveOutOfSilo(self)
end
--- Has the driver reached the wait point and is waiting for the unloader?
functionCompactingAIDriver:isWaitingForUnloaders()
returnself.compactingState==self.states.WAITING_FOR_UNLOADERS
end
--- If an unloader was found, continue with the main course.
functionCompactingAIDriver:getCanContinueDrivingSiloCourse()
returnnotself.hasFoundUnloaders
end
functionCompactingAIDriver:onWaypointPassed(ix)
ifself.course:isWaitAt(ix) then
self.compactingState=self.states.WAITING_FOR_UNLOADERS
end
BunkerSiloAIDriver.onWaypointPassed(self, ix)
end
--- Override this function, as the stopping at wait point gets handled separately.
functionCompactingAIDriver:isStoppingAtWaitPointAllowed()
returnfalse
end
functionCompactingAIDriver:lowerImplements()
self.vehicle:raiseAIEvent("onAIStart", "onAIImplementStart")
self.vehicle:requestActionEventUpdate()
for_, implementinpairs(self.vehicle:getAttachedImplements()) do
ifimplement.object.aiImplementStartLinethen
implement.object:aiImplementStartLine()
end
end
self.vehicle:raiseStateChange(Vehicle.STATE_CHANGE_AI_START_LINE)
end
functionCompactingAIDriver:raiseImplements()
self.vehicle:raiseAIEvent("onAIEnd", "onAIImplementEnd")
self.vehicle:requestActionEventUpdate()
for_, implementinpairs(self.vehicle:getAttachedImplements()) do
ifimplement.object.aiImplementEndLinethen
implement.object:aiImplementEndLine()
end
end
self.vehicle:raiseStateChange(Vehicle.STATE_CHANGE_AI_END_LINE)
end