forked from civfanatics/Civ6-UIFiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiplomacyDealView.lua
3050 lines (2478 loc) · 121 KB
/
DiplomacyDealView.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
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
-- ===========================================================================
-- Diplomacy Trade View Manager
-- ===========================================================================
include( "InstanceManager" );
include( "Civ6Common" ); -- AutoSizeGridButton
include( "Colors" );
include( "SupportFunctions" );
include( "PopupDialog" );
include( "ToolTipHelper_PlayerYields" );
include( "CivilizationIcon" );
include( "GreatWorksSupport" );
-- ===========================================================================
-- VARIABLES
-- ===========================================================================
local ms_PlayerPanelIM :table = InstanceManager:new( "PlayerAvailablePanel", "Root" );
local ms_IconOnlyIM :table = InstanceManager:new( "IconOnly", "SelectButton", Controls.IconOnlyContainer );
local ms_IconAndTextIM :table = InstanceManager:new( "IconAndText", "SelectButton", Controls.IconAndTextContainer );
local ms_LeftRightListIM :table = InstanceManager:new( "LeftRightList", "List", Controls.LeftRightListContainer );
local ms_TopDownListIM :table = InstanceManager:new( "TopDownList", "List", Controls.TopDownListContainer );
local ms_AgreementOptionIM :table = InstanceManager:new( "AgreementOptionInstance", "AgreementOptionButton", Controls.ValueEditStack );
local ms_CityDetailsIM :table = InstanceManager:new("CityIconAndDetails", "CityDetailsContainer", Controls.IconAndTextContainer);
local ms_MinimizedSectionIM :table = InstanceManager:new( "MinimizedSection","MinimizedSectionContainer" );
local m_uiMyOffers :table = {};
local m_uiTheirOffers :table = {};
local ms_ValueEditDealItemID = -1; -- The ID of the deal item that is being value edited.
local ms_ValueEditDealItemControlTable = nil; -- The control table of the deal item that is being edited.
local OTHER_PLAYER = 0;
local LOCAL_PLAYER = 1;
local ms_LocalPlayerPanel = {};
local ms_OtherPlayerPanel = {};
local ms_LocalPlayer = nil;
local ms_OtherPlayer = nil;
local ms_OtherPlayerID = -1;
local ms_OtherPlayerIsHuman = false;
local ms_InitiatedByPlayerID = -1;
ms_bIsDemand = false;
local ms_bExiting = false;
local ms_LastIncomingDealProposalAction = DealProposalAction.PENDING;
local m_kPopupDialog :table; -- Will use custom "popup" since in leader mode the Popup stack is disabled.
local AvailableDealItemGroupTypes = {};
AvailableDealItemGroupTypes.GOLD = 1;
AvailableDealItemGroupTypes.LUXURY_RESOURCES = 2;
AvailableDealItemGroupTypes.STRATEGIC_RESOURCES = 3;
AvailableDealItemGroupTypes.AGREEMENTS = 4;
AvailableDealItemGroupTypes.CITIES = 5;
AvailableDealItemGroupTypes.OTHER_PLAYERS = 6;
AvailableDealItemGroupTypes.GREAT_WORKS = 7;
AvailableDealItemGroupTypes.CAPTIVES = 8;
AvailableDealItemGroupTypes.COUNT = 8;
local ms_AvailableGroups = {};
-----------------------
local DealItemGroupTypes = {};
DealItemGroupTypes.GOLD = 1;
DealItemGroupTypes.RESOURCES = 2;
DealItemGroupTypes.AGREEMENTS = 3;
DealItemGroupTypes.CITIES = 4;
DealItemGroupTypes.GREAT_WORKS = 5;
DealItemGroupTypes.CAPTIVES = 6;
DealItemGroupTypes.COUNT = 6;
local ms_DealGroups = {};
local ms_DealAgreementsGroup = {};
local ms_DefaultOneTimeGoldAmount = 100;
local ms_DefaultMultiTurnGoldAmount = 10;
local ms_DefaultMultiTurnGoldDuration = 30;
local ms_bForceUpdateOnCommit = false;
local ms_bDontUpdateOnBack = false;
local MAX_DEAL_ITEM_EDIT_HEIGHT = 300;
local m_kCollapsedCityDetails : table = {};
--Pane resizing variables
local m_StartingMouseX : number;
local m_StartingMouseY : number;
local m_OffersStartSizeY: number;
local m_InvStartSizeY : number;
local m_MaxDragResizeY : number = 81;
local m_MinDragResizeY : number = 193;
local DRAG_BUTTON_OFFSET_Y : number = 20;
-- ===========================================================================
function SetIconToSize(iconControl, iconName, iconSize)
if iconSize == nil then
iconSize = 50;
end
local x, y, szIconName, iconSize = IconManager:FindIconAtlasNearestSize(iconName, iconSize, true);
iconControl:SetTexture(x, y, szIconName);
iconControl:SetSizeVal(iconSize, iconSize);
end
-- ===========================================================================
function InitializeDealGroups()
for i = 1, AvailableDealItemGroupTypes.COUNT, 1 do
ms_AvailableGroups[i] = {};
end
for i = 1, DealItemGroupTypes.COUNT, 1 do
ms_DealGroups[i] = {};
end
end
InitializeDealGroups();
-- ===========================================================================
function GetPlayerType(player : table)
if (player:GetID() == ms_LocalPlayer:GetID()) then
return LOCAL_PLAYER;
end
return OTHER_PLAYER;
end
-- ===========================================================================
function GetPlayerOfType(playerType : number)
if (playerType == LOCAL_PLAYER) then
return ms_LocalPlayer;
end
return ms_OtherPlayer;
end
-- ===========================================================================
function GetOtherPlayer(player : table)
if (player ~= nil and player:GetID() == ms_OtherPlayer:GetID()) then
return ms_LocalPlayer;
end
return ms_OtherPlayer;
end
-- ===========================================================================
function SetDefaultLeaderDialogText()
if (ms_bIsDemand == true and ms_InitiatedByPlayerID == ms_OtherPlayerID) then
SetLeaderDialog("LOC_DIPLO_DEMAND_INTRO", "");
else
SetLeaderDialog("LOC_DIPLO_DEAL_INTRO", "");
end
end
-- ===========================================================================
function ProposeWorkingDeal(bIsAutoPropose : boolean)
if (bIsAutoPropose == nil) then
bIsAutoPropose = false;
end
if (not DealManager.HasPendingDeal(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID())) then
if (ms_bIsDemand) then
DealManager.SendWorkingDeal(DealProposalAction.DEMANDED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
else
if (bIsAutoPropose) then
DealManager.SendWorkingDeal(DealProposalAction.INSPECT, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
else
DealManager.SendWorkingDeal(DealProposalAction.PROPOSED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
end
end
end
end
-- ===========================================================================
function RequestEqualizeWorkingDeal()
if (not DealManager.HasPendingDeal(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID())) then
DealManager.SendWorkingDeal(DealProposalAction.EQUALIZE, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
end
end
-- ===========================================================================
function DealIsEmpty()
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal == nil or pDeal:GetItemCount() == 0) then
return true;
end
return false;
end
-- ===========================================================================
-- Update the proposed working deal. This is called as items are changed in the deal.
-- It is primarily used to 'auto-propose' the deal when working with an AI.
function UpdateProposedWorkingDeal()
if (ms_LastIncomingDealProposalAction ~= DealProposalAction.PENDING or IsAutoPropose()) then
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal == nil or pDeal:GetItemCount() == 0 or ms_bIsDemand) then
-- Is a demand or no items, restart
ms_LastIncomingDealProposalAction = DealProposalAction.PENDING;
UpdateDealStatus();
else
if (IsAutoPropose()) then
ProposeWorkingDeal(true);
end
end
end
end
-- ===========================================================================
function UpdateOtherPlayerText(otherPlayerSays)
local bHide = true;
if (ms_OtherPlayer ~= nil and otherPlayerSays ~= nil) then
local playerConfig = PlayerConfigurations[ms_OtherPlayer:GetID()];
if (playerConfig ~= nil) then
-- leader icon
local otherPlayerController = CivilizationIcon:AttachInstance(Controls.OtherPlayerBubbleIcon);
otherPlayerController:UpdateIconFromPlayerID(ms_OtherPlayer:GetID());
-- Set the leader name
local leaderDesc = playerConfig:GetLeaderName();
Controls.OtherPlayerBubbleName:SetText(Locale.ToUpper(Locale.Lookup("LOC_DIPLOMACY_DEAL_OTHER_PLAYER_SAYS", leaderDesc)));
end
end
-- When we get dialog for what the leaders say during a trade, we can add it here!
end
-- ===========================================================================
function OnToggleCollapseGroup(iconList : table)
if (iconList.ListStack:IsHidden()) then
iconList.ListStack:SetHide(false);
else
iconList.ListStack:SetHide(true);
end
iconList.List:CalculateSize();
end
-- ===========================================================================
function CreateHorizontalGroup(rootStack : table, title : string)
local iconList = ms_LeftRightListIM:GetInstance(rootStack);
if (title == nil or title == "") then
iconList.Title:SetHide(true); -- No title
else
iconList.TitleText:LocalizeAndSetText(title);
end
iconList.List:CalculateSize();
return iconList;
end
-- ===========================================================================
function CreateVerticalGroup(rootStack : table, title : string)
local iconList = ms_TopDownListIM:GetInstance(rootStack);
if (title == nil or title == "") then
iconList.Title:SetHide(true); -- No title
else
iconList.TitleText:LocalizeAndSetText(title);
end
iconList.List:CalculateSize();
return iconList;
end
-- ===========================================================================
function CreatePlayerAvailablePanel(playerType : number, rootControl : table)
--local playerPanel = ms_PlayerPanelIM:GetInstance(rootControl);
ms_AvailableGroups[AvailableDealItemGroupTypes.GOLD][playerType] = CreateHorizontalGroup(rootControl);
ms_AvailableGroups[AvailableDealItemGroupTypes.LUXURY_RESOURCES][playerType] = CreateHorizontalGroup(rootControl, "LOC_DIPLOMACY_DEAL_LUXURY_RESOURCES");
ms_AvailableGroups[AvailableDealItemGroupTypes.STRATEGIC_RESOURCES][playerType] = CreateHorizontalGroup(rootControl, "LOC_DIPLOMACY_DEAL_STRATEGIC_RESOURCES");
ms_AvailableGroups[AvailableDealItemGroupTypes.AGREEMENTS][playerType] = CreateVerticalGroup(rootControl, "LOC_DIPLOMACY_DEAL_AGREEMENTS");
ms_AvailableGroups[AvailableDealItemGroupTypes.CITIES][playerType] = CreateVerticalGroup(rootControl, "LOC_DIPLOMACY_DEAL_CITIES");
ms_AvailableGroups[AvailableDealItemGroupTypes.OTHER_PLAYERS][playerType] = CreateVerticalGroup(rootControl, "LOC_DIPLOMACY_DEAL_OTHER_PLAYERS");
ms_AvailableGroups[AvailableDealItemGroupTypes.GREAT_WORKS][playerType] = CreateVerticalGroup(rootControl, "LOC_DIPLOMACY_DEAL_GREAT_WORKS");
ms_AvailableGroups[AvailableDealItemGroupTypes.CAPTIVES][playerType] = CreateVerticalGroup(rootControl, "LOC_DIPLOMACY_DEAL_CAPTIVES");
rootControl:CalculateSize();
return playerPanel;
end
-- ===========================================================================
function CreatePlayerDealPanel(playerType : number, rootControl : table)
--This creates the containers for the offer area...
--ms_DealGroups[DealItemGroupTypes.RESOURCES][playerType] = CreateHorizontalGroup(rootControl);
--ms_DealGroups[DealItemGroupTypes.AGREEMENTS][playerType] = CreateVerticalGroup(rootControl);
--**********************************************************************
-- Currently putting them all in the same control.
ms_DealGroups[DealItemGroupTypes.RESOURCES][playerType] = rootControl;
ms_DealGroups[DealItemGroupTypes.AGREEMENTS][playerType] = rootControl;
ms_DealGroups[DealItemGroupTypes.CITIES][playerType] = rootControl;
ms_DealGroups[DealItemGroupTypes.GREAT_WORKS][playerType] = rootControl;
ms_DealGroups[DealItemGroupTypes.CAPTIVES][playerType] = rootControl;
end
-- ===========================================================================
function OnValuePulldownCommit(forType)
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
local pDealItem = pDeal:FindItemByID(ms_ValueEditDealItemID);
if (pDealItem ~= nil) then
pDealItem:SetValueType( forType );
local valueName = pDealItem:GetValueTypeNameID();
if (ms_ValueEditDealItemControlTable ~= nil) then
-- Keep the text on the icon, that is currently hidden, up to date too.
ms_ValueEditDealItemControlTable.ValueText:LocalizeAndSetText(pDealItem:GetValueTypeNameID(valueName));
end
UpdateDealStatus();
UpdateProposedWorkingDeal();
end
end
Controls.ValueEditPopupBackground:SetHide(true);
end
-- ===========================================================================
function SetValueText(uiIcon, pDealItem)
if (uiIcon.ValueText ~= nil) then
local valueName = pDealItem:GetValueTypeNameID();
if (valueName == nil) then
if (pDealItem:HasPossibleValues()) then
valueName = "LOC_DIPLOMACY_DEAL_CLICK_TO_CHANGE_DEAL_PARAMETER";
end
end
if (valueName ~= nil) then
uiIcon.ValueText:LocalizeAndSetText(valueName);
uiIcon.ValueText:SetHide(false);
else
uiIcon.ValueText:SetHide(true);
end
end
end
-- ===========================================================================
function CreatePanels()
-- Create the Other Player Panels
CreatePlayerAvailablePanel(OTHER_PLAYER, Controls.TheirInventoryStack);
-- Create the Local Player Panels
CreatePlayerAvailablePanel(LOCAL_PLAYER, Controls.MyInventoryStack);
CreatePlayerDealPanel(OTHER_PLAYER, m_uiTheirOffers);
CreatePlayerDealPanel(LOCAL_PLAYER, m_uiMyOffers);
Controls.EqualizeDeal:RegisterCallback( Mouse.eLClick, OnEqualizeDeal );
Controls.EqualizeDeal:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.AcceptDeal:RegisterCallback( Mouse.eLClick, OnProposeOrAcceptDeal );
Controls.AcceptDeal:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.DemandDeal:RegisterCallback( Mouse.eLClick, OnProposeOrAcceptDeal );
Controls.DemandDeal:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.RefuseDeal:RegisterCallback(Mouse.eLClick, OnRefuseDeal);
Controls.RefuseDeal:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
Controls.ResumeGame:RegisterCallback(Mouse.eLClick, OnResumeGame);
Controls.ResumeGame:RegisterCallback( Mouse.eMouseEnter, function() UI.PlaySound("Main_Menu_Mouse_Over"); end);
end
-- ===========================================================================
-- Find the 'instance' table from the control
function FindIconInstanceFromControl(rootControl : table)
if (rootControl ~= nil) then
local controlTable = ms_IconOnlyIM:FindInstanceByControl(rootControl);
if (controlTable == nil) then
controlTable = ms_IconAndTextIM:FindInstanceByControl(rootControl);
end
return controlTable;
end
return nil;
end
-- ===========================================================================
-- Show or hide the "amount text" or the "Value Text" sub-control of the supplied control instance
function SetHideValueText(controlTable : table, bHide : boolean)
if (controlTable ~= nil) then
if (controlTable.AmountText ~= nil) then
controlTable.AmountText:SetHide(bHide);
end
if (controlTable.ValueText ~= nil) then
controlTable.ValueText:SetHide(bHide);
end
end
end
-- ===========================================================================
-- Detach the value edit overlay from anything it is attached to.
function ClearValueEdit()
SetHideValueText(ms_ValueEditDealItemControlTable, false);
ms_ValueEditDealItemControlTable = nil
ms_ValueEditDealItemID = -1;
end
-- ===========================================================================
-- Is the deal a gift to the other player?
function IsGiftToOtherPlayer()
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil and not ms_bIsDemand and pDeal:IsValid()) then
local itemsFromLocal : number = pDeal:GetItemCount(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
local itemsFromOther : number = pDeal:GetItemCount(ms_OtherPlayer:GetID(), ms_LocalPlayer:GetID());
if (itemsFromLocal > 0 and itemsFromOther == 0) then
return true;
end
end
return false;
end
-- ===========================================================================
function UpdateDealStatus()
local bDealValid = false;
ClearValueEdit();
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
if (pDeal:GetItemCount() > 0) then
bDealValid = true;
end
end
if (bDealValid) then
if pDeal:Validate() ~= DealValidationResult.VALID then
bDealValid = false;
end
end
Controls.EqualizeDeal:SetHide(ms_bIsDemand);
-- Have we sent out a deal?
local bHasPendingDeal = DealManager.HasPendingDeal(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (not bHasPendingDeal and ms_LastIncomingDealProposalAction == DealProposalAction.PENDING) then
-- We have yet to send out a deal.
Controls.AcceptDeal:SetHide(true);
local showDemand = bDealValid and ms_bIsDemand;
Controls.DemandDeal:SetHide(not showDemand);
else
local cantAccept = (ms_LastIncomingDealProposalAction ~= DealProposalAction.ACCEPTED and ms_LastIncomingDealProposalAction ~= DealProposalAction.PROPOSED and ms_LastIncomingDealProposalAction ~= DealProposalAction.ADJUSTED) or not bDealValid or bHasPendingDeal;
Controls.AcceptDeal:SetHide(cantAccept);
if (ms_bIsDemand) then
if (ms_LocalPlayer:GetID() == ms_InitiatedByPlayerID) then
-- Local human is making a demand
if (ms_LastIncomingDealProposalAction == DealProposalAction.ACCEPTED) then
Controls.DemandDeal:SetHide(cantAccept);
-- The other player has accepted the demand, but we must enact it.
-- We won't have the human need to press the accept button, just do it and exit.
OnProposeOrAcceptDeal();
return;
else
Controls.AcceptDeal:SetHide(true);
Controls.DemandDeal:SetHide(false);
end
else
Controls.DemandDeal:SetHide(true);
end
else
Controls.DemandDeal:SetHide(true);
end
end
-- Hide/show directions if either side has no items
local itemsFromLocal : number = pDeal:GetItemCount(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
local itemsFromOther : number = pDeal:GetItemCount(ms_OtherPlayer:GetID(), ms_LocalPlayer:GetID());
m_uiMyOffers.DirectionsBracket:SetHide( itemsFromLocal > 0);
m_uiTheirOffers.DirectionsBracket:SetHide( itemsFromOther > 0);
UpdateProposalButtons(bDealValid);
ResizeDealAndButtons();
end
-- ===========================================================================
function ResizeDealAndButtons()
-- Find the widest deal button text and size others to match
local refuseX, refuseY = AutoSizeGridButton(Controls.RefuseDeal,200,32,10,"1");
local equalizeX, equalizeY = AutoSizeGridButton(Controls.EqualizeDeal,200,32,10,"1");
local acceptX, acceptY = AutoSizeGridButton(Controls.AcceptDeal,200,41,10,"1");
local minX = refuseX;
if not Controls.EqualizeDeal:IsHidden() and minX < equalizeX then
minX = equalizeX;
end
if not Controls.AcceptDeal:IsHidden() and minX < acceptX then
minX = acceptX;
end
if Controls.RefuseDeal:GetSizeX() < minX then
Controls.RefuseDeal:SetSizeX(minX);
end
if Controls.EqualizeDeal:GetSizeX() < minX then
Controls.EqualizeDeal:SetSizeX(minX);
end
if Controls.AcceptDeal:GetSizeX() < minX then
Controls.AcceptDeal:SetSizeX(minX);
end
Controls.DealOptionsStack:CalculateSize();
m_uiTheirOffers.OfferStack:CalculateSize();
Controls.TheirOfferScroll:CalculateSize();
m_uiMyOffers.OfferStack:CalculateSize();
Controls.MyOfferScroll:CalculateSize();
end
-- ===========================================================================
-- The Human has ask to have the deal equalized. Well, what the AI is
-- willing to take.
function OnEqualizeDeal()
ClearValueEdit();
RequestEqualizeWorkingDeal();
end
-- ===========================================================================
-- Propose the deal, if this is the first time, or accept it, if the other player has
-- accepted it.
function OnProposeOrAcceptDeal()
ClearValueEdit();
if (ms_LastIncomingDealProposalAction == DealProposalAction.PENDING or
ms_LastIncomingDealProposalAction == DealProposalAction.REJECTED or
ms_LastIncomingDealProposalAction == DealProposalAction.EQUALIZE_FAILED) then
ProposeWorkingDeal();
UpdateDealStatus();
UI.PlaySound("Confirm_Bed_Positive");
else
if (ms_LastIncomingDealProposalAction == DealProposalAction.ACCEPTED or ms_LastIncomingDealProposalAction == DealProposalAction.PROPOSED or ms_LastIncomingDealProposalAction == DealProposalAction.ADJUSTED) then
-- Any adjustments?
if (DealManager.AreWorkingDealsEqual(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID())) then
-- Yes, we can accept
-- if deal will trigger war, prompt user before confirming deal
local sendDealAndContinue = function()
-- Send the deal. This will also send out a POSITIVE response statement
DealManager.SendWorkingDeal(DealProposalAction.ACCEPTED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
OnContinue();
UI.PlaySound("Confirm_Bed_Positive");
end;
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
local pJointWarItem = pDeal:FindItemByType(DealItemTypes.AGREEMENTS, DealAgreementTypes.JOINT_WAR);
if DealAgreementTypes.JOINT_WAR and pJointWarItem then
local iWarType = pJointWarItem:GetParameterValue("WarType");
if (iWarType == nil) then iWarType = WarTypes.FORMAL_WAR; end
local targetPlayerID = pJointWarItem:GetValueType();
if (targetPlayerID >= 0) then
LuaEvents.DiplomacyActionView_ConfirmWarDialog(ms_LocalPlayer:GetID(), targetPlayerID, iWarType, sendDealAndContinue);
else
UI.DataError("Invalid Player ID to declare Joint War to: " .. targetPlayerID);
end
else
local pThirdPartyWarItem = pDeal:FindItemByType(DealItemTypes.AGREEMENTS, DealAgreementTypes.THIRD_PARTY_WAR);
if (DealAgreementTypes.THIRD_PARTY_WAR and pThirdPartyWarItem) then
local iWarType = pThirdPartyWarItem:GetParameterValue("WarType");
if (iWarType == nil) then iWarType = WarTypes.FORMAL_WAR; end
local targetPlayerID = pThirdPartyWarItem:GetValueType();
if (targetPlayerID >= 0) then
LuaEvents.DiplomacyActionView_ConfirmWarDialog(ms_LocalPlayer:GetID(), targetPlayerID, iWarType, sendDealAndContinue);
else
UI.DataError("Invalid Player ID to declare Third Party War to: " .. targetPlayerID);
end
else
sendDealAndContinue();
end
end
else
-- No, send an adjustment and stay in the deal view.
DealManager.SendWorkingDeal(DealProposalAction.ADJUSTED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
UpdateDealStatus();
end
end
end
end
-- ===========================================================================
function OnRefuseDeal(bForceClose)
if (bForceClose == nil) then
bForceClose = false;
end
local bHasPendingDeal = DealManager.HasPendingDeal(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
local sessionID = DiplomacyManager.FindOpenSessionID(Game.GetLocalPlayer(), ms_OtherPlayer:GetID());
if (sessionID ~= nil) then
if (not ms_OtherPlayerIsHuman and not bHasPendingDeal) then
-- Refusing an AI's deal
ClearValueEdit();
if (ms_InitiatedByPlayerID == ms_OtherPlayerID) then
-- AI started this, so tell them that we don't want the deal
if (bForceClose == true) then
-- Forcing the close, usually because the turn timer expired
DealManager.SendWorkingDeal(DealProposalAction.REJECTED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
DiplomacyManager.CloseSession(sessionID);
StartExitAnimation();
else
DiplomacyManager.AddResponse(sessionID, Game.GetLocalPlayer(), "NEGATIVE");
end
else
-- Else close the session
DiplomacyManager.CloseSession(sessionID);
StartExitAnimation();
end
else
if (ms_OtherPlayerIsHuman) then
if (bHasPendingDeal) then
-- Canceling the deal with the other player.
DealManager.SendWorkingDeal(DealProposalAction.CLOSED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
else
if (ms_InitiatedByPlayerID ~= Game.GetLocalPlayer()) then
-- Refusing the deal with the other player.
DealManager.SendWorkingDeal(DealProposalAction.REJECTED, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
end
end
DiplomacyManager.CloseSession(sessionID);
StartExitAnimation();
end
end
else
-- We have lost our session!
if (not ContextPtr:IsHidden()) then
if (not ms_bExiting) then
OnResumeGame();
end
end
end
end
-- ===========================================================================
function OnResumeGame()
-- Exiting back to wait for a response
ClearValueEdit();
local sessionID = DiplomacyManager.FindOpenSessionID(Game.GetLocalPlayer(), ms_OtherPlayer:GetID());
if (sessionID ~= nil) then
DiplomacyManager.CloseSession(sessionID);
end
-- Start the exit animation, it will call OnContinue when complete
StartExitAnimation();
end
-- ===========================================================================
function OnExitFadeComplete()
if(Controls.TradePanelFade:IsReversing()) then
Controls.TradePanelFade:SetSpeed(2);
Controls.TradePanelSlide:SetSpeed(2);
OnContinue();
end
end
Controls.TradePanelFade:RegisterEndCallback(OnExitFadeComplete);
-- ===========================================================================
-- Change the value number edit by a delta
function OnValueAmountEditDelta(dealItemID:number, delta:number)
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
local pDealItem = pDeal:FindItemByID(dealItemID);
if (pDealItem ~= nil) then
local iNewAmount = tonumber(Controls.ValueAmountEditBox:GetText() or 0) + delta;
iNewAmount = clip(iNewAmount, 1, pDealItem:GetMaxAmount());
Controls.ValueAmountEditBox:SetText(tostring(iNewAmount));
end
end
end
-- ===========================================================================
-- Detach the value edit if it is attached to the control
function DetachValueEdit(itemID: number)
if (itemID == ms_ValueEditDealItemID) then
ClearValueEdit();
end
end
-- ===========================================================================
-- Reattach the value edit overlay to the control set it is editing.
function ReAttachValueEdit()
if (ms_ValueEditDealItemControlTable ~= nil) then
SetHideValueText(ms_ValueEditDealItemControlTable, true);
-- Display the number in the value edit field
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
local pDealItem = pDeal:FindItemByID(ms_ValueEditDealItemID);
if (pDealItem ~= nil) then
local itemID = pDealItem:GetID();
local itemType = pDealItem:GetType();
if (itemType == DealItemTypes.GOLD or itemType == DealItemTypes.RESOURCES) then
-- Hide/show everything for GOLD and RESOURCE options
ms_AgreementOptionIM:ResetInstances();
Controls.ValueEditIconGrid:SetHide(false);
Controls.ValueAmountEditBoxContainer:SetHide(false);
local iDuration = pDealItem:GetDuration();
Controls.ValueEditHeaderLabel:SetText(Locale.Lookup("LOC_DIPLOMACY_DEAL_HOW_MANY"));
if (iDuration == 0) then
---- One time
Controls.ValueEditValueText:SetHide(true);
else
---- Multi-turn
Controls.ValueEditValueText:LocalizeAndSetText("LOC_DIPLOMACY_DEAL_FOR_TURNS", iDuration);
Controls.ValueEditValueText:SetHide(false);
end
if (itemType == DealItemTypes.GOLD) then
SetIconToSize(Controls.ValueEditIcon, "ICON_YIELD_GOLD_5");
elseif (itemType == DealItemTypes.RESOURCES) then
local resourceType = pDealItem:GetValueType();
local resourceDesc = GameInfo.Resources[resourceType];
SetIconToSize(Controls.ValueEditIcon, "ICON_" .. resourceDesc.ResourceType);
end
Controls.ValueEditAmountText:SetText(tostring(pDealItem:GetAmount()));
Controls.ValueEditAmountText:SetHide(false);
Controls.ValueAmountEditBox:SetText(tostring(pDealItem:GetAmount()));
Controls.ValueEditButton:RegisterCallback( Mouse.eLClick, function() OnValueEditButton(itemID); end );
Controls.ValueAmountEditLeftButton:RegisterCallback( Mouse.eLClick, function() OnValueAmountEditDelta(itemID, -1); end );
Controls.ValueAmountEditRightButton:RegisterCallback( Mouse.eLClick, function() OnValueAmountEditDelta(itemID, 1); end );
elseif (itemType == DealItemTypes.AGREEMENTS) then
local subType = pDealItem:GetSubType();
local iDuration = pDealItem:GetDuration();
ShowAgreementOptionPopup(subType, iDuration, pDealItem:GetFromPlayerID());
else
-- The value of the item cannot be adjusted so don't show a popup
return;
end
end
end
ResizeValueEditScrollPanel();
Controls.ValueEditPopupBackground:SetHide(false);
end
end
-- ===========================================================================
-- Attach the value edit overlay to a control set.
function AttachValueEdit(rootControl : table, dealItemID : number)
ClearValueEdit();
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
local pDealItem = pDeal:FindItemByID(dealItemID);
if (pDealItem ~= nil) then
-- Do we have something to edit?
if (pDealItem:HasPossibleValues() or pDealItem:HasPossibleAmounts()) then
-- Yes
ms_ValueEditDealItemControlTable = FindIconInstanceFromControl(rootControl);
ms_ValueEditDealItemID = dealItemID;
ReAttachValueEdit();
end
end
end
end
-- ===========================================================================
-- Update the deal panel for a player
function UpdateDealPanel(player)
-- If we modify the deal without sending it to the AI then reset the status to PENDING
ms_LastIncomingDealProposalAction = DealProposalAction.PENDING;
UpdateDealStatus();
PopulatePlayerDealPanel(m_uiTheirOffers, ms_OtherPlayer);
PopulatePlayerDealPanel(m_uiMyOffers, ms_LocalPlayer);
ResizeDealAndButtons();
end
-- ===========================================================================
function OnClickAvailableOneTimeGold(player, addAmount : number)
if (ms_bIsDemand == true and ms_InitiatedByPlayerID == ms_OtherPlayerID) then
-- Can't modifiy demand that is not ours
return;
end
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
local pPlayerTreasury = player:GetTreasury();
local bFound = false;
-- Already there?
local dealItems = pDeal:FindItemsByType(DealItemTypes.GOLD, DealItemSubTypes.NONE, player:GetID());
local pDealItem;
if (dealItems ~= nil) then
for i, pDealItem in ipairs(dealItems) do
if (pDealItem:GetDuration() == 0) then
-- Already have a one time gold. Up the amount
addAmount = pDealItem:GetAmount() + addAmount;
addAmount = clip(addAmount, nil, pDealItem:GetMaxAmount());
if (addAmount ~= pDealItem:GetAmount()) then
pDealItem:SetAmount(addAmount);
bFound = true;
break;
else
return; -- No change, just exit
end
end
end
end
-- Doesn't exist yet, add it.
if (not bFound) then
-- Going to add anything?
pDealItem = pDeal:AddItemOfType(DealItemTypes.GOLD, player:GetID());
if (pDealItem ~= nil) then
-- Set the duration, so the max amount calculation knows what we are doing
pDealItem:SetDuration(0);
-- Adjust the gold to our max
addAmount = clip(addAmount, nil, pDealItem:GetMaxAmount());
if (addAmount > 0) then
pDealItem:SetAmount(addAmount);
bFound = true;
else
-- It is empty, remove it.
local itemID = pDealItem:GetID();
pDeal:RemoveItemByID(itemID);
end
end
end
if (bFound) then
UpdateProposedWorkingDeal();
UpdateDealPanel(player);
end
end
end
-- ===========================================================================
function OnClickAvailableMultiTurnGold(player, addAmount : number, iDuration : number)
if (ms_bIsDemand == true and ms_InitiatedByPlayerID == ms_OtherPlayerID) then
-- Can't modifiy demand that is not ours
return;
end
local pDeal = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (pDeal ~= nil) then
local pPlayerTreasury = player:GetTreasury();
local bFound = false;
UI.PlaySound("UI_GreatWorks_Put_Down");
-- Already there?
local dealItems = pDeal:FindItemsByType(DealItemTypes.GOLD, DealItemSubTypes.NONE, player:GetID());
local pDealItem;
if (dealItems ~= nil) then
for i, pDealItem in ipairs(dealItems) do
if (pDealItem:GetDuration() ~= 0) then
-- Already have a multi-turn gold. Up the amount
addAmount = pDealItem:GetAmount() + addAmount;
addAmount = clip(addAmount, nil, pDealItem:GetMaxAmount());
if (addAmount ~= pDealItem:GetAmount()) then
pDealItem:SetAmount(addAmount);
bFound = true;
break;
else
return; -- No change, just exit
end
end
end
end
-- Doesn't exist yet, add it.
if (not bFound) then
-- Going to add anything?
pDealItem = pDeal:AddItemOfType(DealItemTypes.GOLD, player:GetID());
if (pDealItem ~= nil) then
-- Set the duration, so the max amount calculation knows what we are doing
pDealItem:SetDuration(iDuration);
-- Adjust the gold to our max
addAmount = clip(addAmount, nil, pDealItem:GetMaxAmount());
if (addAmount > 0) then
pDealItem:SetAmount(addAmount);
bFound = true;
else
-- It is empty, remove it.
local itemID = pDealItem:GetID();
pDeal:RemoveItemByID(itemID);
end
end
end
if (bFound) then
UpdateProposedWorkingDeal();
UpdateDealPanel(player);
end
end
end
-- ===========================================================================
-- Clip val to be within the range of min and max
function clip(val: number, min: number, max: number)
if min and val < min then
val = min;
elseif max and val > max then
val = max;
end
return val;
end
-- ===========================================================================
-- Check to see if the deal should be auto-proposed.
function IsAutoPropose()
if (not ms_OtherPlayerIsHuman) then
local pDeal : table = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
pDeal:Validate();
if (pDeal ~= nil and not ms_bIsDemand and pDeal:IsValid() and not DealManager.HasPendingDeal(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID())) then
local itemsFromLocal : number = pDeal:GetItemCount(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
local itemsFromOther : number = pDeal:GetItemCount(ms_OtherPlayer:GetID(), ms_LocalPlayer:GetID());
if (itemsFromLocal > 0 or itemsFromOther > 0) then
return true;
end
end
end
return false;
end
-- ===========================================================================
-- Check the state of the deal and show/hide the special proposal buttons
function UpdateProposalButtons(bDealValid)
local bDealIsPending : boolean = DealManager.HasPendingDeal(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
if (bDealValid and (not bDealIsPending or not ms_OtherPlayerIsHuman)) then
Controls.ResumeGame:SetHide(true);
local pDeal : table = DealManager.GetWorkingDeal(DealDirection.OUTGOING, ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
Controls.EqualizeDeal:SetHide(ms_bIsDemand);
if (pDeal ~= nil) then
local itemsFromLocal : number = pDeal:GetItemCount(ms_LocalPlayer:GetID(), ms_OtherPlayer:GetID());
local itemsFromOther : number = pDeal:GetItemCount(ms_OtherPlayer:GetID(), ms_LocalPlayer:GetID());
if (not ms_bIsDemand) then