forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode2.lua
1316 lines (1157 loc) · 56.4 KB
/
mode2.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local curFile = 'mode2.lua';
local abs, ceil, max, min = math.abs, math.ceil, math.max, math.min;
--[[ MODE 2 STATES
0: default, when not active
1: wait for work at start point
2: drive to combine
3: drive to pipe / unload
4: drive to the rear of the combine
5: follow target points
6: follow tractor
7: wait for pipe
9: wait till combine is gone outa my way
81: all trailers are full, tractor turns away from the combine
10: switch side
--]]
function courseplay:handle_mode2(vehicle, dt)
local frontTractor;
-- STATE 0 (default, when not active)
if vehicle.cp.modeState == 0 then
courseplay:setModeState(vehicle, 1);
end
-- STATE 1 (wait for work at start point)
if vehicle.cp.modeState == 1 and vehicle.cp.activeCombine ~= nil then
courseplay:unregisterFromCombine(vehicle, vehicle.cp.activeCombine)
end
-- support multiple tippers
if vehicle.cp.currentTrailerToFill == nil then
vehicle.cp.currentTrailerToFill = 1
end
local currentTipper = vehicle.cp.workTools[vehicle.cp.currentTrailerToFill]
if currentTipper == nil then
vehicle.cp.toolsDirty = true
return false
end
-- STATE 10 (switch side)
if vehicle.cp.activeCombine ~= nil and (vehicle.cp.modeState == 10 or vehicle.cp.activeCombine.turnAP ~= nil and vehicle.cp.activeCombine.turnAP == true) then
local node = vehicle.cp.activeCombine.cp.DirectionNode or vehicle.cp.activeCombine.rootNode;
if vehicle.cp.combineOffset > 0 then
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(node, 25, 0, 0)
else
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(node, -25, 0, 0)
end
courseplay:setModeState(vehicle, 5);
courseplay:setMode2NextState(vehicle, 2);
end
if currentTipper.fillLevel >= currentTipper.capacity or vehicle.cp.isLoaded then
if #(vehicle.cp.workTools) > vehicle.cp.currentTrailerToFill and not vehicle.cp.isLoaded then -- TODO (Jakob): use numWorkTools
vehicle.cp.currentTrailerToFill = vehicle.cp.currentTrailerToFill + 1
else
vehicle.cp.currentTrailerToFill = nil
if vehicle.cp.modeState ~= 5 then
if vehicle.cp.modeState == 9 then
vehicle.cp.nextTargets ={}
end
local targetIsInFront = false
local cx,cz = vehicle.Waypoints[vehicle.cp.waypointIndex].cx, vehicle.Waypoints[vehicle.cp.waypointIndex].cz
local cy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 0, cz)
local x,_,z = worldToLocal(vehicle.cp.DirectionNode or vehicle.rootNode, cx, cy, cz)
local overTakeDistance = 15
if z > overTakeDistance then
targetIsInFront = true
end
if (vehicle.cp.activeCombine ~= nil and vehicle.cp.activeCombine.cp.isWoodChipper) or targetIsInFront then
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0, 0, overTakeDistance)
else
if vehicle.cp.combineOffset > 0 then
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, vehicle.cp.turnDiameter+2, 0, -(vehicle.cp.totalLength+2))
else
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, -vehicle.cp.turnDiameter-2, 0, -(vehicle.cp.totalLength+2))
end
end
courseplay:setModeState(vehicle, 5);
courseplay:setMode2NextState(vehicle, 81);
end
end
end
if vehicle.cp.modeState == 1 and (vehicle.cp.tipperFillLevelPct >= vehicle.cp.driveOnAtFillLevel or vehicle.cp.isLoaded) then
vehicle.cp.currentTrailerToFill = nil
courseplay:setWaypointIndex(vehicle, 2);
courseplay:setIsLoaded(vehicle, true);
end
if vehicle.cp.activeCombine ~= nil then
if not vehicle.cp.activeCombine.cp.isChopper and courseplay:isSpecialChopper(vehicle.cp.activeCombine)then -- attached wood chipper will not be recognised as chopper before
vehicle.cp.activeCombine.cp.isChopper = true
end
if vehicle.cp.positionWithCombine == 1 then
-- is there a trailer to fill, or at least a waypoint to go to?
if vehicle.cp.currentTrailerToFill or vehicle.cp.modeState == 5 then
if vehicle.cp.modeState == 6 then
courseplay:setModeState(vehicle, 2);
end
courseplay:unload_combine(vehicle, dt)
end
else
-- follow tractor in front of me
frontTractor = vehicle.cp.activeCombine.courseplayers[vehicle.cp.positionWithCombine - 1]
courseplay:debug(string.format('%s: activeCombine ~= nil, my position=%d, frontTractor (positionWithCombine %d) = %q', nameNum(vehicle), vehicle.cp.positionWithCombine, vehicle.cp.positionWithCombine - 1, nameNum(frontTractor)), 4);
-- courseplay:follow_tractor(vehicle, dt, tractor)
courseplay:setModeState(vehicle, 6);
courseplay:unload_combine(vehicle, dt)
end
else -- NO active combine
-- STOP!!
AIVehicleUtil.driveInDirection(vehicle, dt, vehicle.cp.steeringAngle, 0, 0, 28, false, moveForwards, 0, 1)
courseplay:resetSlippingTimers(vehicle)
if vehicle.cp.isLoaded then
courseplay:setWaypointIndex(vehicle, 2);
courseplay:setModeState(vehicle, 99);
return false
end
-- are there any combines out there that need my help?
if CpManager.realTime5SecsTimerThrough then
if vehicle.cp.lastActiveCombine ~= nil then
local distance = courseplay:distanceToObject(vehicle, vehicle.cp.lastActiveCombine)
if distance > 20 then
vehicle.cp.lastActiveCombine = nil
courseplay:debug(string.format("%s (%s): last combine = nil", nameNum(vehicle), tostring(vehicle.id)), 4);
else
courseplay:debug(string.format("%s (%s): last combine is just %.0fm away, so wait", nameNum(vehicle), tostring(vehicle.id), distance), 4);
end
end
if vehicle.cp.lastActiveCombine == nil then -- it's important to call this function in the same loop like nilling vehicle.cp.lastActiveCombine
courseplay:updateReachableCombines(vehicle)
end
end
--is any of the reachable combines full?
if vehicle.cp.reachableCombines ~= nil then
if #vehicle.cp.reachableCombines > 0 then
-- choose the combine that needs me the most
if vehicle.cp.bestCombine ~= nil and vehicle.cp.activeCombine == nil then
courseplay:debug(string.format("%s (%s): request check-in @ %s", nameNum(vehicle), tostring(vehicle.id), tostring(vehicle.cp.combineID)), 4);
if courseplay:registerAtCombine(vehicle, vehicle.cp.bestCombine) then
courseplay:setModeState(vehicle, 2);
end
else
courseplay:setInfoText(vehicle,"COURSEPLAY_WAITING_FOR_FILL_LEVEL")
end
local highest_fill_level = 0;
local num_courseplayers = 0; --TODO: = fewest courseplayers ?
local distance = 0;
vehicle.cp.bestCombine = nil;
vehicle.cp.combineID = 0;
vehicle.cp.distanceToCombine = 99999999999;
-- chose the combine who needs me the most
for k, combine in pairs(vehicle.cp.reachableCombines) do
local fillLevel, capacity = combine:getAttachedTrailersFillLevelAndCapacity();
if combine.acParameters ~= nil and combine.acParameters.enabled and combine.isHired and fillLevel >= 0.99*capacity then --AC stops at 99% fillLevel so we have to set this as full
combine.cp.wantsCourseplayer = true
end
if (fillLevel >= (capacity * vehicle.cp.followAtFillLevel / 100)) or capacity == 0 or combine.cp.wantsCourseplayer then
if capacity == 0 then
if combine.courseplayers == nil then
vehicle.cp.bestCombine = combine
else
local numCombineCourseplayers = #combine.courseplayers;
if numCombineCourseplayers <= num_courseplayers or vehicle.cp.bestCombine == nil then
num_courseplayers = numCombineCourseplayers;
if numCombineCourseplayers > 0 then
frontTractor = combine.courseplayers[num_courseplayers];
local canFollowFrontTractor = frontTractor.cp.tipperFillLevelPct and frontTractor.cp.tipperFillLevelPct >= vehicle.cp.followAtFillLevel;
courseplay:debug(string.format('%s: frontTractor (pos %d)=%q, canFollowFrontTractor=%s', nameNum(vehicle), numCombineCourseplayers, nameNum(frontTractor), tostring(canFollowFrontTractor)), 4);
if canFollowFrontTractor then
vehicle.cp.bestCombine = combine
end
else
vehicle.cp.bestCombine = combine
end
end;
end
elseif fillLevel >= highest_fill_level and combine.cp.isCheckedIn == nil then
highest_fill_level = fillLevel
vehicle.cp.bestCombine = combine
distance = courseplay:distanceToObject(vehicle, combine);
vehicle.cp.distanceToCombine = distance
vehicle.cp.callCombineFillLevel = vehicle.cp.tipperFillLevelPct
vehicle.cp.combineID = combine.id
end
end
end
if vehicle.cp.combineID ~= 0 then
courseplay:debug(string.format("%s (%s): call combine: %s", nameNum(vehicle), tostring(vehicle.id), tostring(vehicle.cp.combineID)), 4);
end
else
courseplay:setInfoText(vehicle, "COURSEPLAY_NO_COMBINE_IN_REACH");
end
end
end
-- Four wheel drive
if vehicle.cp.hasDriveControl and vehicle.cp.driveControl.hasFourWD then
courseplay:setFourWheelDrive(vehicle);
end;
end
function courseplay:unload_combine(vehicle, dt)
local curFile = "mode2.lua"
local allowedToDrive = true
local combine = vehicle.cp.activeCombine;
local combineDirNode = combine.cp.DirectionNode or combine.rootNode;
local x, y, z = getWorldTranslation(vehicle.cp.DirectionNode)
local currentX, currentY, currentZ;
local combineFillLevel, combineIsTurning = nil, false
local refSpeed;
local handleTurn = false
local isHarvester = false
local xt, yt, zt;
local dod;
local currentTipper = {}
-- Calculate Trailer Offset
if vehicle.cp.currentTrailerToFill ~= nil then
currentTipper = vehicle.cp.workTools[vehicle.cp.currentTrailerToFill]
if not currentTipper.cp.realUnloadOrFillNode then
currentTipper.cp.realUnloadOrFillNode = courseplay:getRealUnloadOrFillNode(currentTipper);
end;
xt, yt, zt = worldToLocal(currentTipper.cp.realUnloadOrFillNode, x, y, z)
else
--courseplay:debug(nameNum(vehicle) .. ": no cp.currentTrailerToFillSet", 4);
xt, yt, zt = worldToLocal(vehicle.cp.workTools[1].rootNode, x, y, z)
end
-- support for tippers like hw80
if zt < 0 then
zt = zt * -1
end
local trailerOffset = zt + vehicle.cp.tipperOffset
local totalLength = vehicle.cp.totalLength+2
local turnDiameter = vehicle.cp.turnDiameter+2
if vehicle.cp.chopperIsTurning == nil then
vehicle.cp.chopperIsTurning = false
end
local fillLevel, capacity = combine:getAttachedTrailersFillLevelAndCapacity();
if capacity > 0 then
combineFillLevel = fillLevel * 100 / capacity
else -- combine is a chopper / has no tank
combineFillLevel = 99;
end
local tractor = combine
if courseplay:isAttachedCombine(combine) then
tractor = combine.attacherVehicle
-- Really make sure the combine's attacherVehicle still exists - see issue #443
if tractor == nil then
courseplay:removeActiveCombineFromTractor(vehicle);
return;
end;
end;
local combineIsStopped = tractor.lastSpeedReal*3600 < 0.5
-- auto combine
local AutoCombineIsTurning = false
local combineIsAutoCombine = false
local autoCombineExtraMoveBack = 0
if tractor.acParameters ~= nil and tractor.acParameters.enabled and tractor.isHired then
combineIsAutoCombine = true
if tractor.cp.turnStage == nil then
tractor.cp.turnStage = 0
end
-- if tractor.acTurnStage ~= 0 then
if tractor.acTurnStage > 0 and not (tractor.acTurnStage >= 20 and tractor.acTurnStage <= 22) then
tractor.cp.turnStage = 2
autoCombineExtraMoveBack = vehicle.cp.turnDiameter*1.5
AutoCombineIsTurning = true
-- print(('%s: acTurnStage=%d -> cp.turnState=2, AutoCombineIsTurning=true'):format(nameNum(tractor), tractor.acTurnStage)); --TODO: 140308 AutoTractor
else
tractor.cp.turnStage = 0
end
end
-- is combine turning ?
if not vehicle.cp.choppersTurnHasEnded and combine.cp.isChopper and combine.turnStage == 3 and combine.waitingForTrailerToUnload then
vehicle.cp.choppersTurnHasEnded = true
elseif combine.turnStage ~= 3 then
vehicle.cp.choppersTurnHasEnded = false
end
local aiTurn = combine.isAIThreshing and combine.turnStage > 0 and not (combine.turnStage == 3 and vehicle.cp.choppersTurnHasEnded)
if tractor ~= nil and (aiTurn or tractor.cp.turnStage > 0) then
--courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_IS_TURNING");
combineIsTurning = true
-- print(('%s: cp.turnStage=%d -> combineIsTurning=true'):format(nameNum(tractor), tractor.cp.turnStage));
end
vehicle.cp.mode2DebugTurning = combineIsTurning
if vehicle.cp.modeState == 2 or vehicle.cp.modeState == 3 or vehicle.cp.modeState == 4 then
if combine == nil then
courseplay:setInfoText(vehicle, "combine == nil, this should never happen");
allowedToDrive = false
end
end
local offset_to_chopper = vehicle.cp.combineOffset
if combineIsTurning then
offset_to_chopper = vehicle.cp.combineOffset * 1.6 --1,3
end
local x1, y1, z1 = worldToLocal(combineDirNode, x, y, z)
local distance = Utils.vector2Length(x1, z1)
local safetyDistance = 11;
if combine.cp.isHarvesterSteerable or combine.cp.isSugarBeetLoader or combine.cp.isWoodChipper then
safetyDistance = 24;
elseif courseplay:isAttachedCombine(combine) then
safetyDistance = 11;
elseif combine.cp.isCombine then
safetyDistance = 10;
elseif combine.cp.isChopper then
safetyDistance = 11;
end;
-- STATE 2 (drive to combine)
if vehicle.cp.modeState == 2 then
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
courseplay:setInfoText(vehicle, "COURSEPLAY_DRIVE_BEHIND_COMBINE");
local x1, y1, z1 = worldToLocal(tractor.cp.DirectionNode or tractor.rootNode, x, y, z)
if z1 > -(turnDiameter + safetyDistance) then -- tractor in front of combine
-- left side of combine
local cx_left, cy_left, cz_left = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, 20, 0, -30)
-- righ side of combine
local cx_right, cy_right, cz_right = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, -20, 0, -30)
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, cx_left, y, cz_left)
-- distance to left position
local disL = Utils.vector2Length(lx, lz)
local rx, ry, rz = worldToLocal(vehicle.cp.DirectionNode, cx_right, y, cz_right)
-- distance to right position
local disR = Utils.vector2Length(rx, rz)
if disL < disR then
currentX, currentY, currentZ = cx_left, cy_left, cz_left
else
currentX, currentY, currentZ = cx_right, cy_right, cz_right
end
else
-- tractor behind combine
if not combine.cp.isChopper then
currentX, currentY, currentZ = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, vehicle.cp.combineOffset, 0, -(turnDiameter + safetyDistance)) --!!!
else
currentX, currentY, currentZ = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, 0, 0, -(turnDiameter + safetyDistance))
end
end
--[[
-- PATHFINDING / REALISTIC DRIVING (ASTAR)
if vehicle.cp.realisticDriving and not vehicle.cp.calculatedCourseToCombine then
-- if courseplay:calculate_course_to(vehicle, currentX, currentZ) then
if courseplay:calculateAstarPathToCoords(vehicle, currentX, currentZ) then
courseplay:setModeState(vehicle, 5);
vehicle.cp.shortestDistToWp = nil;
courseplay:setMode2NextState(vehicle, 2); -- modeState when waypoint is reached
end;
end;
--]]
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, currentX, currentY, currentZ)
dod = Utils.vector2Length(lx, lz)
-- near point
if dod < 3 then -- change to vehicle.cp.modeState 4 == drive behind combine or cornChopper
if combine.cp.isChopper and (not vehicle.cp.chopperIsTurning or combineIsAutoCombine) then -- decide on which side to drive based on ai-combine
courseplay:sideToDrive(vehicle, combine, 10);
if vehicle.sideToDrive == "right" then
vehicle.cp.combineOffset = abs(vehicle.cp.combineOffset) * -1;
else
vehicle.cp.combineOffset = abs(vehicle.cp.combineOffset);
end
end
courseplay:setModeState(vehicle, 4);
end;
-- END STATE 2
-- STATE 4 (drive to rear of combine)
elseif vehicle.cp.modeState == 4 then
if combine.cp.offset == nil or vehicle.cp.combineOffset == 0 or combine.cp.isHolmerDlcCrabSteeringPossible then
--print("offset not saved - calculate")
courseplay:calculateCombineOffset(vehicle, combine);
elseif not combine.cp.isChopper and not combine.cp.isSugarBeetLoader and vehicle.cp.combineOffsetAutoMode and vehicle.cp.combineOffset ~= combine.cp.offset then
--print("set saved offset")
vehicle.cp.combineOffset = combine.cp.offset
end
courseplay:setInfoText(vehicle, "COURSEPLAY_DRIVE_TO_COMBINE");
--courseplay:addToCombinesIgnoreList(vehicle, combine)
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
local tX, tY, tZ = nil, nil, nil
if combine.cp.isSugarBeetLoader then
local prnToCombineZ = courseplay:calculateVerticalOffset(vehicle, combine);
tX, tY, tZ = localToWorld(combineDirNode, vehicle.cp.combineOffset, 0, prnToCombineZ -5);
else
tX, tY, tZ = localToWorld(combineDirNode, vehicle.cp.combineOffset, 0, -5);
end
if combine.attachedImplements ~= nil then
for k, i in pairs(combine.attachedImplements) do
local implement = i.object;
if implement.haeckseldolly == true then
tX, tY, tZ = localToWorld(implement.rootNode, vehicle.cp.combineOffset, 0, trailerOffset)
end
end
end
currentX, currentZ = tX, tZ
local lx, ly, lz = nil, nil, nil
lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, tX, y, tZ)
if currentX ~= nil and currentZ ~= nil then
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, currentX, y, currentZ)
dod = Utils.vector2Length(lx, lz)
else
dod = Utils.vector2Length(lx, lz)
end
if dod < 2 then -- dod < 2
allowedToDrive = false
courseplay:setModeState(vehicle, 3); -- change to modeState 3 == drive to unload pipe
vehicle.cp.chopperIsTurning = false
end
if dod > 50 then
courseplay:setModeState(vehicle, 2);
end
-- END STATE 4
-- STATE 3 (drive to unload pipe)
elseif vehicle.cp.modeState == 3 then
--courseplay:addToCombinesIgnoreList(vehicle, combine)
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if vehicle.cp.nextTargets ~= nil then
vehicle.cp.nextTargets = {}
end
if (not combine.cp.isChopper or combine.haeckseldolly) and (combineFillLevel == 0 or vehicle.cp.forceNewTargets) then --combine empty set waypoints on the field !!!
if combine.cp.offset == nil then
--print("saving offset")
combine.cp.offset = vehicle.cp.combineOffset;
end
local sideMultiplier = 0;
if tractor.cp.workWidth == nil or tractor.cp.workWidth == 0 or not tractor.cp.isDriving then
courseplay:calculateWorkWidth(tractor, true)
end
local workWidth = tractor.cp.workWidth
local combineOffset = vehicle.cp.combineOffset
local offset = abs(combineOffset)
local fruitSide = "404notFound"
local nodeSet = false
if workWidth < offset then
local diff = max (1.5,workWidth/2)
if combine.cp.isHarvesterAttachable or combine.cp.hasCrabSteeringActive then
diff = 5
end
fruitSide = courseplay:sideToDrive(vehicle, combine, 0);
if (fruitSide == "right" and combineOffset > 0) or (fruitSide == "left" and combineOffset < 0) then
offset = offset-diff
else
offset = offset+diff
end
end
courseplay:debug(string.format("%s: combine.workWidth: %.2f,vehicle.cp.combineOffset: %.2f, calculated offset: %.2f, fruitSide: %s ",nameNum(vehicle),workWidth,combineOffset,offset,fruitSide),4)
if combineOffset > 0 then
sideMultiplier = -1;
else
sideMultiplier = 1;
end
if combineIsTurning or vehicle.cp.forceNewTargets then
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(currentTipper.rootNode, -sideMultiplier*turnDiameter, 0, trailerOffset);
courseplay:debug(string.format("%s: combine is empty and turning",nameNum(vehicle)),4)
if combineIsAutoCombine then
local index = combine.acDirectionBeforeTurn.traceIndex+1
if index > #combine.acDirectionBeforeTurn.trace then
index = 1
end
local tipperX,tipperY,tipperZ = getWorldTranslation(currentTipper.rootNode)
local dirX,dirZ = combine.acDirectionBeforeTurn.trace[index].dx,combine.acDirectionBeforeTurn.trace[index].dz
vehicle.cp.cpTurnBaseNode = createTransformGroup('cpTurnBaseNode');
link(getRootNode(), vehicle.cp.cpTurnBaseNode);
setTranslation(vehicle.cp.cpTurnBaseNode, tipperX,tipperY,tipperZ);
setRotation(vehicle.cp.cpTurnBaseNode, 0, math.atan2(dirX, dirZ), 0)
nodeSet = true
courseplay:debug(string.format("%s: combineIsAutoCombine- create vehicle.cp.cpTurnBaseNode (%s; %s)",nameNum(vehicle),tostring(vehicle.cp.cpTurnBaseNode), tostring(getName(vehicle.cp.cpTurnBaseNode))),4)
end
courseplay:debug(string.format("%s: addNewTargetVector: currentTipper: %s ,vehicle.cp.cpTurnBaseNode: %s",nameNum(vehicle),tostring(currentTipper),tostring(vehicle.cp.cpTurnBaseNode)),4)
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset*0.5 , (-totalLength*2)+trailerOffset,currentTipper,vehicle.cp.cpTurnBaseNode);
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset , (-totalLength*3)+trailerOffset,currentTipper,vehicle.cp.cpTurnBaseNode);
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset , (-totalLength*4)+trailerOffset,currentTipper,vehicle.cp.cpTurnBaseNode);
courseplay:setModeState(vehicle, 5);
if vehicle.cp.forceNewTargets then
vehicle.cp.forceNewTargets = nil
end
else
courseplay:debug(string.format("%s: combine is empty ",nameNum(vehicle)),4)
if combine.cp.isHarvesterAttachable then
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(currentTipper.rootNode, 0 , 0, 5);
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset*0.8 ,totalLength + trailerOffset,currentTipper);
else
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(currentTipper.rootNode, sideMultiplier*offset*0.8 , 0, totalLength + trailerOffset);
end
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset ,(totalLength*3)+trailerOffset,currentTipper);
courseplay:setModeState(vehicle, 9);
end
if nodeSet then
unlink(vehicle.cp.cpTurnBaseNode);
delete(vehicle.cp.cpTurnBaseNode);
vehicle.cp.cpTurnBaseNode = nil
end
if vehicle.cp.nextTargets ~= nil then
courseplay:debug(string.format("%s: vehicle.cp.nextTargets: %s ",nameNum(vehicle),tostring(#vehicle.cp.nextTargets)),4)
else
courseplay:debug(string.format("%s: vehicle.cp.nextTargets: nil ",nameNum(vehicle)),4)
end
courseplay:setMode2NextState(vehicle, 1);
end
--CALCULATE HORIZONTAL OFFSET (side offset)
if combine.cp.offset == nil and not combine.cp.isChopper then
courseplay:calculateCombineOffset(vehicle, combine);
end
currentX, currentY, currentZ = localToWorld(combineDirNode, vehicle.cp.combineOffset, 0, trailerOffset + 5)
--CALCULATE VERTICAL OFFSET (tipper offset)
local prnToCombineZ = courseplay:calculateVerticalOffset(vehicle, combine);
--SET TARGET UNLOADING COORDINATES @ COMBINE
local ttX, ttZ = courseplay:getTargetUnloadingCoords(vehicle, combine, trailerOffset, prnToCombineZ);
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, ttX, y, ttZ)
dod = Utils.vector2Length(lx, lz)
if dod > 40 or vehicle.cp.chopperIsTurning == true then
courseplay:setModeState(vehicle, 2);
end
-- combine is not moving and trailer is under pipe
if lz < 5 and combine.fillLevel > 100 then
-- print(string.format("lz: %.4f, prnToCombineZ: %.2f, trailerOffset: %.2f",lz,prnToCombineZ,trailerOffset))
end
if not combine.cp.isChopper and combineIsStopped and (lz <= 1 or lz < -0.1 * trailerOffset) then
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_WANTS_ME_TO_STOP");
allowedToDrive = false
elseif combine.cp.isChopper then
if (combineIsStopped or courseplay:isSpecialChopper(combine)) and dod == -1 and vehicle.cp.chopperIsTurning == false then
allowedToDrive = false
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_WANTS_ME_TO_STOP");
end
if lz < -2 then
allowedToDrive = false
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_WANTS_ME_TO_STOP");
-- courseplay:setModeState(vehicle, 2);
end
elseif lz < -1.5 then
allowedToDrive = false
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_WANTS_ME_TO_STOP");
end
if vehicle.cp.infoText == nil then
courseplay:setInfoText(vehicle, "COURSEPLAY_DRIVE_NEXT_TO_COMBINE");
end
-- refspeed depends on the distance to the combine
local combine_speed = tractor.lastSpeed*3600
if combine.cp.isChopper then
if lz > 20 then
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif lz > 4 and (combine_speed*3600) > 5 then
refSpeed = max(combine_speed *1.5,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif lz > 10 then
refSpeed = vehicle.cp.speeds.turn
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif lz < -1 then
refSpeed = max(combine_speed/2,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
refSpeed = max(combine_speed,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
if (combineIsTurning and lz < 20) or (combineIsStopped and lz < 5) then
refSpeed = vehicle.cp.speeds.crawl
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
else
if lz > 5 then
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif lz < -0.5 then
refSpeed = max(combine_speed - vehicle.cp.speeds.crawl,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
elseif lz > 1 or combine.sentPipeIsUnloading ~= true then
refSpeed = max(combine_speed + vehicle.cp.speeds.crawl,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
refSpeed = max(combine_speed,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
if (combineIsTurning and lz < 20) or (vehicle.timer < vehicle.cp.driveSlowTimer) or (combineIsStopped and lz < 15) then
refSpeed = vehicle.cp.speeds.crawl
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if combineIsTurning then
vehicle.cp.driveSlowTimer = vehicle.timer + 2000
end
end
end
--courseplay:debug("combine.sentPipeIsUnloading: "..tostring(combine.sentPipeIsUnloading).." refSpeed: "..tostring(refSpeed*3600).." combine_speed: "..tostring(combine_speed*3600), 4)
--END STATE 3
-- STATE 9 (wait till combine is gone)
elseif vehicle.cp.modeState == 9 then
local lastIndex = #vehicle.cp.nextTargets
local tx,ty,tz = vehicle.cp.nextTargets[lastIndex].x,vehicle.cp.nextTargets[lastIndex].y,vehicle.cp.nextTargets[lastIndex].z;
if vehicle.cp.swayPointDistance == nil then
_,_,vehicle.cp.swayPointDistance = worldToLocal(vehicle.cp.DirectionNode, tx,ty,tz)
end
local x,y,z = getWorldTranslation(combineDirNode)
local _,_,combineDistance = worldToLocal(vehicle.cp.DirectionNode, x,y,z)
local backupDistance = worldToLocal(combineDirNode, tx,ty,tz)
if combineDistance > vehicle.cp.swayPointDistance + 3 or backupDistance < -5 then
vehicle.cp.swayPointDistance = nil
courseplay:setModeState(vehicle, 5);
else
allowedToDrive = false
courseplay:setInfoText(vehicle, "COURSEPLAY_WAITING_TILL_WAITING_POSITION_IS_FREE");
end
if combineIsTurning then
vehicle.cp.swayPointDistance = nil
courseplay:setModeState(vehicle, 3);
vehicle.cp.forceNewTargets = true
vehicle.cp.nextTargets = {}
end
end;
---------------------------------------------------------------------
local cx, cy, cz = getWorldTranslation(combineDirNode)
local sx, sy, sz = getWorldTranslation(vehicle.cp.DirectionNode)
distance = courseplay:distance(sx, sz, cx, cz)
if combineIsTurning and not combine.cp.isChopper and vehicle.cp.modeState > 1 then
if combine.fillLevel > combine.capacity*0.9 then
if combineIsAutoCombine and tractor.acIsCPStopped ~= nil then
-- print(nameNum(tractor) .. ': fillLevel > 90%% -> set acIsCPStopped to true'); --TODO: 140308 AutoTractor
tractor.acIsCPStopped = true
elseif combine.isAIThreshing then
combine.waitForTurnTime = combine.timer + 100
elseif tractor:getIsCourseplayDriving() then
combine.cp.waitingForTrailerToUnload = true
end
elseif distance < 50 then
--[[for i=1, #combine.acDirectionBeforeTurn.trace do
local px,pz = combine.acDirectionBeforeTurn.trace[i].px,combine.acDirectionBeforeTurn.trace[i].pz
local dirX,dirZ = combine.acDirectionBeforeTurn.trace[i].dx,combine.acDirectionBeforeTurn.trace[i].dz
drawDebugPoint(px+(-dirX*100),cy+10,pz+(-dirZ*100), 1, 1, 1, 1);
drawDebugLine(px,cy+3,pz, 1, 0, 1, px+(-dirX*100), cy+10,pz+(-dirZ*100), 1, 0, 1);
end
local index = combine.acDirectionBeforeTurn.traceIndex+1
if index > #combine.acDirectionBeforeTurn.trace then
index = 1
end
local px,pz = combine.acDirectionBeforeTurn.trace[index].px,combine.acDirectionBeforeTurn.trace[index].pz
local dirX,dirZ = combine.acDirectionBeforeTurn.trace[index].dx,combine.acDirectionBeforeTurn.trace[index].dz
drawDebugPoint(px+(-dirX*100),cy+10,pz+(-dirZ*100), 1, 1, 1, 1);
drawDebugLine(px,cy+3,pz, 1, 1, 1, px+(-dirX*100), cy+10,pz+(-dirZ*100), 1, 1, 1);]]
--courseplay:setCustomTimer(vehicle, 'fieldEdgeTimeOut', 15);
--courseplay:resetCustomTimer(vehicle, 'fieldEdgeTimeOut');
if not courseplay:timerIsThrough(vehicle, 'fieldEdgeTimeOut') or vehicle.cp.modeState > 2 then
if AutoCombineIsTurning and tractor.acIsCPStopped ~= nil then
-- print(nameNum(tractor) .. ': distance < 50 -> set acIsCPStopped to true'); --TODO: 140308 AutoTractor
tractor.acIsCPStopped = true
elseif combine.isAIThreshing then --and not (combineFillLevel == 0 and combine.currentPipeState ~= 2) then
combine.waitForTurnTime = combine.timer + 100
elseif tractor:getIsCourseplayDriving() then --and not (combineFillLevel == 0 and combine:getOverloadingTrailerInRangePipeState()==0) then
combine.cp.waitingForTrailerToUnload = true
end
elseif vehicle.cp.fieldEdgeTimeOutSet ~= true then
--print("set timer")
courseplay:setCustomTimer(vehicle, 'fieldEdgeTimeOut', 20);
vehicle.cp.fieldEdgeTimeOutSet = true
--print("set vehicle.cp.fieldEdgeTimeOutSet")
else
allowedToDrive = false;
if combine.cp.waitingForTrailerToUnload then
--print("reset combine.cp.waitingForTrailerToUnload")
combine.cp.waitingForTrailerToUnload = false
elseif tractor.acIsCPStopped then
tractor.acIsCPStopped = false
end
end
elseif distance < 100 and vehicle.cp.modeState == 2 then
allowedToDrive = false;
end
elseif vehicle.cp.fieldEdgeTimeOutSet then
vehicle.cp.fieldEdgeTimeOutSet = false
--print("reset vehicle.cp.fieldEdgeTimeOutSet")
end
if combineIsTurning and distance < 20 then
if vehicle.cp.modeState == 3 or vehicle.cp.modeState == 4 then
if combine.cp.isChopper then
local fruitSide = courseplay:sideToDrive(vehicle, combine, -10,true);
--new chopper turn maneuver by Thomas Gärtner
if fruitSide == "left" then -- chopper will turn left
if vehicle.cp.combineOffset > 0 then -- I'm left of chopper
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns left, I'm left", curFile, debug.getinfo(1).currentline, nameNum(vehicle), tostring(combine.name)), 4);
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0, 0, turnDiameter);
courseplay:addNewTargetVector(vehicle, 2*turnDiameter*-1 , turnDiameter);
vehicle.cp.chopperIsTurning = true
else --i'm right of choppper
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns left, I'm right", curFile, debug.getinfo(1).currentline, nameNum(vehicle), tostring(combine.name)), 4);
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, turnDiameter*-1, 0, turnDiameter);
vehicle.cp.chopperIsTurning = true
end
else -- chopper will turn right
if vehicle.cp.combineOffset < 0 then -- I'm right of chopper
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns right, I'm right", curFile, debug.getinfo(1).currentline, nameNum(vehicle), tostring(combine.name)), 4);
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0, 0, turnDiameter);
courseplay:addNewTargetVector(vehicle, 2*turnDiameter, turnDiameter);
vehicle.cp.chopperIsTurning = true
else -- I'm left of chopper
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns right, I'm left", curFile, debug.getinfo(1).currentline, nameNum(vehicle), tostring(combine.name)), 4);
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, turnDiameter, 0, turnDiameter);
vehicle.cp.chopperIsTurning = true
end
end
if vehicle.cp.combineOffsetAutoMode then
if vehicle.sideToDrive == "right" then
vehicle.cp.combineOffset = combine.cp.offset * -1;
elseif vehicle.sideToDrive == "left" then
vehicle.cp.combineOffset = combine.cp.offset;
end;
else
if vehicle.sideToDrive == "right" then
vehicle.cp.combineOffset = abs(vehicle.cp.combineOffset) * -1;
elseif vehicle.sideToDrive == "left" then
vehicle.cp.combineOffset = abs(vehicle.cp.combineOffset);
end;
end;
courseplay:setModeState(vehicle, 5);
vehicle.cp.shortestDistToWp = nil
courseplay:setMode2NextState(vehicle, 7);
end
-- elseif vehicle.cp.modeState ~= 5 and vehicle.cp.modeState ~= 99 and not vehicle.cp.realisticDriving then
elseif vehicle.cp.modeState ~= 5 and vehicle.cp.modeState ~= 9 and not vehicle.cp.realisticDriving then
-- just wait until combine has turned
allowedToDrive = false
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_WANTS_ME_TO_STOP");
end
end
-- STATE 7
if vehicle.cp.modeState == 7 then
if not combineIsTurning then
courseplay:setModeState(vehicle, 2);
else
courseplay:setInfoText(vehicle, "COURSEPLAY_WAITING_FOR_COMBINE_TURNED");
end
refSpeed = vehicle.cp.speeds.turn
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
--[[ TODO: MODESTATE 99 - WTF?
-- STATE 99 (turn maneuver)
if vehicle.cp.modeState == 99 and vehicle.cp.curTarget.x ~= nil and vehicle.cp.curTarget.z ~= nil then
--courseplay:removeFromCombinesIgnoreList(vehicle, combine)
courseplay:setInfoText(vehicle, string.format("COURSEPLAY_TURNING_TO_COORDS;%d;%d",vehicle.cp.curTarget.x,vehicle.cp.curTarget.z));
allowedToDrive = false
local mx, mz = vehicle.cp.curTarget.x, vehicle.cp.curTarget.z
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, mx, y, mz)
refSpeed = vehicle.cp.speeds.field --vehicle.cp.speeds.turn
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
if lz > 0 and abs(lx) < lz * 0.5 then -- lz * 0.5 --2
if vehicle.cp.mode2nextState == 4 and not combineIsTurning then
vehicle.cp.curTarget.x = nil
vehicle.cp.curTarget.z = nil
courseplay:switchToNextMode2State(vehicle);
courseplay:setMode2NextState(vehicle, 0);
end
if vehicle.cp.mode2nextState == 1 or vehicle.cp.mode2nextState == 2 then
-- is there another waypoint to go to?
if #(vehicle.cp.nextTargets) > 0 then
courseplay:setModeState(vehicle, 5);
vehicle.cp.shortestDistToWp = nil
courseplay:setCurrentTargetFromList(vehicle, 1);
else
courseplay:switchToNextMode2State(vehicle);
courseplay:setMode2NextState(vehicle, 0);
end
end
else
currentX, currentY, currentZ = localToWorld(vehicle.cp.DirectionNode, vehicle.turn_factor, 0, 5)
allowedToDrive = true
end
end
]]
-- STATE 5 (follow target points)
if vehicle.cp.modeState == 5 and vehicle.cp.curTarget.x ~= nil and vehicle.cp.curTarget.z ~= nil then
if combine ~= nil then
--courseplay:removeFromCombinesIgnoreList(vehicle, combine)
end
courseplay:setInfoText(vehicle, string.format("COURSEPLAY_DRIVE_TO_WAYPOINT;%d;%d",vehicle.cp.curTarget.x,vehicle.cp.curTarget.z));
currentX = vehicle.cp.curTarget.x
currentY = vehicle.cp.curTarget.y
currentZ = vehicle.cp.curTarget.z
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
local distance_to_wp = courseplay:distanceToPoint(vehicle, currentX, y, currentZ);
if #(vehicle.cp.nextTargets) == 0 then
if distance_to_wp < 10 then
refSpeed = vehicle.cp.speeds.turn
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
end
-- avoid circling
local distToChange = 1
if vehicle.cp.shortestDistToWp == nil or vehicle.cp.shortestDistToWp > distance_to_wp then
vehicle.cp.shortestDistToWp = distance_to_wp
end
if distance_to_wp > vehicle.cp.shortestDistToWp and distance_to_wp < 3 then
distToChange = distance_to_wp + 1
end
if distance_to_wp < distToChange then
--[[if vehicle.cp.mode2nextState == 81 then
if vehicle.cp.activeCombine ~= nil then
courseplay:unregisterFromCombine(vehicle, vehicle.cp.activeCombine)
end
end]]
vehicle.cp.shortestDistToWp = nil
if #(vehicle.cp.nextTargets) > 0 then
-- courseplay:setModeState(vehicle, 5);
courseplay:setCurrentTargetFromList(vehicle, 1);
else
allowedToDrive = false
if vehicle.cp.mode2nextState ~= 2 then
vehicle.cp.calculatedCourseToCombine = false
end
if vehicle.cp.mode2nextState == 7 then
courseplay:switchToNextMode2State(vehicle);
--vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(combineDirNode, vehicle.chopper_offset*0.7, 0, -9) -- -2 --??? *0,5 -10
elseif vehicle.cp.mode2nextState == 4 and combineIsTurning then
courseplay:setInfoText(vehicle, "COURSEPLAY_WAITING_FOR_COMBINE_TURNED");
elseif vehicle.cp.mode2nextState == 81 then -- tipper turning from combine
-- print(('%s [%s(%d)]: no nextTargets, mode2nextState=81 -> set waypointIndex to 2, modeState to 99, isLoaded to true, return false'):format(nameNum(vehicle), curFile, debug.getinfo(1).currentline)); -- DEBUG140301
courseplay:unregisterFromCombine(vehicle, vehicle.cp.activeCombine)
courseplay:setIsLoaded(vehicle, true);
courseplay:setModeState(vehicle, 0);
courseplay:setWaypointIndex(vehicle, 2);
elseif vehicle.cp.mode2nextState == 1 then
-- refSpeed = vehicle.cp.speeds.turn
courseplay:switchToNextMode2State(vehicle);
courseplay:setMode2NextState(vehicle, 0);
else
courseplay:switchToNextMode2State(vehicle);
courseplay:setMode2NextState(vehicle, 0);
end
end
end
end
-- STATE 6 (follow tractor)
local frontTractor;
if vehicle.cp.activeCombine and vehicle.cp.activeCombine.courseplayers and vehicle.cp.positionWithCombine then
frontTractor = vehicle.cp.activeCombine.courseplayers[vehicle.cp.positionWithCombine - 1];
end;
if vehicle.cp.modeState == 6 and frontTractor ~= nil then --Follow Tractor
courseplay:setInfoText(vehicle, "COURSEPLAY_FOLLOWING_TRACTOR");
--use the current tractor's sideToDrive as own
if frontTractor.sideToDrive ~= nil and frontTractor.sideToDrive ~= vehicle.sideToDrive then
courseplay:debug(string.format("%s: setting current tractor's sideToDrive (%s) as my own", nameNum(vehicle), tostring(frontTractor.sideToDrive)), 4);
vehicle.sideToDrive = frontTractor.sideToDrive;
end;
-- drive behind tractor
local backDistance = max(10,(turnDiameter + safetyDistance))
local dx,dz = AIVehicleUtil.getDriveDirection(frontTractor.cp.DirectionNode, x, y, z);
local x1, y1, z1 = worldToLocal(frontTractor.cp.DirectionNode, x, y, z)
local distance = Utils.vector2Length(x1, z1)
if z1 > -backDistance and dz > -0.9 then
-- tractor in front of tractor
-- left side of tractor
local cx_left, cy_left, cz_left = localToWorld(frontTractor.cp.DirectionNode, 30, 0, -backDistance-20)
-- righ side of tractor
local cx_right, cy_right, cz_right = localToWorld(frontTractor.cp.DirectionNode, -30, 0, -backDistance-20)
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, cx_left, y, cz_left)
-- distance to left position
local disL = Utils.vector2Length(lx, lz)
local rx, ry, rz = worldToLocal(vehicle.cp.DirectionNode, cx_right, y, cz_right)
-- distance to right position
local disR = Utils.vector2Length(rx, rz)
if disL < disR then
currentX, currentY, currentZ = cx_left, cy_left, cz_left
else
currentX, currentY, currentZ = cx_right, cy_right, cz_right
end
else
-- tractor behind tractor
currentX, currentY, currentZ = localToWorld(frontTractor.cp.DirectionNode, 0, 0, -backDistance * 1.5); -- -backDistance * 1
end;
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, currentX, currentY, currentZ)
dod = Utils.vector2Length(lx, lz)
-- if dod < 2 or (vehicle.cp.positionWithCombine == 2 and frontTractor.cp.modeState ~= 3 and dod < 100) then
if dod < 2 or (vehicle.cp.positionWithCombine == 2 and combine.courseplayers[1].cp.modeState ~= 3 and dod < 100) then
courseplay:debug(string.format('\tdod=%s, frontTractor.cp.modeState=%s -> brakeToStop', tostring(dod), tostring(frontTractor.cp.modeState)), 4);
allowedToDrive = false;
end
if combine.cp.isSugarBeetLoader or combine.cp.isWoodChipper then
if distance > 100 then
refSpeed = vehicle.cp.speeds.street
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
refSpeed = Utils.clamp(frontTractor.lastSpeedReal*3600, vehicle.cp.speeds.turn, vehicle.cp.speeds.field)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
else
if distance > 50 then
refSpeed = vehicle.cp.speeds.street
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
else
refSpeed = max(frontTractor.lastSpeedReal*3600,vehicle.cp.speeds.crawl)
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
end
end
--courseplay:debug(string.format("distance: %d dod: %d",distance,dod ), 4)
end
if vehicle.cp.modeState ~= 9 and (currentX == nil or currentZ == nil) then
if vehicle.cp.infoText == nil then
courseplay:setInfoText(vehicle, "COURSEPLAY_WAITING_FOR_WAYPOINT");
end
allowedToDrive = false;
end
if vehicle.cp.forcedToStop then
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_WANTS_ME_TO_STOP");
allowedToDrive = false;
end
if vehicle.showWaterWarning then