forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
drive.lua
1379 lines (1215 loc) · 58 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;
-- drives recored course
function courseplay:drive(self, dt)
if not courseplay:getCanUseCpMode(self) then
return;
end;
-- debug for workAreas
if courseplay.debugChannels[6] then
local tx1, ty1, tz1 = localToWorld(self.cp.DirectionNode,3,1,self.cp.aiFrontMarker)
local tx2, ty2, tz2 = localToWorld(self.cp.DirectionNode,3,1,self.cp.backMarkerOffset)
local nx, ny, nz = localDirectionToWorld(self.cp.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
local refSpeed = huge
local speedDebugLine = "refSpeed"
self.cp.speedDebugLine = nil
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)
self.cp.tipperFillLevel, self.cp.tipperCapacity = self:getAttachedTrailersFillLevelAndCapacity();
if self.cp.tipperFillLevel == nil then self.cp.tipperFillLevel = 0; end;
if self.cp.tipperCapacity == nil or self.cp.tipperCapacity == 0 then self.cp.tipperCapacity = 0.00001; end;
self.cp.tipperFillLevelPct = self.cp.tipperFillLevel * 100 / self.cp.tipperCapacity;
-- 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
-- Turn on sound / control lights
if not self.isControlled then
self:setLightsVisibility(CpManager.lightsNeeded);
end;
-- current position
local ctx, cty, ctz = getWorldTranslation(self.cp.DirectionNode);
if self.recordnumber > self.maxnumber then
courseplay:debug(string.format("drive %d: %s: self.recordnumber (%s) > self.maxnumber (%s)", debug.getinfo(1).currentline, nameNum(self), tostring(self.recordnumber), tostring(self.maxnumber)), 12); --this should never happen
courseplay:setRecordNumber(self, self.maxnumber);
end;
if self.cp.mode ~= 7 or (self.cp.mode == 7 and self.cp.modeState ~= 5) then
cx, cz = self.Waypoints[self.recordnumber].cx, self.Waypoints[self.recordnumber].cz
end
if courseplay.debugChannels[12] and self.cp.isTurning == nil then
drawDebugPoint(cx, cty+3, cz, 1, 0 , 1, 1);
end;
-- 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;
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.recordnumber].turn ~= nil then
self.cp.isTurning = self.Waypoints[self.recordnumber].turn
end
if self.cp.abortWork ~= nil and self.cp.tipperFillLevelPct == 0 then
self.cp.isTurning = nil
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 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;
-- WARNING LIGHTS
if self.cp.warningLightsMode == courseplay.WARNING_LIGHTS_NEVER then -- never
if self.beaconLightsActive then
self:setBeaconLightsVisibility(false);
end;
if self.cp.hasHazardLights and self.turnSignalState ~= Vehicle.TURNSIGNAL_OFF then
self:setTurnSignalState(Vehicle.TURNSIGNAL_OFF);
end;
else -- on street/always
local combineBeaconOn = self.cp.isCombine and (self.fillLevel / self.capacity) > 0.8;
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.recordnumber > 2)
or ((self.cp.mode == 4 or self.cp.mode == 6) and self.recordnumber > self.cp.stopWork)
or combineBeacon;
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.turnSignalState ~= Vehicle.TURNSIGNAL_OFF then
self:setTurnSignalState(Vehicle.TURNSIGNAL_OFF);
elseif hazardOn and self.turnSignalState ~= Vehicle.TURNSIGNAL_HAZARD then
self:setTurnSignalState(Vehicle.TURNSIGNAL_HAZARD);
end;
end;
end;
-- the tipper that is currently loaded/unloaded
local activeTipper;
local isBypassing = false
local isCrawlingToWait = false
-- ### WAITING POINTS - START
if self.Waypoints[self.cp.lastRecordnumber].wait and self.cp.wait then
-- 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 == 3 and self.cp.workToolAttached then
courseplay:handleMode3(self, self.cp.tipperFillLevelPct, allowedToDrive, dt);
elseif self.cp.mode == 4 then
local drive_on = false
if self.cp.lastRecordnumber == self.cp.startWork then
courseplay:setVehicleWait(self, false);
elseif self.cp.lastRecordnumber == self.cp.stopWork and self.cp.abortWork ~= nil then
courseplay:setVehicleWait(self, false);
elseif self.cp.waitPoints[3] and self.cp.lastRecordnumber == self.cp.waitPoints[3] then
local isInWorkArea = self.recordnumber > self.cp.startWork and self.recordnumber <= 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:refillSprayer(self, self.cp.tipperFillLevelPct, 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.tipperFillLevelPct == self.cp.prevFillLevelPct and self.cp.tipperFillLevelPct >= self.cp.refillUntilPct then
drive_on = true
end
self.cp.prevFillLevelPct = self.cp.tipperFillLevelPct
courseplay:setCustomTimer(self, "fillLevelChange", 7);
end
if self.cp.tipperFillLevelPct >= self.cp.refillUntilPct or drive_on then
courseplay:setVehicleWait(self, false);
end
courseplay:setInfoText(self, string.format(courseplay:loc("COURSEPLAY_LOADING_AMOUNT"), self.cp.tipperFillLevel, self.cp.tipperCapacity));
end
elseif self.cp.mode == 6 then
if self.cp.lastRecordnumber == self.cp.startWork then
courseplay:setVehicleWait(self, false);
elseif self.cp.lastRecordnumber == self.cp.stopWork and self.cp.abortWork ~= nil then
courseplay:setVehicleWait(self, false);
elseif self.cp.lastRecordnumber ~= self.cp.startWork and self.cp.lastRecordnumber ~= self.cp.stopWork then
CpManager:setGlobalInfoText(self, 'UNLOADING_BALE');
if self.cp.tipperFillLevelPct == 0 or drive_on then
courseplay:setVehicleWait(self, false);
end;
end;
elseif self.cp.mode == 7 then
if self.cp.lastRecordnumber == self.cp.startWork then
if self.fillLevel > 0 then
self:setPipeState(2)
CpManager:setGlobalInfoText(self, 'OVERLOADING_POINT');
else
courseplay:setVehicleWait(self, false);
self.cp.isUnloaded = true
end
end
elseif self.cp.mode == 8 then
CpManager:setGlobalInfoText(self, 'OVERLOADING_POINT');
if self.cp.workToolAttached then
-- drive on if tipperFillLevelPct doesn't change and fill level is < 100-self.cp.followAtFillLevel
courseplay:handle_mode8(self)
local drive_on = false
if courseplay:timerIsThrough(self, "fillLevelChange") or self.cp.prevFillLevelPct == nil then
if self.cp.prevFillLevelPct ~= nil and self.cp.tipperFillLevelPct == self.cp.prevFillLevelPct and self.cp.tipperFillLevelPct < self.cp.followAtFillLevel then
drive_on = true
end
self.cp.prevFillLevelPct = self.cp.tipperFillLevelPct
courseplay:setCustomTimer(self, "fillLevelChange", 7);
end
if self.cp.tipperFillLevelPct == 0 or drive_on then
courseplay:setVehicleWait(self, false);
self.cp.prevFillLevelPct = nil
self.cp.isUnloaded = true
end
end
elseif self.cp.mode == 9 then
courseplay:setVehicleWait(self, false);
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
local _,_,zDist = worldToLocal(self.cp.DirectionNode, self.Waypoints[self.cp.lastRecordnumber].cx, cty, self.Waypoints[self.cp.lastRecordnumber].cz);
if zDist < 1 then -- don't stop immediately when hitting the waitPoints recordnumber, but rather wait until we're close enough (1m)
allowedToDrive = false;
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.tipperFillLevel ~= nil and self.cp.tipRefOffset ~= nil and self.cp.workToolAttached then
if self.cp.currentTipTrigger == nil and self.cp.tipperFillLevel > 0 and self.recordnumber > 2 and self.recordnumber < self.maxnumber and not self.Waypoints[self.recordnumber].rev then
courseplay:doTriggerRaycasts(self, 'tipTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
end;
allowedToDrive = courseplay:handle_mode1(self, allowedToDrive);
end;
-- COMBI MODE / BYPASSING
if (((self.cp.mode == 2 or self.cp.mode == 3) and self.recordnumber < 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.recordnumber < 3 then
isBypassing = true
lx, lz = courseplay:isTheWayToTargetFree(self,lx, lz)
elseif self.cp.mode == 6 and self.cp.hasBaleLoader and (self.recordnumber == self.cp.stopWork - 4 or (self.cp.abortWork ~= nil and self.recordnumber == self.cp.abortWork)) then
isBypassing = true
lx, lz = courseplay:isTheWayToTargetFree(self,lx, lz)
elseif self.cp.mode ~= 7 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.recordnumber >= 2 and self.cp.modeState == 0 then
courseplay:handleMode3(self, self.cp.tipperFillLevelPct, allowedToDrive, dt);
end;
-- MODE 4: REFILL SPRAYER or SEEDER
if self.cp.mode == 4 then
if self.cp.workToolAttached and self.cp.startWork ~= nil and self.cp.stopWork ~= nil then
local isInWorkArea = self.recordnumber > self.cp.startWork and self.recordnumber <= self.cp.stopWork;
if self.cp.workTools ~= nil and not isInWorkArea then
allowedToDrive,lx,lz = courseplay:refillSprayer(self, self.cp.tipperFillLevelPct, self.cp.refillUntilPct, allowedToDrive, lx, lz, dt);
end
end;
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;
-- MODE 8: REFILL LIQUID MANURE TRANSPORT
if self.cp.mode == 8 then
courseplay:doTriggerRaycasts(self, 'specialTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
if self.cp.workToolAttached then
if self.cp.workTools ~= nil then
allowedToDrive,lx,lz = courseplay:refillSprayer(self, self.cp.tipperFillLevelPct, self.cp.refillUntilPct, allowedToDrive, lx, lz, dt);
end;
end;
end;
--FUEL LEVEL + REFILLING
if self.fuelCapacity > 0 then
local currentFuelPercentage = (self.fuelFillLevel / self.fuelCapacity + 0.0001) * 100;
if currentFuelPercentage < 5 then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'FUEL_MUST');
elseif currentFuelPercentage < 20 and not self.isFuelFilling 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;
CpManager:setGlobalInfoText(self, 'FUEL_SHOULD');
if self.fuelFillTriggers[1] then
allowedToDrive = false;
self:setIsFuelFilling(true, self.fuelFillTriggers[1].isEnabled, false);
end;
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 AND END OR TRIGGER
if self.cp.stopAtEnd and (self.recordnumber == self.maxnumber or self.cp.currentTipTrigger ~= nil or self.cp.fillTrigger ~= nil) then
allowedToDrive = false;
CpManager:setGlobalInfoText(self, 'END_POINT');
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, self.cp.tipperFillLevelPct, refSpeed);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if not workArea and self.cp.tipperFillLevelPct < 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, activeTipper, isFinishingWork,refSpeed = courseplay:handle_mode6(self, allowedToDrive, workSpeed, self.cp.tipperFillLevelPct, lx, lz,refSpeed);
speedDebugLine = ("drive("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if not workArea and self.cp.currentTipTrigger == nil and self.cp.tipperFillLevel and self.cp.tipperFillLevel > 0 and self.capacity == nil and self.cp.tipRefOffset ~= nil and not self.Waypoints[self.recordnumber].rev then
courseplay:doTriggerRaycasts(self, 'tipTrigger', 'fwd', true, tx, ty, tz, nx, ny, nz);
end;
-- MODE 9
elseif self.cp.mode == 9 then
allowedToDrive = courseplay:handle_mode9(self, self.cp.tipperFillLevelPct, allowedToDrive, dt);
end;
self.cp.inTraffic = false;
-- AI TRACTOR DIRECTION
local dx,_,dz = localDirectionToWorld(self.cp.DirectionNode, 0, 0, 1);
local length = Utils.vector2Length(dx,dz);
if self.cp.turnStage == 0 then
self.aiTractorDirectionX = dx/length;
self.aiTractorDirectionZ = dz/length;
end
-- 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 == 5 or self.cp.mode == 6) then
local showCover = false;
if self.cp.mode ~= 6 then
local minCoverWaypoint = self.cp.mode == 1 and 4 or 3;
showCover = self.recordnumber >= minCoverWaypoint and self.recordnumber < self.maxnumber and self.cp.currentTipTrigger == nil and self.cp.trailerFillDistance == nil;
else
showCover = not workArea and self.cp.currentTipTrigger == nil;
end;
courseplay:openCloseCover(self, dt, showCover, self.cp.currentTipTrigger ~= nil);
end;
-- CHECK TRAFFIC
allowedToDrive = courseplay:checkTraffic(self, true, allowedToDrive)
if self.cp.waitForTurnTime > self.timer 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.recordnumber >=3 then
WpUnload = self.recordnumber == 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.recordnumber >=3 then
WpLoadEnd = self.recordnumber == 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.tipperFillLevelPct == 0 then
allowedToDrive = false
CpManager:setGlobalInfoText(self, 'WORK_END');
else
courseplay:setIsLoaded(self, true);
courseplay:setRecordNumber(self, i + 2);
end
end
end
-- MODE 9 END
-- 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;
elseif self.cp.curSpeed < 0.2 then
-- ## The infamous "SUCK IT, GIANTS" fix, a.k.a "chain that fucker down, it ain't goin' nowhere!"
courseplay:getAndSetFixedWorldPosition(self);
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;
if self.cp.isTurning ~= nil then
courseplay:turn(self, dt);
self.cp.TrafficBrake = false
return
end
self.cp.checkMarkers = false
--SPEED SETTING
local isAtEnd = self.recordnumber > self.maxnumber - 3;
local isAtStart = self.recordnumber < 3;
if ((self.cp.mode == 1 or self.cp.mode == 5 or self.cp.mode == 8) and (isAtStart or isAtEnd))
or ((self.cp.mode == 2 or self.cp.mode == 3) and isAtEnd)
or (self.cp.mode == 9 and self.recordnumber > self.cp.shovelFillStartPoint and self.recordnumber <= self.cp.shovelFillEndPoint)
or (not workArea and self.cp.wait and ((isAtEnd and self.Waypoints[self.recordnumber].wait) or courseplay:waypointsHaveAttr(self, self.recordnumber, 0, 2, "wait", true, false)))
or (isAtEnd and self.Waypoints[self.recordnumber].rev)
or (not isAtEnd and (self.Waypoints[self.recordnumber].rev or self.Waypoints[self.recordnumber + 1].rev or self.Waypoints[self.recordnumber + 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) 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.recordnumber].speed ~= nil and mode7onCourse then
if self.Waypoints[self.recordnumber].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.recordnumber].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.unload, 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.recordnumber].rev then
lx,lz,fwd = courseplay:goReverse(self,lx,lz)
refSpeed = Utils.getNoNil(self.cp.speeds.unload, 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 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
courseplay:setSpeed(self, refSpeed)
-- Four wheel drive
if self.cp.hasDriveControl and self.cp.driveControl.hasFourWD then
courseplay:setFourWheelDrive(self, workArea);
end;
-- DISTANCE TO CHANGE WAYPOINT
if self.recordnumber == 1 or self.recordnumber == self.maxnumber - 1 or self.Waypoints[self.recordnumber].turn then
if self.cp.hasSpecializationArticulatedAxis then
distToChange = self.cp.mode == 9 and 2 or 1; -- ArticulatedAxis vehicles
else
distToChange = 0.5;
end;
elseif self.recordnumber + 1 <= self.maxnumber then
local beforeReverse = (self.Waypoints[self.recordnumber + 1].rev and (self.Waypoints[self.recordnumber].rev == false))
local afterReverse = (not self.Waypoints[self.recordnumber + 1].rev and self.Waypoints[self.cp.lastRecordnumber].rev)
if (self.Waypoints[self.recordnumber].wait or beforeReverse) and self.Waypoints[self.recordnumber].rev == false then -- or afterReverse or self.recordnumber == 1
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 2; -- ArticulatedAxis vehicles
else
distToChange = 1;
end;
elseif (self.Waypoints[self.recordnumber].rev and self.Waypoints[self.recordnumber].wait) or afterReverse then
if self.cp.hasSpecializationArticulatedAxis then
distToChange = 4; -- ArticulatedAxis vehicles
else
distToChange = 2;
end;
elseif self.Waypoints[self.recordnumber].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;
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
-- 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 beforeReverse then
self.cp.shortestDistToWp = nil
end
if self.invertedDrivingDirection then
lx = -lx
lz = -lz
end
-- if distance grows i must be circling
if self.cp.distanceToTarget > self.cp.shortestDistToWp and self.recordnumber > 3 and self.cp.distanceToTarget < 15 and self.Waypoints[self.recordnumber].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;
--self,dt,steeringAngleLimit,acceleration,slowAcceleration,slowAngleLimit,allowedToDrive,moveForwards,lx,lz,maxSpeed,slowDownFactor,angle
--AIVehicleUtil.driveInDirection(dt,25,acceleration,0.5,20,true,true,-0.028702223698223,0.99958800630799,22,1,nil)
AIVehicleUtil.driveInDirection(self, dt, self.cp.steeringAngle, acceleration, 0.5, 20, true, fwd, lx, lz, refSpeed, 1);
if not isBypassing then
courseplay:setTrafficCollision(self, lx, lz, workArea)
end
end
else
-- reset distance to waypoint
self.cp.shortestDistToWp = nil
if self.recordnumber < self.maxnumber then -- = New
if not self.cp.wait then
courseplay:setVehicleWait(self, true);
end
if self.cp.mode == 7 and self.cp.modeState == 5 then
else
courseplay:setRecordNumber(self, self.recordnumber + 1);
end
else -- last waypoint: reset some variables
if (self.cp.mode == 4 or self.cp.mode == 6) and not self.cp.hasUnloadingRefillingCourse then
else
courseplay:setRecordNumber(self, 1);
end
self.cp.isUnloaded = false
courseplay:setStopAtEnd(self, false);
courseplay:setIsLoaded(self, false);
courseplay:setIsRecording(self, false);
self.cp.canDrive = true
end
end
end
-- END drive();
function courseplay:setTrafficCollision(vehicle, lx, lz, workArea) --!!!
--local goForRaycast = vehicle.cp.mode == 1 or (vehicle.cp.mode == 3 and vehicle.recordnumber > 3) or vehicle.cp.mode == 5 or vehicle.cp.mode == 8 or ((vehicle.cp.mode == 4 or vehicle.cp.mode == 6) and vehicle.recordnumber > vehicle.cp.stopWork) or (vehicle.cp.mode == 2 and vehicle.recordnumber > 3)
--print("lx: "..tostring(lx).." distance: "..tostring(distance))
local maxlx = 0.5; --sin(maxAngle); --sin30° old was : 0.7071067 sin 45°
local colDirX = lx;
local colDirZ = lz;
if colDirX > maxlx then
colDirX = maxlx;
elseif colDirX < -maxlx then
colDirX = -maxlx;
end;
if colDirZ < -0.4 then
colDirZ = 0.4;
end;
--courseplay:debug(string.format("colDirX: %f colDirZ %f ",colDirX,colDirZ ), 3)
if vehicle.cp.trafficCollisionTriggers[1] ~= nil then
AIVehicleUtil.setCollisionDirection(vehicle.cp.DirectionNode, vehicle.cp.trafficCollisionTriggers[1], colDirX, colDirZ);
local recordNumber = vehicle.recordnumber
if vehicle.cp.collidingVehicleId == nil then
for i=2,vehicle.cp.numTrafficCollisionTriggers do
if workArea or recordNumber + i > vehicle.maxnumber or recordNumber < 2 then
AIVehicleUtil.setCollisionDirection(vehicle.cp.trafficCollisionTriggers[i-1], vehicle.cp.trafficCollisionTriggers[i], 0, -1);
else
local nodeX,nodeY,nodeZ = getWorldTranslation(vehicle.cp.trafficCollisionTriggers[i]);
local nodeDirX,nodeDirY,nodeDirZ,distance = courseplay:getWorldDirection(nodeX,nodeY,nodeZ, vehicle.Waypoints[recordNumber+i].cx,nodeY,vehicle.Waypoints[recordNumber+i].cz);
if distance < 5.5 and recordNumber + i +1 <= vehicle.maxnumber then
nodeDirX,nodeDirY,nodeDirZ,distance = courseplay:getWorldDirection(nodeX,nodeY,nodeZ, vehicle.Waypoints[recordNumber+i+1].cx,nodeY,vehicle.Waypoints[recordNumber+i+1].cz);
end;
nodeDirX,nodeDirY,nodeDirZ = worldDirectionToLocal(vehicle.cp.trafficCollisionTriggers[i-1], nodeDirX,nodeDirY,nodeDirZ);
AIVehicleUtil.setCollisionDirection(vehicle.cp.trafficCollisionTriggers[i-1], vehicle.cp.trafficCollisionTriggers[i], nodeDirX, nodeDirZ);
end;
end
end
end;
end;
function courseplay:checkTraffic(vehicle, displayWarnings, allowedToDrive)
local ahead = false
local inQuery = false
local collisionVehicle = g_currentMission.nodeToVehicle[vehicle.cp.collidingVehicleId]
if collisionVehicle ~= nil and not (vehicle.cp.mode == 9 and (collisionVehicle.allowFillFromAir or (collisionVehicle.cp and collisionVehicle.cp.mode9TrafficIgnoreVehicle))) then
local vx, vy, vz = getWorldTranslation(vehicle.cp.collidingVehicleId);
local tx, ty, tz = worldToLocal(vehicle.aiTrafficCollisionTrigger, vx, vy, vz);
local x, y, z = getWorldTranslation(vehicle.cp.DirectionNode);
local halfLength = (collisionVehicle.sizeLength or 5) * 0.5;
local x1,z1 = AIVehicleUtil.getDriveDirection(vehicle.cp.collidingVehicleId, x, y, z);
if z1 > -0.9 then -- tractor in front of vehicle face2face or beside < 4 o'clock
ahead = true
end;
local _,transY,_ = getTranslation(vehicle.cp.collidingVehicleId);
if transY < 0 or abs(tx) > 5 and collisionVehicle.rootNode ~= nil and not vehicle.cp.collidingObjects.all[vehicle.cp.collidingVehicleId] then
courseplay:debug(('%s: checkTraffic:\tcall deleteCollisionVehicle(), transY= %s'):format(nameNum(vehicle),tostring(transY)), 3);
courseplay:deleteCollisionVehicle(vehicle);
return allowedToDrive;
end;
if collisionVehicle.lastSpeedReal == nil or collisionVehicle.lastSpeedReal*3600 < 5 or ahead then
-- courseplay:debug(('%s: checkTraffic:\tcall distance=%.2f'):format(nameNum(vehicle), tz-halfLength), 3);
if tz <= halfLength + 4 then --TODO: abs(tz) ?
allowedToDrive = false;
vehicle.cp.inTraffic = true;
courseplay:debug(('%s: checkTraffic:\tstop'):format(nameNum(vehicle)), 3);
elseif vehicle.cp.curSpeed > 10 then
-- courseplay:debug(('%s: checkTraffic:\tbrake'):format(nameNum(vehicle)), 3);
allowedToDrive = false;
else
-- courseplay:debug(('%s: checkTraffic:\tdo nothing - go, but set "vehicle.cp.isTrafficBraking"'):format(nameNum(vehicle)), 3);
vehicle.cp.isTrafficBraking = true;
end;
end;
local attacher
if collisionVehicle.getRootAttacherVehicle then
attacher = collisionVehicle:getRootAttacherVehicle()
inQuery = vehicle.cp.mode == 1 and vehicle.recordnumber == 1 and attacher.cp ~= nil and attacher.cp.isDriving and attacher.cp.mode == 1 and attacher.recordnumber == 2
end
end;
if displayWarnings and vehicle.cp.inTraffic and not inQuery then
CpManager:setGlobalInfoText(vehicle, 'TRAFFIC');
end;
return allowedToDrive;
end
function courseplay:setSpeed(vehicle, refSpeed)
local newSpeed = math.max(refSpeed,3)
if vehicle.cruiseControl.state == Drivable.CRUISECONTROL_STATE_OFF then
vehicle:setCruiseControlState(Drivable.CRUISECONTROL_STATE_ACTIVE)
end
vehicle:setCruiseControlMaxSpeed(newSpeed)
courseplay:handleSlipping(vehicle, refSpeed);
local deltaMinus = vehicle.cp.curSpeed - refSpeed;
local tolerance = 2.5;
if vehicle.cp.currentTipTrigger and vehicle.cp.currentTipTrigger.bunkerSilo then
tolerance = 1;
end;
if deltaMinus > tolerance then
vehicle.cp.speedBrake = true;
else
vehicle.cp.speedBrake = false;
end;
end
function courseplay:openCloseCover(vehicle, dt, showCover, isAtTipTrigger)
for i,twc in pairs(vehicle.cp.tippersWithCovers) do
local tIdx, coverType, showCoverWhenTipping, coverItems = twc.tipperIndex, twc.coverType, twc.showCoverWhenTipping, twc.coverItems;
local tipper = vehicle.cp.workTools[tIdx];
-- default Giants trailers
if coverType == 'defaultGiants' then
if tipper.isCoverOpen == showCover then
tipper:setCoverState(not showCover);
end;
-- Example: for mods trailer that don't use the default cover specialization
else--if coverType == 'CoverVehicle' then
--for _,ci in pairs(coverItems) do
-- if getVisibility(ci) ~= showCover then
-- setVisibility(ci, showCover);
-- end;
--end;
--if showCoverWhenTipping and isAtTipTrigger and not showCover then
--
--else
-- tipper:setPlane(not showCover);
--end;
end;
end; --END for i,tipperWithCover in vehicle.cp.tippersWithCovers
end;
function courseplay:refillSprayer(vehicle, fillLevelPct, driveOn, allowedToDrive, lx, lz, dt)
for i=1, vehicle.cp.numWorkTools do
local activeTool = vehicle.cp.workTools[i];
local isSpecialSprayer = false
local fillTrigger;
isSpecialSprayer, allowedToDrive, lx, lz = courseplay:handleSpecialSprayer(vehicle, activeTool, fillLevelPct, driveOn, allowedToDrive, lx, lz, dt, 'pull');
if isSpecialSprayer then
return allowedToDrive,lx,lz
end;
-- SPRAYER
if courseplay:isSprayer(activeTool) or activeTool.cp.isLiquidManureOverloader then
-- print(('\tworkTool %d (%q)'):format(i, nameNum(activeTool)));
if vehicle.cp.fillTrigger ~= nil then
local trigger = courseplay.triggers.all[vehicle.cp.fillTrigger];
if (trigger.isSprayerFillTrigger or trigger.isLiquidManureFillTrigger) and courseplay:fillTypesMatch(trigger, activeTool) then
--print('\t\tslow down, it\'s a sprayerFillTrigger');
vehicle.cp.isInFilltrigger = true
end
end;
local activeToolFillLevel;
if activeTool.fillLevel ~= nil and activeTool.capacity ~= nil then
activeToolFillLevel = (activeTool.fillLevel / activeTool.capacity) * 100;
end;
--vehicle.cp.lastMode8UnloadTriggerId
-- check for fillTrigger
if fillTrigger == nil and activeTool.fillTriggers then
local activeToolFillTrigger = activeTool.fillTriggers[1];
if activeToolFillTrigger ~= nil and (activeToolFillTrigger.isSprayerFillTrigger or activeToolFillTrigger.isLiquidManureFillTrigger) then
fillTrigger = activeToolFillTrigger;
vehicle.cp.fillTrigger = nil;
end;
end;
-- check for UPK fillTrigger
if fillTrigger == nil and activeTool.upkTrigger then
local activeToolFillTrigger = activeTool.upkTrigger[1];
if activeToolFillTrigger ~= nil and (activeToolFillTrigger.isSprayerFillTrigger or activeToolFillTrigger.isLiquidManureFillTrigger) then
fillTrigger = activeToolFillTrigger;
vehicle.cp.fillTrigger = nil;
end;
end;
local fillTypesMatch = courseplay:fillTypesMatch(fillTrigger, activeTool);
local canRefill = (activeToolFillLevel ~= nil and activeToolFillLevel < driveOn) and fillTypesMatch;
if vehicle.cp.mode == 8 then
canRefill = canRefill and not courseplay:waypointsHaveAttr(vehicle, vehicle.recordnumber, -2, 2, 'wait', true, false);
if (activeTool.isSpreaderInRange ~= nil and activeTool.isSpreaderInRange.manureTriggerc ~= nil)
--normal fill triggers
or (fillTrigger ~= nil and fillTrigger.triggerId ~= nil and vehicle.cp.lastMode8UnloadTriggerId ~= nil and fillTrigger.triggerId == vehicle.cp.lastMode8UnloadTriggerId)
-- manureLager fill trigger
or (fillTrigger ~= nil and fillTrigger.manureTrigger ~= nil and vehicle.cp.lastMode8UnloadTriggerId ~= nil and fillTrigger.manureTrigger == vehicle.cp.lastMode8UnloadTriggerId)
then
canRefill = false;
end;
end;
-- print(('activeToolFillLevel=%s, driveOn=%s, fillTrigger=%s, fillTypesMatch=%s, canRefill=%s'):format(tostring(activeToolFillLevel), tostring(driveOn), tostring(fillTrigger), tostring(fillTypesMatch), tostring(canRefill)));
if canRefill then
allowedToDrive = false;
--courseplay:handleSpecialTools(vehicle,workTool,unfold,lower,turnOn,allowedToDrive,cover,unload)
courseplay:handleSpecialTools(vehicle,activeTool,nil,nil,nil,allowedToDrive,false,false)
if not activeTool.isFilling then
activeTool:setIsFilling(true);
end;
courseplay:setInfoText(vehicle, courseplay:loc("COURSEPLAY_LOADING_AMOUNT"):format(activeTool.fillLevel, activeTool.capacity));
elseif vehicle.cp.isLoaded or (activeToolFillLevel ~= nil and activeToolFillLevel >= driveOn) then
if activeTool.isFilling then
activeTool:setIsFilling(false);
end;
courseplay:handleSpecialTools(vehicle,activeTool,nil,nil,nil,allowedToDrive,false,false)
vehicle.cp.fillTrigger = nil
end;
end;
-- SOWING MACHINE
if courseplay:isSowingMachine(activeTool) then
if vehicle.cp.fillTrigger ~= nil then
local trigger = courseplay.triggers.all[vehicle.cp.fillTrigger]
if trigger.isSowingMachineFillTrigger then
--print("slow down , its a SowingMachineFillTrigger")
vehicle.cp.isInFilltrigger = true
end
end
if fillLevelPct < driveOn and activeTool.fillTriggers[1] ~= nil and activeTool.fillTriggers[1].isSowingMachineFillTrigger then
--print(tableShow(activeTool.fillTriggers,"activeTool.fillTriggers"))
if not activeTool.isFilling then
activeTool:setIsFilling(true);
end;
allowedToDrive = false;
courseplay:setInfoText(vehicle, courseplay:loc('COURSEPLAY_LOADING_AMOUNT'):format(activeTool.fillLevel, activeTool.capacity));
elseif activeTool.fillTriggers[1] ~= nil then
if activeTool.isFilling then
activeTool:setIsFilling(false);
end;
vehicle.cp.fillTrigger = nil
end;
end;
--Tree Planter
if activeTool.cp.isTreePlanter then
if activeTool.nearestSaplingPallet ~= nil and activeTool.mountedSaplingPallet == nil then
local id = activeTool.nearestSaplingPallet.id;
-- print("load Pallet "..tostring(id));
activeTool:loadPallet(id);
end
end
end;
return allowedToDrive,lx,lz
end;
function courseplay:regulateTrafficSpeed(vehicle,refSpeed,allowedToDrive)
if vehicle.cp.isTrafficBraking then
return refSpeed
end
if vehicle.cp.collidingVehicleId ~= nil then
local collisionVehicle = g_currentMission.nodeToVehicle[vehicle.cp.collidingVehicleId];
local vehicleBehind = false
if collisionVehicle == nil then
courseplay:debug(nameNum(vehicle)..": regulateTrafficSpeed(1216): setting vehicle.cp.collidingVehicleId nil",3)
courseplay:deleteCollisionVehicle(vehicle)
vehicle.cp.collidingVehicleId = nil
vehicle.CPnumCollidingVehicles = max(vehicle.CPnumCollidingVehicles-1, 0);
return refSpeed
else
local name = getName(vehicle.cp.collidingVehicleId)
courseplay:debug(nameNum(vehicle)..": regulateTrafficSpeed: "..tostring(name),3)
end