forked from Courseplay/courseplay
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCpManager.lua
More file actions
983 lines (848 loc) · 41.7 KB
/
CpManager.lua
File metadata and controls
983 lines (848 loc) · 41.7 KB
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
local curFile = 'CpManager.lua';
CpManager = {};
local CpManager_mt = Class(CpManager);
addModEventListener(CpManager);
function CpManager:loadMap(name)
self.isCourseplayManager = true;
self.firstRun = true;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- XML PATHS
local savegameDir;
if g_currentMission.missionInfo.savegameDirectory then
savegameDir = g_currentMission.missionInfo.savegameDirectory;
end;
if not savegameDir and g_careerScreen.currentSavegame and g_careerScreen.currentSavegame.savegameIndex then -- TODO (Jakob): g_careerScreen.currentSavegame not available on DS. MP maybe as well
savegameDir = ('%ssavegame%d'):format(getUserProfileAppPath(), g_careerScreen.currentSavegame.savegameIndex);
end;
if not savegameDir and g_currentMission.missionInfo.savegameIndex ~= nil then
savegameDir = ('%ssavegame%d'):format(getUserProfileAppPath(), g_careerScreen.missionInfo.savegameIndex);
end;
self.savegameFolderPath = savegameDir;
self.cpXmlFilePath = self.savegameFolderPath .. '/courseplay.xml';
self.cpFieldsXmlFilePath = self.savegameFolderPath .. '/courseplayFields.xml';
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- SETUP DEFAULT GLOBAL DATA
courseplay.signs:setup();
courseplay.fields:setup();
self.showFieldScanYesNoDialogue = false;
self:setupWages();
self:setupIngameMap();
courseplay.courses:setup(); -- NOTE: this call is only to set up batchWriteSize, without loading anything
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- LOAD SETTINGS FROM COURSEPLAY.XML / SAVE DEFAULT SETTINGS IF NOT EXISTING
self:loadOrSetXmlSettings();
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 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(true); -- NOTE: courses:setup is called a second time, now we actually load the courses and folders from the XML
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- COURSEPLAYERS TABLES
self.totalCoursePlayers = {};
self.activeCoursePlayers = {};
self.numActiveCoursePlayers = 0;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- height for mouse text line in game's help menu
self.hudHelpMouseLineHeight = g_currentMission.hudHelpTextSize + g_currentMission.hudHelpTextLineSpacing*2;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- INPUT
self.playerOnFootMouseEnabled = false;
self.wasPlayerFrozen = false;
local ovl = courseplay.inputBindings.mouse.overlaySecondary;
if ovl then
local h = (2.5 * g_currentMission.hudHelpTextSize);
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:loadAllCustomFields();
end;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- TIMERS
g_currentMission.environment:addMinuteChangeListener(self);
self.realTimeMinuteTimer = 0;
self.realTime10SecsTimer = 0;
self.realTime5SecsTimer = 0;
self.realTime5SecsTimerThrough = 0;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 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;
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- 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;
end;
function CpManager:update(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 courseplay.fields.automaticScan and not courseplay.fields.allFieldsScanned 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;
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 courseplay.fields.automaticScan and not courseplay.fields.allFieldsScanned 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;
-- ####################################################################################################
-- Giants - very intelligently - deletes any mod file in the savegame folder when saving. And now we get it back!
function CpManager.backupCpFiles(self)
if g_server == nil and g_dedicatedServerInfo == nil then return end;
-- print('backupCpFiles()');
if not fileExists(CpManager.cpXmlFilePath) then
-- ERROR: CP FILE DOESN'T EXIST
return;
end;
-- backup CP files before saveSavegame() deletes them
local savegameIndex = g_currentMission.missionInfo.savegameIndex;
CpManager.cpTempSaveFolderPath = getUserProfileAppPath() .. 'courseplayBackupSavegame' .. savegameIndex;
createFolder(CpManager.cpTempSaveFolderPath);
-- print(' create folder at ' .. CpManager.cpTempSaveFolderPath);
CpManager.cpFileBackupPath = CpManager.cpTempSaveFolderPath .. '/courseplay.xml';
copyFile(CpManager.cpXmlFilePath, CpManager.cpFileBackupPath, true);
-- print((' copy cp file to backup folder: %q'):format(CpManager.cpFileBackupPath));
if fileExists(CpManager.cpFieldsXmlFilePath) then
CpManager.cpFieldsFileBackupPath = CpManager.cpTempSaveFolderPath .. '/courseplayFields.xml';
copyFile(CpManager.cpFieldsXmlFilePath, CpManager.cpFieldsFileBackupPath, true);
-- print((' copy cp fields file to backup folder: %q'):format(CpManager.cpFieldsFileBackupPath));
end;
end;
g_careerScreen.saveSavegame = Utils.prependedFunction(g_careerScreen.saveSavegame, CpManager.backupCpFiles);
function CpManager.getThatFuckerBack(self)
if g_server == nil and g_dedicatedServerInfo == nil then return end;
-- print('getThatFuckerBack()');
if not CpManager.cpFileBackupPath then return end;
local savegameIndex = g_currentMission.missionInfo.savegameIndex;
local savegameFolderPath = g_currentMission.missionInfo.savegameDirectory or CpManager.savegameFolderPath;
if savegameFolderPath == nil then
savegameFolderPath = ('%ssavegame%d'):format(getUserProfileAppPath(), savegameIndex);
end;
if fileExists(savegameFolderPath .. '/careerSavegame.xml') then -- savegame isn't corrupted and has been saved correctly
-- print(' orig savegame folder still exists');
-- copy backed up files back to our savegame directory
-- print((' copy backup cp file to orig savegame folder: %q'):format(CpManager.cpXmlFilePath));
copyFile(CpManager.cpFileBackupPath, CpManager.cpXmlFilePath, true);
CpManager.cpFileBackupPath = nil;
if CpManager.cpFieldsFileBackupPath then
-- print((' copy backup cp fields file to orig savegame folder: %q'):format(CpManager.cpFieldsXmlFilePath));
copyFile(CpManager.cpFieldsFileBackupPath, CpManager.cpFieldsXmlFilePath, true);
CpManager.cpFieldsFileBackupPath = nil;
end;
deleteFolder(CpManager.cpTempSaveFolderPath);
-- print(' delete backup folder');
else -- corrupt savegame: display backup info message
print(('This savegame has been corrupted. The Courseplay file has been backed up to %q'):format(CpManager.cpTempSaveFolderPath));
local msgTitle = 'Courseplay';
local msgTxt = ('This savegame has been corrupted.\nYour Courseplay %s been backed up to the "courseplayBackupSavegame%d" directory.\n\nFull path: %q'):format(CpManager.cpFieldsFileBackupPath and 'files have' or 'file has', savegameIndex, CpManager.cpTempSaveFolderPath);
g_currentMission.inGameMessage:showMessage(msgTitle, msgTxt, 15000, false);
end;
end;
g_careerScreen.saveSavegame = Utils.appendedFunction(g_careerScreen.saveSavegame, CpManager.getThatFuckerBack);
-- 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,Fillable.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: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 = {
{ pct = 0.0, color = { 225/255, 27/255, 0/255 } },
{ pct = 0.5, color = { 255/255, 204/255, 0/255 } },
{ pct = 1.0, color = { 137/255, 243/255, 0/255 } }
};
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, fsi.percentColors);
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({ 0.8, 0.8, 0.8, 1 }, true, 'left');
renderText(fsi.textPosX, fsi.titlePosY - 0.001, fsi.titleFontSize, courseplay:loc('COURSEPLAY_FIELD_SCAN_IN_PROGRESS'));
courseplay:setFontSettings('shadow', true);
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({ 0.8, 0.8, 0.8, 1 }, false);
renderText(fsi.textPosX, fsi.textPosY - 0.001, fsi.textFontSize, text);
courseplay:setFontSettings('shadow', false);
renderText(fsi.textPosX, fsi.textPosY, fsi.textFontSize, text);
-- reset font settings
courseplay:setFontSettings('white', true);
end;
function CpManager.drawMouseButtonHelp(self, posY, txt)
local xLeft = g_currentMission.hudHelpTextPosX1;
local xRight = g_currentMission.hudHelpTextPosX2;
local ovl = courseplay.inputBindings.mouse.overlaySecondary;
if ovl then
local y = posY - g_currentMission.hudHelpTextSize - g_currentMission.hudHelpTextLineSpacing*3;
ovl:setPosition(xLeft - ovl.width*0.2, y);
ovl:render();
xLeft = xLeft + ovl.width*0.6;
end;
posY = posY - g_currentMission.hudHelpTextSize - g_currentMission.hudHelpTextLineSpacing*2;
setTextAlignment(RenderText.ALIGN_RIGHT);
renderText(xRight, posY, g_currentMission.hudHelpTextSize, txt);
setTextAlignment(RenderText.ALIGN_LEFT);
renderText(xLeft, posY, g_currentMission.hudHelpTextSize, 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.isHired 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.titleElement:setText(title);
yesNoDialogue.target:setText(text);
yesNoDialogue.target:setCallbacks(callbackFn, self);
self[showBoolVar] = false;
end;
-- ####################################################################################################
-- FIELD SCAN Y/N DIALOGUE
function CpManager:fieldScanDialogueCallback(setActive)
courseplay.fields.automaticScan = setActive;
local file = loadXMLFile('courseplayFile', self.cpXmlFilePath);
if file and file ~= 0 then
setXMLBool(file, 'XML.courseplayFields#automaticScan', setActive);
saveXMLFile(file);
delete(file);
end;
g_gui:showGui('');
end;
-- ####################################################################################################
-- WAGES
function CpManager:setupWages()
self.wageDifficultyMultiplier = Utils.lerp(0.5, 1, (g_currentMission.missionStats.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;
local file = loadXMLFile('courseplayFile', self.cpXmlFilePath);
if file and file ~= 0 then
setXMLBool(file, 'XML.courseplayWages#active', setActive);
saveXMLFile(file);
delete(file);
end;
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' };
FARM_SILO_NO_FILLTYPE = { level = -2, text = 'COURSEPLAY_FARM_SILO_NO_FILLTYPE'};
FARM_SILO_IS_EMPTY = { level = 0, text = 'COURSEPLAY_FARM_SILO_IS_EMPTY'};
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' };
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;
-- ####################################################################################################
-- LOAD SETTINGS FROM courseplay.xml / SET DEFAULT SETTINGS IF NOT EXISTING
function CpManager:loadOrSetXmlSettings()
if self.savegameFolderPath and self.cpXmlFilePath then
createFolder(self.savegameFolderPath);
local cpFile;
if fileExists(self.cpXmlFilePath) then
cpFile = loadXMLFile('cpFile', self.cpXmlFilePath);
else
self:createXmlSettings();
return;
end;
print('## Courseplay: loading settings from "courseplay.xml"');
-- hud position
local key = 'XML.courseplayHud';
local posX, posY = getXMLFloat(cpFile, key .. '#posX'), getXMLFloat(cpFile, key .. '#posY');
if posX then
courseplay.hud.basePosX = courseplay.hud:getFullPx(posX, 'x');
else
setXMLFloat(cpFile, key .. '#posX', courseplay.hud.basePosX);
end;
if posY then
courseplay.hud.basePosY = courseplay.hud:getFullPx(posY, 'y');
else
setXMLFloat(cpFile, key .. '#posY', courseplay.hud.basePosY);
end;
-- fields settings
key = 'XML.courseplayFields';
local fieldsAutomaticScan = getXMLBool(cpFile, key .. '#automaticScan');
local fieldsOnlyScanOwnedFields = getXMLBool(cpFile, key .. '#onlyScanOwnedFields');
local fieldsDebugScan = getXMLBool(cpFile, key .. '#debugScannedFields');
local fieldsDebugCustomLoad = getXMLBool(cpFile, key .. '#debugCustomLoadedFields');
local fieldsCustomScanStep = getXMLInt(cpFile, key .. '#scanStep');
if fieldsAutomaticScan ~= nil then
courseplay.fields.automaticScan = fieldsAutomaticScan;
else
setXMLBool(cpFile, key .. '#automaticScan', courseplay.fields.automaticScan);
self.showFieldScanYesNoDialogue = true;
end;
if fieldsOnlyScanOwnedFields ~= nil then
courseplay.fields.onlyScanOwnedFields = fieldsOnlyScanOwnedFields;
else
setXMLBool(cpFile, key .. '#onlyScanOwnedFields', courseplay.fields.onlyScanOwnedFields);
end;
if fieldsDebugScan ~= nil then
courseplay.fields.debugScannedFields = fieldsDebugScan;
else
setXMLBool(cpFile, key .. '#debugScannedFields', courseplay.fields.debugScannedFields);
end;
if fieldsDebugCustomLoad ~= nil then
courseplay.fields.debugCustomLoadedFields = fieldsDebugCustomLoad;
else
setXMLBool(cpFile, key .. '#debugCustomLoadedFields', courseplay.fields.debugCustomLoadedFields);
end;
if fieldsCustomScanStep ~= nil then
courseplay.fields.scanStep = fieldsCustomScanStep;
else
setXMLInt(cpFile, key .. '#scanStep', courseplay.fields.scanStep);
end;
-- wages
key = 'XML.courseplayWages';
local wagesActive, wagePerHour = getXMLBool(cpFile, key .. '#active'), getXMLInt(cpFile, key .. '#wagePerHour');
if wagesActive ~= nil then
self.wagesActive = wagesActive;
else
setXMLBool(cpFile, key .. '#active', self.wagesActive);
self.showWagesYesNoDialogue = true;
end;
if wagePerHour ~= nil then
self.wagePerHour = wagePerHour;
else
setXMLInt(cpFile, key .. '#wagePerHour', self.wagePerHour);
self.showWagesYesNoDialogue = true;
end;
self.wagePer10Secs = self.wagePerHour / 360;
-- ingame map
key = 'XML.courseplayIngameMap';
local active, showName, showCourse = getXMLBool(cpFile, key .. '#active'), getXMLBool(cpFile, key .. '#showName'), getXMLBool(cpFile, key .. '#showCourse');
if active ~= nil then
self.ingameMapIconActive = active;
else
setXMLBool(cpFile, key .. '#active', self.ingameMapIconActive);
end;
if showName ~= nil then
self.ingameMapIconShowName = showName;
else
setXMLBool(cpFile, key .. '#showName', self.ingameMapIconShowName);
end;
if showCourse ~= nil then
self.ingameMapIconShowCourse = showCourse;
else
setXMLBool(cpFile, key .. '#showCourse', self.ingameMapIconShowCourse);
end;
self.ingameMapIconShowText = self.ingameMapIconShowName or self.ingameMapIconShowCourse;
-- batch write size (used in deleteSaveAll())
key = 'XML.courseManagement';
local batchWriteSize = getXMLInt(cpFile, key .. '#batchWriteSize');
if batchWriteSize ~= nil then
courseplay.courses.batchWriteSize = batchWriteSize;
else
setXMLInt(cpFile, key .. '#batchWriteSize', courseplay.courses.batchWriteSize);
end;
--------------------------------------------------
saveXMLFile(cpFile);
delete(cpFile);
end;
end;
-- CREATE courseplay.xml AND SET DEFAULT SETTINGS
function CpManager:createXmlSettings()
print('## Courseplay: creating "courseplay.xml" with default settings');
local cpFile = createXMLFile('cpFile', self.cpXmlFilePath, 'XML');
-- hud position
local key = 'XML.courseplayHud';
setXMLFloat(cpFile, key .. '#posX', courseplay.hud.basePosX);
setXMLFloat(cpFile, key .. '#posY', courseplay.hud.basePosY);
-- fields settings
self.showFieldScanYesNoDialogue = true;
key = 'XML.courseplayFields';
setXMLBool(cpFile, key .. '#automaticScan', courseplay.fields.automaticScan);
setXMLBool(cpFile, key .. '#onlyScanOwnedFields', courseplay.fields.onlyScanOwnedFields);
setXMLBool(cpFile, key .. '#debugScannedFields', courseplay.fields.debugScannedFields);
setXMLBool(cpFile, key .. '#debugCustomLoadedFields', courseplay.fields.debugCustomLoadedFields);
setXMLInt(cpFile, key .. '#scanStep', courseplay.fields.scanStep);
-- wages
self.showWagesYesNoDialogue = true;
key = 'XML.courseplayWages';
setXMLBool(cpFile, key .. '#active', self.wagesActive);
setXMLInt(cpFile, key .. '#wagePerHour', self.wagePerHour);
-- ingame map
key = 'XML.courseplayIngameMap';
setXMLBool(cpFile, key .. '#active', self.ingameMapIconActive);
setXMLBool(cpFile, key .. '#showName', self.ingameMapIconShowName);
setXMLBool(cpFile, key .. '#showCourse', self.ingameMapIconShowCourse);
-- batch write size (used in deleteSaveAll())
key = 'XML.courseManagement';
setXMLInt(cpFile, key .. '#batchWriteSize', courseplay.courses.batchWriteSize);
--------------------------------------------------
saveXMLFile(cpFile);
delete(cpFile);
end;