forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.lua
1250 lines (1096 loc) · 53.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;
-- 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.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
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.cp.waypointIndex].turn ~= nil then
self.cp.isTurning = self.Waypoints[self.cp.waypointIndex].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.cp.waypointIndex > 2)
or ((self.cp.mode == 4 or self.cp.mode == 6) and self.cp.waypointIndex > self.cp.stopWork)
or combineBeaconOn;
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.previousWaypointIndex].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.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.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, ('COURSEPLAY_LOADING_AMOUNT;%d;%d'):format(courseplay.utils:roundToLowerInterval(self.cp.tipperFillLevel, 100), self.cp.tipperCapacity));
end
elseif self.cp.mode == 6 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.previousWaypointIndex ~= self.cp.startWork and self.cp.previousWaypointIndex ~= 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.previousWaypointIndex == 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
allowedToDrive, lx, lz = courseplay:handleMode8(self, false, true, allowedToDrive, lx, lz, dt);
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.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;
-- ### 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.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 = courseplay:handle_mode1(self, allowedToDrive);
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 - 4 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 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, self.cp.tipperFillLevelPct, 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.tipperFillLevelPct, self.cp.refillUntilPct, allowedToDrive, lx, lz, dt);
end
end;
-- 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;
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.cp.waypointIndex == self.cp.numWaypoints 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.cp.waypointIndex].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.cp.waypointIndex >= minCoverWaypoint and self.cp.waypointIndex < self.cp.numWaypoints 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.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.tipperFillLevelPct == 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
-- 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.cp.waypointIndex > self.cp.numWaypoints - 3;
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.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 (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) 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 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.cp.waypointIndex == 1 or self.cp.waypointIndex == self.cp.numWaypoints - 1 or self.Waypoints[self.cp.waypointIndex].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.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;
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.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;
--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, 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.cp.waypointIndex < self.cp.numWaypoints 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:setWaypointIndex(self, self.cp.waypointIndex + 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:setWaypointIndex(self, 1);
end
self.cp.isUnloaded = false
courseplay:setStopAtEnd(self, false);
courseplay:setIsLoaded(self, false);
courseplay:setIsRecording(self, false);
self:setCpVar('canDrive',true,courseplay.isClient)
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.cp.waypointIndex > 3) or vehicle.cp.mode == 5 or vehicle.cp.mode == 8 or ((vehicle.cp.mode == 4 or vehicle.cp.mode == 6) and vehicle.cp.waypointIndex > vehicle.cp.stopWork) or (vehicle.cp.mode == 2 and vehicle.cp.waypointIndex > 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.cp.waypointIndex
if vehicle.cp.collidingVehicleId == nil then
for i=2,vehicle.cp.numTrafficCollisionTriggers do
if workArea or recordNumber + i >= vehicle.cp.numWaypoints 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].cx,nodeY,vehicle.Waypoints[recordNumber].cz);
local _,_,Z = worldToLocal(vehicle.cp.trafficCollisionTriggers[i], vehicle.Waypoints[recordNumber].cx,nodeY,vehicle.Waypoints[recordNumber].cz);
local index = 1
local oldValue = Z
while Z < 5.5 do
recordNumber = recordNumber+index
if recordNumber > vehicle.cp.numWaypoints then -- just a backup
break
end
nodeDirX,nodeDirY,nodeDirZ,distance = courseplay:getWorldDirection(nodeX,nodeY,nodeZ, vehicle.Waypoints[recordNumber].cx,nodeY,vehicle.Waypoints[recordNumber].cz);
_,_,Z = worldToLocal(vehicle.cp.trafficCollisionTriggers[i], vehicle.Waypoints[recordNumber].cx,nodeY,vehicle.Waypoints[recordNumber].cz);
if oldValue > Z then
AIVehicleUtil.setCollisionDirection(vehicle.cp.trafficCollisionTriggers[1], vehicle.cp.trafficCollisionTriggers[i], 0, 1);
break
end
index = index +1
oldValue = Z
end
nodeDirX,nodeDirY,nodeDirZ = worldDirectionToLocal(vehicle.cp.trafficCollisionTriggers[i-1], nodeDirX,nodeDirY,nodeDirZ);
--print("colli"..i..": setDirection z= "..tostring(nodeDirZ).." waypoint: "..tostring(recordNumber))
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 inQueue = 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, _, tz = worldToLocal(vehicle.cp.trafficCollisionTriggers[1], 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 and collisionVehicle.rootNode == nil) 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, tx: %s, vehicle.cp.collidingObjects.all[Id]: %s'):format(nameNum(vehicle),tostring(transY),tostring(tx),tostring(vehicle.cp.collidingObjects.all[vehicle.cp.collidingVehicleId])), 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()
inQueue = vehicle.cp.mode == 1 and vehicle.cp.waypointIndex == 1 and attacher.cp ~= nil and attacher.cp.isDriving and attacher.cp.mode == 1 and attacher.cp.waypointIndex == 2
end
end;
if displayWarnings and vehicle.cp.inTraffic and not inQueue 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: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
local x, y, z = getWorldTranslation(vehicle.cp.collidingVehicleId)
local x1, y1, z1 = worldToLocal(vehicle.cp.DirectionNode, x, y, z)
if z1 < 0 or abs(x1) > 5 and not vehicle.cp.collidingObjects.all[vehicle.cp.collidingVehicleId] then -- vehicle behind tractor
vehicleBehind = true
end
local distance = 0
if collisionVehicle.rootNode ~= nil then
distance = courseplay:distanceToObject(vehicle, collisionVehicle)
end
if collisionVehicle.rootNode == nil or collisionVehicle.lastSpeedReal == nil or (distance > 40) or vehicleBehind then
courseplay:debug(string.format("%s: v.rootNode= %s,v.lastSpeedReal= %s, distance: %f, vehicleBehind= %s",nameNum(vehicle),tostring(collisionVehicle.rootNode),tostring(collisionVehicle.lastSpeedReal),distance,tostring(vehicleBehind)),3)
courseplay:deleteCollisionVehicle(vehicle)
--courseplay:debug(nameNum(vehicle)..": regulateTrafficSpeed(1230): setting vehicle.cp.collidingVehicleId nil",3)
else
if allowedToDrive and not (vehicle.cp.mode == 9 and collisionVehicle.allowFillFromAir) then
if vehicle.cp.curSpeed - (collisionVehicle.lastSpeedReal*3600) > 15 or z1 < 3 then
vehicle.cp.TrafficBrake = true
else
return min(collisionVehicle.lastSpeedReal*3600,refSpeed)
end
end
end
end
return refSpeed
end
function courseplay:getIsVehicleOffsetValid(vehicle)
local valid = vehicle.cp.totalOffsetX ~= nil and vehicle.cp.toolOffsetZ ~= nil and (vehicle.cp.totalOffsetX ~= 0 or vehicle.cp.toolOffsetZ ~= 0);
if not valid then
return false;
end;
if vehicle.cp.mode == 3 then
if vehicle.cp.laneOffset ~= 0 then
courseplay:changeLaneOffset(vehicle, nil, 0);
end;
return vehicle.cp.waypointIndex > 2 and vehicle.cp.waypointIndex > vehicle.cp.waitPoints[1] - 6 and vehicle.cp.waypointIndex <= vehicle.cp.waitPoints[1] + 3;
elseif vehicle.cp.mode == 4 or vehicle.cp.mode == 6 then
return vehicle.cp.waypointIndex >= vehicle.cp.startWork and vehicle.cp.waypointIndex <= vehicle.cp.stopWork;
elseif vehicle.cp.mode == 7 then
if vehicle.cp.laneOffset ~= 0 then
courseplay:changeLaneOffset(vehicle, nil, 0);
end;
return vehicle.cp.waypointIndex > 3 and vehicle.cp.waypointIndex > vehicle.cp.waitPoints[1] - 6 and vehicle.cp.waypointIndex <= vehicle.cp.waitPoints[1] + 3 and not vehicle.cp.mode7GoBackBeforeUnloading;
elseif vehicle.cp.mode == 8 then
if vehicle.cp.laneOffset ~= 0 then
courseplay:changeLaneOffset(vehicle, nil, 0);
end;
return vehicle.cp.waypointIndex > vehicle.cp.waitPoints[1] - 6 and vehicle.cp.waypointIndex <= vehicle.cp.waitPoints[1] + 3;
end;
return false;
end;
function courseplay:getVehicleOffsettedCoords(vehicle, x, z)
--courseplay:debug(string.format('%s: waypoint before offset: cx=%.2f, cz=%.2f', nameNum(vehicle), cx, cz), 2);
local fromX, fromZ, toX, toZ;
if vehicle.cp.waypointIndex == 1 then
fromX = x;
fromZ = z;
toX = vehicle.Waypoints[2].cx;
toZ = vehicle.Waypoints[2].cz;
elseif vehicle.Waypoints[vehicle.cp.previousWaypointIndex].rev then
fromX = x;
fromZ = z;
toX = vehicle.Waypoints[vehicle.cp.previousWaypointIndex].cx;
toZ = vehicle.Waypoints[vehicle.cp.previousWaypointIndex].cz;
else
fromX = vehicle.Waypoints[vehicle.cp.previousWaypointIndex].cx;
fromZ = vehicle.Waypoints[vehicle.cp.previousWaypointIndex].cz;
toX = x;
toZ = z;
end;
local dx,_,dz,dist = courseplay:getWorldDirection(fromX, 0, fromZ, toX, 0, toZ)
if dist and dist > 0.01 then
x = x - dz * vehicle.cp.totalOffsetX + dx * vehicle.cp.toolOffsetZ;
z = z + dx * vehicle.cp.totalOffsetX + dz * vehicle.cp.toolOffsetZ;
end;
--courseplay:debug(string.format('%s: waypoint after offset [%.1fm]: cx=%.2f, cz=%.2f', nameNum(vehicle), vehicle.cp.totalOffsetX, cx, cz), 2);
return x, z;
end;
function courseplay:handleMapWeightStation(vehicle, allowedToDrive)
local station, name, x, y, z, vehToCenterX, vehToCenterZ;
local isInFrontOfStation = vehicle.cp.fillTrigger ~= nil;
if isInFrontOfStation and vehicle.cp.curMapWeightStation == nil then
station = courseplay.triggers.all[vehicle.cp.fillTrigger];
-- station couldn't be found -> abort
if station == nil then
courseplay:debug(('%s: station == nil -> set fillTrigger to nil, return allowedToDrive'):format(nameNum(vehicle)), 20);
vehicle.cp.fillTrigger = nil;
return allowedToDrive;
end;
name = tostring(station.name);
x, y, z = getWorldTranslation(station.id);
local distToStation = courseplay:distanceToPoint(vehicle, x, y, z);
-- too far away from station -> abort
if distToStation > 60 then
vehicle.cp.fillTrigger = nil;
courseplay:debug(('%s: station=%s, distToStation=%.1f -> set fillTrigger to nil, return allowedToDrive'):format(nameNum(vehicle), name, distToStation), 20);
return allowedToDrive;
end;
if #station.vehiclesInTrigger > 0 then
local iAmInTrigger = false;
for i,id in pairs(station.vehiclesInTrigger) do
local vehInTrigger = g_currentMission.nodeToVehicle[id];
-- VEHICLE (or some part of it) IN TRIGGER
if vehicle.cpTrafficCollisionIgnoreList[id] then