-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCvMinorCivAI.cpp
5128 lines (4167 loc) · 151 KB
/
CvMinorCivAI.cpp
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
/* -------------------------------------------------------------------------------------------------------
© 1991-2012 Take-Two Interactive Software and its subsidiaries. Developed by Firaxis Games.
Sid Meier's Civilization V, Civ, Civilization, 2K Games, Firaxis Games, Take-Two Interactive Software
and their respective logos are all trademarks of Take-Two interactive Software, Inc.
All other marks and trademarks are the property of their respective owners.
All rights reserved.
------------------------------------------------------------------------------------------------------- */
#include "CvGameCoreDLLPCH.h"
#include "CvMinorCivAI.h"
#include "ICvDLLUserInterface.h"
#include "CvGameCoreUtils.h"
#include "CvInternalGameCoreUtils.h"
#include "CvEnumSerialization.h"
#include "CvNotifications.h"
#include "CvDiplomacyAI.h"
#include "CvDllInterfaces.h"
// must be included after all other headers
#include "LintFree.h"
//======================================================================================================
// CvMinorCivAI
//======================================================================================================
CvMinorCivAI::CvMinorCivAI ()
{
}
//------------------------------------------------------------------------------
CvMinorCivAI::~CvMinorCivAI (void)
{
Uninit();
}
/// Initialize
void CvMinorCivAI::Init(CvPlayer *pPlayer)
{
m_pPlayer = pPlayer;
m_minorCivType = CvPreGame::minorCivType(m_pPlayer->GetID());
Reset();
}
/// Deallocate memory created in initialize
void CvMinorCivAI::Uninit()
{
}
/// Reset AIStrategy status array to all false
void CvMinorCivAI::Reset()
{
m_ePersonality = NO_MINOR_CIV_PERSONALITY_TYPE;
m_eStatus = NO_MINOR_CIV_STATUS_TYPE;
m_iTurnsSinceThreatenedByBarbarians = -1;
m_eAlly = NO_PLAYER;
int iI, iJ;
for (iI = 0; iI < MAX_MAJOR_CIVS; iI++)
{
m_abWarQuestAgainstMajor[iI] = false;
for (iJ = 0; iJ < MAX_MAJOR_CIVS; iJ++)
{
m_aaiNumEnemyUnitsLeftToKillByMajor[iI][iJ] = -1;
}
m_abRouteConnectionEstablished[iI] = false;
m_aiFriendshipWithMajorTimes100[iI] = 0;
m_aiAngerFreeIntrusionCounter[iI] = 0;
m_aiPlayerQuests[iI] = NO_MINOR_CIV_QUEST_TYPE;
m_aiQuestData1[iI] = -1;
m_aiQuestData2[iI] = -1;
m_aiQuestCountdown[iI] = -1;
m_aiUnitSpawnCounter[iI] = -1;
m_aiNumUnitsGifted[iI] = 0;
m_abUnitSpawningDisabled[iI] = false;
m_abMajorIntruding[iI] = false;
m_abEverFriends[iI] = false;
m_aiMajorScratchPad[iI] = 0;
}
for (iI = 0; iI < REALLY_MAX_TEAMS; iI++)
{
m_abPermanentWar[iI] = false;
}
if (GetPlayer()->isMinorCiv())
{
CvPlot* pLoopPlot;
TeamTypes eTeam = GetPlayer()->getTeam();
int iNumPlotsInEntireWorld = GC.getMap().numPlots();
for (int iLoopPlot = 0; iLoopPlot < iNumPlotsInEntireWorld; iLoopPlot++)
{
pLoopPlot = GC.getMap().plotByIndexUnchecked(iLoopPlot);
if (pLoopPlot)
pLoopPlot->setRevealed(eTeam, true);
}
}
}
/// Serialization read
void CvMinorCivAI::Read(FDataStream& kStream)
{
// Version number to maintain backwards compatibility
uint uiVersion;
kStream >> uiVersion;
kStream >> m_ePersonality;
kStream >> m_eStatus;
kStream >> m_iTurnsSinceThreatenedByBarbarians;
kStream >> m_eAlly;
kStream >> m_abWarQuestAgainstMajor;
kStream >> m_aaiNumEnemyUnitsLeftToKillByMajor;
kStream >> m_abRouteConnectionEstablished;
kStream >> m_aiFriendshipWithMajorTimes100;
if (uiVersion >= 5)
kStream >> m_aiAngerFreeIntrusionCounter;
else
{
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
m_aiAngerFreeIntrusionCounter[iPlayerLoop] = 0;
}
kStream >> m_aiPlayerQuests;
kStream >> m_aiQuestData1;
kStream >> m_aiQuestData2;
kStream >> m_aiQuestCountdown;
kStream >> m_aiUnitSpawnCounter;
if (uiVersion > 3)
kStream >> m_aiNumUnitsGifted;
else
{
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
m_aiNumUnitsGifted[iPlayerLoop] = 0;
}
if (uiVersion <= 1)
{
// int aiGreatPeopleSpawnCounter[MAX_MAJOR_CIVS];
// kStream >> aiGreatPeopleSpawnCounter;
}
kStream >> m_abUnitSpawningDisabled;
kStream >> m_abMajorIntruding;
kStream >> m_abEverFriends;
kStream >> m_abPermanentWar;
}
/// Serialization write
void CvMinorCivAI::Write(FDataStream& kStream) const
{
// Current version number
uint uiVersion = 5;
kStream << uiVersion;
kStream << m_ePersonality;
kStream << m_eStatus;
kStream << m_iTurnsSinceThreatenedByBarbarians;
kStream << m_eAlly;
kStream << m_abWarQuestAgainstMajor;
kStream << m_aaiNumEnemyUnitsLeftToKillByMajor;
kStream << m_abRouteConnectionEstablished;
kStream << m_aiFriendshipWithMajorTimes100;
kStream << m_aiAngerFreeIntrusionCounter;
kStream << m_aiPlayerQuests;
kStream << m_aiQuestData1;
kStream << m_aiQuestData2;
kStream << m_aiQuestCountdown;
kStream << m_aiUnitSpawnCounter;
kStream << m_aiNumUnitsGifted;
kStream << m_abUnitSpawningDisabled;
kStream << m_abMajorIntruding;
kStream << m_abEverFriends;
kStream << m_abPermanentWar;
}
/// Returns the Player object this MinorCivAI is associated with
CvPlayer* CvMinorCivAI::GetPlayer()
{
return m_pPlayer;
}
/// Returns the MinorCivType this Minor is playing as (e.g. Scotland, Switzerland, etc.)
MinorCivTypes CvMinorCivAI::GetMinorCivType() const
{
// return m_minorCivType;
return CvPreGame::minorCivType(m_pPlayer->GetID());
}
/// What is the personality of this Minor
MinorCivPersonalityTypes CvMinorCivAI::GetPersonality() const
{
return m_ePersonality;
}
/// Picks a random Personality for this minor
void CvMinorCivAI::DoPickPersonality()
{
FlavorTypes eFlavorCityDefense = NO_FLAVOR;
FlavorTypes eFlavorDefense = NO_FLAVOR;
FlavorTypes eFlavorOffense = NO_FLAVOR;
for (int iFlavorLoop = 0; iFlavorLoop < GC.getNumFlavorTypes(); iFlavorLoop++)
{
if (GC.getFlavorTypes((FlavorTypes)iFlavorLoop) == "FLAVOR_CITY_DEFENSE")
{
eFlavorCityDefense = (FlavorTypes)iFlavorLoop;
}
if (GC.getFlavorTypes((FlavorTypes)iFlavorLoop) == "FLAVOR_DEFENSE")
{
eFlavorDefense = (FlavorTypes)iFlavorLoop;
}
if (GC.getFlavorTypes((FlavorTypes)iFlavorLoop) == "FLAVOR_OFFENSE")
{
eFlavorOffense = (FlavorTypes)iFlavorLoop;
}
}
CvFlavorManager* pFlavorManager = m_pPlayer->GetFlavorManager();
int* pFlavors = pFlavorManager->GetAllPersonalityFlavors();
MinorCivPersonalityTypes eRandPersonality = (MinorCivPersonalityTypes) GC.getGame().getJonRandNum(NUM_MINOR_CIV_PERSONALITY_TYPES, "Minor Civ AI: Picking Personality for this Game (should happen only once per player)");
m_ePersonality = eRandPersonality;
switch (eRandPersonality)
{
case MINOR_CIV_PERSONALITY_FRIENDLY:
pFlavors[eFlavorCityDefense] = pFlavorManager->GetAdjustedValue(pFlavors[eFlavorCityDefense], -2, 0, 10);
pFlavors[eFlavorDefense] = pFlavorManager->GetAdjustedValue(pFlavors[eFlavorDefense], -2, 0, 10);
pFlavors[eFlavorOffense] = pFlavorManager->GetAdjustedValue(pFlavors[eFlavorOffense], -2, 0, 10);
pFlavorManager->ResetToBasePersonality();
break;
case MINOR_CIV_PERSONALITY_HOSTILE:
pFlavors[eFlavorCityDefense] = pFlavorManager->GetAdjustedValue(pFlavors[eFlavorCityDefense], 2, 0, 10);
pFlavors[eFlavorDefense] = pFlavorManager->GetAdjustedValue(pFlavors[eFlavorDefense], 2, 0, 10);
pFlavors[eFlavorOffense] = pFlavorManager->GetAdjustedValue(pFlavors[eFlavorOffense], 2, 0, 10);
pFlavorManager->ResetToBasePersonality();
break;
}
}
/// What is this civ's trait?
MinorCivTraitTypes CvMinorCivAI::GetTrait() const
{
CvMinorCivInfo* pkMinorCivInfo = GC.getMinorCivInfo(GetMinorCivType());
if(pkMinorCivInfo)
{
return (MinorCivTraitTypes) pkMinorCivInfo->GetMinorCivTrait();
}
return NO_MINOR_CIV_TRAIT_TYPE;
}
// ******************************
// Main functions
// ******************************
/// Processed every turn
void CvMinorCivAI::DoTurn()
{
if (GetPlayer()->isMinorCiv())
{
DoTurnStatus();
DoFriendship();
DoTestBarbarianQuest();
DoTestWarWithMajorQuest();
DoTestProxyWarNotification();
DoTurnQuests();
DoUnitSpawnTurn();
DoIntrusion();
}
}
/// Minor is now dead or alive (haha, get it?)
void CvMinorCivAI::DoChangeAliveStatus(bool bAlive)
{
bool bFriends;
bool bAllies;
int iBestFriendship = 0;
PlayerTypes eBestPlayer = NO_PLAYER;
PlayerTypes ePlayer;
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
{
ePlayer = (PlayerTypes) iPlayerLoop;
bFriends = false;
bAllies = false;
// Add or remove bonuses depending on bAlive status
if (IsFriendshipAboveFriendsThreshold(GetFriendshipWithMajor(ePlayer)))
bFriends = true;
if (GetFriendshipWithMajor(ePlayer) > iBestFriendship)
{
iBestFriendship = GetFriendshipWithMajor(ePlayer);
eBestPlayer = ePlayer;
}
if (GetAlly() == ePlayer)
{
bAllies = true;
eBestPlayer = ePlayer;
}
if (bFriends || bAllies)
DoSetBonus(ePlayer, bAlive, bFriends, bAllies);
// If we're not alive we can't have a quest to fight a major off
if (!bAlive)
{
SetWarQuestWithMajorActive(ePlayer, false);
EndActiveQuestForPlayer(ePlayer);
}
}
// Now alive
if (bAlive)
{
if (GetAlly() == NO_PLAYER)
{
if (eBestPlayer != NO_PLAYER && iBestFriendship > GetAlliesThreshold())
{
DoSetBonus(eBestPlayer, bAlive, /*bFriends*/ false, /*bAllies*/ true);
SetAlly(eBestPlayer);
}
}
}
// Now dead
else
{
SetTurnsSinceThreatenedByBarbarians(-1);
}
}
/// First contact
void CvMinorCivAI::DoFirstContactWithMajor(TeamTypes eTeam)
{
// This guy's a warmonger so we DoW him
if (IsPeaceBlocked(eTeam))
{
GET_TEAM(GetPlayer()->getTeam()).declareWar(eTeam);
}
// Normal diplo
else
{
int iGoldGift = 0;
bool bFirstMajorCiv = false;
// If this guy has been mean then no Gold gifts
if (!GET_TEAM(eTeam).IsMinorCivAggressor())
{
// Hasn't met anyone yet?
if (GET_TEAM(GetPlayer()->getTeam()).getHasMetCivCount(true) == 0)
{
iGoldGift = /*60*/ GC.getMINOR_CIV_CONTACT_GOLD_FIRST();
bFirstMajorCiv = true;
}
else
{
iGoldGift = /*30*/ GC.getMINOR_CIV_CONTACT_GOLD_OTHER();
}
}
PlayerTypes eEnemy;
int iEnemyLoop;
PlayerTypes ePlayer;
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
{
ePlayer = (PlayerTypes) iPlayerLoop;
if (GET_PLAYER(ePlayer).getTeam() == eTeam)
{
// Gold gift
GET_PLAYER(ePlayer).GetTreasury()->ChangeGold(iGoldGift);
// Need to seed quest counter?
if (GC.getGame().getElapsedGameTurns() > GetFirstPossibleTurnForQuests())
{
DoSeedQuestCountdown(ePlayer);
}
// Barbarian Quest is already active, let the player know!
if (GetTurnsSinceThreatenedByBarbarians() >= 0 && GetTurnsSinceThreatenedByBarbarians() < 10)
{
// Don't show notification if player is too far away
if (IsPlayerCloseEnoughForBarbQuestAnnouncement(ePlayer))
DoLaunchBarbariansQuestForPlayerNotification(ePlayer);
}
// Humans intrude automatically upon meeting
if (GET_PLAYER(ePlayer).isHuman())
{
SetMajorIntruding(ePlayer, true);
}
else
{
SetMajorIntruding(ePlayer, false);
}
// Notification for War quest
for (iEnemyLoop = 0; iEnemyLoop < MAX_MAJOR_CIVS; iEnemyLoop++)
{
eEnemy = (PlayerTypes) iEnemyLoop;
// Quest is active
if (IsWarQuestWithMajorActive(eEnemy))
{
// Still at war
if (IsAtWarWithPlayersTeam(eEnemy))
{
DoLaunchWarQuestForPlayerNotification(ePlayer, eEnemy);
}
}
}
// Greeting for active human player
if (ePlayer == GC.getGame().getActivePlayer())
{
if (!GC.getGame().isNetworkMultiPlayer()) // KWG: Should this be !GC.getGame().isMPOption(MPOPTION_SIMULTANEOUS_TURNS)
{
CvPopupInfo kPopupInfo(BUTTONPOPUP_CITY_STATE_GREETING, GetPlayer()->GetID(), iGoldGift, -1, 0, bFirstMajorCiv);
GC.GetEngineUserInterface()->AddPopup(kPopupInfo);
// We are adding a popup that the player must make a choice in, make sure they are not in the end-turn phase.
CancelActivePlayerEndTurn();
}
// update the mouseover text for the city-state's city banners
int iLoop = 0;
CvCity* pLoopCity = NULL;
for (pLoopCity = m_pPlayer->firstCity(&iLoop); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iLoop))
{
if (pLoopCity->plot()->isRevealed(eTeam))
{
auto_ptr<ICvCity1> pDllLoopCity = GC.WrapCityPointer(pLoopCity);
GC.GetEngineUserInterface()->SetSpecificCityInfoDirty(pDllLoopCity.get(), CITY_UPDATE_TYPE_BANNER);
}
}
}
}
}
}
}
/// Are we at war with a minor and not allied with anyone?
void CvMinorCivAI::DoTestEndWarsVSMinors(PlayerTypes eOldAlly, PlayerTypes eNewAlly)
{
if (eOldAlly == NO_PLAYER)
return;
if (!GetPlayer()->isAlive())
return;
PlayerTypes eOtherMinor;
int iOtherMinorLoop;
PlayerTypes eOtherAlly;
bool bForcedWar;
TeamTypes eLoopTeam;
for (int iTeamLoop = 0; iTeamLoop < MAX_CIV_TEAMS; iTeamLoop++)
{
eLoopTeam = (TeamTypes) iTeamLoop;
// Another Minor
if (!GET_TEAM(eLoopTeam).isMinorCiv())
continue;
// They not alive!
if (!GET_TEAM(eLoopTeam).isAlive())
continue;
// At war with him
if (!GET_TEAM(GetPlayer()->getTeam()).isAtWar(eLoopTeam))
continue;
if (eOldAlly != NO_PLAYER)
{
// Old ally wasn't at war
if (!GET_TEAM(GET_PLAYER(eOldAlly).getTeam()).isAtWar(eLoopTeam))
continue;
}
if (eNewAlly != NO_PLAYER)
{
// New ally IS at war
if (GET_TEAM(GET_PLAYER(eNewAlly).getTeam()).isAtWar(eLoopTeam))
continue;
}
// Make sure this guy isn't allied with someone at war with us
bForcedWar = false;
for (iOtherMinorLoop = 0; iOtherMinorLoop < MAX_CIV_TEAMS; iOtherMinorLoop++)
{
eOtherMinor = (PlayerTypes) iOtherMinorLoop;
// Other minor is on this team
if (GET_PLAYER(eOtherMinor).getTeam() == eLoopTeam)
{
eOtherAlly = GET_PLAYER(eOtherMinor).GetMinorCivAI()->GetAlly();
if (eOtherAlly != NO_PLAYER)
{
// This guy's ally at war with us?
if (GET_TEAM(GET_PLAYER(eOtherAlly).getTeam()).isAtWar(GetPlayer()->getTeam()))
{
bForcedWar = true;
break;
}
}
}
}
if (bForcedWar)
continue;
if (IsPermanentWar(eLoopTeam))
continue;
GET_TEAM(GetPlayer()->getTeam()).makePeace(eLoopTeam);
}
}
/// Update what our status is
void CvMinorCivAI::DoTurnStatus()
{
int iWeight = 0;
PlayerProximityTypes eProximity;
CvPlayer* pPlayer;
CvTeam* pTeam;
PlayerTypes ePlayer;
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
{
ePlayer = (PlayerTypes) iPlayerLoop;
pPlayer = &GET_PLAYER(ePlayer);
pTeam = &GET_TEAM(pPlayer->getTeam());
eProximity = pPlayer->GetProximityToPlayer(GetPlayer()->GetID());
// Check how close the player is
switch (eProximity)
{
// DISTANT: Elevated if we're at war
case PLAYER_PROXIMITY_DISTANT:
break;
// if (IsAtWarWithPlayersTeam(ePlayer))
// {
// iWeight += 10;
// }
// FAR: Elevated if they're an aggressor OR we're at war (note the ELSE IF)
case PLAYER_PROXIMITY_FAR:
break;
// if (pTeam->IsMinorCivAggressor())
// {
// iWeight += 10;
// }
// else if (IsAtWarWithPlayersTeam(ePlayer))
// {
// iWeight += 10;
// }
// CLOSE: Elevated if they're an aggressor, critical if we're at war
case PLAYER_PROXIMITY_CLOSE:
if (pTeam->IsMinorCivAggressor())
{
iWeight += 10;
}
if (pTeam->IsMinorCivWarmonger())
{
iWeight += 20;
}
break;
// NEIGHBORS: Pretty much anything makes the situation critical
case PLAYER_PROXIMITY_NEIGHBORS:
if (pTeam->IsMinorCivAggressor())
{
iWeight += 20;
}
if (IsAtWarWithPlayersTeam(ePlayer))
{
iWeight += 20;
}
break;
default:
break;
}
}
// Do the final math
if (iWeight >= 20)
{
m_eStatus = MINOR_CIV_STATUS_CRITICAL;
}
else if (iWeight >= 10)
{
m_eStatus = MINOR_CIV_STATUS_ELEVATED;
}
else
{
m_eStatus = MINOR_CIV_STATUS_NORMAL;
}
}
/// What is our status
MinorCivStatusTypes CvMinorCivAI::GetStatus() const
{
return m_eStatus;
}
/// Notifications
void CvMinorCivAI::AddNotification(Localization::String strString, Localization::String strSummaryString, PlayerTypes ePlayer, int iX, int iY)
{
if (iX == -1 && iY == -1)
{
CvCity* capCity = GetPlayer()->getCapitalCity();
if (capCity != NULL)
{
iX = capCity->getX();
iY = capCity->getY();
}
}
CvNotifications* pNotifications = GET_PLAYER(ePlayer).GetNotifications();
if (pNotifications)
{
pNotifications->Add(NOTIFICATION_MINOR, strString.toUTF8(), strSummaryString.toUTF8(), iX, iY, GetPlayer()->GetID());
}
}
/// Quest Notifications
void CvMinorCivAI::AddQuestNotification(Localization::String strString, Localization::String strSummaryString, PlayerTypes ePlayer, int iX, int iY)
{
if (iX == -1 && iY == -1)
{
CvCity* capCity = GetPlayer()->getCapitalCity();
if (capCity != NULL)
{
iX = capCity->getX();
iY = capCity->getY();
}
}
CvNotifications* pNotifications = GET_PLAYER(ePlayer).GetNotifications();
if (pNotifications)
{
CvString pTempString = strString.toUTF8();
pTempString += "[NEWLINE][NEWLINE]";
pTempString += Localization::Lookup("TXT_KEY_MINOR_QUEST_BLOCKING_TT").toUTF8();
pNotifications->Add(NOTIFICATION_MINOR_QUEST, pTempString, strSummaryString.toUTF8(), iX, iY, GetPlayer()->GetID());
}
}
// ******************************
// Fend off Barbarians
// ******************************
/// Barbarians threatening this Minor?
void CvMinorCivAI::DoTestBarbarianQuest()
{
// Increment counter - this is only used when sending notifications to players
if (GetTurnsSinceThreatenedByBarbarians() >= 0)
{
ChangeTurnsSinceThreatenedByBarbarians(1);
// Long enough to have expired?
if (GetTurnsSinceThreatenedByBarbarians() >= 30)
SetTurnsSinceThreatenedByBarbarians(-1);
}
// Not already threatened?
if (GetTurnsSinceThreatenedByBarbarians() == -1)
{
if (GetNumThreateningBarbarians() >= 2)
{
// Wasn't under attack before, but is now!
SetTurnsSinceThreatenedByBarbarians(0);
PlayerTypes ePlayer;
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
{
ePlayer = (PlayerTypes) iPlayerLoop;
if (IsPlayerCloseEnoughForBarbQuestAnnouncement(ePlayer))
{
DoLaunchBarbariansQuestForPlayerNotification(ePlayer);
}
}
}
}
}
/// Barbs in or near our borders?
int CvMinorCivAI::GetNumThreateningBarbarians()
{
int iCount = 0;
int iLoop;
for (CvUnit* pLoopUnit = GET_PLAYER(BARBARIAN_PLAYER).firstUnit(&iLoop); NULL != pLoopUnit; pLoopUnit = GET_PLAYER(BARBARIAN_PLAYER).nextUnit(&iLoop))
{
if (pLoopUnit->IsBarbarianUnitThreateningMinor(GetPlayer()->GetID()))
iCount++;
}
return iCount;
}
/// Player killed a threatening barb, so reward him!
void CvMinorCivAI::DoThreateningBarbKilled(PlayerTypes eKillingPlayer, int iX, int iY)
{
CvAssertMsg(eKillingPlayer >= 0, "eMajor is expected to be non-negative (invalid Index)");
CvAssertMsg(eKillingPlayer < MAX_MAJOR_CIVS, "eMajor is expected to be within maximum bounds (invalid Index)");
ChangeFriendshipWithMajor(eKillingPlayer, /*5*/ GC.getFRIENDSHIP_PER_BARB_KILLED());
ChangeAngerFreeIntrusionCounter(eKillingPlayer, 5);
Localization::String strMessage = Localization::Lookup("TXT_KEY_NOTIFICATION_MINOR_BARB_KILLED");
strMessage << GetPlayer()->getNameKey();
Localization::String strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SM_MINOR_BARB_KILLED");
strSummary << GetPlayer()->getNameKey();
AddNotification(strMessage, strSummary, eKillingPlayer, iX, iY);
}
/// How long has this Minor been under attack from Barbs?
int CvMinorCivAI::GetTurnsSinceThreatenedByBarbarians() const
{
return m_iTurnsSinceThreatenedByBarbarians;
}
/// How long has this Minor been under attack from Barbs?
void CvMinorCivAI::SetTurnsSinceThreatenedByBarbarians(int iValue)
{
if (GetTurnsSinceThreatenedByBarbarians() != iValue)
m_iTurnsSinceThreatenedByBarbarians = iValue;
}
/// How long has this Minor been under attack from Barbs?
void CvMinorCivAI::ChangeTurnsSinceThreatenedByBarbarians(int iChange)
{
SetTurnsSinceThreatenedByBarbarians(GetTurnsSinceThreatenedByBarbarians() + iChange);
}
/// Is player close enough for the game to tell him about the Barb Quest
bool CvMinorCivAI::IsPlayerCloseEnoughForBarbQuestAnnouncement(PlayerTypes eMajor)
{
CvCity* pCapital = GetPlayer()->getCapitalCity();
// Minor must have Capital
if (pCapital == NULL)
{
return false;
}
// Has Minor met this player yet?
if (IsHasMetPlayer(eMajor))
{
bool bCloseEnoughForQuest = false;
CvCity* pMajorsCapital = GET_PLAYER(eMajor).getCapitalCity();
if (pMajorsCapital != NULL)
{
if (pCapital->getArea() == pMajorsCapital->getArea())
{
return true;
}
if (!bCloseEnoughForQuest)
{
int iDistance = plotDistance(pCapital->getX(), pCapital->getY(), pMajorsCapital->getX(), pMajorsCapital->getY());
if (iDistance <= /*50*/ GC.getMAX_DISTANCE_MINORS_BARB_QUEST())
{
return true;
}
}
}
}
return false;
}
/// Launch Barbarians Quest
void CvMinorCivAI::DoLaunchBarbariansQuestForPlayerNotification(PlayerTypes ePlayer)
{
if (ePlayer == GC.getGame().getActivePlayer())
{
if (!IsAtWarWithPlayersTeam(ePlayer))
{
Localization::String strMessage = Localization::Lookup("TXT_KEY_NOTIFICATION_MINOR_BARBS_QUEST");
strMessage << GetPlayer()->getNameKey();
Localization::String strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SUMMARY_MINOR_BARBS_QUEST");
strSummary << GetPlayer()->getNameKey();
AddQuestNotification(strMessage, strSummary, ePlayer);
}
}
}
// ******************************
// ***** War with Major Quest *****
// ******************************
/// Quest to help defend from war with Major
void CvMinorCivAI::DoTestWarWithMajorQuest()
{
TeamTypes eEnemyTeam;
PlayerTypes eEnemy;
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
{
eEnemy = (PlayerTypes) iPlayerLoop;
eEnemyTeam = GET_PLAYER(eEnemy).getTeam();
bool bAtWar = IsAtWarWithPlayersTeam(eEnemy);
// Quest is active
if (IsWarQuestWithMajorActive(eEnemy))
{
// If they're dead now, cancel the quest
if (!GET_PLAYER(eEnemy).isAlive())
SetWarQuestWithMajorActive(eEnemy, false);
// Not at war any more
if (!bAtWar)
{
SetWarQuestWithMajorActive(eEnemy, false);
if (GET_TEAM(GetPlayer()->getTeam()).isHasMet(GC.getGame().getActiveTeam()))
{
if (!IsAtWarWithPlayersTeam(GC.getGame().getActivePlayer()) && eEnemyTeam != GC.getGame().getActiveTeam())
{
Localization::String strMessage = Localization::Lookup("TXT_KEY_NOTIFICATION_MINOR_WAR_QUEST_LOST_CHANCE");
strMessage << GetPlayer()->getNameKey() << GET_PLAYER(eEnemy).getNameKey();
Localization::String strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SUMMARY_MINOR_WAR_QUEST_LOST_CHANCE");
strSummary << GetPlayer()->getNameKey();
AddQuestNotification(strMessage, strSummary, GC.getGame().getActivePlayer());
}
}
}
}
}
}
/// Time to send out a "Help us with Units" notification?
void CvMinorCivAI::DoTestProxyWarNotification()
{
for(int iNotifyLoop = 0; iNotifyLoop < MAX_MAJOR_CIVS; ++iNotifyLoop){
PlayerTypes eNotifyPlayer = (PlayerTypes) iNotifyLoop;
CvPlayerAI& kCurNotifyPlayer = GET_PLAYER(eNotifyPlayer);
CvTeam* pNotifyTeam = &GET_TEAM(kCurNotifyPlayer.getTeam());
Localization::String strMessage;
Localization::String strSummary;
if(pNotifyTeam->isHasMet(GetPlayer()->getTeam()))
{
PlayerTypes eEnemyLeader;
TeamTypes eEnemyTeam;
for(int iMajorLoop = 0; iMajorLoop < MAX_MAJOR_CIVS; iMajorLoop++)
{
PlayerTypes eMajorLoop = (PlayerTypes) iMajorLoop;
CvPlayer* pMajorLoop = &GET_PLAYER(eMajorLoop);
CvAssertMsg(pMajorLoop, "Error sending out proxy war notification from a city-state. Please send Anton your save file and version.");
if (pMajorLoop)
{
eEnemyTeam = pMajorLoop->getTeam();
// Minor is at war
if(GET_TEAM(GetPlayer()->getTeam()).isAtWar(eEnemyTeam))
{
// Notify player must NOT be at war with either the major or the minor
if(!pNotifyTeam->isAtWar(eEnemyTeam) && !pNotifyTeam->isAtWar(GetPlayer()->getTeam()))
{
// Don't send out notification here for Warmonger - we centralize this elsewhere (so that players don't get spammed with 10 Notifications)
if(!IsPeaceBlocked(eEnemyTeam))
{
if(GET_TEAM(GetPlayer()->getTeam()).GetNumTurnsAtWar(eEnemyTeam) == /*2*/ GC.getTXT_KEY_MINOR_GIFT_UNITS_REMINDER()) //antonjs: consider: this text key is hacked to act like a value, fix it for clarity
{
eEnemyLeader = GET_TEAM(eEnemyTeam).getLeaderID();
CvPlayer* pEnemyLeader = &GET_PLAYER(eEnemyLeader);
// Do some additional checks to safeguard against weird scenario cases (ex. major and minor on same team, major is dead)
if (pEnemyLeader)
{
if (!pEnemyLeader->isMinorCiv() && pEnemyLeader->isAlive())
{
strMessage = Localization::Lookup("TXT_KEY_NOTIFICATION_MINOR_WAR_UNIT_HELP");
strMessage << GetPlayer()->getCivilizationShortDescriptionKey() << pEnemyLeader->getCivilizationShortDescriptionKey();
strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SUMMARY_MINOR_WAR_UNIT_HELP");
strSummary << GetPlayer()->getCivilizationShortDescriptionKey();
AddQuestNotification(strMessage.toUTF8(), strSummary.toUTF8(), eNotifyPlayer);
break;
}
}
}
}
}
}
}
}
}
}
}
/// Quest to help defend from war with Major
void CvMinorCivAI::DoLaunchWarWithMajorQuest(TeamTypes eAttackingTeam)
{
int iQuestPlayerLoop;
PlayerTypes eQuestPlayer;
PlayerTypes eEnemy;
for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
{
eEnemy = (PlayerTypes) iPlayerLoop;
if (GET_PLAYER(eEnemy).isAlive() && GET_PLAYER(eEnemy).getTeam() == eAttackingTeam)
{
SetWarQuestWithMajorActive(eEnemy, true);
// Number of Units that must be killed to fulfill this quest
int iNumUnitsToKill;
int iTotalMilitaryUnits = 0;
int iLoop;
for (CvUnit* pLoopUnit = GET_PLAYER(eEnemy).firstUnit(&iLoop); NULL != pLoopUnit; pLoopUnit = GET_PLAYER(eEnemy).nextUnit(&iLoop))
{
if (pLoopUnit->IsCombatUnit())
{
iTotalMilitaryUnits++;
}
}
iNumUnitsToKill = iTotalMilitaryUnits / /*4*/ GC.getWAR_QUEST_UNITS_TO_KILL_DIVISOR();
int iMinUnitsToKill = /*3*/ GC.getWAR_QUEST_MIN_UNITS_TO_KILL();
if (iNumUnitsToKill < iMinUnitsToKill)
{
iNumUnitsToKill = iMinUnitsToKill;
}
for (iQuestPlayerLoop = 0; iQuestPlayerLoop < MAX_MAJOR_CIVS; iQuestPlayerLoop++)
{
eQuestPlayer = (PlayerTypes) iQuestPlayerLoop;
SetNumEnemyUnitsLeftToKillByMajor(eQuestPlayer, eEnemy, iNumUnitsToKill);
}
if (GET_TEAM(GetPlayer()->getTeam()).isHasMet(GC.getGame().getActiveTeam()))
{
DoLaunchWarQuestForPlayerNotification(GC.getGame().getActivePlayer(), eEnemy);
}
}
}