forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_stop.lua
More file actions
434 lines (382 loc) · 14.6 KB
/
start_stop.lua
File metadata and controls
434 lines (382 loc) · 14.6 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
local curFile = 'start_stop.lua';
-- starts driving the course
function courseplay:start(self)
self.maxnumber = #(self.Waypoints)
if self.maxnumber < 1 then
return
end
courseplay:setEngineState(self, true);
if self.cp.orgRpm == nil then
self.cp.orgRpm = {}
self.cp.orgRpm[1] = self.motor.maxRpm[1]
self.cp.orgRpm[2] = self.motor.maxRpm[2]
self.cp.orgRpm[3] = self.motor.maxRpm[3]
end
if self.ESLimiter ~= nil and self.ESLimiter.maxRPM[5] ~= nil then
self.cp.ESL = {}
self.cp.ESL[1] = self.ESLimiter.percentage[2]
self.cp.ESL[2] = self.ESLimiter.percentage[3]
self.cp.ESL[3] = self.ESLimiter.percentage[4]
end
self.CPnumCollidingVehicles = 0;
self.cp.collidingVehicleId = nil
courseplay:debug(string.format("%s: Start/Stop: deleting \"self.cp.collidingVehicleId\"", nameNum(self)), 3);
--self.numToolsCollidingVehicles = {};
self.drive = false
self.cp.isRecording = false
self.cp.recordingIsPaused = false
self.cp.calculatedCourseToCombine = false
AITractor.addCollisionTrigger(self, self);
if self.attachedCutters ~= nil then
for cutter, implement in pairs(self.attachedCutters) do
--remove cutter atTrafficCollisionTrigger in case of having changed or removed it while not in CP
if self.cp.trafficCollisionTriggers[0] ~= nil then
removeTrigger(self.cp.trafficCollisionTriggers[0])
self.cp.trafficCollisionTriggerToTriggerIndex[self.cp.trafficCollisionTriggers[0]] = nil
end
--set cutter aiTrafficCollisionTrigger to cp.aiTrafficCollisionTrigger's list
if cutter.aiTrafficCollisionTrigger ~= nil then
if cutter.cpTrafficCollisionTrigger == nil then
cutter.cpTrafficCollisionTrigger = clone(cutter.aiTrafficCollisionTrigger, true);
self.cp.trafficCollisionTriggers[0] = cutter.cpTrafficCollisionTrigger
else
self.cp.trafficCollisionTriggers[0] = cutter.cpTrafficCollisionTrigger
end
addTrigger(self.cp.trafficCollisionTriggers[0], 'cpOnTrafficCollisionTrigger', self);
self.cp.trafficCollisionTriggerToTriggerIndex[self.cp.trafficCollisionTriggers[0]] = 0
self.cp.collidingObjects[0] = {};
courseplay:debug(string.format("%s: Start/Stop: cutter.aiTrafficCollisionTrigger present -> adding %s to self.cp.trafficCollisionTriggers[0]",nameNum(self),tostring(self.cp.trafficCollisionTriggers[0])),3)
--self.cp.numCollidingObjects[0] = 0;
else
courseplay:debug(string.format('## Courseplay: %s: aiTrafficCollisionTrigger in cutter missing. Traffic collision prevention will not work!', nameNum(self)),3);
end
end
end
self.orig_maxnumber = self.maxnumber
-- set default modeState if not in mode 2 or 3
if self.cp.mode ~= 2 and self.cp.mode ~= 3 then
self.cp.modeState = 0
end
if self.recordnumber < 1 then
self.recordnumber = 1
end
-- add do working players if not already added
if self.cp.coursePlayerNum == nil then
self.cp.coursePlayerNum = courseplay:addToTotalCoursePlayers(self)
end;
--add to activeCoursePlayers
courseplay:addToActiveCoursePlayers(self);
self.cp.backMarkerOffset = nil
self.cp.aiFrontMarker = nil
courseplay:reset_tools(self)
-- show arrow
self.cp.distanceCheck = true
-- current position
local ctx, cty, ctz = getWorldTranslation(self.rootNode);
-- position of next waypoint
local cx, cz = self.Waypoints[self.recordnumber].cx, self.Waypoints[self.recordnumber].cz
-- distance
local dist = courseplay:distance(ctx, ctz, cx, cz)
local setLaneNumber = false;
for k,workTool in pairs(self.tippers) do --TODO temporary solution (better would be Tool:getIsAnimationPlaying(animationName))
if courseplay:isFolding(workTool) then
if self.setAIImplementsMoveDown ~= nil then
self:setAIImplementsMoveDown(true)
elseif self.setFoldState ~= nil then
self:setFoldState(-1, true)
end
end;
--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;
end;
local numWaitPoints = 0
local numCrossingPoints = 0
self.cp.waitPoints = {};
self.cp.shovelFillStartPoint = nil
self.cp.shovelFillEndPoint = nil
self.cp.shovelEmptyPoint = nil
local nearestpoint = dist
local recordNumber = 0
local curLaneNumber = 1;
-- print(('%s [%s(%d)]: start(), modeState=%d, mode2nextState=%s'):format(nameNum(self), curFile, debug.getinfo(1).currentline, self.cp.modeState, tostring(self.cp.mode2nextState))); -- DEBUG140301
for i,wp in pairs(self.Waypoints) do
local cx, cz = wp.cx, wp.cz
if self.cp.modeState == 0 or self.cp.modeState == 99 then
dist = courseplay:distance(ctx, ctz, cx, cz)
if dist <= nearestpoint then
nearestpoint = dist
recordNumber = i
end;
end;
if wp.wait then
numWaitPoints = numWaitPoints + 1;
self.cp.waitPoints[numWaitPoints] = i;
end;
if wp.crossing then
numCrossingPoints = numCrossingPoints + 1;
self.cp.crossingPoints[numCrossingPoints] = i;
end;
-- specific Workzone
if self.cp.mode == 4 or self.cp.mode == 6 or self.cp.mode == 7 then
if numWaitPoints == 1 and (self.cp.startWork == nil or self.cp.startWork == 0) then
self.cp.startWork = i
end
if numWaitPoints > 1 and (self.cp.stopWork == nil or self.cp.stopWork == 0) then
self.cp.stopWork = i
end
--unloading point for transporter
elseif self.cp.mode == 8 then
--
--work points for shovel
elseif self.cp.mode == 9 then
if numWaitPoints == 1 and self.cp.shovelFillStartPoint == nil then
self.cp.shovelFillStartPoint = i;
end;
if numWaitPoints == 2 and self.cp.shovelFillEndPoint == nil then
self.cp.shovelFillEndPoint = i;
end;
if numWaitPoints == 3 and self.cp.shovelEmptyPoint == nil then
self.cp.shovelEmptyPoint = i;
end;
end;
--laneNumber (for seeders)
if setLaneNumber and wp.generated ~= nil and wp.generated == true then
if wp.turnEnd ~= nil and wp.turnEnd == true then
curLaneNumber = curLaneNumber + 1;
courseplay:debug(string.format('%s: waypoint %d: turnEnd=true -> new curLaneNumber=%d', nameNum(self), i, curLaneNumber), 12);
end;
wp.laneNum = curLaneNumber;
end;
end;
-- mode 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.maxnumber;
end;
end;
self.cp.numWaitPoints = numWaitPoints;
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);
-- print(('%s [%s(%d)]: start(), modeState=%d, mode2nextState=%s, recordNumber=%d'):format(nameNum(self), curFile, debug.getinfo(1).currentline, self.cp.modeState, tostring(self.cp.mode2nextState), recordNumber)); -- DEBUG140301
if self.cp.modeState == 0 or self.cp.modeState == 99 then
local changed = false
for i=recordNumber,recordNumber+3 do
if self.Waypoints[i]~= nil and self.Waypoints[i].turn ~= nil then
self.recordnumber = i + 2
-- print(('\t(%d) self.recordnumber=%d'):format(debug.getinfo(1).currentline, self.recordnumber)); -- DEBUG140301
changed = true
break
end
end
if changed == false then
self.recordnumber = recordNumber
-- print(('\t(%d) self.recordnumber=%d'):format(debug.getinfo(1).currentline, self.recordnumber)); -- DEBUG140301
end
if self.recordnumber > self.maxnumber then
self.recordnumber = 1
end
end --END if modeState == 0
if self.recordnumber > 2 and self.cp.mode ~= 4 and self.cp.mode ~= 6 then
self.cp.isLoaded = true
-- print(('%s [%s(%d)]: start(), recordnumber=%d -> set isLoaded to true'):format(nameNum(self), curFile, debug.getinfo(1).currentline, self.recordnumber)); -- DEBUG140301
elseif self.cp.mode == 4 or self.cp.mode == 6 then
self.cp.isLoaded = false;
self.cp.hasUnloadingRefillingCourse = self.maxnumber > 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
self.cp.finishWork = self.cp.stopWork-5
else
self.cp.finishWork = self.cp.stopWork
end
if self.cp.finishWork ~= self.cp.stopWork and self.recordnumber > self.cp.finishWork and self.recordnumber <= self.cp.stopWork then
self.recordnumber = 2
end
courseplay:debug(string.format("%s: maxnumber=%d, stopWork=%d, finishWork=%d, hasUnloadingRefillingCourse=%s, recordnumber=%d", nameNum(self), self.maxnumber, self.cp.stopWork, self.cp.finishWork, tostring(self.cp.hasUnloadingRefillingCourse), self.recordnumber), 12);
end
if self.cp.mode == 9 or self.cp.startAtFirstPoint then
self.recordnumber = 1;
self.cp.shovelState = 1;
end;
courseplay:updateAllTriggers();
self.forceIsActive = true;
self.stopMotorOnLeave = false;
self.steeringEnabled = false;
self.deactivateOnLeave = false
self.disableCharacterOnLeave = false
-- ok i am near the waypoint, let's go
self.checkSpeedLimit = false
self.cp.runOnceStartCourse = true;
self.drive = true;
self.cp.maxFieldSpeed = 0
self.cp.isRecording = false
self.cp.distanceCheck = false;
if self.isRealistic then
self.cpSavedRealAWDModeOn = self.realAWDModeOn
end
--EifokLiquidManure
self.cp.EifokLiquidManure.searchMapHoseRefStation.pull = true;
self.cp.EifokLiquidManure.searchMapHoseRefStation.push = true;
courseplay:validateCanSwitchMode(self);
end;
function courseplay:getCanUseAiMode(vehicle)
if not vehicle.isMotorStarted or (vehicle.motorStartTime and vehicle.motorStartTime > vehicle.time) then
return false;
end;
local mode = vehicle.cp.mode;
if mode ~= 5 and mode ~= 6 and mode ~= 7 and not vehicle.cp.tipperAttached then
vehicle.cp.infoText = courseplay:loc('CPWrongTrailer');
return false;
end;
if mode == 3 or mode == 7 or mode == 8 then
if vehicle.cp.numWaitPoints < 1 then
vehicle.cp.infoText = string.format(courseplay:loc('CPTooFewWaitingPoints'), 1);
return false;
elseif vehicle.cp.numWaitPoints > 1 then
vehicle.cp.infoText = string.format(courseplay:loc('CPTooManyWaitingPoints'), 1);
return false;
end;
if mode == 3 then
if vehicle.tippers[1] == nil or vehicle.tippers[1].cp == nil or not vehicle.tippers[1].cp.isAugerWagon then
vehicle.cp.infoText = courseplay:loc('CPWrongTrailer');
return false;
end;
elseif mode == 7 then
if vehicle.isAutoCombineActivated ~= nil and vehicle.isAutoCombineActivated then
vehicle.cp.infoText = courseplay:loc('COURSEPLAY_NO_AUTOCOMBINE_MODE_7');
return false;
end;
end;
elseif mode == 4 or mode == 6 then
if vehicle.cp.startWork == nil or vehicle.cp.stopWork == nil then
vehicle.cp.infoText = courseplay:loc('CPNoWorkArea');
return false;
end;
if mode == 6 then
if vehicle.cp.hasBaleLoader then
if vehicle.cp.numWaitPoints < 2 then
vehicle.cp.infoText = string.format(courseplay:loc('CPTooFewWaitingPoints'), 2);
return false;
elseif vehicle.cp.numWaitPoints > 3 then
vehicle.cp.infoText = string.format(courseplay:loc('CPTooManyWaitingPoints'), 3);
return false;
end;
end;
end;
elseif mode == 9 then
if vehicle.cp.numWaitPoints < 3 then
vehicle.cp.infoText = string.format(courseplay:loc('CPTooFewWaitingPoints'), 3);
return false;
elseif vehicle.cp.numWaitPoints > 3 then
vehicle.cp.infoText = string.format(courseplay:loc('CPTooManyWaitingPoints'), 3);
return false;
end;
end;
return true;
end;
-- stops driving the course
function courseplay:stop(self)
--self:dismiss()
self.forceIsActive = false;
self.stopMotorOnLeave = true;
self.steeringEnabled = true;
self.deactivateOnLeave = true
self.disableCharacterOnLeave = true
if self.cp.orgRpm then
self.motor.maxRpm[1] = self.cp.orgRpm[1]
self.motor.maxRpm[2] = self.cp.orgRpm[2]
self.motor.maxRpm[3] = self.cp.orgRpm[3]
end
if self.ESLimiter ~= nil and self.ESLimiter.maxRPM[5] ~= nil then
self.ESLimiter.percentage[2] = self.cp.ESL[1]
self.ESLimiter.percentage[3] = self.cp.ESL[2]
self.ESLimiter.percentage[4] = self.cp.ESL[3]
end
self.cp.forcedToStop = false
self.cp.isRecording = false
self.cp.recordingIsPaused = false
if self.cp.modeState > 4 then
self.cp.modeState = 1
end
self.cp.turnStage = 0
self.cp.isTurning = nil
self.aiTractorTargetX = nil
self.aiTractorTargetZ = nil
self.aiTractorTargetBeforeTurnX = nil
self.aiTractorTargetBeforeTurnZ = nil
self.cp.backMarkerOffset = nil
self.cp.aiFrontMarker = nil
self.cp.aiTurnNoBackward = false
self.cp.noStopOnEdge = false
if self.isRealistic then
self.motor.speedLevel = 0
self:realSetAwdActive(self.cpSavedRealAWDModeOn)
if self.realForceAiDriven then
self.realForceAiDriven = false
end
end
self.cp.fillTrigger = nil
self.cp.unloadOrder = false
AITractor.removeCollisionTrigger(self, self);
self.cpTrafficCollisionIgnoreList = {}
self.cp.foundColli = {}
self.cp.inTraffic = false
self.cp.bypassWaypointsSet = false
--deactivate beacon lights
if self.beaconLightsActive then
self:setBeaconLightsVisibility(false);
end;
--open all covers
if self.cp.tipperAttached and self.cp.tipperHasCover and self.cp.mode == 1 or self.cp.mode == 2 or self.cp.mode == 5 or self.cp.mode == 6 then
courseplay:openCloseCover(self, nil, false);
end;
-- resetting variables
courseplay:setMinHudPage(self, nil);
self.cp.attachedCombineIdx = nil;
self.cp.tempCollis = {}
self.checkSpeedLimit = true
self.cp.currentTipTrigger = nil
self.drive = false
self.cp.canDrive = true
self.cp.distanceCheck = false
self.cp.mode7GoBackBeforeUnloading = false
if self.cp.checkReverseValdityPrinted then
self.cp.checkReverseValdityPrinted = false
end
self.motor:setSpeedLevel(0, false);
self.motor.maxRpmOverride = nil;
self.cp.startWork = nil
self.cp.stopWork = nil
self.cp.hasUnloadingRefillingCourse = false;
self.cp.stopAtEnd = false
self.cp.isUnloaded = false;
self.cp.prevFillLevel = nil;
self.cp.isInRepairTrigger = nil;
self.cp.hasBaleLoader = false;
self.cp.hasSowingMachine = false;
self.cp.hasPlough = false;
if self.cp.tempToolOffsetX ~= nil then
courseplay:changeToolOffsetX(self, nil, self.cp.tempToolOffsetX, true);
self.cp.tempToolOffsetX = nil
end;
self.cp.timers.slippingWheels = 0;
--remove any global info texts
if g_server ~= nil then
self.cp.infoText = nil;
for refIdx,_ in pairs(courseplay.globalInfoText.msgReference) do
if self.cp.activeGlobalInfoTexts[refIdx] ~= nil then
courseplay:setGlobalInfoText(self, refIdx, true);
end;
end;
end
--reset EifokLiquidManure
courseplay.thirdParty.EifokLiquidManure:resetData(self);
--remove from activeCoursePlayers
courseplay:removeFromActiveCoursePlayers(self);
--validation: can switch ai_mode?
courseplay:validateCanSwitchMode(self);
end