-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgui.lua
788 lines (672 loc) · 37.3 KB
/
gui.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
local Consumption = require("consumption")
local Constants = require("constants")
local Util = require("util")
local GuiState = require("gui-state")
local function setConstructionMaterialsProvided(city, resource, amount)
-- dev support, delete me
if city.stats.construction_materials == nil then
city.stats.construction_materials = {}
end
if city.stats.construction_materials[resource] == nil then
city.stats.construction_materials[resource] = {
provided = 0,
}
end
city.stats.construction_materials[resource].provided = math.floor(amount)
end
local function getBasicNeeds(city, tier)
if tier == "simple" then
return {
water = city.stats.basic_needs.water,
["tycoon-apple"] = city.stats.basic_needs["tycoon-apple"],
}
elseif tier == "residential" then
return {
water = city.stats.basic_needs.water,
["tycoon-milk-bottle"] = city.stats.basic_needs["tycoon-milk-bottle"],
["tycoon-meat"] = city.stats.basic_needs["tycoon-meat"],
["tycoon-bread"] = city.stats.basic_needs["tycoon-bread"],
["tycoon-fish-filet"] = city.stats.basic_needs["tycoon-fish-filet"],
}
elseif tier == "highrise" then
return {
water = city.stats.basic_needs.water,
["tycoon-smoothie"] = city.stats.basic_needs["tycoon-smoothie"],
["tycoon-apple-cake"] = city.stats.basic_needs["tycoon-apple-cake"],
["tycoon-cheese"] = city.stats.basic_needs["tycoon-cheese"],
["tycoon-burger"] = city.stats.basic_needs["tycoon-burger"],
["tycoon-dumpling"] = city.stats.basic_needs["tycoon-dumpling"],
}
else
assert(false, "Unknown tier for getBasicNeeds: " .. tier)
end
end
local function getAdditionalNeeds(city, tier)
if city.stats.additional_needs == nil then
-- edge case for when this has not been initialized (e.g. when upgrading the savegame to the latest version of this mod)
return {}
end
if tier == "simple" then
return {
["tycoon-cooking-pot"] = city.stats.additional_needs["tycoon-cooking-pot"],
["tycoon-cooking-pan"] = city.stats.additional_needs["tycoon-cooking-pan"],
["tycoon-cutlery"] = city.stats.additional_needs["tycoon-cutlery"],
}
elseif tier == "residential" then
return {
["tycoon-bicycle"] = city.stats.additional_needs["tycoon-bicycle"],
["tycoon-candle"] = city.stats.additional_needs["tycoon-candle"],
["tycoon-soap"] = city.stats.additional_needs["tycoon-soap"],
["tycoon-gloves"] = city.stats.additional_needs["tycoon-gloves"],
["tycoon-television"] = city.stats.additional_needs["tycoon-television"],
}
elseif tier == "highrise" then
return {
["tycoon-smartphone"] = city.stats.additional_needs["tycoon-smartphone"],
["tycoon-laptop"] = city.stats.additional_needs["tycoon-laptop"],
}
else
assert(false, "Unknown tier for getAdditionalNeeds: " .. tier)
end
end
local function getItemPrice(itemName)
local value = Consumption.resourcePrices[itemName]
if value == nil then
return "?"
else
return string.format("%.1f", value)
end
end
local house_ratios = {
residential = Constants.RESIDENTIAL_HOUSE_RATIO,
highrise = Constants.HIGHRISE_HOUSE_RATIO,
}
--- @param rootGui any
--- @param constructionNeeds any[]
--- @param city City
--- @param hardwareStores any[]
local function addConstructionMaterialsGui(rootGui, constructionNeeds, city, hardwareStores, housingType)
local constructionGui = rootGui.add{type = "frame", direction = "vertical", caption = {"", {"tycoon-gui-construction"}}}
local tbl = constructionGui.add{type = "table", column_count = 4, draw_horizontal_lines = true}
if #hardwareStores == 0 then
tbl.add{type = "label", caption = {"", "[color=red]", {"tycoon-gui-missing", {"entity-name.tycoon-hardware-store"}}, "[/color]"}}
else
local supply = Util.aggregateSupplyBuildingResources(hardwareStores)
for resource, required_amount in pairs(constructionNeeds) do
setConstructionMaterialsProvided(city, resource, supply[resource] or 0)
local amounts = city.stats.construction_materials[resource] or {provided = 0}
local itemName = "item-name." .. resource
local fallbackName = "entity-name." .. resource
local color = "green"
if amounts.provided < required_amount then
color = "red"
end
local c1 = tbl.add{type = "label", caption = "[item=" .. resource .. "]"}
c1.style.padding = 5
c1.style.minimal_width = 100
local c2 = tbl.add{type = "label", caption = {"", {"?", {itemName}, {fallbackName}}}}
c2.style.padding = 5
c2.style.minimal_width = 100
local c3 = tbl.add{type = "label", caption = {"", "[color=" .. color .. "]", amounts.provided, "[/color]"}}
c3.style.padding = 5
c3.style.minimal_width = 100
local c4 = tbl.add{type = "label", caption = "[item=tycoon-currency] " .. getItemPrice(resource)}
c4.style.padding = 5
c4.style.minimal_width = 100
end
end
constructionGui.add{type = "line"}
constructionGui.add{type = "label", caption = {"", {"tycoon-gui-construction-requirement-1"}}}
constructionGui.add{type = "label", caption = {"", {"tycoon-gui-construction-requirement-2"}}}
constructionGui.add{type = "line"}
local lowerTierMap = {
residential = "simple",
highrise = "residential",
}
if housingType ~= "simple" then
local lowerTierCount = ((city.buildingCounts or {})[lowerTierMap[housingType]] or 0)
local higherTierCount = ((city.buildingCounts or {})[housingType] or 0)
local numberOfLowerTierHousesNeeded = Util.countPendingLowerTierHouses(lowerTierCount, higherTierCount, house_ratios[housingType])
if numberOfLowerTierHousesNeeded > 0 then
constructionGui.add{type = "label", caption = {"", "[color=red]", {"tycoon-gui-grow-other-housing-tier", {"", {"technology-name.tycoon-" .. housingType .. "-housing"}}, numberOfLowerTierHousesNeeded, {"", {"technology-name.tycoon-" .. lowerTierMap[housingType] .. "-housing"}}}, "[/color]"}}
end
end
end
--- @param rootGui any
--- @param basicNeeds string[]
--- @param city City
--- @param waterTowers any[]
--- @param markets any[]
--- @param housingTier string
local function addBasicNeedsView(rootGui, basicNeeds, city, waterTowers, markets, housingTier)
local basicNeedsGui = rootGui.add{type = "frame", direction = "vertical", caption = {"", {"tycoon-gui-basic-needs"}}, name = "basic_needs"}
basicNeedsGui.add{type = "label", caption = {"", {"tycoon-gui-consumption-cycle-1"}}}
basicNeedsGui.add{type = "label", caption = {"", {"tycoon-gui-consumption-cycle-2"}}}
basicNeedsGui.add{type = "label", caption = {"", {"tycoon-gui-consumption-cycle-3"}}}
basicNeedsGui.add{type = "line"}
local displayedMissingSuppliers = {}
local tbl = basicNeedsGui.add{type = "table", column_count = 4, draw_horizontal_lines = true}
for _, resource in ipairs(basicNeeds) do
local missingSupplier = nil
if resource == "water" then
if #waterTowers == 0 then
missingSupplier = "tycoon-water-tower"
end
else
if #markets == 0 then
missingSupplier = "tycoon-market"
end
end
if missingSupplier ~= nil then
if displayedMissingSuppliers[missingSupplier] ~= true then
tbl.add{type = "label", caption = {"", "[color=red]", {"tycoon-gui-missing", {"entity-name." .. missingSupplier}}, "[/color]"}}
tbl.add{type = "label", caption = ""}
tbl.add{type = "label", caption = ""}
tbl.add{type = "label", caption = ""}
displayedMissingSuppliers[missingSupplier] = true
end
else
local amounts = city.stats.basic_needs[resource]
local itemName = resource
if string.find(resource, "tycoon-", 1, true) then
itemName = "item-name." .. itemName
elseif resource == "water" then
-- Vanilla items like water are not in our localization config, and therefore have to be accessed differently
itemName = "fluid-name." .. resource
end
local color = "green"
if amounts.provided > 0 and amounts.provided < amounts.required then
color = "orange"
elseif amounts.provided == 0 then
color = "red"
end
local imgName
if resource == "water" or resource == "tycoon-milk" then
imgName = "fluid=" .. resource
else
-- Vanilla items like water are not in our localization config, and therefore have to be accessed differently
imgName = "item=" .. resource
end
local c1 = tbl.add{type = "label", caption = "[" .. imgName .. "]"}
c1.style.padding = 5
c1.style.minimal_width = 100
local c2 = tbl.add{type = "label", caption = {"", {itemName}}}
c2.style.padding = 5
c2.style.minimal_width = 100
local captionElements = {"", "[color=" .. color .. "]", amounts.provided, "/", amounts.required, "[/color]"}
if resource == "water" and (amounts.provided == 0 or (amounts.required / amounts.provided) > 0.75) then
table.insert(captionElements, " ")
table.insert(captionElements, {"tycoon-gui-add-more-water-towers"})
end
local c3 = tbl.add{type = "label", caption = captionElements}
c3.style.padding = 5
c3.style.minimal_width = 100
if resource ~= "water" then
local c4 = tbl.add{type = "label", caption = "[item=tycoon-currency] " .. getItemPrice(resource)}
c4.style.padding = 5
c4.style.minimal_width = 100
else
local c5 = tbl.add{type = "label", caption = ""}
c5.style.padding = 5
c5.style.minimal_width = 100
end
end
end
end
--- @param rootGui any
--- @param additionalNeeds string[]
--- @param city City
--- @param markets any[]
--- @param housingTier string
local function addAdditionalNeedsView(rootGui, additionalNeeds, city, markets, housingTier)
if city.stats.additional_needs == nil then
-- These stats have never been set yet, so there's no way we can display this info.
-- Todo: move this down and explain why we're not showing it yet.
return
end
local additionalNeedsGui = rootGui.add{type = "frame", direction = "vertical", caption = {"", {"tycoon-gui-additional-needs"}}, name = "additional_needs"}
additionalNeedsGui.add{type = "label", caption = {"", {"tycoon-gui-additional-needs-consumption"}}}
additionalNeedsGui.add{type = "line"}
local displayedMissingSuppliers = {}
local tbl = additionalNeedsGui.add{type = "table", column_count = 4, draw_horizontal_lines = true}
for _, resource in ipairs(additionalNeeds) do
local missingSupplier = nil
if #markets == 0 then
missingSupplier = "tycoon-market"
end
if missingSupplier ~= nil then
if displayedMissingSuppliers[missingSupplier] ~= true then
tbl.add{type = "label", caption = {"", "[color=red]", {"tycoon-gui-missing", {"entity-name." .. missingSupplier}}, "[/color]"}}
tbl.add{type = "label", caption = ""}
tbl.add{type = "label", caption = ""}
tbl.add{type = "label", caption = ""}
displayedMissingSuppliers[missingSupplier] = true
end
elseif city.stats.additional_needs[resource] ~= nil then
local amounts = city.stats.additional_needs[resource]
local itemName = resource
if string.find(resource, "tycoon-", 1, true) then
itemName = "item-name." .. itemName
end
local color = "green"
if amounts.provided > 0 and amounts.provided < amounts.required then
color = "orange"
elseif amounts.provided == 0 then
color = "red"
end
local imgName = "item=" .. resource
local c1 = tbl.add{type = "label", caption = "[" .. imgName .. "]"}
c1.style.padding = 5
c1.style.minimal_width = 100
local c2 = tbl.add{type = "label", caption = {"", {itemName}}}
c2.style.padding = 5
c2.style.minimal_width = 100
local captionElements = {"", "[color=" .. color .. "]", amounts.provided, "/", amounts.required, "[/color]"}
local c3 = tbl.add{type = "label", caption = captionElements}
c3.style.padding = 5
c3.style.minimal_width = 100
if resource ~= "water" then
local c4 = tbl.add{type = "label", caption = "[item=tycoon-currency] " .. getItemPrice(resource)}
c4.style.padding = 5
c4.style.minimal_width = 100
else
local c5 = tbl.add{type = "label", caption = ""}
c5.style.padding = 5
c5.style.minimal_width = 100
end
end
end
additionalNeedsGui.add{type = "line"}
end
--- @param city City
--- @param filter string | nil
local function countCitizens(city, filter)
local total = 0
for tier, count in pairs(city.citizens) do
if filter == nil then
total = total + count
elseif filter == tier then
total = total + count
end
end
return total
end
local basicNeeds = {
simple = {"water", "tycoon-apple"},
residential = {"water", "tycoon-milk-bottle", "tycoon-meat", "tycoon-bread", "tycoon-fish-filet"},
highrise = {"water", "tycoon-smoothie", "tycoon-apple-cake", "tycoon-cheese", "tycoon-burger", "tycoon-dumpling" }
}
local additionalNeeds = {
simple = {"tycoon-cooking-pan", "tycoon-cooking-pot", "tycoon-cutlery"},
residential = {"tycoon-bicycle", "tycoon-candle", "tycoon-soap", "tycoon-gloves", "tycoon-television"},
highrise = {"tycoon-smartphone", "tycoon-laptop"}
}
local function getSupplyLevelsSummary(supplyLevels)
local total = 0
for _, value in pairs(supplyLevels) do
total = total + value
end
local average = total / #supplyLevels
if average == 1 then
return "supplied"
elseif average == 0 then
return "missing"
else
return "lacking"
end
end
local function mapSupplyLevelToLocalised(supplyLevel)
if supplyLevel == "supplied" then
return {"", "[color=green]", {"tycoon-gui-status-supplied"}, "[/color]"}
elseif supplyLevel == "missing" then
return {"", "[color=red]", {"tycoon-gui-status-missing"}, "[/color]"}
else
return {"", "[color=orange]", {"tycoon-gui-status-lacking"}, "[/color]"}
end
end
local function areConstructionNeedsMet(city, housingTier, stores)
local hardwareStores = stores or Util.list_special_city_buildings(city, "tycoon-hardware-store")
local supply = Util.aggregateSupplyBuildingResources(hardwareStores)
local needs = Constants.CONSTRUCTION_MATERIALS[housingTier]
for name, required in pairs(needs) do
if (supply[name] or 0) < required then
return false
end
end
return true
end
local function areThereEnoughConstructionMaterials(city, housingTier)
local hardwareStores = Util.list_special_city_buildings(city, "tycoon-hardware-store")
local isMet = areConstructionNeedsMet(city, housingTier, hardwareStores)
return isMet
end
local function getConstructionMaterialsLevelLocalised(city, housingTier)
if areThereEnoughConstructionMaterials(city, housingTier) then
return {"", "[color=green]", {"tycoon-gui-status-supplied"}, "[/color]"}
else
return {"", "[color=red]", {"tycoon-gui-status-missing"}, "[/color]"}
end
end
local lowerTierMap = {
residential = "simple",
highrise = "residential"
}
local function get_colouring(isTrue)
return isTrue and "[color=green]" or "[color=red]"
end
--- @param city City
--- @param housingType string
local function addHousingView(housingType, city, anchor, player_index)
anchor.style.natural_height = 600
if housingType ~= "simple" and not game.forces.player.technologies["tycoon-" .. housingType .. "-housing"].researched then
anchor.add{type = "label", caption = {"", "[color=red]", {"tycoon-gui-not-researched"}, "[/color]"}}
anchor.add{type = "button", caption = "Open Technology", name = "tycoon_open_tech:" .. housingType .. "-housing"}
return
end
local waterTowers = Util.list_special_city_buildings(city, "tycoon-water-tower")
local markets = Util.list_special_city_buildings(city, "tycoon-market")
local hardwareStores = Util.list_special_city_buildings(city, "tycoon-hardware-store")
local tabbed_pane = anchor.add{type="tabbed-pane"}
local tab_overview = tabbed_pane.add{type="tab", caption={"", {"tycoon-gui-overview"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:" .. GuiState.housing_type_to_tab[housingType] .. ":needs_tab:1"}
local tab_basic_needs = tabbed_pane.add{type="tab", caption={"", {"tycoon-gui-basic-needs"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:" .. GuiState.housing_type_to_tab[housingType] .. ":needs_tab:2"}
local tab_additional_needs = tabbed_pane.add{type="tab", caption={"", {"tycoon-gui-additional-needs"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:" .. GuiState.housing_type_to_tab[housingType] .. ":needs_tab:3"}
local tab_construction_material = tabbed_pane.add{type="tab", caption={"", {"tycoon-gui-construction-materials"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:" .. GuiState.housing_type_to_tab[housingType] .. ":needs_tab:4"}
local overview_container = tabbed_pane.add{type = "flow", direction = "vertical"}
tabbed_pane.add_tab(tab_overview, overview_container)
local stats = overview_container.add{type = "frame", direction = "vertical", caption = {"", {"tycoon-gui-stats"}}, name = "city_stats"}
stats.add{type = "label", caption = {"", {"tycoon-gui-citizens"}, ": ", countCitizens(city, housingType)}}
local basicNeedsSupplyLevelsSummary = getSupplyLevelsSummary(Consumption.getSupplyLevels(city, getBasicNeeds(city, housingType)));
local additionalNeedsSupplyLevelsSummary = getSupplyLevelsSummary(Consumption.getSupplyLevels(city, getAdditionalNeeds(city, housingType)));
local tbl = stats.add{type = "table", column_count = 2, draw_horizontal_lines = true}
-- overall basic needs status
tbl.add{type = "label", caption = {"", {"tycoon-gui-basic-needs"}, ": "}}
tbl.add{type = "label", caption = mapSupplyLevelToLocalised(basicNeedsSupplyLevelsSummary)}
-- overall additional needs status
tbl.add{type = "label", caption = {"", {"tycoon-gui-additional-needs"}, ": "}}
tbl.add{type = "label", caption = mapSupplyLevelToLocalised(additionalNeedsSupplyLevelsSummary)}
-- overall construction materials status
tbl.add{type = "label", caption = {"", {"tycoon-gui-construction-materials"}, ": "}}
tbl.add{type = "label", caption = getConstructionMaterialsLevelLocalised(city, housingType)}
local construction_info = overview_container.add{type = "frame", direction = "vertical", caption = {"", {"tycoon-gui-construction-info"}}}
local lowerTierCount = ((city.buildingCounts or {})[lowerTierMap[housingType]] or 0)
local higherTierCount = ((city.buildingCounts or {})[housingType] or 0)
local numberOfLowerTierHousesNeeded
if lowerTierCount == 0 then
numberOfLowerTierHousesNeeded = house_ratios[housingType] or 0
else
numberOfLowerTierHousesNeeded = Util.countPendingLowerTierHouses(lowerTierCount, higherTierCount, house_ratios[housingType])
end
local met_hardware_store = #hardwareStores > 0
local met_construction_material = areThereEnoughConstructionMaterials(city, housingType)
local met_lower_tier_houses = housingType == "simple" or numberOfLowerTierHousesNeeded == 0
local construction_info_table = construction_info.add{type = "table", column_count = 3, draw_horizontal_lines = true}
construction_info_table.add{type = "label", caption = {"", {"item-name.tycoon-hardware-store"}}}
local hardware_store_status = not met_hardware_store and "tycoon-gui-status-missing" or "tycoon-gui-status-built"
construction_info_table.add{type = "label", caption = " "}
construction_info_table.add{type = "label", caption = {"", get_colouring(met_hardware_store), {hardware_store_status},"[/color]"}}
construction_info_table.add{type = "label", caption = {"", {"tycoon-gui-construction-materials"}}}
local construction_material_status = met_construction_material and "tycoon-gui-status-supplied" or "tycoon-gui-status-missing"
construction_info_table.add{type = "label", caption = " "}
construction_info_table.add{type = "label", caption = {"", get_colouring(met_construction_material), {construction_material_status},"[/color]"}}
if housingType ~= "simple" then
construction_info_table.add{type = "label", caption = {"", {"tycoon-gui-lower-tier-houses-requirement"}}}
construction_info_table.add{type = "label", caption = " "}
construction_info_table.add{type = "label", caption = {"", get_colouring(met_lower_tier_houses), numberOfLowerTierHousesNeeded,"[/color]"}}
end
construction_info_table.add{type = "label", caption = {"", {"tycoon-gui-next-construction"}}}
construction_info_table.add{type = "label", caption = " "}
local timer = (city.construction_timers or {})[housingType] or {
last_construction = 0,
construction_interval = math.huge
}
local remaining_seconds = math.max(math.ceil(((timer.last_construction + timer.construction_interval) - game.tick) / 60), 0)
local minutes = math.floor(remaining_seconds / 60)
local seconds = remaining_seconds % 60
if not (met_hardware_store and met_construction_material and met_lower_tier_houses) or minutes > 30 then
construction_info_table.add{type = "label", caption = {"", "[color=red]", {"tycoon-housing-missing-prerequisites"}, "[/color]"}}
elseif minutes > 10 then
construction_info_table.add{type = "label", caption = {"", "[color=orange]", {"tycoon-housing-construction-time-remaining", string.format("%02d", minutes), string.format("%02d", seconds)},"[/color]"}}
else
construction_info_table.add{type = "label", caption = {"", "[color=green]", {"tycoon-housing-construction-time-remaining", string.format("%02d", minutes), string.format("%02d", seconds)},"[/color]"}}
end
construction_info.add{type = "label", caption = {"", {"tycoon-gui-boost-construction-speed"}}}
local basic_needs_container = tabbed_pane.add{type = "scroll-pane", direction = "vertical"}
tabbed_pane.add_tab(tab_basic_needs, basic_needs_container)
addBasicNeedsView(basic_needs_container, basicNeeds[housingType], city, waterTowers, markets, housingType)
local advanced_needs_container = tabbed_pane.add{type = "scroll-pane", direction = "vertical"}
tabbed_pane.add_tab(tab_additional_needs, advanced_needs_container)
addAdditionalNeedsView(advanced_needs_container, additionalNeeds[housingType], city, markets, housingType)
local construction_needs_container = tabbed_pane.add{type = "scroll-pane", direction = "vertical"}
tabbed_pane.add_tab(tab_construction_material, construction_needs_container)
addConstructionMaterialsGui(construction_needs_container, Constants.CONSTRUCTION_MATERIALS[housingType], city, hardwareStores, housingType)
tabbed_pane.selected_tab_index = GuiState.get_state(player_index, "city_tab:" .. city.id + 1 .. ":housing_tab:" .. GuiState.housing_type_to_tab[housingType] .. ":needs_tab") or 1
end
local function getOverallSupplyLevelsSummary(city, needsFn)
local simpleLevels = Consumption.getSupplyLevels(city, needsFn(city, "simple"))
local residentialLevels = Consumption.getSupplyLevels(city, needsFn(city, "residential"))
local highriseLevels = Consumption.getSupplyLevels(city, needsFn(city, "highrise"))
local simpleLevelSummary = getSupplyLevelsSummary(simpleLevels)
local residentialLevelSummary = game.forces.player.technologies["tycoon-residential-housing"].researched and getSupplyLevelsSummary(residentialLevels) or "supplied"
local highriseLevelSummary = game.forces.player.technologies["tycoon-highrise-housing"].researched and getSupplyLevelsSummary(highriseLevels) or "supplied"
if simpleLevelSummary == "supplied" and residentialLevelSummary == "supplied" and highriseLevelSummary == "supplied" then
return "supplied"
elseif simpleLevelSummary == "missing" and residentialLevelSummary == "missing" and highriseLevelSummary == "missing" then
return "missing"
else
return "lacking"
end
end
local function getOverallAdditionalNeedsCaption(city)
local supplyLevel = getOverallSupplyLevelsSummary(city, getAdditionalNeeds)
return mapSupplyLevelToLocalised(supplyLevel)
end
local function getOverallBasicNeedsCaption(city)
local supplyLevel = getOverallSupplyLevelsSummary(city, getBasicNeeds)
return mapSupplyLevelToLocalised(supplyLevel)
end
local function getOverallConstructionMaterialsCaption(city)
local hardwareStores = Util.list_special_city_buildings(city, "tycoon-hardware-store")
local simpleMet = areConstructionNeedsMet(city, "simple", hardwareStores)
local residentialMet = not game.forces.player.technologies["tycoon-residential-housing"].researched or areConstructionNeedsMet(city, "residential", hardwareStores)
local highriseMet = not game.forces.player.technologies["tycoon-highrise-housing"].researched or areConstructionNeedsMet(city, "highrise", hardwareStores)
if simpleMet and residentialMet and highriseMet then
return {"", "[color=green]", {"tycoon-gui-status-supplied"}, "[/color]"}
elseif simpleMet or residentialMet or highriseMet then
return {"", "[color=orange]", {"tycoon-gui-status-lacking"}, "[/color]"}
else
return {"", "[color=red]", {"tycoon-gui-status-missing"}, "[/color]"}
end
end
local function addCityOverview(city, anchor)
local stats = anchor.add{type = "frame", direction = "vertical", caption = {"", {"tycoon-gui-stats"}}, name = "city_stats"}
local tbl = stats.add{type = "table", column_count = 2, draw_horizontal_lines = true}
-- citizen count
tbl.add{type = "label", caption = {"", {"tycoon-gui-citizens"}, ": "}}
tbl.add{type = "label", caption = {"", countCitizens(city)}}
-- debt
tbl.add{type = "label", caption = {"", {"tycoon-gui-debt"}, ": "}, tooltip = {"", {"tycoon-gui-debt-tooltip"}}}
tbl.add{type = "label",
caption = {"", tostring(math.floor(city.stats.debt))},
tooltip = {"", string.format("%.2f", city.stats.debt)},
}
-- overall basic needs status
tbl.add{type = "label", caption = {"", {"tycoon-gui-basic-needs"}, ": "}}
tbl.add{type = "label", caption = getOverallBasicNeedsCaption(city)}
-- overall additional needs status
tbl.add{type = "label", caption = {"", {"tycoon-gui-additional-needs"}, ": "}}
tbl.add{type = "label", caption = getOverallAdditionalNeedsCaption(city)}
-- overall construction materials status
tbl.add{type = "label", caption = {"", {"tycoon-gui-construction-materials"}, ": "}}
tbl.add{type = "label", caption = getOverallConstructionMaterialsCaption(city)}
end
local function canCreatePassengerForCity(train_station_number, destination_city_id)
-- If the player has not set any filters for this station (i.e. they are nil or none are false), then we can display new cities as accepted
-- If any city has been disabled, then we won't allow new cities to automatically show up and produce passengers
local areAllFiltersPositive = true
if storage.tycoon_train_station_passenger_filters ~= nil and storage.tycoon_train_station_passenger_filters[train_station_number] ~= nil then
for _, c in ipairs(storage.tycoon_cities) do
if storage.tycoon_train_station_passenger_filters[train_station_number][c.id] == false then
areAllFiltersPositive = false
break
end
end
end
if areAllFiltersPositive then
return true
end
-- If the station does not know how to filter a station yet and not all filters are positive, then set the new one to negative
if storage.tycoon_train_station_passenger_filters[train_station_number][destination_city_id] == nil then
storage.tycoon_train_station_passenger_filters[train_station_number][destination_city_id] = areAllFiltersPositive
end
return storage.tycoon_train_station_passenger_filters[train_station_number][destination_city_id]
end
local function addTrainStationView(trainStationUnitNumber, anchor, city)
if not city then
anchor.add{type = "label", caption = {"", {"tycoon-gui-train-station-is-missing-city" }}}
return
end
if not storage.tycoon_train_station_limits then
storage.tycoon_train_station_limits = {}
end
if not storage.tycoon_train_station_limits[trainStationUnitNumber] then
-- 100 is the inventory_size of the passenger-train-station entity, filling up 80% is a good default to not immediately get stuck
storage.tycoon_train_station_limits[trainStationUnitNumber] = 80
end
local flow = anchor.add{type = "flow", direction = "vertical"}
if city ~= nil then
flow.add{type = "label", caption = {"", {"tycoon-gui-train-station-for-city", city.name}}}
flow.add{type = "line"}
end
flow.add{type = "label", caption = {"", {"tycoon-gui-train-station-limit", 0, 100}, ":"}}
flow.add{
type = "textfield",
numeric = true,
allow_decimal = false,
allow_negative = false,
text = "" .. storage.tycoon_train_station_limits[trainStationUnitNumber],
name = "train_station_limit:" .. trainStationUnitNumber
}
flow.add{type = "line", direction = "horizontal"}
flow.add{type = "label", caption = {"", {"tycoon-gui-select-departures"}}}
for _, c in ipairs(storage.tycoon_cities or {}) do
if city.name == c.name then
flow.add{type = "checkbox", caption = c.name, state = false, enabled = false}
else
flow.add{type = "checkbox", caption = c.name,
state = canCreatePassengerForCity(trainStationUnitNumber, c.id),
name = "train_station_gui_checkbox:" .. string.lower(c.name),
tags = {
destination_city_id = c.id,
train_station_unit_number = trainStationUnitNumber
}
}
end
end
end
local function addTreasuryView(treasuryUnitNumber, anchor, city)
if not storage.tycoon_money_stacks_treasury_enabled then
storage.tycoon_money_stacks_treasury_enabled = {}
end
if not storage.tycoon_money_stacks_treasury_enabled[treasuryUnitNumber] then
storage.tycoon_money_stacks_treasury_enabled[treasuryUnitNumber] = false
end
local flow = anchor.add{type = "flow", direction = "vertical"}
if city ~= nil then
flow.add{type = "label", caption = {"", {"tycoon-gui-train-station-for-city", city.name}}}
-- debt
local tbl = flow.add{type = "table", column_count = 2, draw_horizontal_lines = false}
tbl.add{type = "label", caption = {"", {"tycoon-gui-debt"}, ": "}, tooltip = {"", {"tycoon-gui-debt-tooltip"}}}
tbl.add{type = "label",
caption = {"", tostring(math.floor(city.stats.debt))},
tooltip = {"", string.format("%.2f", city.stats.debt)},
}
flow.add{type = "line"}
end
if storage.tycoon_money_stacks_treasury_researched == nil then
flow.add{type = "checkbox", caption = {"", {"tycoon-gui-treasury-money-stack-enabled"}, ":"}, state = false, enabled = false, tooltip = {"", {"tycoon-gui-treasury-money-stack-how-to", {"technology-name.tycoon-advanced-treasury-payouts"}}}}
else
-- todo: the ui showed 150:2. why?
flow.add{type = "checkbox", caption = {"", {"tycoon-gui-treasury-money-stack-enabled"}}, state = storage.tycoon_money_stacks_treasury_enabled[treasuryUnitNumber], tooltip = {"", {"tycoon-gui-treasury-money-stack-info", Constants.TREASURY_CONVERSION_RATE["tycoon-currency"], Constants.TREASURY_CONVERSION_RATE["tycoon-money-stack"]}}, name = "treasury_money_stack_enabled:" .. treasuryUnitNumber}
end
end
local function addCityView(city, anchor, player_index)
local tabbed_pane = anchor.add{type="tabbed-pane"}
local tab_overview = tabbed_pane.add{type="tab", caption={"", {"tycoon-gui-city-overview"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:1"}
local tab_simple = tabbed_pane.add{type="tab", caption={"", {"technology-name.tycoon-simple-housing"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:2"}
local tab_residential = tabbed_pane.add{type="tab", caption={"", {"technology-name.tycoon-residential-housing"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:3"}
local tab_highrise = tabbed_pane.add{type="tab", caption={"", {"technology-name.tycoon-highrise-housing"}}, name = "city_tab:" .. city.id + 1 .. ":housing_tab:4"}
local overviewContainer = tabbed_pane.add{type = "flow", direction = "vertical"}
tabbed_pane.add_tab(tab_overview, overviewContainer)
addCityOverview(city, overviewContainer)
local simpleContainer = tabbed_pane.add{type = "scroll-pane", direction = "vertical"}
tabbed_pane.add_tab(tab_simple, simpleContainer)
addHousingView("simple", city, simpleContainer, player_index)
local residentialContainer = tabbed_pane.add{type = "scroll-pane", direction = "vertical"}
tabbed_pane.add_tab(tab_residential, residentialContainer)
addHousingView("residential", city, residentialContainer, player_index)
local highriseContainer = tabbed_pane.add{type = "scroll-pane", direction = "vertical"}
tabbed_pane.add_tab(tab_highrise, highriseContainer)
addHousingView("highrise", city, highriseContainer, player_index)
tabbed_pane.selected_tab_index = GuiState.get_state(player_index, "city_tab:" .. city.id + 1 .. ":housing_tab") or 1
end
local function addCitiesOverview(anchor)
local columnWidth = 100
local tbl = anchor.add{type = "table", column_count = 4, draw_horizontal_lines = true}
local c1 = tbl.add{type = "label", caption = {"", {"tycoon-gui-name"}}}
c1.style.width = columnWidth
local c2 = tbl.add{type = "label", caption = {"", {"tycoon-gui-citizens"}}}
c2.style.width = columnWidth
local c3 = tbl.add{type = "label", caption = {"", {"tycoon-gui-basic-needs-met"}}}
c3.style.width = columnWidth + 50
local c4 = tbl.add{type = "label", caption = ""}
c4.style.width = columnWidth
for i, city in ipairs(storage.tycoon_cities or {}) do
tbl.add{type = "label", caption = {"", "[font=default-bold]", city.name, "[/font]"}}
tbl.add{type = "label", caption = countCitizens(city)}
tbl.add{type = "label", caption = getOverallBasicNeedsCaption(city)}
tbl.add{type = "button", caption = {"", {"tycoon-gui-show-details"}}, name = "multiple_cities_select_tab:" .. i, tags = {selected_tab = i + 1}}
end
end
local function addMultipleCitiesOverview(anchor, player_index)
local flow_title_bar = anchor.add{type="flow", direction="horizontal"}
flow_title_bar.add{type = "label", caption = {"", "[font=default-bold]", {"tycoon-gui-cities-overview"}, "[/font]"}}
flow_title_bar.add{
type="sprite-button",
sprite="utility/close",
hovered_sprite="utility/close_black",
clicked_sprite="utility/close_black",
style="frame_action_button", -- needed to keep the icon small
name="close_multiple_cities_overview"
}
local tabbed_pane = anchor.add{type="tabbed-pane", name = "multiple_cities_overview_tabbed_pane"}
local tab_all_cities = tabbed_pane.add{type="tab", caption={"", {"tycoon-gui-all-cities"}}, name = "city_tab:1"}
local overviewContainer = tabbed_pane.add{type = "flow", direction = "vertical"}
overviewContainer.style.padding = {10, 10, 10, 10}
tabbed_pane.add_tab(tab_all_cities, overviewContainer)
addCitiesOverview(overviewContainer)
for i, city in ipairs(storage.tycoon_cities or {}) do
local tab_city = tabbed_pane.add{type="tab", caption=city.name, name = "city_tab:" .. i + 1}
local cityContainer = tabbed_pane.add{type = "flow", direction = "vertical"}
tabbed_pane.add_tab(tab_city, cityContainer)
addCityView(city, cityContainer, player_index)
end
tabbed_pane.selected_tab_index = GuiState.get_state(player_index, "city_tab") or 1
end
local function addSupplyBuildingOverview(anchor, cityName)
if cityName == "Unknown" then
anchor.add{type = "label", caption = {"", {"tycoon-supply-building-not-connected"}}}
else
anchor.add{type = "label", caption = {"", {"tycoon-supply-building-city", cityName}}}
end
end
local GUI = {
addHousingView = addHousingView,
addBasicNeedsView = addBasicNeedsView,
addConstructionMaterialsGui = addConstructionMaterialsGui,
addCityView = addCityView,
addTrainStationView = addTrainStationView,
addMultipleCitiesOverview = addMultipleCitiesOverview,
addSupplyBuildingOverview = addSupplyBuildingOverview,
addTreasuryView = addTreasuryView,
}
return GUI