forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.lua
1812 lines (1615 loc) · 77.4 KB
/
drive.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local curFile = 'drive.lua';
local abs, max, min, pow, sin , huge = math.abs, math.max, math.min, math.pow, math.sin, math.huge;
local _;
local avoidWorkAreaType = {};
-- drives recored course
function courseplay:drive(self, dt)
-- Reset Character each 2 min to prevent glitching out.
if courseplay:timerIsThrough(self, "resetCharacter", false) then
if self.currentHelper == nil then
self.currentHelper = HelperUtil.getRandomHelper()
end;
if self.vehicleCharacter ~= nil then
self.vehicleCharacter:delete();
self.vehicleCharacter:loadCharacter(self.currentHelper.xmlFilename, getUserRandomizedMpColor(self.currentHelper.name));
if self.isEntered then
self.vehicleCharacter:setCharacterVisibility(false);
end;
end;
--print("Character have been reset!");
courseplay:setCustomTimer(self, "resetCharacter", 300);
end;
if self.cp.saveFuel then
if self.isMotorStarted then
--print("stop Order")
courseplay:setEngineState(self, false);
end
elseif not self.isMotorStarted then
courseplay:setEngineState(self, true);
--("start Order")
elseif not courseplay:getIsEngineReady(self) then
AIVehicleUtil.driveInDirection(self, dt, 30, -1, 0, 28, allowedToDrive, moveForwards, 0, 1)
end;
if not courseplay:getCanUseCpMode(self) then
return;
end;
--keeping steering disabled
if self.steeringEnabled then
self.steeringEnabled = false;
end
-- debug for workAreas
if courseplay.debugChannels[6] then
if self.cp.aiFrontMarker and self.cp.backMarkerOffset then
local directionNode = self.isReverseDriving and self.cp.reverseDrivingDirectionNode or self.cp.DirectionNode;
local tx1, ty1, tz1 = localToWorld(directionNode,3,1,self.cp.aiFrontMarker)
local tx2, ty2, tz2 = localToWorld(directionNode,3,1,self.cp.backMarkerOffset)
local nx, ny, nz = localDirectionToWorld(directionNode, -1, 0, 0)
local distance = 6
drawDebugLine(tx1, ty1, tz1, 1, 0, 0, tx1+(nx*distance), ty1+(ny*distance), tz1+(nz*distance), 1, 0, 0)
drawDebugLine(tx2, ty2, tz2, 1, 0, 0, tx2+(nx*distance), ty2+(ny*distance), tz2+(nz*distance), 1, 0, 0)
end;
if #avoidWorkAreaType == 0 then
avoidWorkAreaType[WorkArea.AREATYPE_RIDGEMARKER] = true;
avoidWorkAreaType[WorkArea.AREATYPE_MOWERDROP] = true;
avoidWorkAreaType[WorkArea.AREATYPE_WINDROWERDROP] = true;
avoidWorkAreaType[WorkArea.AREATYPE_TEDDERDROP] = true;
end;
for _,workTool in pairs(self.cp.workTools) do
if workTool.workAreas then
for k = 1, #workTool.workAreas do
if not avoidWorkAreaType[workTool.workAreas[k].type] then
for _, workArea in ipairs(workTool.workAreas) do
local sx, sy, sz = getWorldTranslation(workArea["start"]);
drawDebugLine(sx, sy, sz, 1, 0, 0, sx, sy+3, sz, 1, 0, 0);
local wx, wy, wz = getWorldTranslation(workArea["width"]);
drawDebugLine(wx, wy, wz, 1, 0, 0, wx, wy+3, wz, 1, 0, 0);
local hx, hy, hz = getWorldTranslation(workArea["height"]);
drawDebugLine(hx, hy, hz, 1, 0, 0, hx, hy+3, hz, 1, 0, 0);
end;
end;
end;
end;
end;
end
local forceTrueSpeed = false
local refSpeed = huge
local speedDebugLine = "refSpeed"
self.cp.speedDebugLine = "no speed info"
self.cp.speedDebugStreet = nil
local cx,cy,cz = 0,0,0
-- may I drive or should I hold position for some reason?
local allowedToDrive = true
self.cp.curSpeed = self.lastSpeedReal * 3600;
-- TIPPER FILL LEVELS (get once for all following functions)
courseplay:updateFillLevelsAndCapacities(self)
-- RESET TRIGGER RAYCASTS
self.cp.hasRunRaycastThisLoop['tipTrigger'] = false;
self.cp.hasRunRaycastThisLoop['specialTrigger'] = false;
-- combine self unloading
if self.cp.mode == 7 then
local continue;
continue, cx, cy, cz, refSpeed, allowedToDrive = courseplay:handleMode7(self, cx, cy, cz, refSpeed, allowedToDrive);
if not continue then
return;
end;
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-4).."): refSpeed = "..tostring(refSpeed))
end;
--[[ unregister at combine, if there is one
if self.cp.isLoaded == true and self.cp.positionWithCombine ~= nil then
courseplay:unregisterFromCombine(self, self.cp.activeCombine)
end]]
-- current position
local ctx, cty, ctz = getWorldTranslation(self.cp.DirectionNode);
if self.Waypoints[self.cp.waypointIndex].rev and self.cp.oldDirectionNode then
ctx, cty, ctz = getWorldTranslation(self.cp.oldDirectionNode);
end;
if self.cp.waypointIndex > self.cp.numWaypoints then
courseplay:debug(string.format("drive %d: %s: self.cp.waypointIndex (%s) > self.cp.numWaypoints (%s)", debug.getinfo(1).currentline, nameNum(self), tostring(self.cp.waypointIndex), tostring(self.cp.numWaypoints)), 12); --this should never happen
courseplay:setWaypointIndex(self, self.cp.numWaypoints);
end;
if self.cp.mode ~= 7 or (self.cp.mode == 7 and self.cp.modeState ~= 5) then
cx, cz = self.Waypoints[self.cp.waypointIndex].cx, self.Waypoints[self.cp.waypointIndex].cz
end
-- FIELDWORK - HORIZONTAL/VERTICAL OFFSET
if courseplay:getIsVehicleOffsetValid(self) then
cx, cz = courseplay:getVehicleOffsettedCoords(self, cx, cz);
if courseplay.debugChannels[12] and self.cp.isTurning == nil then
drawDebugPoint(cx, cty+3, cz, 0, 1 , 1, 1);
end;
end;
-- LOAD/UNLOAD/WAIT - HORIZONTAL/VERTICAL OFFSET
if courseplay:getIsVehicleOffsetValid(self, true) then
cx, cz = courseplay:getVehicleOffsettedCoords(self, cx, cz, true);
if courseplay.debugChannels[12] and self.cp.isTurning == nil then
drawDebugPoint(cx, cty+3, cz, 0, 1 , 1, 1);
end;
end;
if courseplay.debugChannels[12] and self.cp.isTurning == nil then
local posY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 300, cz);
drawDebugLine(ctx, cty + 3, ctz, 0, 1, 0, cx, posY + 3, cz, 0, 0, 1)
end;
if CpManager.isDeveloper and self.cp.hasSpecializationArticulatedAxis and courseplay.debugChannels[12] then
local posY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 300, cz);
local desX, _, desZ = localToWorld(self.cp.DirectionNode, 0, 0, 5);
drawDebugLine(ctx, cty + 3.5, ctz, 0, 0, 1, desX, cty + 3.5, desZ, 0, 0, 1);
end;
self.cp.distanceToTarget = courseplay:distance(cx, cz, ctx, ctz);
-- courseplay:debug(('ctx=%.2f, ctz=%.2f, cx=%.2f, cz=%.2f, distanceToTarget=%.2f'):format(ctx, ctz, cx, cz, self.cp.distanceToTarget), 2);
local fwd;
local distToChange;
-- coordinates of coli
local tx, ty, tz = localToWorld(self.cp.DirectionNode, 0, 1, 3); --local tx, ty, tz = getWorldTranslation(self.aiTrafficCollisionTrigger)
-- local direction of from DirectionNode to waypoint
local lx, lz = AIVehicleUtil.getDriveDirection(self.cp.DirectionNode, cx, cty, cz);
-- world direction of from DirectionNode to waypoint
local nx, ny, nz = localDirectionToWorld(self.cp.DirectionNode, lx, 0, lz);
if self.cp.mode == 4 or self.cp.mode == 6 then
if self.Waypoints[self.cp.waypointIndex].turnStart then
self.cp.isTurning = self.Waypoints[self.cp.waypointIndex].turnStart;
end
--- If we are turning and abortWork is set, then check if we need to abort the turn
if self.cp.isTurning and self.cp.abortWork ~= nil then
local abortTurn = false;
-- Mode 4 Check
if self.cp.mode == 4 and self.cp.workTools ~= nil then
for _,tool in pairs(self.cp.workTools) do
local hasMoreFillUnits = courseplay:setOwnFillLevelsAndCapacities(tool)
if hasMoreFillUnits and tool ~= self and
((tool.sowingMachine ~= nil and self.cp.totalSeederFillLevelPercent == 0) or (tool.sprayer ~= nil and self.cp.totalSprayerFillLevelPercent == 0))
then
abortTurn = true;
end;
end;
-- Mode 6 Check
elseif self.cp.mode == 6 and self.cp.totalFillLevelPercent == 100 then
abortTurn = true;
end;
-- Abort turn if needed.
if abortTurn then
self.cp.isTurning = nil;
if #(self.cp.turnTargets) > 0 then
courseplay:clearTurnTargets(self);
end;
end;
end
--RESET OFFSET TOGGLES
if not self.cp.isTurning then
if self.cp.symmetricLaneChange and not self.cp.switchLaneOffset then
self.cp.switchLaneOffset = true;
courseplay:debug(string.format("%s: isTurning=false, switchLaneOffset=false -> set switchLaneOffset to true", nameNum(self)), 12);
end;
if self.cp.hasPlough and self.cp.hasRotateablePlough and not self.cp.switchToolOffset then
self.cp.switchToolOffset = true;
courseplay:debug(string.format("%s: isTurning=false, switchToolOffset=false -> set switchToolOffset to true", nameNum(self)), 12);
end;
end;
end;
-- LIGHTS
local lightMask = 0
local combineBeaconOn = self.cp.isCombine and self.cp.totalFillLevelPercent > 80;
local beaconOn = (self.cp.warningLightsMode == courseplay.WARNING_LIGHTS_BEACON_ALWAYS
or ((self.cp.mode == 1 or self.cp.mode == 2 or self.cp.mode == 3 or self.cp.mode == 5) and self.cp.waypointIndex > 2 and self.cp.trailerFillDistance == nil)
or ((self.cp.mode == 4 or self.cp.mode == 6) and self.cp.waypointIndex > self.cp.stopWork)
or (self.cp.mode == 10 and (self.cp.waypointIndex > 1 or #self.cp.mode10.stoppedCourseplayers >0) )
or combineBeaconOn) or false;
if beaconOn then
lightMask = 1
else
lightMask = 7
end
if self.cp.warningLightsMode == courseplay.WARNING_LIGHTS_NEVER then -- never
if self.beaconLightsActive then
self:setBeaconLightsVisibility(false);
end;
if self.cp.hasHazardLights and self.turnLightState ~= Lights.TURNSIGNAL_OFF then
self:setTurnLightState(Lights.TURNLIGHT_OFF);
end;
else -- on street/always
if self.beaconLightsActive ~= beaconOn then
self:setBeaconLightsVisibility(beaconOn);
end;
if self.cp.hasHazardLights then
local hazardOn = self.cp.warningLightsMode == courseplay.WARNING_LIGHTS_BEACON_HAZARD_ON_STREET and beaconOn and not combineBeaconOn;
if not hazardOn and self.turnLightState ~= Lights.TURNLIGHT_OFF then
self:setTurnLightState(Lights.TURNLIGHT_OFF);
elseif hazardOn and self.turnLightState ~= Lights.TURNLIGHT_HAZARD then
self:setTurnLightState(Lights.TURNLIGHT_HAZARD);
end;
end;
end;
-- lights
if CpManager.lightsNeeded then
if self.lightsTypesMask ~= lightMask then
self:setLightsTypesMask(lightMask)
end
elseif self.lightsTypesMask ~= 0 then
self:setLightsTypesMask(0)
end;
-- the tipper that is currently loaded/unloaded
local activeTipper;
local isBypassing = false
local isCrawlingToWait = false
local isWaitingThisLoop = false
local drive_on = false
local wayPointIsWait = self.Waypoints[self.cp.previousWaypointIndex].wait
local wayPointIsUnload = self.Waypoints[self.cp.previousWaypointIndex].unload
local wayPointIsRevUnload = wayPointIsUnload and self.Waypoints[self.cp.previousWaypointIndex].rev
local stopForUnload = false
local breakCode = false
local revUnloadingPoint = 0
if self.Waypoints[self.cp.previousWaypointIndex].rev and self.cp.numUnloadPoints > 0 then
for i=1,self.cp.numUnloadPoints do
local index = self.cp.unloadPoints[i]
if self.Waypoints[index].rev then
revUnloadingPoint = index
end
end
end
if revUnloadingPoint > 0 then
stopForUnload,breakCode = courseplay:handleUnloading(self,true,dt,revUnloadingPoint)
if stopForUnload then
allowedToDrive = false;
end
if breakCode then
return;
end
end
-- ### WAITING POINTS - START
if (wayPointIsWait or wayPointIsUnload) and self.cp.wait then
isWaitingThisLoop = true
-- set wait time end
if self.cp.waitTimer == nil and self.cp.waitTime > 0 then
self.cp.waitTimer = self.timer + self.cp.waitTime * 1000;
end;
if self.cp.mode <= 2 then
if wayPointIsUnload then
stopForUnload,breakCode = courseplay:handleUnloading(self,wayPointIsRevUnload,dt)
elseif self.cp.mode == 1 and wayPointIsWait then
if self.cp.hasAugerWagon then
courseplay:handle_mode1(self, allowedToDrive, dt);
else
CpManager:setGlobalInfoText(self, 'WAIT_POINT');
end;
end;
elseif self.cp.mode == 3 and self.cp.workToolAttached then
courseplay:handleMode3(self, allowedToDrive, dt);
elseif self.cp.mode == 4 then
if self.cp.previousWaypointIndex == self.cp.startWork then
courseplay:setVehicleWait(self, false);
elseif self.cp.previousWaypointIndex == self.cp.stopWork and self.cp.abortWork ~= nil then
courseplay:setVehicleWait(self, false);
elseif self.cp.waitPoints[3] and self.cp.previousWaypointIndex == self.cp.waitPoints[3] then
local isInWorkArea = self.cp.waypointIndex > self.cp.startWork and self.cp.waypointIndex <= self.cp.stopWork;
if self.cp.workToolAttached and self.cp.startWork ~= nil and self.cp.stopWork ~= nil and self.cp.workTools ~= nil and not isInWorkArea then
allowedToDrive,lx,lz = courseplay:refillWorkTools(self, self.cp.refillUntilPct, allowedToDrive, lx, lz, dt);
end;
if courseplay:timerIsThrough(self, "fillLevelChange") or self.cp.prevFillLevelPct == nil then
if self.cp.prevFillLevelPct ~= nil and self.cp.totalFillLevelPercent == self.cp.prevFillLevelPct and self.cp.totalFillLevelPercent >= self.cp.refillUntilPct then
drive_on = true
end
self.cp.prevFillLevelPct = self.cp.totalFillLevelPercent
courseplay:setCustomTimer(self, "fillLevelChange", 7);
end
if self.cp.totalFillLevelPercent >= self.cp.refillUntilPct or drive_on then
courseplay:setVehicleWait(self, false);
end
courseplay:setInfoText(self, ('COURSEPLAY_LOADING_AMOUNT;%d;%d'):format(courseplay.utils:roundToLowerInterval(self.cp.totalFillLevel, 100), self.cp.totalCapacity));
end
elseif self.cp.mode == 6 then
if wayPointIsUnload then
if self.cp.makeHeaps then
stopForUnload = courseplay:handleHeapUnloading(self);
else
stopForUnload,breakCode = courseplay:handleUnloading(self,wayPointIsRevUnload,dt);
end;
elseif self.cp.previousWaypointIndex == self.cp.startWork then
courseplay:setVehicleWait(self, false);
elseif self.cp.previousWaypointIndex == self.cp.stopWork and self.cp.abortWork ~= nil then
courseplay:setVehicleWait(self, false);
elseif self.cp.previousWaypointIndex ~= self.cp.startWork and self.cp.previousWaypointIndex ~= self.cp.stopWork then
if self.cp.hasBaleLoader then
CpManager:setGlobalInfoText(self, 'UNLOADING_BALE');
else
CpManager:setGlobalInfoText(self, 'OVERLOADING_POINT');
-- Set Timer if unloading pipe takes time before empty.
if self.getFirstEnabledFillType and self.pipeParticleSystems and self.cp.totalFillLevelPercent > 0 then
local filltype = self:getFirstEnabledFillType();
if filltype ~= FillUtil.FILLTYPE_UNKNOWN and self.pipeParticleSystems[filltype] then
local stopTime = self.pipeParticleSystems[filltype][1].stopTime;
if stopTime then
courseplay:setCustomTimer(self, "waitUntilPipeIsEmpty", stopTime);
end;
end;
end;
end;
if (self.cp.totalFillLevelPercent == 0 and courseplay:timerIsThrough(self, "waitUntilPipeIsEmpty")) or drive_on then
courseplay:resetCustomTimer(self, "waitUntilPipeIsEmpty", true);
courseplay:setVehicleWait(self, false);
end;
end;
elseif self.cp.mode == 7 then
if wayPointIsUnload and self.cp.makeHeaps then
stopForUnload = courseplay:handleHeapUnloading(self);
elseif wayPointIsWait then
if self.cp.totalFillLevel > 0 then
CpManager:setGlobalInfoText(self, 'OVERLOADING_POINT');
-- Set Timer if unloading pipe takes time before empty.
if self.getFirstEnabledFillType and self.pipeParticleSystems and self.cp.totalFillLevelPercent > 0 then
local filltype = self:getFirstEnabledFillType();
if filltype ~= FillUtil.FILLTYPE_UNKNOWN and self.pipeParticleSystems[filltype] then
local stopTime = self.pipeParticleSystems[filltype][1].stopTime;
if stopTime then
courseplay:setCustomTimer(self, "waitUntilPipeIsEmpty", stopTime);
end;
end;
end;
elseif courseplay:timerIsThrough(self, "waitUntilPipeIsEmpty") then
courseplay:resetCustomTimer(self, "waitUntilPipeIsEmpty", true);
courseplay:setVehicleWait(self, false);
self.cp.isUnloaded = true
end
end;
elseif self.cp.mode == 8 then
allowedToDrive, lx, lz = courseplay:handleMode8(self, false, true, allowedToDrive, lx, lz, dt);
elseif self.cp.mode == 9 then
courseplay:setVehicleWait(self, false);
elseif self.cp.mode == 10 then
if #self.cp.mode10.stoppedCourseplayers > 0 then
for i=1,#self.cp.mode10.stoppedCourseplayers do
local courseplayer = self.cp.mode10.stoppedCourseplayers[i]
local tx,tz = self.Waypoints[1].cx,self.Waypoints[1].cz
local ty = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, tx, 1, tz);
local distance = courseplay:distanceToPoint(courseplayer,tx,ty,tz)
if distance > self.cp.mode10.searchRadius then
table.remove(self.cp.mode10.stoppedCourseplayers,i)
break
end
end
end
if (self.cp.actualTarget and self.cp.actualTarget.empty) or (self.cp.mode10.deadline and self.cp.mode10.deadline == self.cp.mode10.firstLine) then
if courseplay:timerIsThrough(self,'BunkerEmpty') then
courseplay:setCustomTimer(self, "BunkerEmpty", 5);
courseplay:getActualTarget(self)
--print("check for FillLevel")
end
else
self.cp.actualTarget = nil
end
else
CpManager:setGlobalInfoText(self, 'WAIT_POINT');
end;
-- wait time passed -> continue driving
if self.cp.waitTimer and self.timer > self.cp.waitTimer then
self.cp.waitTimer = nil
courseplay:setVehicleWait(self, false);
end
isCrawlingToWait = true
if wayPointIsWait or wayPointIsRevUnload then
local _,_,zDist = worldToLocal(self.cp.DirectionNode, self.Waypoints[self.cp.previousWaypointIndex].cx, cty, self.Waypoints[self.cp.previousWaypointIndex].cz);
if zDist < 1 then -- don't stop immediately when hitting the waitPoints waypointIndex, but rather wait until we're close enough (1m)
allowedToDrive = false;
end;
elseif stopForUnload then
allowedToDrive = false;
end;
if breakCode then
return
end
-- ### WAITING POINTS - END
-- ### NON-WAITING POINTS
else
-- MODES 1 & 2: unloading in trigger
if (self.cp.mode == 1 or (self.cp.mode == 2 and self.cp.isLoaded)) and self.cp.totalFillLevel ~= nil and self.cp.tipRefOffset ~= nil and self.cp.workToolAttached then
if not self.cp.hasAugerWagon and self.cp.currentTipTrigger == nil and self.cp.totalFillLevel > 0 and self.cp.waypointIndex > 2 and self.cp.waypointIndex < self.cp.numWaypoints and not self.Waypoints[self.cp.waypointIndex].rev then
courseplay:doTriggerRaycasts(self, 'tipTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
end;
allowedToDrive,breakCode = courseplay:handle_mode1(self, allowedToDrive, dt);
end;
if breakCode then
return
end
--resetTrailer when empty after unloading in Bunkersilo
if self.cp.totalFillLevel == 0 then
for _,tipper in pairs (self.cp.workTools) do
if tipper.tipState ~= nil and (tipper.tipState == Trailer.TIPSTATE_OPENING or tipper.tipState == Trailer.TIPSTATE_OPEN) then
tipper:toggleTipState();
end
end
end
-- COMBI MODE / BYPASSING
if (((self.cp.mode == 2 or self.cp.mode == 3) and self.cp.waypointIndex < 2) or self.cp.activeCombine) and self.cp.workToolAttached then
self.cp.inTraffic = false
courseplay:handle_mode2(self, dt);
return;
elseif (self.cp.mode == 2 or self.cp.mode == 3) and self.cp.waypointIndex < 3 then
isBypassing = true
lx, lz = courseplay:isTheWayToTargetFree(self,lx, lz)
elseif self.cp.mode == 6 and self.cp.hasBaleLoader and (self.cp.waypointIndex == self.cp.stopWork + 1 or (self.cp.abortWork ~= nil and self.cp.waypointIndex == self.cp.abortWork)) then
isBypassing = true
lx, lz = courseplay:isTheWayToTargetFree(self,lx, lz)
elseif self.cp.mode ~= 7 and self.cp.mode ~= 10 then
if self.cp.modeState ~= 0 then
courseplay:setModeState(self, 0);
end;
end;
-- MODE 3: UNLOADING
if self.cp.mode == 3 and self.cp.workToolAttached and self.cp.waypointIndex >= 2 and self.cp.modeState == 0 then
courseplay:handleMode3(self, allowedToDrive, dt);
-- MODE 4: REFILL SPRAYER or SEEDER
elseif self.cp.mode == 4 then
if self.cp.workToolAttached and self.cp.startWork ~= nil and self.cp.stopWork ~= nil then
local isInWorkArea = self.cp.waypointIndex > self.cp.startWork and self.cp.waypointIndex <= self.cp.stopWork;
if self.cp.workTools ~= nil and not isInWorkArea then
allowedToDrive,lx,lz = courseplay:refillWorkTools(self, self.cp.refillUntilPct, allowedToDrive, lx, lz, dt);
end
end;
-- MODE 6-7: HEAP UNLOADING
elseif (self.cp.mode == 6 or self.cp.mode == 7) and self.cp.makeHeaps then
courseplay:handleHeapUnloading(self);
-- MODE 8: REFILL LIQUID MANURE TRANSPORT
elseif self.cp.mode == 8 then
allowedToDrive, lx, lz = courseplay:handleMode8(self, true, false, allowedToDrive, lx, lz, dt, tx, ty, tz, nx, ny, nz);
end;
-- MAP WEIGHT STATION
if courseplay:canUseWeightStation(self) then
if self.cp.curMapWeightStation ~= nil or (self.cp.fillTrigger ~= nil and courseplay.triggers.all[self.cp.fillTrigger].isWeightStation) then
allowedToDrive = courseplay:handleMapWeightStation(self, allowedToDrive);
elseif courseplay:canScanForWeightStation(self) then
courseplay:doTriggerRaycasts(self, 'specialTrigger', 'fwd', false, tx, ty, tz, nx, ny, nz);
end;
end;
--VEHICLE DAMAGE
if self.damageLevel then
if self.damageLevel >= 90 and not self.isInRepairTrigger then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'DAMAGE_MUST');
elseif self.damageLevel >= 50 and not self.isInRepairTrigger then
CpManager:setGlobalInfoText(self, 'DAMAGE_SHOULD');
end;
if self.damageLevel > 70 then
courseplay:doTriggerRaycasts(self, 'specialTrigger', 'fwd', false, tx, ty, tz, nx, ny, nz);
if self.cp.fillTrigger ~= nil then
if courseplay.triggers.all[self.cp.fillTrigger].isDamageModTrigger then
self.cp.isInFilltrigger = true
end
end
if self.isInRepairTrigger then
self.cp.isInRepairTrigger = true
end;
elseif self.damageLevel == 0 then
self.cp.isInRepairTrigger = false
end;
if self.cp.isInRepairTrigger then
allowedToDrive = false;
self.cp.fillTrigger = nil;
CpManager:setGlobalInfoText(self, 'DAMAGE_IS');
end;
end;
--FUEL LEVEL + REFILLING
if self.fuelCapacity > 0 then
local currentFuelPercentage = (self.fuelFillLevel / self.fuelCapacity + 0.0001) * 100;
local searchForFuel = (self.cp.allwaysSearchFuel and (currentFuelPercentage < 99) and self.cp.waypointIndex > 2 and self.cp.waypointIndex < self.cp.numWaypoints) or (currentFuelPercentage < 20) and not self.isFuelFilling
if searchForFuel then
courseplay:doTriggerRaycasts(self, 'specialTrigger', 'fwd', false, tx, ty, tz, nx, ny, nz);
if self.cp.fillTrigger ~= nil and courseplay.triggers.all[self.cp.fillTrigger].isGasStationTrigger then
self.cp.isInFilltrigger = true;
end;
if self.fuelFillTriggers[1] then
allowedToDrive = false;
self:setIsFuelFilling(true, self.fuelFillTriggers[1].isEnabled, false);
end;
end
if currentFuelPercentage < 5 then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'FUEL_MUST');
elseif currentFuelPercentage < 20 and not self.isFuelFilling then
CpManager:setGlobalInfoText(self, 'FUEL_SHOULD');
elseif self.isFuelFilling and currentFuelPercentage < 99.9 then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'FUEL_IS');
end;
if self.fuelFillTriggers[1] and self.cp.fillTrigger and courseplay.triggers.all[self.cp.fillTrigger].isGasStationTrigger then
courseplay:debug(nameNum(self) .. ': self.fuelFillTriggers[1] ~= nil -> resetting "self.cp.fillTrigger"', 1);
self.cp.fillTrigger = nil;
end;
end;
-- WATER WARNING
if self.showWaterWarning then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'WATER');
end;
-- STOP AT END OR TRIGGER
if self.cp.stopAtEnd and (self.cp.waypointIndex == self.cp.numWaypoints or self.cp.currentTipTrigger ~= nil or self.cp.fillTrigger ~= nil) then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'END_POINT');
end;
-- STOP AT END MODE 1
if self.cp.stopAtEndMode1 and self.cp.waypointIndex == self.cp.numWaypoints then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'END_POINT_MODE_1');
end;
--STOP AT END OF RUNCOUNTER
if self.cp.runCounter >= self.cp.runNumber then
if self.cp.mode == 8 and (self.cp.fillTrigger or self.cp.isInFilltrigger) and not self.cp.runCounterBool and not courseplay:waypointsHaveAttr(self, self.cp.waypointIndex, -3, 3, 'wait', true, false) then
allowedToDrive = false;
self.cp.runReset = true;
courseplay:debug(string.format('%s: Mode8 Has tried to stop', nameNum(self)), 12);
CpManager:setGlobalInfoText(self, 'END_POINT_MODE_8');
elseif self.cp.mode == 1 and self.cp.waypointIndex == self.cp.numWaypoints then
allowedToDrive = false;
self.cp.runReset = true;
CpManager:setGlobalInfoText(self, 'END_POINT_MODE_1');
courseplay:debug(string.format('%s: Mode 1 Has tried to stop', nameNum(self)), 12);
elseif self.cp.runCounter > self.cp.runNumber then --Something broke stop.
allowedToDrive = false;
self.cp.runReset = true
courseplay:debug(string.format('%s: Something went wrong with runcounter stopping', nameNum(self)), 12);
CpManager:setGlobalInfoText(self, 'END_POINT_MODE_8');
end;
end;
end;
-- ### NON-WAITING POINTS END
--------------------------------------------------
local workArea = false;
local workSpeed = 0;
local isFinishingWork = false;
-- MODE 4
if self.cp.mode == 4 and self.cp.startWork ~= nil and self.cp.stopWork ~= nil and self.cp.workToolAttached then
allowedToDrive, workArea, workSpeed, isFinishingWork, refSpeed = courseplay:handle_mode4(self, allowedToDrive, workSpeed, refSpeed);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if not workArea and self.cp.totalFillLevelPercent < self.cp.refillUntilPct then
courseplay:doTriggerRaycasts(self, 'specialTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
end;
-- MODE 6
elseif self.cp.mode == 6 and self.cp.startWork ~= nil and self.cp.stopWork ~= nil then
allowedToDrive, workArea, workSpeed, breakCode, isFinishingWork,refSpeed = courseplay:handle_mode6(self, allowedToDrive, workSpeed, lx, lz,refSpeed,dt);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if not workArea and self.cp.currentTipTrigger == nil and self.cp.totalFillLevel and self.cp.totalFillLevel > 0 and self.capacity == nil and self.cp.tipRefOffset ~= nil and not self.Waypoints[self.cp.waypointIndex].rev then
courseplay:doTriggerRaycasts(self, 'tipTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
end;
if breakCode then
return
end
-- MODE 9
elseif self.cp.mode == 9 then
allowedToDrive,lx,lz = courseplay:handle_mode9(self,self.cp.totalFillLevelPercent, allowedToDrive,lx,lz, dt);
-- MODE 10
elseif self.cp.mode == 10 then
local continue = true ;
continue,allowedToDrive = courseplay:handleMode10(self,allowedToDrive,lx,lz, dt);
if self.cp.shieldState ~= self.cp.targetShieldState then
--print(string.format("self.cp.shieldState(%s) ~= self.cp.targetShieldState(%s)",tostring(self.cp.shieldState),tostring(self.cp.targetShieldState)))
if courseplay:moveShield(self,self.cp.targetShieldState,dt) then
self.cp.shieldState = self.cp.targetShieldState
end
end
if not continue then
courseplay:checkSaveFuel(self,allowedToDrive)
return;
end;
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-4).."): refSpeed = "..tostring(refSpeed))
end;
self.cp.inTraffic = false;
-- HANDLE TIPPER COVER
if self.cp.tipperHasCover and self.cp.automaticCoverHandling and (self.cp.mode == 1 or self.cp.mode == 2 or self.cp.mode == 4 or self.cp.mode == 5 or self.cp.mode == 6) then
local showCover = false;
if self.cp.mode ~= 6 and self.cp.mode ~= 4 then
local minCoverWaypoint = self.cp.mode == 1 and 4 or 3;
showCover = self.cp.waypointIndex >= minCoverWaypoint and self.cp.waypointIndex < self.cp.numWaypoints and self.cp.currentTipTrigger == nil and self.cp.trailerFillDistance == nil and not courseplay:waypointsHaveAttr(self, self.cp.waypointIndex, -1, 2, "unload", true, false);
elseif self.cp.mode == 4 then
if self.cp.waitPoints[3] and self.cp.previousWaypointIndex == self.cp.waitPoints[3] then
-- open cover at loading point
showCover = false;
else
showCover = true; --will be handled in courseplay:openCloseCover() to prevent extra loops
end;
else
showCover = not workArea and self.cp.currentTipTrigger == nil;
end;
courseplay:openCloseCover(self, dt, showCover, self.cp.currentTipTrigger ~= nil);
elseif self.cp.tipperHasCover then
courseplay:openCloseCover(self, dt, false);
end;
-- CHECK TRAFFIC
allowedToDrive = courseplay:checkTraffic(self, true, allowedToDrive)
if self.cp.waitForTurnTime > self.timer or self.cp.isNotAllowedToDrive then
allowedToDrive = false
end
-- MODE 9 --TODO (Jakob): why is this in drive instead of mode9?
local WpUnload = false
if self.cp.shovelEmptyPoint ~= nil and self.cp.waypointIndex >=3 then
WpUnload = self.cp.waypointIndex == self.cp.shovelEmptyPoint
end
if WpUnload then
local i = self.cp.shovelEmptyPoint
local x,y,z = getWorldTranslation(self.cp.DirectionNode)
local _,_,ez = worldToLocal(self.cp.DirectionNode, self.Waypoints[i].cx , y , self.Waypoints[i].cz)
if ez < 0 then
allowedToDrive = false
end
end
local WpLoadEnd = false
if self.cp.shovelFillEndPoint ~= nil and self.cp.waypointIndex >=3 then
WpLoadEnd = self.cp.waypointIndex == self.cp.shovelFillEndPoint
end
if WpLoadEnd then
local i = self.cp.shovelFillEndPoint
local x,y,z = getWorldTranslation(self.cp.DirectionNode)
local _,_,ez = worldToLocal(self.cp.DirectionNode, self.Waypoints[i].cx , y , self.Waypoints[i].cz)
if ez < 0.2 then
if self.cp.totalFillLevelPercent == 0 then
allowedToDrive = false
CpManager:setGlobalInfoText(self, 'WORK_END');
else
courseplay:setIsLoaded(self, true);
courseplay:setWaypointIndex(self, i + 2);
end
end
end
-- MODE 9 END
courseplay:checkSaveFuel(self,allowedToDrive)
-- allowedToDrive false -> STOP OR HOLD POSITION
if not allowedToDrive then
-- reset slipping timers
courseplay:resetSlippingTimers(self)
if courseplay.debugChannels[21] then
renderText(0.5,0.85-(0.03*self.cp.coursePlayerNum),0.02,string.format("%s: self.lastSpeedReal: %.8f km/h ",nameNum(self),self.lastSpeedReal*3600))
end
self.cp.TrafficBrake = false;
self.cp.isTrafficBraking = false;
local moveForwards = true;
if self.cp.curSpeed > 1 and not self.cp.mode == 9 then
allowedToDrive = true;
moveForwards = self.movingDirection == 1;
end;
AIVehicleUtil.driveInDirection(self, dt, 30, -1, 0, 28, allowedToDrive, moveForwards, 0, 1)
self.cp.speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): allowedToDrive false ")
return;
end;
-- reset fixedWorldPosition
if self.cp.fixedWorldPosition ~= nil then
courseplay:deleteFixedWorldPosition(self);
end;
local isFieldWorking = self.cp.mode == 4 or self.cp.mode == 6;
if self.cp.isTurning then
if isFieldWorking then
if self.cp.turnTimeRecorded then
self.cp.turnTimeRecorded = nil
self.cp.recordedTurnTime = nil
else
self.cp.recordedTurnTime = (self.cp.recordedTurnTime or 0) + dt
end
end
courseplay:turn(self, dt);
self.cp.TrafficBrake = false
return
elseif isFieldWorking and self.cp.recordedTurnTime and not self.cp.turnTimeRecorded then
local turnCount = 0
for i=self.cp.waypointIndex,self.cp.stopWork do
if self.Waypoints[i].turnStart then
turnCount = turnCount +1
end
end
self.cp.calculatedTurnTime = turnCount*(self.cp.recordedTurnTime/1000)
--print(" self.cp.recordedTurnTime: "..tostring(self.cp.recordedTurnTime).." turns: "..tostring(turnCount))
self.cp.turnTimeRecorded = true
elseif isFieldWorking and self.cp.waypointIndex < self.cp.stopWork then
local distance = (self.cp.stopWork-self.cp.waypointIndex) *self.cp.mediumWpDistance -- m
local speed = self.cruiseControl.speed/3.6 --m/s
local turnTime = math.floor(self.cp.calculatedTurnTime or 5)
self.cp.timeRemaining = distance/speed + turnTime
elseif not isFieldWorking then
self.cp.timeRemaining = nil
end
--SPEED SETTING
local isAtEnd = self.cp.waypointIndex > self.cp.numWaypoints - 2;
local isAtStart = self.cp.waypointIndex < 3;
if ((self.cp.mode == 1 or self.cp.mode == 5 or self.cp.mode == 8) and (isAtStart or isAtEnd or self.cp.trailerFillDistance ~= nil))
or ((self.cp.mode == 2 or self.cp.mode == 3) and isAtEnd)
or (self.cp.mode == 9 and self.cp.waypointIndex > self.cp.shovelFillStartPoint and self.cp.waypointIndex <= self.cp.shovelFillEndPoint)
or (not workArea and self.cp.wait and ((isAtEnd and self.Waypoints[self.cp.waypointIndex].wait) or courseplay:waypointsHaveAttr(self, self.cp.waypointIndex, 0, 2, "wait", true, false)))
or courseplay:waypointsHaveAttr(self, self.cp.waypointIndex, 0, 2, "unload", true, false)
or (isAtEnd and self.Waypoints[self.cp.waypointIndex].rev)
or (not isAtEnd and (self.Waypoints[self.cp.waypointIndex].rev or self.Waypoints[self.cp.waypointIndex + 1].rev or self.Waypoints[self.cp.waypointIndex + 2].rev))
or (workSpeed ~= nil and workSpeed == 0.5) -- baler in mode 6 , slow down
or isCrawlingToWait
then
refSpeed = math.min(self.cp.speeds.turn,refSpeed); -- we are on the field, go field speed
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif ((self.cp.mode == 2 or self.cp.mode == 3) and isAtStart)
or (workSpeed ~= nil and workSpeed == 1)
or isFinishingWork then
refSpeed = math.min(self.cp.speeds.field,refSpeed);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
local mode7onCourse = true
self.cp.speedDebugStreet = true
if self.cp.mode ~= 7 then
refSpeed = self.cp.speeds.street;
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif self.cp.modeState == 5 then
mode7onCourse = false
end
if self.cp.speeds.useRecordingSpeed and self.Waypoints[self.cp.waypointIndex].speed ~= nil and mode7onCourse then
if self.Waypoints[self.cp.waypointIndex].speed < self.cp.speeds.crawl then
refSpeed = courseplay:getAverageWpSpeed(self , 4)
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
refSpeed = Utils.clamp(refSpeed, self.cp.speeds.crawl, self.Waypoints[self.cp.waypointIndex].speed); --normaly use speed from waypoint, but maximum street speed
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
end;
end;
if self.cp.collidingVehicleId ~= nil then
refSpeed = courseplay:regulateTrafficSpeed(self, refSpeed, allowedToDrive);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
if self.cp.currentTipTrigger ~= nil then
if self.cp.currentTipTrigger.bunkerSilo ~= nil then
refSpeed = Utils.getNoNil(self.cp.speeds.reverse, self.cp.speeds.crawl);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
refSpeed = self.cp.speeds.turn;
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end;
elseif self.cp.isInFilltrigger then
refSpeed = self.cp.speeds.turn;
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
self.cp.isInFilltrigger = false;
end;
--finishing field work- go straight till tool is ready
if isFinishingWork then
lx=0
lz=1
end
--reverse
if self.Waypoints[self.cp.waypointIndex].rev then
lx,lz,fwd = courseplay:goReverse(self,lx,lz)
refSpeed = Utils.getNoNil(self.cp.speeds.reverse, self.cp.speeds.crawl)
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
fwd = true
end
if self.cp.TrafficBrake then
fwd = self.movingDirection == -1;
--lx = 0;
--lz = 1;
end
self.cp.TrafficBrake = false
self.cp.isTrafficBraking = false
if self.cp.mode7GoBackBeforeUnloading then
fwd = false;
lz = lz * -1;
lx = lx * -1;
elseif self.cp.isReverseBackToPoint then
if self.cp.reverseBackToPoint then
local _, _, zDis = worldToLocal(self.cp.DirectionNode, self.cp.reverseBackToPoint.x, self.cp.reverseBackToPoint.y, self.cp.reverseBackToPoint.z);
if zDis < 0 then
fwd = false;
lx = 0
lz = 1
refSpeed = self.cp.speeds.crawl
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
self.cp.reverseBackToPoint = nil;
end;
else
self.cp.isReverseBackToPoint = false;
end;
end
if self.cp.makeHeaps and self.cp.waypointIndex >= self.cp.heapStart - 1 and self.cp.waypointIndex <= self.cp.heapStop and self.cp.totalFillLevel > 0 then
refSpeed = self.cp.speeds.discharge
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
forceTrueSpeed = true
end
if abs(lx) > 0.5 then
refSpeed = min(refSpeed, self.cp.speeds.turn)
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
self.cp.speedDebugLine = speedDebugLine
refSpeed = courseplay:setSpeed(self, refSpeed, forceTrueSpeed)
-- Four wheel drive
if self.cp.hasDriveControl and self.cp.driveControl.hasFourWD then
courseplay:setFourWheelDrive(self, workArea);
end;
-- DISTANCE TO CHANGE WAYPOINT
if self.cp.waypointIndex == 1 or self.cp.waypointIndex == self.cp.numWaypoints - 1 or self.Waypoints[self.cp.waypointIndex].turnStart then
if self.cp.hasSpecializationArticulatedAxis then
distToChange = self.cp.mode == 9 and 2 or 1; -- ArticulatedAxis vehicles
else
distToChange = 0.5;
end;
elseif self.cp.waypointIndex + 1 <= self.cp.numWaypoints then
local beforeReverse = (self.Waypoints[self.cp.waypointIndex + 1].rev and (self.Waypoints[self.cp.waypointIndex].rev == false))
local afterReverse = (not self.Waypoints[self.cp.waypointIndex + 1].rev and self.Waypoints[self.cp.previousWaypointIndex].rev)
if (self.Waypoints[self.cp.waypointIndex].wait or beforeReverse) and self.Waypoints[self.cp.waypointIndex].rev == false then -- or afterReverse or self.cp.waypointIndex == 1
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 2; -- ArticulatedAxis vehicles
else
distToChange = 1;
end;
elseif (self.Waypoints[self.cp.waypointIndex].rev and self.Waypoints[self.cp.waypointIndex].wait) or afterReverse then
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 4; -- ArticulatedAxis vehicles
else
distToChange = 2;
end;
elseif self.Waypoints[self.cp.waypointIndex].rev then
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 4; -- ArticulatedAxis vehicles
else
distToChange = 2; --orig:1
end;
elseif self.cp.mode == 4 or self.cp.mode == 6 or self.cp.mode == 7 then
distToChange = 5;
elseif self.cp.mode == 9 then
distToChange = 4;
else
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 5; -- ArticulatedAxis vehicles
else
distToChange = 2.85; --orig: 5
end;
end;
if beforeReverse then
self.cp.shortestDistToWp = nil
end
else
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 5; -- ArticulatedAxis vehicles stear better with a longer change distance
else
distToChange = 2.85; --orig: 5
end;
end
--distToChange = 2;
-- record shortest distance to the next waypoint
if self.cp.shortestDistToWp == nil or self.cp.shortestDistToWp > self.cp.distanceToTarget then
self.cp.shortestDistToWp = self.cp.distanceToTarget
end
if self.isReverseDriving and not isFinishingWork then
lz = -lz
end
-- if distance grows i must be circling
if self.cp.distanceToTarget > self.cp.shortestDistToWp and self.cp.waypointIndex > 3 and self.cp.distanceToTarget < 15 and self.Waypoints[self.cp.waypointIndex].rev ~= true then
distToChange = self.cp.distanceToTarget + 1
end
if self.cp.distanceToTarget > distToChange or WpUnload or WpLoadEnd or isFinishingWork then
if g_server ~= nil then
local acceleration = 1;
if self.cp.speedBrake then
-- We only need to break sligtly.
acceleration = (self.movingDirection == 1) == fwd and -0.25 or 0.25; -- Setting accelrator to a negative value will break the tractor.
end;
local steeringAngle = self.cp.steeringAngle;
if self.cp.isFourWheelSteering and self.cp.curSpeed > 20 then
-- We are a four wheel steered vehicle, so dampen the steeringAngle when driving fast, since we turn double as fast as normal and will cause oscillating.
steeringAngle = self.cp.steeringAngle * 2;
end;