This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CpManager.lua
1288 lines (1104 loc) · 54.2 KB
/
CpManager.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 = 'CpManager.lua';
CpManager = {};
local CpManager_mt = Class(CpManager);
addModEventListener(CpManager);
function CpManager:loadMap(name)
self.isCourseplayManager = true;
self.firstRun = true;
-- MULTIPLAYER
CpManager.isMP = g_currentMission.missionDynamicInfo.isMultiplayer;
courseplay.isClient = not g_server; -- TODO JT: not needed, as every vehicle always has self.isServer and self.isClient
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- XML PATHS
if g_server ~= nil then
-- Settings and custom fields path and files
self.savegameFolderPath = ('%ssavegame%d'):format(getUserProfileAppPath(), g_careerScreen.selectedIndex); -- This should work for both SP, MP and Dedicated Servers
self.cpSettingsXmlFilePath = self.savegameFolderPath .. '/courseplaySettings.xml';
self.cpCustomFieldsXmlFilePath = self.savegameFolderPath .. '/courseplayCustomFields.xml';
self.cpOldCustomFieldsXmlFilePath = self.savegameFolderPath .. '/courseplayFields.xml';
self.cpXmlFilePath = self.savegameFolderPath .. '/courseplay.xml';
self.oldCPFileExists = fileExists(self.cpXmlFilePath);
-- Course save path
self.cpCoursesFolderPath = ("%s%s/%s"):format(getUserProfileAppPath(),"CoursePlay_Courses", g_careerScreen.savegames[g_careerScreen.selectedIndex].mapId);
self.cpCourseManagerXmlFilePath = self.cpCoursesFolderPath .. "/courseManager.xml";
self.cpCourseStorageXmlFileTemplate = "courseStorage%04d.xml";
-- we need to create CoursePlay_Courses folder before we can create any new folders inside it.
createFolder(("%sCoursePlay_Courses"):format(getUserProfileAppPath()));
createFolder(self.cpCoursesFolderPath);
-- Add / at end of path, so we dont save that in the courseManager.xml (Needs to be done after folder creation!)
self.cpCoursesFolderPath = self.cpCoursesFolderPath .. "/";
end
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- SETUP DEFAULT GLOBAL DATA
courseplay.signs:setup();
courseplay.fields:setup();
self.showFieldScanYesNoDialogue = false;
self:setupWages();
self:setupIngameMap();
self:setup2dCourseData(false); -- NOTE: this call is only to initiate the position and opacity
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- LOAD SETTINGS FROM COURSEPLAYSETTINGS.XML / SAVE DEFAULT SETTINGS IF NOT EXISTING
if g_server ~= nil then
self:loadXmlSettings();
end
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- SETUP (continued)
courseplay.hud:setup(); -- NOTE: hud has to be set up after the xml settings have been loaded, as almost all its values are based on basePosX/Y
self:setUpDebugChannels(); -- NOTE: debugChannels have to be set up after the hud, as they rely on some hud values [positioning]
self:setupGlobalInfoText(); -- NOTE: globalInfoText has to be set up after the hud, as they rely on some hud values [colors, function]
courseplay.courses:setup(); -- NOTE: load the courses and folders from the XML
self:setup2dCourseData(true); -- NOTE: setup2dCourseData is called a second time, now we actually create the data and overlays
courseplay:register(true)-- NOTE: running here again to check whether there were mods loaded after courseplay
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- COURSEPLAYERS TABLES
self.totalCoursePlayers = {};
self.activeCoursePlayers = {};
self.numActiveCoursePlayers = 0;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- height for mouse text line in game's help menu
self.hudHelpMouseLineHeight = g_currentMission.helpBoxTextSize + g_currentMission.helpBoxTextLineSpacing*2;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- INPUT
self.playerOnFootMouseEnabled = false;
self.wasPlayerFrozen = false;
local ovl = courseplay.inputBindings.mouse.overlaySecondary;
if ovl then
local h = (2.5 * g_currentMission.helpBoxTextSize);
local w = h / g_screenAspectRatio;
ovl:setDimension(w, h);
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- FIELDS
if courseplay.fields.automaticScan then
self:setupFieldScanInfo();
end;
if g_server ~= nil then
courseplay.fields:loadCustomFields(fileExists(self.cpOldCustomFieldsXmlFilePath) and not fileExists(self.cpCustomFieldsXmlFilePath));
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- TIMERS
g_currentMission.environment:addMinuteChangeListener(self);
self.realTimeMinuteTimer = 0;
self.realTime10SecsTimer = 0;
self.realTime5SecsTimer = 0;
self.realTime5SecsTimerThrough = 0;
self.startFieldScanAfter = 1500; -- Start field scanning after specified milliseconds
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- DEV CONSOLE COMMANDS
if CpManager.isDeveloper then
addConsoleCommand('cpAddMoney', ('Add %s to your bank account'):format(g_i18n:formatMoney(5000000)), 'devAddMoney', self);
addConsoleCommand('cpAddFillLevels', 'Add 500\'000 l to all of your silos', 'devAddFillLevels', self);
end;
addConsoleCommand('cpStopAll', 'Stop all Courseplayers', 'devStopAll', self);
addConsoleCommand( 'cpSaveAllFields', 'Save all fields', 'devSaveAllFields', self )
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- TRIGGERS
self.confirmedNoneTipTriggers = {};
self.confirmedNoneTipTriggersCounter = 0;
self.confirmedNoneSpecialTriggers = {};
self.confirmedNoneSpecialTriggersCounter = 0;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- TRAFFIC
self.trafficCollisionIgnoreList = {};
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- MISCELLANEOUS
self.lightsNeeded = false;
end;
function CpManager:deleteMap()
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- empty courses and folders tables
g_currentMission.cp_courses = nil;
g_currentMission.cp_folders = nil;
g_currentMission.cp_sorted = nil;
courseplay.courses.batchWriteSize = nil;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- deactivate debug channels
for channel,_ in pairs(courseplay.debugChannels) do
courseplay.debugChannels[channel] = false;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete vehicles' button overlays
for i,vehicle in pairs(g_currentMission.steerables) do
if vehicle.cp ~= nil and vehicle.hasCourseplaySpec and vehicle.cp.buttons ~= nil then
courseplay.buttons:deleteButtonOverlays(vehicle);
end;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--delete globalInfoText overlays
for i,button in pairs(self.globalInfoText.buttons) do
button:deleteOverlay();
if self.globalInfoText.overlays[i] then
local ovl = self.globalInfoText.overlays[i];
if ovl.overlayId ~= nil and ovl.delete ~= nil then
ovl:delete();
end;
end;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete waypoint signs
for section,signDatas in pairs(courseplay.signs.buffer) do
for k,signData in pairs(signDatas) do
courseplay.signs:deleteSign(signData.sign);
end;
courseplay.signs.buffer[section] = {};
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete fields data and overlays
courseplay.fields.fieldData = {};
courseplay.fields.curFieldScanIndex = 0;
courseplay.fields.allFieldsScanned = false;
courseplay.fields.ingameDataSetUp = false;
for i,fruitData in pairs(courseplay.fields.seedUsageCalculator.fruitTypes) do
if fruitData.overlay then
fruitData.overlay:delete();
end;
end;
courseplay.fields.seedUsageCalculator = {};
courseplay.fields.seedUsageCalculator.fieldsWithoutSeedData = {};
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete help menu mouse overlay
if courseplay.inputBindings.mouse.overlaySecondary then
courseplay.inputBindings.mouse.overlaySecondary:delete();
courseplay.inputBindings.mouse.overlaySecondary = nil;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete fieldScanInfo overlays
if self.fieldScanInfo then
self.fieldScanInfo.bgOverlay:delete();
self.fieldScanInfo.progressBarOverlay:delete();
self.fieldScanInfo = nil;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete 2D course overlays
if self.course2dPolyOverlayId and self.course2dPolyOverlayId ~= 0 then
delete(self.course2dPolyOverlayId);
end;
if self.course2dTractorOverlay then
self.course2dTractorOverlay:delete();
end;
if self.course2dPdaMapOverlay then
self.course2dPdaMapOverlay:delete();
end;
end;
function CpManager:update(dt)
-- UPDATE CLOCK
courseplay.clock = courseplay.clock + dt
if g_currentMission.paused or (g_gui.currentGui ~= nil and g_gui.currentGuiName ~= 'inputCourseNameDialogue') then
return;
end;
if self.firstRun then
courseplay:addCpNilTempFillLevelFunction();
self.firstRun = false;
end;
if g_gui.currentGui == nil then
-- SETUP FIELD INGAME DATA
if not courseplay.fields.ingameDataSetUp then
courseplay.fields:setUpFieldsIngameData();
end;
-- SCAN ALL FIELD EDGES
if self.startFieldScanAfter > 0 then
self.startFieldScanAfter = self.startFieldScanAfter - dt;
end;
if g_currentMission.fieldDefinitionBase and courseplay.fields.automaticScan and not courseplay.fields.allFieldsScanned and self.startFieldScanAfter <= 0 then
courseplay.fields:setAllFieldEdges();
end;
-- Field scan, wages yes/no dialogue
if self.showFieldScanYesNoDialogue then
self:showYesNoDialogue('Courseplay', courseplay:loc('COURSEPLAY_YES_NO_FIELDSCAN'), self.fieldScanDialogueCallback, 'showFieldScanYesNoDialogue');
elseif self.showWagesYesNoDialogue then
local txt = courseplay:loc('COURSEPLAY_YES_NO_WAGES'):format(g_i18n:formatMoney(g_i18n:getCurrency(self.wagePerHour * self.wageDifficultyMultiplier), 2));
self:showYesNoDialogue('Courseplay', txt, self.wagesDialogueCallback, 'showWagesYesNoDialogue');
end;
end;
-- REAL TIME 10 SECS CHANGER
if self.wagesActive and g_server ~= nil then -- NOTE: if there are more items to be dealt with every 10 secs, remove the "wagesActive" restriction
if self.realTime10SecsTimer < 10000 then
self.realTime10SecsTimer = self.realTime10SecsTimer + dt;
else
self:realTime10SecsChanged();
self.realTime10SecsTimer = self.realTime10SecsTimer - 10000;
end;
end;
-- REAL TIME 5 SECS CHANGER
if self.realTime5SecsTimer < 5000 then
self.realTime5SecsTimer = self.realTime5SecsTimer + dt;
self.realTime5SecsTimerThrough = false;
else
self.realTime5SecsTimer = self.realTime5SecsTimer - 5000;
self.realTime5SecsTimerThrough = true;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- HELP MENU
if g_currentMission.showHelpText and g_gui.currentGui == nil and g_currentMission.controlledVehicle == nil and not g_currentMission.player.currentTool then
if self.playerOnFootMouseEnabled then
g_currentMission:addHelpTextFunction(self.drawMouseButtonHelp, self, self.hudHelpMouseLineHeight, courseplay:loc('COURSEPLAY_MOUSEARROW_HIDE'));
elseif self.globalInfoText.hasContent then
g_currentMission:addHelpTextFunction(self.drawMouseButtonHelp, self, self.hudHelpMouseLineHeight, courseplay:loc('COURSEPLAY_MOUSEARROW_SHOW'));
end;
end;
-- add a debug marker to the log file when Left Alt-D pressed
if InputBinding.hasEvent( InputBinding.COURSEPLAY_DEBUG_MARKER ) then
courseplay.logDebugMarker()
end
end;
function CpManager:draw()
if g_currentMission.paused then
return;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- DISPLAY GLOBALINFOTEXTS
local git = self.globalInfoText;
git.hasContent = false;
local numLinesRendered = 0;
local basePosY = git.posY;
if not (g_currentMission.ingameMap.isVisible and g_currentMission.ingameMap:getIsFullSize()) and next(git.content) ~= nil then
git.hasContent = true;
if g_currentMission.ingameMap.isVisible then
basePosY = git.posYAboveMap;
end;
numLinesRendered = self:renderGlobalInfoTexts(basePosY);
end;
git.buttonsClickArea.y1 = basePosY;
git.buttonsClickArea.y2 = basePosY + (numLinesRendered * (git.lineHeight + git.lineMargin));
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- DISPLAY FIELD SCAN MSG
if g_currentMission.fieldDefinitionBase and courseplay.fields.automaticScan and not courseplay.fields.allFieldsScanned and self.startFieldScanAfter <= 0 then
self:renderFieldScanInfo();
end;
end;
function CpManager:mouseEvent(posX, posY, isDown, isUp, mouseKey)
if g_currentMission.paused then return; end;
local area = self.globalInfoText.buttonsClickArea;
if area == nil then
return;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- LEFT CLICK
if (isDown or isUp) and mouseKey == courseplay.inputBindings.mouse.primaryButtonId and courseplay:mouseIsInArea(posX, posY, area.x1, area.x2, area.y1, area.y2) then
if self.globalInfoText.hasContent then
for i,button in pairs(self.globalInfoText.buttons) do
if button.show and button:getHasMouse(posX, posY) then
button:setClicked(isDown);
if isUp then
local sourceVehicle = g_currentMission.controlledVehicle or button.parameter;
button:handleMouseClick(sourceVehicle);
end;
break;
end;
end;
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- RIGHT CLICK
elseif isUp and mouseKey == courseplay.inputBindings.mouse.secondaryButtonId and g_currentMission.controlledVehicle == nil then
if self.globalInfoText.hasContent and not self.playerOnFootMouseEnabled and not g_currentMission.player.currentTool then
self.playerOnFootMouseEnabled = true;
self.wasPlayerFrozen = g_currentMission.isPlayerFrozen;
g_currentMission.isPlayerFrozen = true;
elseif self.playerOnFootMouseEnabled then
self.playerOnFootMouseEnabled = false;
if self.globalInfoText.hasContent then --if a button was hovered when deactivating the cursor, deactivate hover state
for _,button in pairs(self.globalInfoText.buttons) do
button:setClicked(false);
button:setHovered(false);
end;
end;
if not self.wasPlayerFrozen then
g_currentMission.isPlayerFrozen = false;
end;
end;
InputBinding.setShowMouseCursor(self.playerOnFootMouseEnabled);
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- HOVER
elseif not isDown and not isUp and self.globalInfoText.hasContent then
for _,button in pairs(self.globalInfoText.buttons) do
button:setClicked(false);
if button.show and not button.isHidden then
button:setHovered(button:getHasMouse(posX, posY));
end;
end;
end;
end;
function CpManager:keyEvent() end;
-- ####################################################################################################
function CpManager.saveXmlSettings(self)
if g_server == nil and g_dedicatedServerInfo == nil then return end;
-- Create folder in case there is none
createFolder(CpManager.savegameFolderPath);
-- createXMLFile will clear settings file if it exists
local cpSettingsXml = createXMLFile("cpSettingsXml", CpManager.cpSettingsXmlFilePath, "CPSettings");
if cpSettingsXml and cpSettingsXml ~= 0 then
local key = '';
-- Save Hud Possition
key = 'CPSettings.courseplayHud';
setXMLFloat(cpSettingsXml, key .. '#posX', courseplay.hud.basePosX);
setXMLFloat(cpSettingsXml, key .. '#posY', courseplay.hud.basePosY);
setXMLFloat(cpSettingsXml, key .. '#hudScale', courseplay.hud.sizeRatio);
setXMLFloat(cpSettingsXml, key .. '#uiScale', courseplay.hud.uiScale);
local string = "\n\tNOTE 1: Do not change the uiScale Manually.\n\tNOTE 2: If you change the hudScale and you haven't changed the posX and posY manually,\n\t\t\tthen you need to delete the posX and posY section to center the hud again.\n\t";
setXMLString(cpSettingsXml, key, string);
-- Save Fields Settings
key = 'CPSettings.courseplayFields';
setXMLBool(cpSettingsXml, key .. '#automaticScan', courseplay.fields.automaticScan);
setXMLBool(cpSettingsXml, key .. '#onlyScanOwnedFields', courseplay.fields.onlyScanOwnedFields);
setXMLBool(cpSettingsXml, key .. '#debugScannedFields', courseplay.fields.debugScannedFields);
setXMLBool(cpSettingsXml, key .. '#debugCustomLoadedFields', courseplay.fields.debugCustomLoadedFields);
setXMLInt (cpSettingsXml, key .. '#scanStep', courseplay.fields.scanStep);
-- Save Wages Settings
key = 'CPSettings.courseplayWages';
setXMLBool(cpSettingsXml, key .. '#active', CpManager.wagesActive);
setXMLInt (cpSettingsXml, key .. '#wagePerHour', CpManager.wagePerHour);
-- Save Ingame Map Settings
key = 'CPSettings.courseplayIngameMap';
setXMLBool(cpSettingsXml, key .. '#active', CpManager.ingameMapIconActive);
setXMLBool(cpSettingsXml, key .. '#showName', CpManager.ingameMapIconShowName);
setXMLBool(cpSettingsXml, key .. '#showCourse', CpManager.ingameMapIconShowCourse);
-- Save 2D Course Settings
key = 'CPSettings.course2D';
setXMLFloat(cpSettingsXml, key .. '#posX', CpManager.course2dPlotPosX);
setXMLFloat(cpSettingsXml, key .. '#posY', CpManager.course2dPlotPosY);
setXMLFloat(cpSettingsXml, key .. '#opacity', CpManager.course2dPdaMapOpacity);
saveXMLFile(cpSettingsXml);
delete(cpSettingsXml);
else
print(("COURSEPLAY ERROR: unable to load or create file -> %s"):format(CpManager.cpSettingsXmlFilePath));
end;
end;
g_careerScreen.saveSavegame = Utils.appendedFunction(g_careerScreen.saveSavegame, CpManager.saveXmlSettings);
-- adds courseplayer to global table, so that the system knows all of them
function CpManager:addToTotalCoursePlayers(vehicle)
local vehicleNum = (table.maxn(self.totalCoursePlayers) or 0) + 1;
self.totalCoursePlayers[vehicleNum] = vehicle;
CourseplayEvent.sendEvent(vehicle, "self.cp.coursePlayerNum", vehicleNum);
return vehicleNum;
end;
function CpManager:addToActiveCoursePlayers(vehicle)
self.numActiveCoursePlayers = self.numActiveCoursePlayers + 1;
self.activeCoursePlayers[vehicle.rootNode] = vehicle;
end;
function CpManager:removeFromActiveCoursePlayers(vehicle)
self.activeCoursePlayers[vehicle.rootNode] = nil;
self.numActiveCoursePlayers = math.max(self.numActiveCoursePlayers - 1, 0);
end;
function CpManager:devAddMoney()
if g_server ~= nil then
g_currentMission:addSharedMoney(5000000, 'other');
return ('Added %s to your bank account'):format(g_i18n:formatMoney(5000000));
end;
end;
function CpManager:devAddFillLevels()
if g_server ~= nil then
for fillType=1,FillUtil.NUM_FILLTYPES do
g_currentMission:setSiloAmount(fillType, g_currentMission:getSiloAmount(fillType) + 500000);
end;
return 'All silo fill levels increased by 500\'000.';
end;
end;
function CpManager:devStopAll()
if g_server ~= nil then
for _,vehicle in pairs (self.activeCoursePlayers) do
courseplay:stop(vehicle);
end
return ('stopped all Courseplayers');
end;
end;
function CpManager:devSaveAllFields()
courseplay.fields.saveAllFields()
return( 'All fields saved' )
end
function CpManager:setupFieldScanInfo()
-- FIELD SCAN INFO DISPLAY
self.fieldScanInfo = {};
local gfxPath = Utils.getFilename('img/fieldScanInfo.png', courseplay.path);
self.fieldScanInfo.fileWidth = 512;
self.fieldScanInfo.fileHeight = 256;
local bgUVs = { 41,210, 471,10 };
local bgW = courseplay.hud:pxToNormal(bgUVs[3] - bgUVs[1], 'x');
local bgH = courseplay.hud:pxToNormal(bgUVs[2] - bgUVs[4], 'y');
local bgX = 0.5 - bgW * 0.5;
local bgY = 0.5 - bgH * 0.5;
self.fieldScanInfo.bgOverlay = Overlay:new('fieldScanInfoBackground', gfxPath, bgX, bgY, bgW, bgH);
courseplay.utils:setOverlayUVsPx(self.fieldScanInfo.bgOverlay, bgUVs, self.fieldScanInfo.fileWidth, self.fieldScanInfo.fileHeight);
self.fieldScanInfo.textPosX = bgX + courseplay.hud:pxToNormal(10, 'x');
self.fieldScanInfo.textPosY = bgY + courseplay.hud:pxToNormal(55, 'y');
self.fieldScanInfo.titlePosY = bgY + courseplay.hud:pxToNormal(88, 'y');
self.fieldScanInfo.titleFontSize = courseplay.hud:pxToNormal(22, 'y');
self.fieldScanInfo.textFontSize = courseplay.hud:pxToNormal(16, 'y');
self.fieldScanInfo.progressBarMaxWidthPx = 406;
self.fieldScanInfo.progressBarMaxWidth = courseplay.hud:pxToNormal(406, 'x');
local pbH = courseplay.hud:pxToNormal(26, 'y');
self.fieldScanInfo.progressBarUVs = { 53,246, 459,220 };
local pbX = bgX + courseplay.hud:pxToNormal(12, 'x');
local pbY = bgY + courseplay.hud:pxToNormal(12, 'y');
self.fieldScanInfo.progressBarOverlay = Overlay:new('fieldScanInfoProgressBar', gfxPath, pbX, pbY, self.fieldScanInfo.progressBarMaxWidth, pbH);
courseplay.utils:setOverlayUVsPx(self.fieldScanInfo.progressBarOverlay, self.fieldScanInfo.progressBarUVs, self.fieldScanInfo.fileWidth, self.fieldScanInfo.fileHeight);
self.fieldScanInfo.percentColors = {
[0] = courseplay.utils:rgbToNormal(225, 27, 0),
[50] = courseplay.utils:rgbToNormal(255, 204, 0),
[100] = courseplay.utils:rgbToNormal(137, 243, 0)
};
self.fieldScanInfo.colorMapStep = 50;
end;
function CpManager:renderFieldScanInfo()
local fsi = self.fieldScanInfo;
fsi.bgOverlay:render();
local pct = courseplay.fields.curFieldScanIndex / g_currentMission.fieldDefinitionBase.numberOfFields;
local r, g, b = courseplay.utils:getColorFromPct(pct * 100, fsi.percentColors, fsi.colorMapStep);
fsi.progressBarOverlay:setColor(r, g, b, 1);
fsi.progressBarOverlay.width = fsi.progressBarMaxWidth * pct;
local widthPx = courseplay:round(fsi.progressBarMaxWidthPx * pct);
local newUVs = { fsi.progressBarUVs[1], fsi.progressBarUVs[2], fsi.progressBarUVs[1] + widthPx, fsi.progressBarUVs[4] };
courseplay.utils:setOverlayUVsPx(fsi.progressBarOverlay, newUVs, fsi.fileWidth, fsi.fileHeight);
fsi.progressBarOverlay:render();
courseplay:setFontSettings('white', false, 'left');
renderText(fsi.textPosX, fsi.titlePosY, fsi.titleFontSize, courseplay:loc('COURSEPLAY_FIELD_SCAN_IN_PROGRESS'));
local text = courseplay:loc('COURSEPLAY_SCANNING_FIELD_NMB'):format(courseplay.fields.curFieldScanIndex, g_currentMission.fieldDefinitionBase.numberOfFields);
courseplay:setFontSettings('white', false, 'left');
renderText(fsi.textPosX, fsi.textPosY, fsi.textFontSize, text);
-- reset font settings
courseplay:setFontSettings('white', false, 'left');
end;
function CpManager.drawMouseButtonHelp(self, posY, txt)
local xLeft = g_currentMission.helpBoxTextPos1X;
local xRight = g_currentMission.helpBoxTextPos2X;
local ovl = courseplay.inputBindings.mouse.overlaySecondary;
if ovl then
local y = posY - g_currentMission.helpBoxTextSize - g_currentMission.helpBoxTextLineSpacing*3;
ovl:setPosition(xLeft - ovl.width*0.2, y);
ovl:render();
xLeft = xLeft + ovl.width*0.6;
end;
posY = posY - g_currentMission.helpBoxTextSize - g_currentMission.helpBoxTextLineSpacing*2;
setTextAlignment(RenderText.ALIGN_RIGHT);
renderText(xRight, posY, g_currentMission.helpBoxTextSize, txt);
setTextAlignment(RenderText.ALIGN_LEFT);
renderText(xLeft, posY, g_currentMission.helpBoxTextSize, courseplay.inputBindings.mouse.secondaryTextI18n);
end;
function CpManager:severCombineTractorConnection(vehicle, callDelete)
if vehicle.cp then
-- VEHICLE IS COMBINE
if vehicle.cp.isCombine or vehicle.cp.isChopper or vehicle.cp.isHarvesterSteerable or vehicle.cp.isSugarBeetLoader or courseplay:isSpecialChopper(vehicle) then
courseplay:debug(('BaseMission:removeVehicle() -> severCombineTractorConnection(%q, %s) [VEHICLE IS COMBINE]'):format(nameNum(vehicle), tostring(callDelete)), 4);
local combine = vehicle;
-- remove this combine as savedCombine from all tractors
for i,tractor in pairs(g_currentMission.steerables) do
if tractor.hasCourseplaySpec and tractor.cp.savedCombine and tractor.cp.savedCombine == combine then
courseplay:debug(('\ttractor %q: savedCombine=%q --> removeSavedCombineFromTractor()'):format(nameNum(tractor), nameNum(combine)), 4);
courseplay:removeSavedCombineFromTractor(tractor);
end;
end;
-- unregister all tractors from this combine (activeCombine)
if combine.courseplayers ~= nil then
courseplay:debug(('\t.courseplayers ~= nil (%d courseplayers)'):format(#combine.courseplayers), 4);
if #combine.courseplayers > 0 then
for i,tractor in pairs(combine.courseplayers) do
courseplay:debug(('\t\t%q: removeActiveCombineFromTractor(), removeSavedCombineFromTractor()'):format(nameNum(tractor)), 4);
courseplay:removeActiveCombineFromTractor(tractor);
courseplay:removeSavedCombineFromTractor(tractor); --TODO (Jakob): unnecessary, as done above in steerables table already?
tractor.cp.reachableCombines = nil;
end;
courseplay:debug(('\t-> now has %d courseplayers'):format(#combine.courseplayers), 4);
end;
end;
-- VEHICLE IS TRACTOR
elseif vehicle.cp.activeCombine ~= nil or vehicle.cp.lastActiveCombine ~= nil or vehicle.cp.savedCombine ~= nil then
courseplay:debug(('BaseMission:removeVehicle() -> severCombineTractorConnection(%q, %s) [VEHICLE IS TRACTOR]'):format(nameNum(vehicle), tostring(callDelete)), 4);
courseplay:debug(('\tactiveCombine=%q, lastActiveCombine=%q, savedCombine=%q -> removeActiveCombineFromTractor(), removeSavedCombineFromTractor()'):format(nameNum(vehicle.cp.activeCombine), nameNum(vehicle.cp.lastActiveCombine), nameNum(vehicle.cp.savedCombine)), 4);
courseplay:removeActiveCombineFromTractor(vehicle);
courseplay:removeSavedCombineFromTractor(vehicle);
courseplay:debug(('\t-> activeCombine=%q, lastActiveCombine=%q, savedCombine=%q'):format(nameNum(vehicle.cp.activeCombine), nameNum(vehicle.cp.lastActiveCombine), nameNum(vehicle.cp.savedCombine)), 4);
end;
end;
end;
BaseMission.removeVehicle = Utils.prependedFunction(BaseMission.removeVehicle, CpManager.severCombineTractorConnection);
local nightStart, dayStart = 19 * 3600000, 7.5 * 3600000; -- from 7pm until 7:30am
function CpManager:minuteChanged()
-- WEATHER
local env = g_currentMission.environment;
self.lightsNeeded = env.needsLights or (env.dayTime >= nightStart or env.dayTime <= dayStart) or env.currentRain ~= nil or env.curRain ~= nil or (env.lastRainScale > 0.1 and env.timeSinceLastRain < 30);
end;
function CpManager:realTime10SecsChanged()
-- WAGES
if self.wagesActive and g_server ~= nil then
local totalWages = 0;
for vehicleNum, vehicle in pairs(self.activeCoursePlayers) do
if vehicle:getIsCourseplayDriving() and not vehicle.aiIsStarted then
totalWages = totalWages + self.wagePer10Secs;
end;
end;
if totalWages > 0 then
g_currentMission:addSharedMoney(-totalWages * self.wageDifficultyMultiplier, 'wagePayment');
end;
end;
end;
function CpManager:showYesNoDialogue(title, text, callbackFn, showBoolVar)
local yesNoDialogue = g_gui:showGui('YesNoDialog');
yesNoDialogue.target:setTitle(title);
yesNoDialogue.target:setText(text);
yesNoDialogue.target:setCallback(callbackFn, self);
self[showBoolVar] = false;
end;
-- ####################################################################################################
-- FIELD SCAN Y/N DIALOGUE
function CpManager:fieldScanDialogueCallback(setActive)
courseplay.fields.automaticScan = setActive;
g_gui:showGui('');
end;
-- ####################################################################################################
-- WAGES
function CpManager:setupWages()
self.wageDifficultyMultiplier = Utils.lerp(0.5, 1, (g_currentMission.missionInfo.difficulty - 1) / 2);
self.wagesActive = true;
self.wagePerHour = 1500;
self.wagePer10Secs = self.wagePerHour / 360;
self.showWagesYesNoDialogue = false;
end;
function CpManager:wagesDialogueCallback(setActive)
self.wagesActive = setActive;
g_gui:showGui('');
end;
-- ####################################################################################################
-- INGAME MAP
function CpManager:setupIngameMap()
self.ingameMapIconActive = true;
self.ingameMapIconShowName = true;
self.ingameMapIconShowCourse = true;
self.ingameMapIconShowText = self.ingameMapIconShowName or self.ingameMapIconShowCourse;
self.ingameMapIconShowTextLoaded = self.ingameMapIconShowText;
end;
-- ####################################################################################################
-- GLOBALINFOTEXT
function CpManager:setupGlobalInfoText()
print('## Courseplay: setting up globalInfoText');
self.globalInfoText = {};
self.globalInfoText.posY = 0.01238; -- = ingameMap posY
self.globalInfoText.posYAboveMap = self.globalInfoText.posY + 0.027777777777778 + 0.20833333333333;
self.globalInfoText.fontSize = courseplay.hud:pxToNormal(18, 'y');
self.globalInfoText.lineHeight = self.globalInfoText.fontSize * 1.2;
self.globalInfoText.lineMargin = self.globalInfoText.lineHeight * 0.2;
self.globalInfoText.buttonHeight = self.globalInfoText.lineHeight;
self.globalInfoText.buttonWidth = self.globalInfoText.buttonHeight / g_screenAspectRatio;
self.globalInfoText.buttonPosX = 0.015625; -- = ingameMap posX
self.globalInfoText.buttonMargin = self.globalInfoText.buttonWidth * 0.4;
self.globalInfoText.backgroundPadding = self.globalInfoText.buttonWidth * 0.2;
self.globalInfoText.backgroundImg = 'dataS2/menu/white.png';
self.globalInfoText.backgroundPosX = self.globalInfoText.buttonPosX + self.globalInfoText.buttonWidth + self.globalInfoText.buttonMargin;
self.globalInfoText.backgroundPosY = self.globalInfoText.posY;
self.globalInfoText.textPosX = self.globalInfoText.backgroundPosX + self.globalInfoText.backgroundPadding;
self.globalInfoText.content = {};
self.globalInfoText.vehicleHasText = {};
self.globalInfoText.levelColors = {
[-2] = courseplay.hud.colors.closeRed;
[-1] = courseplay.hud.colors.activeRed;
[0] = courseplay.hud.colors.hover;
[1] = courseplay.hud.colors.activeGreen;
};
self.globalInfoText.maxNum = 20;
self.globalInfoText.overlays = {};
self.globalInfoText.buttons = {};
for i=1, self.globalInfoText.maxNum do
local posY = self.globalInfoText.backgroundPosY + (i - 1) * self.globalInfoText.lineHeight;
self.globalInfoText.overlays[i] = Overlay:new('globalInfoTextOverlay' .. i, self.globalInfoText.backgroundImg, self.globalInfoText.backgroundPosX, posY, 0.1, self.globalInfoText.buttonHeight);
courseplay.button:new(self, 'globalInfoText', 'iconSprite.png', 'goToVehicle', i, self.globalInfoText.buttonPosX, posY, self.globalInfoText.buttonWidth, self.globalInfoText.buttonHeight);
end;
self.globalInfoText.buttonsClickArea = {
x1 = self.globalInfoText.buttonPosX;
x2 = self.globalInfoText.buttonPosX + self.globalInfoText.buttonWidth;
y1 = self.globalInfoText.backgroundPosY,
y2 = self.globalInfoText.backgroundPosY + (self.globalInfoText.maxNum * (self.globalInfoText.lineHeight + self.globalInfoText.lineMargin));
};
self.globalInfoText.hasContent = false;
self.globalInfoText.msgReference = {
BALER_NETS = { level = -2, text = 'COURSEPLAY_BALER_NEEDS_NETS' };
BGA_IS_FULL = { level = -1, text = 'COURSEPLAY_BGA_IS_FULL'};
DAMAGE_IS = { level = 0, text = 'COURSEPLAY_DAMAGE_IS_BEING_REPAIRED' };
DAMAGE_MUST = { level = -2, text = 'COURSEPLAY_DAMAGE_MUST_BE_REPAIRED' };
DAMAGE_SHOULD = { level = -1, text = 'COURSEPLAY_DAMAGE_SHOULD_BE_REPAIRED' };
END_POINT = { level = 0, text = 'COURSEPLAY_REACHED_END_POINT' };
END_POINT_MODE_1 = { level = 0, text = 'COURSEPLAY_REACHED_END_POINT_MODE_1' };
END_POINT_MODE_8 = { level = 0, text = 'COURSEPLAY_REACHED_END_POINT_MODE_8' };
FARM_SILO_NO_FILLTYPE = { level = -2, text = 'COURSEPLAY_FARM_SILO_NO_FILLTYPE'};
FARM_SILO_IS_EMPTY = { level = 0, text = 'COURSEPLAY_FARM_SILO_IS_EMPTY'};
FARM_SILO_IS_FULL = { level = 0, text = 'COURSEPLAY_FARM_SILO_IS_FULL'};
FUEL_IS = { level = 0, text = 'COURSEPLAY_IS_BEING_REFUELED' };
FUEL_MUST = { level = -2, text = 'COURSEPLAY_MUST_BE_REFUELED' };
FUEL_SHOULD = { level = -1, text = 'COURSEPLAY_SHOULD_BE_REFUELED' };
HOSE_MISSING = { level = -2, text = 'COURSEPLAY_HOSEMISSING' };
NEEDS_REFILLING = { level = -1, text = 'COURSEPLAY_NEEDS_REFILLING' };
NEEDS_UNLOADING = { level = -1, text = 'COURSEPLAY_NEEDS_UNLOADING' };
OVERLOADING_POINT = { level = 0, text = 'COURSEPLAY_REACHED_OVERLOADING_POINT' };
PICKUP_JAMMED = { level = -2, text = 'COURSEPLAY_PICKUP_JAMMED' };
SLIPPING_1 = { level = -1, text = 'COURSEPLAY_SLIPPING_WARNING' };
SLIPPING_2 = { level = -2, text = 'COURSEPLAY_SLIPPING_WARNING' };
TRAFFIC = { level = -1, text = 'COURSEPLAY_IS_IN_TRAFFIC' };
UNLOADING_BALE = { level = 0, text = 'COURSEPLAY_UNLOADING_BALES' };
WAIT_POINT = { level = 0, text = 'COURSEPLAY_REACHED_WAITING_POINT' };
WATER = { level = -2, text = 'COURSEPLAY_WATER_WARNING' };
WEATHER = { level = 0, text = 'COURSEPLAY_WEATHER_WARNING' };
WEIGHING_VEHICLE = { level = 0, text = 'COURSEPLAY_IS_BEING_WEIGHED' };
WORK_END = { level = 1, text = 'COURSEPLAY_WORK_END' };
};
end;
function CpManager:setGlobalInfoText(vehicle, refIdx, forceRemove)
local git = self.globalInfoText;
--print(string.format('setGlobalInfoText(vehicle, %s, %s)', tostring(refIdx), tostring(forceRemove)));
if forceRemove == true then
if g_server ~= nil then
CourseplayEvent.sendEvent(vehicle, "setMPGlobalInfoText", refIdx, false, forceRemove)
end
if git.content[vehicle.rootNode][refIdx] then
git.content[vehicle.rootNode][refIdx] = nil;
end;
vehicle.cp.activeGlobalInfoTexts[refIdx] = nil;
vehicle.cp.numActiveGlobalInfoTexts = vehicle.cp.numActiveGlobalInfoTexts - 1;
--print(string.format('\t%s: remove globalInfoText[%s] from global table, numActiveGlobalInfoTexts=%d', nameNum(vehicle), refIdx, vehicle.cp.numActiveGlobalInfoTexts));
if vehicle.cp.numActiveGlobalInfoTexts == 0 then
git.content[vehicle.rootNode] = nil;
--print(string.format('\t\tset globalInfoText.content[rootNode] to nil'));
end;
return;
end;
vehicle.cp.hasSetGlobalInfoTextThisLoop[refIdx] = true;
local data = git.msgReference[refIdx];
--print(string.format('refIdx=%q, level=%s, text=%q, textLoc=%q', tostring(refIdx), tostring(data.level), tostring(data.text), tostring(courseplay:loc(data.text))));
if vehicle.cp.activeGlobalInfoTexts[refIdx] == nil or vehicle.cp.activeGlobalInfoTexts[refIdx] ~= data.level then
if g_server ~= nil then
CourseplayEvent.sendEvent(vehicle, "setMPGlobalInfoText", refIdx, false, forceRemove)
end
if vehicle.cp.activeGlobalInfoTexts[refIdx] == nil then
vehicle.cp.numActiveGlobalInfoTexts = vehicle.cp.numActiveGlobalInfoTexts + 1;
end;
local text = nameNum(vehicle) .. " " .. courseplay:loc(data.text);
--print(string.format('\t%s: setGlobalInfoText [%q] numActiveGlobalInfoTexts=%d, lvl %d, text=%q', nameNum(vehicle), refIdx, vehicle.cp.numActiveGlobalInfoTexts, data.level, tostring(text)));
vehicle.cp.activeGlobalInfoTexts[refIdx] = data.level;
if git.content[vehicle.rootNode] == nil then
git.content[vehicle.rootNode] = {};
end;
git.content[vehicle.rootNode][refIdx] = {
level = data.level,
text = text,
backgroundWidth = getTextWidth(git.fontSize, text) + git.backgroundPadding * 2.5,
vehicle = vehicle
};
end;
end;
function CpManager:renderGlobalInfoTexts(basePosY)
local git = self.globalInfoText;
local line = 0;
courseplay:setFontSettings('white', false, 'left');
for _,refIndexes in pairs(git.content) do
if line >= self.globalInfoText.maxNum then
break;
end;
for refIdx,data in pairs(refIndexes) do
line = line + 1;
-- background
local bg = git.overlays[line];
bg:setColor(unpack(git.levelColors[data.level]));
local gfxPosY = basePosY + (line - 1) * (git.lineHeight + git.lineMargin);
bg:setPosition(bg.x, gfxPosY);
bg:setDimension(data.backgroundWidth, bg.height);
bg:render();
-- text
local textPosY = gfxPosY + (git.lineHeight - git.fontSize) * 1.2; -- should be (lineHeight-fontSize)*0.5, but there seems to be some pixel/sub-pixel rendering error
renderText(git.textPosX, textPosY, git.fontSize, data.text);
-- button
local button = self.globalInfoText.buttons[line];
if button ~= nil then
button:setPosition(button.overlay.x, gfxPosY)
local currentColor = button.curColor;
local targetColor = currentColor;
button:setCanBeClicked(true);
button:setDisabled(data.vehicle.isBroken or data.vehicle.isControlled);
button:setParameter(data.vehicle);
if g_currentMission.controlledVehicle and g_currentMission.controlledVehicle == data.vehicle then
targetColor = 'activeGreen';
button:setCanBeClicked(false);
elseif button.isDisabled then
targetColor = 'whiteDisabled';
elseif button.isClicked then
targetColor = 'activeRed';
elseif button.isHovered then
targetColor = 'hover';
else
targetColor = 'white';
end;
-- set color
if currentColor ~= targetColor then
button:setColor(targetColor);
end;
-- NOTE: do not use button:render() here, as we neither need the button.show check, nor the hoveredButton var, nor the color setting. Simply rendering the overlay suffices
button.overlay:render();
end;
end;
end;
return line;
end;
-- ####################################################################################################
-- 2D COURSE DRAW SETUP
function CpManager:setup2dCourseData(createOverlays)
if not createOverlays then
self.course2dPlotPosX = 0.65;
self.course2dPlotPosY = 0.35;
self.course2dPdaMapOpacity = 0.7;
self.course2dColorTable = {
[0] = courseplay.utils:rgbToNormal( 24, 225, 0),
[50] = courseplay.utils:rgbToNormal(255, 230, 0),
[100] = courseplay.utils:rgbToNormal(210, 5, 0)
};
self.course2dColorPctStep = 50;
local height = courseplay.hud:getFullPx(0.3 * 1920 / 1080, 'y');
local width = height / g_screenAspectRatio;
self.course2dPlotField = { x = self.course2dPlotPosX, y = self.course2dPlotPosY, width = width, height = height }; -- definition of plot field for 2D
-- print(('course2dPlotField: x=%f (%.1f px), y=%f (%.1f px), width=%.1f (%.1f px), height=%.2f (%.1f px)'):format(self.course2dPlotPosX, self.course2dPlotPosX * g_screenWidth, self.course2dPlotPosY, self.course2dPlotPosY * g_screenHeight, width, width * g_screenWidth, height, height * g_screenHeight));
return;
end;
self.course2dPolyOverlayId = createImageOverlay('dataS/scripts/shared/graph_pixel.dds');
local w, h = courseplay.hud:getPxToNormalConstant(14, 10);
self.course2dTractorOverlay = Overlay:new('cpTractorIndicator', courseplay.hud.iconSpritePath, 0.5, 0.5, w, h);
courseplay.utils:setOverlayUVsPx(self.course2dTractorOverlay, courseplay.hud.buttonUVsPx.recordingPlay, courseplay.hud.iconSpriteSize.x, courseplay.hud.iconSpriteSize.y);
self.course2dTractorOverlay:setColor(0,0.8,1,1);
end;
-- ####################################################################################################
-- LOAD SETTINGS FROM COURSEPLAYSETTINGS.XML / SET DEFAULT SETTINGS IF NOT EXISTING
function CpManager:loadXmlSettings()
createFolder(self.savegameFolderPath);
local cpSettingsXml;
if fileExists(self.cpSettingsXmlFilePath) then
cpSettingsXml = loadXMLFile('cpSettingsXml', self.cpSettingsXmlFilePath);
else
print('## Courseplay: loading default settings');
if not self.oldCPFileExists then
self.showFieldScanYesNoDialogue = true;
self.showWagesYesNoDialogue = true;
end;
return;
end;
if cpSettingsXml and cpSettingsXml ~= 0 then
print('## Courseplay: loading settings from "courseplaySettings.xml"');
-- hud position
local key = 'CPSettings.courseplayHud';
local sizeRatio, uiScale = getXMLFloat(cpSettingsXml, key .. '#hudScale'), getXMLFloat(cpSettingsXml, key .. '#uiScale');
if sizeRatio and sizeRatio ~= courseplay.hud.sizeRatio then
courseplay.hud.sizeRatio = sizeRatio;
-- Reposition hud based on size.
courseplay.hud.basePosX = 0.5 - courseplay.hud:pxToNormal(630 / 2, 'x'); -- Center Screen - half hud width
courseplay.hud.basePosY = courseplay.hud:pxToNormal(32, 'y');
end;
local newUiScale = courseplay.hud.uiScale;
local posX, posY = getXMLFloat(cpSettingsXml, key .. '#posX'), getXMLFloat(cpSettingsXml, key .. '#posY');
if uiScale and posX then
posX = courseplay.hud:getFullPx(posX, 'x');
end;
if uiScale and posY then
posY = courseplay.hud:getFullPx(posY, 'y');
end;
-- Check if the UI Scale have been changed since last time and reset center position if needed.
if uiScale and uiScale ~= newUiScale then
print("## CoursePlay: UI Scale have changed. Recalculating hud positions.");
-- Set the uiScale to the loaded one so we can get the original posX
courseplay.hud.uiScale = uiScale;
-- Get the original posX
local oldPosX = 0.5 - courseplay.hud:pxToNormal(630 / 2, 'x');
local oldPosY = courseplay.hud:pxToNormal(32, 'y');
-- Reset the uiScale back to the new one.
courseplay.hud.uiScale = newUiScale;
-- if the position is the same, then we need to update it to the new center position.
-- NOTE: If they are not the same, then the posX might have been changed by the user for there own position, and then we dont change it back to the center position.
if not posX or (posX and oldPosX == posX) then
courseplay.hud.basePosX = 0.5 - courseplay.hud:pxToNormal(630 / 2, 'x'); -- Center Screen - half hud width
end;
if not posY or (posY and oldPosY == posY) then
courseplay.hud.basePosY = courseplay.hud:pxToNormal(32, 'y');
end;
-- Get the saved position if UI Scale are the same.
else
if uiScale and posX then
courseplay.hud.basePosX = posX;
end;
if uiScale and posY then
courseplay.hud.basePosY = posY;
end;
end;
-- fields settings
key = 'CPSettings.courseplayFields';
local fieldsAutomaticScan = getXMLBool(cpSettingsXml, key .. '#automaticScan');
if fieldsAutomaticScan ~= nil then
courseplay.fields.automaticScan = fieldsAutomaticScan;
elseif not self.oldCPFileExists then
self.showFieldScanYesNoDialogue = true;
end;
courseplay.fields.onlyScanOwnedFields = Utils.getNoNil(getXMLBool(cpSettingsXml, key .. '#onlyScanOwnedFields'), courseplay.fields.onlyScanOwnedFields);
courseplay.fields.debugScannedFields = Utils.getNoNil(getXMLBool(cpSettingsXml, key .. '#debugScannedFields'), courseplay.fields.debugScannedFields);
courseplay.fields.debugCustomLoadedFields = Utils.getNoNil(getXMLBool(cpSettingsXml, key .. '#debugCustomLoadedFields'), courseplay.fields.debugCustomLoadedFields);
courseplay.fields.scanStep = Utils.getNoNil( getXMLInt(cpSettingsXml, key .. '#scanStep'), courseplay.fields.scanStep);
-- wages
key = 'CPSettings.courseplayWages';
local wagesActive, wagePerHour = getXMLBool(cpSettingsXml, key .. '#active'), getXMLInt(cpSettingsXml, key .. '#wagePerHour');
if wagesActive ~= nil then
self.wagesActive = wagesActive;
elseif not self.oldCPFileExists then
self.showWagesYesNoDialogue = true;
end;
if wagePerHour ~= nil then
self.wagePerHour = wagePerHour;
elseif not self.oldCPFileExists then
self.showWagesYesNoDialogue = true;
end;
self.wagePer10Secs = self.wagePerHour / 360;
-- ingame map
key = 'CPSettings.courseplayIngameMap';
self.ingameMapIconActive = Utils.getNoNil(getXMLBool(cpSettingsXml, key .. '#active'), self.ingameMapIconActive);
self.ingameMapIconShowName = Utils.getNoNil(getXMLBool(cpSettingsXml, key .. '#showName'), self.ingameMapIconShowName);
self.ingameMapIconShowCourse = Utils.getNoNil(getXMLBool(cpSettingsXml, key .. '#showCourse'), self.ingameMapIconShowCourse);
self.ingameMapIconShowText = true --self.ingameMapIconShowName or self.ingameMapIconShowCourse;
-- 2D course
key = 'CPSettings.course2D';
self.course2dPlotPosX = Utils.getNoNil(getXMLFloat(cpSettingsXml, key .. '#posX'), self.course2dPlotPosX);
self.course2dPlotPosY = Utils.getNoNil(getXMLFloat(cpSettingsXml, key .. '#posY'), self.course2dPlotPosY);
self.course2dPdaMapOpacity = Utils.getNoNil(getXMLFloat(cpSettingsXml, key .. '#opacity'), self.course2dPdaMapOpacity);
self.course2dPlotField.x = self.course2dPlotPosX;
self.course2dPlotField.y = self.course2dPlotPosY;