This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
/
vehicles.lua
1423 lines (1227 loc) · 56.1 KB
/
vehicles.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, max, rad, deg, cos, sin, ceil = math.abs, math.max, math.rad, math.deg, math.cos, math.sin, math.ceil;
local _;
local truckAttacherJoint = {};
-- ##### VEHICLE TOOLS ##### --
courseplay.attacherJointNodeRotationList = {};
function courseplay:calculateTurnRadius(type, wheelBase, rotMax, CPRatio)
local turnRadius = 0;
CPRatio = CPRatio or 0;
-- ArticulatedAxis Steering
if (type == "ASW" or type == "Tool") and CPRatio > 0 and CPRatio < 1 then
if CPRatio <= 0.5 then
turnRadius = wheelBase * (1-CPRatio) / sin(rotMax) * (CPRatio * cos(rotMax) + 1 - CPRatio);
else
turnRadius = wheelBase * CPRatio / sin(rotMax) * (CPRatio + (1 - CPRatio) * cos(rotMax));
end;
-- 4 Wheel Steering
elseif type == "4WS" then
turnRadius = wheelBase / (2 * sin(rotMax)) * cos(rotMax);
-- 2 Wheel Steering
elseif (type == "2WS" or type == "Tool") then
turnRadius = wheelBase / sin(rotMax) * cos(rotMax);
end;
return turnRadius;
end;
function courseplay:createNewLinkedNode(object, nodeName, linkToNode)
if not object.cp.notesToDelete then object.cp.notesToDelete = {}; end;
local node = createTransformGroup(nodeName);
link(linkToNode, node);
table.insert(object.cp.notesToDelete, 1, node);
return node;
end;
--- courseplay:findJointNodeConnectingToNode(workTool, fromNode, toNode, doReverse)
-- Returns: (node, backtrack, rotLimits)
-- node will return either: 1. The jointNode that connects to the toNode,
-- 2. The toNode if no jointNode is found but the fromNode is inside the same component as the toNode
-- 3. nil in case none of the above fails.
-- backTrack will return either: 1. A table of all the jointNodes found from fromNode to toNode, if the jointNode that connects to the toNode is found.
-- 2: nil if no jointNode is found.
-- rotLimits will return either: 1. A table of all the rotLimits of the componentJoint, found from fromNode to toNode, if the jointNode that connects to the toNode is found.
-- 2: nil if no jointNode is found.
function courseplay:findJointNodeConnectingToNode(workTool, fromNode, toNode, doReverse)
if fromNode == toNode then return toNode; end;
-- Attempt to find the jointNode by backtracking the compomentJoints.
for index, component in ipairs(workTool.components) do
if courseplay:isPartOfNode(fromNode, component.node) then
if not doReverse then
for _, joint in ipairs(workTool.componentJoints) do
if joint.componentIndices[2] == index then
if workTool.components[joint.componentIndices[1]].node == toNode then
-- node backtrack rotLimits
return joint.jointNode, {joint.jointNode}, {joint.rotLimit};
else
local node, backTrack, rotLimits = courseplay:findJointNodeConnectingToNode(workTool, workTool.components[joint.componentIndices[1]].node, toNode);
if backTrack then table.insert(backTrack, 1, joint.jointNode); end;
if rotLimits then table.insert(rotLimits, 1, joint.rotLimit); end;
return node, backTrack, rotLimits;
end;
end;
end;
end;
-- Do Reverse in case not found
for _, joint in ipairs(workTool.componentJoints) do
if joint.componentIndices[1] == index then
if workTool.components[joint.componentIndices[2]].node == toNode then
-- node backtrack rotLimits
return joint.jointNode, {joint.jointNode}, {joint.rotLimit};
else
local node, backTrack, rotLimits = courseplay:findJointNodeConnectingToNode(workTool, workTool.components[joint.componentIndices[2]].node, toNode, true);
if backTrack then table.insert(backTrack, 1, joint.jointNode); end;
if rotLimits then table.insert(rotLimits, 1, joint.rotLimit); end;
return node, backTrack, rotLimits;
end;
end;
end;
end;
end;
-- Last attempt to find the jointNode by getting parent of parent untill hit or the there is no more parents.
if courseplay:isPartOfNode(fromNode, toNode) then
return toNode, nil;
end;
-- If anything else fails, return nil
return nil, nil;
end;
function courseplay:getDistances(object)
if not object.cp.distances then
courseplay.debugLine(courseplay.DBG_IMPLEMENTS);
local distances = {};
-- STEERABLES
if object.cp.directionNode then
-- Finde the front and rear distance from the direction node
local front, rear = 0, 0;
local haveRunnedOnce = false
for _, wheel in ipairs(object:getWheels()) do
local wdnrxTemp, wdnryTemp, wdnrzTemp = getRotation(wheel.driveNode);
setRotation(wheel.driveNode, 0, 0, 0);
local wreprxTemp, wrepryTemp, wreprzTemp = getRotation(wheel.repr);
setRotation(wheel.repr, 0, 0, 0);
local xw, yw, zw = getWorldTranslation(wheel.driveNode);
local _,_,dis = worldToLocal(object.cp.directionNode, xw, yw, zw);
setRotation(wheel.repr, wreprxTemp, wrepryTemp, wreprzTemp);
setRotation(wheel.driveNode, wdnrxTemp, wdnryTemp, wdnrzTemp);
if haveRunnedOnce then
if dis < rear then rear = dis; end;
if dis > front then front = dis; end;
else
rear = dis;
front = dis;
haveRunnedOnce = true;
end;
end;
-- Set the wheel offset anddistance
distances.frontWheelToDirectionNodeOffset = front * -1;
distances.frontWheelToRearWheel = abs(front - rear);
courseplay:debug(('%s: frontWheelToDirectionNodeOffset=%.2f, frontWheelToRearWheel=%.2f'):format(nameNum(object), distances.frontWheelToDirectionNodeOffset, distances.frontWheelToRearWheel), courseplay.DBG_IMPLEMENTS);
-- Finde the attacherJoints distance from the direction node
for _, attacherJoint in ipairs(object.spec_attacherJoints.attacherJoints) do
local xj, yj, zj = getWorldTranslation(attacherJoint.jointTransform);
local _,_,dis = worldToLocal(object.cp.directionNode, xj, yj, zj);
if dis < front then
if not distances.frontWheelToRearTrailerAttacherJoints then
distances.frontWheelToRearTrailerAttacherJoints = {};
end;
distances.frontWheelToRearTrailerAttacherJoints[attacherJoint.jointType] = abs(front - dis);
courseplay:debug(('%s: frontWheelToRearTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.frontWheelToRearTrailerAttacherJoints[attacherJoint.jointType]), courseplay.DBG_IMPLEMENTS);
end;
end
-- Finde the attacherJoints distance from the turning node
local turningNode = courseplay:getRealTurningNode(object);
for _, attacherJoint in ipairs(object.spec_attacherJoints.attacherJoints) do
local xj, yj, zj = getWorldTranslation(attacherJoint.jointTransform);
local _, _, deltaZ = worldToLocal(object.cp.directionNode, xj, yj, zj);
-- If we are behind the front wheel, then it should be an attacherJoing on the rear
if deltaZ < front then
local _,_,dis = worldToLocal(turningNode, xj, yj, zj);
dis = dis * -1;
if not distances.turningNodeToRearTrailerAttacherJoints then
distances.turningNodeToRearTrailerAttacherJoints = {};
end;
distances.turningNodeToRearTrailerAttacherJoints[attacherJoint.jointType] = dis;
courseplay:debug(('%s: turningNodeToRearTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.turningNodeToRearTrailerAttacherJoints[attacherJoint.jointType]), courseplay.DBG_IMPLEMENTS);
end;
end
-- IMPLEMENTS OR TRAILERS
else
local activeInputAttacherJoint = object:getActiveInputAttacherJoint();
local node = activeInputAttacherJoint.node;
local isHookLift = courseplay:isHookLift(object);
local lastNode = courseplay:getLastComponentNodeWithWheels(object)
if activeInputAttacherJoint.rootNode ~= lastNode and not isHookLift then
local tempNode, backTrack, rotLimits = courseplay:findJointNodeConnectingToNode(object, activeInputAttacherJoint.rootNode, lastNode);
if tempNode and backTrack then
node = tempNode;
local nodeLength = 0;
local isPivoted = false;
for i = 1, #backTrack do
if rotLimits ~= nil and rotLimits[i]~= nil and rotLimits[i][2] ~= nil and rotLimits[i][2] > rad(15) then
isPivoted = true;
end;
if i == 1 then
tempNode = activeInputAttacherJoint.node;
else
tempNode = backTrack[i-1];
end;
local tmpnx, tmpny, tmpnz = getWorldTranslation(tempNode);
local _,_,dis = worldToLocal(backTrack[i], tmpnx, tmpny, tmpnz);
courseplay:debug(('%s: backTrack[%d](node: %s) Length = %.2f'):format(nameNum(object), i, tostring(backTrack[i]), abs(dis)), courseplay.DBG_IMPLEMENTS);
nodeLength = nodeLength + abs(dis);
end;
if isPivoted then
distances.attacherJointToPivot = nodeLength;
courseplay:debug(('%s: attacherJointToPivot=%.2f'):format(nameNum(object), distances.attacherJointToPivot), courseplay.DBG_IMPLEMENTS);
else
distances.attacherJointToLastMovingPart = nodeLength;
courseplay:debug(('%s: attacherJointToLastMovingPart=%.2f'):format(nameNum(object), distances.attacherJointToLastMovingPart), courseplay.DBG_IMPLEMENTS);
end;
end;
end;
-- backup node rotation and set the rotation to 0
local nodeXTemp, nodeYTemp, nodeZTemp = getRotation(node);
setRotation(node, 0, 0, 0);
-- Find the distance from attacherJoint to rear wheel
local objectWheels = object:getWheels();
if objectWheels and #objectWheels > 0 and not isHookLift then
local length = 0;
for _, wheel in ipairs(objectWheels) do
if wheel.maxLatStiffnessLoad > 0.5 then
local nx, ny, nz = getWorldTranslation(wheel.driveNode);
local _,_,dis = worldToLocal(node, nx, ny, nz);
if abs(dis) > length then
length = abs(dis);
end;
end;
end;
if distances.attacherJointToPivot then
distances.pivotToRearWheel = length;
distances.attacherJointToRearWheel = distances.attacherJointToPivot + length;
elseif distances.attacherJointToLastMovingPart then
distances.attacherJointToRearWheel = distances.attacherJointToLastMovingPart + length;
else
distances.attacherJointToRearWheel = length;
end;
courseplay:debug(('%s: attacherJointToRearWheel=%.2f'):format(nameNum(object), distances.attacherJointToRearWheel), courseplay.DBG_IMPLEMENTS);
end;
-- Finde the attacherJoints distance from the direction node
for _, attacherJoint in ipairs(object.spec_attacherJoints.attacherJoints) do
local nx, ny, nz = getWorldTranslation(attacherJoint.jointTransform);
local _,_,dis = worldToLocal(node, nx, ny, nz);
dis = dis * -1;
if dis > 0 then
if not distances.attacherJointToRearTrailerAttacherJoints then
distances.attacherJointToRearTrailerAttacherJoints = {};
end;
if distances.attacherJointToPivot then
if not distances.pivotToRearTrailerAttacherJoints then
distances.pivotToRearTrailerAttacherJoints = {};
end;
distances.pivotToRearTrailerAttacherJoints[attacherJoint.jointType] = dis;
distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType] = distances.attacherJointToPivot + dis;
elseif distances.attacherJointToLastMovingPart then
distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType] = distances.attacherJointToLastMovingPart + dis;
else
distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType] = dis;
end;
courseplay:debug(('%s: attacherJointToRearTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.attacherJointToRearTrailerAttacherJoints[attacherJoint.jointType]), courseplay.DBG_IMPLEMENTS);
end;
end;
if distances.attacherJointToRearWheel then
local turningNode = courseplay:getRealTurningNode(object);
-- Finde the attacherJoints distance from the turning node
for _, attacherJoint in ipairs(object.spec_attacherJoints.attacherJoints) do
local nx, ny, nz = getWorldTranslation(attacherJoint.jointTransform);
local _,_,dis = worldToLocal(turningNode, nx, ny, nz);
dis = dis * -1;
if not distances.turningNodeToTrailerAttacherJoints then
distances.turningNodeToTrailerAttacherJoints = {};
end;
distances.turningNodeToTrailerAttacherJoints[attacherJoint.jointType] = dis;
courseplay:debug(('%s: turningNodeToTrailerAttacherJoints[%d]=%.2f'):format(nameNum(object), attacherJoint.jointType, distances.turningNodeToTrailerAttacherJoints[attacherJoint.jointType]), courseplay.DBG_IMPLEMENTS);
end;
-- Finde the attacherJoint/Pivot distance to the turning node
local nx, ny, nz = getWorldTranslation(node);
local _,_,dis = worldToLocal(turningNode, nx, ny, nz);
if distances.attacherJointToLastMovingPart then
distances.attacherJointOrPivotToTurningNode = distances.attacherJointToLastMovingPart + dis;
else
distances.attacherJointOrPivotToTurningNode = dis;
end;
courseplay:debug(('%s: attacherJointOrPivotToTurningNode=%.2f'):format(nameNum(object), distances.attacherJointOrPivotToTurningNode), courseplay.DBG_IMPLEMENTS);
end;
-- restore node rotation from backup.
setRotation(node, nodeXTemp, nodeYTemp, nodeZTemp);
end;
object.cp.distances = distances;
end;
return object.cp.distances;
end;
function courseplay:getDirectionNodeToTurnNodeLength(vehicle)
--- This is in case vehicle is a tool and CP havent been set on it
if not vehicle.cp then
vehicle.cp = {};
end;
local totalDistance = 0;
--- If this have not been set before after last stop command, we need to reset it again.
-- This also prevents from this code to calculate each loop while we are turning and can save CPU usage
if not vehicle.cp.directionNodeToTurnNodeLength then
local distances = vehicle.cp.distances;
for _, imp in ipairs(vehicle:getAttachedImplements()) do
if courseplay:isRearAttached(vehicle, imp.jointDescIndex) then
local workTool = imp.object;
local activeInputAttacherJoint = workTool:getActiveInputAttacherJoint();
if courseplay:isWheeledWorkTool(workTool) then
local workToolDistances = workTool.cp.distances;
if workToolDistances.attacherJointToPivot then
totalDistance = totalDistance + workToolDistances.attacherJointToPivot;
courseplay:debug(('getDirectionNodeToTurnNodeLength() -> %s: attacherJointToPivot=%.2fm'):format(
nameNum(workTool), workToolDistances.attacherJointToPivot), courseplay.DBG_IMPLEMENTS);
end;
totalDistance = totalDistance + workToolDistances.attacherJointOrPivotToTurningNode;
courseplay:debug(('getDirectionNodeToTurnNodeLength() -> %s: attacherJointOrPivotToTurningNode=%.2fm'):format(
nameNum(workTool), workToolDistances.attacherJointOrPivotToTurningNode), courseplay.DBG_IMPLEMENTS);
courseplay:debug(('getDirectionNodeToTurnNodeLength() -> %s: attacherJointToTurningNode=%.2fm'):format(
nameNum(workTool), totalDistance), courseplay.DBG_IMPLEMENTS);
else
if not distances.attacherJointOrPivotToTurningNode and distances.attacherJointToRearTrailerAttacherJoints then
totalDistance = totalDistance + distances.attacherJointToRearTrailerAttacherJoints[activeInputAttacherJoint.jointType];
end;
totalDistance = totalDistance + courseplay:getDirectionNodeToTurnNodeLength(workTool);
--courseplay:debug(('%s: directionNodeToTurnNodeLength=%.2fm'):format(nameNum(workTool), totalDistance), courseplay.DBG_IMPLEMENTS);
end;
break;
end;
end;
if vehicle.cp.directionNode and totalDistance > 0 then
for _, imp in ipairs(vehicle:getAttachedImplements()) do
if courseplay:isRearAttached(vehicle, imp.jointDescIndex) then
local workTool = imp.object;
local activeInputAttacherJoint = workTool:getActiveInputAttacherJoint();
totalDistance = totalDistance + distances.turningNodeToRearTrailerAttacherJoints[activeInputAttacherJoint.jointType];
break;
end;
end;
vehicle.cp.directionNodeToTurnNodeLength = totalDistance;
courseplay:debug(('getDirectionNodeToTurnNodeLength() -> %s: directionNodeToTurnNodeLength=%.2fm'):format(
nameNum(vehicle), totalDistance), courseplay.DBG_IMPLEMENTS);
end;
end;
return vehicle.cp.directionNodeToTurnNodeLength or totalDistance;
end;
function courseplay:getRealDollyFrontNode(dolly)
if dolly.cp.realDollyFrontNode == nil then
local activeInputAttacherJoint = dolly:getActiveInputAttacherJoint();
local node, _ = courseplay:findJointNodeConnectingToNode(dolly, activeInputAttacherJoint.rootNode, dolly.rootNode);
if node then
-- Trailers without pivote
if (node == dolly.rootNode and activeInputAttacherJoint.jointType ~= AttacherJoints.JOINTTYPE_IMPLEMENT)
-- Implements with pivot and wheels that do not lift the wheels from the ground.
or (node ~= dolly.rootNode and activeInputAttacherJoint.jointType == AttacherJoints.JOINTTYPE_IMPLEMENT and not activeInputAttacherJoint.topReferenceNode) then
dolly.cp.realDollyFrontNode = courseplay:getRealTurningNode(dolly);
else
dolly.cp.realDollyFrontNode = false;
end;
end;
end;
return dolly.cp.realDollyFrontNode
end;
function courseplay:getRealTrailerDistanceToPivot(workTool)
-- Attempt to find the pivot node.
local activeInputAttacherJoint = workTool:getActiveInputAttacherJoint();
local node, backTrack = courseplay:findJointNodeConnectingToNode(workTool, activeInputAttacherJoint.rootNode, courseplay:getLastComponentNodeWithWheels(workTool));
if node then
local x,y,z;
if node == workTool.rootNode then
x,y,z = getWorldTranslation(activeInputAttacherJoint.node);
else
x,y,z = getWorldTranslation(node);
end;
local _,_,tz = worldToLocal(courseplay:getRealTurningNode(workTool), x,y,z);
return tz;
else
return 3;
end;
end;
function courseplay:getRealTrailerFrontNode(workTool)
if not workTool.cp.realFrontNode then
local activeInputAttacherJoint = workTool:getActiveInputAttacherJoint();
local jointNode, backtrack = courseplay:findJointNodeConnectingToNode(workTool, activeInputAttacherJoint.rootNode, workTool.rootNode);
if jointNode and backtrack and activeInputAttacherJoint.jointType ~= AttacherJoints.JOINTTYPE_IMPLEMENT then
local rootNode;
for _, joint in ipairs(workTool.componentJoints) do
if joint.jointNode == jointNode and joint.rotLimit~= nil and joint.rotLimit[2] ~= nil and joint.rotLimit[2] > rad(15) then
rootNode = workTool.components[joint.componentIndices[2]].node;
break;
end;
end;
if rootNode then
local node = courseplay:createNewLinkedNode(workTool, "realFrontNode", rootNode);
local x, y, z = getWorldTranslation(jointNode);
local _,_,delta = worldToLocal(rootNode, x, y, z);
setTranslation(node, 0, 0, delta);
if courseplay:isInvertedToolNode(workTool, node) then
setRotation(node, 0, rad(180), 0);
end;
workTool.cp.realFrontNode = node;
end;
end;
if not workTool.cp.realFrontNode then
if courseplay:getLastComponentNodeWithWheels(workTool) ~= workTool.rootNode then
workTool.cp.realFrontNode = courseplay:getRealTurningNode(workTool, workTool.rootNode, "realFrontNode");
else
workTool.cp.realFrontNode = courseplay:getRealTurningNode(workTool);
end;
end;
end;
return workTool.cp.realFrontNode
end;
function courseplay:getRealTurningNode(object, useNode, nodeName)
if not object.cp.turningNode or useNode then
local node; -- Define local value
local _, y, _ = getWorldTranslation(object.rootNode);
local minDis, maxDis = 0, 0;
local minDisRot, maxDisRot = 0, 0;
local haveStraitWheels, haveTurningWheels = false, false;
local Distance = 0;
-- STEERABLES
if object.cp.directionNode then
-- Giants have provided us with steeringCenterNode, so use it.
if object.steeringCenterNode then
-- The steeringCenterNode is already set for us to use.
node = object.steeringCenterNode;
-- Check if it's actually an four wheel steering
if not object.crawlers or #object.crawlers == 0 then
for index, wheel in ipairs(object:getWheels()) do
-- Strait wheels
if wheel.rotMax == 0 and wheel.maxLatStiffness > 0 then
haveStraitWheels = true;
-- Turning wheels
else
haveTurningWheels = true;
end;
end;
if not haveStraitWheels and haveTurningWheels then
object.cp.isFourWheelSteering = true;
end;
end;
else
-- Create an new linked node.
node = courseplay:createNewLinkedNode(object, "realTurningNode", object.rootNode);
-- Find the pivot point on articulated vehicle
if object.spec_articulatedAxis and object.spec_articulatedAxis.componentJoint then
local jointNode = object.spec_articulatedAxis.componentJoint.jointNode;
local x,_,z = getWorldTranslation(jointNode);
_,_,Distance = worldToLocal(object.rootNode, x, y, z);
-- Get the distance from root node to the wheels turning point.
else
local rotMax = 0;
-- Sort wheels in turning wheels and strait wheels and find the min and max distance for each set.
for index, wheel in ipairs(object:getWheels()) do
local x,_,z = getWorldTranslation(wheel.repr);
local _,_,dis = worldToLocal(object.rootNode, x, y, z);
-- Strait wheels
if wheel.rotMax == 0 and wheel.maxLatStiffness > 0 then
if haveStraitWheels then
if dis < minDis then minDis = dis; end;
if dis > maxDis then maxDis = dis; end;
else
minDis = dis;
maxDis = dis;
haveStraitWheels = true;
end;
-- Turning wheels
else
if abs(wheel.rotMax) > rotMax then
rotMax = abs(wheel.rotMax)
end;
if haveTurningWheels then
if dis < minDisRot then minDisRot = dis; end;
if dis > maxDisRot then maxDisRot = dis; end;
else
minDisRot = dis;
maxDisRot = dis;
haveTurningWheels = true;
end;
end;
end;
-- 2WS: Calculate strait wheel median distance
if haveStraitWheels then
if minDis == maxDis then
Distance = minDis;
else
Distance = (minDis + maxDis) * 0.5;
end;
-- 4WS: Calculate turning wheel median distance if there are no strait wheels.
elseif haveTurningWheels then
object.cp.isFourWheelSteering = true;
object.cp.fourWheelSteerMaxRot = rotMax;
if minDisRot == maxDisRot then
Distance = minDisRot;
else
Distance = (minDisRot + maxDisRot) * 0.5;
end;
end;
end;
if Distance ~= 0 then
setTranslation(node, 0, 0, Distance);
end;
end;
-- IMPLEMENTS OR TRAILERS
else
--local AIReverseNode = object:getAIToolReverserDirectionNode();
local invert = courseplay:isInvertedToolNode(object) and -1 or 1;
local steeringAxleScale = 0;
-- Use useNode or Get the last component node with wheels
local componentNode = useNode or courseplay:getLastComponentNodeWithWheels(object);
-- Greate an new linked node based on what component to use or nodeName.
local transformGroupName = nodeName or "realTurningNode";
node = courseplay:createNewLinkedNode(object, transformGroupName, componentNode);
if not useNode and not nodeName then
--if AIReverseNode then
-- local x,_,z = getWorldTranslation(AIReverseNode);
-- local _,_,dis = worldToLocal(componentNode, x, y, z);
-- Distance = dis * invert;
-- courseplay:debug(('%s: getRealTurningNode(): Using getAIToolReverserDirectionNode() -> distance = %.2f'):format(nameNum(object), Distance), courseplay.DBG_IMPLEMENTS);
--else
-- Get the distance from root node to the wheels turning point.
local objectWheels = object:getWheels();
if objectWheels and #objectWheels > 0 then
local steeringAxleScaleMin, steeringAxleScaleMax = 0, 0;
-- Sort wheels in turning wheels and strait wheels and find the min and max distance for each set.
for i = 1, #objectWheels do
if courseplay:isPartOfNode(objectWheels[i].node, componentNode) and AIDriverUtil.isRealWheel(objectWheels[i]) then
local x,_,z = getWorldTranslation(objectWheels[i].driveNode);
local _,_,dis = worldToLocal(componentNode, x, y, z);
dis = dis * invert;
courseplay:debug(('%s: getRealTurningNode(): wheel%d distance = %.2f'):format(nameNum(object), i, dis), courseplay.DBG_IMPLEMENTS);
if object.steeringAxleUpdateBackwards == false or object.spec_wheels.wheels[i].steeringAxleScale == 0 then
if haveStraitWheels then
if dis < minDis then minDis = dis; end;
if dis > maxDis then maxDis = dis; end;
else
minDis = dis;
maxDis = dis;
haveStraitWheels = true;
end;
else
if objectWheels[i].steeringAxleScale < 0 and objectWheels[i].steeringAxleScale < steeringAxleScaleMin then
steeringAxleScaleMin = object.spec_wheels.wheels[i].steeringAxleScale;
elseif objectWheels[i].steeringAxleScale > 0 and objectWheels[i].steeringAxleScale > steeringAxleScaleMax then
steeringAxleScaleMax = objectWheels[i].steeringAxleScale;
end;
if haveTurningWheels then
if dis < minDisRot then minDisRot = dis; end;
if dis > maxDisRot then maxDisRot = dis; end;
else
minDisRot = dis;
maxDisRot = dis;
haveTurningWheels = true;
end;
end;
end;
end;
-- Calculate strait wheel median distance
if haveStraitWheels then
if minDis == maxDis then
Distance = minDis;
else
Distance = (minDis + maxDis) * 0.5;
end;
-- Calculate turning wheel median distance if there are no strait wheels.
elseif haveTurningWheels then
steeringAxleScale = steeringAxleScaleMin + steeringAxleScaleMax;
if minDisRot == maxDisRot then
Distance = minDisRot;
else
Distance = (minDisRot + maxDisRot) * 0.5;
end;
end;
courseplay:debug(('%s: getRealTurningNode(): haveStraitWheels=%q, haveTurningWheels=%q, Distance=%2f'):format(nameNum(object), tostring(haveStraitWheels), tostring(haveTurningWheels), Distance), courseplay.DBG_IMPLEMENTS);
end;
--end;
else
local jointNode = courseplay:getPivotJointNode(object);
if jointNode then
local x,_,z = getWorldTranslation(jointNode);
local _,_,dis = worldToLocal(node, x, y, z);
Distance = dis * invert;
end;
courseplay:debug(('%s: getRealTurningNode(): useNode=%q, nodeName=%q, Distance=%2f'):format(nameNum(object), tostring(useNode ~= nil), tostring(transformGroupName), Distance), courseplay.DBG_IMPLEMENTS);
end;
if Distance ~= 0 then
setTranslation(node, 0, 0, Distance);
end;
if courseplay:isInvertedToolNode(object, node) then
setRotation(node, 0, rad(180), 0);
end;
if not haveStraitWheels and object.steeringAxleUpdateBackwards and steeringAxleScale < 0 then
local activeInputAttacherJoint = object:getActiveInputAttacherJoint();
local tempNode, _ = courseplay:findJointNodeConnectingToNode(object, activeInputAttacherJoint.rootNode, componentNode);
if tempNode then
local x, y, z;
if tempNode == object.rootNode then
x, y, z = getWorldTranslation(activeInputAttacherJoint.node);
else
x, y, z = getWorldTranslation(tempNode);
end;
local _,_,dis = worldToLocal(node, x, y, z);
local offset = (dis * abs(steeringAxleScale)) + Distance;
setTranslation(node, 0, 0, offset);
object.cp.steeringAxleUpdateBackwards = true;
end;
end;
end;
if useNode then
return node;
else
object.cp.turningNode = node;
end;
end;
return object.cp.turningNode;
end;
function courseplay:getPivotJointNode(workTool)
if workTool.cp.jointNode == nil then
local componentNode = courseplay:getLastComponentNodeWithWheels(workTool);
for index, component in ipairs(workTool.components) do
-- Check if we are in the right component.
if component.node == componentNode then
for jointIndex, joint in ipairs(workTool.componentJoints) do
-- Check if we have the right componentJoint and if it's an pivot joint
if joint.componentIndices[2] ~= nil and joint.rotLimit~= nil and joint.rotLimit[2]~= nil and joint.componentIndices[2] == index and joint.rotLimit[2] > rad(15) then
-- Set the joint index and stop the loop.
workTool.cp.jointNode = workTool.componentJoints[jointIndex].jointNode;
break;
end;
end;
break;
end;
end;
if not workTool.cp.jointNode or not courseplay:isWheeledWorkTool(workTool) then workTool.cp.jointNode = false end;
end;
return workTool.cp.jointNode;
end;
function courseplay:getLastComponentNodeWithWheels(workTool)
-- Check if there is more than 1 component
local workToolsWheels = workTool:getWheels();
if workToolsWheels and #workToolsWheels > 0 and #workTool.components > 1 then
-- Check if the tool has inverted nodes
local invert = courseplay:isInvertedToolNode(workTool) and -1 or 1;
-- Set default node to start from.
local node = workTool.rootNode;
-- Loop through all the components.
for index, component in ipairs(workTool.components) do
-- Don't use the component that is the rootNode.
if component.node ~= node then
-- Loop through all the wheels and see if they are attached to this component.
for i = 1, #workToolsWheels do
if AIDriverUtil.isRealWheel(workToolsWheels[i]) then
if courseplay:isPartOfNode(workToolsWheels[i].node, component.node) then
-- Check if they are linked together
for _, joint in ipairs(workTool.componentJoints) do
if joint.componentIndices[2] == index then
if workTool.components[joint.componentIndices[1]].node == node then
-- Check if the component is behind the node.
local xJoint,yJoint,zJoint = getWorldTranslation(joint.jointNode);
local offset,_,direction = worldToLocal(node, xJoint,yJoint,zJoint);
--offset check to make sure we are selecting a node that is centered
if (direction * invert) < 0 and offset == 0 then
-- Component is hehind, so set the node to the new component node.
node = component.node;
end;
end;
end;
end;
break;
end;
end;
end;
end;
end;
-- Return the found node.
return node;
end;
-- Return default rootNode if none is found.
return workTool.rootNode
end;
function courseplay:getRealUnloadOrFillNode(workTool)
if workTool.cp.unloadOrFillNode == nil then
-- BALELOADERS and STRAWBLOWERS
if courseplay:isBaleLoader(workTool) then
-- Create the new node and link it to realTurningNode
local node = courseplay:createNewLinkedNode(workTool, "UnloadOrFillNode", courseplay:getRealTurningNode(workTool));
-- make sure we set the node distance position
setTranslation(node, 0, 0, g_vehicleConfigurations:get(workTool, 'balerUnloadDistance') or -5);
workTool.cp.unloadOrFillNode = node;
-- NORMAL FILLABLE TRAILERS WITH ALLOW TO BE FILLED FROM THE AIR
elseif workTool.cp.hasSpecializationTrailer and
workTool.cp.hasSpecializationFillable and
#workTool.spec_fillUnit.fillUnits > 0 and
workTool.spec_fillUnit.fillUnits[1].hasExactFillRootNodes then
-- Get the current exactFillRootNode.
local exactFillRootNode = workTool.spec_fillUnit.fillUnits[1].exactFillRootNode; -- TODO: Handle multiply exactFillRootNode for trailers with more than 1 fill plane. (temp fix for now)
-- Create the new node and link it to exactFillRootNode
local node = courseplay:createNewLinkedNode(workTool, "UnloadOrFillNode", exactFillRootNode);
-- Make sure ve set the height position to the same as the realTurningNode
local x, y, z = getWorldTranslation(courseplay:getRealTurningNode(workTool));
local _,Height,_ = worldToLocal(exactFillRootNode, x, y, z);
setTranslation(node, 0, Height, 0);
if courseplay:isInvertedToolNode(workTool, node) then
setRotation(node, 0, rad(180), 0);
end;
workTool.cp.unloadOrFillNode = node;
-- NONE OF THE ABOVE
else
workTool.cp.unloadOrFillNode = false;
end;
end;
return workTool.cp.unloadOrFillNode;
end;
function courseplay:getToolTurnRadius(workTool)
local turnRadius = 0; -- Default value if none is set
-- only object with the AttacherJoints spec have the upperRotLimit set up.
if courseplay:isWheeledWorkTool(workTool) and
SpecializationUtil.hasSpecialization(AttacherJoints, workTool.specializations) then
local radiusMultiplier = 1.05; -- Used to add a little bit to the radius, for safer turns.
local wheelBase = 0;
local rotMax = 0;
local CPRatio = 0;
local type = "Tool";
local TR = 0;
local frontLength = 0;
--attacherJointOrPivotToTurningNode
local attacherVehicle = workTool:getAttacherVehicle();
local workToolDistances = workTool.cp.distances or courseplay:getDistances(workTool);
for i, attachedImplement in pairs(attacherVehicle:getAttachedImplements()) do
if attachedImplement.object == workTool then
rotMax = attachedImplement.upperRotLimit[2];
break;
end;
end;
local attacherVehicleDistances = attacherVehicle.cp.distances or courseplay:getDistances(attacherVehicle);
local activeInputAttacherJoint = workTool:getActiveInputAttacherJoint();
if deg(rotMax) >= 30 and deg(rotMax) < 90 then
-- We have turningNodeToRearTrailerAttacherJoints value
if attacherVehicleDistances.turningNodeToRearTrailerAttacherJoints then
frontLength = attacherVehicleDistances.turningNodeToRearTrailerAttacherJoints[activeInputAttacherJoint.jointType] or 0;
-- We have turningNodeToTrailerAttacherJoints value
elseif attacherVehicleDistances.turningNodeToTrailerAttacherJoints then
frontLength = attacherVehicleDistances.turningNodeToTrailerAttacherJoints[activeInputAttacherJoint.jointType] or 0;
-- We have to go backwards to find the real front distance (attacherVehicle dont have wheels and might be a weight or something else)
else
frontLength = attacherVehicleDistances.attacherJointToRearTrailerAttacherJoints[activeInputAttacherJoint.jointType] or 0;
local backTrackVehicle = attacherVehicle;
local oldBackTrackVehicle;
while true do
oldBackTrackVehicle = backTrackVehicle;
backTrackVehicle = oldBackTrackVehicle.getAttacherVehicle and oldBackTrackVehicle:getAttacherVehicle() or false;
if backTrackVehicle and backTrackVehicle ~= {} then
local distances = backTrackVehicle.cp.distances or courseplay:getDistances(backTrackVehicle);
local jointType = oldBackTrackVehicle.attacherJoint.jointType;
if distances.turningNodeToRearTrailerAttacherJoints then
frontLength = frontLength + distances.turningNodeToRearTrailerAttacherJoints[jointType];
break;
elseif distances.turningNodeToTrailerAttacherJoints then
frontLength = frontLength + distances.turningNodeToTrailerAttacherJoints[jointType];
break;
else
frontLength = frontLength + (distances.attacherJointToRearTrailerAttacherJoints[jointType] or 0);
end;
else
break;
end;
end;
end;
courseplay:debug(('%s -> TurnRadius: rotMax=%d°, frontLength=%.2fm'):format(nameNum(workTool), deg(rotMax), frontLength), courseplay.DBG_IMPLEMENTS);
-- WE ARE A PIVOTED TRAILER / IMPLEMENT
if workToolDistances.attacherJointToPivot then
local pivotRotMax = 0;
local lastNode = courseplay:getLastComponentNodeWithWheels(workTool)
local _, _, rotLimits = courseplay:findJointNodeConnectingToNode(workTool, activeInputAttacherJoint.rootNode, lastNode);
if rotLimits then
for _, rotLimit in pairs(rotLimits) do
if rotLimit[2] > pivotRotMax and rotLimit[2] > rad(15) then
pivotRotMax = rotLimit[2];
end;
end;
end;
courseplay:debug(('%s -> TurnRadius: pivotRotMax=%d° (Pivot trailer/implement)'):format(nameNum(workTool), deg(pivotRotMax)), courseplay.DBG_IMPLEMENTS);
-- We are an implement and should be handled a bit different
if workTool.spec_attacherJoints and workTool.spec_attacherJoints.attacherJoint and workTool.spec_attacherJoints.attacherJoint.jointType == AttacherJoints.JOINTTYPE_IMPLEMENT then
-- We have a valid pivotRotMax, so calculate it normally.
if pivotRotMax > rad(15) then
frontLength = frontLength + workToolDistances.attacherJointToPivot;
wheelBase = frontLength + workToolDistances.attacherJointOrPivotToTurningNode;
CPRatio = courseplay:getCenterPivotRatio(nil, wheelBase, frontLength);
TR = courseplay:calculateTurnRadius(type, wheelBase, pivotRotMax, CPRatio);
-- If pivotRotMax is not greater than 15 degrees,
-- we will then use half of the length from attacherJoint to turningNode as the turnRadius instead.
else
TR = ceil((workToolDistances.attacherJointToPivot + workToolDistances.attacherJointOrPivotToTurningNode) / 2 * radiusMultiplier);
end;
courseplay:debug(('%s -> TurnRadius: turnRadius=%.2fm (Pivot implement)'):format(nameNum(workTool), TR), courseplay.DBG_IMPLEMENTS);
-- We are an pivoted trailer
else
-- We have a valid pivotRotMax, so calculate it normally.
if pivotRotMax > rad(15) then
-- Dolly part
wheelBase = frontLength + workToolDistances.attacherJointToPivot;
CPRatio = courseplay:getCenterPivotRatio(nil, wheelBase, frontLength);
local pivotTR = ceil(courseplay:calculateTurnRadius(type, wheelBase, rotMax, CPRatio) * radiusMultiplier);
-- Trailer part
wheelBase = workToolDistances.attacherJointOrPivotToTurningNode;
CPRatio = 0;
TR = ceil(courseplay:calculateTurnRadius(type, wheelBase, pivotRotMax, CPRatio) * radiusMultiplier);
-- Take the highest one
if pivotTR > TR then
TR = pivotTR;
end;
-- If pivotRotMax is not greater than 15 degrees,
-- we will then use half of the length from attacherJoint to turningNode as the turnRadius instead.
else
TR = ceil((workToolDistances.attacherJointToPivot + workToolDistances.attacherJointOrPivotToTurningNode) / 2 * radiusMultiplier);
end;
courseplay:debug(('%s -> TurnRadius: turnRadius=%.2fm (Pivot trailer)'):format(nameNum(workTool), TR), courseplay.DBG_IMPLEMENTS);
end;
-- WE ARE A NORMAL TRAILER OR IMPLEMENT
else
wheelBase = frontLength + (workToolDistances.attacherJointOrPivotToTurningNode or 0);
CPRatio = courseplay:getCenterPivotRatio(nil, wheelBase, frontLength);
TR = ceil(courseplay:calculateTurnRadius(type, wheelBase, rotMax, CPRatio) * radiusMultiplier);
courseplay:debug(('%s -> TurnRadius: turnRadius=%.2fm (Normal trailer/implement)'):format(nameNum(workTool), TR), courseplay.DBG_IMPLEMENTS);
end;
if TR > 0 then
turnRadius = TR;
end;
end;
-- If we are not an implement then check if half trailer length is bigger than the turnRadius and set it, if it is.
if ((deg(rotMax) < 30 and deg(rotMax) >= 90) or activeInputAttacherJoint.jointType ~= AttacherJoints.JOINTTYPE_IMPLEMENT) and workToolDistances.attacherJointToRearWheel then
if (workToolDistances.attacherJointToRearWheel / 2) > turnRadius then
turnRadius = ceil(workToolDistances.attacherJointToRearWheel / 2 * radiusMultiplier);
courseplay:debug(('%s -> TurnRadius: Using half tool length = %.2fm'):format(nameNum(workTool), turnRadius), courseplay.DBG_IMPLEMENTS);
end;
end;
else
courseplay:debug(('%s -> TurnRadius: Have no wheels or has no attacher joints. turnRadius set to 0m'):format(nameNum(workTool)), courseplay.DBG_IMPLEMENTS);
end;
return turnRadius;
end;
function courseplay:getTotalLengthOnWheels(vehicle)
courseplay:debug(('%s: getTotalLengthOnWheels()'):format(nameNum(vehicle)), courseplay.DBG_IMPLEMENTS);
local totalLength = 0;
local directionNodeToFrontWheelOffset;
if not vehicle.cp.distances or (courseplay.debugChannels[courseplay.DBG_IMPLEMENTS] ~= nil and courseplay.debugChannels[courseplay.DBG_IMPLEMENTS] == true) then
vehicle.cp.distances = courseplay:getDistances(vehicle);
end;
-- STEERABLES
if vehicle.cp.directionNode then
directionNodeToFrontWheelOffset = vehicle.cp.distances.frontWheelToDirectionNodeOffset;
local _, y, _ = getWorldTranslation(vehicle.cp.directionNode);
local hasRearAttach = false;
local jointType = 0;
for _, implement in ipairs(vehicle:getAttachedImplements()) do
-- Check if it's rear attached
if courseplay:isRearAttached(vehicle, implement.jointDescIndex) then
hasRearAttach = true;
local length, _ = courseplay:getTotalLengthOnWheels(implement.object);
if length > 0 then
jointType = implement.object:getActiveInputAttacherJoint().jointType;
totalLength = length;
end;
end;
end;
if hasRearAttach and totalLength > 0 and jointType > 0 then
local length = vehicle.cp.distances.frontWheelToRearTrailerAttacherJoints[jointType];
if length then
totalLength = totalLength + length;
else
totalLength = 0;
directionNodeToFrontWheelOffset = 0;
end;
courseplay:debug(('%s: hasRearAttach: totalLength=%.2f'):format(nameNum(vehicle), totalLength), courseplay.DBG_IMPLEMENTS);
else
totalLength = vehicle.cp.distances.frontWheelToRearWheel;
courseplay:debug(('%s: Using frontWheelToRearWheel=%.2f'):format(nameNum(vehicle), totalLength), courseplay.DBG_IMPLEMENTS);
end;
courseplay.debugLine(courseplay.DBG_IMPLEMENTS);
courseplay:debug(('%s: totalLength=%.2f, totalLengthOffset=%.2f'):format(nameNum(vehicle), totalLength, directionNodeToFrontWheelOffset), courseplay.DBG_IMPLEMENTS);
courseplay.debugLine(courseplay.DBG_IMPLEMENTS);
-- IMPLEMENTS OR TRAILERS
else
--local activeInputAttacherJoint = object:getActiveInputAttacherJoint();
local _, y, _ = getWorldTranslation(vehicle:getActiveInputAttacherJoint().node);
local hasRearAttach = false;
local jointType = 0;
for _, implement in ipairs(vehicle:getAttachedImplements()) do
-- Check if it's rear attached
if courseplay:isRearAttached(vehicle, implement.jointDescIndex) then
hasRearAttach = true;
local length, _ = courseplay:getTotalLengthOnWheels(implement.object);
if length > 0 then
jointType = implement.object:getActiveInputAttacherJoint().jointType;
totalLength = length;
end;
end;
end;
if hasRearAttach and totalLength > 0 and jointType > 0 and vehicle.cp.distances.attacherJointToRearTrailerAttacherJoints then
local length = vehicle.cp.distances.attacherJointToRearTrailerAttacherJoints[jointType];