forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolManager.lua
1527 lines (1347 loc) · 64.9 KB
/
toolManager.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 abs, cos, sin, min, max, deg = math.abs, math.cos, math.sin, math.min, math.max, math.deg;
local _;
-- ##### MANAGING TOOLS ##### --
function courseplay:attachImplement(implement)
--- Update Vehicle
local workTool = implement.object;
if workTool.attacherVehicle.cp.hasSpecializationSteerable then
workTool.attacherVehicle.cp.tooIsDirty = true;
end;
courseplay:setAttachedCombine(self);
end;
AIVehicle.onAttachImplement = Utils.appendedFunction(AIVehicle.onAttachImplement, courseplay.attachImplement);
function courseplay:detachImplement(implementIndex)
--- Update Vehicle
self.cp.tooIsDirty = true;
if self.attachedImplements[implementIndex].object == self.cp.attachedCombine then
self.cp.attachedCombine = nil;
courseplay:setMinHudPage(self);
end
end;
AIVehicle.onDetachImplement = Utils.appendedFunction(AIVehicle.onDetachImplement, courseplay.detachImplement);
function courseplay:resetTools(vehicle)
vehicle.cp.workTools = {}
-- are there any tippers?
vehicle.cp.hasAugerWagon = false;
vehicle.cp.workToolAttached = courseplay:updateWorkTools(vehicle, vehicle);
-- Reset fill type.
if #vehicle.cp.workTools > 0 and vehicle.cp.workTools[1].cp.hasSpecializationFillable and vehicle.cp.workTools[1].allowFillFromAir and vehicle.cp.workTools[1].allowTipDischarge then
if vehicle.cp.siloSelectedFillType == FillUtil.FILLTYPE_UNKNOWN or (vehicle.cp.siloSelectedFillType ~= FillUtil.FILLTYPE_UNKNOWN and not vehicle.cp.workTools[1]:allowFillType(vehicle.cp.siloSelectedFillType)) then
vehicle.cp.siloSelectedFillType = vehicle.cp.workTools[1]:getFirstEnabledFillType();
end;
else
vehicle.cp.siloSelectedFillType = FillUtil.FILLTYPE_UNKNOWN;
end;
if vehicle.cp.hud.currentPage == 1 then
courseplay.hud:setReloadPageOrder(vehicle, 1, true);
end;
vehicle.cp.easyFillTypeList = courseplay:getEasyFillTypeList(courseplay:getAllAvalibleFillTypes(vehicle));
vehicle.cp.siloSelectedEasyFillType = 0;
courseplay:changeSiloFillType(vehicle, 1, vehicle.cp.siloSelectedFillType);
courseplay:calculateWorkWidth(vehicle, true);
vehicle.cp.currentTrailerToFill = nil;
vehicle.cp.trailerFillDistance = nil;
vehicle.cp.tooIsDirty = false;
end;
function courseplay:changeSiloFillType(vehicle, modifyer, currentSelectedFilltype)
local eftl = vehicle.cp.easyFillTypeList;
local newVal = 1;
if currentSelectedFilltype and currentSelectedFilltype ~= FillUtil.FILLTYPE_UNKNOWN then
for index, fillType in ipairs(eftl) do
if currentSelectedFilltype == fillType then
newVal = index;
end;
end;
else
newVal = vehicle.cp.siloSelectedEasyFillType + modifyer
if newVal < 1 then
newVal = #eftl;
elseif newVal > #eftl then
newVal = 1;
end
end;
if vehicle.cp.siloSelectedFillType ~= eftl[newVal] then
--Mode 1 Run Counter Reset
vehicle.cp.runCounter = 0;
courseplay:changeRunCounter(vehicle, false)
end
vehicle.cp.siloSelectedEasyFillType = newVal;
vehicle.cp.siloSelectedFillType = eftl[newVal];
end;
function courseplay:getAvalibleFillTypes(object, fillUnitIndex)
if fillUnitIndex == nil then
local fillTypes = {}
if object.fillUnits then
for _, fillUnit in pairs(object.fillUnits) do
for fillType, enabled in pairs(fillUnit.fillTypes) do
if fillType ~= FillUtil.FILLTYPE_UNKNOWN and enabled then
fillTypes[fillType] = enabled;
end;
end;
end;
end;
return fillTypes;
end;
return object.fillUnits[fillUnitIndex].fillTypes;
end;
function courseplay:getAllAvalibleFillTypes(vehicle)
local fillTypes = {};
if #vehicle.cp.workTools > 0 then
for _, workTool in pairs(vehicle.cp.workTools) do
local toolFillTypes = courseplay:getAvalibleFillTypes(workTool);
for fillType, enabled in pairs(toolFillTypes) do
if fillType ~= FillUtil.FILLTYPE_UNKNOWN and enabled then
fillTypes[fillType] = enabled;
end;
end
end;
end;
return fillTypes;
end;
function courseplay:getEasyFillTypeList(fillTypes)
local easyFillTypeList = {};
for fillType, _ in pairs(fillTypes) do
table.insert(easyFillTypeList, fillType);
end;
return easyFillTypeList;
end;
function courseplay:isAttachedCombine(workTool)
return (workTool.typeName~= nil and (workTool.typeName == 'attachableCombine' or workTool.typeName == 'attachableCombine_mouseControlled')) or (not workTool.cp.hasSpecializationSteerable and workTool.hasPipe and not workTool.cp.isAugerWagon and not workTool.cp.isLiquidManureOverloader) or courseplay:isSpecialChopper(workTool)
end;
function courseplay:isAttachedMixer(workTool)
return workTool.typeName == "mixerWagon" or (not workTool.cp.hasSpecializationSteerable and workTool.cp.hasSpecializationMixerWagon)
end;
function courseplay:isAttacherModule(workTool)
if workTool.attacherJoint then
return (workTool.attacherJoint.jointType == AttacherJoints.JOINTTYPE_SEMITRAILER and (not workTool.wheels or (workTool.wheels and #workTool.wheels == 0))) or workTool.cp.isAttacherModule == true;
end;
return false;
end;
function courseplay:isBaleLoader(workTool) -- is the tool a bale loader?
return workTool.cp.hasSpecializationBaleLoader or (workTool.balesToLoad ~= nil and workTool.baleGrabber ~=nil and workTool.grabberIsMoving~= nil);
end;
function courseplay:isBaler(workTool) -- is the tool a baler?
return workTool.cp.hasSpecializationBaler or workTool.balerUnloadingState ~= nil or courseplay:isSpecialBaler(workTool);
end;
function courseplay:isBigM(workTool)
return workTool.cp.hasSpecializationSteerable and courseplay:isMower(workTool);
end;
function courseplay:isCombine(workTool)
return workTool.cp.hasSpecializationCombine and workTool.attachedCutters ~= nil and workTool.cp.capacity ~= nil and workTool.cp.capacity > 0;
end;
function courseplay:isChopper(workTool)
return workTool.cp.hasSpecializationCombine and workTool.attachedCutters ~= nil and workTool.cp.capacity == 0 or courseplay:isSpecialChopper(workTool);
end;
function courseplay:isFoldable(workTool) --is the tool foldable?
return workTool.cp.hasSpecializationFoldable and workTool.foldingParts ~= nil and #workTool.foldingParts > 0;
end;
function courseplay:isFrontloader(workTool)
return workTool.cp.hasSpecializationCylindered and workTool.cp.hasSpecializationAnimatedVehicle and not workTool.cp.hasSpecializationShovel;
end;
function courseplay:isHarvesterSteerable(workTool)
return Utils.getNoNil(workTool.typeName == "selfPropelledPotatoHarvester" or workTool.cp.isHarvesterSteerable, false);
end;
function courseplay:isHookLift(workTool)
if workTool.attacherJoint then
return workTool.attacherJoint.jointType == AttacherJoints.JOINTTYPE_HOOKLIFT;
end;
return false;
end
function courseplay:isMixer(workTool)
return workTool.typeName == "selfPropelledMixerWagon" or (workTool.cp.hasSpecializationSteerable and workTool.cp.hasSpecializationMixerWagon)
end;
function courseplay:isMower(workTool)
return workTool.cp.hasSpecializationMower or courseplay:isSpecialMower(workTool);
end;
function courseplay:isRoundbaler(workTool) -- is the tool a roundbaler?
return courseplay:isBaler(workTool) and workTool.baler ~= nil and (workTool.baler.baleCloseAnimationName ~= nil and workTool.baler.baleUnloadAnimationName ~= nil or courseplay:isSpecialRoundBaler(workTool));
end;
function courseplay:isSowingMachine(workTool) -- is the tool a sowing machine?
return workTool.cp.hasSpecializationSowingMachine or courseplay:isSpecialSowingMachine(workTool);
end;
function courseplay:isSpecialChopper(workTool)
return workTool.typeName == "woodCrusherTrailer" or workTool.cp.isPoettingerMex5
end
function courseplay:isSprayer(workTool) -- is the tool a sprayer/spreader?
return workTool.cp.hasSpecializationSprayer or courseplay:isSpecialSprayer(workTool)
end;
function courseplay:isWheelloader(workTool)
return workTool.typeName:match("wheelLoader");
end;
-- UPDATE WORKTOOL DATA
function courseplay:updateWorkTools(vehicle, workTool, isImplement)
if not isImplement then
cpPrintLine(6, 3);
courseplay:debug(('%s: updateWorkTools(%s, %q, isImplement=false) (mode=%d)'):format(nameNum(vehicle),tostring(vehicle.name), nameNum(workTool), vehicle.cp.mode), 6);
else
cpPrintLine(6);
courseplay:debug(('%s: updateWorkTools(%s, %q, isImplement=true)'):format(nameNum(vehicle),tostring(vehicle.name), nameNum(workTool)), 6);
end;
-- Reset distances if in debug mode 6.
if courseplay.debugChannels[6] ~= nil and courseplay.debugChannels[6] == true then
workTool.cp.distances = nil;
end;
courseplay:setNameVariable(workTool);
courseplay:setOwnFillLevelsAndCapacities(workTool,vehicle.cp.mode)
local hasWorkTool = false;
local hasWaterTrailer = false
-- MODE 1 + 2: GRAIN TRANSPORT / COMBI MODE
if vehicle.cp.mode == 1 or vehicle.cp.mode == 2 then
if workTool.allowTipDischarge and workTool.cp.capacity and workTool.cp.capacity > 0.1 then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
if workTool.cp.isAugerWagon then
vehicle.cp.hasAugerWagon = true;
end;
end;
-- MODE 3: AUGERWAGON
elseif vehicle.cp.mode == 3 then
if workTool.cp.isAugerWagon then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
vehicle.cp.hasAugerWagon = true;
end
-- MODE 4: FERTILIZER AND SEEDING
elseif vehicle.cp.mode == 4 then
local isSprayer, isSowingMachine = courseplay:isSprayer(workTool), courseplay:isSowingMachine(workTool);
if isSprayer or isSowingMachine or workTool.cp.isTreePlanter or workTool.cp.isKuhnDC401 or workTool.cp.isKuhnHR4004 then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
courseplay:setMarkers(vehicle, workTool)
vehicle.cp.hasMachinetoFill = true;
vehicle.cp.noStopOnEdge = isSprayer and not (isSowingMachine or workTool.cp.isTreePlanter);
vehicle.cp.noStopOnTurn = isSprayer and not (isSowingMachine or workTool.cp.isTreePlanter);
if isSprayer then
vehicle.cp.hasSprayer = true;
end;
if isSowingMachine then
vehicle.cp.hasSowingMachine = true;
end;
end;
-- MODE 5: TRANSFER
elseif vehicle.cp.mode == 5 then
-- For reversing purpose
if isImplement then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
end;
-- DO NOTHING
-- MODE 6: FIELDWORK
elseif vehicle.cp.mode == 6 then
if (courseplay:isBaler(workTool)
or courseplay:isBaleLoader(workTool)
or courseplay:isSpecialBaleLoader(workTool)
or workTool.cp.hasSpecializationCultivator
or workTool.cp.hasSpecializationCutter
or workTool.cp.hasSpecializationFruitPreparer
or workTool.cp.hasSpecializationPlough
or workTool.cp.hasSpecializationTedder
or workTool.cp.hasSpecializationWindrower
or workTool.allowTipDischarge
or courseplay:isMower(workTool)
or courseplay:isAttachedCombine(workTool)
or courseplay:isFoldable(workTool))
and not courseplay:isSprayer(workTool)
and not courseplay:isSowingMachine(workTool)
then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
courseplay:setMarkers(vehicle, workTool);
vehicle.cp.noStopOnTurn = courseplay:isBaler(workTool) or courseplay:isBaleLoader(workTool) or courseplay:isSpecialBaleLoader(workTool) or vehicle.attachedCutters ~= nil;
vehicle.cp.noStopOnEdge = courseplay:isBaler(workTool) or courseplay:isBaleLoader(workTool) or courseplay:isSpecialBaleLoader(workTool) or vehicle.attachedCutters ~= nil;
if workTool.cp.hasSpecializationPlough then
vehicle.cp.hasPlough = true;
if workTool.rotationPart.turnAnimation ~= nil then
vehicle.cp.hasRotateablePlough = true;
end;
end;
if courseplay:isBaleLoader(workTool) or courseplay:isSpecialBaleLoader(workTool) then
vehicle.cp.hasBaleLoader = true;
end;
end;
-- MODE 7: COMBINE SELF UNLOADING
elseif vehicle.cp.mode == 7 then
-- DO NOTHING
-- MODE 8: LIQUID MANURE TRANSFER
elseif vehicle.cp.mode == 8 then
if workTool.cp.hasSpecializationFillable and ((workTool.overloading ~= nil or workTool.setIsReFilling ~= nil) or workTool.cp.isFuelTrailer or workTool.cp.isWaterTrailer) then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
vehicle.cp.hasMachinetoFill = true;
workTool.cp.waterReceiverTrigger = nil; -- water trailer: make sure it has no saved unloading water trigger
end;
hasWaterTrailer = workTool.cp.isWaterTrailer
-- MODE 9: FILL AND EMPTY SHOVEL
elseif vehicle.cp.mode == 9 then
if not isImplement and (courseplay:isWheelloader(workTool) or workTool.typeName == 'frontloader' or courseplay:isMixer(workTool)) then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
workTool.cp.shovelState = 1;
elseif isImplement and (courseplay:isFrontloader(workTool) or workTool.cp.hasSpecializationShovel) then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
workTool.attacherVehicle.cp.shovelState = 1
end;
-- MODE 10:Leveler
elseif vehicle.cp.mode == 10 then
if isImplement and workTool.cp.hasSpecializationLeveler then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
end;
end;
vehicle.cp.hasWaterTrailer = hasWaterTrailer
if hasWorkTool then
courseplay:debug(('%s: workTool %q added to workTools (index %d)'):format(nameNum(vehicle), nameNum(workTool), #vehicle.cp.workTools), 6);
end;
--------------------------------------------------
if not isImplement or hasWorkTool or workTool.cp.isNonTippersHandledWorkTool then
-- SPECIAL SETTINGS
courseplay:askForSpecialSettings(vehicle, workTool);
--FOLDING PARTS: isFolded/isUnfolded states
courseplay:setFoldedStates(workTool);
end;
-- REVERSE PROPERTIES
courseplay:getReverseProperties(vehicle, workTool);
-- aiTurnNoBackward
if isImplement and hasWorkTool then
if not vehicle.cp.aiTurnNoBackward and workTool.cp.notToBeReversed then
vehicle.cp.aiTurnNoBackward = true;
courseplay:debug(('%s: workTool.cp.notToBeReversed == true --> vehicle.cp.aiTurnNoBackward = true'):format(nameNum(workTool)), 6);
end;
end;
-- TRAFFIC COLLISION IGNORE LIST
courseplay:debug(('%s: adding %q (%q) to cpTrafficCollisionIgnoreList'):format(nameNum(vehicle), nameNum(workTool), tostring(workTool.cp.xmlFileName)), 3);
vehicle.cpTrafficCollisionIgnoreList[workTool.rootNode] = true;
-- TRAFFIC COLLISION IGNORE LIST (components)
if not isImplement or workTool.cp.hasSpecializationCutter then
courseplay:debug(('%s: adding %q (%q) components to cpTrafficCollisionIgnoreList'):format(nameNum(vehicle), nameNum(workTool), tostring(workTool.cp.xmlFileName)), 3);
for i,component in pairs(workTool.components) do
vehicle.cpTrafficCollisionIgnoreList[component.node] = true;
end;
end;
-- CHECK ATTACHED IMPLEMENTS
for k,impl in pairs(workTool.attachedImplements) do
local implIsWorkTool = courseplay:updateWorkTools(vehicle, impl.object, true);
if implIsWorkTool then
hasWorkTool = true;
end;
end;
-- STEERABLE (vehicle)
if not isImplement then
vehicle.cp.numWorkTools = #vehicle.cp.workTools;
-- list debug
if courseplay.debugChannels[3] then
cpPrintLine(6);
courseplay:debug(('%s cpTrafficCollisionIgnoreList'):format(nameNum(vehicle)), 3);
for a,b in pairs(vehicle.cpTrafficCollisionIgnoreList) do
local name = g_currentMission.nodeToVehicle[a].name;
courseplay:debug(('\\___ [%s] = %s (%q)'):format(tostring(a), tostring(name), tostring(getName(a))), 3);
end;
end;
-- TURN DIAMETER
if g_server ~= nil then
courseplay:setAutoTurnDiameter(vehicle, hasWorkTool);
end
-- TIP REFERENCE POINTS
courseplay:setTipRefOffset(vehicle);
-- TIPPER COVERS
vehicle.cp.tipperHasCover = false;
vehicle.cp.tippersWithCovers = {};
if hasWorkTool then
courseplay:setTipperCoverData(vehicle);
end;
-- FINAL WORKTOOLS TABLE DEBUG
if courseplay.debugChannels[6] then
cpPrintLine(6);
if vehicle.cp.numWorkTools > 0 then
courseplay:debug(('%s: workTools:'):format(nameNum(vehicle)), 6);
for i=1, vehicle.cp.numWorkTools do
courseplay:debug(('\\___ [%d] = %s'):format(i, nameNum(vehicle.cp.workTools[i])), 6);
end;
else
courseplay:debug(('%s: no workTools set'):format(nameNum(vehicle)), 6);
end;
end;
end;
--------------------------------------------------
if not isImplement then
cpPrintLine(6, 3);
end;
return hasWorkTool;
end;
function courseplay:setTipRefOffset(vehicle)
vehicle.cp.tipRefOffset = 0;
for i=1, vehicle.cp.numWorkTools do
vehicle.cp.workTools[i].cp.rearTipRefPoint = nil;
if vehicle.cp.hasMachinetoFill then
vehicle.cp.tipRefOffset = 1.5;
elseif vehicle.cp.workTools[i].rootNode ~= nil and vehicle.cp.workTools[i].tipReferencePoints ~= nil then
if #(vehicle.cp.workTools[i].tipReferencePoints) > 1 then
for n=1 ,#(vehicle.cp.workTools[i].tipReferencePoints) do
local tipperX, tipperY, tipperZ = getWorldTranslation(vehicle.cp.workTools[i].tipReferencePoints[n].node);
local tipRefPointX, tipRefPointY, tipRefPointZ = worldToLocal(vehicle.cp.workTools[i].rootNode, tipperX, tipperY, tipperZ);
courseplay:debug(string.format("point%s : tipRefPointX (%s), tipRefPointY (%s), tipRefPointZ(%s)",tostring(n),tostring(tipRefPointX),tostring(tipRefPointY),tostring( tipRefPointZ)),13)
tipRefPointX = abs(tipRefPointX);
if tipRefPointX > vehicle.cp.tipRefOffset then
if tipRefPointX > 0.1 then
vehicle.cp.tipRefOffset = tipRefPointX;
else
vehicle.cp.tipRefOffset = 0
end;
end
-- Find the rear tipRefpoint in case we are BGA tipping.
if tipRefPointX < 0.1 and tipRefPointZ < 0 then
if not vehicle.cp.workTools[i].cp.rearTipRefPoint or vehicle.cp.workTools[i].tipReferencePoints[n].width > vehicle.cp.workTools[i].tipReferencePoints[vehicle.cp.workTools[i].cp.rearTipRefPoint].width then
vehicle.cp.workTools[i].cp.rearTipRefPoint = n;
courseplay:debug(string.format("%s: Found rear TipRefPoint: %d - tipRefPointZ = %f", nameNum(vehicle), n, tipRefPointZ), 13);
end;
end;
end;
else
vehicle.cp.workTools[i].cp.rearTipRefPoint = 1
vehicle.cp.tipRefOffset = 0;
end;
end;
end;
end;
function courseplay:setMarkers(vehicle, object)
local realDirectionNode = vehicle.isReverseDriving and vehicle.cp.reverseDrivingDirectionNode or vehicle.cp.DirectionNode;
local aLittleBitMore = 1;
local pivotJointNode = courseplay:getPivotJointNode(object);
object.cp.backMarkerOffset = nil;
object.cp.aiFrontMarker = nil;
-- Get and set vehicle distances if not set
local vehicleDistances = vehicle.cp.distances or courseplay:getDistances(vehicle);
-- Get and set object distances if not set
local objectDistances = object.cp.distances or courseplay:getDistances(object);
-- get the behindest and the frontest points :-) ( as offset to root node)
local area = object.workAreas
if object.attachedCutters ~= nil and not object.cp.hasSpecializationFruitPreparer and not courseplay:isAttachedCombine(object) then
courseplay:debug(('%s: setMarkers(): %s is a combine -> return '):format(nameNum(vehicle), tostring(object.name)), 6);
return;
end
if not area or object.cp.noWorkArea then
courseplay:debug(('%s: setMarkers(): %s has no workAreas -> return '):format(nameNum(vehicle), tostring(object.name)), 6);
return;
end;
local tableLength = #(area)
if tableLength == 0 then
if courseplay:isWheeledWorkTool(object) and object.attacherJoint.jointType and vehicleDistances.turningNodeToRearTrailerAttacherJoints[object.attacherJoint.jointType] then
-- Calculate the offset based on the distances
local ztt = vehicleDistances.turningNodeToRearTrailerAttacherJoints[object.attacherJoint.jointType] * -1;
local backMarkerCorrection = Utils.getNoNil(object.cp.backMarkerOffsetCorection, 0);
if vehicle.cp.backMarkerOffset == nil or (abs(backMarkerCorrection) > 0 and ztt + backMarkerCorrection > vehicle.cp.backMarkerOffset) then
vehicle.cp.backMarkerOffset = abs(backMarkerCorrection) > 0 and ztt + backMarkerCorrection or 0;
end
local frontMarkerCorrection = Utils.getNoNil(object.cp.frontMarkerOffsetCorection, 0);
if vehicle.cp.aiFrontMarker == nil or (abs(frontMarkerCorrection) > 0 and ztt + frontMarkerCorrection < vehicle.cp.aiFrontMarker) then
vehicle.cp.aiFrontMarker = abs(frontMarkerCorrection) > 0 and ztt + frontMarkerCorrection or -3;
end
courseplay:debug(('%s: setMarkers(): cp.backMarkerOffset = %s, cp.aiFrontMarker = %s'):format(nameNum(vehicle), tostring(vehicle.cp.backMarkerOffset), tostring(vehicle.cp.aiFrontMarker)), 6);
else
--- Set front and back marker to default values, so we don't check again.
if vehicle.cp.backMarkerOffset == nil then
vehicle.cp.backMarkerOffset = 0;
end
if vehicle.cp.aiFrontMarker == nil then
vehicle.cp.aiFrontMarker = -3;
end
courseplay:debug(('%s: setMarkers(): %s has no workAreas -> return '):format(nameNum(vehicle), tostring(object.name)), 6);
end;
return;
end
local avoidType = {
[WorkArea.AREATYPE_RIDGEMARKER] = true;
[WorkArea.AREATYPE_MOWERDROP] = true;
[WorkArea.AREATYPE_WINDROWERDROP] = true;
[WorkArea.AREATYPE_TEDDERDROP] = true;
}
for k = 1, tableLength do
if not avoidType[area[k].type] then
for j,node in pairs(area[k]) do
if j == "start" or j == "height" or j == "width" then
local x, y, z;
local ztt = 0;
local type;
if pivotJointNode and object.attacherJoint.jointType and vehicleDistances.turningNodeToRearTrailerAttacherJoints[object.attacherJoint.jointType] then
type = "Pivot Trailer";
x, y, z = getWorldTranslation(pivotJointNode);
-- Get the marker offset from the pivot node.
_, _, ztt = worldToLocal(node, x, y, z);
-- Calculate the offset based on the distances
ztt = ((vehicleDistances.turningNodeToRearTrailerAttacherJoints[object.attacherJoint.jointType] + objectDistances.attacherJointToPivot) * -1) - ztt;
elseif courseplay:isWheeledWorkTool(object) and object.attacherJoint.jointType and vehicleDistances.turningNodeToRearTrailerAttacherJoints[object.attacherJoint.jointType] then
type = "Trailer";
x, y, z = getWorldTranslation(object.attacherJoint.node)
-- Get the marker offset from the pivot node.
_, _, ztt = worldToLocal(node, x, y, z);
-- Calculate the offset based on the distances
ztt = (vehicleDistances.turningNodeToRearTrailerAttacherJoints[object.attacherJoint.jointType] * -1) - ztt;
else
type = "Vehicle";
x, y, z = getWorldTranslation(node);
_, _, ztt = worldToLocal(realDirectionNode, x, y, z);
end;
courseplay:debug(('%s: %s %s Point(%s) %s: ztt = %s'):format(nameNum(vehicle), tostring(object.name), type, tostring(k), tostring(j), tostring(ztt)), 6);
if object.cp.backMarkerOffset == nil or ztt + Utils.getNoNil(object.cp.backMarkerOffsetCorection, 0) > object.cp.backMarkerOffset then
object.cp.backMarkerOffset = ztt + Utils.getNoNil(object.cp.backMarkerOffsetCorection, 0);
end
if object.cp.aiFrontMarker == nil or ztt + Utils.getNoNil(object.cp.frontMarkerOffsetCorection, 0) < object.cp.aiFrontMarker then
object.cp.aiFrontMarker = ztt + Utils.getNoNil(object.cp.frontMarkerOffsetCorection, 0);
end
end
end
else
courseplay:debug(("%s: Avoiding workArea Type %s"):format(nameNum(vehicle), WorkArea.areaTypeIntToName[area[k].type]), 6);
end;
end
if vehicle.cp.backMarkerOffset == nil or object.cp.backMarkerOffset < (vehicle.cp.backMarkerOffset + aLittleBitMore) then
vehicle.cp.backMarkerOffset = object.cp.backMarkerOffset - aLittleBitMore;
end
if vehicle.cp.aiFrontMarker == nil or object.cp.aiFrontMarker > (vehicle.cp.aiFrontMarker - aLittleBitMore) then
vehicle.cp.aiFrontMarker = object.cp.aiFrontMarker + aLittleBitMore * 0.75;
end
courseplay:debug(('%s: setMarkers(): cp.backMarkerOffset = %s, cp.aiFrontMarker = %s'):format(nameNum(vehicle), tostring(vehicle.cp.backMarkerOffset), tostring(vehicle.cp.aiFrontMarker)), 6);
end;
function courseplay:setFoldedStates(object)
if courseplay:isFoldable(object) and object.turnOnFoldDirection then
cpPrintLine(17);
courseplay:debug(nameNum(object) .. ': setFoldedStates()', 17);
object.cp.realUnfoldDirection = object.turnOnFoldDirection;
if object.cp.foldingPartsStartMoveDirection and object.cp.foldingPartsStartMoveDirection ~= 0 and object.cp.foldingPartsStartMoveDirection ~= object.turnOnFoldDirection then
object.cp.realUnfoldDirection = object.turnOnFoldDirection * object.cp.foldingPartsStartMoveDirection;
end;
if object.cp.realUnfoldDirectionIsReversed then
object.cp.realUnfoldDirection = -object.cp.realUnfoldDirection;
end;
courseplay:debug(string.format('startAnimTime=%s, turnOnFoldDirection=%s, foldingPartsStartMoveDirection=%s --> realUnfoldDirection=%s', tostring(object.startAnimTime), tostring(object.turnOnFoldDirection), tostring(object.cp.foldingPartsStartMoveDirection), tostring(object.cp.realUnfoldDirection)), 17);
for i,foldingPart in pairs(object.foldingParts) do
foldingPart.isFoldedAnimTime = 0;
foldingPart.isFoldedAnimTimeNormal = 0;
foldingPart.isUnfoldedAnimTime = foldingPart.animDuration;
foldingPart.isUnfoldedAnimTimeNormal = 1;
if object.cp.realUnfoldDirection < 0 then
foldingPart.isFoldedAnimTime = foldingPart.animDuration;
foldingPart.isFoldedAnimTimeNormal = 1;
foldingPart.isUnfoldedAnimTime = 0;
foldingPart.isUnfoldedAnimTimeNormal = 0;
end;
courseplay:debug(string.format('\tfoldingPart %d: isFoldedAnimTime=%s (normal: %d), isUnfoldedAnimTime=%s (normal: %d)', i, tostring(foldingPart.isFoldedAnimTime), foldingPart.isFoldedAnimTimeNormal, tostring(foldingPart.isUnfoldedAnimTime), foldingPart.isUnfoldedAnimTimeNormal), 17);
end;
cpPrintLine(17);
end;
end;
function courseplay:setTipperCoverData(vehicle)
for i=1, vehicle.cp.numWorkTools do
local workTool = vehicle.cp.workTools[i];
-- Default Giants trailers
if workTool.cp.hasSpecializationCover and not workTool.cp.isStrawBlower then
courseplay:debug(string.format('Implement %q has a cover (hasSpecializationCover == true)', tostring(workTool.name)), 6);
local data = {
coverType = 'defaultGiants',
tipperIndex = i,
};
table.insert(vehicle.cp.tippersWithCovers, data);
vehicle.cp.tipperHasCover = true;
-- TODO: Delete old mod code if sure not needed anymore.
-- Example: for mods trailer that don't use the default cover specialization. Look at openCloseCover() to see how this is used!
else--if workTool.cp.isCoverVehicle then
--courseplay:debug(string.format('Implement %q has a cover (isCoverVehicle == true)', tostring(workTool.name)), 6);
--vehicle.cp.tipperHasCover = true;
--local coverItems = someCreatedCoverList;
--local data = {
-- coverType = 'CoverVehicle',
-- tipperIndex = i,
-- coverItems = coverItems,
-- showCoverWhenTipping = true
--};
--table.insert(vehicle.cp.tippersWithCovers, data);
end;
end;
end;
function courseplay:setAutoTurnDiameter(vehicle, hasWorkTool)
cpPrintLine(6, 3);
local turnRadius, turnRadiusAuto = 10, 10;
vehicle.cp.turnDiameterAuto = vehicle.cp.vehicleTurnRadius * 2;
courseplay:debug(('%s: Set turnDiameterAuto to %.2fm (2 x vehicleTurnRadius)'):format(nameNum(vehicle), vehicle.cp.turnDiameterAuto), 6);
-- Check if we have worktools and if we are in a valid mode
if hasWorkTool and (vehicle.cp.mode == 2 or vehicle.cp.mode == 3 or vehicle.cp.mode == 4 or vehicle.cp.mode == 6) then
courseplay:debug(('%s: getHighestToolTurnDiameter(%s)'):format(nameNum(vehicle), vehicle.name), 6);
local toolTurnDiameter = courseplay:getHighestToolTurnDiameter(vehicle);
-- If the toolTurnDiameter is bigger than the turnDiameterAuto, then set turnDiameterAuto to toolTurnDiameter
if toolTurnDiameter > vehicle.cp.turnDiameterAuto then
courseplay:debug(('%s: toolTurnDiameter(%.2fm) > turnDiameterAuto(%.2fm), turnDiameterAuto set to %.2fm'):format(nameNum(vehicle), toolTurnDiameter, vehicle.cp.turnDiameterAuto, toolTurnDiameter), 6);
vehicle.cp.turnDiameterAuto = toolTurnDiameter;
end;
end;
if vehicle.cp.turnDiameterAutoMode then
vehicle.cp.turnDiameter = vehicle.cp.turnDiameterAuto;
courseplay:debug(('%s: turnDiameterAutoMode is active: turnDiameter set to %.2fm'):format(nameNum(vehicle), vehicle.cp.turnDiameterAuto), 6);
end;
cpPrintLine(6, 1);
end;
--##################################################
-- ##### LOADING TOOLS ##### --
function courseplay:load_tippers(vehicle, allowedToDrive)
--- vehicle.cp.tipperLoadMode == num
-- 0 = No loading mode is set. Will continue driving forward until mode 1 or 2 is set
-- 1 = Load at silo trigger. Will continue driving forward until the trailer is centered underneath the silo trigger, then stop and fill up
-- 2 = Stop at underneath wp 1 for field loading
if vehicle.cp.currentTrailerToFill == nil then
vehicle.cp.currentTrailerToFill = 1;
end
local currentTrailer = vehicle.cp.workTools[vehicle.cp.currentTrailerToFill];
local driveOn = vehicle.cp.isLoaded;
if not currentTrailer.cp.realUnloadOrFillNode and not driveOn then
currentTrailer.cp.realUnloadOrFillNode = courseplay:getRealUnloadOrFillNode(currentTrailer);
if not currentTrailer.cp.realUnloadOrFillNode then
if vehicle.cp.numWorkTools > vehicle.cp.currentTrailerToFill then
vehicle.cp.currentTrailerToFill = vehicle.cp.currentTrailerToFill + 1;
else
driveOn = true;
end;
end;
end;
if vehicle.cp.tipperLoadMode == 0 and not driveOn then
if vehicle.cp.waypointIndex == 2 and currentTrailer.cp.currentSiloTrigger == nil then
--- We must be on an loading point at a field so we stop under wp1 and wait for trailer to be filled up
vehicle.cp.tipperLoadMode = 2;
elseif currentTrailer.cp.currentSiloTrigger then
--- We have an silo trigger, so we go load at silo trigger mode
vehicle.cp.tipperLoadMode = 1;
else
--- We were not able to determ what mode to set, so we move further forward until one is set.
return allowedToDrive;
end;
end;
local unloadDistance = 1000;
local trailerX,_,trailerZ = getWorldTranslation(currentTrailer.cp.realUnloadOrFillNode);
if not driveOn then
if vehicle.cp.tipperLoadMode == 1 then
local directionNode = vehicle.aiVehicleDirectionNode or vehicle.cp.DirectionNode;
local _,vehicleY,_ = getWorldTranslation(directionNode);
local _,_,z = worldToLocal(directionNode, trailerX, vehicleY, trailerZ);
vehicle.cp.trailerFillDistance = z + 0.5;
if currentTrailer.cp.currentSiloTrigger ~= nil then
local triggerX,_,triggerZ = getWorldTranslation(currentTrailer.cp.currentSiloTrigger.rootNode);
_,_,unloadDistance = worldToLocal(directionNode, triggerX, vehicleY, triggerZ);
end;
elseif vehicle.cp.tipperLoadMode == 2 then
local cx, cz = vehicle.Waypoints[2].cx, vehicle.Waypoints[2].cz;
if not vehicle.cp.trailerFillDistance then
vehicle.cp.trailerFillDistance = courseplay:distance(cx, cz, trailerX, trailerZ) + 0.5;
end;
unloadDistance = courseplay:distance(cx, cz, trailerX, trailerZ);
end;
end;
-- SiloTrigger (Giants)
if vehicle.cp.tipperLoadMode == 1 and currentTrailer.cp.currentSiloTrigger ~= nil and not driveOn then
local acceptedFillType = false;
local siloTrigger = currentTrailer.cp.currentSiloTrigger;
for _, fillType in pairs(siloTrigger.fillTypes) do
if fillType == vehicle.cp.siloSelectedFillType then
acceptedFillType = true;
break;
end;
end;
if acceptedFillType then
local siloIsEmpty = siloTrigger:getFillLevel(vehicle.cp.siloSelectedFillType) <= 1;
if not siloTrigger.isFilling and not siloIsEmpty and currentTrailer:allowFillType(vehicle.cp.siloSelectedFillType, false) and unloadDistance < vehicle.cp.trailerFillDistance then
siloTrigger:startFill(vehicle.cp.siloSelectedFillType);
courseplay:setCustomTimer(vehicle, 'siloEmptyMessageDelay', 1);
courseplay:debug(('%s: SiloTrigger: selectedFillType = %s, isFilling = %s'):format(nameNum(vehicle), tostring(FillUtil.fillTypeIntToName[siloTrigger.selectedFillType]), tostring(siloTrigger.isFilling)), 2);
elseif siloTrigger.isFilling then
courseplay:setCustomTimer(vehicle, 'siloEmptyMessageDelay', 1);
elseif siloIsEmpty and vehicle.cp.totalFillLevelPercent < vehicle.cp.driveOnAtFillLevel and courseplay:timerIsThrough(vehicle, 'siloEmptyMessageDelay') then
CpManager:setGlobalInfoText(vehicle, 'FARM_SILO_IS_EMPTY');
end;
else
CpManager:setGlobalInfoText(vehicle, 'FARM_SILO_NO_FILLTYPE');
end;
end;
-- drive on when required fill level is reached
if not driveOn and (courseplay:timerIsThrough(vehicle, 'fillLevelChange') or vehicle.cp.prevFillLevelPct == nil) then
if vehicle.cp.prevFillLevelPct ~= nil and vehicle.cp.totalFillLevelPercent == vehicle.cp.prevFillLevelPct and vehicle.cp.totalFillLevelPercent > vehicle.cp.driveOnAtFillLevel then
driveOn = true;
end;
vehicle.cp.prevFillLevelPct = vehicle.cp.totalFillLevelPercent;
courseplay:setCustomTimer(vehicle, 'fillLevelChange', 7);
end;
if vehicle.cp.totalFillLevelPercent == 100 or driveOn then
vehicle.cp.prevFillLevelPct = nil;
courseplay:setIsLoaded(vehicle, true);
vehicle.cp.trailerFillDistance = nil;
vehicle.cp.currentTrailerToFill = nil;
vehicle.cp.tipperLoadMode = 0;
courseplay:changeRunCounter(vehicle, true)
return allowedToDrive;
end;
if currentTrailer.cp.realUnloadOrFillNode and vehicle.cp.trailerFillDistance then
if currentTrailer:getFreeCapacity(vehicle.cp.siloSelectedFillType) == 0
or currentTrailer.cp.currentSiloTrigger ~= nil and not (currentTrailer:getFreeCapacity(vehicle.cp.siloSelectedFillType) > 0 and #currentTrailer:getFillUnitsWithFillType(vehicle.cp.siloSelectedFillType) > 0) then
if vehicle.cp.numWorkTools > vehicle.cp.currentTrailerToFill then
vehicle.cp.currentTrailerToFill = vehicle.cp.currentTrailerToFill + 1;
else
vehicle.cp.prevFillLevelPct = nil;
courseplay:setIsLoaded(vehicle, true);
vehicle.cp.trailerFillDistance = nil;
vehicle.cp.currentTrailerToFill = nil;
vehicle.cp.tipperLoadMode = 0;
end;
else
if unloadDistance < vehicle.cp.trailerFillDistance then
allowedToDrive = false;
end;
end;
end;
-- normal mode if all tippers are empty
return allowedToDrive;
end
-- ##################################################
-- ##### UNLOADING TOOLS ##### --
function courseplay:unload_tippers(vehicle, allowedToDrive,dt)
local ctt = vehicle.cp.currentTipTrigger;
local takeOverSteering = false
if ctt.getTipDistanceFromTrailer == nil then
courseplay:debug(nameNum(vehicle) .. ": getTipDistanceFromTrailer function doesn't exist for currentTipTrigger - unloading function aborted", 2);
return allowedToDrive;
end
-- If we are less than 15 waypoints from the end point, don't reset the stopAtEndMode1 (We might be at the same silo as we are filling from)
if vehicle.cp.waypointIndex + 20 < vehicle.cp.numWaypoints then
vehicle.cp.stopAtEndMode1 = false;
end;
-- If the tipTrigger is full, then drive on to see if there is
local freeCapacity = 0;
if ctt.bunkerSilo then
freeCapacity = ctt.capacity - ctt.fillLevel;
else
for _, tipper in pairs(vehicle.cp.workTools) do
if ctt.animalHusbandry then
if ctt.animalHusbandry:getHasSpaceForTipping(tipper.cp.fillType) then
freeCapacity = 100
end
elseif tipper.tipReferencePoints ~= nil then
freeCapacity = freeCapacity + courseplay:getTipTriggerFreeCapacity(ctt, tipper.cp.fillType);
if tipper.cp.isTipping then
vehicle.cp.isTipping = true;
end;
end;
end;
end
if freeCapacity == 0 then
if vehicle.cp.isTipping then
if not courseplay.SiloIsFullMessageTimeOn then
courseplay:setCustomTimer(vehicle, 'SiloIsFullMessage', 5);
courseplay.SiloIsFullMessageTimeOn = true;
end;
if courseplay:timerIsThrough(vehicle, 'SiloIsFullMessage') then
courseplay:resetTipTrigger(vehicle)
courseplay:resetCustomTimer(vehicle, 'SiloIsFullMessage', true);
courseplay.SiloIsFullMessageTimeOn = nil;
vehicle.cp.isTipping = false;
if vehicle.cp.mode == 1 then
vehicle.cp.stopAtEndMode1 = true;
end
end;
CpManager:setGlobalInfoText(vehicle, 'FARM_SILO_IS_FULL')
else
courseplay:resetTipTrigger(vehicle)
courseplay:resetCustomTimer(vehicle, 'SiloIsFullMessage', true);
courseplay.SiloIsFullMessageTimeOn = nil;
if vehicle.cp.mode == 1 then
vehicle.cp.stopAtEndMode1 = true;
end
end;
return allowedToDrive;
end;
local isBGA = ctt.bunkerSilo ~= nil and vehicle.cp.handleAsOneSilo ~= true;
local bgaIsFull = isBGA and (ctt.fillLevel >= ctt.capacity);
if isBGA then
vehicle.cp.isBGATipping = true;
end;
for k, tipper in pairs(vehicle.cp.workTools) do
if tipper.tipReferencePoints ~= nil then
local allowedToDriveBackup = allowedToDrive;
local fillType = tipper.cp.fillType;
local distanceToTrigger, bestTipReferencePoint = 0,1;
if isBGA then
if tipper.cp.rearTipRefPoint and tipper.cp.rearTipRefPoint ~= bestTipReferencePoint then
bestTipReferencePoint = tipper.cp.rearTipRefPoint;
end;
distanceToTrigger, bestTipReferencePoint = ctt:getTipDistanceFromTrailer(tipper,bestTipReferencePoint);
else
distanceToTrigger, bestTipReferencePoint = ctt:getTipDistanceFromTrailer(tipper);
end
tipper.preferedTipReferencePointIndex = bestTipReferencePoint
local trailerInTipRange = false
if not isBGA then
trailerInTipRange = g_currentMission:getIsTrailerInTipRange(tipper, ctt, bestTipReferencePoint);
courseplay:debug(('%s: distanceToTrigger=%s, bestTipReferencePoint=%s -> trailerInTipRange=%s'):format(nameNum(vehicle), tostring(distanceToTrigger), tostring(bestTipReferencePoint), tostring(trailerInTipRange)), 2);
end
local goForTipping = false;
local unloadWhileReversing = false; -- Used by Reverse BGA Tipping
local isRePositioning = false; -- Used by Reverse BGA Tipping
if tipper.tipState == Trailer.TIPSTATE_CLOSED and vehicle.cp.keepOnTipping then
vehicle.cp.keepOnTipping = false
print("reset vehicle.cp.keepOnTipping")
end
--BGA TRIGGER
if isBGA and not bgaIsFull then
local stopAndGo = false;
-- Make sure we are using the rear TipReferencePoint as bestTipReferencePoint if possible.
-- Check if bestTipReferencePoint it's inversed
--[[if tipper.cp.inversedRearTipNode == nil then
local vx,vy,vz = getWorldTranslation(vehicle.rootNode)
local _,_,tz = worldToLocal(tipper.tipReferencePoints[bestTipReferencePoint].node, vx,vy,vz);
tipper.cp.inversedRearTipNode = tz < 0;
end;
]]
-- Local values used in both normal and reverse direction
local x,y,z = getWorldTranslation(tipper.tipReferencePoints[bestTipReferencePoint].node)
local tx,ty,tz = x,y,z+0.50
local x1,z1 = ctt.bunkerSiloArea.sx,ctt.bunkerSiloArea.sz
local x2,z2 = ctt.bunkerSiloArea.wx,ctt.bunkerSiloArea.wz
local x3,z3 = ctt.bunkerSiloArea.hx,ctt.bunkerSiloArea.hz
trailerInTipRange = Utils.hasRectangleLineIntersection2D(x1,z1,x2-x1,z2-z1,x3-x1,z3-z1,x,z,tx-x,tz-z)
local sx, sy, sz = worldToLocal(ctt.triggerStartId, x, y, z);
local ex, ey, ez = worldToLocal(ctt.triggerEndId, x, y, z);
local startDistance = Utils.vector2Length(sx, sz);
local endDistance = Utils.vector2Length(ex, ez);
courseplay:debug(('%s: startDistance=%s, endDistance=%s -> trailerInTipRange=%s'):format(nameNum(vehicle), tostring(startDistance), tostring(endDistance), tostring(trailerInTipRange)), 2);
--stop if we are not empty but near the end of the trigger
if tipper.cp.isTipping and (endDistance <1 or startDistance <1) then
allowedToDrive = false;
end
-------------------------------
--- Reverse into BGA and unload
-------------------------------
if vehicle.Waypoints[vehicle.cp.waypointIndex].rev or vehicle.cp.isReverseBGATipping then
if vehicle.cp.totalFillLevel > 0 then
if trailerInTipRange and ((startDistance > 8 and endDistance > 8) or vehicle.cp.keepOnTipping) then
goForTipping = true
allowedToDrive = false
if vehicle.cp.lastValidTipDistance and not vehicle.cp.keepOnTipping then
vehicle.cp.keepOnTipping = true
print("set vehicle.cp.keepOnTipping")
end
end
else
courseplay:setWaypointIndex(vehicle, courseplay:getNextFwdPoint(vehicle))
end
-------------------------------
--- Normal BGA unload
-------------------------------
else
if trailerInTipRange then
goForTipping = true
end
-- Get the animation
local animation = tipper.tipAnimations[bestTipReferencePoint];
local totalLength = abs(endDistance - startDistance)*0.9;
local fillDelta = vehicle.cp.totalFillLevel / vehicle.cp.totalCapacity;
local totalTipDuration = ((animation.dischargeEndTime - animation.dischargeStartTime) / animation.animationOpenSpeedScale) * fillDelta / 1000;
local meterPrSeconds = totalLength / totalTipDuration;
if stopAndGo then
meterPrSeconds = vehicle.cp.speeds.reverse * 1000;
end;
-- Slow down to real unload speed
if not vehicle.cp.backupUnloadSpeed and not stopAndGo then --and vectorDistance < 6*meterPrSeconds then
-- Calculate the unloading speed.
local refSpeed = meterPrSeconds * 3.6; -- * 0.90;
vehicle.cp.backupUnloadSpeed = vehicle.cp.speeds.reverse;
courseplay:changeReverseSpeed(vehicle, nil, refSpeed, true);
courseplay:debug(string.format("%s: BGA totalLength=%.2f, totalTipDuration%.2f, refSpeed=%.2f", nameNum(vehicle), totalLength, totalTipDuration, refSpeed), 2);
--print(string.format("%s: BGA totalLength=%.2f, totalTipDuration%.2f, refSpeed=%.2f", nameNum(vehicle), totalLength, totalTipDuration, refSpeed));
end;
end;
--BGA TIPTRIGGER BUT IS FULL AND WE ARE REVERSE TIPPING
elseif isBGA and bgaIsFull and (vehicle.Waypoints[vehicle.cp.waypointIndex].rev or vehicle.cp.isReverseBGATipping) then
-- Stop the vehicle, since we don't want to reverse into the BGA if it's full.
allowedToDrive = false;
-- Tell the user why we have stoped.
CpManager:setGlobalInfoText(vehicle, 'BGA_IS_FULL');
-- BGA TIPTRIGGER IS FULL
elseif isBGA and bgaIsFull and not vehicle.Waypoints[vehicle.cp.waypointIndex].rev and not vehicle.cp.isReverseBGATipping then
-- set trigger to nil
courseplay:resetTipTrigger(vehicle);
-- REGULAR TIPTRIGGER
elseif not isBGA then
if ctt.isAreaTrigger then
trailerInTipRange = g_currentMission.trailerTipTriggers[tipper] ~= nil