forked from StingerTopGun/ContractHUD
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContractHUD.lua
591 lines (519 loc) · 27.1 KB
/
ContractHUD.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
-- Contract HUD for FS22
--
-- This mod displays completion of active contracts for a player farm.
-- If 0 active contracts then hud is hidden.
--
-- Author: StingerTopGun, the.geremy
ContractHUD = {}
ContractHUD.eventName = {}
ContractHUD.ModName = g_currentModName
ContractHUD.ModDirectory = g_currentModDirectory
ContractHUD.Version = "1.2.1.0"
ContractHUD.Colors = {}
ContractHUD.Colors[1] = {'col_white', {1, 1, 1, 1}}
ContractHUD.Colors[2] = {'col_black', {0, 0, 0, 0.7}}
ContractHUD.Colors[3] = {'col_grey', {0.7411, 0.7450, 0.7411, 1}}
ContractHUD.Colors[4] = {'col_blue', {0.0044, 0.15, 0.6376, 1}}
ContractHUD.Colors[5] = {'col_red', {0.7835, 0.0034, 0.0030, 1}}
ContractHUD.Colors[6] = {'col_green', {0.0395, 0.2015, 0.0242, 1}}
ContractHUD.Colors[7] = {'col_cyan', {0.0003, 0.5647, 0.9822, 1}}
ContractHUD.Colors[8] = {'col_orange', {1.0000, 0.1413, 0.0000, 1}}
ContractHUD.Colors[9] = {'col_dgreen', {0.1062, 0.5234, 0.1450, 1}}
ContractHUD.Colors[10] = {'col_sorange', {1.0000, 0.1830, 0.0210, 1}}
ContractHUD.Colors[11] = {'col_green2', {0.5059, 0.7647, 0.1961, 1}}
ContractHUD.Colors[12] = {'col_green3', {0.391, 0.521, 0.366, 1}}
ContractHUD.Colors[13] = {'col_cyan2', {0.0, 0.7764, 0.9921, 1}}
ContractHUD.Colors[14] = {'alert_color', {0.6662, 0.3839, 0.5481, 1}}
ContractHUD.Colors[15] = {'under_minute_color', {0.9411, 0.1960, 0.2456, 1}}
ContractHUD.background = 'dataS/menu/hud/ui_elements.png'
ContractHUD.overlay = nil
ContractHUD.overlayIsRendered = false
ContractHUD.maxTextWidth = 0
ContractHUD.displayModeChanged = false
ContractHUD.activeMissons = 0
ContractHUD.baseXchanged = false
ContractHUD.baseX = 0
-- settings secton --
ContractHUD.HeadlineColor = 7 -- put here color index from above
ContractHUD.MissionTextColor = 1 -- default active mission color, put here color index from above
ContractHUD.MissionAlertColor = 14
ContractHUD.MissionUnderMinuteColor = 15
ContractHUD.FieldDisplay = false -- false => Field, true => Field No.
ContractHUD.TransportDisplay = false -- false => Supply, true => Transporting
ContractHUD.displayDecimals = true -- display or not display decimal in percentage => 8 % or 8.2 %
ContractHUD.shortTime = true -- true => 15:40, false => 15:40:26
ContractHUD.alertTime = 60 -- alert time in minutes, when remianing time less than specified minutes, change color of contract to alert_color
-- !!! To change these, may have unexpected effects !!!
ContractHUD.defaultOverlayWidth = 0.06
ContractHUD.horizontalMargin = 0.0032
ContractHUD.verticalMargin = 0.0008
-- ContractHUD.ColorSuccess = 9 -- not used at the moment
-- ContractHUD.ColorFail = 5 -- not used at the moment
-- end of settings section --
-- displayMode
-- 0 - field mission - display field number and field work type and also crop type if available, if progress display bar (default)
-- - transport mission - display mission type and crop type, if progress display bar instead of remaining time (default)
-- 1 - field mission - display field number and field work type and also crop type if available, if progress display bar
-- - transport mission - display mission type and crop type, if progress display % number instead of required amount
-- 2 - field mission - display field number and field work type and also crop type if available, if progress display % number and bar
-- - transport mission - display mission type and crop type, if progress display % number and bar instead of required amount
-- 3 - field mission - display field number and field work type and also crop type if available, if progress display % number and bar
-- - transport mission - display crop type, if progress display % number instead of required amount also display destination
-- 4 - field mission - display field number and field work type and also crop type if available, if progress display % number and bar
-- - transport mission - display crop type, if progress display % number instead of required amount also display destination and remianing time
-- 5 - field mission - display field number and field work type and also crop type if available, if progress display % number and bar
-- - transport mission - display crop type, if progress display remianing amount instead of required amount also display destination and remianing time
-- 6 = hide HUD
ContractHUD.displayMode = 5 -- default display mode
function ContractHUD:registerActionEvents()
g_inputBinding:registerActionEvent('CH_toggle_displaymode', self, ContractHUD.toggleDisplayMode, false, true, false, true)
end
--function ContractHUD:onLoad(savegame)
--end
function ContractHUD:update(dt)
-- if zero active missions, check for new active missions (the.geremy)
if ContractHUD.activeMissons == 0 then
local countContracts = 0
for _, contract in ipairs(g_missionManager.missions) do
if contract.status >= 1 and g_currentMission.player.farmId == contract.farmId then -- only show active contracts, 0 is inactive i guess, and only from current farm
countContracts = countContracts + 1
end
end
ContractHUD.activeMissons = countContracts
end
end
function ContractHUD:draw()
-- added condition to display info only if any active mission (the.geremy)
if g_client ~= nil and g_currentMission.hud.isVisible and ContractHUD.displayMode ~= 6 and ContractHUD.activeMissons ~= 0 and not g_noHudModeEnabled and not g_sleepManager:getIsSleeping() then
local posX = g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.x
local posY = g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.y
local size = g_currentMission.inGameMenu.hud.inputHelp.helpTextSize * 1.1 -- add 10%
posY = posY + g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.height - size
posX = posX - ( g_currentMission.inGameMenu.hud.inputHelp.helpTextOffsetY * 2 )
local outputText = ContractHUD:translate("headline") .. ": " .. ContractHUD.activeMissons
local completion = 0
local countContracts = 0
local field_text = ""
local field_work = ""
local fruitTypeName = ""
-- for boxOverlay
local baseX = posX
local baseY = g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.y + g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.height
local maxTextWidth = 0.0
if ContractHUD.baseX ~= baseX then
ContractHUD.baseXchanged = true
ContractHUD.baseX = baseX
end
-- print haadline
ContractHUD:renderText(posX, posY, size, outputText, false, ContractHUD.HeadlineColor)--FSBaseMission.INGAME_NOTIFICATION_INFO) --ContractHUD.HeadlineColor)
maxTextWidth = getTextWidth(size, outputText)
posY = posY - size -- shift one line down because of headline
for _, contract in ipairs(g_missionManager.missions) do
if contract.status >= 1 and g_currentMission.player.farmId == contract.farmId then -- only show active contracts, 0 is inactive i guess, and only from current farm
-- handle color and completion
if contract.status == 2 and contract.success == true then -- contract finished, set completion to 100% and text green
completion = 1
textColor = FSBaseMission.INGAME_NOTIFICATION_OK --ContractHUD.ColorSuccess -- green
elseif contract.status == 2 and contract.success == false then -- contract failed
completion = contract.completion
textColor = FSBaseMission.INGAME_NOTIFICATION_CRITICAL --ContractHUD.ColorFail -- red
else -- contract active, get current completion and set text color
completion = contract.completion
if contract.type.name == "supplyTransport" then
if ContractHUD:getRemainingTime(contract, false) == 0 then -- when less than minute it will return 0
textColor = ContractHUD.MissionUnderMinuteColor
elseif ContractHUD:getRemainingTime(contract, false) <= ContractHUD.alertTime then
textColor = ContractHUD.MissionAlertColor
else
textColor = ContractHUD.MissionTextColor
end
else
textColor = ContractHUD.MissionTextColor
end
end
-- handle field_text also for supplyTransport contract
if contract.type.name == "supplyTransport" then -- check if supplyTransport contract
field_text = ContractHUD:translate("transport") --g_i18n:getText("CH_supply")
else
field_text = ContractHUD:translate("field_no") .. " " .. contract.field.fieldId
end
-- add info about field work or fill type if supplyTransport contract
if contract.type.name == "supplyTransport" then -- check if supplyTransport contract
field_work = ContractHUD:getFillTypeTitle(contract.fillType) -- fillType has only index, we need to change it to field type name, already translated
-- contracted liters contract.contractLiters
elseif contract.type.name ~= nil then
field_work = ContractHUD:firstToUpper(ContractHUD:translate(contract.type.name)) -- make first letter Upper case and translate, other type missions like sow, fertilize, harvest....
else
field_work = "N/A"
end
if ContractHUD.displayMode == 0 then
if contract.type.name == "supplyTransport" then
if completion == 0 then -- when 0%, display remianing time
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getRemainingTime(contract)
else -- else progress bar
outputText = field_text .. " - " .. field_work .. " " .. ContractHUD:buildProgressBar(completion)
end
else -- other then supplyTransport contracts
if completion == 0 then -- when 0%, display fuit type title
if contract.type.name == "sow" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFruitTypeName(contract.fruitType)
elseif contract.type.name == "harvest" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFillTypeTitle(contract.fillType)
else
outputText = field_text .. " - " .. field_work
end
else -- else progress bar
outputText = field_text .. " " .. ContractHUD:buildProgressBar(completion)
end
end
elseif ContractHUD.displayMode == 1 then
if contract.type.name == "supplyTransport" then
if completion == 0 then -- when 0%, display contracted liters
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:formatNumber(contract.contractLiters, true, false)
else -- else display percentage
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:formatNumber(completion, false, true)
end
else -- other then supplyTransport contracts
if completion == 0 then -- when 0%, display fruit type title
if contract.type.name == "sow" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFruitTypeName(contract.fruitType)
elseif contract.type.name == "harvest" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFillTypeTitle(contract.fillType)
else
outputText = field_text .. " - " .. field_work
end
else -- else display percentage
outputText = field_text .. " - " .. ContractHUD:formatNumber(completion, false, true)
end
end
elseif ContractHUD.displayMode == 2 then
if contract.type.name == "supplyTransport" then
if completion == 0 then -- when 0%, display contracted liters
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:formatNumber(contract.contractLiters, true, false)
else -- else display percentage and bar
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:formatNumber(completion, false, true) .. " " .. ContractHUD:buildProgressBar(completion)
end
else -- other then supplyTransport contracts
if completion == 0 then -- when 0%, display fruit type title
if contract.type.name == "sow" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFruitTypeName(contract.fruitType)
elseif contract.type.name == "harvest" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFillTypeTitle(contract.fillType)
else
outputText = field_text .. " - " .. field_work
end
else -- else display percentage and bar
outputText = field_text .. " - " .. ContractHUD:formatNumber(completion, false, true) .. " " .. ContractHUD:buildProgressBar(completion)
end
end
elseif ContractHUD.displayMode == 3 then
if contract.type.name == "supplyTransport" then
if completion == 0 then -- when 0%, display required amount
outputText = field_work .. " (" .. ContractHUD:formatNumber(contract.contractLiters, true, false) .. ") => " .. contract.sellingStation.uiName
else -- else display percentage and destination
outputText = field_work .. " (" .. ContractHUD:formatNumber(completion, false, true) .. ") => " .. contract.sellingStation.uiName
end
else -- other then supplyTransport contracts
if completion == 0 then -- when 0%, display fruit type title
if contract.type.name == "sow" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFruitTypeName(contract.fruitType)
elseif contract.type.name == "harvest" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFillTypeTitle(contract.fillType)
else
outputText = field_text .. " - " .. field_work
end
else -- else display percentage and bar
outputText = field_text .. " - " .. ContractHUD:formatNumber(completion, false, true) .. " " .. ContractHUD:buildProgressBar(completion)
end
end
elseif ContractHUD.displayMode == 4 then --contract.deliveredLiters
if contract.type.name == "supplyTransport" then
if completion == 0 then -- when 0%, display required amount
outputText = field_work .. " (" .. ContractHUD:formatNumber(contract.contractLiters, true, false) .. ") => " .. contract.sellingStation.uiName .. " (" .. ContractHUD:getRemainingTime(contract) .. ")"
else -- else display percentage and destination and remianing time
outputText = field_work .. " (" .. ContractHUD:formatNumber(completion, false, true) .. ") => " .. contract.sellingStation.uiName .. " (" .. ContractHUD:getRemainingTime(contract) .. ")"
end
else -- other then supplyTransport contracts
if completion == 0 then -- when 0%, display fruit type title
if contract.type.name == "sow" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFruitTypeName(contract.fruitType)
elseif contract.type.name == "harvest" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFillTypeTitle(contract.fillType)
else
outputText = field_text .. " - " .. field_work
end
else -- else display percentage and bar
outputText = field_text .. " - " .. ContractHUD:formatNumber(completion, false, true) .. " " .. ContractHUD:buildProgressBar(completion)
end
end
elseif ContractHUD.displayMode == 5 then
if contract.type.name == "supplyTransport" then
if contract.status == 2 and contract.success == true then
outputText = field_text .. " (" .. field_work .. ") => " .. contract.sellingStation.uiName .. " (" .. g_currentMission.hud.l10n.texts.fieldJob_finished .. ")"
elseif contract.status == 2 and contract.success == false then
outputText = field_text .. " (" .. field_work .. ") => " .. contract.sellingStation.uiName .. " (" .. g_currentMission.hud.l10n.texts.fieldJob_failed .. ")"
elseif completion == 0 then -- when 0%, display required amount
outputText = field_work .. " (" .. ContractHUD:formatNumber(contract.contractLiters, true, false) .. ") => " .. contract.sellingStation.uiName .. " (" .. ContractHUD:getRemainingTime(contract) .. ")"
else -- else display remianing amount, destination and remianing time
outputText = field_work .. " (" .. ContractHUD:formatNumber((contract.contractLiters - contract.deliveredLiters), true, false) .. ") => " .. contract.sellingStation.uiName .. " (" .. ContractHUD:getRemainingTime(contract) .. ")"
end
else -- other then supplyTransport contracts
if contract.status == 2 and contract.success == true then
outputText = field_text .. " - " .. field_work .. " (" .. g_currentMission.hud.l10n.texts.fieldJob_finished .. ")"
elseif contract.status == 2 and contract.success == false then
outputText = field_text .. " - " .. field_work .. " (" .. g_currentMission.hud.l10n.texts.fieldJob_failed .. ")"
elseif completion == 0 then -- when 0%, display fruit type title
if contract.type.name == "sow" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFruitTypeName(contract.fruitType)
elseif contract.type.name == "harvest" then
outputText = field_text .. " - " .. field_work .. " - " .. ContractHUD:getFillTypeTitle(contract.fillType)
else
outputText = field_text .. " - " .. field_work
end
else -- else display percentage and bar
outputText = field_text .. " - " .. ContractHUD:formatNumber(completion, false, true) .. " " .. ContractHUD:buildProgressBar(completion)
end
end
end
ContractHUD:renderText(posX, posY, size, outputText, false, textColor)
posY = posY - size -- shift one line down
-- handle counting
countContracts = countContracts + 1
-- get max width of the text
if getTextWidth(size, outputText) > maxTextWidth then
maxTextWidth = getTextWidth(size, outputText)
end
end
end
local overlayX = 0
local overlayY = 0
local overlayWidth = 0
local overlayHeight = 0
-- draw overlay if not exists
if not ContractHUD.overlayIsRendered then
overlayWidth = maxTextWidth + ContractHUD.horizontalMargin*2
if overlayWidth < ContractHUD.defaultOverlayWidth then
overlayWidth = ContractHUD.defaultOverlayWidth
end
overlayHeight = size * (countContracts + 1) + ContractHUD.verticalMargin + g_currentMission.inGameMenu.hud.inputHelp.helpTextOffsetY
if overlayHeight < g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.height then
overlayHeight = g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.height
end
overlayX = baseX - overlayWidth + ContractHUD.horizontalMargin
overlayY = baseY - overlayHeight
ContractHUD.overlay = ContractHUD.createImageOverlay(ContractHUD.background, overlayX, overlayY, overlayWidth, overlayHeight)
ContractHUD.overlay:setUVs(g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.uvs)
ContractHUD.overlay:setColor(unpack(self.Colors[2][2]))
g_currentMission.hud.gameInfoDisplay:addChild(ContractHUD.overlay)
ContractHUD.overlayIsRendered = true
elseif math.abs(ContractHUD.maxTextWidth - maxTextWidth) > ContractHUD.horizontalMargin/2 -- when maxTextWidth change more than half of the horizontal margin
or ContractHUD.activeMissons ~= countContracts -- when active contract count changed
or ContractHUD.displayModeChanged -- when display mode changed
or ContractHUD.baseXchanged -- when weather is about to change
then -- move or adjust size of overlay
overlayWidth = maxTextWidth + ContractHUD.horizontalMargin*2
if overlayWidth < ContractHUD.defaultOverlayWidth then
overlayWidth = ContractHUD.defaultOverlayWidth
end
overlayHeight = size * (countContracts + 1) + ContractHUD.verticalMargin + g_currentMission.inGameMenu.hud.inputHelp.helpTextOffsetY
if overlayHeight < g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.height then
overlayHeight = g_currentMission.hud.gameInfoDisplay.backgroundOverlay.overlay.height
end
overlayX = baseX - overlayWidth + ContractHUD.horizontalMargin
overlayY = baseY - overlayHeight
ContractHUD.overlay:setPosition(overlayX, overlayY)
ContractHUD.overlay:setDimension(overlayWidth, overlayHeight)
ContractHUD.displayModeChanged = false
ContractHUD.baseXchanged = false
end
ContractHUD.activeMissons = countContracts
ContractHUD.maxTextWidth = maxTextWidth
else
if ContractHUD.overlayIsRendered then
ContractHUD.overlay:delete()
ContractHUD.overlayIsRendered = false
end
end
end
function ContractHUD.createImageOverlay(texturePath, baseX, baseY, baseWidth, baseHeight)
return HUDElement.new(Overlay.new(texturePath, baseX, baseY, baseWidth, baseHeight))
end
function ContractHUD:translate(text)
local result = ""
if text == "headline" then
result = ContractHUD:firstToUpper(g_currentMission.hud.l10n.texts.fieldJob_active .. " " .. g_currentMission.hud.l10n.texts.ui_ingameMenuContracts)
--result = g_currentMission.hud.l10n.texts.ui_pendingMissions
elseif text == "field_no" then
result = ContractHUD.FieldDisplay and g_currentMission.hud.l10n.texts.ui_fieldNo or g_i18n:getText("CH_field")
elseif text == "mow_bale" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_baling
elseif text == "plow" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_plowing
elseif text == "cultivate" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_cultivating
elseif text == "sow" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_sowing
elseif text == "harvest" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_harvesting
elseif text == "weed" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_weeding
elseif text == "spray" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_spraying
elseif text == "fertilize" then
result = g_currentMission.hud.l10n.texts.fieldJob_jobType_fertilizing
elseif text == "transport" then
result = ContractHUD.TransportDisplay and g_currentMission.hud.l10n.texts.fieldJob_jobType_transporting or g_i18n:getText("CH_supply")
else
result = "N/A"
end
return result
end
function ContractHUD:formatNumber(number, is_whole_number, is_percentage)
local decimal_separator = g_currentMission.hud.l10n.decimalSeparator or "."
local thousands_grouping = g_currentMission.hud.l10n.thousandsGroupingChar or " "
if is_whole_number then
local s = string.format("%d", math.floor(number))
local pos = string.len(s) % 3
if pos == 0 then pos = 3 end
return string.sub(s, 1, pos) .. string.gsub(string.sub(s, pos+1), "(...)", "" .. thousands_grouping .. "%1") .. " " .. (g_currentMission.hud.l10n.unit_literShort or "l")
elseif is_percentage then
if number == 1 then
return "100 %"
else
number = ContractHUD.displayDecimals and math.floor(number * 1000) / 10 or math.floor(number * 100)
local per, n = string.gsub(tostring(number),"[\\.]",decimal_separator)
if not string.find(number, "[\\.]") and ContractHUD.displayDecimals then
per = tostring(per) .. ".0 %"
else
per = tostring(per) .. " %"
end
return per
end
else
return number
end
end
function ContractHUD:buildProgressBar(completion)
-- constract text progress bar like this: [||||||||¦:::::]
local maxPositions = 20
local currentLevel5 = math.floor((completion / 0.05))
local modulo5 = (completion % 0.05)
local currentLevel25 = math.floor((modulo5 / 0.025))
local remain = maxPositions - currentLevel5 - currentLevel25
local barText = "["
-- add progress by 5 %
for i = 1, currentLevel5 do
barText = barText .. "|"
end
-- add progress 2.5%
for i = 1, currentLevel25 do
barText = barText .. "¦"
end
-- add empty
for i = 1, remain do
barText = barText .. ":"
end
barText = barText .. "]"
return barText
end
function ContractHUD:getFruitTypeName(fruitTypeIndex)
if fruitTypeIndex ~= nil and type(fruitTypeIndex) == "number" then
local fruitTypeTable = g_fruitTypeManager:getFruitTypeByIndex(fruitTypeIndex)
return fruitTypeTable ~= nil and ContractHUD:firstToUpper(fruitTypeTable.name) or ""
end
return "NULL"
end
-- this function is for debugging purpose
function ContractHUD:printValue(name, value)
-- print to log.txt
print("ContractHUD Info: " .. name .. " >> " .. value)
end
-- this function is for debugging purpose
function ContractHUD:getTableKeys(tableObject)
if tableObject ~= nil and type(tableObject) == "table" then
local keyset={}
local n=0
for k,v in pairs(tableObject) do
n=n+1
keyset[n]=k
end
-- print to log.txt
print(table.concat(keyset,","))
--returns concatenated string of table keys
return table.concat(keyset,",")
end
if tableObject == nil then
return "NULL2"
elseif type(tableObject) == "string" then
return tableObject
else
return type(tableObject)
end
end
function ContractHUD:getFillTypeTitle(fillTypeIndex)
if fillTypeIndex ~= nil then
local fillTypeDesc = g_fillTypeManager:getFillTypeByIndex(fillTypeIndex)
return fillTypeDesc ~= nil and ContractHUD:firstToUpper(fillTypeDesc.title) or ""
end
return nil
end
function ContractHUD:firstToUpper(str)
return (string.lower(str):gsub("^%l", string.upper))
end
function ContractHUD:getRemainingTime(contract, formated)
if formated == false then
formated = false
else
formated = true
end
local environment = g_currentMission.environment
local usedDaysToMinutes = (environment.currentDay - contract.contractDay) * 24 * 60
local usedMinutes = MathUtil.msToMinutes(environment.dayTime - contract.contractTime)
local remainingMinutes = 0
if contract.status == 2 then
remainingMinutes = 0
else
remainingMinutes = contract.contractDuration - (usedDaysToMinutes + usedMinutes)
end
if formated then
local hours = math.floor(remainingMinutes / 60)
local minutes = remainingMinutes - hours * 60
local seconds = (remainingMinutes - math.floor(remainingMinutes)) * 60
if remainingMinutes == 0 then
return ContractHUD.shortTime and string.format("%02d:%02d", 0, 0) or string.format("%02d:%02d:%02d", 0, 0, 0)
elseif math.floor(hours) == 0 and math.floor(minutes) == 0 then
return string.format("%02d", seconds)
else
return ContractHUD.shortTime and string.format("%02d:%02d", hours, minutes) or string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
else
return math.floor(remainingMinutes)
end
end
function ContractHUD:renderText(x, y, size, text, bold, textColor) --colorId )
if type(textColor) == 'table' then
setTextColor(unpack(textColor))
elseif textColor % 1 == 0 then
setTextColor(unpack(self.Colors[textColor][2]))
else
setTextColor(unpack(self.Colors[1][2])) -- set default color which is white
end
setTextBold(bold)
setTextAlignment(RenderText.ALIGN_RIGHT)
renderText(x, y, size, text)
-- Back to defaults
setTextBold(false)
setTextColor(unpack(self.Colors[1][2])) --Back to default color which is white
setTextAlignment(RenderText.ALIGN_LEFT)
end
function ContractHUD.toggleDisplayMode()
-- keep it simple
if ContractHUD.displayMode >= 6 then -- 6 = hidden, highest value
ContractHUD.displayMode = 0
else
ContractHUD.displayMode = ContractHUD.displayMode + 1
end
ContractHUD.displayModeChanged = true
end
addModEventListener(ContractHUD)
FSBaseMission.registerActionEvents = Utils.appendedFunction(FSBaseMission.registerActionEvents, ContractHUD.registerActionEvents);