-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCiv6Common.lua
879 lines (785 loc) · 34.2 KB
/
Civ6Common.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
------------------------------------------------------------------------------
-- Common LUA support functions specific to Civilization 6
------------------------------------------------------------------------------
include( "ToolTipHelper" );
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local TUTORIAL_UUID :string = "17462E0F-1EE1-4819-AAAA-052B5896B02A";
ProductionType = {
BUILDING = "BUILDING",
DISTRICT = "DISTRICT",
PROJECT = "PROJECT",
UNIT = "UNIT"
}
-- ===========================================================================
-- FUNCTIONS
-- ===========================================================================
-- ===========================================================================
-- Return the inline text-icon for a given yield
-- yieldType A database YIELD_TYPE
-- returns The [ICON_yield] string
-- ===========================================================================
function GetYieldTextIcon( yieldType:string )
local iconString:string = "";
if yieldType == nil or yieldType == "" then
iconString="Error:NIL";
elseif GameInfo.Yields[yieldType] ~= nil and GameInfo.Yields[yieldType].IconString ~= nil and GameInfo.Yields[yieldType].IconString ~= "" then
iconString=GameInfo.Yields[yieldType].IconString;
else
iconString = "Unknown:"..yieldType;
end
return iconString;
end
-- ===========================================================================
-- Return the inline entry for a yield's color
-- ===========================================================================
function GetYieldTextColor( yieldType:string )
if yieldType == nil or yieldType == "" then return "[COLOR:255,255,255,255]NIL ";
elseif yieldType == "YIELD_FOOD" then return "[COLOR:ResFoodLabelCS]";
elseif yieldType == "YIELD_PRODUCTION" then return "[COLOR:ResProductionLabelCS]";
elseif yieldType == "YIELD_GOLD" then return "[COLOR:ResGoldLabelCS]";
elseif yieldType == "YIELD_SCIENCE" then return "[COLOR:ResScienceLabelCS]";
elseif yieldType == "YIELD_CULTURE" then return "[COLOR:ResCultureLabelCS]";
elseif yieldType == "YIELD_FAITH" then return "[COLOR:ResFaithLabelCS]";
else return "[COLOR:255,255,255,0]ERROR ";
end
end
-- ===========================================================================
-- Return a string with +/- or 0 based on any value.
-- ===========================================================================
function toPlusMinusString( value:number )
if(value == 0) then
return "0";
else
return Locale.ToNumber(value, "+#,###.#;-#,###.#");
end
end
-- ===========================================================================
-- Return a string with +/- or 0 based on any value.
-- ===========================================================================
function toPlusMinusNoneString( value:number )
if(value == 0) then
return " ";
else
return Locale.ToNumber(value, "+#,###.#;-#,###.#");
end
end
-- ===========================================================================
-- Return a string with a yield icon and a +/- based on yield amount.
-- ===========================================================================
function GetYieldString( yieldType:string, amount:number )
return GetYieldTextIcon(yieldType)..GetYieldTextColor(yieldType)..toPlusMinusString(amount).."[ENDCOLOR]";
end
-- ===========================================================================
-- Move a unit to X,Y
-- ===========================================================================
function MoveUnitToPlot( kUnit:table, plotX:number, plotY:number )
if kUnit ~= nil then
local tParameters:table = {};
tParameters[UnitOperationTypes.PARAM_X] = plotX;
tParameters[UnitOperationTypes.PARAM_Y] = plotY;
-- Will this start a war? Note, we are ignoring destinations in the for that will start a war, the unit will be allowed to move until they are adjacent.
-- We may want to also skip the war check if the move will take more than one turn to get to the destination.
local eAttackingPlayer:number = kUnit:GetOwner();
local eUnitComponentID:table = kUnit:GetComponentID();
local bWillStartWar = PlayersVisibility[eAttackingPlayer]:IsVisible(plotX, plotY) and CombatManager.IsAttackChangeWarState(eUnitComponentID, plotX, plotY);
if (bWillStartWar) then
local eDefendingPlayer = CombatManager.GetBestDefender(eUnitComponentID, plotX, plotY );
if (eDefendingPlayer == nil) then
local pPlot = Map.GetPlot(plotX, plotY);
eDefendingPlayer = pPlot:GetOwner();
end
-- Create the action specific parameters
if (eDefendingPlayer ~= nil and eDefendingPlayer ~= -1) then
LuaEvents.Civ6Common_ConfirmWarDialog(eAttackingPlayer, eDefendingPlayer, WarTypes.SURPRISE_WAR);
end
else
RequestMoveOperation(kUnit, tParameters, plotX, plotY);
end
end
end
-- ===========================================================================
-- Requests an operation based on the type of unit and parameters
-- ===========================================================================
function RequestMoveOperation( kUnit:table, tParameters:table, plotX:number, plotY:number )
-- Air units move and attack slightly differently than land and naval units
if ( GameInfo.Units[kUnit:GetUnitType()].Domain == "DOMAIN_AIR" ) then
tParameters[UnitOperationTypes.PARAM_MODIFIERS] = UnitOperationMoveModifiers.ATTACK;
if (UnitManager.CanStartOperation( kUnit, UnitOperationTypes.AIR_ATTACK, nil, tParameters) ) then
UnitManager.RequestOperation(kUnit, UnitOperationTypes.AIR_ATTACK, tParameters);
elseif (UnitManager.CanStartOperation( kUnit, UnitOperationTypes.DEPLOY, nil, tParameters) ) then
UnitManager.RequestOperation(kUnit, UnitOperationTypes.DEPLOY, tParameters);
end
else
tParameters[UnitOperationTypes.PARAM_MODIFIERS] = UnitOperationMoveModifiers.NONE;
if (UnitManager.CanStartOperation( kUnit, UnitOperationTypes.RANGE_ATTACK, nil, tParameters) ) then
UnitManager.RequestOperation(kUnit, UnitOperationTypes.RANGE_ATTACK, tParameters);
else
-- Allow for attacking and don't early out if the destination is blocked, etc., but is in the fog.
tParameters[UnitOperationTypes.PARAM_MODIFIERS] = UnitOperationMoveModifiers.ATTACK + UnitOperationMoveModifiers.MOVE_IGNORE_UNEXPLORED_DESTINATION;
if (UnitManager.CanStartOperation( kUnit, UnitOperationTypes.COASTAL_RAID, nil, tParameters) ) then
UnitManager.RequestOperation( kUnit, UnitOperationTypes.COASTAL_RAID, tParameters);
else
-- Check that unit isn't already in the plot (essentially canceling the move),
-- otherwise the operation will complete, and while no move is made, the next
-- unit will auto seltect.
if plotX ~= kUnit:GetX() or plotY ~= kUnit:GetY() then
if (UnitManager.CanStartOperation( kUnit, UnitOperationTypes.SWAP_UNITS, nil, tParameters) ) then
UnitManager.RequestOperation(kUnit, UnitOperationTypes.SWAP_UNITS, tParameters);
else
UnitManager.RequestOperation(kUnit, UnitOperationTypes.MOVE_TO, tParameters);
end
end
end
end
end
end
-- ===========================================================================
-- Multiplier value
-- Return a string with a colorized # and a +/- based on 1.0 based percent.
-- ===========================================================================
function GetColorPercentString( multiplier:number )
if multiplier > 1 then return "[COLOR:StatGoodCS]+"..tostring((multiplier-1)*100).."%[ENDCOLOR]";
elseif multiplier < 1 then return "[COLOR:StatBadCS]-"..tostring((1-multiplier)*100).."%[ENDCOLOR]";
else return "[COLOR:StatNormalCS]100%[ENDCOLOR]";
end
end
function GetFilteredUnitStatString( statData:table )
if statData == nil then
UI.DataError("Invalid stat data passed to GetFilteredUnitStatString");
return "";
end
local statString = "";
local statStringTooltip = "";
local newlineCounter = 0;
for _,statTable in pairs(statData) do
statString = statString.. statTable.FontIcon.. " ".. statTable.Value.. " ";
if (newlineCounter == 2) then
statString = statString.. "[NEWLINE]";
newlineCounter = 0;
end
newlineCounter = newlineCounter + 1;
statStringTooltip = statStringTooltip.. Locale.Lookup(statTable.Label).. " ".. statTable.Value.. "[NEWLINE]";
end
--return statString, statStringTooltip;
return statString;
end
function FilterUnitStats( hashOrType:number, ignoreStatType:number )
local unitInfo = GameInfo.Units[hashOrType];
if(unitInfo == nil) then
UI.DataError("Invalid unit hash passed to FilterUnitStats");
return {};
end
local data:table = {};
-- Strength
if ( unitInfo.Combat > 0 and (ignoreStatType == nil or ignoreStatType ~= CombatTypes.MELEE)) then
table.insert(data, {Value = unitInfo.Combat, Type = "Combat", Label = "LOC_HUD_UNIT_PANEL_STRENGTH", FontIcon="[ICON_Strength_Large]", IconName="ICON_STRENGTH"});
end
if ( unitInfo.RangedCombat > 0 and (ignoreStatType == nil or ignoreStatType ~= CombatTypes.RANGED)) then
table.insert(data, {Value = unitInfo.RangedCombat, Label = "LOC_HUD_UNIT_PANEL_RANGED_STRENGTH", FontIcon="[ICON_RangedStrength_Large]", IconName="ICON_RANGED_STRENGTH"});
end
if (unitInfo.Bombard > 0 and (ignoreStatType == nil or ignoreStatType ~= CombatTypes.BOMBARD)) then
table.insert(data, {Value = unitInfo.Bombard, Label = "LOC_HUD_UNIT_PANEL_BOMBARD_STRENGTH", FontIcon="[ICON_Bombard_Large]", IconName="ICON_BOMBARD"});
end
if (unitInfo.ReligiousStrength > 0 and (ignoreStatType == nil or ignoreStatType ~= CombatTypes.RELIGIOUS)) then
table.insert(data, {Value = unitInfo.ReligiousStrength, Label = "LOC_HUD_UNIT_PANEL_RELIGIOUS_STRENGTH", FontIcon="[ICON_ReligionStat_Large]", IconName="ICON_RELIGION"});
end
if (unitInfo.AntiAirCombat > 0 and (ignoreStatType == nil or ignoreStatType ~= CombatTypes.AIR)) then
table.insert(data, {Value = unitInfo.AntiAirCombat, Label = "LOC_HUD_UNIT_PANEL_ANTI_AIR_STRENGTH", FontIcon="[ICON_AntiAir_Large]", IconName="ICON_STATS_ANTIAIR"});
end
-- Movement
if(unitInfo.BaseMoves > 0) then
table.insert(data, {Value = unitInfo.BaseMoves, Type = "BaseMoves", Label = "LOC_HUD_UNIT_PANEL_MOVEMENT", FontIcon="[ICON_Movement_Large]", IconName="ICON_MOVES"});
end
-- Range
if (unitInfo.Range > 0) then
table.insert(data, {Value = unitInfo.Range; Label = "LOC_HUD_UNIT_PANEL_ATTACK_RANGE", FontIcon="[ICON_Range_Large]", IconName="ICON_RANGE"});
end
-- Charges
if (unitInfo.SpreadCharges > 0) then
table.insert(data, {Value = unitInfo.SpreadCharges, Type = "SpreadCharges", Label = "LOC_HUD_UNIT_PANEL_SPREADS", FontIcon="[ICON_ReligionStat_Large]", IconName="ICON_RELIGION"});
end
if (unitInfo.BuildCharges > 0) then
table.insert(data, {Value = unitInfo.BuildCharges, Type = "BuildCharges", Label = "LOC_HUD_UNIT_PANEL_BUILDS", FontIcon="[ICON_Charges_Large]", IconName="ICON_BUILD_CHARGES"});
end
-- If we have more than 4 stats then try to remove melee strength
if (table.count(data) > 4) then
for i,stat in ipairs(data) do
if stat.Type == "Combat" then
table.remove(data, i);
end
end
end
-- If we still have more than 4 stats through a data error
if (table.count(data) > 4) then
UI.DataError("More than four stats were picked to display for unit ".. unitInfo.UnitType);
end
return data;
end
-- ===========================================================================
-- Obtains the texture for a city's current production.
-- pCity The city
-- productionHash the production hash (present or past) that you want the info for
--
-- RETURNS NIL if error, otherwise a table containing:
-- name of production item
-- description
-- icon texture of the produced item
-- u offset of the icon texture
-- v offset of the icon texture
-- (0-1) percent complete
-- (0-1) percent complete after next turn
-- ===========================================================================
function GetProductionInfoOfCity( pCity:table, productionHash:number )
local pBuildQueue :table = pCity:GetBuildQueue();
if pBuildQueue == nil then
UI.DataError("No production queue in city!");
return nil;
end
local hash = productionHash;
local progress :number = 0;
local cost :number = 0;
local percentComplete :number = 0;
local percentCompleteNextTurn :number = 0;
local productionName :string;
local description :string;
local statString :string; -- stats for unit to display
local iconName :string; -- name of icon to look up
local texture :string; -- texture of icon
local u :number = 0; -- texture horiztonal offset
local v :number = 0; -- texture vertical offset
-- Nothing being produced.
if hash == 0 then
return {
Name = Locale.Lookup("LOC_HUD_CITY_NOTHING_PRODUCED"),
Description = "",
Texture = "CityPanel_CitizenIcon", -- Default texture
u = 0,
v = 0,
PercentComplete = 0,
PercentCompleteNextTurn = 0,
Turns = 0,
Progress = 0,
Cost = 0
};
end
-- Find the information
local buildingDef :table = GameInfo.Buildings[hash];
local districtDef :table = GameInfo.Districts[hash];
local unitDef :table = GameInfo.Units[hash];
local projectDef :table = GameInfo.Projects[hash];
local type :string= "";
if( buildingDef ~= nil ) then
prodTurnsLeft = pBuildQueue:GetTurnsLeft(buildingDef.BuildingType);
productionName = Locale.Lookup(buildingDef.Name);
description = buildingDef.Description;
progress = pBuildQueue:GetBuildingProgress(buildingDef.Index);
percentComplete = progress / pBuildQueue:GetBuildingCost(buildingDef.Index);
cost = pBuildQueue:GetBuildingCost(buildingDef.Index);
iconName = "ICON_"..buildingDef.BuildingType;
type = ProductionType.BUILDING;
elseif( districtDef ~= nil ) then
prodTurnsLeft = pBuildQueue:GetTurnsLeft(districtDef.DistrictType);
productionName = Locale.Lookup(districtDef.Name);
description = districtDef.Description;
progress = pBuildQueue:GetDistrictProgress(districtDef.Index);
percentComplete = progress / pBuildQueue:GetDistrictCost(districtDef.Index);
cost = pBuildQueue:GetDistrictCost(districtDef.Index);
iconName = "ICON_"..districtDef.DistrictType;
type = ProductionType.DISTRICT;
elseif( unitDef ~= nil ) then
prodTurnsLeft = pBuildQueue:GetTurnsLeft(unitDef.UnitType);
local eMilitaryFormationType :number = pBuildQueue:GetCurrentProductionTypeModifier();
productionName = Locale.Lookup(unitDef.Name);
description = unitDef.Description;
progress = pBuildQueue:GetUnitProgress(unitDef.Index);
prodTurnsLeft = pBuildQueue:GetTurnsLeft(unitDef.UnitType, eMilitaryFormationType);
iconName = "ICON_"..unitDef.UnitType.."_PORTRAIT";
statString = GetFilteredUnitStatString(FilterUnitStats(hash));
type = ProductionType.UNIT;
--Units need some additional information to represent the Standard, Corps, and Army versions. This is determined by the MilitaryFormationType
if (eMilitaryFormationType == MilitaryFormationTypes.STANDARD_FORMATION) then
percentComplete = progress / pBuildQueue:GetUnitCost(unitDef.Index);
cost = pBuildQueue:GetUnitCost(unitDef.Index);
elseif (eMilitaryFormationType == MilitaryFormationTypes.CORPS_FORMATION) then
percentComplete = progress / pBuildQueue:GetUnitCorpsCost(unitDef.Index);
cost = pBuildQueue:GetUnitCorpsCost(unitDef.Index);
if (unitDef.Domain == "DOMAIN_SEA") then
productionName = productionName .. " " .. Locale.Lookup("LOC_UNITFLAG_FLEET_SUFFIX");
else
productionName = productionName .. " " .. Locale.Lookup("LOC_UNITFLAG_CORPS_SUFFIX");
end
elseif (eMilitaryFormationType == MilitaryFormationTypes.ARMY_FORMATION) then
percentComplete = progress / pBuildQueue:GetUnitArmyCost(unitDef.Index);
cost = pBuildQueue:GetUnitArmyCost(unitDef.Index);
if (unitDef.Domain == "DOMAIN_SEA") then
productionName = productionName .. " " .. Locale.Lookup("LOC_UNITFLAG_ARMADA_SUFFIX");
else
productionName = productionName .. " " .. Locale.Lookup("LOC_UNITFLAG_ARMY_SUFFIX");
end
end
elseif (projectDef ~= nil) then
prodTurnsLeft = pBuildQueue:GetTurnsLeft(projectDef.ProjectType);
productionName = Locale.Lookup(projectDef.Name);
description = projectDef.Description;
progress = pBuildQueue:GetProjectProgress(projectDef.Index);
cost = pBuildQueue:GetProjectCost(projectDef.Index);
percentComplete = progress / pBuildQueue:GetProjectCost(projectDef.Index);
iconName = "ICON_"..projectDef.ProjectType;
type = ProductionType.PROJECT;
else
for row in GameInfo.Types() do
if row.Hash == hash then
UI.DataError("Unknown kind of item being produced in city \""..tostring(row.Kind).."\"");
return nil;
end
end
UI.DataError("Game database does not contain information that matches what the city "..Locale.Lookup(data.CityName).." is producing!");
return nil;
end
if percentComplete > 1 then
percentComplete = 1;
end
percentCompleteNextTurn = (1-percentComplete)/prodTurnsLeft;
percentCompleteNextTurn = percentComplete + percentCompleteNextTurn;
return {
Name = productionName,
Description = description,
Type = type;
Icon = iconName,
PercentComplete = percentComplete,
PercentCompleteNextTurn = percentCompleteNextTurn,
Turns = prodTurnsLeft,
StatString = statString;
Progress = progress;
Cost = cost;
};
end
-- ===========================================================================
-- Obtain the stats for a unit, given it's hash or type string
-- RETURNS: nil if not found, or table o' stats
-- ===========================================================================
function GetUnitStats( hashOrType )
local info:table= GameInfo.Units[hashOrType];
if info == nil then
--error("Was unable to find a Unit to get it's stats with the value \""..tostring(hashOrType).."\"");
return nil;
end
return {
Bombard = info.Bombard,
Combat = info.Combat,
Moves = info.BaseMoves,
RangedCombat= info.RangedCombat,
Range = info.Range
}
end
-- ===========================================================================
-- Returns the icon info and shadow icon info for the passed in unit or returns default icons if those can't be found
-- RETURN 1: iconInfo - table containing textureSheet, textureOffsetX, and textureOffsetY
-- RETURN 2: iconShadowInfo - table containing textureSheetShadow, textureOffsetShadowX, and textureOffsetShadowY
-- ===========================================================================
function GetUnitIcon( pUnit:table, iconSize:number )
local iconInfo:table = {};
if pUnit then
local unitIcon:string = nil;
local individual:number = pUnit:GetGreatPerson():GetIndividual();
if individual >= 0 then
local individualType:string = GameInfo.GreatPersonIndividuals[individual].GreatPersonIndividualType;
local iconModifier:table = GameInfo.GreatPersonIndividualIconModifiers[individualType];
if iconModifier then
unitIcon = iconModifier.OverrideUnitIcon;
end
end
if not unitIcon then
local unit:table = GameInfo.Units[pUnit:GetUnitType()];
unitIcon = "ICON_" .. unit.UnitType;
end
iconInfo.textureOffsetX, iconInfo.textureOffsetY, iconInfo.textureSheet = IconManager:FindIconAtlas(unitIcon, iconSize);
if (iconInfo.textureSheet == nil) then --Check to see if the unit has an icon atlas index defined
print("UIWARNING: Could not find icon for " .. unitIcon);
iconInfo.textureOffsetX, iconInfo.textureOffsetY, iconInfo.textureSheet = IconManager:FindIconAtlas("ICON_UNIT_UNKNOWN", iconSize); --If not, resolve the index to be a generic unknown index
end
end
return iconInfo;
end
-- ===========================================================================
-- A helper function to size a GridButton to its string contents
-- ARG 1: gridButton (table) - expects a control of type GridButton to be resized
-- ARG 4: minX (number) - the minimum width of the button
-- ARG 5: minY (number) - the minimum height of the button
-- ARG 2: OPTIONAL padding (number) - the amount of padding which should be inserted around the text string
-- ARG 3: OPTIONAL sizeOption (string) - expects V or H - to specifiy if the button should only be sized vertically or horizontally
-- ===========================================================================
function AutoSizeGridButton(gridButton:table,minX: number, minY: number, padding:number, sizeOption:string)
if (sizeOption == nil) then
sizeOption = "1";
end
if (padding == nil) then
padding = 0;
end
local labelControl = gridButton:GetTextControl();
local labelX = labelControl:GetSizeX() + padding*2;
local labelY = labelControl:GetSizeY() + padding*2;
if (minX ~= nil) then
labelX = math.max(minX, labelX);
end
if (minY ~= nil) then
labelY = math.max(minY, labelY);
end
if(sizeOption == "V" or sizeOption == "1") then
gridButton:SetSizeY(labelY);
end
if(sizeOption == "H" or sizeOption == "1") then
gridButton:SetSizeX(labelX);
end
return labelX, labelY;
end
-- ===========================================================================
function GetLeaderUniqueTraits( leaderType:string )
-- Gather info.
local base_leader = GameInfo.Leaders[leaderType];
if(base_leader == nil) then
return;
end
function AddInheritedLeaders(leaders, leader)
local inherit = leader.InheritFrom;
if(inherit ~= nil) then
local parent = GameInfo.Leaders[inherit];
if(parent) then
table.insert(leaders, parent);
AddInheritedLeaders(leaders, parent);
end
end
end
local leaders = {};
table.insert(leaders, base_leader);
AddInheritedLeaders(leaders, base_leader);
-- Enumerate final list and index.
local has_leader = {};
for i,leader in ipairs(leaders) do
has_leader[leader.LeaderType] = true;
end
-- Unique Abilities
-- We're considering a unique ability to be a trait which does
-- not have a unique unit, building, district, or improvement associated with it.
-- While we scrub for unique units and infrastructure, mark traits that match
-- so we can filter them later.
local traits = {};
local has_trait = {};
local not_ability = {};
for row in GameInfo.LeaderTraits() do
if(has_leader[row.LeaderType] == true) then
local trait = GameInfo.Traits[row.TraitType];
if(trait) then
table.insert(traits, trait);
end
has_trait[row.TraitType] = true;
end
end
-- Unique Units
local uu = {};
for row in GameInfo.Units() do
local trait = row.TraitType;
if(trait) then
not_ability[trait] = true;
if(has_trait[trait] == true) then
local description :string = Locale.Lookup("LOC_LOADING_"..row.Domain);
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Units[row.UnitType].Description);
end
table.insert(uu, { Type = row.UnitType, Name = row.Name, Description = description });
end
end
end
-- Unique Buildings/Districts/Improvements
local ub = {};
for row in GameInfo.Buildings() do
local trait = row.TraitType;
if(trait) then
not_ability[trait] = true;
if(has_trait[trait] == true) then
local districtName:string = Locale.Lookup(GameInfo.Districts[row.PrereqDistrict].Name);
local description :string = Locale.Lookup("LOC_LOADING_DISTRICT_BUILDING", districtName);
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Buildings[row.BuildingType].Description);
end
table.insert(ub, {Type = row.BuildingType, Name = row.Name, Description = description});
end
end
end
for row in GameInfo.Districts() do
local trait = row.TraitType;
if(trait) then
not_ability[trait] = true;
if(has_trait[trait] == true) then
local description :string = Locale.Lookup("LOC_LOADING_UNIQUE_DISTRICT");
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Districts[row.DistrictType].Description);
end
table.insert(ub, {Type = row.DistrictType, Name = row.Name, Description = description});
end
end
end
for row in GameInfo.Improvements() do
local trait = row.TraitType;
if(trait) then
not_ability[trait] = true;
if(has_trait[trait] == true) then
local description :string = Locale.Lookup("LOC_LOADING_UNIQUE_IMPROVEMENT");
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Improvements[row.ImprovementType].Description);
end
table.insert(ub, {Type = row.ImprovementType, Name = row.Name, Description = description});
end
end
end
local unique_abilities = {};
for i, trait in ipairs(traits) do
print(trait.InternalOnly);
if(not_ability[trait.TraitType] ~= true and not trait.InternalOnly) then
table.insert(unique_abilities, trait);
end
end
return unique_abilities,uu,ub;
end
-- ===========================================================================
function GetCivilizationUniqueTraits( civType:string )
local traits = {};
for row in GameInfo.CivilizationTraits() do
if(row.CivilizationType == civType) then
traits[row.TraitType] = true;
end
end
-- Unique Abilities
-- We're considering a unique ability to be a trait which does
-- not have a unique unit, building, district, or improvement associated with it.
-- While we scrub for unique units and infrastructure, mark traits that match
-- so we can filter them later.
local not_abilities = {};
-- Unique Units
local uu = {};
for row in GameInfo.Units() do
local trait = row.TraitType;
if(trait) then
not_abilities[trait] = true;
if(traits[trait] == true) then
local description :string = Locale.Lookup("LOC_LOADING_"..row.Domain);
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Units[row.UnitType].Description);
end
table.insert(uu, { Type = row.UnitType, Name = row.Name, Description = description });
end
end
end
-- Unique Buildings/Districts/Improvements
local ub = {};
for row in GameInfo.Buildings() do
local trait = row.TraitType;
if(trait) then
not_abilities[trait] = true;
if(traits[trait] == true) then
local building :table = GameInfo.Buildings[row.BuildingType];
local description :string = Locale.Lookup("LOC_LOADING_UNIQUE_BUILDING");
if m_isTraitsFullDescriptions then
if building == nil then
UI.DataError("Could not get CIV trait as GameInfo.Buildings["..row.BuildingType.."] does not exist.");
elseif building.Description == nil then
UI.DataError("Could not get CIV trait description for GameInfo.Buildings["..row.BuildingType.."]. None supplied.");
else
description = Locale.Lookup(building.Description);
end
end
table.insert(ub, {Type = row.BuildingType, Name = row.Name, Description = description});
end
end
end
for row in GameInfo.Districts() do
local trait = row.TraitType;
if(trait) then
not_abilities[trait] = true;
if(traits[trait] == true) then
local description :string = Locale.Lookup("LOC_LOADING_UNIQUE_DISTRICT");
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Districts[row.DistrictType].Description);
end
table.insert(ub, {Type = row.DistrictType, Name = row.Name, Description = description});
end
end
end
for row in GameInfo.Improvements() do
local trait = row.TraitType;
if(trait) then
not_abilities[trait] = true;
if(traits[trait] == true) then
local description :string = Locale.Lookup("LOC_LOADING_UNIQUE_IMPROVEMENT");
if m_isTraitsFullDescriptions then
description = Locale.Lookup(GameInfo.Improvements[row.ImprovementType].Description);
end
table.insert(ub, {Type = row.ImprovementType, Name = row.Name, Description = description});
end
end
end
local unique_abilities = {};
for row in GameInfo.CivilizationTraits() do
if(row.CivilizationType == civType and not_abilities[row.TraitType] ~= true) then
local trait = GameInfo.Traits[row.TraitType];
if(trait) then
table.insert(unique_abilities, trait);
end
end
end
return unique_abilities, uu, ub;
end
-- ===========================================================================
-- Is the on-rails tutorial active?
-- ===========================================================================
function IsTutorialRunning()
local mods = Modding.GetActiveMods();
if mods ~= nil then
for i,v in ipairs(mods) do
if v.Id == TUTORIAL_UUID then
return true;
end
end
else
UI.DataError("Unable to obtain mods table to determine if tutorial is running.");
end
return false;
end
-- ===========================================================================
-- DifferentiateCiv
-- ===========================================================================
-- This is a fix for duplicate civs. If you feed it a control, it will generate a tooltip which lists the cities for that civ, to help differentiate which civ you are currently viewing.
-- Note: This function does NOT contain the IsDuplicate? check. Use this function once you have already determined whether or not you wish to differentiate the civ.
-- Additionally, you can pass icon backing/icon controls to be colored with the civ's colors.
-- ARG1 playerID (number) The player id of the civ in question
-- ARG2 tooltipControl (table) OPTIONAL This is the control that should receive the tooltip
-- ARG3 icon (table) OPTIONAL The icon control which will receive the foreground color for the civ
-- ARG4 iconBacking (table) OPTIONAL The image behind the icon which will receive the background color for the civ
-- ARG5 iconBackingDarker (table) OPTIONAL If you are making a fancy icon with more depth, you can additionally pass the layer to be darkened
-- ARG6 iconBackingLighter (table) OPTIONAL .. and also the layer to be lightened
-- ARG7 observerPlayerID (number) OPTIONAL Checks if the players met
-- RETURNS (string) String to be used as a tooltip which lists Civ name, Leader/Player name, list of cities
function DifferentiateCiv(playerID:number, tooltipControl:table, icon:table, iconBacking:table, iconBackingDarker:table, iconBackingLighter:table, observerPlayerID:number)
local player:table = Players[playerID];
local playerConfig:table = PlayerConfigurations[playerID];
local hasMet:boolean = true;
if player ~= nil and observerPlayerID ~= nil and playerID ~= observerPlayerID then
hasMet = player:GetDiplomacy():HasMet(observerPlayerID);
end
if (player ~= nil and hasMet and iconBacking ~= nil and icon ~= nil) then
m_primaryColor, m_secondaryColor = UI.GetPlayerColors( playerID );
iconBacking:SetColor(m_primaryColor);
if(iconBackingLighter ~= nil and iconBackingDarker ~= nil) then
local darkerBackColor = DarkenLightenColor(m_primaryColor,(-85),100);
local brighterBackColor = DarkenLightenColor(m_primaryColor,90,255);
iconBackingLighter:SetColor(brighterBackColor);
iconBackingDarker:SetColor(darkerBackColor);
end
icon:SetColor(m_secondaryColor);
end
-- Set the leader name, civ name, and civ icon data
local leader:string = playerConfig:GetLeaderTypeName();
if GameInfo.CivilizationLeaders[leader] == nil then
UI.DataError("Banners found a leader \""..leader.."\" which is not/no longer in the game; icon may be whack.");
else
if(GameInfo.CivilizationLeaders[leader].CivilizationType ~= nil) then
local civIcon:string;
local civTooltip:string;
if hasMet then
local civTypeName = GameInfo.CivilizationLeaders[leader].CivilizationType;
local civName = Locale.Lookup(GameInfo.Civilizations[civTypeName].Name);
local leaderName = Locale.Lookup(GameInfo.Leaders[leader].Name);
if GameConfiguration.IsAnyMultiplayer() and player:IsHuman() then
local playerName = Locale.Lookup(playerConfig:GetPlayerName());
leaderName = leaderName .. " ("..Locale.ToUpper(playerName)..")"
end
civIcon = "ICON_"..civTypeName;
--Create a tooltip which shows a list of this Civ's cities
civTooltip = civName .. "[NEWLINE]".. leaderName;
local playerCities = player:GetCities();
if(playerCities ~= nil) then
civTooltip = civTooltip .. "[NEWLINE]"..Locale.Lookup("LOC_PEDIA_CONCEPTS_PAGEGROUP_CITIES_NAME").. ":";
for i,city in playerCities:Members() do
civTooltip = civTooltip.. "[NEWLINE]".. Locale.Lookup(city:GetName());
end
end
else
civIcon = "ICON_LEADER_DEFAULT";
civTooltip = Locale.Lookup("LOC_DIPLOPANEL_UNMET_PLAYER");
if GameConfiguration.IsAnyMultiplayer() and player:IsHuman() then
local playerName = Locale.Lookup(playerConfig:GetPlayerName());
civTooltip = civTooltip .. " ("..Locale.ToUpper(playerName)..")";
end
end
if (icon ~= nil) then
icon:SetIcon(civIcon);
end
if (tooltipControl ~= nil) then
tooltipControl:SetToolTipString(Locale.Lookup(civTooltip));
end
return civTooltip;
end
end
end
-- Duplicating this function from SupportFunctions so that we won't have to pull in the entire file just to support DifferentiateCivs
-- ===========================================================================
-- Transforms a ABGR color by some amount
-- ARGS: hexColor Hex color value (0xAAGGBBRR)
-- amt (0-255) the amount to darken or lighten the color
-- alpha ???
-- RETURNS: transformed color (0xAAGGBBRR)
-- ===========================================================================
function DarkenLightenColor( hexColor:number, amt:number, alpha:number )
--Parse the a,g,b,r hex values from the string
local hexString :string = string.format("%x",hexColor);
local b = string.sub(hexString,3,4);
local g = string.sub(hexString,5,6);
local r = string.sub(hexString,7,8);
b = tonumber(b,16);
g = tonumber(g,16);
r = tonumber(r,16);
if (b == nil) then b = 0; end
if (g == nil) then g = 0; end
if (r == nil) then r = 0; end
local a = string.format("%x",alpha);
if (string.len(a)==1) then
a = "0"..a;
end
b = b + amt;
if (b < 0 or b == 0) then
b = "00";
elseif (b > 255 or b == 255) then
b = "FF";
else
b = string.format("%x",b);
if (string.len(b)==1) then
b = "0"..b;
end
end
g = g + amt;
if (g < 0 or g == 0) then
g = "00";
elseif (g > 255 or g == 255) then
g = "FF";
else
g = string.format("%x",g);
if (string.len(g)==1) then
g = "0"..g;
end
end
r = r + amt;
if (r < 0 or r == 0) then
r = "00";
elseif (r > 255 or r == 255) then
r = "FF";
else
r = string.format("%x",r);
if (string.len(r)==1) then
r = "0"..r;
end
end
hexString = a..b..g..r;
return tonumber(hexString,16);
end