forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.lua
1610 lines (1396 loc) · 62.6 KB
/
settings.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 = 'settings.lua';
local abs, ceil, max, min = math.abs, math.ceil, math.max, math.min;
function courseplay:openCloseHud(vehicle, open)
courseplay:setMouseCursor(vehicle, open);
vehicle.cp.hud.show = open;
if open then
courseplay.buttons:setActiveEnabled(vehicle, 'all');
else
courseplay.buttons:setHoveredButton(vehicle, nil);
end;
end;
function courseplay:setCpMode(vehicle, modeNum)
if vehicle.cp.mode ~= modeNum then
vehicle.cp.mode = modeNum;
courseplay:setNextPrevModeVars(vehicle);
courseplay.utils:setOverlayUVsPx(vehicle.cp.hud.currentModeIcon, courseplay.hud.bottomInfo.modeUVsPx[modeNum], courseplay.hud.iconSpriteSize.x, courseplay.hud.iconSpriteSize.y);
courseplay.buttons:setActiveEnabled(vehicle, 'all');
if modeNum == 1 then
courseplay:resetTools(vehicle);
end;
end;
end;
function courseplay:setNextPrevModeVars(vehicle)
local curMode = vehicle.cp.mode;
local nextMode, prevMode, nextModeTest, prevModeTest = nil, nil, curMode + 1, curMode - 1;
if curMode > courseplay.MODE_GRAIN_TRANSPORT then
while prevModeTest >= courseplay.MODE_GRAIN_TRANSPORT do
if courseplay:getCanVehicleUseMode(vehicle, prevModeTest) then
prevMode = prevModeTest;
break;
else
-- invalid mode --> skip
prevModeTest = prevModeTest - 1;
end;
end;
end;
vehicle.cp.prevMode = prevMode;
if curMode < courseplay.NUM_MODES then
while nextModeTest <= courseplay.NUM_MODES do
if courseplay:getCanVehicleUseMode(vehicle, nextModeTest) then
nextMode = nextModeTest;
break;
else
-- invalid mode --> skip
nextModeTest = nextModeTest + 1;
end;
end;
end;
vehicle.cp.nextMode = nextMode;
end;
function courseplay:getCanVehicleUseMode(vehicle, mode)
if mode == courseplay.MODE_COMBINE_SELF_UNLOADING and not vehicle.cp.isCombine and not vehicle.cp.isChopper and not vehicle.cp.isHarvesterSteerable then
return false;
elseif (vehicle.cp.isCombine or vehicle.cp.isChopper or vehicle.cp.isHarvesterSteerable) and (mode ~= courseplay.MODE_TRANSPORT and mode ~= courseplay.MODE_FIELDWORK and mode ~= courseplay.MODE_COMBINE_SELF_UNLOADING) then
return false;
elseif mode ~= courseplay.MODE_TRANSPORT and (vehicle.cp.isWoodHarvester or vehicle.cp.isWoodForwarder) then
return false;
end;
return true;
end;
function courseplay:toggleWantsCourseplayer(combine)
combine.cp.wantsCourseplayer = not combine.cp.wantsCourseplayer;
end;
function courseplay:startStopCourseplayer(combine)
local tractor = combine.courseplayers[1];
tractor.cp.forcedToStop = not tractor.cp.forcedToStop;
end;
function courseplay:setVehicleWait(vehicle, active)
vehicle.cp.wait = active;
end;
function courseplay:cancelWait(vehicle, cancelStopAtEnd)
if vehicle.cp.wait then
courseplay:setVehicleWait(vehicle, false);
end;
if vehicle.cp.mode == 3 then
vehicle.cp.isUnloaded = true;
end;
if cancelStopAtEnd then
courseplay:setStopAtEnd(vehicle, false);
end;
end;
function courseplay:setStopAtEnd(vehicle, bool)
vehicle.cp.stopAtEnd = bool;
end;
function courseplay:setIsLoaded(vehicle, bool)
if vehicle.cp.isLoaded ~= bool then
vehicle.cp.isLoaded = bool;
end;
end;
function courseplay:sendCourseplayerHome(combine)
courseplay:setIsLoaded(combine.courseplayers[1], true);
end
function courseplay:switchCourseplayerSide(combine)
if courseplay:isChopper(combine) then
local tractor = combine.courseplayers[1];
if tractor == nil then
return;
end;
courseplay:setModeState(tractor, 10);
if combine.cp.forcedSide == nil then
combine.cp.forcedSide = "left";
elseif combine.cp.forcedSide == "left" then
combine.cp.forcedSide = "right";
else
combine.cp.forcedSide = nil;
end;
end;
end;
function courseplay:setHudPage(vehicle, pageNum)
if vehicle.cp.mode == nil then
vehicle.cp.hud.currentPage = pageNum;
elseif courseplay.hud.pagesPerMode[vehicle.cp.mode] ~= nil and courseplay.hud.pagesPerMode[vehicle.cp.mode][pageNum] then
if pageNum == 0 then
if vehicle.cp.minHudPage == 0 or vehicle.cp.isCombine or vehicle.cp.isChopper or vehicle.cp.isHarvesterSteerable or vehicle.cp.isSugarBeetLoader or vehicle.cp.attachedCombine ~= nil then
vehicle.cp.hud.currentPage = pageNum;
end;
else
vehicle.cp.hud.currentPage = pageNum;
end;
end;
courseplay.hud:setReloadPageOrder(vehicle, vehicle.cp.hud.currentPage, true);
courseplay.buttons:setActiveEnabled(vehicle, "all");
end;
function courseplay:changeCombineOffset(vehicle, changeBy)
local previousOffset = vehicle.cp.combineOffset;
vehicle.cp.combineOffsetAutoMode = false;
vehicle.cp.combineOffset = courseplay:round(vehicle.cp.combineOffset, 1) + changeBy;
if abs(vehicle.cp.combineOffset) < 0.1 then
vehicle.cp.combineOffset = 0.0;
vehicle.cp.combineOffsetAutoMode = true;
end;
courseplay:debug(nameNum(vehicle) .. ": manual combine_offset change: prev " .. previousOffset .. " // new " .. vehicle.cp.combineOffset .. " // auto = " .. tostring(vehicle.cp.combineOffsetAutoMode), 4);
end
function courseplay:changeTipperOffset(vehicle, changeBy)
vehicle.cp.tipperOffset = courseplay:round(vehicle.cp.tipperOffset, 1) + changeBy;
if abs(vehicle.cp.tipperOffset) < 0.1 then
vehicle.cp.tipperOffset = 0;
end;
end
function courseplay:changeLaneOffset(vehicle, changeBy, force)
vehicle.cp.laneOffset = force or (courseplay:round(vehicle.cp.laneOffset, 1) + changeBy);
if abs(vehicle.cp.laneOffset) < 0.1 then
vehicle.cp.laneOffset = 0;
end;
vehicle.cp.totalOffsetX = vehicle.cp.laneOffset + vehicle.cp.toolOffsetX;
end;
function courseplay:changeToolOffsetX(vehicle, changeBy, force, noDraw)
vehicle.cp.toolOffsetX = force or (courseplay:round(vehicle.cp.toolOffsetX, 1) + changeBy);
if abs(vehicle.cp.toolOffsetX) < 0.1 then
vehicle.cp.toolOffsetX = 0;
end;
vehicle.cp.totalOffsetX = vehicle.cp.laneOffset + vehicle.cp.toolOffsetX;
if not noDraw and vehicle.cp.mode ~= 2 and vehicle.cp.mode ~= 3 and vehicle.cp.mode ~= 7 then
courseplay:setCustomTimer(vehicle, 'showWorkWidth', 2);
end;
end;
function courseplay:changeToolOffsetZ(vehicle, changeBy, force, noDraw)
vehicle.cp.toolOffsetZ = force or (courseplay:round(vehicle.cp.toolOffsetZ, 1) + changeBy);
if abs(vehicle.cp.toolOffsetZ) < 0.1 then
vehicle.cp.toolOffsetZ = 0;
end;
if not noDraw and vehicle.cp.DirectionNode and vehicle.cp.backMarkerOffset and vehicle.cp.aiFrontMarker then
courseplay:setCustomTimer(vehicle, 'showWorkWidth', 2);
end;
end;
function courseplay:calculateWorkWidth(vehicle, noDraw)
local l,r;
courseplay:debug(('%s: calculateWorkWidth()'):format(nameNum(vehicle)), 7);
local vehL,vehR = courseplay:getCuttingAreaValuesX(vehicle);
courseplay:debug(('\tvehL=%s, vehR=%s'):format(tostring(vehL), tostring(vehR)), 7);
local implL,implR = -9999,9999;
if vehicle.attachedImplements then
for i,implement in pairs(vehicle.attachedImplements) do
local tool = implement.object
local workWidth = courseplay:getSpecialWorkWidth(tool);
if vehicle.cp.mode == 9 and tool.cp.hasSpecializationShovel then
workWidth = tool.sizeWidth
end
if workWidth then
courseplay:debug(('\tSpecial workWidth found: %.1fm'):format(workWidth), 7);
courseplay:changeWorkWidth(vehicle, nil, workWidth, noDraw);
return;
end;
local left, right = courseplay:getCuttingAreaValuesX(implement.object);
if left and right then
implL = max(implL, left);
implR = min(implR, right);
end;
courseplay:debug(('\t-> implL=%s, implR=%s'):format(tostring(implL), tostring(implR)), 7);
if tool.attachedImplements then
for j,subImplement in pairs(tool.attachedImplements) do
local subLeft, subRight = courseplay:getCuttingAreaValuesX(subImplement.object);
if subLeft and subRight then
implL = max(implL, subLeft);
implR = min(implR, subRight);
end;
courseplay:debug(('\t-> implL=%s, implR=%s'):format(j, tostring(implL), tostring(implR)), 7);
end;
end;
end;
end;
if implL == -9999 or implR == 9999 then
implL, implR = nil, nil;
courseplay:debug('\timplL=nil, implR=nil', 7);
end;
if vehL and vehR then
if implL and implR then
l = max(vehL, implL);
r = min(vehR, implR);
else
l = vehL;
r = vehR;
end;
else
if implL and implR then
l = implL;
r = implR;
else
l = 1.5;
r = -1.5;
end;
end;
local workWidth = l - r;
courseplay:debug(('\tl=%s, r=%s -> workWidth=l-r=%s'):format(tostring(l), tostring(r), tostring(workWidth)), 7);
courseplay:changeWorkWidth(vehicle, nil, workWidth, noDraw);
end;
function courseplay:getCuttingAreaValuesX(object)
courseplay:debug(('\tgetCuttingAreaValuesX(%s)'):format(nameNum(object)), 7);
if object.aiLeftMarker and object.aiRightMarker then
local x, y, z = getWorldTranslation(object.aiLeftMarker);
local left, _, _ = worldToLocal(object.cp.DirectionNode or object.rootNode, x, y, z);
x, y, z = getWorldTranslation(object.aiRightMarker);
local right, _, _ = worldToLocal(object.cp.DirectionNode or object.rootNode, x, y, z);
courseplay:debug(('\t\taiMarkers: left=%s, right=%s'):format(tostring(left), tostring(right)), 7);
if left < right then
local rightBackup = right;
right = left;
left = rightBackup;
courseplay:debug(('\t\tleft < right -> switch -> left=%s, right=%s'):format(tostring(left), tostring(right)), 7);
end;
return left, right;
end;
local areas = object.workAreas;
local min, max = math.min, math.max;
local left, right = -9999, 9999;
if areas and #areas > 0 then
for i=1,#areas do
for caType,node in pairs(areas[i]) do
if caType == 'start' or caType == 'height' or caType == 'width' then
local x, y, z = getWorldTranslation(node);
local caX, _, _ = worldToLocal(object.cp.DirectionNode or object.rootNode, x, y, z);
left = max(left, caX);
right = min(right, caX);
courseplay:debug(('\t\t\tarea %d, type=%s, caX=%s -> left=%s, right=%s'):format(i, tostring(caType), tostring(caX), tostring(left), tostring(right)), 7);
end;
end;
end;
end;
if left == -9999 or right == 9999 then
left, right = nil, nil;
courseplay:debug('\t\t\tareas=nil -> left=nil, right=nil', 7);
end;
courseplay:debug(('\t\tareas: left=%s, right=%s'):format(tostring(left), tostring(right)), 7);
return left, right;
end;
function courseplay:changeWorkWidth(vehicle, changeBy, force, noDraw)
if force then
vehicle.cp.workWidth = max(courseplay:round(abs(force), 1), 0.1);
else
if vehicle.cp.workWidth + changeBy > 10 then
if abs(changeBy) == 0.1 and not (Input.keyPressedState[Input.KEY_lalt]) then -- pressing left Alt key enables to have small 0.1 steps even over 10.0
changeBy = 0.5 * Utils.sign(changeBy);
elseif abs(changeBy) == 0.5 then
changeBy = 2 * Utils.sign(changeBy);
end;
end;
if (vehicle.cp.workWidth < 10 and vehicle.cp.workWidth + changeBy > 10) or (vehicle.cp.workWidth > 10 and vehicle.cp.workWidth + changeBy < 10) then
vehicle.cp.workWidth = 10;
else
vehicle.cp.workWidth = max(vehicle.cp.workWidth + changeBy, 0.1);
end;
end;
if not noDraw then
courseplay:setCustomTimer(vehicle, 'showWorkWidth', 2);
end;
end;
function courseplay:toggleShowVisualWaypointsStartEnd(vehicle, force, visibilityUpdate)
vehicle.cp.visualWaypointsStartEnd = Utils.getNoNil(force, not vehicle.cp.visualWaypointsStartEnd);
-- also deactivate "all" points when deactivating startEnd
if not vehicle.cp.visualWaypointsStartEnd then
courseplay:toggleShowVisualWaypointsAll(vehicle, false, false);
end;
if visibilityUpdate == nil or visibilityUpdate then
courseplay.buttons:setActiveEnabled(vehicle, 'visualWaypoints');
courseplay.signs:setSignsVisibility(vehicle);
end;
end;
function courseplay:toggleShowVisualWaypointsAll(vehicle, force, visibilityUpdate)
vehicle.cp.visualWaypointsAll = Utils.getNoNil(force, not vehicle.cp.visualWaypointsAll);
-- also activate "start/end" points when activating "all"
if vehicle.cp.visualWaypointsAll then
courseplay:toggleShowVisualWaypointsStartEnd(vehicle, true, false);
end;
if visibilityUpdate == nil or visibilityUpdate then
courseplay.buttons:setActiveEnabled(vehicle, 'visualWaypoints');
courseplay.signs:setSignsVisibility(vehicle);
end;
end;
function courseplay:toggleShowVisualWaypointsCrossing(vehicle, force, visibilityUpdate)
vehicle.cp.visualWaypointsCrossing = Utils.getNoNil(force, not vehicle.cp.visualWaypointsCrossing);
if visibilityUpdate == nil or visibilityUpdate then
courseplay.buttons:setActiveEnabled(vehicle, 'visualWaypoints');
courseplay.signs:setSignsVisibility(vehicle);
end;
end;
function courseplay:changeDriveOnAtFillLevel(vehicle, changeBy)
vehicle.cp.driveOnAtFillLevel = Utils.clamp(vehicle.cp.driveOnAtFillLevel + changeBy, 0, 100);
end
function courseplay:changeFollowAtFillLevel(vehicle, changeBy)
vehicle.cp.followAtFillLevel = Utils.clamp(vehicle.cp.followAtFillLevel + changeBy, 0, 100);
end
function courseplay:changeTurnDiameter(vehicle, changeBy)
vehicle.cp.turnDiameter = vehicle.cp.turnDiameter + changeBy;
vehicle.cp.turnDiameterAutoMode = false;
if vehicle.cp.turnDiameter < 0.5 then
vehicle.cp.turnDiameter = 0;
end;
if vehicle.cp.turnDiameter <= 0 then
vehicle.cp.turnDiameterAutoMode = true;
vehicle.cp.turnDiameter = vehicle.cp.turnDiameterAuto
end;
end
function courseplay:changeWaitTime(vehicle, changeBy)
vehicle.cp.waitTime = math.max(0, vehicle.cp.waitTime + changeBy);
end;
function courseplay:getCanHaveWaitTime(vehicle)
return vehicle.cp.mode == 1 or vehicle.cp.mode == 2 or vehicle.cp.mode == 5 or (vehicle.cp.mode == 6 and not vehicle.cp.hasBaleLoader) or vehicle.cp.mode == 8;
end;
function courseplay:changeTurnSpeed(vehicle, changeBy)
local speed = vehicle.cp.speeds.turn;
speed = Utils.clamp(speed + changeBy, vehicle.cp.speeds.minTurn, vehicle.cp.speeds.max);
vehicle.cp.speeds.turn = speed ;
end
function courseplay:changeFieldSpeed(vehicle, changeBy)
local speed = vehicle.cp.speeds.field;
speed = Utils.clamp(speed + changeBy, vehicle.cp.speeds.minField, vehicle.cp.speeds.max);
vehicle.cp.speeds.field = speed;
end
function courseplay:changeMaxSpeed(vehicle, changeBy)
if not vehicle.cp.speeds.useRecordingSpeed then
local speed = vehicle.cp.speeds.street;
speed = Utils.clamp(speed + changeBy, vehicle.cp.speeds.minStreet, vehicle.cp.speeds.max);
vehicle.cp.speeds.street = speed;
end;
end
function courseplay:changeReverseSpeed(vehicle, changeBy, force, forceReloadPage)
local speed = force or (vehicle.cp.speeds.reverse + changeBy);
if not force then
speed = Utils.clamp(speed, vehicle.cp.speeds.minReverse, vehicle.cp.speeds.max);
end;
vehicle.cp.speeds.reverse = speed;
if forceReloadPage then
courseplay.hud:setReloadPageOrder(vehicle, 5, true);
end;
end
function courseplay:toggleUseRecordingSpeed(vehicle)
vehicle.cp.speeds.useRecordingSpeed = not vehicle.cp.speeds.useRecordingSpeed;
end;
function courseplay:changeWarningLightsMode(vehicle, changeBy)
vehicle.cp.warningLightsMode = Utils.clamp(vehicle.cp.warningLightsMode + changeBy, courseplay.WARNING_LIGHTS_NEVER, courseplay.WARNING_LIGHTS_BEACON_ALWAYS);
end;
function courseplay:toggleOpenHudWithMouse(vehicle)
vehicle.cp.hud.openWithMouse = not vehicle.cp.hud.openWithMouse;
end;
function courseplay:toggleRealisticDriving(vehicle)
vehicle.cp.realisticDriving = not vehicle.cp.realisticDriving;
end;
function courseplay:toggleSearchCombineMode(vehicle)
vehicle.cp.searchCombineAutomatically = not vehicle.cp.searchCombineAutomatically;
if not vehicle.cp.searchCombineAutomatically then
courseplay:setSearchCombineOnField(vehicle, nil, 0);
end;
end;
function courseplay:setSearchCombineOnField(vehicle, changeDir, force)
if courseplay.fields.numAvailableFields == 0 or not vehicle.cp.searchCombineAutomatically then
vehicle.cp.searchCombineOnField = 0;
return;
end;
if force and courseplay.fields.fieldData[force] then
vehicle.cp.searchCombineOnField = force;
return;
end;
local newFieldNum = vehicle.cp.searchCombineOnField + changeDir;
if newFieldNum == 0 then
vehicle.cp.searchCombineOnField = newFieldNum;
return;
end;
while courseplay.fields.fieldData[newFieldNum] == nil do
if newFieldNum == 0 then
vehicle.cp.searchCombineOnField = newFieldNum;
return;
end;
newFieldNum = Utils.clamp(newFieldNum + changeDir, 0, courseplay.fields.numAvailableFields);
end;
vehicle.cp.searchCombineOnField = newFieldNum;
end;
function courseplay:selectAssignedCombine(vehicle, changeBy)
local combines = courseplay:getAllCombines();
vehicle.cp.selectedCombineNumber = Utils.clamp(vehicle.cp.selectedCombineNumber + changeBy, 0, #combines);
if vehicle.cp.selectedCombineNumber == 0 then
vehicle.cp.savedCombine = nil;
vehicle:setCpVar('HUD4savedCombineName',"",courseplay.isClient);
else
vehicle.cp.savedCombine = combines[vehicle.cp.selectedCombineNumber];
local combineName = vehicle.cp.savedCombine.name or courseplay:loc('COURSEPLAY_COMBINE');
local x1 = courseplay.hud.col2posX[4];
local x2 = courseplay.hud.buttonPosX[1] - getTextWidth(courseplay.hud.fontSizes.contentValue, ' (9999m)');
local shortenedName, firstChar, lastChar = Utils.limitTextToWidth(combineName, courseplay.hud.fontSizes.contentValue, x2 - x1, false, '...');
vehicle:setCpVar('HUD4savedCombineName',shortenedName,courseplay.isClient);
end;
courseplay:removeActiveCombineFromTractor(vehicle);
end;
function courseplay:removeActiveCombineFromTractor(vehicle)
if vehicle.cp.activeCombine ~= nil then
courseplay:unregisterFromCombine(vehicle, vehicle.cp.activeCombine);
end;
vehicle.cp.lastActiveCombine = nil;
courseplay.hud:setReloadPageOrder(vehicle, 4, true);
end;
function courseplay:removeSavedCombineFromTractor(vehicle)
vehicle.cp.savedCombine = nil;
vehicle.cp.selectedCombineNumber = 0;
vehicle:setCpVar('HUD4savedCombine',nil,courseplay.isClient);
vehicle:setCpVar('HUD4savedCombineName',nil,courseplay.isClient);
courseplay.hud:setReloadPageOrder(vehicle, 4, true);
end;
function courseplay:switchDriverCopy(vehicle, changeBy)
local drivers = courseplay:findDrivers(vehicle);
if drivers ~= nil then
vehicle.cp.selectedDriverNumber = Utils.clamp(vehicle.cp.selectedDriverNumber + changeBy, 0, #(drivers));
if vehicle.cp.selectedDriverNumber == 0 then
vehicle.cp.copyCourseFromDriver = nil;
vehicle.cp.hasFoundCopyDriver = false;
else
vehicle.cp.copyCourseFromDriver = drivers[vehicle.cp.selectedDriverNumber];
vehicle.cp.hasFoundCopyDriver = true;
end;
else
vehicle.cp.copyCourseFromDriver = nil;
vehicle.cp.selectedDriverNumber = 0;
vehicle.cp.hasFoundCopyDriver = false;
end;
end;
function courseplay:findDrivers(vehicle)
local foundDrivers = {}; -- resetting all drivers
for _,otherVehicle in pairs(g_currentMission.steerables) do
if otherVehicle.Waypoints ~= nil and otherVehicle.hasCourseplaySpec then
if otherVehicle.rootNode ~= vehicle.rootNode and #(otherVehicle.Waypoints) > 0 then
table.insert(foundDrivers, otherVehicle);
end;
end;
end;
return foundDrivers;
end;
function courseplay:copyCourse(vehicle)
if vehicle.cp.hasFoundCopyDriver ~= nil and vehicle.cp.copyCourseFromDriver ~= nil then
local src = vehicle.cp.copyCourseFromDriver;
vehicle.Waypoints = src.Waypoints;
vehicle:setCpVar('currentCourseName',src.cp.currentCourseName,courseplay.isClient);
vehicle.cp.loadedCourses = src.cp.loadedCourses;
vehicle.cp.numCourses = src.cp.numCourses;
courseplay:setWaypointIndex(vehicle, 1);
vehicle.cp.numWaypoints = #(vehicle.Waypoints);
vehicle.cp.numWaitPoints = src.cp.numWaitPoints;
vehicle.cp.numCrossingPoints = src.cp.numCrossingPoints;
courseplay:setIsRecording(vehicle, false);
courseplay:setRecordingIsPaused(vehicle, false);
vehicle:setIsCourseplayDriving(false);
vehicle:setCpVar('distanceCheck',false,courseplay.isClient);
vehicle:setCpVar('canDrive',true,courseplay.isClient);
vehicle.cp.abortWork = nil;
vehicle.cp.curTarget.x, vehicle.cp.curTarget.y, vehicle.cp.curTarget.z = nil, nil, nil;
vehicle.cp.nextTargets = {};
if vehicle.cp.activeCombine ~= nil then
courseplay:unregisterFromCombine(vehicle, vehicle.cp.activeCombine);
end
if vehicle.cp.mode == 2 or vehicle.cp.mode == 3 then
courseplay:setModeState(vehicle, 0);
-- print(('%s [%s(%d)]: copyCourse(): mode=%d -> set modeState to 0'):format(nameNum(vehicle), curFile, debug.getinfo(1).currentline, vehicle.cp.mode)); -- DEBUG140301
else
courseplay:setModeState(vehicle, 1);
-- print(('%s [%s(%d)]: copyCourse() -> set modeState to 1'):format(nameNum(vehicle), curFile, debug.getinfo(1).currentline)); -- DEBUG140301
end;
vehicle.cp.recordingTimer = 1;
courseplay.signs:updateWaypointSigns(vehicle, 'current');
--reset variables
vehicle.cp.selectedDriverNumber = 0;
vehicle.cp.hasFoundCopyDriver = false;
vehicle.cp.copyCourseFromDriver = nil;
courseplay:validateCanSwitchMode(vehicle);
-- SETUP 2D COURSE DRAW DATA
vehicle.cp.course2dUpdateDrawData = true;
end;
end;
function courseplay.settings.add_folder_settings(folder)
folder.showChildren = false
folder.skipMe = false
end
function courseplay.settings.add_folder(input1, input2)
-- function might be called like add_folder(vehicle, id) or like add_folder(id)
local vehicle, id
if input2 ~= nil then
vehicle = input1
id = input2
else
vehicle = false
id = input1
end
if vehicle == false then
-- no vehicle given -> add folder to all vehicles
for k,v in pairs(g_currentMission.steerables) do
if v.hasCourseplaySpec then -- alternative way to check if SpecializationUtil.hasSpecialization(courseplay, v.specializations)
v.cp.folder_settings[id] = {}
courseplay.settings.add_folder_settings(v.cp.folder_settings[id])
end
end
else
-- vehicle given -> add folder to that vehicle
vehicle.cp.folder_settings[id] = {}
courseplay.settings.add_folder_settings(vehicle.cp.folder_settings[id])
end
end
function courseplay.settings.update_folders(vehicle)
local old_settings
if vehicle == nil then
-- no vehicle given -> update all folders in all vehicles
for k,v in pairs(g_currentMission.steerables) do
if v.hasCourseplaySpec then -- alternative way to check if SpecializationUtil.hasSpecialization(courseplay, v.specializations)
old_settings = v.cp.folder_settings
v.cp.folder_settings = {}
for _,f in pairs(g_currentMission.cp_folders) do
if old_settings[f.id] ~= nil then
v.cp.folder_settings[f.id] = old_settings[f.id]
else
v.cp.folder_settings[f.id] = {}
courseplay.settings.add_folder_settings(v.cp.folder_settings[f.id])
end
end
old_settings = nil
end
end
else
-- vehicle given -> update all folders in that vehicle
old_settings = vehicle.cp.folder_settings
vehicle.cp.folder_settings = {}
for _,f in pairs(g_currentMission.cp_folders) do
if old_settings[f.id] ~= nil then
vehicle.cp.folder_settings[f.id] = old_settings[f.id]
else
vehicle.cp.folder_settings[f.id] = {}
courseplay.settings.add_folder_settings(vehicle.cp.folder_settings[f.id])
end
end
end
old_settings = nil
end
function courseplay.settings.setReloadCourseItems(vehicle)
if vehicle ~= nil then
vehicle.cp.reloadCourseItems = true
courseplay.hud:setReloadPageOrder(vehicle, 2, true);
else
for k,v in pairs(g_currentMission.steerables) do
if v.hasCourseplaySpec then -- alternative way to check if SpecializationUtil.hasSpecialization(courseplay, v.specializations)
v.cp.reloadCourseItems = true
--print(string.format("courseplay.hud:setReloadPageOrder(%s, 2, true) TypeName: %s ;",tostring(v.name), v.typeName))
courseplay.hud:setReloadPageOrder(v, 2, true);
end
end
end
end
function courseplay.settings.toggleFilter(vehicle, enable)
if enable and not vehicle.cp.hud.filterEnabled then
vehicle.cp.sorted = vehicle.cp.filtered;
vehicle.cp.hud.filterEnabled = true;
elseif not enable and vehicle.cp.hud.filterEnabled then
vehicle.cp.filtered = vehicle.cp.sorted;
vehicle.cp.sorted = g_currentMission.cp_sorted;
vehicle.cp.hud.filterEnabled = false;
end;
end;
function courseplay.hud.setCourses(self, start_index)
start_index = start_index or 1
if start_index < 1 then
start_index = 1
elseif start_index > #self.cp.sorted.item then
start_index = #self.cp.sorted.item
end
-- delete content of hud.courses
self.cp.hud.courses = {}
local index = start_index
local hudLines = courseplay.hud.numLines
local i = 1
if index == 1 and self.cp.hud.showZeroLevelFolder then
table.insert(self.cp.hud.courses, { id=0, uid=0, name='Level 0', displayname='Level 0', parent=0, type='folder', level=0})
i = 2 -- = i+1
end
-- is start_index even showed?
index = courseplay.courses:getMeOrBestFit(self, index)
if index ~= 0 then
-- insert first entry
table.insert(self.cp.hud.courses, self.cp.sorted.item[index])
i = i+1
-- now search for the next entries
while i <= hudLines do
index = courseplay.courses:getNextCourse(self,index)
if index == 0 then
-- no next item found: fill table with previous items and abort the loop
if start_index > 1 then
-- shift up
courseplay:shiftHudCourses(self, -(hudLines - i + 1))
end
i = hudLines+1 -- abort the loop
else
table.insert(self.cp.hud.courses, self.cp.sorted.item[index])
i = i + 1
end
end --while
end -- i<3
courseplay.hud:setReloadPageOrder(self, 2, true);
end
function courseplay.hud.reloadCourses(vehicle)
local index = 1
local i = 1
if vehicle ~= nil then
while i <= #vehicle.cp.hud.courses and vehicle.cp.sorted.info[ vehicle.cp.hud.courses[i].uid ] == nil do
i = i + 1
end
if i <= #vehicle.cp.hud.courses then
index = vehicle.cp.sorted.info[ vehicle.cp.hud.courses[i].uid ].sorted_index
end
courseplay.hud.setCourses(vehicle, index)
else
for k,v in pairs(g_currentMission.steerables) do
if v.hasCourseplaySpec then -- alternative way to check if SpecializationUtil.hasSpecialization(courseplay, v.specializations)
i = 1
-- course/folder in the hud might have been deleted -> info no longer available
while i <= #v.cp.hud.courses and v.cp.sorted.info[ v.cp.hud.courses[i].uid ] == nil do
i = i + 1
end
if i > #v.cp.hud.courses then
index = 1
else
index = v.cp.sorted.info[ v.cp.hud.courses[i].uid ].sorted_index
end
courseplay.hud.setCourses(v,index)
end
end
end
end
function courseplay:shiftHudCourses(vehicle, change_by)
local hudLines = courseplay.hud.numLines
local index = hudLines
while change_by > 0 do
-- get the index of the last showed item
index = vehicle.cp.sorted.info[vehicle.cp.hud.courses[#(vehicle.cp.hud.courses)].uid].sorted_index
-- search for the next item
index = courseplay.courses:getNextCourse(vehicle,index)
if index == 0 then
-- there is no next item: abort
change_by = 0
else
if #(vehicle.cp.hud.courses) == hudLines then
-- remove first entry...
table.remove(vehicle.cp.hud.courses, 1)
end
-- ... and add one at the end
table.insert(vehicle.cp.hud.courses, vehicle.cp.sorted.item[index])
change_by = change_by - 1
end
end
while change_by < 0 do
-- get the index of the first showed item
index = vehicle.cp.sorted.info[vehicle.cp.hud.courses[1].uid].sorted_index
-- search reverse for the next item
index = courseplay.courses:getNextCourse(vehicle, index, true)
if index == 0 then
-- there is no next item: abort
change_by = 0
-- show LevelZeroFolder?
if vehicle.cp.hud.showZeroLevelFolder then
if #(vehicle.cp.hud.courses) >= hudLines then
-- remove last entry...
table.remove(vehicle.cp.hud.courses)
end
table.insert(vehicle.cp.hud.courses, 1, { id=0, uid=0, name='Level 0', displayname='Level 0', parent=0, type='folder', level=0})
end
else
if #(vehicle.cp.hud.courses) >= hudLines then
-- remove last entry...
table.remove(vehicle.cp.hud.courses)
end
-- ... and add one at the beginning:
table.insert(vehicle.cp.hud.courses, 1, vehicle.cp.sorted.item[index])
change_by = change_by + 1
end
end
courseplay.hud:setReloadPageOrder(vehicle, 2, true);
end
--Update all vehicles' course list arrow displays
function courseplay.settings.validateCourseListArrows(vehicle)
local n_courses = #(vehicle.cp.sorted.item)
local n_hudcourses, prev, next
if vehicle then
-- update vehicle only
prev = true
next = true
n_hudcourses = #(vehicle.cp.hud.courses)
if not (n_hudcourses > 0) then
prev = false
next = false
else
-- update prev
if vehicle.cp.hud.showZeroLevelFolder then
if vehicle.cp.hud.courses[1].uid == 0 then
prev = false
end
elseif vehicle.cp.sorted.info[ vehicle.cp.hud.courses[1].uid ].sorted_index == 1 then
prev = false
end
-- update next
if n_hudcourses < courseplay.hud.numLines then
next = false
elseif vehicle.cp.hud.showZeroLevelFolder and vehicle.cp.hud.courses[n_hudcourses].uid == 0 then
next = false
elseif 0 == courseplay.courses:getNextCourse(vehicle, vehicle.cp.sorted.info[ vehicle.cp.hud.courses[n_hudcourses].uid ].sorted_index) then
next = false
end
end
vehicle.cp.hud.courseListPrev = prev
vehicle.cp.hud.courseListNext = next
else
-- update all vehicles
for k,v in pairs(g_currentMission.steerables) do
if v.hasCourseplaySpec then -- alternative way to check if SpecializationUtil.hasSpecialization(courseplay, v.specializations)
prev = true
next = true
n_hudcourses = #(v.cp.hud.courses)
if not (n_hudcourses > 0) then
prev = false
next = false
else
-- update prev
if v.cp.hud.showZeroLevelFolder then
if v.cp.hud.courses[1].uid == 0 then
prev = false
end
elseif v.cp.sorted.info[v.cp.hud.courses[1].uid].sorted_index == 1 then
prev = false
end
-- update next
if n_hudcourses < coursplay.hud.numLines then
next = false
elseif 0 == courseplay.courses:getNextCourse(v, v.cp.sorted.info[v.cp.hud.courses[n_hudcourses].uid].sorted_index) then
next = false
end
end
v.cp.hud.courseListPrev = prev
v.cp.hud.courseListNext = next
end -- if hasSpecialization
end -- in pairs(steerables)
end -- if vehicle
end;
function courseplay:expandFolder(vehicle, index)
-- expand/reduce a folder in the hud
if vehicle.cp.hud.courses[index].type == 'folder' then
local f = vehicle.cp.folder_settings[ vehicle.cp.hud.courses[index].id ]
f.showChildren = not f.showChildren
if f.showChildren then
-- from not showing to showing -> put it on top to see as much of the content as possible
courseplay.hud.setCourses(vehicle, vehicle.cp.sorted.info[vehicle.cp.hud.courses[index].uid].sorted_index)
else
-- from showing to not showing -> stay where it was
courseplay.hud.reloadCourses(vehicle)
end
end
end
function courseplay:toggleDebugChannel(self, channel, force)
if courseplay.debugChannels[channel] ~= nil then
courseplay.debugChannels[channel] = Utils.getNoNil(force, not courseplay.debugChannels[channel]);
courseplay.buttons:setActiveEnabled(self, "debug");
end;
end;
--Course generation
function courseplay:switchStartingCorner(vehicle)
vehicle.cp.startingCorner = vehicle.cp.startingCorner + 1;
if vehicle.cp.startingCorner > 4 then
vehicle.cp.startingCorner = 1;
end;
vehicle.cp.hasStartingCorner = true;
vehicle.cp.hasStartingDirection = false;
vehicle.cp.startingDirection = 0;
courseplay:validateCourseGenerationData(vehicle);
end;
function courseplay:changeStartingDirection(vehicle)
-- corners: 1 = SW, 2 = NW, 3 = NE, 4 = SE
-- directions: 1 = North, 2 = East, 3 = South, 4 = West
local clockwise = true
local validDirections = {};
if vehicle.cp.hasStartingCorner then
if vehicle.cp.startingCorner == 1 then --SW
validDirections[1] = 1; --N
validDirections[2] = 2; --E
elseif vehicle.cp.startingCorner == 2 then --NW
validDirections[1] = 2; --E
validDirections[2] = 3; --S
elseif vehicle.cp.startingCorner == 3 then --NE
validDirections[1] = 3; --S
validDirections[2] = 4; --W
elseif vehicle.cp.startingCorner == 4 then --SE
validDirections[1] = 4; --W
validDirections[2] = 1; --N
end;
--would be easier with i=i+1, but more stored variables would be needed
if vehicle.cp.startingDirection == 0 then
vehicle.cp.startingDirection = validDirections[1];
elseif vehicle.cp.startingDirection == validDirections[1] then
vehicle.cp.startingDirection = validDirections[2];
clockwise = false
elseif vehicle.cp.startingDirection == validDirections[2] then
vehicle.cp.startingDirection = validDirections[1];
end;
vehicle.cp.hasStartingDirection = true;
end;
if vehicle.cp.headland.userDirClockwise ~= clockwise then
courseplay:toggleHeadlandDirection(vehicle)
end
courseplay:validateCourseGenerationData(vehicle);
end;
function courseplay:toggleReturnToFirstPoint(vehicle)
vehicle.cp.returnToFirstPoint = not vehicle.cp.returnToFirstPoint;
end;
function courseplay:changeHeadlandNumLanes(vehicle, changeBy)
vehicle.cp.headland.numLanes = Utils.clamp(vehicle.cp.headland.numLanes + changeBy, 0, vehicle.cp.headland.maxNumLanes);
courseplay:validateCourseGenerationData(vehicle);
end;
function courseplay:toggleHeadlandDirection(vehicle)
vehicle.cp.headland.userDirClockwise = not vehicle.cp.headland.userDirClockwise;
vehicle.cp.headland.directionButton:setSpriteSectionUVs(vehicle.cp.headland.userDirClockwise and 'headlandDirCW' or 'headlandDirCCW');
end;
function courseplay:toggleHeadlandOrder(vehicle)
vehicle.cp.headland.orderBefore = not vehicle.cp.headland.orderBefore;
vehicle.cp.headland.orderButton:setSpriteSectionUVs(vehicle.cp.headland.orderBefore and 'headlandOrdBef' or 'headlandOrdAft');
-- courseplay:debug(string.format('toggleHeadlandOrder(): orderBefore=%s -> set to %q, setOverlay(orderButton, %d)', tostring(not vehicle.cp.headland.orderBefore), tostring(vehicle.cp.headland.orderBefore), vehicle.cp.headland.orderBefore and 1 or 2), 7);
end;
function courseplay:validateCourseGenerationData(vehicle)
local numWaypoints = 0;
if vehicle.cp.fieldEdge.selectedField.fieldNum > 0 then
numWaypoints = #(courseplay.fields.fieldData[vehicle.cp.fieldEdge.selectedField.fieldNum].points);
elseif vehicle.Waypoints ~= nil then
numWaypoints = #(vehicle.Waypoints);
end;
local hasEnoughWaypoints = numWaypoints >= 4
if vehicle.cp.headland.numLanes ~= 0 then
hasEnoughWaypoints = numWaypoints >= 20;
end;