forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrive.lua
2089 lines (1849 loc) · 88.3 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 recorded course
function courseplay:drive(self, dt)
if self.cp.drivingMode:is(DrivingModeSetting.DRIVING_MODE_AIDRIVER) and self.cp.driver then
self.cp.driver:update(dt)
return
end
--[[ This is FS17 code -- 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:getIsEntered() 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.spec_motorized.isMotorStarted then
--print("stop Order")
courseplay:setEngineState(self, false);
end
elseif not self.spec_motorized.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
--print('Can Use CP mode is false I dont want to drive')
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
cpDebug:drawLine(tx1, ty1, tz1, 1, 0, 0, tx1+(nx*distance), ty1+(ny*distance), tz1+(nz*distance))
cpDebug:drawLine(tx2, ty2, tz2, 1, 0, 0, tx2+(nx*distance), ty2+(ny*distance), tz2+(nz*distance))
end;
--[[Tommi 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"]);
cpDebug:drawLine(sx, sy, sz, 1, 0, 0, sx, sy+3, sz);
local wx, wy, wz = getWorldTranslation(workArea["width"]);
cpDebug:drawLine(wx, wy, wz, 1, 0, 0, wx, wy+3, wz);
local hx, hy, hz = getWorldTranslation(workArea["height"]);
cpDebug:drawLine(hx, hy, hz, 1, 0, 0, hx, hy+3, hz);
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;
--[[ unregister at combine, if there is one
if self.cp.driveUnloadNow == true and self.cp.positionWithCombine ~= nil then
courseplay:unregisterFromCombine(self, self.cp.activeCombine)
end]]
-- === CURRENT VEHICLE POSITION ===
-- cty is used throughout this function as the terrain height.
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;
-- update the pure pursuit controller state
self.cp.ppc:update()
-- === CURRENT WAYPOINT POSITION ===
-- cx, cz only used to get lx, lz (driving direction) unless we are using driveToPoint (which we don't at the moment)
if self.cp.mode ~= 7 then
cx, _, cz = self.cp.ppc:getCurrentWaypointPosition()
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
cpDebug:drawPoint(cx, cty+3, cz, 0, 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
cpDebug:drawPoint(cx, cty+3, cz, 0, 1 , 1);
end;
end;
local isTightTurn
cx, cz, isTightTurn = courseplay.applyTightTurnOffset( self, cx, cz )
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)
cpDebug:drawLine(ctx, cty + 3, ctz, 0, 1, 0, cx, posY + 3, cz);
if self.drawDebugLine then self.drawDebugLine() end
end;
if CpManager.isDeveloper and self.spec_articulatedAxis and self.spec_articulatedAxis.rotMin and courseplay.debugChannels[12] then
local posY = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 300, cz);
local desX, _, desZ = localToWorld(self.cp.directionNode, 0, 0, 5);
cpDebug:drawLine(ctx, cty + 3.5, ctz, 0, 0, 1, desX, cty + 3.5, desZ);
end;
self.cp.distanceToTarget = courseplay:distance(cx, cz, ctx, ctz);
-- from this point onward, ctx and ctz is not used. cty used only for terrain height
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);
-- at this point, we used the current waypoint position and the current vehicle position to calculate
-- lx, lz, that is, the direction we want to drive.
-- world direction of from DirectionNode to waypoint
local nx, ny, nz = localDirectionToWorld(self.cp.directionNode, lx, -0.1, 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 and not courseplay:onAlignmentCourse(self) 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
end;
-- LIGHTS
local combineBeaconOn = self.cp.isCombine and self.cp.totalFillLevelPercent > 80;
local onStreet = (((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 false;
if onStreet then
self.spec_lights.aiLightsTypesMask = courseplay.lights.HEADLIGHT_STREET
else
self.spec_lights.aiLightsTypesMask = courseplay.lights.HEADLIGHT_FULL
end
if self.cp.warningLightsMode == courseplay.lights.WARNING_LIGHTS_NEVER then -- never
if self.beaconLightsActive then
self:setBeaconLightsVisibility(false);
end;
if self.cp.hasHazardLights and self.spec_lights.turnLightState ~= Lights.TURNSIGNAL_OFF then
self:setTurnLightState(Lights.TURNLIGHT_OFF);
end;
else -- on street/always
local beaconOn = onStreet or combineBeaconOn or self.cp.warningLightsMode == courseplay.lights.WARNING_LIGHTS_BEACON_ALWAYS;
if self.beaconLightsActive ~= beaconOn then
self:setBeaconLightsVisibility(beaconOn);
end;
if self.cp.hasHazardLights then
local hazardOn = self.cp.warningLightsMode == courseplay.lights.WARNING_LIGHTS_BEACON_HAZARD_ON_STREET and onStreet and not combineBeaconOn;
if not hazardOn and self.spec_lights.turnLightState ~= Lights.TURNLIGHT_OFF then
self:setTurnLightState(Lights.TURNLIGHT_OFF);
elseif hazardOn and self.spec_lights.turnLightState ~= Lights.TURNLIGHT_HAZARD then
self:setTurnLightState(Lights.TURNLIGHT_HAZARD);
end;
end;
end;
-- the tipper that is currently loaded/unloaded
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
-- this call never changes lx or lz
allowedToDrive,lx,lz = courseplay:refillWorkTools(self, self.cp.refillUntilPct, allowedToDrive, lx, lz);
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 ~= 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 == 8 then
-- this call does not change lx or lz
allowedToDrive, lx, lz = courseplay:handleMode8(self, false, true, allowedToDrive, lx, lz, dt);
elseif self.cp.mode == 10 then
self.cp.mode10.newApproach = true
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.driveUnloadNow)) 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
-- 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
allowedToDrive = courseplay:handle_mode2(self, dt);
return;
elseif (self.cp.mode == 2 or self.cp.mode == 3) and self.cp.waypointIndex < 2 then
isBypassing = true
-- this changes the direction by changing lx/lz
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)) and not self.cp.realisticDriving then
isBypassing = true
-- this changes the direction by changing lx/lz
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
-- lx, lz not changed by this call
allowedToDrive,lx,lz = courseplay:refillWorkTools(self, self.cp.refillUntilPct, allowedToDrive, lx, lz);
end
end;
-- MODE 6-7: HEAP UNLOADING
elseif self.cp.mode == 6 and self.cp.makeHeaps then
courseplay:handleHeapUnloading(self);
-- MODE 8: REFILL LIQUID MANURE TRANSPORT
elseif self.cp.mode == 8 then
-- lx, lz not changed by this call
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
-- DO NOT DELETE fuelFillLevel is gone. This variable may be a replacment
allowedToDrive = courseplay:checkFuel(self,lx,lz,allowedToDrive)
-- 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.ppc:reachedLastWaypoint() 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;
end;
-- ### NON-WAITING POINTS END
--------------------------------------------------
-- MODE 3
-- Support for mode3 handling multiple sugar cane trailers
if self.cp.isMode3Unloading then
allowedToDrive = courseplay:handleMode3(self, allowedToDrive, dt);
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 and not self.cp.fillTrigger then
courseplay:doTriggerRaycasts(self, 'specialTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
end;
if self.Waypoints[self.cp.waypointIndex].isConnectingTrack then
courseplay:raiseImplements(self)
end;
if self.cp.abortWork then
-- If we are navigating pathfinding then don't let drive handle driving and use the below function
if self.cp.isNavigatingPathfinding == true then
--inTraffic back to false this, clears the inTraffic Message
self.cp.inTraffic = false
courseplay:navigatePathToUnloadCourse(self, dt, allowedToDrive)
return
end
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 self.Waypoints[self.cp.waypointIndex].isConnectingTrack then
courseplay:raiseImplements(self)
end;
if breakCode then
return
end
if self.cp.abortWork then
-- If we are navigating pathfinding then don't let drive handle driving and use the below function
if self.cp.isNavigatingPathfinding == true then
--inTraffic back to false this, clears the inTraffic Message
self.cp.inTraffic = false
courseplay:navigatePathToUnloadCourse(self, dt, allowedToDrive)
return
end;
end
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, showCover);
elseif self.cp.tipperHasCover then
courseplay:openCloseCover(self, false);
end;
-- CHECK TRAFFIC
-- Ryan missing gcurrent_mission variable in this function 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:setDriveUnloadNow(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 then
allowedToDrive = true;
moveForwards = self.movingDirection == 1;
end;
--print('broken 779')
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 ) and not courseplay:onAlignmentCourse( self )
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, math.min( #self.Waypoints, 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 = math.max (courseplay:round(self.lastSpeedReal*3600)/3.6,0.1) --m/s
--local speed = self:getCruiseControlSpeed()/3.6 --m/s
local turnTime = math.floor(self.cp.calculatedTurnTime or 5)
if self.cp.course.hasChangedTheWaypointIndex then
self.cp.course.hasChangedTheWaypointIndex = nil
self:setCpVar('timeRemaining',distance/speed + turnTime,courseplay.isClient)
end
elseif not isFieldWorking then
self:setCpVar('timeRemaining',nil,courseplay.isClient)
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 (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 (self.cp.mode == 3 and self.cp.isMode3Unloading == true)-- Mode 3 SugarCane Trailer
or isCrawlingToWait
or isTightTurn
then
refSpeed = math.min(self.cp.speeds.turn,refSpeed); -- we are on the field, go field speed
--debug nil 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);
--debug nil 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;
--debug nil 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 = MathUtil.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;
allowedToDrive = courseplay:checkTraffic(self, true, 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 then
allowedToDrive = true;
moveForwards = self.movingDirection == 1;
end;
--print('broken 779')
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;
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);
if courseplay.debugChannels[14] and g_updateLoopIndex % 100 == 0 then
courseplay:debug(string.format("%s: refSpeed: %.2f ", nameNum(self),refSpeed), 14);
end
--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.fillTrigger ~= nil 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
local isReverseActive
lx,lz,fwd, isReverseActive = courseplay:goReverse(self,lx,lz)
-- let the PPC know if goReverse is the driving.
self.cp.ppc:setReverseActive(isReverseActive)
refSpeed = Utils.getNoNil(self.cp.speeds.reverse, self.cp.speeds.crawl)
--speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
-- goReverse is not driving
self.cp.ppc:setReverseActive(false)
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;
local beforeReverse, afterReverse
-- DISTANCE TO CHANGE WAYPOINT
if ( self.cp.waypointIndex == 1 and not self.cp.alignment.justFinished ) or self.cp.waypointIndex == self.cp.numWaypoints - 1 or self.Waypoints[self.cp.waypointIndex].turnStart then
if self.spec_articulatedAxis and self.spec_articulatedAxis.rotMin then
distToChange = 1; -- ArticulatedAxis vehicles
else
distToChange = 0.5;
end;
elseif self.cp.waypointIndex + 1 <= self.cp.numWaypoints then
beforeReverse = (self.Waypoints[self.cp.waypointIndex + 1].rev and (self.Waypoints[self.cp.waypointIndex].rev == false))
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.spec_articulatedAxis and self.spec_articulatedAxis.rotMin 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.spec_articulatedAxis and self.spec_articulatedAxis.rotMin then
distToChange = 4; -- ArticulatedAxis vehicles
else
distToChange = 2;
end;
elseif self.Waypoints[self.cp.waypointIndex].rev then
if self.spec_articulatedAxis and self.spec_articulatedAxis.rotMin then
distToChange = 4; -- ArticulatedAxis vehicles
else
distToChange = 2; --orig:1
end;
elseif self.cp.mode == 4 or self.cp.mode == 6 then
distToChange = 5
else
if self.spec_articulatedAxis and self.spec_articulatedAxis.rotMin then
distToChange = 5; -- ArticulatedAxis vehicles
else
distToChange = 2.85; --orig: 5
end;
end;
if beforeReverse then
self.cp.shortestDistToWp = nil
end
else
if self.spec_articulatedAxis and self.spec_articulatedAxis.rotMin 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
local shortestDistToWp = Utils.getNoNil( self.cp.shortestDistToWp, -1 )
self.cp.shortestDistToWp = self.cp.distanceToTarget
-- courseplay:debug( string.format( "shortestDistToWp %.3f, distanceToTarget %.3f", shortestDistToWp, self.cp.distanceToTarget ), 12 )
end
if self.isReverseDriving and not isFinishingWork then
lz = -lz
end
-- if distance grows i must be circling. Allow for half a meter to tolerate slight calculation errors, especially at
-- tight turns where we constantly recalculate the target
if self.cp.distanceToTarget > ( self.cp.shortestDistToWp + 0.5 ) and self.cp.waypointIndex > 3 and self.cp.distanceToTarget < 15 and self.Waypoints[self.cp.waypointIndex].rev ~= true then
distToChange = self.cp.distanceToTarget + 1
--courseplay.debugVehicle( 12, self, "circling? wp=%d distToChange %.1f, shortestDistToWp %.3f, distanceToTarget %.3f",
-- self.cp.waypointIndex, distToChange, self.cp.shortestDistToWp, self.cp.distanceToTarget )
end
if not self.cp.ppc:shouldChangeWaypoint(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 brake slightly.
local mrAccelrator = .25
if self.cp.useProgessiveBraking then
mrAccelrator = self.cp.mrAccelrator -- We need to actually break the tractor even more when using MR due to no engine break
end
acceleration = (self.movingDirection == 1) == fwd and -mrAccelrator or mrAccelrator; -- 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;
-- Using false to disable the driveToPoint. This could be made into an setting option later on.
local useDriveToPoint = false --and self.cp.mode == 1 or self.cp.mode == 5 or (self.cp.waypointIndex > 4 and (self.cp.mode == 2 or self.cp.mode == 3));
local disableLongCollisionCheck = workArea;
if self.Waypoints[self.cp.waypointIndex].rev or not useDriveToPoint then
if self.Waypoints[self.cp.waypointIndex].rev then
if self.cp.revSteeringAngle then