-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerLogic.cpp
More file actions
1443 lines (1108 loc) · 34 KB
/
Copy pathPlayerLogic.cpp
File metadata and controls
1443 lines (1108 loc) · 34 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
#include <cassert>
#include <UI/UI.h>
#include <WorldLogic.h>
#include "PlayerLogic.h"
#include <PolicyTemplate.h>
#include <ExpeditionTemplate.h>
#include <CityTemplate.h>
#include <Platform/NumberConverter.h>
#include <Messages/All.h>
#include "PoliciesManipulator.h"
PlayerLogic::PlayerLogic() {
}
const DiscoveryCollection& PlayerLogic::GetDiscoveries() const {
return _discoveries;
}
void PlayerLogic::AddTaxLevel(int count) {
if (count == 0)
return;
SetTax(_curTax + count);
}
void PlayerLogic::AddTrackLevel(ProgressTrackType type, int count) {
for (int i = 0; i < count; ++i) {
auto oldValue = GetTrack(type);
if (oldValue == max_city_track_level)
return;
auto newValue = oldValue + 1;
SetTrack(type, newValue);
GetTrackUpgradePerk(type, oldValue)(*this);
_progressState.TrackIncreased();
}
}
void PlayerLogic::AddEconomyLevel(int count) {
assert(count >= 0);
AddTrackLevel(ProgressTrackType::Economy, count);
}
void PlayerLogic::AddCulture(int count) {
assert(count >= 0);
AddTrackLevel(ProgressTrackType::Culture, count);
CultureChangedMessage msg(GetId(), GetCultureLevel());
Notify(msg);
}
void PlayerLogic::AddMilitaryLevel(int count) {
assert(count >= 0);
AddTrackLevel(ProgressTrackType::Military, count);
MilitaryChangedMessage msg(_id, GetMilitaryLevel());
Notify(msg);
_progressState.TrackIncreased();
}
void PlayerLogic::AddMoney(int count) {
if (count == 0)
return;
_curMoney += count;
{
AddMoneyMessage msg{ GetId(), count };
Notify(msg);
}
{
MoneyChangedMessage evt{ _id, _curMoney };
Notify(evt);
}
}
player_color PlayerLogic::GetColor() const {
return _color;
}
void PlayerLogic::SetColor(player_color color) {
_color = color;
PlayerColorSelectedMessage msg{ GetId(), color };
Notify(msg);
}
Cities PlayerLogic::GetCity() const {
return _city;
}
void PlayerLogic::SetCity(Cities value) {
_city = value;
CitySelectedMessage msg{ GetId(), _city };
Notify(msg);
}
void PlayerLogic::UpgradeCity() {
assert(CanUpgradeCity());
++_curCityLevel;
auto& effect = GetCityTemplate(_city).GetUpgradeEffect(_curCityLevel);
CityLevelChangedMessage msg{ _id, _curCityLevel };
Notify(msg);
effect.Apply(*this);
}
bool PlayerLogic::CanUpgradeCity() {
if (_curCityLevel == -1)
return true;
if (_curCityLevel >= 4)
return false;
return GetCityTemplate(_city).GetUpgradeEffect(_curCityLevel).CanApply(*this);
}
void PlayerLogic::SetId(int id) {
_id = id;
}
int PlayerLogic::GetDiscoveryCount(discovery_type type) const {
auto& d = GetDiscoveries();
return std::count(d.begin(), d.end(), type);
}
bool PlayerLogic::HasRedDiscovery(int count) const {
return count <= GetRedDiscovery();
}
bool PlayerLogic::HasBlueDiscovery(int count) const {
return count <= GetBlueDiscovery();
}
bool PlayerLogic::HasGreenDiscovery(int count) const {
return count <= GetGreenDiscovery();
}
int PlayerLogic::GetRedDiscovery() const {
return GetDiscoveryCount(discovery_type::red) +
GetDiscoveryCount(discovery_type::epic_red);
}
int PlayerLogic::GetBlueDiscovery() const {
return GetDiscoveryCount(discovery_type::blue) +
GetDiscoveryCount(discovery_type::epic_blue);
}
int PlayerLogic::GetGreenDiscovery() const {
return GetDiscoveryCount(discovery_type::green) +
GetDiscoveryCount(discovery_type::epic_green);
}
bool PlayerLogic::HasMoney(int count) const {
return _curMoney >= count;
}
int PlayerLogic::GetId() const {
return _id;
}
void PlayerLogic::RemoveMoney(int count) {
if (count == 0)
return;
_curMoney -= count;
assert(_curMoney >= 0);
{
RemoveMoneyMessage msg{ GetId(), count };
Notify(msg);
}
{
MoneyChangedMessage msg{ GetId(), _curMoney };
Notify(msg);
}
}
int PlayerLogic::GetPhilosophyCount() const {
return _philosophyCount;
}
void PlayerLogic::AddPhilosophy(int count) {
if (count == 0)
return;
_philosophyCount += count;
PhilosophyAddedMessage msg{ GetId(), count };
Notify(msg);
}
void PlayerLogic::RemovePhilosophy(int count) {
if (count == 0)
return;
_philosophyCount -= count;
assert(_philosophyCount >= 0);
PhilosophyRemovedMessage msg{ GetId(), count };
Notify(msg);
}
void PlayerLogic::AddTradePerk(PlayerPerk perk) {
_tradePerks.push_back(perk);
}
void PlayerLogic::AddPolicyPerk(PlayerPerk perk) {
_policyPerks.push_back(perk);
}
void PlayerLogic::AddPoints(int count) {
if (count == 0)
return;
SetPoints(_curPoints + count);
}
int PlayerLogic::GetPoints() const {
return _curPoints;
}
void PlayerLogic::RemovePoints(int count) {
if (count == 0 || _curPoints == 0)
return;
_curPoints -= count;
_curPoints = std::max(0, _curPoints);
ScorePointsChangedMessage msg(GetId(), _curPoints);
Notify(msg);
}
void PlayerLogic::AddArmy(int count, bool allowOverflow) {
assert(count >= 0);
if (count == 0)
return;
SetArmy(_curArmy + count, allowOverflow);
}
void PlayerLogic::RemoveArmy(int count) {
assert(count >= 0);
if (count == 0)
return;
_curArmy -= count;
_curArmy = std::max(0, std::min(max_army_level, _curArmy));
ArmyChangedMessage msg(GetId(), _curArmy);
Notify(msg);
}
void PlayerLogic::AddGlory(int count) {
assert(count >= 0);
if (count == 0)
return;
SetGlory(_curGlory + count);
}
void PlayerLogic::RemoveGlory(int count) {
assert(count >= 0);
if (count == 0)
return;
_curGlory -= count;
_curGlory = std::max(0, std::min(max_glory_level, _curGlory));
GloryChangedMessage msg(GetId(), _curGlory);
Notify(msg);
}
int PlayerLogic::GetPoliciesInActiveDeckCount() const {
return static_cast<int>(_policiesInActiveDeck.size());
}
PoliciesType PlayerLogic::GetPolicyInActiveDeck(int index) const {
assert(index >= 0 && index < _policiesInActiveDeck.size());
return _policiesInActiveDeck[index];
}
int PlayerLogic::GetPoliciesInHandCount() const {
return static_cast<int>(_policiesInHands.size());
}
void PlayerLogic::AddEndGamePerk(PlayerPerk perk) {
_endGamePerks.push_back(perk);
}
void PlayerLogic::AddProgressCostPerk(ProgressCostPerk perk) {
_progressCostPerks.push_back(perk);
}
void PlayerLogic::AddAvailableProgress(int count) {
_availableProgressCount += count;
}
void PlayerLogic::RemoveAvailableProgress(int count) {
_availableProgressCount -= count;
assert(_availableProgressCount >= 1);
}
void PlayerLogic::AddCulturePerk(PlayerPerk perk) {
_culturePerks.push_back(perk);
}
void PlayerLogic::SetActionCallback(ActionType type, ActionCompleteCallback cb) {
const auto index = As<int>(type);
assert(_actionCallbacks[index] == nullptr);
_actionCallbacks[index] = cb;
}
void PlayerLogic::DoPhilosophy(ActionCompleteCallback cb) {
INFO("Player {}: DoPhilosophy", _id);
SetActionCallback(ActionType::Philosophy, cb);
AddPhilosophy(1);
for (auto& perk : _philosophyPerks) {
perk(*this);
}
InvokeActionCallback(ActionType::Philosophy);
}
void PlayerLogic::DoCulture(ActionCompleteCallback cb) {
AddPoints(GetCultureLevel());
for (auto& perk : _culturePerks) {
perk(*this);
}
cb();
}
void PlayerLogic::DoTrade(ActionCompleteCallback cb) {
AddMoney(GetEconomy() + 1);
for (auto& perk : _tradePerks) {
perk(*this);
}
cb();
}
void PlayerLogic::BeginDoMilitary(ActionCompleteCallback cb) {
BeginActiveTask("military");
SetActionCallback(ActionType::Military, cb);
AddArmy(GetMilitaryLevel(), true);
BeginExpedition([this]() {
EndDoMilitary();
});
}
void PlayerLogic::EndDoMilitary() {
for (auto& perk : _militaryPerks) {
perk(*this);
}
InvokeActionCallback(ActionType::Military);
EndActiveTask("military");
}
void PlayerLogic::BeginExpedition(ActionCompleteCallback cb) {
if (cb) {
assert(_expeditionCallback == nullptr);
_expeditionCallback = cb;
}
BeginActiveTask("expedition");
SelectExpeditionMessage msg{ _id };
Notify(msg);
}
bool PlayerLogic::CanGoExpedition(int expeditionId) const {
if (expeditionId < 0)
return true;
auto expedition = GetExpeditionTemplate(expeditionId);
return GetArmySize() >= expedition.armyRequire;
}
void PlayerLogic::EndExpedition(int expeditionId) {
if (expeditionId >= 0) {
auto expedition = GetExpeditionTemplate(expeditionId);
assert(GetArmySize() >= expedition.armyRequire);
auto loses = GetLosses(expedition.armyLosses);
RemoveArmy(loses);
AddMoney(expedition.rewardMoney);
AddPoints(expedition.rewardPoints);
for (int i = 0; i < 3; ++i) {
auto d = expedition.discoveries[i];
if (d != discovery_type::no) {
AddDiscovery(d, expeditionId, i, -1);
}
}
_world->CompleteExpedition(_id, expeditionId);
}
if (_expeditionCallback) {
_expeditionCallback();
_expeditionCallback = nullptr;
}
EndActiveTask("expedition");
}
bool PlayerLogic::CanActivatePolicy(::PoliciesType policy) const {
if (policy == PoliciesType::policy_unknown)
return true;
auto& p = GetPolicyTemplate(policy);
return p.GetEffect().CanApply(*this);
}
//
//void PlayerLogic::BeginActivatePolicy(ActionCompleteCallback cb) {
// INFO("Player {} begin policy activation", GetId());
//
// BeginActiveTask();
// SetActionCallback(ActionType::Policy, cb);
//
// SelectPolicyFromHandsMessage msg{ GetId() };
// Notify(msg);
//}
void PlayerLogic::ActivatePolicy(::PoliciesType policy) {
if (policy == ::PoliciesType::policy_unknown)
return;
auto& p = GetPolicyTemplate(policy);
assert(p.GetEffect().CanApply(*this));
p.GetEffect().Apply(*this);
RemovePolicy(policy);
{
auto index = As<int>(_policiesInActiveDeck.size());
_policiesInActiveDeck.push_back(policy);
PolicyAddedToPlayedMessage msg{ GetId(), policy, index };
Notify(msg);
}
{
PolicyActivatedMessage msg{ GetId(), policy };
Notify(msg);
}
for (auto& perk : _policyPerks) {
perk(*this);
}
// InvokeActionCallback(ActionType::Policy);
//EndActiveTask();
}
void PlayerLogic::DoExpand(ActionCompleteCallback cb) {
if (CanUpgradeCity()) {
UpgradeCity();
}
else {
WARN("Player {} select city upgrade but can't do that", GetId());
}
cb();
}
int PlayerLogic::GetCultureLevel() const {
return _cityTracks[(int)ProgressTrackType::Culture] + 1;
}
void PlayerLogic::AddMilitaryLossesPerk(MilitaryLossesPerk perk) {
_militaryLossesPerks.push_back(perk);
}
void PlayerLogic::AddMilitaryPerk(PlayerPerk perk) {
_militaryPerks.push_back(perk);
}
int PlayerLogic::GetMilitaryLevel() const {
return _cityTracks[(int)ProgressTrackType::Military] + 1;
}
int PlayerLogic::GetLosses(int baseLosses) {
for (auto& perk : _militaryLossesPerks) {
baseLosses = perk(baseLosses);
}
return std::max(0, baseLosses);
}
int PlayerLogic::GetArmySize() const {
return _curArmy;
}
void PlayerLogic::AddDiscovery(discovery_type type, int fromExpedition, int indexInExpedition, int selectionIndex) {
auto index = As<int>(_discoveries.size());
_discoveries.push_back(type);
DiscoveryAddedMessage msg(GetId(), type, index, fromExpedition, indexInExpedition, selectionIndex);
Notify(msg);
}
void PlayerLogic::RemoveDiscovery(discovery_type type) {
auto it = std::find(_discoveries.begin(), _discoveries.end(), type);
if (it != _discoveries.end()) {
auto index = std::distance(_discoveries.begin(), it);
_discoveries.erase(it);
DiscoveryRemovedMessage msg(GetId(), type, index);
Notify(msg);
}
}
void PlayerLogic::AddAnyMomentPerk(PlayerPerk perk) {
_anyMomentPerks.push_back(perk);
}
int PlayerLogic::GetGlory() const {
return _curGlory;
}
int PlayerLogic::GetSmallDiscoveries() const {
return GetDiscoveryCount(discovery_type::blue) + GetDiscoveryCount(discovery_type::red) + GetDiscoveryCount(discovery_type::green);
}
void PlayerLogic::AddPopulation(int count) {
assert(count >= 0);
if (count == 0)
return;
SetPopulation(_curPopulation + count);
}
void PlayerLogic::RemovePopulation(int count) {
assert(count >= 0);
if (count == 0)
return;
SetPopulation(_curPopulation - count);
}
int PlayerLogic::GetPopulation() const {
return _curPopulation;
}
int PlayerLogic::GetEconomy() const {
return _cityTracks[(int)ProgressTrackType::Economy] + 1;
}
void PlayerLogic::AddLawPerk(PlayerPerk perk) {
_lawPerks.push_back(perk);
}
int PlayerLogic::GetMoney() const {
return _curMoney;
}
void PlayerLogic::AddDiscoveryCostTradePerk(DiscoveryCostPerk perk) {
_discoveryCostPerks.push_back(perk);
}
int PlayerLogic::GetTax() const {
return _curTax;
}
void PlayerLogic::AddPhilosophyPerk(PlayerPerk perk) {
_philosophyPerks.push_back(perk);
}
void PlayerLogic::AddPostProgressPerk(PlayerPerk perk) {
_postProgressPerks.push_back(perk);
}
bool PlayerLogic::HasUpgradedAnyTrack() const {
return _progressState.HasUpgradedTracks();
}
void PlayerLogic::AddUpgradePerk(PlayerPerk perk) {
_upgradePerks.push_back(perk);
}
void PlayerLogic::AddTaxPerk(PlayerPerk perk) {
_taxPerks.push_back(perk);
}
void PlayerLogic::SetWorld(WorldLogic* logic) {
_world = logic;
}
const WorldLogic& PlayerLogic::GetWorld() const {
return *_world;
}
WorldLogic& PlayerLogic::GetWorld() {
return *_world;
}
void PlayerLogic::AddProgressPerk(PlayerPerk perk) {
_progressPerks.push_back(perk);
}
void PlayerLogic::UnlockDice() {
assert(_dicesCount == 2);
_dicesCount++;
DiceUnlockedMessage msg{ GetId() };
Notify(msg);
}
int PlayerLogic::GetTrackProgressCost(ProgressTrackType type, int discont, bool extraUpgrade) const {
assert(CanUpgradeTrack(type, discont, extraUpgrade));
auto baseCost = std::max(0, GetTrackUpgradeBaseCost(type, GetTrack(type)) - discont);
for (auto& perk : _progressCostPerks) {
baseCost = perk(type, baseCost);
}
return baseCost;
}
void PlayerLogic::SetTrack(ProgressTrackType type, int value) {
_cityTracks[(int)type] = value;
if (type == ProgressTrackType::Economy) {
EconomyChangedMessage msg(GetId(), value);
Notify(msg);
}
else if (type == ProgressTrackType::Culture) {
CultureChangedMessage msg(GetId(), value);
Notify(msg);
}
else if (type == ProgressTrackType::Military) {
MilitaryChangedMessage msg(GetId(), value);
Notify(msg);
}
}
int PlayerLogic::GetTrack(ProgressTrackType type) const {
return _cityTracks[(int)type];
}
const PlayerPerk& PlayerLogic::GetTrackUpgradePerk(ProgressTrackType type, int level) const {
auto& city = GetCityTemplate(_city);
return city.GetTrackPerk(type, level);
}
int PlayerLogic::GetTrackUpgradeBaseCost(ProgressTrackType type, int level) const {
auto& city = GetCityTemplate(_city);
return city.GetTrackUpgradeCost(type, level);
}
bool PlayerLogic::CanUpgradeTrack(ProgressTrackType type, int discont, bool extraUpgrade) const {
if (type == ProgressTrackType::Unknown) {
// Upgrade was skipped
return true;
}
auto level = GetTrack(type);
if (level == max_city_track_upgrades)
return false;
if (!extraUpgrade) {
if (_progressState.tracksLeft == 0) {
// further upgrades require philosophies
if (GetPhilosophyCount() == 0)
return false;
}
}
auto baseCost = std::max(0, GetTrackUpgradeBaseCost(type, level) - discont);
for (auto& perk : _progressCostPerks) {
baseCost = perk(type, baseCost);
}
if (baseCost > GetMoney())
return false;
return true;
}
void PlayerLogic::UpgradeTrack(ProgressTrackType type, int discont, bool extraUpgrade) {
assert(CanUpgradeTrack(type, discont, extraUpgrade));
if (type != ProgressTrackType::Unknown) {
auto cost = GetTrackProgressCost(type, discont, extraUpgrade);
RemoveMoney(cost);
if (!extraUpgrade) {
if (_progressState.tracksLeft == 0) {
assert(GetPhilosophyCount() > 0);
RemovePhilosophy(1);
}
}
AddTrackLevel(type, 1);
if (_progressState.tracksLeft > 0) {
_progressState.tracksLeft--;
}
}
else {
if (!extraUpgrade) {
CompleteProgressStep();
}
}
}
int PlayerLogic::GetDiceCount() const {
return _dicesCount;
}
int PlayerLogic::GetDice(int index) const {
return _currentDices[index];
}
void PlayerLogic::DropPolicy(int index) {
INFO("Player {} is dropping policy at index {}", _id, index);
assert(index >= 0 && index < _policiesInHands.size());
auto it = _policiesInHands.begin();
std::advance(it, index);
auto policy = *it;
RemovePolicy(policy);
PolicyDroppedMessage msg(GetId(), policy);
Notify(msg);
}
//
//void PlayerLogic::DropDiscovery(discovery_type discovery) {
// auto it = std::find(_discoveries.begin(), _discoveries.end(), discovery);
// assert(it != _discoveries.end());
//
// _discoveries.erase(it);
//
// DiscoveryDroppedMessage msg{ GetId(), discovery };
// Notify(msg);
//}
void PlayerLogic::AddPolicy(::PoliciesType policy, bool selectFlag) {
_policiesInHands.push_back(policy);
PolicyAddedInHandsMessage msg{ GetId(), policy, selectFlag };
Notify(msg);
}
//void PlayerLogic::SetState(PlayerState state) {
// assert(_state != state);
// _state = state;
//}
bool PlayerLogic::IsIdle() const {
return _activeTasks == 0; // _state == PlayerState::Idle;
}
//Policies* PlayerLogic::TakeLawPolicies(Policies a, Policies b) {
// AddPolicy()
// _lawPolicySlot[0] = a;
// _lawPolicySlot[1] = b;
//
// return &_lawPolicySlot[0];
//}
void PlayerLogic::InvokeActionCallback(ActionType type) {
const auto index = As<int>(type);
assert(_actionCallbacks[index] != nullptr);
_actionCallbacks[index]();
_actionCallbacks[index] = nullptr;
}
void PlayerLogic::EndDoLaw(::PoliciesType selected, ::PoliciesType dropped) {
if (selected != PoliciesType::policy_unknown) {
AddPolicy(selected, false);
}
if (dropped != PoliciesType::policy_unknown) {
_world->Policies().Drop(dropped);
}
AddPopulation(LAW_ACTION_POPULATION_GROWTH);
InvokeActionCallback(ActionType::Law);
EndActiveTask("law");
}
void PlayerLogic::BeginDoLaw(ActionCompleteCallback cb) {
// assert(IsIdle());
// SetState(PlayerState::SelectPolicy);
BeginActiveTask("law");
auto a = PoliciesType::policy_unknown;
if (!World().Policies().CanTake()) {
a = World().Policies().GetNextPolicy();
}
auto b = PoliciesType::policy_unknown;
if (!World().Policies().CanTake()) {
b = World().Policies().GetNextPolicy();
}
assert(_actionCallbacks[As<int>(ActionType::Law)] == nullptr);
_actionCallbacks[As<int>(ActionType::Law)] = cb;
SelectLawPolicyMessage msg{ _id, a, b };
Notify(msg);
/*ui().SelectPolicy(GetId(), TakeLawPolicies(a, b), 2,
[cb](PlayerLogic& v, Policies* p, int count, int selected) {
v.AddPolicy(p[selected]);
v.SetState(PlayerState::Idle);
cb();
});*/
}
WorldLogic& PlayerLogic::World() {
return *_world;
}
void PlayerLogic::BeginDraft() {
BeginActiveTask("draft");
assert(_policiesInHands.size() == 5);
for (int i = 0; i < As<int>(_policiesInHands.size()); ++i) {
PolicyRemovedFromHandsMessage msg{ GetId(), _policiesInHands[i], 0};
Notify(msg);
}
GiveDraft(std::move(_policiesInHands));
assert(_policiesInHands.empty());
assert(_policiesInDraft.size() == 5);
BeginDraftMessage msg{ GetId() };
Notify(msg);
}
void PlayerLogic::EndDraft() {
EndActiveTask("draft");
}
void PlayerLogic::TakePolicyFromDraft(::PoliciesType policy) {
auto it = std::find(_policiesInDraft.begin(), _policiesInDraft.end(), policy);
assert(_policiesInDraft.end() != it);
auto index = static_cast<int>(std::distance(_policiesInDraft.begin(), it));
_policiesInDraft.erase(it);
PolicyRemovedFromDraftMessage msg{ GetId(), policy, index };
Notify(msg);
AddPolicy(policy, false);
}
std::vector<PoliciesType> PlayerLogic::TakeDraft() {
assert(!_policiesInDraft.empty());
for (int i = 0; i < As<int>(_policiesInDraft.size()); ++i) {
PolicyRemovedFromDraftMessage msg{ GetId(), _policiesInDraft[i], 0 };
Notify(msg);
}
return std::move(_policiesInDraft);
}
void PlayerLogic::GiveDraft(std::vector<::PoliciesType>&& deck) {
assert(!deck.empty());
if (deck.size() == 1) {
AddPolicy(deck.front(), false);
}
else {
for (auto p : deck) {
_policiesInDraft.push_back(p);
PolicyAddedToDraftMessage msg{ GetId(), p, static_cast<int>(_policiesInDraft.size() - 1) };
Notify(msg);
}
}
deck.clear();
}
PoliciesType PlayerLogic::GetDraftPolicy(int index) const {
assert(index >= 0 && index < GetDraftSize());
return _policiesInDraft[index];
}
int PlayerLogic::GetDraftSize() const {
return As<int>(_policiesInDraft.size());
}
void DraftManipulator::ForEach(std::function<void(PoliciesType policy)> action) {
for (int i = 0; i < _player.GetDraftSize(); ++i) {
action(_player.GetDraftPolicy(i));
}
}
DraftManipulator PlayerLogic::Draft() {
return { *this };
}
DraftManipulatorConst PlayerLogic::Draft() const {
return { *this };
}
void DraftManipulatorConst::ForEach(std::function<void(PoliciesType policy)> action) {
for (int i = 0; i < _player.GetDraftSize(); ++i) {
action(_player.GetDraftPolicy(i));
}
}
PoliciesType DraftManipulatorConst::Select(int index) const {
return _player.GetDraftPolicy(index);
}
PoliciesType DraftManipulator::Select(int index) const {
return _player.GetDraftPolicy(index);
}
int DraftManipulator::Size() const {
return _player.GetDraftSize();
}
int DraftManipulatorConst::Size() const {
return _player.GetDraftSize();
}
void DraftManipulator::Take(PoliciesType policy) {
_player.TakePolicyFromDraft(policy);
}
PoliciesType DraftManipulator::Random() {
auto index = rand() % Size();
return _player.GetDraftPolicy(index);
}
PoliciesType DraftManipulatorConst::Random() {
auto index = rand() % Size();
return _player.GetDraftPolicy(index);
}
void PlayerLogic::SetPoints(int level) {
_curPoints = level;
ScorePointsChangedMessage msg(GetId(), _curPoints);
Notify(msg);
}
void PlayerLogic::SetPopulation(int level) {
_curPopulation = level;
_curPopulation = std::min(max_population_level, std::max(0, _curPopulation));
PopulationChangedMessage msg(GetId(), _curPopulation);
Notify(msg);
}
void PlayerLogic::SetTax(int level) {
_curTax = level;
_curTax = std::min(max_tax_level, std::max(0, _curTax));
TaxChangedMessage msg(_id, _curTax);
Notify(msg);
}
void PlayerLogic::SetGlory(int level) {
_curGlory = level;
_curGlory = std::max(0, std::min(max_glory_level, _curGlory));
GloryChangedMessage msg(GetId(), _curGlory);
Notify(msg);
}
void PlayerLogic::SetArmy(int level, bool allowOverflow) {
_curArmy = level;
if (!allowOverflow) {
_curArmy = std::max(0, std::min(max_army_level, _curArmy));
}
ArmyChangedMessage msg(GetId(), _curArmy);