forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_stop.lua
346 lines (288 loc) · 11.7 KB
/
start_stop.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
local curFile = 'start_stop.lua';
-- starts driving the course
function courseplay:start(self)
if g_server == nil then
return
end
if not CpManager.trafficCollisionIgnoreList[g_currentMission.terrainRootNode] then -- ???
CpManager.trafficCollisionIgnoreList[g_currentMission.terrainRootNode] = true;
end;
-- TODO: move this to TrafficCollision.lua
if self:getAINeedsTrafficCollisionBox() then
local collisionRoot = g_i3DManager:loadSharedI3DFile(AIVehicle.TRAFFIC_COLLISION_BOX_FILENAME, self.baseDirectory, false, true, false)
if collisionRoot ~= nil and collisionRoot ~= 0 then
local collision = getChildAt(collisionRoot, 0)
link(getRootNode(), collision)
self.spec_aiVehicle.aiTrafficCollision = collision
delete(collisionRoot)
end
end
self.cp.numWayPoints = #self.Waypoints;
--self:setCpVar('numWaypoints', #self.Waypoints,courseplay.isClient);
if self.cp.numWaypoints < 1 then
return
end
--setEngineState needed or AIDriver handling this ??
courseplay:setEngineState(self, true);
self.cp.saveFuel = false
courseplay.alreadyPrinted = {}
self.cpTrafficCollisionIgnoreList = {}
self:setIsCourseplayDriving(false);
courseplay:setIsRecording(self, false);
courseplay:setRecordingIsPaused(self, false);
courseplay:resetTools(self)
if self.cp.waypointIndex < 1 then
courseplay:setWaypointIndex(self, 1);
end
-- show arrow
self:setCpVar('distanceCheck',true,courseplay.isClient);
-- current position
local ctx, cty, ctz = getWorldTranslation(self.cp.directionNode);
-- TODO: temporary bandaid here for the case when the legacy waypointIndex isn't set correctly
if self.cp.waypointIndex > #self.Waypoints then
courseplay.infoVehicle(self, 'Waypoint index %d reset to %d', self.cp.waypointIndex, #self.Waypoints)
self.cp.waypointIndex = #self.Waypoints
end
-- position of next waypoint
local cx, cz = self.Waypoints[self.cp.waypointIndex].cx, self.Waypoints[self.cp.waypointIndex].cz
-- distance (in any direction)
local dist = courseplay:distance(ctx, ctz, cx, cz)
local setLaneNumber = false;
local isFrontAttached = false;
local isReversePossible = true;
local tailerCount = 0;
for k,workTool in pairs(self.cp.workTools) do --TODO temporary solution (better would be Tool:getIsAnimationPlaying(animationName))
--DrivingLine spec: set lane numbers
if self.cp.mode == 4 and not setLaneNumber and workTool.cp.hasSpecializationDrivingLine and not workTool.manualDrivingLine then
setLaneNumber = true;
end;
if self.cp.mode == 10 then
local x,y,z = getWorldTranslation(workTool.rootNode)
local _,_,tz = worldToLocal(self.cp.directionNode,x,y,z)
if tz > 0 then
isFrontAttached = true
end
end
if workTool.cp.hasSpecializationTrailer then
tailerCount = tailerCount + 1
if tailerCount > 1 then
isReversePossible = false
end
end
if workTool.cp.isSugarCaneAugerWagon then
isReversePossible = false
end
end;
self.cp.isReversePossible = isReversePossible
local numWaitPoints = 0
local numUnloadPoints = 0
local numCrossingPoints = 0
self.cp.waitPoints = {};
self.cp.unloadPoints = {};
-- modes 4/6 without start and stop point, set them at start and end, for only-on-field-courses
if (self.cp.mode == 4 or self.cp.mode == 6) then
if numWaitPoints == 0 or self.cp.startWork == nil then
self.cp.startWork = 1;
end;
if numWaitPoints == 0 or self.cp.stopWork == nil then
self.cp.stopWork = self.cp.numWaypoints;
end;
end;
self.cp.numWaitPoints = numWaitPoints;
self.cp.numUnloadPoints = numUnloadPoints;
self.cp.numCrossingPoints = numCrossingPoints;
courseplay:debug(string.format("%s: numWaitPoints=%d, waitPoints[1]=%s, numCrossingPoints=%d", nameNum(self), self.cp.numWaitPoints, tostring(self.cp.waitPoints[1]), numCrossingPoints), 12);
-- set waitTime to 0 if necessary
if not courseplay:getCanHaveWaitTime(self) and self.cp.waitTime > 0 then
courseplay:changeWaitTime(self, -self.cp.waitTime);
end;
if self.cp.waypointIndex > 2 and self.cp.mode ~= 4 and self.cp.mode ~= 6 and self.cp.mode ~= 8 then
courseplay:setDriveUnloadNow(self, true);
elseif self.cp.mode == 4 or self.cp.mode == 6 then
courseplay:setDriveUnloadNow(self, false);
self.cp.hasUnloadingRefillingCourse = self.cp.numWaypoints > self.cp.stopWork + 7;
if self.Waypoints[self.cp.stopWork].cx == self.Waypoints[self.cp.startWork].cx
and self.Waypoints[self.cp.stopWork].cz == self.Waypoints[self.cp.startWork].cz then -- TODO: VERY unsafe, there could be LUA float problems (e.g. 7 + 8 = 15.000000001)
self.cp.finishWork = self.cp.stopWork-5
else
self.cp.finishWork = self.cp.stopWork
end
-- NOTE: if we want to start the course but catch one of the last 5 points ("returnToStartPoint"), make sure we get wp 2
if self.cp.settings.startingPoint:is(StartingPointSetting.START_AT_NEAREST_POINT) and self.cp.finishWork ~= self.cp.stopWork and self.cp.waypointIndex > self.cp.finishWork and self.cp.waypointIndex <= self.cp.stopWork then
courseplay:setWaypointIndex(self, 2);
end
elseif self.cp.mode == 8 then
courseplay:setDriveUnloadNow(self, false);
end
if self.cp.settings.startingPoint:is(StartingPointSetting.START_AT_FIRST_POINT) then
if self.cp.mode == 2 or self.cp.mode == 3 then
-- TODO: really? 3?
courseplay:setWaypointIndex(self, 3);
courseplay:setDriveUnloadNow(self, true);
else
courseplay:setWaypointIndex(self, 1);
end
end;
courseplay:updateAllTriggers();
self.cp.aiLightsTypesMaskBackup = self.spec_lights.aiLightsTypesMask
self.cp.cruiseControlSpeedBackup = self:getCruiseControlSpeed();
-- ok i am near the waypoint, let's go
self.cp.savedCheckSpeedLimit = self.checkSpeedLimit;
self.checkSpeedLimit = false
self:setIsCourseplayDriving(true);
courseplay:setIsRecording(self, false);
self:setCpVar('distanceCheck',false,courseplay.isClient);
self.cp.totalLength, self.cp.totalLengthOffset = courseplay:getTotalLengthOnWheels(self);
courseplay:validateCanSwitchMode(self);
-- and another ugly hack here as when settings.lua setAIDriver() is called the bale loader does not seem to be
-- attached and I don't have the motivation do dig through the legacy code to find out why
if self.cp.mode == courseplay.MODE_FIELDWORK then
self.cp.driver:delete()
self.cp.driver = UnloadableFieldworkAIDriver.create(self)
end
StartStopEvent:sendStartEvent(self)
self.cp.driver:start(self.cp.settings.startingPoint)
end;
-- stops driving the course
function courseplay:stop(self)
if g_server == nil then
return
end
-- Stop AI Driver
if self.cp.driver then
self.cp.driver:dismiss()
end
-- TODO: move this to TrafficCollision.lua
if self:getAINeedsTrafficCollisionBox() then
setTranslation(self.spec_aiVehicle.aiTrafficCollision, 0, -1000, 0)
self.spec_aiVehicle.aiTrafficCollisionRemoveDelay = 200
end
--is this one still used ??
if g_currentMission.missionInfo.automaticMotorStartEnabled and self.cp.saveFuel and not self.spec_motorized.isMotorStarted then
courseplay:setEngineState(self, true);
self.cp.saveFuel = false;
end
if courseplay:getCustomTimerExists(self,'fuelSaveTimer') then
--print("reset existing timer")
courseplay:resetCustomTimer(self,'fuelSaveTimer',true)
end
--stop special tools
for _, tool in pairs (self.cp.workTools) do
-- vehicle, workTool, unfold, lower, turnOn, allowedToDrive, cover, unload, ridgeMarker,forceSpeedLimit)
if tool.cp.originalCapacities then
for index,fillUnit in pairs(tool:getFillUnits()) do
fillUnit.capacity = tool.cp.originalCapacities[index]
end
tool.cp.originalCapacities = nil
end
end
if self.cp.directionNodeToTurnNodeLength ~= nil then
self.cp.directionNodeToTurnNodeLength = nil
end
self.cp.lastInfoText = nil
if self.cp.cruiseControlSpeedBackup then
self.spec_drivable.cruiseControl.speed = self.cp.cruiseControlSpeedBackup; -- NOTE JT: no need to use setter or event function - Drivable's update() checks for changes in the var and calls the event itself
self.cp.cruiseControlSpeedBackup = nil;
end;
self.spec_lights.aiLightsTypesMask = self.cp.aiLightsTypesMaskBackup
self:setCruiseControlState(Drivable.CRUISECONTROL_STATE_OFF)
self.spec_drivable.cruiseControl.minSpeed = 1
self.cp.settings.forcedToStop:set(false)
self.cp.waitingForTrailerToUnload = false
courseplay:setIsRecording(self, false);
courseplay:setRecordingIsPaused(self, false);
self.cp.isTurning = nil;
courseplay:clearTurnTargets(self);
self.cp.fillTrigger = nil;
self.cp.hasMachineToFill = false;
-- deactivate beacon and hazard lights
if self.beaconLightsActive then
self:setBeaconLightsVisibility(false);
end;
if self.spec_lights.turnLightState and self.spec_lights.turnLightState ~= Lights.TURNLIGHT_OFF then
self:setTurnLightState(Lights.TURNLIGHT_OFF);
end;
-- resetting variables
self.checkSpeedLimit = self.cp.savedCheckSpeedLimit;
courseplay:resetTipTrigger(self);
self:setIsCourseplayDriving(false);
self:setCpVar('canDrive',true,courseplay.isClient)
self:setCpVar('distanceCheck',false,courseplay.isClient);
if self.cp.checkReverseValdityPrinted then
self.cp.checkReverseValdityPrinted = false
end
self.cp.curSpeed = 0;
self.spec_motorized.motor.maxRpmOverride = nil;
self.cp.startWork = nil
self.cp.stopWork = nil
self.cp.hasFinishedWork = nil
self.cp.turnTimeRecorded = nil;
self.cp.hasUnloadingRefillingCourse = false;
self.cp.prevFillLevelPct = nil;
courseplay:setSlippingStage(self, 0);
courseplay:resetCustomTimer(self, 'slippingStage1');
courseplay:resetCustomTimer(self, 'slippingStage2');
courseplay:resetCustomTimer(self, 'foldBaleLoader', true);
self.cp.hasBaleLoader = false;
if self.cp.manualWorkWidth ~= nil then
courseplay:changeWorkWidth(self, nil, self.cp.manualWorkWidth, true)
if self.cp.hud.currentPage == courseplay.hud.PAGE_COURSE_GENERATION then
courseplay.hud:setReloadPageOrder(self, self.cp.hud.currentPage, true);
end
end
self.cp.totalLength, self.cp.totalLengthOffset = 0, 0;
self.cp.numWorkTools = 0;
--remove any local and global info texts
if g_server ~= nil then
courseplay:setInfoText(self, nil);
for refIdx,_ in pairs(CpManager.globalInfoText.msgReference) do
if self.cp.activeGlobalInfoTexts[refIdx] ~= nil then
CpManager:setGlobalInfoText(self, refIdx, true);
end;
end;
end
--validation: can switch mode?
courseplay:validateCanSwitchMode(self);
StartStopEvent:sendStopEvent(self)
-- reactivate load/add/delete course buttons
--courseplay.buttons:setActiveEnabled(self, 'page2');
end
function courseplay:findVehicleHeights(transformId, x, y, z, distance)
local startHeight = math.max(self.sizeLength,5)
local height = startHeight - distance
local vehicle = false
--print(string.format("found %s (%s)",tostring(getName(transformId)),tostring(transformId)))
-- if self.cp.aiTrafficCollisionTrigger == transformId then
if self.aiTrafficCollisionTrigger == transformId then
if self.cp.HeightsFoundColli < height then
self.cp.HeightsFoundColli = height
end
elseif transformId == self.rootNode then
vehicle = true
elseif getParent(transformId) == self.rootNode and self.aiTrafficCollisionTrigger ~= transformId then
vehicle = true
elseif self.cpTrafficCollisionIgnoreList[transformId] or self.cpTrafficCollisionIgnoreList[getParent(transformId)] then
vehicle = true
end
if vehicle and self.cp.HeightsFound < height then
self.cp.HeightsFound = height
end
return true
end
function courseplay:safeSetWaypointIndex( vehicle, newIx )
for i = newIx, newIx do
-- don't set it too close to a turn start,
if vehicle.Waypoints[ i ] ~= nil and vehicle.Waypoints[ i ].turnStart then
-- set it to after the turn
newIx = i + 2
break
end
end
if vehicle.cp.waypointIndex > vehicle.cp.numWaypoints then
courseplay:setWaypointIndex(vehicle, 1);
else
courseplay:setWaypointIndex( vehicle, newIx );
end
end
-- do not remove this comment
-- vim: set noexpandtab: