-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitFlagManager.lua
More file actions
2176 lines (1908 loc) · 78.6 KB
/
UnitFlagManager.lua
File metadata and controls
2176 lines (1908 loc) · 78.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ===========================================================================
-- Unit Flag Manager
-- Manages all the 2d "flags" above units on the world map.
-- ===========================================================================
include( "InstanceManager" );
include( "SupportFunctions" );
include( "Civ6Common" );
include("TeamSupport");
print("UnitFlagManager for Better Spectator Mod and Team Color Mod");
-- ===========================================================================
-- GLOBALS
-- ===========================================================================
g_UnitsMilitary = UILens.CreateLensLayerHash("Units_Military");
g_UnitsReligious = UILens.CreateLensLayerHash("Units_Religious");
g_UnitsCivilian = UILens.CreateLensLayerHash("Units_Civilian");
g_UnitsArcheology = UILens.CreateLensLayerHash("Units_Archaeology");
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
COLOR_RED = UI.GetColorValue("COLOR_RED"); -- Obtain colors from colorDB (not const or colorAtlas)
COLOR_YELLOW = UI.GetColorValue("COLOR_YELLOW"); -- ditto
COLOR_GREEN = UI.GetColorValue("COLOR_STANDARD_GREEN_LT"); -- "
COLOR_BLUE = UI.GetColorValue("COLOR_BLUE"); -- "
HEALTH_PERCENT_GOOD = 0.8; -- This and above means a unit is still in good shape
HEALTH_PERCENT_BAD = 0.4; -- Above this the unit is okay but below it, the unit is considered to be in bad shape
YOFFSET_2DVIEW = 26;
ZOFFSET_3DVIEW = 36;
ALPHA_DIM = 0.65;
FLAGSTATE_NORMAL = 0;
FLAGSTATE_FORTIFIED = 1;
FLAGSTATE_EMBARKED = 2;
FLAGSTYLE_MILITARY = 0;
FLAGSTYLE_CIVILIAN = 1;
FLAGSTYLE_SUPPORT = 2;
FLAGSTYLE_TRADE = 3;
FLAGSTYLE_NAVAL = 4;
FLAGSTYLE_RELIGION = 5;
FLAGTYPE_UNIT = 0;
TEXTURE_BASE = "UnitFlagBase_Combo";
TEXTURE_CIVILIAN = "UnitFlagCivilian_Combo";
TEXTURE_RELIGION = "UnitFlagReligion_Combo";
TEXTURE_EMBARK = "UnitFlagEmbark_Combo";
TEXTURE_FORTIFY = "UnitFlagFortify_Combo";
TEXTURE_NAVAL = "UnitFlagNaval_Combo";
TEXTURE_SUPPORT = "UnitFlagSupport_Combo";
TEXTURE_TRADE = "UnitFlagTrade_Combo";
TXT_UNITFLAG_ARMY_SUFFIX = " " .. Locale.Lookup("LOC_UNITFLAG_ARMY_SUFFIX");
TXT_UNITFLAG_CORPS_SUFFIX = " " .. Locale.Lookup("LOC_UNITFLAG_CORPS_SUFFIX");
TXT_UNITFLAG_ARMADA_SUFFIX = " " .. Locale.Lookup("LOC_UNITFLAG_ARMADA_SUFFIX");
TXT_UNITFLAG_FLEET_SUFFIX = " " .. Locale.Lookup("LOC_UNITFLAG_FLEET_SUFFIX");
TXT_UNITFLAG_ACTIVITY_ON_SENTRY = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_ON_SENTRY");
TXT_UNITFLAG_ACTIVITY_ON_INTERCEPT = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_ON_INTERCEPT");
TXT_UNITFLAG_ACTIVITY_AWAKE = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_AWAKE");
TXT_UNITFLAG_ACTIVITY_HOLD = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_HOLD");
TXT_UNITFLAG_ACTIVITY_SLEEP = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_SLEEP");
TXT_UNITFLAG_ACTIVITY_HEALING = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_HEALING");
TXT_UNITFLAG_ACTIVITY_NO_ACTIVITY = " " .. Locale.Lookup("LOC_UNITFLAG_ACTIVITY_NO_ACTIVITY");
m_FlagOffsets = {};
m_FlagOffsets[1] = {-32,0};
m_FlagOffsets[2] = {32,0};
m_FlagOffsets[3] = {0,-45};
m_LinkOffsets = {};
m_LinkOffsets[1] = {0,0};
m_LinkOffsets[2] = {0,-20};
m_LinkOffsets[3] = {16,22};
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
-- A link to a container that is rendered after the Unit/City flags. This is used
-- so that selected units will always appear above the other objects.
local m_SelectedContainer :table = ContextPtr:LookUpControl( "../SelectedUnitContainer" );
local m_MilitaryInstanceManager :table = InstanceManager:new( "UnitFlag", "Anchor", Controls.MilitaryFlags );
local m_CivilianInstanceManager :table = InstanceManager:new( "UnitFlag", "Anchor", Controls.CivilianFlags );
local m_SupportInstanceManager :table = InstanceManager:new( "UnitFlag", "Anchor", Controls.SupportFlags );
local m_TradeInstanceManager :table = InstanceManager:new( "UnitFlag", "Anchor", Controls.TradeFlags );
local m_NavalInstanceManager :table = InstanceManager:new( "UnitFlag", "Anchor", Controls.NavalFlags );
local m_AttentionMarkerIM :table = InstanceManager:new( "AttentionMarkerInstance", "Root" );
local m_HeroGlowIM :table = InstanceManager:new( "HeroGlowInstance", "Root" );
local m_DirtyComponents :table = nil;
local m_UnitFlagInstances :table = {};
local m_isMapDeselectDisabled :boolean= false;
local g_anon :boolean= false;
local g_spec :boolean= false;
-- COMMENTING OUT hstructures.
-- These structures remained defined for the entire lifetime of the application.
-- If a modder or scenario script needs to redefine it, yer boned.
-- Replacing these with regular tables, for now.
-- The meta table definition that holds the function pointers
--hstructure UnitFlagMeta
---- Pointer back to itself. Required.
--__index : UnitFlagMeta
--
--new : ifunction;
--destroy : ifunction;
--Initialize : ifunction;
--GetUnit : ifunction;
--SetInteractivity : ifunction;
--SetFogState : ifunction;
--SetHide : ifunction;
--SetForceHide : ifunction;
--SetFlagUnitEmblem : ifunction;
--SetColor : ifunction;
--SetDim : ifunction;
--OverrideDimmed : ifunction;
--UpdateDimmedState : ifunction;
--UpdateFlagType : ifunction;
--UpdateHealth : ifunction;
--UpdateVisibility : ifunction;
--UpdateSelected : ifunction;
--UpdateFormationIndicators : ifunction;
--UpdateName : ifunction;
--UpdatePosition : ifunction;
--SetPosition : ifunction;
--UpdateStats : ifunction;
--UpdateReadyState : ifunction;
--UpdatePromotions : ifunction;
--UpdateAircraftCounter : ifunction;
--end
--
---- The structure that holds the banner instance data
--hstructure UnitFlag
--meta : UnitFlagMeta;
--
--m_InstanceManager : table; -- The instance manager that made the control set.
--m_Instance : table; -- The instanced control set.
--
--m_cacheMilitaryFormation : number; -- Name of last military formation this flag was in.
--m_Type : number;
--m_Style : number;
--m_eVisibility : number;
--m_IsInitialized : boolean; -- Is flag done it's initial creation.
--m_IsSelected : boolean;
--m_IsCurrentlyVisible : boolean;
--m_IsForceHide : boolean;
--m_IsDimmed : boolean;
--m_OverrideDimmed : boolean;
--m_OverrideDim : boolean;
--m_FogState : number;
--
--m_Player : table;
--m_UnitID : number; -- The unit ID. Keeping just the ID, rather than a reference because there will be times when we need the value, but the unit instance will not exist.
--end
-- Create one instance of the meta object as a global variable with the same name as the data structure portion.
-- This allows us to do a UnitFlag:new, so the naming looks consistent.
--UnitFlag = hmake UnitFlagMeta {};
UnitFlag = {};
-- Link its __index to itself
UnitFlag.__index = UnitFlag;
-- ===========================================================================
-- Obtain the unit flag associate with a player and unit.
-- RETURNS: flag object (if found), nil otherwise
-- ===========================================================================
function GetUnitFlag(playerID:number, unitID:number)
if m_UnitFlagInstances[playerID]==nil then
return nil;
end
return m_UnitFlagInstances[playerID][unitID];
end
------------------------------------------------------------------
-- constructor
------------------------------------------------------------------
function UnitFlag.new( self, playerID: number, unitID : number, flagType : number, flagStyle : number )
-- local o = hmake UnitFlag { };
local o = {};
setmetatable( o, self );
o:Initialize(playerID, unitID, flagType, flagStyle);
if (m_UnitFlagInstances[playerID] == nil) then
m_UnitFlagInstances[playerID] = {};
end
m_UnitFlagInstances[playerID][unitID] = o;
end
------------------------------------------------------------------
function UnitFlag.destroy( self )
if ( self.m_InstanceManager ~= nil ) then
self:UpdateSelected( false );
if (self.m_Instance ~= nil) then
if(self.m_Instance.AirUnitInstance) then
self.m_Instance.FlagRoot:DestroyChild(self.m_Instance.AirUnitInstance.Root);
self.m_Instance.AirUnitInstance = nil;
end
if(self.m_Instance.HeroGlowInstance) then
m_HeroGlowIM:ReleaseInstance(self.m_Instance.HeroGlowInstance);
end
-- Release any attention markers
if ( self.bHasAttentionMarker == true ) then
m_AttentionMarkerIM:ReleaseInstanceByParent(self.m_Instance.FlagRoot);
end
self.m_InstanceManager:ReleaseInstance( self.m_Instance );
end
end
end
------------------------------------------------------------------
function UnitFlag.GetUnit( self )
local pUnit : table = self.m_Player:GetUnits():FindID(self.m_UnitID);
return pUnit;
end
------------------------------------------------------------------
function UnitFlag.Initialize( self, playerID: number, unitID : number, flagType : number, flagStyle : number)
if (flagType == FLAGTYPE_UNIT) then
if (flagStyle == FLAGSTYLE_MILITARY) then
self.m_InstanceManager = m_MilitaryInstanceManager;
elseif flagstyle == FLAGSTYLE_NAVAL then
self.m_InstanceManager = m_NavalInstanceManager;
elseif flagstyle == FLAGSTYLE_TRADE then
self.m_InstanceManager = m_TradeInstanceManager;
elseif flagstyle == FLAGSTYLE_SUPPORT then
self.m_InstanceManager = m_SupportInstanceManager;
else
self.m_InstanceManager = m_CivilianInstanceManager;
end
self.m_Instance = self.m_InstanceManager:GetInstance();
self.m_Type = flagType;
self.m_Style = flagStyle;
self.m_IsInitialized = false;
self.m_IsSelected = false;
self.m_IsCurrentlyVisible = false;
self.m_IsForceHide = false;
self.m_IsDimmed = false;
self.m_OverrideDimmed = false;
self.m_FogState = 0;
if (g_spec == true) then
self.m_IsCurrentlyVisible = true
self.m_FogState = 2;
end
self.m_Player = Players[playerID];
self.m_UnitID = unitID;
self:SetFlagUnitEmblem();
self:SetInteractivity();
self:UpdateFlagType();
self:UpdateHealth();
self:UpdateName();
self:UpdateReligion();
self:UpdatePosition();
self:UpdateVisibility();
self:UpdateStats();
if( playerID == Game.GetLocalPlayer() ) then
self:UpdateReadyState();
end
self:UpdateHeroGlow();
self:UpdateDimmedState();
self:SetColor(); -- Ensure this happens near the end in case we need to color addon instances like AirUnitInstance
self.m_IsInitialized = true;
end
end
-- ===========================================================================
function OnUnitFlagClick( playerID : number, unitID : number )
local pPlayer = Players[playerID];
if (pPlayer == nil) then return; end
local pUnit = pPlayer:GetUnits():FindID(unitID);
if (pUnit == nil ) then
print("Player clicked a unit flag for unit '"..tostring(unitID).."' but that unit doesn't exist.");
Controls.PanelTop:ForceAnAssertDueToAboveCondition();
return;
end
if (g_spec == true) then
UI.DeselectAllUnits();
UI.DeselectAllCities();
UI.SelectUnit( pUnit );
return
end
if Game.GetLocalPlayer() ~= pUnit:GetOwner() and g_spec == false then
-- Enemy unit; this may start an attack...
-- Does player have a selected unit?
local pSelectedUnit = UI.GetHeadSelectedUnit();
if ( pSelectedUnit ~= nil ) then
local tParameters = {};
tParameters[UnitOperationTypes.PARAM_X] = pUnit:GetX();
tParameters[UnitOperationTypes.PARAM_Y] = pUnit:GetY();
tParameters[UnitOperationTypes.PARAM_MODIFIERS] = UnitOperationMoveModifiers.ATTACK;
if (UnitManager.CanStartOperation( pSelectedUnit, UnitOperationTypes.RANGE_ATTACK, nil, tParameters) ) then
UnitManager.RequestOperation(pSelectedUnit, UnitOperationTypes.RANGE_ATTACK, tParameters);
elseif (UnitManager.CanStartOperation( pSelectedUnit, UnitOperationTypes.MOVE_TO, nil, tParameters) ) then
UnitManager.RequestOperation(pSelectedUnit, UnitOperationTypes.MOVE_TO, tParameters);
end
end
elseif CanSelectUnit(playerID, unitID) then
-- Player's unit; show info:
UI.DeselectAllUnits();
UI.DeselectAllCities();
UI.SelectUnit( pUnit );
end
end
------------------------------------------------------------------
function CanSelectUnit( playerID : number, unitID : number )
if playerID < 0 or m_isMapDeselectDisabled then
return false;
end
-- Only allow a unit selection when in one of the following modes:
local interfaceMode:number = UI.GetInterfaceMode();
if interfaceMode ~= InterfaceModeTypes.SELECTION and
interfaceMode ~= InterfaceModeTypes.MAKE_TRADE_ROUTE and
interfaceMode ~= InterfaceModeTypes.SPY_CHOOSE_MISSION and
interfaceMode ~= InterfaceModeTypes.SPY_TRAVEL_TO_CITY and
interfaceMode ~= InterfaceModeTypes.CITY_RANGE_ATTACK and
interfaceMode ~= InterfaceModeTypes.DISTRICT_RANGE_ATTACK and
interfaceMode ~= InterfaceModeTypes.VIEW_MODAL_LENS then
return false;
end
return Game.GetLocalPlayer() == playerID;
end
------------------------------------------------------------------
-- Set the user interativity for the flag.
function UnitFlag.SetInteractivity( self )
local unitID:number = self.m_UnitID;
local flagPlayerID:number = self.m_Player:GetID();
self.m_Instance.NormalButton:SetVoid1( flagPlayerID );
self.m_Instance.NormalButton:SetVoid2( unitID );
self.m_Instance.NormalButton:RegisterCallback( Mouse.eLClick, OnUnitFlagClick );
self.m_Instance.NormalButton:RegisterCallback( Mouse.eRClick, OnUnitFlagClick );
self.m_Instance.HealthBarButton:SetVoid1( flagPlayerID );
self.m_Instance.HealthBarButton:SetVoid2( unitID );
self.m_Instance.HealthBarButton:RegisterCallback( Mouse.eLClick, OnUnitFlagClick );
self.m_Instance.HealthBarButton:RegisterCallback( Mouse.eRClick, OnUnitFlagClick );
-- Off of the root flag set callbacks to let other UI pieces know that it's focus.
-- This cannot be done on the buttons because enemy flags are disabled and some
-- UI (e.g., CombatPreview) may want to query this.
self.m_Instance.FlagRoot:RegisterMouseEnterCallback( function()
LuaEvents.UnitFlagManager_PointerEntered( flagPlayerID, unitID );
-- Signal to who is handling drawing the arrow lens.
--[[
TODO: Refactor: Currently WorldInput receives the input from the LUAContext
so while the flag is signaled (control gets eHasMouseOver) the input chain
still sends input through to the context.
interfaceMode = UI.GetInterfaceMode();
if interfaceMode == InterfaceModeTypes.CITY_RANGE_ATTACK or
interfaceMode == InterfaceModeTypes.DISTRICT_RANGE_ATTACK then
local pUnit :table = self:GetUnit();
local unitX :number = pUnit:GetX();
local unitY :number = pUnit:GetY();
local pPlot :table = Map.GetPlot( unitX, unitY );
local plotIndex :number = pPlot:GetIndex();
LuaEvents.UnitFlagManager_DrawRangeAttackPreview( plotIndex );
end
]]
end );
self.m_Instance.FlagRoot:RegisterMouseExitCallback( function()
LuaEvents.UnitFlagManager_PointerExited( flagPlayerID, unitID );
end );
end
------------------------------------------------------------------
function UnitFlag.UpdateReadyState( self )
local pUnit : table = self:GetUnit();
if (pUnit ~= nil and pUnit:IsHuman()) then
self:SetDim(not pUnit:IsReadyToSelect());
end
end
------------------------------------------------------------------
function UnitFlag.UpdateStats( self )
local pUnit : table = self:GetUnit();
if (pUnit ~= nil) then
self:UpdateFlagType();
self:UpdateHealth();
self:UpdatePromotions();
self:UpdateAircraftCounter();
end
end
------------------------------------------------------------------
function UnitFlag.UpdateAircraftCounter( self )
local pUnit : table = self:GetUnit();
if (pUnit ~= nil) then
local airUnitCapacity = pUnit:GetAirSlots();
if airUnitCapacity > 0 then
local instance:table = self.m_Instance.AirUnitInstance;
if not instance then
instance = {};
ContextPtr:BuildInstanceForControl("AirUnitInstance", instance, self.m_Instance.FlagRoot);
self.m_Instance.AirUnitInstance = instance;
end
-- Clear previous list entries
instance.UnitListPopup:ClearEntries();
-- Set max capacity
instance.MaxUnitCount:SetText(airUnitCapacity);
local bHasAirUnits, tAirUnits = pUnit:GetAirUnits();
if (bHasAirUnits and tAirUnits ~= nil) then
-- Set current capacity
local numAirUnits = table.count(tAirUnits);
instance.CurrentUnitCount:SetText(numAirUnits);
-- Update unit instances in unit list
for i,unit in ipairs(tAirUnits) do
local unitEntry:table = {};
instance.UnitListPopup:BuildEntry( "UnitListEntry", unitEntry );
-- Update name
unitEntry.UnitName:SetText( Locale.ToUpper(unit:GetName()) );
-- Update icon
local iconInfo:table, iconShadowInfo:table = GetUnitIcon(unit, 22, true);
if iconInfo.textureSheet then
unitEntry.UnitTypeIcon:SetTexture( iconInfo.textureOffsetX, iconInfo.textureOffsetY, iconInfo.textureSheet );
end
-- Update callback
unitEntry.Button:RegisterCallback( Mouse.eLClick, OnUnitSelected );
unitEntry.Button:SetVoid1(unit:GetOwner());
unitEntry.Button:SetVoid2(unit:GetID());
-- Fade out the button icon and text if the unit is not able to move
if unit:IsReadyToMove() then
unitEntry.UnitName:SetAlpha(1.0);
unitEntry.UnitTypeIcon:SetAlpha(1.0);
else
unitEntry.UnitName:SetAlpha(ALPHA_DIM);
unitEntry.UnitTypeIcon:SetAlpha(ALPHA_DIM);
end
end
-- If current air unit count is 0 then disabled popup
if numAirUnits <= 0 then
instance.UnitListPopup:SetDisabled(true);
else
instance.UnitListPopup:SetDisabled(false);
end
instance.UnitListPopup:CalculateInternals();
-- Adjust the scroll panel offset so stack is centered whether scrollbar is visible or not
local scrollPanel = instance.UnitListPopup:GetScrollPanel();
if scrollPanel then
if scrollPanel:GetScrollBar():IsHidden() then
scrollPanel:SetOffsetX(0);
else
scrollPanel:SetOffsetX(7);
end
end
else
-- Set current capacity to 0
instance.CurrentUnitCount:SetText(0);
end
end
end
end
-- ===========================================================================
function OnUnitSelected( playerID:number, unitID:number )
if playerID == Game.GetLocalPlayer() or g_spec == true then -- The local player can only select their own units.
local playerUnits:table = Players[playerID]:GetUnits();
if playerUnits then
local selectedUnit:table = playerUnits:FindID(unitID);
if selectedUnit then
UI.SelectUnit( selectedUnit );
end
end
end
end
------------------------------------------------------------------
-- Set the flag color based on the player colors.
function UnitFlag.SetColor( self )
local primaryColor, secondaryColor = UI.GetPlayerColors( self.m_Player:GetID() );
local teamID = self.m_Player:GetTeam();
local instance:table = self.m_Instance;
instance.FlagBase:SetColor( primaryColor );
instance.HealthBarBG:SetColor( primaryColor );
instance.UnitIcon:SetColor( secondaryColor );
instance.FlagMouseOut:SetColor( secondaryColor );
instance.FlagMouseOver:SetColor( secondaryColor );
if ( teamID == 0 ) then
instance.TeamFlag:SetColor( COLOR_RED );
elseif( teamID == 1 ) then
instance.TeamFlag:SetColor( COLOR_BLUE );
end
for _, playerID in ipairs(PlayerManager.GetAliveMajorIDs()) do
if playerID == self.m_Player:GetID() and ( teamID == 0 or teamID == 1 ) then
instance.TeamFlag:SetHide(false);
break;
else
instance.TeamFlag:SetHide(true);
end
end
-- Set air unit list button color
instance = instance.AirUnitInstance;
if instance then
instance.AirUnitListButton_Base:SetColor( primaryColor );
instance.AirUnitListButtonIcon:SetColor( secondaryColor );
end
end
------------------------------------------------------------------
-- Set the flag texture based on the unit's type
function UnitFlag.SetFlagUnitEmblem( self )
local icon:string = nil;
local pUnit:table = self:GetUnit();
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
icon = iconModifier.OverrideUnitIcon;
end
end
if not icon then
icon = "ICON_"..GameInfo.Units[pUnit:GetUnitType()].UnitType;
end
self.m_Instance.UnitIcon:SetIcon(icon);
end
------------------------------------------------------------------
function UnitFlag.SetDim( self, bDim : boolean )
if (self.m_IsDimmed ~= bDim) then
self.m_IsDimmed = bDim;
self:UpdateDimmedState();
end
end
-----------------------------------------------------------------
-- Set whether or not the dimmed state for the flag is overridden
function UnitFlag.OverrideDimmed( self, bOverride : boolean )
self.m_OverrideDimmed = bOverride;
self:UpdateDimmedState();
end
-----------------------------------------------------------------
-- Set the flag's alpha state, based on the current dimming flags.
function UnitFlag.UpdateDimmedState( self )
if( self.m_IsDimmed and not self.m_OverrideDimmed ) then
self.m_Instance.FlagRoot:SetToEnd(true);
self.m_Instance.FlagRoot:SetAlpha( ALPHA_DIM );
self.m_Instance.HealthBar:SetAlpha( 1.0 / ALPHA_DIM ); -- Health bar doesn't get dimmed, else it is too hard to see.
else
self.m_Instance.FlagRoot:SetAlpha( 1.0 );
self.m_Instance.HealthBar:SetAlpha( 1.0 );
end
end
------------------------------------------------------------------
-- Change the flag's fog state
function UnitFlag.SetFogState( self, fogState : number )
if (g_spec == true) then
self.m_eVisibility = 2
self:SetHide( false )
self.m_FogState = 2
return
end
self.m_eVisibility = fogState;
if (fogState ~= RevealedState.VISIBLE) then
self:SetHide( true );
else
self:SetHide( false );
end
self.m_FogState = fogState;
end
------------------------------------------------------------------
-- Change the flag's overall visibility
function UnitFlag.SetHide( self, bHide : boolean )
local isPreviouslyVisible :boolean = self.m_IsCurrentlyVisible;
self.m_IsCurrentlyVisible = not bHide;
if self.m_IsCurrentlyVisible ~= isPreviouslyVisible then
self:UpdateVisibility();
end
end
------------------------------------------------------------------
-- Change the flag's force hide
function UnitFlag.SetForceHide( self, bHide : boolean )
self.m_IsForceHide = bHide;
self:UpdateVisibility();
end
------------------------------------------------------------------
-- Update the flag's type. This adjust the look of the flag based
-- on the state of the unit.
function UnitFlag.UpdateFlagType( self )
local pUnit = self:GetUnit();
if pUnit == nil then
return;
end
local textureName:string;
-- Make this more data driven. It would be nice to have it so any state the unit could be in could have its own look.
if( pUnit:IsEmbarked() ) then
textureName = TEXTURE_EMBARK;
elseif( pUnit:GetFortifyTurns() > 0 ) then
textureName = TEXTURE_FORTIFY;
elseif( self.m_Style == FLAGSTYLE_CIVILIAN ) then
textureName = TEXTURE_CIVILIAN;
elseif( self.m_Style == FLAGSTYLE_RELIGION ) then
textureName = TEXTURE_RELIGION;
elseif( self.m_Style == FLAGSTYLE_NAVAL) then
textureName = TEXTURE_NAVAL;
elseif( self.m_Style == FLAGSTYLE_SUPPORT) then
textureName = TEXTURE_SUPPORT;
elseif( self.m_Style == FLAGSTYLE_TRADE) then
textureName = TEXTURE_TRADE;
else
textureName = TEXTURE_BASE;
end
self.m_Instance.FlagBase:SetTexture( textureName );
self.m_Instance.FlagMouseOut:SetTexture( textureName );
self.m_Instance.FlagMouseOver:SetTexture( textureName );
self.m_Instance.HealthBarBG:SetTexture( textureName );
self.m_Instance.TeamFlag:SetTexture( textureName );
end
------------------------------------------------------------------
-- Update the health bar.
function UnitFlag.UpdateHealth( self )
local pUnit = self:GetUnit();
if pUnit == nil then
return;
end
local healthPercent = 0;
local maxDamage = pUnit:GetMaxDamage();
if (maxDamage > 0) then
healthPercent = math.max( math.min( (maxDamage - pUnit:GetDamage()) / maxDamage, 1 ), 0 );
end
-- going to damaged state
if( healthPercent < 1 ) then
-- show the bar and the button anim
self.m_Instance.HealthBarBG:SetHide( false );
self.m_Instance.HealthBar:SetHide( false );
self.m_Instance.HealthBarButton:SetHide( false );
-- hide the normal bg and button
self.m_Instance.FlagBase:SetHide( true );
self.m_Instance.NormalButton:SetHide( true );
if ( healthPercent >= HEALTH_PERCENT_GOOD ) then
self.m_Instance.HealthBar:SetColor( COLOR_GREEN );
elseif( healthPercent >= HEALTH_PERCENT_BAD ) then
self.m_Instance.HealthBar:SetColor( COLOR_YELLOW );
else
self.m_Instance.HealthBar:SetColor( COLOR_RED );
end
--------------------------------------------------------------------
-- going to full health
else
self.m_Instance.HealthBar:SetColor( COLOR_GREEN );
-- hide the bar and the button anim
self.m_Instance.HealthBarBG:SetHide( true );
self.m_Instance.HealthBarButton:SetHide( true );
-- show the normal bg and button
self.m_Instance.NormalButton:SetHide( false );
self.m_Instance.FlagBase:SetHide( false );
end
self.m_Instance.HealthBar:SetPercent( healthPercent );
end
------------------------------------------------------------------
-- Update the hero glow.
function UnitFlag.UpdateHeroGlow( self )
local pUnit:table = self:GetUnit();
if pUnit ~= nil then
local unitType:string = GameInfo.Units[pUnit:GetUnitType()].UnitType;
if GameInfo.HeroClasses ~= nil then
for row in GameInfo.HeroClasses() do
if row.UnitType == unitType then
self.m_Instance.HeroGlowInstance = m_HeroGlowIM:GetInstance(self.m_Instance.HeroGlowAnchor);
return;
end
end
end
end
end
------------------------------------------------------------------
-- Update the visibility of the flag based on the current state.
function UnitFlag.UpdateVisibility( self )
if self.m_IsForceHide then
self.m_Instance.Anchor:SetHide(true);
else
self.m_Instance.Anchor:SetHide(false);
if self.m_IsCurrentlyVisible then
self.m_Instance.FlagRoot:ClearEndCallback();
if( self.m_IsDimmed and not self.m_OverrideDimmed ) then
self.m_Instance.FlagRoot:SetToEnd();
self.m_Instance.FlagRoot:SetAlpha( ALPHA_DIM );
else
-- Fade in (show)
self.m_Instance.FlagRoot:SetToBeginning();
self.m_Instance.FlagRoot:Play();
end
else
-- Fade out (hide)
-- One case where a unit flag is first created, if this check isn't done
-- it will pop into existance and then immediately fade out in the FOW.
if self.m_IsInitialized then
self.m_Instance.FlagRoot:RegisterEndCallback(function() self.m_Instance.Anchor:SetHide(not self.m_IsCurrentlyVisible); end);
self.m_Instance.FlagRoot:SetToEnd();
self.m_Instance.FlagRoot:Reverse();
else
self.m_Instance.Anchor:SetHide(true);
end
self.m_Instance.Formation3:SetHide(true);
self.m_Instance.Formation2:SetHide(true);
end
end
end
------------------------------------------------------------------
function GetLevyTurnsRemaining(pUnit : table)
if (pUnit ~= nil) then
if (pUnit:GetCombat() > 0) then
local iOwner = pUnit:GetOwner();
local iOriginalOwner = pUnit:GetOriginalOwner();
if (iOwner ~= iOriginalOwner) then
if (pUnit:GetProperty("LEVY_TURN") ~= nil and pUnit:GetProperty("LEVY_DURATION") ~= nil) then
local levyturn = pUnit:GetProperty("LEVY_TURN");
local levyduration = pUnit:GetProperty("LEVY_DURATION");
local gameturn = Game.GetCurrentGameTurn();
return levyturn + levyduration - gameturn;
end
local pOriginalOwner = Players[iOriginalOwner];
if (pOriginalOwner ~= nil and pOriginalOwner:GetInfluence() ~= nil) then
local iLevyTurnCounter = pOriginalOwner:GetInfluence():GetLevyTurnCounter();
if (iLevyTurnCounter >= 0 and iOwner == pOriginalOwner:GetInfluence():GetSuzerain()) then
return (pOriginalOwner:GetInfluence():GetLevyTurnLimit() - iLevyTurnCounter);
end
end
end
end
end
return -1;
end
------------------------------------------------------------------
function UnitFlag.UpdatePromotions( self )
self.m_Instance.Promotion_Flag:SetHide(true);
local pUnit : table = self:GetUnit();
if pUnit ~= nil then
-- If this unit is levied (ie. from a city-state), showing that takes precedence
local iLevyTurnsRemaining = GetLevyTurnsRemaining(pUnit);
if (iLevyTurnsRemaining >= 0) then
self.m_Instance.UnitNumPromotions:SetText("[ICON_Turn]");
self.m_Instance.Promotion_Flag:SetHide(false);
-- Otherwise, show the experience level
else
local unitExperience = pUnit:GetExperience();
if (unitExperience ~= nil) then
local promotionList :table = unitExperience:GetPromotions();
if (#promotionList > 0) then
--[[
local tooltipString :string = "";
for i, promotion in ipairs(promotionList) do
tooltipString = tooltipString .. Locale.Lookup(GameInfo.UnitPromotions[promotion].Name);
if (i < #promotionList) then
tooltipString = tooltipString .. "[NEWLINE]";
end
end
self.m_Instance.Promotion_Flag:SetToolTipString(tooltipString);
--]]
self.m_Instance.UnitNumPromotions:SetText(#promotionList);
self.m_Instance.Promotion_Flag:SetHide(false);
end
end
end
end
end
------------------------------------------------------------------
-- Update the unit religion indicator icon
function UnitFlag.UpdateReligion( self )
local pUnit : table = self:GetUnit();
if pUnit ~= nil then
local religionType = pUnit:GetReligionType();
if (religionType > 0 and pUnit:GetReligiousStrength() > 0) then
local religion:table = GameInfo.Religions[religionType];
local religionIcon:string = "ICON_" .. religion.ReligionType;
local religionColor:number = UI.GetColorValue(religion.Color);
local religionName:string = Game.GetReligion():GetName(religion.Index);
self.m_Instance.ReligionIcon:SetIcon(religionIcon);
self.m_Instance.ReligionIcon:SetColor(religionColor);
self.m_Instance.ReligionIconBacking:LocalizeAndSetToolTip(religionName);
self.m_Instance.ReligionIconBacking:SetHide(false);
else
self.m_Instance.ReligionIconBacking:SetHide(true);
end
end
end
------------------------------------------------------------------
-- Update the unit name / tooltip
function UnitFlag.UpdateName( self )
local pUnit : table = self:GetUnit();
if pUnit ~= nil then
local unitName = pUnit:GetName();
local pPlayerCfg = PlayerConfigurations[ self.m_Player:GetID() ];
local playername = pPlayerCfg:GetPlayerName()
if g_anon == true and not g_spec then
playername = "Anon_"..self.m_Player:GetID()
end
if playername == nil then
playername = ""
end
local nameString : string;
if(GameConfiguration.IsAnyMultiplayer() and pPlayerCfg:IsHuman()) then
nameString = Locale.Lookup( pPlayerCfg:GetCivilizationShortDescription() ) .. " (" .. playername .. ") - " .. Locale.Lookup( unitName );
else
nameString = Locale.Lookup( pPlayerCfg:GetCivilizationShortDescription() ) .. " - " .. Locale.Lookup( unitName );
end
local pUnitDef = GameInfo.Units[pUnit:GetUnitType()];
if pUnitDef then
local unitTypeName:string = pUnitDef.Name;
if unitName ~= unitTypeName then
nameString = nameString .. " " .. Locale.Lookup("LOC_UNIT_UNIT_TYPE_NAME_SUFFIX", unitTypeName);
end
end
-- display military formation indicator(s)
local militaryFormation = pUnit:GetMilitaryFormation();
if self.m_Style == FLAGSTYLE_NAVAL then
if (militaryFormation == MilitaryFormationTypes.CORPS_FORMATION) then
nameString = nameString .. TXT_UNITFLAG_FLEET_SUFFIX;
elseif (militaryFormation == MilitaryFormationTypes.ARMY_FORMATION) then
nameString = nameString .. TXT_UNITFLAG_ARMADA_SUFFIX;
end
else
if (militaryFormation == MilitaryFormationTypes.CORPS_FORMATION) then
nameString = nameString .. TXT_UNITFLAG_CORPS_SUFFIX;
elseif (militaryFormation == MilitaryFormationTypes.ARMY_FORMATION) then
nameString = nameString .. TXT_UNITFLAG_ARMY_SUFFIX;
end
end
-- DEBUG TEXT FOR SHOWING UNIT ACTIVITY TYPE
--[[
local activityType = UnitManager.GetActivityType(pUnit);
if (activityType == ActivityTypes.ACTIVITY_SENTRY) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_ON_SENTRY;
elseif (activityType == ActivityTypes.ACTIVITY_INTERCEPT) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_ON_INTERCEPT;
elseif (activityType == ActivityTypes.ACTIVITY_AWAKE) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_AWAKE;
elseif (activityType == ActivityTypes.ACTIVITY_HOLD) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_HOLD;
elseif (activityType == ActivityTypes.ACTIVITY_SLEEP) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_SLEEP;
elseif (activityType == ActivityTypes.ACTIVITY_HEAL) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_HEALING;
elseif (activityType == ActivityTypes.NO_ACTIVITY) then
nameString = nameString .. TXT_UNITFLAG_ACTIVITY_NO_ACTIVITY;
end
]]--
-- display archaeology info
local idArchaeologyHomeCity = pUnit:GetArchaeologyHomeCity();
if (idArchaeologyHomeCity ~= 0) then
local pCity = self.m_Player:GetCities():FindID(idArchaeologyHomeCity);
if (pCity ~= nil) then
nameString = nameString .. "[NEWLINE]" .. Locale.Lookup("LOC_UNITFLAG_ARCHAEOLOGY_HOME_CITY", pCity:GetName());
local iGreatWorkIndex = pUnit:GetGreatWorkIndex();
if (iGreatWorkIndex >= 0) then
local eGWType = Game.GetGreatWorkType(iGreatWorkIndex);
local eGWPlayer = Game.GetGreatWorkPlayer(iGreatWorkIndex);
nameString = nameString .. "[NEWLINE]" .. Locale.Lookup("LOC_UNITFLAG_ARCHAEOLOGY_ARTIFACT", GameInfo.GreatWorks[eGWType].Name, PlayerConfigurations[eGWPlayer]:GetPlayerName());
end
end
end
-- display religion info
if (pUnit:GetReligiousStrength() > 0) then
local eReligion = pUnit:GetReligionType();
if (eReligion > 0) then
nameString = nameString .. " (" .. Game.GetReligion():GetName(eReligion) .. ")";
end
end
-- display levy status
local iLevyTurnsRemaining = GetLevyTurnsRemaining(pUnit);
if (iLevyTurnsRemaining >= 0 and PlayerConfigurations[pUnit:GetOriginalOwner()] ~= nil) then
nameString = nameString .. "[NEWLINE]" .. Locale.Lookup("LOC_UNITFLAG_LEVY_ACTIVE", PlayerConfigurations[pUnit:GetOriginalOwner()]:GetPlayerName(), iLevyTurnsRemaining);
end
self.m_Instance.UnitIcon:SetToolTipString( Locale.Lookup(nameString) );
end
end
------------------------------------------------------------------
-- The selection state has changed.
function UnitFlag.UpdateSelected( self, isSelected : boolean )
local pUnit : table = self:GetUnit();
if (pUnit ~= nil) then
self.m_IsSelected = isSelected;
self:OverrideDimmed( isSelected );
end
end
------------------------------------------------------------------
-- Update the position of the flag to match the current unit position.
function UnitFlag.UpdatePosition( self )
local pUnit : table = self:GetUnit();
if (pUnit ~= nil) then
self:SetPosition( UI.GridToWorld( pUnit:GetX(), pUnit:GetY() ) );
end
end
------------------------------------------------------------------
function CanRangeAttack(pCityOrDistrict : table)
-- An invalid plot means we want to know if there are any locations that the city can range strike.
return CityManager.CanStartCommand( pCityOrDistrict, CityCommandTypes.RANGE_ATTACK );
end
-- ===========================================================================
-- Returns a city object immediately left of plot, or NIL if no city there.
-- ===========================================================================
function GetCityPlotLeftOf( x:number, y:number )
local pPlot:table = Map.GetAdjacentPlot( x, y, DirectionTypes.DIRECTION_WEST );
if pPlot == nil then
return nil; --This will happen in non-world-wrapping maps.
end
return Cities.GetCityInPlot( pPlot:GetX(), pPlot:GetY() );
end
-- ===========================================================================
-- Returns a city object immediately right of plot, or NIL if no city there.
-- ===========================================================================
function GetCityPlotRightOf( x:number, y:number )
local pPlot:table = Map.GetAdjacentPlot( x, y, DirectionTypes.DIRECTION_EAST );