This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 525
/
settings.lua
4237 lines (3718 loc) · 140 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 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;
--print(string.format("courseplay:openCloseHud set to %s",tostring(vehicle.cp.hud.show)))
if open then
--courseplay.buttons:setActiveEnabled(vehicle, 'all');
else
courseplay.buttons:setHoveredButton(vehicle, nil);
end;
end;
function courseplay:setAIDriver(vehicle, mode)
local errorHandler = function(err)
printCallstack()
courseplay.infoVehicle(vehicle, "Exception, can't init mode %d: %s", mode, tostring(err))
return err
end
if vehicle.cp.driver then
vehicle.cp.driver:delete()
end
local status, result
if mode == courseplay.MODE_TRANSPORT then
status, result = xpcall(AIDriver, errorHandler, vehicle)
elseif mode == courseplay.MODE_GRAIN_TRANSPORT then
status, result = xpcall(GrainTransportAIDriver, errorHandler, vehicle)
elseif mode == courseplay.MODE_COMBI then
status, result = xpcall(CombineUnloadAIDriver, errorHandler, vehicle)
elseif mode == courseplay.MODE_OVERLOADER then
status, result = xpcall(OverloaderAIDriver, errorHandler, vehicle)
elseif mode == courseplay.MODE_SHOVEL_FILL_AND_EMPTY then
status, result = xpcall(ShovelAIDriver.create, errorHandler, vehicle)
elseif mode == courseplay.MODE_SEED_FERTILIZE then
status, result = xpcall(FillableFieldworkAIDriver, errorHandler, vehicle)
elseif mode == courseplay.MODE_FIELDWORK then
status, result = xpcall(UnloadableFieldworkAIDriver.create, errorHandler, vehicle)
elseif mode == courseplay.MODE_BALE_COLLECTOR then
status, result = xpcall(BaleCollectorAIDriver, errorHandler, vehicle)
elseif mode == courseplay.MODE_BUNKERSILO_COMPACTER then
status, result = xpcall(BunkerSiloAIDriver.create, errorHandler, vehicle)
elseif mode == courseplay.MODE_FIELD_SUPPLY then
status, result = xpcall(FieldSupplyAIDriver, errorHandler, vehicle)
end
if status then
vehicle.cp.driver = result
else
-- give it another try as a fallback level
courseplay.infoVehicle(vehicle, 'Retrying initialization in mode 5')
self.cp.settings.driverMode:resetToDefault(true)
end
end
function courseplay:setDriveNow(vehicle)
vehicle.cp.driver:setDriveNow()
end
---This function gets only called locally like an actionEvent,
---For reference: giants AIVehicle.actionEventToggleAIState()
function courseplay:startStop(vehicle)
if vehicle.cp.canDrive then
---These to function also handle the sync for multiplayer
if not vehicle:getIsCourseplayDriving() then
courseplay.onStartCpAIDriver(vehicle,nil,false, g_currentMission.player.farmId)
else
courseplay.onStopCpAIDriver(vehicle,AIVehicle.STOP_REASON_USER)
end
else
---Not sure why this is hear might be related to an key actionEvent,
---Should currently be broken in multiplayer now...
courseplay:start_record(vehicle);
end
courseplay.hud:setReloadPageOrder(vehicle, vehicle.cp.hud.currentPage, true);
end;
function courseplay:startStopUnloader(combine)
local tractor = g_combineUnloadManager:getUnloaderByNumber(1, combine)
if tractor then
tractor.cp.settings.forcedToStop:toggle()
end
end;
function courseplay:sendUnloaderHome(combine)
local unloader = g_combineUnloadManager:getUnloaderByNumber(1, combine)
if unloader then
unloader.cp.driver:setDriveUnloadNow(true)
end
end
function courseplay:setHudPage(vehicle, pageNum)
vehicle.cp.hud.hudPageButtons[vehicle.cp.hud.currentPage]:setActive(false)
vehicle.cp.hud.currentPage = pageNum;
vehicle.cp.hud.hudPageButtons[pageNum]:setActive(true)
courseplay.hud:setReloadPageOrder(vehicle, vehicle.cp.hud.currentPage, true);
end;
--- These three tool offset function should be handled by the setting.
function courseplay:changeToolOffsetX(vehicle, changeBy)
vehicle.cp.settings.toolOffsetX:changeBy(changeBy * 0.1,true)
vehicle.cp.totalOffsetX = vehicle.cp.settings.toolOffsetX:get();
-- show new setting for a few seconds on the screen
courseplay:setCustomTimer(vehicle, 'showWorkWidth', 2);
end
function courseplay:setAutoToolOffsetX(vehicle)
vehicle.cp.settings.toolOffsetX:setToConfiguredValue(vehicle)
end
function courseplay:changeToolOffsetZ(vehicle, changeBy, force, noDraw)
vehicle.cp.settings.toolOffsetZ:changeBy(changeBy * 0.1,true)
-- show new setting for a few seconds on the screen
courseplay:setCustomTimer(vehicle, 'showWorkWidth', 2);
end;
--legancy Code in toolManager still using it!
function courseplay:changeReverseSpeed(vehicle, changeBy, force, forceReloadPage)
local speed = force or (vehicle.cp.speeds.reverse + changeBy);
if not force then
speed = MathUtil.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:selectAssignedCombine(vehicle, changeBy)
vehicle.cp.settings.selectedCombineToUnload:refresh()
if changeBy > 0 then
vehicle.cp.settings.selectedCombineToUnload:setNext()
else
vehicle.cp.settings.selectedCombineToUnload:setPrevious()
end
courseplay:removeActiveCombineFromTractor(vehicle)
courseplay.hud:setReloadPageOrder(vehicle, vehicle.cp.hud.currentPage, true);
end;
function courseplay:removeActiveCombineFromTractor(vehicle)
if vehicle.cp.driver and vehicle.cp.driver.onUserUnassignedActiveCombine then
vehicle.cp.driver:onUserUnassignedActiveCombine()
end
courseplay.hud:setReloadPageOrder(vehicle, 4, true);
end;
function courseplay:switchDriverCopy(vehicle, changeBy)
local drivers = courseplay:findDrivers(vehicle);
if drivers ~= nil then
vehicle.cp.selectedDriverNumber = MathUtil.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.enterables) 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.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.enterables) 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.enterables) 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
-- make sure the course list is reloaded when tabbed to all vehicles.
for k,v in pairs(g_currentMission.enterables) do
if v.hasCourseplaySpec then -- alternative way to check if SpecializationUtil.hasSpecialization(courseplay, v.specializations)
v.cp.reloadCourseItems = true
courseplay.debugVehicle(courseplay.DBG_COURSES, v,"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.enterables) 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
return prev, next;
--vehicle.cp.hud.courseListPrev = prev
--vehicle.cp.hud.courseListNext = next
--[[else
-- update all vehicles
for k,v in pairs(g_currentMission.enterables) 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(enterables)
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:validateCanSwitchMode(vehicle)
vehicle:setCpVar('canSwitchMode', not vehicle:getIsCourseplayDriving() and not vehicle.cp.isRecording and
not vehicle.cp.recordingIsPaused ,courseplay.isClient);
courseplay:debug(('%s: validateCanSwitchMode(): isDriving=%s, isRecording=%s, recordingIsPaused=%s ==> canSwitchMode=%s'):format(
nameNum(vehicle), tostring(vehicle:getIsCourseplayDriving()), tostring(vehicle.cp.isRecording),
tostring(vehicle.cp.recordingIsPaused), tostring(vehicle.cp.canSwitchMode)), courseplay.DBG_UNCATEGORIZED);
end;
function courseplay:reloadCoursesFromXML(vehicle)
courseplay:debug("reloadCoursesFromXML()", courseplay.DBG_COURSES);
if g_server ~= nil then
courseplay.courses:loadCoursesAndFoldersFromXml();
--courseplay:debug(tableShow(g_currentMission.cp_courses, "g_cM cp_courses", 8), courseplay.DBG_COURSES);
courseplay:debug("g_currentMission.cp_courses = courseplay.courses:loadCoursesAndFoldersFromXml()", courseplay.DBG_COURSES);
if not vehicle:getIsCourseplayDriving() then
local loadedCoursesBackup = vehicle.cp.loadedCourses;
courseplay:clearCurrentLoadedCourse(vehicle);
vehicle.cp.loadedCourses = loadedCoursesBackup;
courseplay:reloadCourses(vehicle, true);
end;
courseplay.settings.update_folders()
courseplay.settings.setReloadCourseItems()
--courseplay.hud.reloadCourses()
end
end;
function courseplay:setMouseCursor(self, show)
self.cp.mouseCursorActive = show;
g_inputBinding:setShowMouseCursor(show);
--Cameras: deactivate/reactivate zoom function in order to allow CP mouse wheel
for camIndex,_ in pairs(self.cp.camerasBackup) do
self.spec_enterable.cameras[camIndex].allowTranslation = not show;
--print(string.format("%s: right mouse key (mouse cursor=%s): camera %d allowTranslation=%s", nameNum(self), tostring(self.cp.mouseCursorActive), camIndex, tostring(self.cameras[camIndex].allowTranslation)));
end;
if not show then
for i,button in pairs(self.cp.buttons.global) do
button:setHovered(false);
end;
for i,button in pairs(self.cp.buttons[self.cp.hud.currentPage]) do
button:setHovered(false);
end;
if self.cp.hud.currentPage == 2 then
for i,button in pairs(self.cp.buttons[-2]) do
button:setHovered(false);
end;
end;
for line=1,courseplay.hud.numLines do
self.cp.hud.content.pages[self.cp.hud.currentPage][line][1].isHovered = false;
end;
courseplay.buttons:setHoveredButton(self, nil);
self.cp.hud.mouseWheel.render = false;
end;
end;
--- Changes the line of debug channels visible in the hud.
--- For example changes dbg channels 1-12 to 13-24 in the hud.
function courseplay:changeDebugChannelSection(vehicle, changeBy)
local debugChannelData = courseplay.hud.debugChannelData
--- Changes the visible debug channels in the hud.
debugChannelData.lineSelected = MathUtil.clamp(debugChannelData.lineSelected + changeBy, 1, debugChannelData.lines)
debugChannelData.lineLastChannel = debugChannelData.buttonsPerLine * debugChannelData.lineSelected
debugChannelData.lineFirstChannel = debugChannelData.lineLastChannel - debugChannelData.buttonsPerLine + 1
--- Sets the buttons to the correct debug channel after a line change.
for channel = debugChannelData.lineFirstChannel, debugChannelData.lineLastChannel do
local col = ((channel-1) % debugChannelData.buttonsPerLine) + 1
local button = vehicle.cp.hud.debugChannelButtons[col]
button:setParameter(channel)
button:setToolTip(debugChannelData.toolTips[channel])
end
end
function courseplay:setEngineState(vehicle, on)
if vehicle == nil or on == nil or vehicle.spec_motorized.isMotorStarted == on then
return;
end;
-- default
if vehicle.startMotor and vehicle.stopMotor then
if on then
vehicle:startMotor();
else
vehicle.lastAcceleration = 0;
vehicle:stopMotor();
end;
end;
end;
function courseplay:toggleFindFirstWaypoint(vehicle)
vehicle:setCpVar('distanceCheck',not vehicle.cp.distanceCheck,courseplay.isClient);
if not courseplay.isClient and not vehicle.cp.distanceCheck then
courseplay:setInfoText(vehicle, nil);
end;
--courseplay.buttons:setActiveEnabled(vehicle, 'findFirstWaypoint');
end;
function courseplay:setSlippingStage(vehicle, stage)
if vehicle.cp.slippingStage ~= stage then
courseplay:debug(('%s: setSlippingStage(..., %d)'):format(nameNum(vehicle), stage), courseplay.DBG_AI_DRIVER);
vehicle.cp.slippingStage = stage;
end;
end;
----------------------------------------------------------------------------------------------------
function courseplay:setCpVar(varName, value, noEventSend)
local split = StringUtil.splitString(".", varName);
if #split ==1 then
if self.cp[varName] ~= value then
local oldValue = self.cp[varName]; --TODO check whether needed or not
self.cp[varName] = value;
if CpManager.isMP and not noEventSend then
--print(courseplay.utils:getFnCallPath(2))
courseplay:debug(string.format("setCpVar: %s: %s -> send Event",varName,tostring(value)), courseplay.DBG_MULTIPLAYER);
CourseplayEvent.sendEvent(self, "self.cp."..varName, value)
end
if varName == "isDriving" then
courseplay:debug("reload page 1", courseplay.DBG_MULTIPLAYER);
courseplay.hud:setReloadPageOrder(self, 1, true);
elseif varName:sub(1, 3) == 'HUD' then
-- TODO: using the variable name to trigger a HUD refresh is not a good idea.
--print('broken settings 1860')
if StringUtil.startsWith(varName, 'HUD0') then
courseplay:debug("reload page 0", courseplay.DBG_MULTIPLAYER);
courseplay.hud:setReloadPageOrder(self, 0, true);
elseif StringUtil.startsWith(varName, 'HUD1') then
courseplay:debug("reload page 1", courseplay.DBG_MULTIPLAYER);
courseplay.hud:setReloadPageOrder(self, 1, true);
elseif StringUtil.startsWith(varName, 'HUD4') then
courseplay:debug("reload page 4", courseplay.DBG_MULTIPLAYER);
courseplay.hud:setReloadPageOrder(self, 4, true);
end;
elseif varName == 'waypointIndex' and self.cp.hud.currentPage == courseplay.hud.PAGE_CP_CONTROL and (self.cp.isRecording or self.cp.recordingIsPaused) and value and value == 4 then -- record pause action becomes available
--courseplay.buttons:setActiveEnabled(self, 'recording');
end;
end;
elseif #split == 2 then
if self.cp[split[1]][split[2]] ~= value then
self.cp[split[1]][split[2]] = value
end
-- TODO: this is unclear, shouldn't this be only called when the value changed?
if CpManager.isMP and not noEventSend then
--print(courseplay.utils:getFnCallPath(2))
courseplay:debug(string.format("setCpVar: %s: %s -> send Event",varName,tostring(value)), courseplay.DBG_MULTIPLAYER);
CourseplayEvent.sendEvent(self, "self.cp."..varName, value)
end
end
end;
---@class Setting
Setting = CpObject()
--- Interface for settings
--- @param name string name of this settings, will be used as an identifier in containers and XML
--- @param label string text ID in translations used as a label for this setting on the GUI
--- @param toolTip string text ID in translations used as a tooltip for this setting on the GUI
--- @param vehicle table vehicle, needed for vehicle specific settings for multiplayer syncs
function Setting:init(name, label, toolTip, vehicle, value)
self.name = name
self.label = label
self.toolTip = toolTip
self.value = value
-- Required to send sync events for settings changes
self.vehicle = vehicle
self.syncValue = false
-- override
self.xmlKey = name
self.xmlAttribute = '#value'
self.events={}
self.debugMpChannel = courseplay.DBG_MULTIPLAYER
end
-- Get the current value
function Setting:get()
return self.value
end
-- Is the current value same as the param?
function Setting:is(value)
return self.value == value
end
function Setting:equals(value)
return self.value == value
end
-- Get the current text to be shown on the UI
function Setting:getText()
return tostring(self.value)
end
function Setting:getLabel()
return courseplay:loc(self.label)
end
function Setting:getName()
return self.name
end
function Setting:getParentName()
return self.parentName
end
function Setting:getToolTip()
return courseplay:loc(self.toolTip)
end
-- function only called from network to set synced setting
function Setting:setFromNetwork(value)
self:set(value,true)
end
function Setting:getDebugString()
return string.format('%s: %s', self.name, tostring(self:get()))
end
--- Set to a specific value
function Setting:set(value)
self.value = value
self:onChange()
end
function Setting:onChange()
-- setting specific implementation in the derived classes
end
function Setting:getKey(parentKey)
return parentKey .. '.' .. self.xmlKey .. self.xmlAttribute
end
function Setting:loadFromXml(xml, parentKey)
-- override
end
function Setting:saveToXml(xml, parentKey)
-- override
end
-- For settings where the valid values depend on other conditions, re-evaluate the validity of the
-- current setting (when for example changed the mode of the vehicle, is the current setting still valid for the new mode)
function Setting:validateCurrentValue()
-- override
end
function Setting:setParent(name)
self.parentName = name
end
-- remember the associated GUI element
function Setting:setGuiElement(element)
self.guiElement = element
end
function Setting:getGuiElement()
return self.guiElement
end
function Setting:hasGuiElement()
return self.guiElement~=nil
end
--- Should this setting be disabled on the GUI?
function Setting:isDisabled()
return false
end
--- Action event for Keys?
function Setting:actionEvent(actionName, inputValue, callbackState, isAnalog)
---override
end
--- Registers an event that synchronizes a value.
---@param eventFunc function Callback function run on the receiving end of the event.
---@param getValueFunc function Callback function to get the value, which needs synchronizing.
---@param readFunc function Reads the value data stream on the receiving end with this function.
---@param writeFunc function Writes the value data stream from the sender with this function.
function Setting:registerEvent(eventFunc,getValueFunc,readFunc,writeFunc)
local ix = #self.events+1
local event = {
eventFunc = eventFunc,
getValueFunc = getValueFunc,
readFunc = readFunc,
writeFunc = writeFunc,
ix = ix
}
table.insert(self.events,event)
return ix
end
--- Registers an event that synchronizes an Int value.
---@param eventFunc function
---@param getValueFunc function
function Setting:registerIntEvent(eventFunc,getValueFunc)
return self:registerEvent(eventFunc,getValueFunc,streamReadInt32,streamWriteInt32)
end
--- Registers an event that synchronizes an Float value.
---@param eventFunc function
---@param getValueFunc function
function Setting:registerFloatEvent(eventFunc,getValueFunc)
return self:registerEvent(eventFunc,getValueFunc,streamReadFloat32,streamWriteFloat32)
end
--- Registers an event that synchronizes an Boolean value.
---@param eventFunc function
---@param getValueFunc function
function Setting:registerBoolEvent(eventFunc,getValueFunc)
return self:registerEvent(eventFunc,getValueFunc,streamReadBool,streamWriteBool)
end
--- Registers an event that requests a function call on the receiving end.
---@param eventFunc function
function Setting:registerFunctionEvent(eventFunc)
return self:registerEvent(eventFunc)
end
--- Gets an event by it's id.
function Setting:getEvent(ix)
return self.events[ix]
end
--- Raises an event by it's id to synchronize a value,
--- which is defined by a callback function.
function Setting:raiseEvent(eventIx,value)
local event = self:getEvent(eventIx)
value = event.getValueFunc and event.getValueFunc(self) or value
if self.vehicle ~= nil then
VehicleSettingEvent.sendEvent(self.vehicle,self,event,value)
else
GlobalSettingEvent.sendEvent(self,event,value)
end
end
--- Setting debug.
---@param channel number debug channel
function Setting:debug(channel,...)
courseplay.debugFormat(channel,...)
end
function Setting:debugMp(...)
self:debug(self.debugMpChannel,...)
end
function Setting:isMpDebugActive()
return courseplay.debugChannels[courseplay.DBG_MULTIPLAYER]
end
--- Debug for synchronizing on joining a game.
---@param value any the value that gets synchronized.
---@param valueName string the value name
function Setting:debugWriteStream(value,valueName)
if self:isMpDebugActive() then
self:debugMp("Write, container: %s, setting: %s, %s: %s",self.parentName,self.name,valueName or "value",tostring(value))
end
end
--- Debug for synchronizing on joining a game.
---@param value any the value that gets synchronized.
---@param valueName string the value name
function Setting:debugReadStream(value,valueName)
if self:isMpDebugActive() then
self:debugMp("Read, container: %s, setting: %s, %s: %s",self.parentName,self.name,valueName or "value",tostring(value))
end
end
--- Is synchronizing of this setting allowed.
function Setting:isSyncAllowed()
return self.syncValue
end
---@class FloatSetting :Setting
FloatSetting = CpObject(Setting)
--- @param name string name of this settings, will be used as an identifier in containers and XML
--- @param label string text ID in translations used as a label for this setting on the GUI
--- @param toolTip string text ID in translations used as a tooltip for this setting on the GUI
--- @param vehicle table vehicle, needed for vehicle specific settings for multiplayer syncs
--- @param value number default value
function FloatSetting:init(name, label, toolTip, vehicle, value)
Setting.init(self, name, label, toolTip, vehicle, value)
self.DEFAULT_EVENT = self:registerFloatEvent(self.setFromNetwork,self.get)
end
function FloatSetting:loadFromXml(xml, parentKey)
local value = getXMLFloat(xml, self:getKey(parentKey))
if value then
self:set(value,true)
end
end
function FloatSetting:saveToXml(xml, parentKey)
setXMLFloat(xml, self:getKey(parentKey), self:get())
end
function FloatSetting:onWriteStream(stream)
local value = self:get()
self:debugWriteStream(value)
streamWriteFloat32(stream, value)
end
function FloatSetting:onReadStream(stream)
local value = streamReadFloat32(stream)
self:debugReadStream(value)
if value then
self:setFromNetwork(value)
end
end
function FloatSetting:changeByX(x)
self:set(self:get()+x)
end
function FloatSetting:set(value,noEventSend)
self.value = value
if noEventSend == nil or noEventSend == false then
if self:isSyncAllowed() then
self:sendEvent(value)
end
end
self:onChange()
end
function FloatSetting:sendEvent(value)
self:raiseEvent(self.DEFAULT_EVENT)
end
---@class IntSetting : Setting
IntSetting = CpObject(Setting)
--- @param name string name of this settings, will be used as an identifier in containers and XML
--- @param label string text ID in translations used as a label for this setting on the GUI
--- @param toolTip string text ID in translations used as a tooltip for this setting on the GUI
--- @param vehicle table vehicle, needed for vehicle specific settings for multiplayer syncs
--- @param MIN number min allowed value
--- @param MAX number max allowed value
--- @param value number default value
function IntSetting:init(name, label, toolTip, vehicle,MIN,MAX,value)
Setting.init(self, name, label, toolTip, vehicle,value)
self.MAX = MAX
self.MIN = MIN
self.DEFAULT_EVENT = self:registerIntEvent(self.setFromNetwork,self.get)
end
function IntSetting:loadFromXml(xml, parentKey)
local value = getXMLInt(xml, self:getKey(parentKey))
if value then
self:set(value,true)
end
end
function IntSetting:saveToXml(xml, parentKey)
setXMLInt(xml, self:getKey(parentKey), self:get())
end
function IntSetting:onWriteStream(stream)
local value = self:get()
self:debugWriteStream(value)
streamWriteInt32(stream, value)
end
function IntSetting:onReadStream(stream)
local value = streamReadInt32(stream)
self:debugReadStream(value)
if value then
self:setFromNetwork(value)
end
end
function IntSetting:changeByX(x)
self:set(self:get()+x)
end
function IntSetting:set(value,noEventSend)
local minOk = self.MIN and value>=self.MIN or self.MIN == nil
local maxOk = self.MAX and value<=self.MAX or self.MAX == nil
if minOk and maxOk and value then
self.value = value
if noEventSend == nil or noEventSend == false then
if self:isSyncAllowed() then
self:sendEvent(value)
end
end
self:onChange()
end
end
function IntSetting:sendEvent(value)
self:raiseEvent(self.DEFAULT_EVENT)
end
---@class SettingList : Setting
SettingList = CpObject(Setting)
--- A setting that can have a predefined set of values
--- @param name string name of this settings, will be used as an identifier in containers and XML
--- @param label string text ID in translations used as a label for this setting on the GUI
--- @param toolTip string text ID in translations used as a tooltip for this setting on the GUI
--- @param vehicle table vehicle, needed for vehicle specific settings for multiplayer syncs
--- @param values table with the valid values
--- @param texts string[] name in the translation XML files describing the corresponding value
function SettingList:init(name, label, toolTip, vehicle, values, texts)