forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode2.lua
1838 lines (1637 loc) · 85 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;
local _;
--[[ 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
--]]
--
local STATE_DEFAULT = 0
local STATE_WAIT_AT_START = 1
local STATE_DRIVE_TO_COMBINE = 2
local STATE_DRIVE_TO_PIPE = 3
local STATE_DRIVE_TO_REAR = 4
local STATE_FOLLOW_TARGET_WPS = 5
local STATE_FOLLOW_TRACTOR = 6
local STATE_WAIT_FOR_PIPE = 7
local STATE_WAIT_FOR_COMBINE_TO_GET_OUT_OF_WAY = 9
local STATE_ALL_TRAILERS_FULL = 81
local STATE_SWITCH_SIDE = 10
function courseplay:handle_mode2(vehicle, dt)
local frontTractor;
-- STATE 0 (default, when not active)
if vehicle.cp.modeState == STATE_DEFAULT then
courseplay:setModeState(vehicle, STATE_WAIT_AT_START);
end
-- STATE 1 (wait for work at start point)
if vehicle.cp.modeState == STATE_WAIT_AT_START and vehicle.cp.activeCombine ~= nil then
courseplay:releaseCombineStop(vehicle,vehicle.cp.activeCombine)
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.tooIsDirty = true
return false
end
-- STATE 10 (switch side)
if vehicle.cp.activeCombine ~= nil and (vehicle.cp.modeState == STATE_SWITCH_SIDE 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)
vehicle.cp.curTarget.rev = false
else
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(node, -25, 0, 0)
vehicle.cp.curTarget.rev = false
end
courseplay:setModeState(vehicle, STATE_FOLLOW_TARGET_WPS);
courseplay:setMode2NextState(vehicle, STATE_DRIVE_TO_COMBINE);
end
-- Trailers full?
if currentTipper.cp.fillLevel >= currentTipper.cp.capacity or vehicle.cp.isLoaded then
if #(vehicle.cp.workTools) > vehicle.cp.currentTrailerToFill and not vehicle.cp.isLoaded then -- TODO (Jakob): use numWorkTools
-- got more than one trailer, switch to next
vehicle.cp.currentTrailerToFill = vehicle.cp.currentTrailerToFill + 1
else
-- one trailer and that's full
vehicle.cp.currentTrailerToFill = nil
if vehicle.cp.modeState ~= STATE_FOLLOW_TARGET_WPS then
if vehicle.cp.modeState == STATE_WAIT_FOR_COMBINE_TO_GET_OUT_OF_WAY then
vehicle.cp.nextTargets ={}
end
local targetIsInFront = false
local cx,cz = vehicle.Waypoints[1].cx, vehicle.Waypoints[1].cz
local cy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, cx, 0, cz)
if vehicle.cp.realisticDriving then
-- generate course to target around fruit when needed but don't end course in turnDiameter distance
-- before to avoid circling when transitioning to the next mode
if courseplay:calculateAstarPathToCoords(vehicle,nil,cx,cz, vehicle.cp.turnDiameter * 2 ) then
courseplay:unregisterFromCombine(vehicle, vehicle.cp.activeCombine)
courseplay:setCurrentTargetFromList(vehicle, 1);
end
end
--turn away from combine to avoid crashing into it
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)
vehicle.cp.curTarget.rev = false
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))
vehicle.cp.curTarget.rev = false
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))
vehicle.cp.curTarget.rev = false
end
end
courseplay:setModeState(vehicle, STATE_FOLLOW_TARGET_WPS);
courseplay:setMode2NextState(vehicle, STATE_ALL_TRAILERS_FULL );
end
end
end
-- Have enough payload, can now drive back to the silo
if vehicle.cp.modeState == STATE_WAIT_AT_START and (vehicle.cp.totalFillLevelPercent >= vehicle.cp.driveOnAtFillLevel or vehicle.cp.isLoaded) then
vehicle.cp.currentTrailerToFill = nil
courseplay:debug(string.format("%s (%s): wait for work but trailers full.", nameNum(vehicle), tostring(vehicle.id)), 4);
if vehicle.cp.realisticDriving then
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0, 0, 1)
local cx,cz = vehicle.Waypoints[1].cx, vehicle.Waypoints[1].cz
-- generate course to target around fruit when needed but make sure we end the course far enough
-- before to avoid circling when transitioning to the next mode
if courseplay:calculateAstarPathToCoords(vehicle,nil,cx,cz, vehicle.cp.turnDiameter * 2 ) then
courseplay:setCurrentTargetFromList(vehicle, 1);
courseplay:setModeState(vehicle, STATE_FOLLOW_TARGET_WPS);
courseplay:setMode2NextState(vehicle, STATE_ALL_TRAILERS_FULL );
else
-- start combi course at 2nd wp
courseplay:setWaypointIndex(vehicle, 2);
courseplay:startAlignmentCourse( vehicle, vehicle.Waypoints[ 1 ])
courseplay:setIsLoaded(vehicle, true);
end
else
courseplay:setWaypointIndex(vehicle, 2);
courseplay:startAlignmentCourse( vehicle, vehicle.Waypoints[ 1 ])
courseplay:setIsLoaded(vehicle, true);
end
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 == STATE_FOLLOW_TARGET_WPS then
if vehicle.cp.modeState == STATE_FOLLOW_TRACTOR then
courseplay:setModeState(vehicle, STATE_DRIVE_TO_COMBINE);
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)
if vehicle.cp.modeState == STATE_FOLLOW_TARGET_WPS and vehicle.cp.mode2nextState == STATE_FOLLOW_TRACTOR then
if #vehicle.cp.nextTargets == 0 then
courseplay:setModeState(vehicle, STATE_FOLLOW_TRACTOR);
end
else
courseplay:setModeState(vehicle, STATE_FOLLOW_TRACTOR);
end
courseplay:unload_combine(vehicle, dt)
end
else -- NO active combine
-- fake a last combine if we need to
if vehicle.cp.modeState == STATE_FOLLOW_TARGET_WPS and vehicle.cp.nextTargets ~= nil and vehicle.cp.lastActiveCombine == nil and vehicle.cp.mode2nextState and vehicle.cp.mode2nextState == STATE_ALL_TRAILERS_FULL then
-- this can happen when we turn on combi mode with the trailer full before the tractor ever had a combine assigned
-- let's see if there's a combine around
if vehicle.cp.reachableCombines and #vehicle.cp.reachableCombines > 0 then
-- fake a last combine
courseplay:debug( "Trailer full, picked a reachable combine to be able to call unload_combine()", 4 )
vehicle.cp.lastActiveCombine = vehicle.cp.reachableCombines[ 1 ]
end
end
if vehicle.cp.modeState == STATE_FOLLOW_TARGET_WPS and vehicle.cp.nextTargets ~= nil and vehicle.cp.lastActiveCombine then
courseplay:unload_combine(vehicle, dt)
else
-- STOP!!
courseplay:checkSaveFuel(vehicle,false)
AIVehicleUtil.driveInDirection(vehicle, dt, vehicle.cp.steeringAngle, 0, 0, 28, false, moveForwards, 0, 1)
courseplay:resetSlippingTimers(vehicle)
-- We are loaded and have no lastActiveCombine Aviable to us
if vehicle.cp.isLoaded and vehicle.cp.lastActiveCombine == nil 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 or vehicle.cp.totalFillLevelPercent == 100 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);
local registered,combineIsTurning = courseplay:registerAtCombine(vehicle, vehicle.cp.bestCombine)
if registered then
courseplay:setModeState(vehicle, STATE_DRIVE_TO_COMBINE);
elseif combineIsTurning then
courseplay:setInfoText(vehicle,"COURSEPLAY_COMBINE_IS_TURNING")
end
else
courseplay:setInfoText(vehicle,"COURSEPLAY_WAITING_FOR_FILL_LEVEL")
end
local smallestTimeDiff = math.huge;
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 = math.huge;
-- chose the combine who needs me the most
for k, combine in pairs(vehicle.cp.reachableCombines) do
courseplay:setOwnFillLevelsAndCapacities(combine)
local fillLevel, capacity = combine.cp.fillLevel, combine.cp.capacity
if combine.acParameters ~= nil and combine.acParameters.enabled and combine.isHired and fillLevel >= 0.99*capacity and not combine.cp.isDriving 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 or combine.cp.isSugarBeetLoader then
if capacity == 0 or combine.cp.isSugarBeetLoader 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.totalFillLevelPercent and frontTractor.cp.totalFillLevelPercent >= 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.totalFillLevelPercent
vehicle.cp.combineID = combine.id
end
-- experimental script for big fields
-- checks the time needed to reach combine in time and start earlier if it's time to
-- it's not a precise calculation but it should work somehow... (calculating the true path to all combines every 5 sec is too expensive)
elseif combine.cp.fillLitersPerSecond and not combine.cp.driverPriorityUseFillLevel then
local distanceToCombine = courseplay:distanceToObject(vehicle, combine);
local capacity = combine:getUnitCapacity(combine.overloading.fillUnitIndex)
local fillLevel = combine:getUnitFillLevel(combine.overloading.fillUnitIndex)
local triggerFillLevel = capacity* vehicle.cp.followAtFillLevel / 100
local timeToReachFillLevel = (triggerFillLevel - fillLevel)/combine.cp.fillLitersPerSecond
local approxTimeToCombine = distanceToCombine /(vehicle.cp.speeds.field/3.6)
--print(string.format("timeToReachFillLevel:%s ; approxTimeToCombine: %s ",tostring(timeToReachFillLevel),tostring(approxTimeToCombine)))
if timeToReachFillLevel < approxTimeToCombine then
--print(string.format("timeToReachFillLevel:%s ; approxTimeToCombine: %s ",tostring(timeToReachFillLevel),tostring(approxTimeToCombine)))
if smallestTimeDiff > approxTimeToCombine-timeToReachFillLevel then
smallestTimeDiff = approxTimeToCombine-timeToReachFillLevel
local otherIsCloser = false
--is an other mode2 courseplayer closer ?
for k, courseplayer in pairs(CpManager.activeCoursePlayers) do
if courseplayer.cp.mode == 2 and courseplayer.cp.modeState == STATE_WAIT_AT_START then
local vehiclesDistance = courseplay:distanceToObject(courseplayer, combine);
if vehiclesDistance < distanceToCombine then
otherIsCloser = true
--print(" "..nameNum(courseplayer).." is closer so don't check in")
end
end
end
if not otherIsCloser then
vehicle.cp.bestCombine = combine
vehicle.cp.distanceToCombine = distanceToCombine
vehicle.cp.callCombineFillLevel = vehicle.cp.totalFillLevelPercent
vehicle.cp.combineID = combine.id
end
end
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
elseif vehicle.cp.reachableCombineIsInFruit then
courseplay:setInfoText(vehicle, "COURSEPLAY_COMBINE_IN_FRUIT");
else
courseplay:setInfoText(vehicle, "COURSEPLAY_NO_COMBINE_IN_REACH");
end
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 or vehicle.cp.lastActiveCombine;
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 = {};
local speedDebugLine;
-- 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
courseplay:setOwnFillLevelsAndCapacities(combine)
local fillLevel, capacity = combine.cp.fillLevel, combine.cp.capacity;
if capacity > 0 then
combineFillLevel = fillLevel * 100 / capacity
else -- combine is a chopper / has no tank
combineFillLevel = 99;
end
-- TODO: confusing as hell, in the next sections we sometimes use tractor, sometimes combine
local tractor = combine
if courseplay:isAttachedCombine(combine) then
-- this is the tractor pulling a harvester
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 reverser = 1
if tractor.isReverseDriving then
reverser = -1
end
local combineIsStopped = tractor.lastSpeedReal*3600 < 0.5
-- auto combine
local AutoCombineIsTurning = false
local combineIsAutoCombine = false
local autoCombineExtraMoveBack = 0
local autoCombineCircleMode = false
--print(('tractor.acParameters = %s tractor.acParameters.enabled = %s tractor.acTurnStage = %s tractor.isHired = %s'):format(tostring(tractor.acParameters),tostring(tractor.acParameters.enabled),tostring(tractor.acTurnStage),tostring(tractor.isHired)))
if tractor.acParameters ~= nil and tractor.acParameters.enabled and tractor.isHired and not tractor.cp.isDriving then
combineIsAutoCombine = true
autoCombineCircleMode = not tractor.acParameters.upNDown
if tractor.cp.turnStage == nil then
tractor.cp.turnStage = 0
end
if autoCombineCircleMode and tractor.cp.isChopper then
tractor.acTurnMode = '7'
end;
-- if tractor.acTurnStage ~= 0 then
if tractor.acTurnStage > 0 then
tractor.cp.turnStage = 2
autoCombineExtraMoveBack = vehicle.cp.turnDiameter*2
AutoCombineIsTurning = true
courseplay:debug(string.format('%s: acTurnStage=%d -> cp.turnState=2, AutoCombineIsTurning=true', nameNum(tractor), tractor.acTurnStage), 4); --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 = false
for index,strategy in pairs(tractor.driveStrategies) do
if strategy.activeTurnStrategy ~= nil then
combine.cp.turnStrategyIndex = index
strategy.activeTurnStrategy.didNotMoveTimer = strategy.activeTurnStrategy.didNotMoveTimeout;
aiTurn = true
end
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 == STATE_DRIVE_TO_COMBINE or vehicle.cp.modeState == STATE_DRIVE_TO_PIPE or vehicle.cp.modeState == STATE_DRIVE_TO_REAR 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)
x1,z1 = x1*reverser,z1*reverser;
local distance = Utils.vector2Length(x1, z1)
local safetyDistance = courseplay:getSafetyDistanceFromCombine( combine )
-- STATE 2 (drive to combine)
if vehicle.cp.modeState == STATE_DRIVE_TO_COMBINE then
refSpeed = vehicle.cp.speeds.field
speedDebugLine = ("mode2("..tostring(debug.getinfo(1).currentline-1).."): refSpeed = "..tostring(refSpeed))
courseplay:setInfoText(vehicle, "COURSEPLAY_DRIVE_BEHIND_COMBINE");
-- calculate a world position (currentX/Y/Z) and a vector (lx/lz) to a point near the combine (which is sometimes called 'tractor')
-- here, 'tractor' is the combine, x, y, z is the tractor unloading the combine, z1, y1, z1 is the tractor's local coordinates from
-- the combine
local x1, y1, z1 = worldToLocal(tractor.cp.DirectionNode or tractor.rootNode, x, y, z)
x1,z1 = x1*reverser,z1*reverser;
if not combine.cp.isChopper then
cx_behind, cy_behind, cz_behind = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, vehicle.cp.combineOffset*reverser, 0, -(turnDiameter + safetyDistance)*reverser)
else
cx_behind, cy_behind, cz_behind = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, 0, 0, -(turnDiameter + safetyDistance)*reverser)
end
if z1 > -(turnDiameter + safetyDistance) then
-- tractor in front of combine, drive to a position where we can safely transfer to STATE_DRIVE_TO_REAR mode
-- left side of combine, 30 meters back, 20 to the left
local cx_left, cy_left, cz_left = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, 20*reverser, 0, -30*reverser)
-- righ side of combine, 30 meters back, 20 to the right
local cx_right, cy_right, cz_right = localToWorld(tractor.cp.DirectionNode or tractor.rootNode, -20*reverser, 0, -30*reverser)
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)
-- prefer the one closest to the combine
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, drive to a position behind the combine
currentX, currentY, currentZ = cx_behind, cy_behind, cz_behind
end
-- at this point, currentX/Y/Z is a world position near the combine
-- with no path finding, get vector to currentX/currentZ
local lx, ly, lz = worldToLocal(vehicle.cp.DirectionNode, currentX, currentY, currentZ)
lx,lz = lx*reverser,lz*reverser
dod = Utils.vector2Length(lx, lz)
-- PATHFINDING / REALISTIC DRIVING -
-- if it is enabled and we are not too close to the combine, we abort STATE_DRIVE_TO_COMBINE mode and
-- switch to follow course mode to avoid fruit instead of driving directly
-- to currentX/currentZ
if vehicle.cp.realisticDriving and dod > 20 then
-- if there's fruit between me and the combine, calculate a path around it to a point
-- behind the combine.
if courseplay:calculateAstarPathToCoords(vehicle, nil, cx_behind, cz_behind, nil ) then
-- there's fruit and a path could be calculated, switch to waypoint mode
courseplay.debugVehicle( 4, vehicle, "Combine is %.1f meters away, switching to pathfinding, drive to a point %.1f (%.1f safety distance and %.1f turn diameter) behind to combine",
dod, safetyDistance + turnDiameter, safetyDistance, turnDiameter )
courseplay:setCurrentTargetFromList(vehicle, 1);
courseplay:setModeState(vehicle, STATE_FOLLOW_TARGET_WPS);
courseplay:setMode2NextState(vehicle, STATE_DRIVE_TO_COMBINE); -- modeState when waypoint is reached
vehicle.cp.shortestDistToWp = nil;
end;
end;
-- 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, STATE_DRIVE_TO_REAR);
end;
-- END STATE 2
-- STATE 4 (drive to rear of combine)
elseif vehicle.cp.modeState == STATE_DRIVE_TO_REAR then
if combine.cp.offset == nil or vehicle.cp.combineOffset == 0 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
if combine.cp.isSugarBeetLoader then
local prnToCombineZ = courseplay:calculateVerticalOffset(vehicle, combine);
tX, tY, tZ = localToWorld(combineDirNode, vehicle.cp.combineOffset, 0, prnToCombineZ -5*reverser);
else
tX, tY, tZ = localToWorld(combineDirNode, vehicle.cp.combineOffset, 0, -5*reverser);
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
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, STATE_DRIVE_TO_PIPE); -- change to modeState 3 == drive to unload pipe
vehicle.cp.chopperIsTurning = false
end
if dod > 50 then
courseplay:setModeState(vehicle, STATE_DRIVE_TO_COMBINE);
end
-- END STATE 4
-- STATE 3 (drive to unload pipe)
elseif vehicle.cp.modeState == STATE_DRIVE_TO_PIPE 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.isSugarBeetLoader and (not combine.cp.isChopper or combine.haeckseldolly) and (combineFillLevel < 1 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 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
courseplay:debug(string.format("%s: combine is empty and turning",nameNum(vehicle)),4)
if combineIsAutoCombine then
local index = combine.aiveChain.trace.traceIndex+1
if index > #combine.aiveChain.trace.trace then
index = 1
end
local tipperX,tipperY,tipperZ = getWorldTranslation(currentTipper.rootNode)
local dirX,dirZ = combine.aiveChain.trace.trace[index].dx,combine.aiveChain.trace.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
if vehicle.cp.isReversePossible and vehicle.cp.turnOnField then
local maxDiameter = max(20,turnDiameter);
local maxX,_,maxZ = localToWorld(vehicle.cp.DirectionNode,-sideMultiplier*maxDiameter,0,-(trailerOffset+(0.5*maxDiameter)));
local fieldX,_,fieldZ = localToWorld(vehicle.cp.DirectionNode,0,0,-(trailerOffset+(0.5*maxDiameter)));
courseplay:debug(string.format("%s: is reverse possible",nameNum(vehicle)),4)
if courseplay:isField(maxX, maxZ, 1, 1) and courseplay:getFieldNumForPosition(fieldX,fieldZ) == courseplay:getFieldNumForPosition(maxX,maxZ) then --is the outside point of the course on the field and same field, the make a nice circle
courseplay:debug(string.format("%s: points are on field -> turn",nameNum(vehicle)),4)
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0,0,-(trailerOffset+totalLength+(0.5*maxDiameter)));
vehicle.cp.curTarget.rev = true;
vehicle.cp.nextTargets = courseplay:createTurnAwayCourse(vehicle,-sideMultiplier,maxDiameter,offset,trailerOffset+(0.5*maxDiameter))
courseplay:addNewTargetVector(vehicle,sideMultiplier*offset,-(2*maxDiameter+1.5*totalLength),currentTipper,vehicle.cp.cpTurnBaseNode);
else --go reverse directly
courseplay:debug(string.format("%s: points are not on field -> reverse directly",nameNum(vehicle)),4)
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(currentTipper.rootNode, 0, 0, -(1.5*totalLength));
vehicle.cp.curTarget.rev = true
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset/2, (-totalLength*3),currentTipper,vehicle.cp.cpTurnBaseNode,true);
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset, (-totalLength*4),currentTipper,vehicle.cp.cpTurnBaseNode,true);
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset, (-totalLength*5),currentTipper,vehicle.cp.cpTurnBaseNode,true);
end
else
courseplay:debug(string.format("%s: is not reverse possible",nameNum(vehicle)),4)
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(currentTipper.rootNode, -sideMultiplier*turnDiameter, 0, trailerOffset);
vehicle.cp.curTarget.rev = false
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.5)+trailerOffset,currentTipper,vehicle.cp.cpTurnBaseNode);
end
courseplay:debug(string.format("%s: addedNewTargetVector: currentTipper: %s, vehicle.cp.cpTurnBaseNode: %s",nameNum(vehicle),tostring(currentTipper),tostring(vehicle.cp.cpTurnBaseNode)),4)
courseplay:setModeState(vehicle, STATE_FOLLOW_TARGET_WPS);
vehicle.cp.isParking = true
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
courseplay:debug(string.format("%s: combine is isHarvesterAttachable move out of the way",nameNum(vehicle)),4)
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(currentTipper.rootNode, 0 , 0, 5);
vehicle.cp.curTarget.rev = false
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);
vehicle.cp.curTarget.rev = false
end
courseplay:addNewTargetVector(vehicle, sideMultiplier*offset ,(totalLength*3)+trailerOffset,currentTipper);
courseplay:setModeState(vehicle, STATE_WAIT_FOR_COMBINE_TO_GET_OUT_OF_WAY);
vehicle.cp.isParking = true
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, STATE_WAIT_AT_START);
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, STATE_DRIVE_TO_COMBINE);
end
-- combine is not moving and trailer is under pipe
if lz < 5 and combine.cp.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, STATE_DRIVE_TO_COMBINE);
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 > 2 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 not combine.overloading.isActive 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 == STATE_WAIT_FOR_COMBINE_TO_GET_OUT_OF_WAY 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, STATE_FOLLOW_TARGET_WPS);
else
allowedToDrive = false
courseplay:setInfoText(vehicle, "COURSEPLAY_WAITING_TILL_WAITING_POSITION_IS_FREE");
end
if combineIsTurning then
vehicle.cp.swayPointDistance = nil
courseplay:setModeState(vehicle, STATE_DRIVE_TO_PIPE);
vehicle.cp.forceNewTargets = true
vehicle.cp.nextTargets = {}
end
end;
---------------------------------------------------------------------
local stopAICombine = false
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 > STATE_WAIT_AT_START then
if combine.cp.fillLevel > combine.cp.capacity*0.9 then
if combineIsAutoCombine and tractor.acIsCPStopped ~= nil then
courseplay:debug(nameNum(tractor) .. ': fillLevel > 90%% -> set acIsCPStopped to true', 4); --TODO: 140308 AutoTractor
tractor.acIsCPStopped = true
elseif combine.aiIsStarted then
stopAICombine = true
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 > STATE_DRIVE_TO_COMBINE then
if AutoCombineIsTurning and tractor.acIsCPStopped ~= nil then
courseplay:debug(nameNum(tractor) .. ': distance < 50 -> set acIsCPStopped to true', 4); --TODO: 140308 AutoTractor
tractor.acIsCPStopped = true
elseif combine.aiIsStarted then --and not (combineFillLevel == 0 and combine.currentPipeState ~= 2) then
stopAICombine = true
--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 == STATE_DRIVE_TO_COMBINE then
allowedToDrive = false;
end
elseif vehicle.cp.fieldEdgeTimeOutSet then
vehicle.cp.fieldEdgeTimeOutSet = false
--print("reset vehicle.cp.fieldEdgeTimeOutSet")
end
if combine.aiIsStarted and stopAICombine and combine.cruiseControl.speed > 0 then
combine.cp.lastCruiseControlSpeed = combine.cruiseControl.speed
combine.cruiseControl.speed = 0
end
if combineIsTurning and distance < 20 then
if vehicle.cp.modeState == STATE_DRIVE_TO_PIPE or vehicle.cp.modeState == STATE_DRIVE_TO_REAR then
if combine.cp.isChopper then
local fruitSide = courseplay:sideToDrive(vehicle, combine, -10,true);
local maxDiameter = max(totalLength,turnDiameter)
local extraAlignLength = 9
if vehicle.cp.distances ~= nil and vehicle.cp.distances.frontWheelToRearWheel ~=nil then
extraAlignLength = vehicle.cp.distances.frontWheelToRearWheel*3;
end
--local extraAlignLength = courseplay:getDirectionNodeToTurnNodeLength(vehicle)*2+6;
--another 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);
vehicle.cp.curTarget.rev = false
courseplay:addNewTargetVector(vehicle, 2*turnDiameter*-1 , turnDiameter);
vehicle.cp.chopperIsTurning = true
else --i'm right of choppper
if vehicle.cp.isReversePossible and not autoCombineCircleMode and combine.cp.forcedSide == nil and combine.cp.multiTools == 1 and vehicle.cp.turnOnField then
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns left, I'm right. Turning the New Way", curFile, debug.getinfo(1).currentline, nameNum(vehicle), tostring(combine.name)), 4);
local maxDiameter = max(20,vehicle.cp.turnDiameter)
local verticalWaypointShift = courseplay:getWaypointShift(vehicle,tractor)
tractor.cp.verticalWaypointShift = verticalWaypointShift
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0,0,3);
vehicle.cp.curTarget.rev = false
vehicle.cp.nextTargets = courseplay:createTurnAwayCourse(vehicle,-1,maxDiameter,tractor.cp.workWidth)
courseplay:addNewTargetVector(vehicle,tractor.cp.workWidth,-(max(maxDiameter +vehicle.cp.totalLength+extraAlignLength,maxDiameter +vehicle.cp.totalLength +extraAlignLength -verticalWaypointShift)))
courseplay:addNewTargetVector(vehicle,tractor.cp.workWidth, 2 +verticalWaypointShift,nil,nil,true);
else
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns left, I'm right. Turning the Old Way", 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
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);
vehicle.cp.curTarget.rev = false
courseplay:addNewTargetVector(vehicle, 2*turnDiameter, turnDiameter);
vehicle.cp.chopperIsTurning = true
else -- I'm left of chopper
if vehicle.cp.isReversePossible and not autoCombineCircleMode and combine.cp.forcedSide == nil and combine.cp.multiTools == 1 and vehicle.cp.turnOnField then
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns right, I'm left. Turning the new way", curFile, debug.getinfo(1).currentline, nameNum(vehicle), tostring(combine.name)), 4);
local maxDiameter = max(20,vehicle.cp.turnDiameter)
local verticalWaypointShift = courseplay:getWaypointShift(vehicle,tractor)
tractor.cp.verticalWaypointShift = verticalWaypointShift
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = localToWorld(vehicle.cp.DirectionNode, 0,0,3);
vehicle.cp.curTarget.rev = false
vehicle.cp.nextTargets = courseplay:createTurnAwayCourse(vehicle,1,maxDiameter,tractor.cp.workWidth)
courseplay:addNewTargetVector(vehicle,-tractor.cp.workWidth,-(max(maxDiameter +vehicle.cp.totalLength+extraAlignLength,maxDiameter +vehicle.cp.totalLength +extraAlignLength -verticalWaypointShift)))
courseplay:addNewTargetVector(vehicle,-tractor.cp.workWidth, 2 +verticalWaypointShift,nil,nil,true);
else
courseplay:debug(string.format("%s(%i): %s @ %s: combine turns right, I'm left. Turning the old way", 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
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;