-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTradeOverview.lua
843 lines (721 loc) · 32.9 KB
/
TradeOverview.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
-- ===========================================================================
-- INCLUDES
-- ===========================================================================
include("AnimSidePanelSupport");
include("InstanceManager");
include("SupportFunctions");
include("TradeSupport");
-- ===========================================================================
-- CONSTANTS
-- ===========================================================================
local RELOAD_CACHE_ID:string = "TradeOverview"; -- Must be unique (usually the same as the file name)
local DATA_ICON_PREFIX:string = "ICON_";
local TRADE_TABS:table = {
MY_ROUTES = 0;
ROUTES_TO_CITIES = 1;
AVAILABLE_ROUTES = 2;
};
local m_currentTab:number;
local m_showMyBenefits:boolean = true;
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
local m_RouteInstanceIM:table = InstanceManager:new("RouteInstance", "Top", Controls.BodyStack);
local m_HeaderInstanceIM:table = InstanceManager:new("HeaderInstance", "Top", Controls.BodyStack);
local m_SimpleButtonInstanceIM:table = InstanceManager:new("SimpleButtonInstance", "Top", Controls.BodyStack);
local m_AnimSupport:table; -- AnimSidePanelSupport
-- Show My Routes Tab
function ViewMyRoutes()
-- Update Tabs
SetMyRoutesTabSelected(true);
SetRoutesToCitiesTabSelected(false);
SetAvailableRoutesTabSelected(false);
local localPlayerID = Game.GetLocalPlayer();
if (localPlayerID == -1) then
return;
end
-- Update Header
local playerTrade :table = Players[localPlayerID]:GetTrade();
local routesActive :number = playerTrade:GetNumOutgoingRoutes();
local routesCapacity:number = playerTrade:GetOutgoingRouteCapacity();
Controls.HeaderLabel:SetText(Locale.ToUpper("LOC_TRADE_OVERVIEW_MY_ROUTES"));
Controls.ActiveRoutesLabel:SetHide(false);
-- If our active routes exceed our route capacity then color active route number red
local routesActiveText:string = ""
if routesActive > routesCapacity then
routesActiveText = "[COLOR_RED]" .. tostring(routesActive) .. "[ENDCOLOR]";
else
routesActiveText = tostring(routesActive);
end
Controls.ActiveRoutesLabel:SetText(Locale.Lookup("LOC_TRADE_OVERVIEW_ACTIVE_ROUTES", routesActiveText, routesCapacity));
-- Gather data and sort
local routesSortedByPlayer:table = {};
local localPlayerCities:table = Players[Game.GetLocalPlayer()]:GetCities();
for i,city in localPlayerCities:Members() do
local outgoingRoutes = city:GetTrade():GetOutgoingRoutes();
for i,route in ipairs(outgoingRoutes) do
-- Make sure we have a table for each destination player
if routesSortedByPlayer[route.DestinationCityPlayer] == nil then
local routes:table = {};
routesSortedByPlayer[route.DestinationCityPlayer] = {};
end
table.insert(routesSortedByPlayer[route.DestinationCityPlayer], route);
end
end
-- Add routes to local player cities
if routesSortedByPlayer[Game.GetLocalPlayer()] ~= nil then
CreatePlayerHeader(Players[Game.GetLocalPlayer()]);
for i,route in ipairs(routesSortedByPlayer[Game.GetLocalPlayer()]) do
AddRouteFromRouteInfo(route);
end
end
-- Add routes to other civs
local haveAddedCityStateHeader:boolean = false;
for playerID,routes in pairs(routesSortedByPlayer) do
if playerID ~= Game.GetLocalPlayer() then
-- Skip City States as these are added below
local playerInfluence:table = Players[playerID]:GetInfluence();
if not playerInfluence:CanReceiveInfluence() then
CreatePlayerHeader(Players[playerID]);
for i,route in ipairs(routes) do
AddRouteFromRouteInfo(route);
end
else
-- Add city state routes
if not haveAddedCityStateHeader then
haveAddedCityStateHeader = true;
CreateCityStateHeader();
end
for i,route in ipairs(routes) do
AddRouteFromRouteInfo(route);
end
end
end
end
-- Determine how many unused routes we have
local unusedRoutes :number = routesCapacity - routesActive;
if unusedRoutes > 0 then
CreateUnusedRoutesHeader();
local idleTradeUnits:table = GetIdleTradeUnits(Game.GetLocalPlayer());
-- Assign idle trade units to unused routes
for i=1,unusedRoutes,1 do
if #idleTradeUnits > 0 then
-- Add button to choose a route for this trader
AddChooseRouteButton(idleTradeUnits[1]);
table.remove(idleTradeUnits, 1);
else
-- Add button to produce new trade unit
AddProduceTradeUnitButton();
end
end
end
end
-- Show Routes To My Cities Tab
function ViewRoutesToCities()
-- Update Tabs
SetMyRoutesTabSelected(false);
SetRoutesToCitiesTabSelected(true);
SetAvailableRoutesTabSelected(false);
-- Update Header
Controls.HeaderLabel:SetText(Locale.ToUpper("LOC_TRADE_OVERVIEW_ROUTES_TO_MY_CITIES"));
Controls.ActiveRoutesLabel:SetHide(true);
-- Gather data and sort
local routesSortedByPlayer:table = {};
local players = Game.GetPlayers();
for i, player in ipairs(players) do
local playerCities:table = player:GetCities();
for i,city in playerCities:Members() do
local outgoingRoutes = city:GetTrade():GetOutgoingRoutes();
for i,route in ipairs(outgoingRoutes) do
-- Check that the destination city owner is the local palyer
local isDestinationOwnedByLocalPlayer:boolean = false;
if route.DestinationCityPlayer == Game.GetLocalPlayer() then
isDestinationOwnedByLocalPlayer = true;
end
if isDestinationOwnedByLocalPlayer then
-- Make sure we have a table for each destination player
if routesSortedByPlayer[route.OriginCityPlayer] == nil then
local routes:table = {};
routesSortedByPlayer[route.OriginCityPlayer] = {};
end
table.insert(routesSortedByPlayer[route.OriginCityPlayer], route);
end
end
end
end
-- Add routes to stack
for playerID,routes in pairs(routesSortedByPlayer) do
CreatePlayerHeader(Players[playerID]);
for i,route in ipairs(routes) do
AddRouteFromRouteInfo(route);
end
end
end
-- Show Available Routes Tab
function ViewAvailableRoutes()
-- Update Tabs
SetMyRoutesTabSelected(false);
SetRoutesToCitiesTabSelected(false);
SetAvailableRoutesTabSelected(true);
local localPlayerID = Game.GetLocalPlayer();
if (localPlayerID == -1) then
return;
end
local tradeManager:table = Game.GetTradeManager();
-- Update Header
Controls.HeaderLabel:SetText(Locale.ToUpper("LOC_TRADE_OVERVIEW_AVAILABLE_ROUTES"));
Controls.ActiveRoutesLabel:SetHide(true);
-- Gather a list of trade units
local pPlayerUnits:table = Players[localPlayerID]:GetUnits();
local tradeUnitList:table = {};
for i, pUnit in pPlayerUnits:Members() do
-- Ignore trade units that have a pending operation
if not pUnit:HasPendingOperations() then
-- Find Each Trade Unit
local unitInfo:table = GameInfo.Units[pUnit:GetUnitType()];
if unitInfo.MakeTradeRoute == true then
table.insert(tradeUnitList, pUnit);
end
end
end
-- Determine if a trade unit in a city can trade with any other player cities
local hasTradeRouteWithPlayer:boolean = false;
local hasTradeRouteWithCityStates:boolean = false;
local players:table = Game:GetPlayers();
for i, destinationPlayer in ipairs(players) do
local playerHeader:table = nil;
hasTradeRouteWithPlayer = false;
local cities:table = destinationPlayer:GetCities();
for j, destinationCity in cities:Members() do
for k, tradeUnit in ipairs(tradeUnitList) do
local originCity = Cities.GetCityInPlot(tradeUnit:GetX(), tradeUnit:GetY());
if originCity ~= nil then
if tradeManager:CanStartRoute(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID()) then
-- Add Civ/CityState Header
local pPlayerInfluence:table = Players[destinationPlayer:GetID()]:GetInfluence();
if not pPlayerInfluence:CanReceiveInfluence() then
-- If first available route with this city add a city header
if not hasTradeRouteWithPlayer then
hasTradeRouteWithPlayer = true;
CreatePlayerHeader(destinationPlayer);
end
else
-- If first available route to a city state then add a city state header
if not hasTradeRouteWithCityStates then
hasTradeRouteWithCityStates = true;
CreateCityStateHeader();
end
end
-- Add Route
AddRoute(Players[Game.GetLocalPlayer()], originCity, destinationPlayer, destinationCity, tradeUnit:GetID());
end
end
end
end
end
end
-- ===========================================================================
function SetMyRoutesTabSelected( isSelected:boolean )
Controls.MyRoutesButton:SetSelected(isSelected);
Controls.MyRoutesTabLabel:SetHide(isSelected);
Controls.MyRoutesSelected:SetHide(not isSelected);
Controls.MyRoutesSelectedArrow:SetHide(not isSelected);
Controls.MyRoutesTabSelectedLabel:SetHide(not isSelected);
end
-- ===========================================================================
function SetRoutesToCitiesTabSelected( isSelected:boolean )
Controls.RoutesToCitiesButton:SetSelected(isSelected);
Controls.RoutesToCitiesTabLabel:SetHide(isSelected);
Controls.RoutesToCitiesSelected:SetHide(not isSelected);
Controls.RoutesToCitiesSelectedArrow:SetHide(not isSelected);
Controls.RoutesToCitiesTabSelectedLabel:SetHide(not isSelected);
end
-- ===========================================================================
function SetAvailableRoutesTabSelected( isSelected:boolean )
Controls.AvailableRoutesButton:SetSelected(isSelected);
Controls.AvailableRoutesTabLabel:SetHide(isSelected);
Controls.AvailableRoutesSelected:SetHide(not isSelected);
Controls.AvailableRoutesSelectedArrow:SetHide(not isSelected);
Controls.AvailableRoutesTabSelectedLabel:SetHide(not isSelected);
end
-- ===========================================================================
function AddChooseRouteButton( tradeUnit:table )
local simpleButtonInstance:table = m_SimpleButtonInstanceIM:GetInstance();
simpleButtonInstance.GridButton:SetText(Locale.Lookup("LOC_TRADE_OVERVIEW_CHOOSE_ROUTE"));
simpleButtonInstance.GridButton:RegisterCallback( Mouse.eLClick,
function()
SelectUnit( tradeUnit );
end
);
end
-- ===========================================================================
function SelectUnit( unit:table )
local localPlayer = Game.GetLocalPlayer();
if UI.GetHeadSelectedUnit() ~= unit and localPlayer ~= -1 and localPlayer == unit:GetOwner() then
UI.DeselectAllUnits();
UI.DeselectAllCities();
UI.SelectUnit( unit );
end
UI.LookAtPlotScreenPosition( unit:GetX(), unit:GetY(), 0.42, 0.5 );
end
-- ===========================================================================
function AddProduceTradeUnitButton()
local simpleButtonInstance:table = m_SimpleButtonInstanceIM:GetInstance();
simpleButtonInstance.GridButton:SetText(Locale.Lookup("LOC_TRADE_OVERVIEW_PRODUCE_TRADE_UNIT"));
simpleButtonInstance.GridButton:SetDisabled(true);
end
-- ===========================================================================
function AddRouteFromRouteInfo(routeInfo:table)
local originPlayer:table = Players[routeInfo.OriginCityPlayer];
local originCity:table = originPlayer:GetCities():FindID(routeInfo.OriginCityID);
local destinationPlayer:table = Players[routeInfo.DestinationCityPlayer];
local destinationCity:table = destinationPlayer:GetCities():FindID(routeInfo.DestinationCityID);
AddRoute(originPlayer, originCity, destinationPlayer, destinationCity, routeInfo.TraderUnitID);
end
-- ===========================================================================
function AddRoute(originPlayer:table, originCity:table, destinationPlayer:table, destinationCity:table, traderUnitID:number)
local routeInstance:table = m_RouteInstanceIM:GetInstance();
-- Update Route Label
routeInstance.RouteLabel:SetText(Locale.ToUpper(originCity:GetName()) .. " " .. Locale.ToUpper("LOC_TRADE_OVERVIEW_TO") .. " " .. Locale.ToUpper(destinationCity:GetName()));
-- Update Route Yields
routeInstance.ResourceStack:DestroyAllChildren();
routeInstance.NoBenefitsLabel:SetHide(false);
for yieldInfo in GameInfo.Yields() do
local yieldValue = 0;
-- XOR used to show right benefits when origin/destination switch between tabs
if m_showMyBenefits == not (m_currentTab == TRADE_TABS.ROUTES_TO_CITIES) then
yieldValue = GetYieldFromCity(yieldInfo.Index, originCity, destinationCity);
else
yieldValue = GetYieldForDestinationCity(yieldInfo.Index, originCity, destinationCity);
end
if (yieldValue ~= 0 ) then
local resourceInstance:table = {};
ContextPtr:BuildInstanceForControl( "ResourceInstance", resourceInstance, routeInstance.ResourceStack );
resourceInstance.ResourceIconLabel:SetText(yieldInfo.IconString);
resourceInstance.ResourceValueLabel:SetText("+" .. yieldValue);
-- Set tooltip to resouce name
resourceInstance.Top:LocalizeAndSetToolTip(yieldInfo.Name);
-- Update Label Color
if (yieldInfo.YieldType == "YIELD_FOOD") then
resourceInstance.ResourceValueLabel:SetColorByName("ResFoodLabelCS");
elseif (yieldInfo.YieldType == "YIELD_PRODUCTION") then
resourceInstance.ResourceValueLabel:SetColorByName("ResProductionLabelCS");
elseif (yieldInfo.YieldType == "YIELD_GOLD") then
resourceInstance.ResourceValueLabel:SetColorByName("ResGoldLabelCS");
elseif (yieldInfo.YieldType == "YIELD_SCIENCE") then
resourceInstance.ResourceValueLabel:SetColorByName("ResScienceLabelCS");
elseif (yieldInfo.YieldType == "YIELD_CULTURE") then
resourceInstance.ResourceValueLabel:SetColorByName("ResCultureLabelCS");
elseif (yieldInfo.YieldType == "YIELD_FAITH") then
resourceInstance.ResourceValueLabel:SetColorByName("ResFaithLabelCS");
end
routeInstance.NoBenefitsLabel:SetHide(true);
end
end
routeInstance.ResourceStack:CalculateSize();
-- If showing benefits for my city then the destinations religions pressure will be shown
local influencingCity:table = nil;
local influencedCity:table = nil;
-- XOR used to show right benefits when origin/destination switch between tabs
if m_showMyBenefits == not (m_currentTab == TRADE_TABS.ROUTES_TO_CITIES) then
influencingCity = destinationCity;
influencedCity = originCity;
else
influencingCity = originCity;
influencedCity = destinationCity;
end
routeInstance.ReligionPressureContainer:SetHide(true);
--[[
-- Show the religions pressure from the influencing city
local cityReligion = influencingCity:GetReligion();
local majorReligion = cityReligion:GetMajorityReligion();
if majorReligion > 0 then
local religionInfo:table = GameInfo.Religions[majorReligion];
local iconName:string = DATA_ICON_PREFIX .. religionInfo.ReligionType;
local majorityReligionColor:number = UI.GetColorValue(religionInfo.Color);
if majorityReligionColor ~= nil then
routeInstance.ReligionPressureIcon:SetColor(majorityReligionColor);
end
local textureOffsetX, textureOffsetY, textureSheet = IconManager:FindIconAtlas(iconName,22);
if textureOffsetX ~= nil then
routeInstance.ReligionPressureIcon:SetTexture( textureOffsetX, textureOffsetY, textureSheet );
end
routeInstance.ReligionPressureContainer:SetHide(false);
routeInstance.ReligionPressureContainer:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_RELIGIOUS_INFLUENCE", Locale.Lookup(influencingCity:GetName()), Locale.Lookup(religionInfo.Name), Locale.Lookup(influencedCity:GetName()));
else
routeInstance.ReligionPressureContainer:SetHide(true);
end
--]]
-- Update Trading Post Icon
if destinationCity:GetTrade():HasActiveTradingPost(originPlayer) then
routeInstance.TradingPostIndicator:SetAlpha(1.0);
routeInstance.TradingPostIndicator:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_TRADE_POST_ESTABLISHED");
else
routeInstance.TradingPostIndicator:SetAlpha(0.2);
routeInstance.TradingPostIndicator:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_NO_TRADE_POST");
end
-- Update distance to city
local distanceToDestination:number = Map.GetPlotDistance(originCity:GetX(), originCity:GetY(), destinationCity:GetX(), destinationCity:GetY());
routeInstance.RouteDistance:SetText(distanceToDestination);
-- Update Origin Civ Icon
local originPlayerConfig:table = PlayerConfigurations[originPlayer:GetID()];
local originPlayerIconString:string = "ICON_" .. originPlayerConfig:GetCivilizationTypeName();
local textureOffsetX, textureOffsetY, textureSheet = IconManager:FindIconAtlas(originPlayerIconString, 30);
local secondaryColor, primaryColor = UI.GetPlayerColors( originPlayer:GetID() );
routeInstance.OriginCivIcon:SetTexture(textureOffsetX, textureOffsetY, textureSheet);
routeInstance.OriginCivIcon:LocalizeAndSetToolTip( originPlayerConfig:GetCivilizationDescription() );
routeInstance.OriginCivIcon:SetColor( primaryColor );
routeInstance.OriginCivIconBacking:SetColor( secondaryColor );
local destinationPlayerConfig:table = PlayerConfigurations[destinationPlayer:GetID()];
local destinationPlayerInfluence:table = Players[destinationPlayer:GetID()]:GetInfluence();
if not destinationPlayerInfluence:CanReceiveInfluence() then
-- Destination Icon for Civilizations
if destinationPlayerConfig ~= nil then
local iconString:string = "ICON_" .. destinationPlayerConfig:GetCivilizationTypeName();
local textureOffsetX, textureOffsetY, textureSheet = IconManager:FindIconAtlas(iconString, 30);
routeInstance.DestinationCivIcon:SetTexture(textureOffsetX, textureOffsetY, textureSheet);
routeInstance.DestinationCivIcon:LocalizeAndSetToolTip( destinationPlayerConfig:GetCivilizationDescription() );
end
local secondaryColor, primaryColor = UI.GetPlayerColors( destinationPlayer:GetID() );
routeInstance.DestinationCivIcon:SetColor(primaryColor);
routeInstance.DestinationCivIconBacking:SetColor(secondaryColor);
else
-- Destination Icon for City States
if destinationPlayerConfig ~= nil then
local secondaryColor, primaryColor = UI.GetPlayerColors( destinationPlayer:GetID() );
local leader :string = destinationPlayerConfig:GetLeaderTypeName();
local leaderInfo :table = GameInfo.Leaders[leader];
local iconString:string;
if (leader == "LEADER_MINOR_CIV_SCIENTIFIC" or leaderInfo.InheritFrom == "LEADER_MINOR_CIV_SCIENTIFIC") then
iconString = "ICON_CITYSTATE_SCIENCE";
elseif (leader == "LEADER_MINOR_CIV_RELIGIOUS" or leaderInfo.InheritFrom == "LEADER_MINOR_CIV_RELIGIOUS") then
iconString = "ICON_CITYSTATE_FAITH";
elseif (leader == "LEADER_MINOR_CIV_TRADE" or leaderInfo.InheritFrom == "LEADER_MINOR_CIV_TRADE") then
iconString = "ICON_CITYSTATE_TRADE";
elseif (leader == "LEADER_MINOR_CIV_CULTURAL" or leaderInfo.InheritFrom == "LEADER_MINOR_CIV_CULTURAL") then
iconString = "ICON_CITYSTATE_CULTURE";
elseif (leader == "LEADER_MINOR_CIV_MILITARISTIC" or leaderInfo.InheritFrom == "LEADER_MINOR_CIV_MILITARISTIC") then
iconString = "ICON_CITYSTATE_MILITARISTIC";
elseif (leader == "LEADER_MINOR_CIV_INDUSTRIAL" or leaderInfo.InheritFrom == "LEADER_MINOR_CIV_INDUSTRIAL") then
iconString = "ICON_CITYSTATE_INDUSTRIAL";
end
if iconString ~= nil then
local textureOffsetX, textureOffsetY, textureSheet = IconManager:FindIconAtlas(iconString, 30);
routeInstance.DestinationCivIcon:SetTexture(textureOffsetX, textureOffsetY, textureSheet);
routeInstance.DestinationCivIcon:SetColor(primaryColor);
routeInstance.DestinationCivIconBacking:SetColor(secondaryColor);
routeInstance.DestinationCivIcon:LocalizeAndSetToolTip( destinationCity:GetName() );
end
end
end
-- Update Benefits Arrow
-- XOR used to show right benefits when origin/destination switch between tabs
if m_showMyBenefits == not (m_currentTab == TRADE_TABS.ROUTES_TO_CITIES) then
local primaryColor, secondaryColor = UI.GetPlayerColors( originPlayer:GetID() );
routeInstance.OriginCivArrow:SetHide(false);
routeInstance.OriginCivArrow:SetColor(secondaryColor);
routeInstance.DestinationCivArrow:SetHide(true);
else
local primaryColor, secondaryColor = UI.GetPlayerColors( destinationPlayer:GetID() );
routeInstance.OriginCivArrow:SetHide(true);
routeInstance.DestinationCivArrow:SetHide(false);
routeInstance.DestinationCivArrow:SetColor(secondaryColor);
end
-- Update route status icon
if m_currentTab == TRADE_TABS.MY_ROUTES then
routeInstance.RouteStatusFontIcon:SetHide(false);
else
routeInstance.RouteStatusFontIcon:SetHide(true);
end
-- Find trader unit and set button callback to select that unit
local tradeUnit:table = originPlayer:GetUnits():FindID(traderUnitID);
if tradeUnit then
if m_currentTab == TRADE_TABS.AVAILABLE_ROUTES then
-- If selecting an available route, select unit and select route in route chooser
routeInstance.GridButton:RegisterCallback( Mouse.eLClick,
function()
SelectUnit( tradeUnit );
LuaEvents.TradeOverview_SelectRouteFromOverview( destinationPlayer:GetID(), destinationCity:GetID() );
end
);
else
routeInstance.GridButton:RegisterCallback( Mouse.eLClick,
function()
SelectUnit( tradeUnit );
end
);
end
end
end
-- ===========================================================================
function Refresh()
PreRefresh();
if m_currentTab == TRADE_TABS.MY_ROUTES then
ViewMyRoutes();
elseif m_currentTab == TRADE_TABS.ROUTES_TO_CITIES then
ViewRoutesToCities();
elseif m_currentTab == TRADE_TABS.AVAILABLE_ROUTES then
ViewAvailableRoutes();
end
PostRefresh();
end
-- ===========================================================================
function PreRefresh()
-- Reset Stack
m_RouteInstanceIM:ResetInstances();
m_HeaderInstanceIM:ResetInstances();
m_SimpleButtonInstanceIM:ResetInstances();
end
-- ===========================================================================
function PostRefresh()
-- Calculate Stack Sizess
Controls.HeaderStack:CalculateSize();
Controls.HeaderStack:ReprocessAnchoring();
Controls.BodyScrollPanel:CalculateSize();
end
-- ===========================================================================
function SetBenefitsFilter(showMyBenefits:boolean)
if showMyBenefits == nil then
return;
end
m_showMyBenefits = showMyBenefits;
if m_showMyBenefits then
Controls.BenefitsLabel:SetText(Locale.Lookup("LOC_TRADE_OVERVIEW_MY_BENEFITS"));
else
Controls.BenefitsLabel:SetText(Locale.Lookup("LOC_TRADE_OVERVIEW_THEIR_BENEFITS"));
end
Refresh();
end
-- Create Player Header Instance
function CreatePlayerHeader(player:table)
local headerInstance:table = m_HeaderInstanceIM:GetInstance();
local pPlayerConfig:table = PlayerConfigurations[player:GetID()];
headerInstance.HeaderLabel:SetText(Locale.ToUpper(pPlayerConfig:GetPlayerName()));
-- Determine are diplomatic visibility status
local visibilityIndex:number = Players[Game.GetLocalPlayer()]:GetDiplomacy():GetVisibilityOn(player);
-- Determine this player has a trade route with the local player
local hasTradeRoute:boolean = false;
local playerCities:table = player:GetCities();
for i,city in playerCities:Members() do
if city:GetTrade():HasActiveTradingPost(Game.GetLocalPlayer()) then
hasTradeRoute = true;
end
end
-- Display trade route tourism modifier
local baseTourismModifier = GlobalParameters.TOURISM_TRADE_ROUTE_BONUS;
local extraTourismModifier = Players[Game.GetLocalPlayer()]:GetCulture():GetExtraTradeRouteTourismModifier();
-- TODO: Use LOC_TRADE_OVERVIEW_TOURISM_BONUS when we can update the text
headerInstance.TourismBonusPercentage:SetText("+" .. Locale.ToPercent((baseTourismModifier + extraTourismModifier)/100));
if hasTradeRoute then
headerInstance.TourismBonusCheckmark:SetHide(false);
headerInstance.TourismBonusPercentage:SetColorByName("TradeOverviewTextCS");
headerInstance.TourismBonusIcon:SetTexture(0,0,"Tourism_VisitingSmall");
headerInstance.TourismBonusGrid:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_TOURISM_BONUS");
headerInstance.VisibilityBonusCheckmark:SetHide(false);
headerInstance.VisibilityBonusIcon:SetTexture("Diplomacy_VisibilityIcons");
headerInstance.VisibilityBonusIcon:SetVisState(math.min(math.max(visibilityIndex - 1, 0), 3));
headerInstance.VisibilityBonusGrid:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_DIPLOMATIC_VIS_BONUS");
else
headerInstance.TourismBonusCheckmark:SetHide(true);
headerInstance.TourismBonusPercentage:SetColorByName("TradeOverviewTextDisabledCS");
headerInstance.TourismBonusIcon:SetTexture(0,0,"Tourism_VisitingSmallGrey");
headerInstance.TourismBonusGrid:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_NO_TOURISM_BONUS");
headerInstance.VisibilityBonusCheckmark:SetHide(true);
headerInstance.VisibilityBonusIcon:SetTexture("Diplomacy_VisibilityIconsGrey");
headerInstance.VisibilityBonusIcon:SetVisState(math.min(math.max(visibilityIndex, 0), 3));
headerInstance.VisibilityBonusGrid:LocalizeAndSetToolTip("LOC_TRADE_OVERVIEW_TOOLTIP_NO_DIPLOMATIC_VIS_BONUS");
end
end
-- Create City State Header Instance
function CreateCityStateHeader()
local headerInstance:table = m_HeaderInstanceIM:GetInstance();
headerInstance.HeaderLabel:SetText(Locale.ToUpper("LOC_TRADE_OVERVIEW_CITY_STATES"));
headerInstance.VisibilityBonusGrid:SetHide(true);
headerInstance.TourismBonusGrid:SetHide(true);
end
-- Create Unused Routes Header Instance
function CreateUnusedRoutesHeader()
local headerInstance:table = m_HeaderInstanceIM:GetInstance();
headerInstance.HeaderLabel:SetText(Locale.ToUpper("LOC_TRADE_OVERVIEW_UNUSED_ROUTES"));
headerInstance.VisibilityBonusGrid:SetHide(true);
headerInstance.TourismBonusGrid:SetHide(true);
end
-- ===========================================================================
function GetYieldFromCity(yieldIndex:number, originCity:table, destinationCity:table)
local tradeManager = Game.GetTradeManager();
-- From route
local yieldValue = tradeManager:CalculateOriginYieldFromPotentialRoute(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID(), yieldIndex);
-- From path
yieldValue = yieldValue + tradeManager:CalculateOriginYieldFromPath(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID(), yieldIndex);
-- From modifiers
local resourceID = -1;
yieldValue = yieldValue + tradeManager:CalculateOriginYieldFromModifiers(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID(), yieldIndex, resourceID);
return yieldValue;
end
-- ===========================================================================
function GetYieldForDestinationCity(yieldIndex:number, originCity:table, destinationCity:table)
local tradeManager = Game.GetTradeManager();
-- From route
local yieldValue = tradeManager:CalculateDestinationYieldFromPotentialRoute(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID(), yieldIndex);
-- From path
yieldValue = yieldValue + tradeManager:CalculateDestinationYieldFromPath(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID(), yieldIndex);
-- From modifiers
local resourceID = -1;
yieldValue = yieldValue + tradeManager:CalculateDestinationYieldFromModifiers(originCity:GetOwner(), originCity:GetID(), destinationCity:GetOwner(), destinationCity:GetID(), yieldIndex, resourceID);
return yieldValue;
end
-- ===========================================================================
function Open()
-- dont show panel if there is no local player
local localPlayerID = Game.GetLocalPlayer();
if (localPlayerID == -1) then
return
end
m_AnimSupport.Show();
UI.PlaySound("CityStates_Panel_Open");
end
-- ===========================================================================
function Close()
if not ContextPtr:IsHidden() then
UI.PlaySound("CityStates_Panel_Close");
end
m_AnimSupport.Hide();
end
-- ===========================================================================
function OnOpen()
-- Default to My Routes
OnMyRoutesButton();
Open();
end
-- ===========================================================================
function OnMyRoutesButton()
m_currentTab = TRADE_TABS.MY_ROUTES;
Refresh();
end
-- ===========================================================================
function OnRoutesToCitiesButton()
m_currentTab = TRADE_TABS.ROUTES_TO_CITIES;
Refresh();
end
-- ===========================================================================
function OnAvailableRoutesButton()
m_currentTab = TRADE_TABS.AVAILABLE_ROUTES;
Refresh();
end
-- ===========================================================================
function OnClose()
Close();
end
-- ===========================================================================
function OnBenefitsButton()
if m_showMyBenefits then
SetBenefitsFilter(false);
else
SetBenefitsFilter(true);
end
end
-- ===========================================================================
-- LUA Event
-- Explicit close (from partial screen hooks), part of closing everything,
-- ===========================================================================
function OnCloseAllExcept( contextToStayOpen:string )
if contextToStayOpen == ContextPtr:GetID() then return; end
Close();
end
------------------------------------------------------------------------------------------------
function OnLocalPlayerTurnEnd()
if(GameConfiguration.IsHotseat()) then
Close();
end
end
-- ===========================================================================
-- Game Event
-- ===========================================================================
function OnInterfaceModeChanged(eOldMode:number, eNewMode:number)
if eNewMode == InterfaceModeTypes.VIEW_MODAL_LENS then
Close();
end
end
-- ===========================================================================
-- UI EVENT
-- ===========================================================================
function OnInit(isReload:boolean)
if isReload then
LuaEvents.GameDebug_GetValues(RELOAD_CACHE_ID);
end
end
-- ===========================================================================
-- UI EVENT
-- ===========================================================================
function OnShutdown()
LuaEvents.GameDebug_AddValue(RELOAD_CACHE_ID, "isHidden", ContextPtr:IsHidden());
LuaEvents.GameDebug_AddValue(RELOAD_CACHE_ID, "currentTab", m_currentTab);
LuaEvents.GameDebug_AddValue(RELOAD_CACHE_ID, "showMyBenefits", m_showMyBenefits);
end
-- ===========================================================================
-- LUA EVENT
-- Reload support
-- ===========================================================================
function OnGameDebugReturn(context:string, contextTable:table)
if context == RELOAD_CACHE_ID then
if contextTable["showMyBenefits"] ~= nil and contextTable["showMyBenefits"] then
SetBenefitsFilter(true);
else
SetBenefitsFilter(false);
end
if contextTable["isHidden"] ~= nil and not contextTable["isHidden"] then
Open();
end
if contextTable["currentTab"] ~= nil then
m_currentTab = contextTable["currentTab"];
Refresh();
end
end
end
-- ===========================================================================
function OnUnitOperationStarted(ownerID:number, unitID:number, operationID:number)
if m_AnimSupport.IsVisible() and operationID == UnitOperationTypes.MAKE_TRADE_ROUTE then
Refresh();
end
end
-- ===========================================================================
function OnPolicyChanged( ePlayer )
if m_AnimSupport.IsVisible() and ePlayer == Game.GetLocalPlayer() then
Refresh();
end
end
-- ===========================================================================
function Initialize()
-- Control Events
Controls.CloseButton:RegisterCallback(Mouse.eLClick, OnClose);
Controls.CloseButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.MyRoutesButton:RegisterCallback(Mouse.eLClick, OnMyRoutesButton);
Controls.MyRoutesButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.RoutesToCitiesButton:RegisterCallback(Mouse.eLClick, OnRoutesToCitiesButton);
Controls.RoutesToCitiesButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.AvailableRoutesButton:RegisterCallback(Mouse.eLClick, OnAvailableRoutesButton);
Controls.AvailableRoutesButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.BenefitsButton:RegisterCallback(Mouse.eLClick, OnBenefitsButton);
Controls.BenefitsButton:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
-- Lua Events
LuaEvents.PartialScreenHooks_OpenTradeOverview.Add( OnOpen );
LuaEvents.PartialScreenHooks_CloseTradeOverview.Add( OnClose );
LuaEvents.PartialScreenHooks_CloseAllExcept.Add( OnCloseAllExcept );
-- Animation Controller
m_AnimSupport = CreateScreenAnimation(Controls.SlideAnim);
-- Rundown / Screen Events
Events.SystemUpdateUI.Add(m_AnimSupport.OnUpdateUI);
ContextPtr:SetInputHandler(m_AnimSupport.OnInputHandler, true);
Controls.Title:SetText(Locale.Lookup("LOC_TRADE_OVERVIEW_TITLE"));
-- Game Engine Events
Events.UnitOperationStarted.Add( OnUnitOperationStarted );
Events.GovernmentPolicyChanged.Add( OnPolicyChanged );
Events.GovernmentPolicyObsoleted.Add( OnPolicyChanged );
Events.LocalPlayerTurnEnd.Add( OnLocalPlayerTurnEnd );
Events.InterfaceModeChanged.Add( OnInterfaceModeChanged );
-- Hot-Reload Events
ContextPtr:SetInitHandler(OnInit);
ContextPtr:SetShutdown(OnShutdown);
LuaEvents.GameDebug_Return.Add(OnGameDebugReturn);
end
Initialize();