forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tippers.lua
1180 lines (1013 loc) · 52.4 KB
/
tippers.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;
-- ##### MANAGING TOOLS ##### --
function courseplay:attachImplement(implement)
--- Update Vehicle
local workTool = implement.object;
if workTool.attacherVehicle.cp.hasSpecializationSteerable then
workTool.attacherVehicle.cp.toolsDirty = true;
end;
courseplay:setAttachedCombine(self);
end;
function courseplay:detachImplement(implementIndex)
--- Update Vehicle
self.cp.toolsDirty = true;
end;
local origVehicleDetachImplement = Vehicle.detachImplement;
Vehicle.detachImplement = function(self, implementIndex, noEventSend)
-- don't allow detaching while CP is active
local tractor = self:getRootAttacherVehicle();
if tractor and tractor.hasCourseplaySpec and tractor.cp.isDriving and not noEventSend then -- if noEventSend == true, detachImplement has been called from Vehicle:delete() -> no need to abort
print('Courseplay warning: you need to stop Courseplay before detaching implements!');
return;
end;
origVehicleDetachImplement(self, implementIndex, noEventSend);
-- update attachCombineIndex and minHudPage
if self.hasCourseplaySpec and not noEventSend then -- if noEventSend == true, detachImplement has been called from Vehicle:delete() -> no need to set attachedCombine anymore
courseplay:setAttachedCombine(self);
end;
end;
function courseplay:reset_tools(vehicle)
vehicle.cp.workTools = {}
-- are there any tippers?
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.multiSiloSelectedFillType == Fillable.FILLTYPE_UNKNOWN or (vehicle.cp.multiSiloSelectedFillType ~= Fillable.FILLTYPE_UNKNOWN and not vehicle.cp.workTools[1].fillTypes[vehicle.cp.multiSiloSelectedFillType]) then
vehicle.cp.multiSiloSelectedFillType = vehicle.cp.workTools[1]:getFirstEnabledFillType();
end;
else
vehicle.cp.multiSiloSelectedFillType = Fillable.FILLTYPE_UNKNOWN;
end;
if vehicle.cp.hud.currentPage == 1 then
courseplay.hud:setReloadPageOrder(vehicle, 1, true);
end;
vehicle.cp.currentTrailerToFill = nil;
vehicle.cp.trailerFillDistance = nil;
vehicle.cp.toolsDirty = false;
end;
function courseplay:getNextFillableFillType(vehicle)
local workTool = vehicle.cp.workTools[1];
if vehicle.cp.multiSiloSelectedFillType == Fillable.FILLTYPE_UNKNOWN or vehicle.cp.multiSiloSelectedFillType == Fillable.NUM_FILLTYPES then
return workTool:getFirstEnabledFillType();
end;
local nextFillType, enabled = next(workTool.fillTypes, vehicle.cp.multiSiloSelectedFillType);
if nextFillType and enabled then
return nextFillType;
end;
return workTool:getFirstEnabledFillType();
end;
function courseplay:isCombine(workTool)
return (workTool.cp.hasSpecializationCombine or workTool.cp.hasSpecializationAICombine) and workTool.attachedCutters ~= nil and workTool.capacity > 0;
end;
function courseplay:isChopper(workTool)
return (workTool.cp.hasSpecializationCombine or workTool.cp.hasSpecializationAICombine) and workTool.attachedCutters ~= nil and workTool.capacity == 0 or courseplay:isSpecialChopper(workTool);
end;
function courseplay:isHarvesterSteerable(workTool)
return workTool.typeName == "selfPropelledPotatoHarvester" or workTool.cp.isGrimmeMaxtron620 or workTool.cp.isGrimmeTectron415;
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:isRoundbaler(workTool) -- is the tool a roundbaler?
return courseplay:isBaler(workTool) and (workTool.baleCloseAnimationName ~= nil and workTool.baleUnloadAnimationName ~= nil or courseplay:isSpecialRoundBaler(workTool));
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:isSprayer(workTool) -- is the tool a sprayer/spreader?
return workTool.cp.hasSpecializationSprayer or courseplay:isSpecialSprayer(workTool)
end;
function courseplay:isSowingMachine(workTool) -- is the tool a sowing machine?
return workTool.cp.hasSpecializationSowingMachine or courseplay:isSpecialSowingMachine(workTool);
end;
function courseplay:isFoldable(workTool) --is the tool foldable?
return workTool.cp.hasSpecializationFoldable or workTool.foldingParts ~= nil;
end;
function courseplay:isMower(workTool)
return workTool.cp.hasSpecializationMower or courseplay:isSpecialMower(workTool);
end;
function courseplay:isBigM(workTool)
return workTool.cp.hasSpecializationSteerable and courseplay:isMower(workTool);
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:isMixer(workTool)
return workTool.typeName == "selfPropelledMixerWagon" or (workTool.cp.hasSpecializationSteerable and workTool.cp.hasSpecializationMixerWagon)
end;
function courseplay:isFrontloader(workTool)
return workTool.cp.hasSpecializationCylindered and workTool.cp.hasSpecializationAnimatedVehicle and not workTool.cp.hasSpecializationShovel;
end;
function courseplay:isWheelloader(workTool)
return workTool.typeName:match("wheelLoader");
end;
function courseplay:isPushWagon(workTool)
return workTool.typeName:match("forageWagon") or workTool.cp.hasSpecializationSiloTrailer or workTool.cp.isPushWagon;
end;
function courseplay:isSpecialChopper(workTool)
return workTool.typeName == "woodCrusherTrailer" or workTool.cp.isPoettingerMex5
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;
courseplay:setNameVariable(workTool);
local hasWorkTool = false;
-- MODE 1 + 2: GRAIN TRANSPORT / COMBI MODE
if vehicle.cp.mode == 1 or vehicle.cp.mode == 2 then
if workTool.allowTipDischarge then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
end;
-- MODE 3: AUGERWAGON
elseif vehicle.cp.mode == 3 then
if workTool.cp.isAugerWagon then -- if workTool.cp.hasSpecializationTrailer then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
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 then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
courseplay:setMarkers(vehicle, workTool)
vehicle.cp.hasMachinetoFill = true;
vehicle.cp.noStopOnEdge = isSprayer;
vehicle.cp.noStopOnTurn = isSprayer;
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 reverse testing and only for developers!!!!
if isImplement and CpManager.isDeveloper 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 workTool.cp.isCaseIHPuma160
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,isImplement);
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;
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.getOverloadingTrailerInRangePipeState ~= nil or workTool.setIsReFilling ~= nil) then
hasWorkTool = true;
vehicle.cp.workTools[#vehicle.cp.workTools + 1] = workTool;
vehicle.cp.hasMachinetoFill = true
end;
-- 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;
end;
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
local implX,implY,implZ = getWorldTranslation(workTool.rootNode);
local _,_,tractorToImplZ = worldToLocal(vehicle.cp.DirectionNode, implX,implY,implZ);
vehicle.cp.aiBackMarker = nil; --TODO (Jakob): still needed?
if not vehicle.cp.aiTurnNoBackward and workTool.aiLeftMarker ~= nil and workTool.aiForceTurnNoBackward == true then
vehicle.cp.aiTurnNoBackward = true;
courseplay:debug(('%s: workTool.aiLeftMarker ~= nil and workTool.aiForceTurnNoBackward == true --> vehicle.cp.aiTurnNoBackward = true'):format(nameNum(workTool)), 6);
elseif not vehicle.cp.aiTurnNoBackward and workTool.aiLeftMarker == nil and #(workTool.wheels) > 0 and tractorToImplZ <= 0 then
vehicle.cp.aiTurnNoBackward = true;
courseplay:debug(('%s: workTool.aiLeftMarker == nil and #(workTool.wheels) > 0 and tractorToImplZ (%.2f) <= 0 --> vehicle.cp.aiTurnNoBackward = true'):format(nameNum(workTool), tractorToImplZ), 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 CpManager.isDeveloper then
-- New Turn Radius Calculation
courseplay:setAutoTurnDiameter(vehicle, hasWorkTool);
--[[else
-- Old Turn Radius Calculation
courseplay:setOldAutoTurnDiameter(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 = nil;
local foundTipRefOffset = false;
for i=1, vehicle.cp.numWorkTools do
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
local tipperX, tipperY, tipperZ = getWorldTranslation(vehicle.cp.workTools[i].rootNode);
if #(vehicle.cp.workTools[i].tipReferencePoints) > 1 then
vehicle.cp.workTools[i].cp.rearTipRefPoint = nil;
for n=1 ,#(vehicle.cp.workTools[i].tipReferencePoints) do
local tipRefPointX, tipRefPointY, tipRefPointZ = worldToLocal(vehicle.cp.workTools[i].tipReferencePoints[n].node, tipperX, tipperY, tipperZ);
tipRefPointX = abs(tipRefPointX);
if not foundTipRefOffset then
if (vehicle.cp.tipRefOffset == nil or vehicle.cp.tipRefOffset == 0) and tipRefPointX > 0.1 then
vehicle.cp.tipRefOffset = tipRefPointX;
foundTipRefOffset = true;
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.tipRefOffset = 0;
end;
end;
end;
end;
function courseplay:setMarkers(vehicle, object,isImplement)
local aLittleBitMore = 1;
object.cp.backMarkerOffset = nil
object.cp.aiFrontMarker = nil
-- 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 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
courseplay:debug(('%s: setMarkers(): %s has no workAreas -> return '):format(nameNum(vehicle), tostring(object.name)), 6);
return
end
for k = 1, tableLength do
for j,node in pairs(area[k]) do
if j == "start" or j == "height" or j == "width" then
local x, y, z = getWorldTranslation(node)
local _, _, ztt = worldToLocal(vehicle.cp.DirectionNode, x, y, z)
courseplay:debug(('%s:%s Point %s: ztt = %s'):format(nameNum(vehicle), tostring(object.name), tostring(j), tostring(ztt)), 6);
if object.cp.backMarkerOffset == nil or ztt > object.cp.backMarkerOffset then
object.cp.backMarkerOffset = ztt
end
if object.cp.aiFrontMarker == nil or ztt < object.cp.aiFrontMarker then
object.cp.aiFrontMarker = ztt
end
end
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;
end
if vehicle.cp.aiFrontMarker < -7 then
vehicle.aiToolExtraTargetMoveBack = abs(vehicle.cp.aiFrontMarker)
end
courseplay:debug(('%s: setMarkers(): turnEndBackDistance = %s, aiToolExtraTargetMoveBack = %s'):format(nameNum(vehicle), tostring(vehicle.turnEndBackDistance), tostring(vehicle.aiToolExtraTargetMoveBack)), 6);
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;
-- 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;
function courseplay:setOldAutoTurnDiameter(vehicle, hasWorkTool)
local sinAlpha = 0; -- Sinus vom Lenkwinkel
local wheelbase = 0; -- Radstand
local track = 0; -- Spurweite
local turnDiameter = 0; -- Wendekreis unbereinigt
local xerion = false
if vehicle.foundWheels == nil then
vehicle.foundWheels = {}
end
for i=1, #(vehicle.wheels) do
local wheel = vehicle.wheels[i]
if wheel.rotMax ~= 0 then
if vehicle.foundWheels[1] == nil then
sinAlpha = wheel.rotMax
vehicle.foundWheels[1] = wheel
elseif vehicle.foundWheels[2] == nil then
vehicle.foundWheels[2] = wheel
elseif vehicle.foundWheels[4] == nil then
vehicle.foundWheels[4] = wheel
end
elseif vehicle.foundWheels[3] == nil then
vehicle.foundWheels[3] = wheel
end
end
if vehicle.foundWheels[3] == nil then --Xerion and Co
sinAlpha = sinAlpha *2
xerion = true
end
if #(vehicle.foundWheels) == 3 or xerion then
local wh1X, wh1Y, wh1Z = getWorldTranslation(vehicle.foundWheels[1].driveNode);
local wh2X, wh2Y, wh2Z = getWorldTranslation(vehicle.foundWheels[2].driveNode);
local wh3X, wh3Y, wh3Z = 0,0,0
if xerion then
wh3X, wh3Y, wh3Z = getWorldTranslation(vehicle.foundWheels[4].driveNode);
else
wh3X, wh3Y, wh3Z = getWorldTranslation(vehicle.foundWheels[3].driveNode);
end
track = courseplay:distance(wh1X, wh1Z, wh2X, wh2Z)
wheelbase = courseplay:distance(wh1X, wh1Z, wh3X, wh3Z)
turnDiameter = 2*wheelbase/sinAlpha+track
vehicle.foundWheels = {}
else
turnDiameter = vehicle.cp.turnDiameter -- Kasi and Co are not supported. Nobody does hauling with a Kasi or Quadtrack !!!
end;
if hasWorkTool and (vehicle.cp.mode == 2 or vehicle.cp.mode == 3 or vehicle.cp.mode == 4 or vehicle.cp.mode == 6) then --TODO (Jakob): I've added modes 3, 4 & 6 - needed?
vehicle.cp.turnDiameterAuto = turnDiameter;
--print(string.format("vehicle.cp.workTools[1].sizeLength = %s turnDiameter = %s", tostring(vehicle.cp.workTools[1].sizeLength),tostring( turnDiameter)))
if vehicle.cp.numWorkTools == 1 and vehicle.cp.workTools[1].attacherVehicle ~= vehicle and (vehicle.cp.workTools[1].sizeLength > turnDiameter) then
vehicle.cp.turnDiameterAuto = vehicle.cp.workTools[1].sizeLength;
end;
if (vehicle.cp.numWorkTools > 1) then
vehicle.cp.turnDiameterAuto = turnDiameter * 1.5;
end
end;
if vehicle.cp.turnDiameterAutoMode then
vehicle.cp.turnDiameter = vehicle.cp.turnDiameterAuto;
if abs(vehicle.cp.turnDiameter) > 50 then
vehicle.cp.turnDiameter = 15
end
end;
end
--##################################################
-- ##### LOADING TOOLS ##### --
function courseplay:load_tippers(vehicle, allowedToDrive)
local cx, cz = vehicle.Waypoints[2].cx, vehicle.Waypoints[2].cz;
if vehicle.cp.currentTrailerToFill == nil then
vehicle.cp.currentTrailerToFill = 1;
end
local currentTrailer = vehicle.cp.workTools[vehicle.cp.currentTrailerToFill];
if not vehicle.cp.trailerFillDistance then
if courseplay:isMixer(currentTrailer) and not currentTrailer.cp.realUnloadOrFillNode then
currentTrailer.cp.realUnloadOrFillNode = courseplay:getRealUnloadOrFillNode(currentTrailer);
end;
if not currentTrailer.cp.realUnloadOrFillNode then
return allowedToDrive;
end;
local _,y,_ = getWorldTranslation(currentTrailer.cp.realUnloadOrFillNode);
local _,_,z = worldToLocal(currentTrailer.cp.realUnloadOrFillNode, cx, y, cz);
vehicle.cp.trailerFillDistance = z;
end;
-- MultiSiloTrigger (Giants)
if currentTrailer.cp.currentMultiSiloTrigger ~= nil then
local acceptedFillType = false;
local mst = currentTrailer.cp.currentMultiSiloTrigger;
for _, fillType in pairs(mst.fillTypes) do
if fillType == vehicle.cp.multiSiloSelectedFillType then
acceptedFillType = true;
break;
end;
end;
if acceptedFillType then
local siloIsEmpty = g_currentMission.missionStats.farmSiloAmounts[vehicle.cp.multiSiloSelectedFillType] <= 1;
if not mst.isFilling and not siloIsEmpty and (currentTrailer.currentFillType == Fillable.FILLTYPE_UNKNOWN or currentTrailer.currentFillType == vehicle.cp.multiSiloSelectedFillType) then
mst:startFill(vehicle.cp.multiSiloSelectedFillType);
courseplay:debug(('%s: MultiSiloTrigger: selectedFillType = %s, isFilling = %s'):format(nameNum(vehicle), tostring(Fillable.fillTypeIntToName[mst.selectedFillType]), tostring(mst.isFilling)), 2);
elseif siloIsEmpty 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
local driveOn = false;
if courseplay:timerIsThrough(vehicle, 'fillLevelChange') or vehicle.cp.prevFillLevelPct == nil then
if vehicle.cp.prevFillLevelPct ~= nil and vehicle.cp.tipperFillLevelPct == vehicle.cp.prevFillLevelPct and vehicle.cp.tipperFillLevelPct > vehicle.cp.driveOnAtFillLevel then
driveOn = true;
end;
vehicle.cp.prevFillLevelPct = vehicle.cp.tipperFillLevelPct;
courseplay:setCustomTimer(vehicle, 'fillLevelChange', 7);
end;
if vehicle.cp.tipperFillLevelPct == 100 or driveOn then
vehicle.cp.prevFillLevelPct = nil;
courseplay:setIsLoaded(vehicle, true);
vehicle.cp.trailerFillDistance = nil;
vehicle.cp.currentTrailerToFill = nil;
return allowedToDrive;
end;
if currentTrailer.cp.realUnloadOrFillNode and vehicle.cp.trailerFillDistance then
if currentTrailer.fillLevel == currentTrailer.capacity
or currentTrailer.cp.currentMultiSiloTrigger ~= nil and not (currentTrailer.currentFillType == Fillable.FILLTYPE_UNKNOWN or currentTrailer.currentFillType == vehicle.cp.multiSiloSelectedFillType) 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;
end;
else
local _,y,_ = getWorldTranslation(currentTrailer.cp.realUnloadOrFillNode);
local _,_,vectorDistanceZ = worldToLocal(currentTrailer.cp.realUnloadOrFillNode, cx, y, cz);
if vectorDistanceZ < 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)
local ctt = vehicle.cp.currentTipTrigger;
if ctt.getTipDistanceFromTrailer == nil then
courseplay:debug(nameNum(vehicle) .. ": getTipDistanceFromTrailer function doesn't exist for currentTipTrigger - unloading function aborted", 2);
return allowedToDrive;
end;
local isBGA = ctt.bunkerSilo ~= nil and ctt.bunkerSilo.movingPlanes ~= nil and vehicle.cp.handleAsOneSilo ~= true;
local bgaIsFull = isBGA and (ctt.bunkerSilo.fillLevel >= ctt.bunkerSilo.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 fruitType = tipper.currentFillType
local distanceToTrigger, bestTipReferencePoint = ctt:getTipDistanceFromTrailer(tipper);
local 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);
local goForTipping = false;
local unloadWhileReversing = false; -- Used by Reverse BGA Tipping
local isRePositioning = false; -- Used by Reverse BGA Tipping
--BGA TRIGGER
if isBGA and not bgaIsFull then
if vehicle.cp.handleAsOneSilo == nil then
local length = 0;
for i = 1, #ctt.bunkerSilo.movingPlanes-1, 1 do
local x, y, z = getWorldTranslation(ctt.bunkerSilo.movingPlanes[i].nodeId);
local lx, _, lz = worldToLocal(ctt.bunkerSilo.movingPlanes[i+1].nodeId, x, y, z);
length = length + Utils.vector2Length(lx, lz);
end;
length = length / #ctt.bunkerSilo.movingPlanes;
if length < 0.5 then
vehicle.cp.handleAsOneSilo = true;
else
vehicle.cp.handleAsOneSilo = false;
end;
courseplay:debug(('%s: Median Silo Section Distance = %s, handleAsOneSilo = %s'):format(nameNum(vehicle), tostring(courseplay:round(length, 1)), tostring(vehicle.cp.handleAsOneSilo)), 13);
if vehicle.cp.handleAsOneSilo == true then return allowedToDrive; end;
end
local stopAndGo = false;
-- Make sure we are using the rear TipReferencePoint as bestTipReferencePoint if possible.
if tipper.cp.rearTipRefPoint and tipper.cp.rearTipRefPoint ~= bestTipReferencePoint then
bestTipReferencePoint = tipper.cp.rearTipRefPoint;
trailerInTipRange = g_currentMission:getIsTrailerInTipRange(tipper, ctt, bestTipReferencePoint);
end;
-- 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 silos = #ctt.bunkerSilo.movingPlanes;
local x, y, z = getWorldTranslation(tipper.tipReferencePoints[bestTipReferencePoint].node);
local sx, sy, sz = worldToLocal(ctt.bunkerSilo.movingPlanes[1].nodeId, x, y, z);
local ex, ey, ez = worldToLocal(ctt.bunkerSilo.movingPlanes[silos].nodeId, x, y, z);
local startDistance = Utils.vector2Length(sx, sz);
local endDistance = Utils.vector2Length(ex, ez);
-- Get nearest silo section number (Code snip taken from BunkerSilo:setFillDeltaAt)
local nearestDistance = math.huge;
local nearestBGASection = 1;
for i, movingPlane in pairs(ctt.bunkerSilo.movingPlanes) do
local wx, _, wz = getWorldTranslation(movingPlane.nodeId);
local distance = Utils.vector2Length(wx - x, wz - z);
if nearestDistance > distance then
nearestBGASection = i;
nearestDistance = distance;
end;
end;
-------------------------------
--- Reverse into BGA and unload
-------------------------------
if vehicle.Waypoints[vehicle.recordnumber].rev or vehicle.cp.isReverseBGATipping then
-- Get the silo section fill level based on how many sections and total capacity.
local medianSiloCapacity = ctt.bunkerSilo.capacity / silos * 0.92; -- we make it a bit smaler than it actually is, since it will still unload a bit to the silo next to it.
-- Find what BGA silo section to unload in if not found
if not tipper.cp.BGASelectedSection then
vehicle.cp.BGASectionInverted = false;
-- Find out what end to start at.
if startDistance > endDistance then
tipper.cp.BGASelectedSection = 1;
else
tipper.cp.BGASelectedSection = silos;
vehicle.cp.BGASectionInverted = true;
end;
-- Find which section to unload into.
while (vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection > 1) or (not vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection < silos) do
if ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].fillLevel < medianSiloCapacity then
break;
end;
if vehicle.cp.BGASectionInverted then
tipper.cp.BGASelectedSection = tipper.cp.BGASelectedSection - 1;
else
tipper.cp.BGASelectedSection = tipper.cp.BGASelectedSection + 1;
end;
end;
courseplay:debug(string.format("%s: BGA selected silo section: %d - Is inverted order: %s", nameNum(vehicle), tipper.cp.BGASelectedSection, tostring(vehicle.cp.BGASectionInverted)), 13);
end;
-- Check if last silo section.
local isLastSiloSection = (vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection == 1) or (not vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection == silos);
-- Start unloading some silo section before main silo section to make a ramp for the trailer.
if not isLastSiloSection and nearestBGASection ~= tipper.cp.BGASelectedSection then
local unloadNumOfSectionBefore = 6;
local sectionMaxFillLevel = 0;
local isInUnloadSection = false;
-- INVERTED SILO DIRECTION
if (vehicle.cp.BGASectionInverted and nearestBGASection >= tipper.cp.BGASelectedSection - unloadNumOfSectionBefore) and nearestBGASection < tipper.cp.BGASelectedSection then
-- recalculate the num of section before, in case the num of section left is less.
unloadNumOfSectionBefore = min(tipper.cp.BGASelectedSection - 1,unloadNumOfSectionBefore);
-- Calculate the current section max fill level.
sectionMaxFillLevel = (unloadNumOfSectionBefore - max(tipper.cp.BGASelectedSection - nearestBGASection, 0) + 1) * ((medianSiloCapacity * 0.5) / unloadNumOfSectionBefore);
isInUnloadSection = true;
-- NORMAL SILO DIRECTION
elseif (not vehicle.cp.BGASectionInverted and nearestBGASection <= tipper.cp.BGASelectedSection + unloadNumOfSectionBefore) and nearestBGASection > tipper.cp.BGASelectedSection then
-- recalculate the num of section before, in case the num of section left is less.
unloadNumOfSectionBefore = min(silos - tipper.cp.BGASelectedSection ,unloadNumOfSectionBefore)
-- Calculate the current section max fill level.
sectionMaxFillLevel = (unloadNumOfSectionBefore - max(nearestBGASection - tipper.cp.BGASelectedSection, 0) + 1) * ((medianSiloCapacity * 0.5) / unloadNumOfSectionBefore);
isInUnloadSection = true;
end;
if ctt.bunkerSilo.movingPlanes[nearestBGASection].fillLevel < sectionMaxFillLevel then
-- Get a vector distance, to make a more precise distance check.
local xmp, _, zmp = getWorldTranslation(ctt.bunkerSilo.movingPlanes[nearestBGASection].nodeId);
local _, _, vectorDistance = worldToLocal(tipper.tipReferencePoints[bestTipReferencePoint].node, xmp, y, zmp);
goForTipping = trailerInTipRange and vectorDistance > -2.5;
unloadWhileReversing = true;
elseif isInUnloadSection and tipper.tipState ~= Trailer.TIPSTATE_CLOSING and tipper.tipState ~= Trailer.TIPSTATE_CLOSED then
tipper:toggleTipState();
tipper.cp.isTipping = false;
courseplay:debug(string.format("%s: Ramp(%d) fill level is at max. Waiting with unloading.]", nameNum(vehicle), nearestBGASection), 13);
end;
end;
-- Get a vector distance, to make a more precise distance check.
local xmp, _, zmp = getWorldTranslation(ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].nodeId);
local _, _, vectorDistance = worldToLocal(tipper.tipReferencePoints[bestTipReferencePoint].node, xmp, y, zmp);
if tipper.cp.inversedRearTipNode then vectorDistance = vectorDistance * -1 end;
-- If we drive too far, then change direction and try to replace again.
if not isLastSiloSection and (vectorDistance > 1 or vectorDistance < -1) and nearestBGASection ~= tipper.cp.BGASelectedSection then
local isChangingDirection = false;
if vectorDistance > 0 and vehicle.Waypoints[vehicle.recordnumber].rev then
-- Change direction to forward
vehicle.cp.isReverseBGATipping = true;
isChangingDirection = true;
courseplay:setRecordNumber(vehicle, courseplay:getNextFwdPoint(vehicle));
elseif vectorDistance < 0 and not vehicle.Waypoints[vehicle.recordnumber].rev then
-- Change direction to reverse
local found = false;
for i = vehicle.recordnumber, 1, -1 do
if vehicle.Waypoints[i].rev then
courseplay:setRecordNumber(vehicle, i);
found = true;
end;
end;
if found then
vehicle.cp.isReverseBGATipping = false;
isChangingDirection = true;
end;
end;
if isChangingDirection then
courseplay:debug(string.format("%s: Changed direction to %s to try reposition again.]", nameNum(vehicle), vehicle.Waypoints[vehicle.recordnumber].rev and "reverse" or "forward"), 13);
end;
end;
-- Make sure we drive to the middle of the next silo section before stopping again.
if vehicle.cp.isReverseBGATipping and vectorDistance > 0 then
courseplay:debug(string.format("%s: Moving to the middle of silo section %d - current distance: %.3fm]", nameNum(vehicle), tipper.cp.BGASelectedSection, vectorDistance), 13);
-- Unload if inside the selected section
elseif trailerInTipRange and nearestBGASection == tipper.cp.BGASelectedSection then
goForTipping = trailerInTipRange and vectorDistance > -2.5;
end;
-- Goto the next silo section if this one is filled and not last silo section.
if not isLastSiloSection and ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].fillLevel >= medianSiloCapacity then
-- Make sure we run this script even that we are not in an reverse waypoint anymore.
vehicle.cp.isReverseBGATipping = true;
-- Find next section to unload into.
while (vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection > 1) or (not vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection < silos) do
if ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].fillLevel < medianSiloCapacity then
break;
end;
if vehicle.cp.BGASectionInverted then
tipper.cp.BGASelectedSection = tipper.cp.BGASelectedSection - 1;
else
tipper.cp.BGASelectedSection = tipper.cp.BGASelectedSection + 1;
end;
end;
-- Find the first forward waypoint ahead of the vehicle so we can drive ahead to the next silo section.
courseplay:setRecordNumber(vehicle, courseplay:getNextFwdPoint(vehicle));
courseplay:debug(string.format("%s: New BGA silo section: %d", nameNum(vehicle), tipper.cp.BGASelectedSection), 13);
elseif isLastSiloSection and goForTipping then
-- Make sure we run this script even that we are not in an reverse waypoint anymore.
vehicle.cp.isReverseBGATipping = true;
-- Make sure that we don't reverse into the silo after it's full
courseplay:setRecordNumber(vehicle, courseplay:getNextFwdPoint(vehicle));
end;
-------------------------------
--- Normal BGA unload
-------------------------------
else
-- Get the silo section fill level based on how many sections and total capacity.
local medianSiloCapacity = ctt.bunkerSilo.capacity / silos;
-- Get the animation
local animation = tipper.tipAnimations[bestTipReferencePoint];
local totalLength = abs(endDistance - startDistance);
local fillDelta = vehicle.cp.tipperFillLevel / vehicle.cp.tipperCapacity;
local totalTipDuration = ((animation.dischargeEndTime - animation.dischargeStartTime) / animation.animationOpenSpeedScale) * fillDelta / 1000;
local meterPrSeconds = totalLength / totalTipDuration;
if stopAndGo then
meterPrSeconds = vehicle.cp.speeds.unload * 1000;
end;
-- Find what BGA silo section to unload in if not found
if not tipper.cp.BGASelectedSection then
local fillLevel = ctt.bunkerSilo.fillLevel;
if ctt.bunkerSilo.cpTempFillLevel then fillLevel = ctt.bunkerSilo.cpTempFillLevel end;
vehicle.cp.bunkerSiloSectionFillLevel = min((fillLevel + (vehicle.cp.tipperFillLevel * 0.9))/silos, medianSiloCapacity);
courseplay:debug(string.format("%s: Max allowed fill level pr. section = %.2f", nameNum(vehicle), vehicle.cp.bunkerSiloSectionFillLevel), 2);
vehicle.cp.BGASectionInverted = false;
ctt.bunkerSilo.cpTempFillLevel = fillLevel + vehicle.cp.tipperFillLevel;
courseplay:debug(string.format("%s: cpTempFillLevel = %.2f", nameNum(vehicle), ctt.bunkerSilo.cpTempFillLevel), 2);
-- Find out what end to start at.
if startDistance < endDistance then
tipper.cp.BGASelectedSection = 1;
else
tipper.cp.BGASelectedSection = silos;
vehicle.cp.BGASectionInverted = true;
end;
-- Find which section to unload into.
while (vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection < silos) or (not vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection > 1) do
if ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].fillLevel < vehicle.cp.bunkerSiloSectionFillLevel then
break;
end;
if vehicle.cp.BGASectionInverted then
tipper.cp.BGASelectedSection = tipper.cp.BGASelectedSection - 1;
else
tipper.cp.BGASelectedSection = tipper.cp.BGASelectedSection + 1;
end;
end;
courseplay:debug(string.format("%s: BGA selected silo section: %d - Is inverted order: %s", nameNum(vehicle), tipper.cp.BGASelectedSection, tostring(vehicle.cp.BGASectionInverted)), 2);
end;
if not vehicle.cp.bunkerSiloSectionFillLevel then
courseplay:resetTipTrigger(vehicle);
return allowedToDrive;
end;
local isLastSiloSection = (vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection == 1) or (not vehicle.cp.BGASectionInverted and tipper.cp.BGASelectedSection == silos);
-- Get a vector distance, to make a more precise distance check.
local xmp, _, zmp = getWorldTranslation(ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].nodeId);
local _, _, vectorDistance = worldToLocal(tipper.tipReferencePoints[bestTipReferencePoint].node, xmp, y, zmp);
local isOpen = tipper:getCurrentTipAnimationTime() >= animation.animationDuration;
if not isLastSiloSection and ctt.bunkerSilo.movingPlanes[tipper.cp.BGASelectedSection].fillLevel >= vehicle.cp.bunkerSiloSectionFillLevel then
if tipper.tipState ~= Trailer.TIPSTATE_CLOSING and tipper.tipState ~= Trailer.TIPSTATE_CLOSED then
tipper:toggleTipState();
tipper.cp.isTipping = false;