/
CvPlayer.cpp
22186 lines (18916 loc) · 641 KB
/
CvPlayer.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
// player.cpp
#include "CvGameCoreDLL.h"
#include "CvGlobals.h"
#include "CvArea.h"
#include "CvGameAI.h"
#include "CvMap.h"
#include "CvPlot.h"
#include "CvRandom.h"
#include "CvTeamAI.h"
#include "CvGameCoreUtils.h"
#include "CvPlayerAI.h"
#include "CvPlayer.h"
#include "CvGameCoreUtils.h"
#include "CvArtFileMgr.h"
#include "CvDiploParameters.h"
#include "CvInitCore.h"
#include "CyArgsList.h"
#include "CvInfos.h"
#include "CvPopupInfo.h"
#include "CvDiploParameters.h"
#include "FProfiler.h"
#include "CvGameTextMgr.h"
#include "CyCity.h"
#include "CyPlot.h"
#include "CvTradeRoute.h"
#include "CvTradeRouteGroup.h" //R&R mod, vetiarvind, trade groups
#include <numeric>
#include <algorithm>
#include "CvDLLInterfaceIFaceBase.h"
#include "CvDLLEntityIFaceBase.h"
#include "CvDLLEngineIFaceBase.h"
#include "CvDLLFAStarIFaceBase.h"
#include "CvDLLEventReporterIFaceBase.h"
#include "CvDLLPythonIFaceBase.h"
#include "CvSavegame.h"
// Public Functions...
CvPlayer::CvPlayer()
{
// R&R, ray, Bargaining - START
m_bWillingToBargain = false;
m_iTimeNoTrade = 0;
// R&R, ray, Bargaining - END
// R&R, ray, Timers Diplo Events - START
m_iTimerNativeMerc = 0;
m_iTimerEuropeanWars = 0;
m_iTimerEuropeanPeace = 0;
m_iTimerPrisonsCrowded = 0;
m_iTimerRevolutionaryNoble = 0;
m_iTimerBishop = 0;
m_iTimerChurchDemand = 0;
m_iTimerChurchWar = 0;
m_iTimerSmugglingShip = 0;
m_iTimerRanger = 0;
m_iTimerConquistador = 0;
m_iTimerPirates = 0;
m_iTimerContinentalGuard = 0;
m_iTimerMortar = 0;
m_iTimerNativeSlave = 0;
m_iTimerAfricanSlaves = 0;
m_iTimerStealingImmigrant = 0;
// R&R, ray, Timers Diplo Events - END
m_iChurchFavoursReceived = 0; // R&R, ray, Church Favours
// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
m_cache_YieldEquipmentAmount = new YieldArray<unsigned short>[GC.getNumProfessionInfos()];
// cache CvPlayer::getYieldEquipmentAmount - end - Nightinggale
reset(NO_PLAYER, true);
}
CvPlayer::~CvPlayer()
{
uninit();
// cache CvPlayer::getYieldEquipmentAmount - start - Nightinggale
if (m_cache_YieldEquipmentAmount != NULL)
{
for (int iProfession = 0; iProfession < GC.getNumProfessionInfos(); iProfession++) {
m_cache_YieldEquipmentAmount[iProfession].reset();
}
SAFE_DELETE_ARRAY(m_cache_YieldEquipmentAmount);
}
// cache CvPlayer::getYieldEquipmentAmount - end - Nightinggale
}
void CvPlayer::init(PlayerTypes eID)
{
//--------------------------------
// Init saved data
reset(eID);
// set the CivEffect cache
CivEffect()->rebuildCivEffectCache();
/** NBMOD TAX **/
m_iMaxTaxRate = GC.getHandicapInfo(getHandicapType()).NBMOD_GetInitMaxTaxRate();
/** NBMOD TAX **/
//assign europe civilization as parent
for (int iParent = 0; iParent < MAX_PLAYERS; ++iParent)
{
CvPlayer& kParent = GET_PLAYER((PlayerTypes) iParent);
if(kParent.getCivilizationType() != NO_CIVILIZATION)
{
if(GC.getCivilizationInfo(kParent.getCivilizationType()).getDerivativeCiv() == getCivilizationType())
{
setParent((PlayerTypes) iParent);
break;
}
}
}
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
YieldTypes eYield = (YieldTypes) iYield;
CvYieldInfo& kYield = GC.getYieldInfo(eYield);
FAssert(kYield.getBuyPriceHigh() >= kYield.getBuyPriceLow());
int iBuyPrice = kYield.getBuyPriceLow() + GC.getGameINLINE().getSorenRandNum(kYield.getBuyPriceHigh() - kYield.getBuyPriceLow() + 1, "Yield Price");
setYieldBuyPrice(eYield, iBuyPrice, false);
}
// R&R, ray, Africa
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
YieldTypes eYield = (YieldTypes) iYield;
CvYieldInfo& kYield = GC.getYieldInfo(eYield);
FAssert(kYield.getAfricaBuyPriceHigh() >= kYield.getAfricaBuyPriceLow());
int iAfricaBuyPrice = kYield.getAfricaBuyPriceLow() + GC.getGameINLINE().getSorenRandNum(kYield.getAfricaBuyPriceHigh() - kYield.getAfricaBuyPriceLow() + 1, "Yield Price");
setYieldAfricaBuyPrice(eYield, iAfricaBuyPrice, false);
}
// R&R, ray, Africa - END
// R&R, ray, Port Royal
for (int iYield = 0; iYield < NUM_YIELD_TYPES; ++iYield)
{
YieldTypes eYield = (YieldTypes) iYield;
CvYieldInfo& kYield = GC.getYieldInfo(eYield);
FAssert(kYield.getPortRoyalBuyPriceHigh() >= kYield.getPortRoyalBuyPriceLow());
int iPortRoyalBuyPrice = kYield.getPortRoyalBuyPriceLow() + GC.getGameINLINE().getSorenRandNum(kYield.getPortRoyalBuyPriceHigh() - kYield.getPortRoyalBuyPriceLow() + 1, "Yield Price");
setYieldPortRoyalBuyPrice(eYield, iPortRoyalBuyPrice, false);
}
// R&R, ray, Port Royal - END
//--------------------------------
// Init containers
m_cities.init();
m_aTradeGroups.reset(); //R&R mod, vetiarvind, trade groups
m_tradeRoutes.reset();
m_units.reset();
freeEuropeUnits();
freeAfricaUnits(); /*** TRIANGLETRADE 10/23/08 by DPII ***/
freePortRoyalUnits(); // R&R, ray, Port Royal
m_selectionGroups.init();
m_eventsTriggered.init();
m_aDocksNextUnits.clear();
//--------------------------------
// Init non-saved data
setupGraphical();
//--------------------------------
// Init other game data
FAssert(getTeam() != NO_TEAM);
GET_TEAM(getTeam()).changeNumMembers(1);
if ((GC.getInitCore().getSlotStatus(getID()) == SS_TAKEN) || (GC.getInitCore().getSlotStatus(getID()) == SS_COMPUTER))
{
setAlive(true);
//ray, fixing bug of player initialization braking traits impacting relations - START
//code moved to initDiplomacy()
/*
FAssertMsg((GC.getNumTraitInfos() > 0), "GC.getNumTraitInfos() is less than or equal to zero but is expected to be larger than zero in CvPlayer::init");
for (int iI = 0; iI < GC.getNumTraitInfos(); iI++)
{
if (GC.getCivilizationInfo(getCivilizationType()).hasTrait(iI))
{
processTrait((TraitTypes) iI, 1);
}
if (GC.getLeaderHeadInfo(getLeaderType()).hasTrait(iI))
{
processTrait((TraitTypes) iI, 1);
}
}
for (int iI = 0; iI < NUM_YIELD_TYPES; iI++)
{
updateExtraYieldThreshold((YieldTypes)iI);
updateCityExtraYield((YieldTypes) iI);
}
*/
//ray, fixing bug of player initialization braking traits impacting relations - END
for (int iI = 0; iI < GC.getNumCivicOptionInfos(); iI++)
{
setCivic(((CivicOptionTypes)iI), ((CivicTypes)(GC.getCivilizationInfo(getCivilizationType()).getCivilizationInitialCivics(iI))));
}
for (int iI = 0; iI < GC.getNumEventInfos(); iI++)
{
resetEventOccured((EventTypes)iI, false);
}
for (int iI = 0; iI < GC.getNumEventTriggerInfos(); iI++)
{
resetTriggerFired((EventTriggerTypes)iI);
}
}
AI_init();
Update_cache_YieldEquipmentAmount(); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}
void CvPlayer::uninit()
{
m_triggersFired.clear();
// PatchMod: Achievements START
m_achievesGained.clear();
m_achievesTurn.clear();
// PatchMod: Achievements END
m_groupCycle.clear();
m_aszCityNames.clear();
m_cities.uninit();
m_tradeRoutes.reset();
m_aTradeGroups.reset(); //R&R mod, vetiarvind, trade groups
m_units.reset();
freeEuropeUnits();
freeAfricaUnits(); /*** TRIANGLETRADE 10/23/08 by DPII ***/
freePortRoyalUnits(); // R&R, ray, Port Royal
m_selectionGroups.uninit();
m_eventsTriggered.uninit();
clearMessages();
clearPopups();
clearDiplomacy();
}
// FUNCTION: reset()
// Initializes data members that are serialized.
void CvPlayer::reset(PlayerTypes eID, bool bConstructorCall)
{
//--------------------------------
// Uninit class
uninit();
resetSavedData(eID,bConstructorCall);
/** NBMOD REF **/
m_bNBMOD_REF_Display = true;
m_iNBMOD_REF_DisplayTurn = 0;
/** NBMOD REF **/
// Dale - AoD: AI Autoplay START
m_bDisableHuman = false;
// Dale - AoD: AI Autoplay END
m_uiStartTime = 0;
m_aszTradeMessages.clear();
// TAC - Trade Messages - koma13 - START
m_aeTradeMessageTypes.clear();
m_aeTradeMessageYields.clear();
m_aiTradeMessageAmounts.clear();
m_aiTradeMessageCommissions.clear();
// TAC - Trade Messages - koma13 - END
if (!bConstructorCall)
{
AI_reset();
}
Update_cache_YieldEquipmentAmount(); // cache CvPlayer::getYieldEquipmentAmount - Nightinggale
}
//////////////////////////////////////
// graphical only setup
//////////////////////////////////////
void CvPlayer::setupGraphical()
{
if (!GC.IsGraphicsInitialized())
return;
CvCity* pLoopCity;
CvUnit* pLoopUnit;
// Setup m_cities
int iLoop;
for (pLoopCity = firstCity(&iLoop); pLoopCity != NULL; pLoopCity = nextCity(&iLoop))
{
pLoopCity->setupGraphical();
}
// Setup m_units
for(pLoopUnit = firstUnit(&iLoop); pLoopUnit != NULL; pLoopUnit = nextUnit(&iLoop))
{
if (pLoopUnit->plot() != NULL)
{
pLoopUnit->setupGraphical();
}
}
}
void CvPlayer::initFreeState()
{
setGold(0);
// Barthoze : Time is the issue - START
int iStartingGold = GC.getHandicapInfo(getHandicapType()).getStartingGold();
iStartingGold += GC.getEraInfo(GC.getGameINLINE().getStartEra()).getStartingGold();
// Gives an appropriate sum of money to trade with Natives, which depends on cargo size.
iStartingGold *= GC.getGameSpeedInfo(GC.getGameINLINE().getGameSpeedType()).getStoragePercent();
iStartingGold /= 100;
changeGold(iStartingGold);
// Barthoze : Time is the issue - END
}
void CvPlayer::initFreeUnits()
{
UnitTypes eLoopUnit;
int iFreeCount;
if ((GC.getGameINLINE().isOption(GAMEOPTION_ADVANCED_START) || GC.getCivilizationInfo(getCivilizationType()).getAdvancedStartPoints() > 0) && !GC.getCivilizationInfo(getCivilizationType()).isEurope())
{
int iPoints = GC.getCivilizationInfo(getCivilizationType()).getAdvancedStartPoints();
if (NO_WORLDSIZE != GC.getInitCore().getWorldSize())
{
iPoints *= GC.getWorldInfo(GC.getInitCore().getWorldSize()).getAdvancedStartPointsMod();
iPoints /= 100;
}
if (NO_GAMESPEED != GC.getInitCore().getGameSpeed())
{
iPoints *= GC.getGameSpeedInfo(GC.getInitCore().getGameSpeed()).getGrowthPercent();
iPoints /= 100;
}
if (GC.getGameINLINE().isOption(GAMEOPTION_ADVANCED_START))
{
iPoints += GC.getGameINLINE().getNumAdvancedStartPoints();
}
iPoints *= GC.getHandicapInfo(getHandicapType()).getAdvancedStartPointsMod();
iPoints /= 100;
if (!isHuman())
{
iPoints *= GC.getHandicapInfo(GC.getGameINLINE().getHandicapType()).getAIAdvancedStartPercent();
iPoints /= 100;
}
setAdvancedStartPoints(iPoints);
// Starting visibility
CvPlot* pStartingPlot = getStartingPlot();
if (NULL != pStartingPlot)
{
for (int iPlotLoop = 0; iPlotLoop < GC.getMapINLINE().numPlots(); ++iPlotLoop)
{
CvPlot* pPlot = GC.getMapINLINE().plotByIndex(iPlotLoop);
if (plotDistance(pPlot->getX_INLINE(), pPlot->getY_INLINE(), pStartingPlot->getX_INLINE(), pStartingPlot->getY_INLINE()) <= GC.getDefineINT("ADVANCED_START_SIGHT_RANGE"))
{
pPlot->setRevealed(getTeam(), true, false, NO_TEAM);
}
}
}
if (!GC.getGameINLINE().isOption(GAMEOPTION_ADVANCED_START) && !isHuman() && (getAdvancedStartPoints() >= 0))
{
AI_doAdvancedStart();
}
}
else
{
CvCivilizationInfo& kCivilizationInfo = GC.getCivilizationInfo(getCivilizationType());
for (int iI = 0; iI < kCivilizationInfo.getNumCivilizationFreeUnits(); iI++)
{
int iLoopUnitClass = kCivilizationInfo.getCivilizationFreeUnitsClass(iI);
ProfessionTypes eLoopUnitProfession = (ProfessionTypes) kCivilizationInfo.getCivilizationFreeUnitsProfession(iI);
eLoopUnit = (UnitTypes)kCivilizationInfo.getCivilizationUnits(iLoopUnitClass);
if (eLoopUnit != NO_UNIT)
{
iFreeCount = (GC.getEraInfo(GC.getGameINLINE().getStartEra()).getStartingUnitMultiplier() + ((!isHuman()) ? GC.getHandicapInfo(GC.getGameINLINE().getHandicapType()).getAIStartingUnitMultiplier() : 0));
for (int iJ = 0; iJ < iFreeCount; iJ++)
{
//add revolution units to children
if (GC.getCivilizationInfo(getCivilizationType()).isEurope())
{
for (int iPlayer=0;iPlayer<MAX_PLAYERS;iPlayer++)
{
CvPlayer& kChild = GET_PLAYER((PlayerTypes) iPlayer);
if(kChild.isAlive() && (kChild.getParent() == getID()))
{
kChild.addRevolutionEuropeUnit(eLoopUnit, eLoopUnitProfession);
}
}
}
else
{
CvUnit* pUnit = addFreeUnit(eLoopUnit, eLoopUnitProfession);
}
}
}
}
/** NBMOD REF **/
if (GC.getNBMOD_REF_ENABLE() == 1)
{
if (GC.getCivilizationInfo(getCivilizationType()).isEurope())
{
for (int iPlayer=0;iPlayer<MAX_PLAYERS;iPlayer++)
{
CvPlayer& kChild = GET_PLAYER((PlayerTypes) iPlayer);
if(kChild.isAlive() && (kChild.getParent() == getID()))
{
// Start-KMW Berechnen
int iKMW = kChild.NBMOD_REF_MakeStartValue();
while(kChild.NBMOD_GetEuropeMilitaryValue() < iKMW)
{
kChild.NBMOD_AddEuropeRandomUnit(false); // Einheit hinzuf�gen
}
int iTransporting = kChild.NBMOD_GetNumEuropeTransporting(); // Wie viel Transportkapazit�t steht dem K�nig zur Verf�gung?
int iNumUnits = kChild.NBMOD_GetNumEuropeUnits(); // Wie viele Landeinheiten besitzt der K�nig?
// Errechnet wie vile Einheiten mit einer Welle zur Kolonie gelangen sollen
iNumUnits = iNumUnits * GC.getNBMOD_REF_NUM_UNITS_PERCENT() / 100;
// Ist die Anzahl der zu transportierenden Einheiten gr��er als die Transportkapazit�t?
while(iNumUnits > iTransporting)
{
// Dann ein Schiff aufr�sten
kChild.NBMOD_AddEuropeShipUnit(false);
iTransporting = kChild.NBMOD_GetNumEuropeTransporting(); // Wie viel Transportkapazit�t steht dem K�nig zur Verf�gung?
}
}
}
}
}
/** NBMOD REF **/
iFreeCount = GC.getEraInfo(GC.getGameINLINE().getStartEra()).getStartingDefenseUnits();
iFreeCount += GC.getHandicapInfo(getHandicapType()).getStartingDefenseUnits();
if (!isHuman())
{
iFreeCount += GC.getHandicapInfo(GC.getGameINLINE().getHandicapType()).getAIStartingDefenseUnits();
}
if (iFreeCount > 0)
{
addFreeUnitAI(UNITAI_DEFENSIVE, iFreeCount);
}
iFreeCount = GC.getEraInfo(GC.getGameINLINE().getStartEra()).getStartingWorkerUnits();
iFreeCount += GC.getHandicapInfo(getHandicapType()).getStartingWorkerUnits();
if (!isHuman())
{
iFreeCount += GC.getHandicapInfo(GC.getGameINLINE().getHandicapType()).getAIStartingWorkerUnits();
}
if (iFreeCount > 0)
{
addFreeUnitAI(UNITAI_SETTLER, iFreeCount);
}
iFreeCount = GC.getEraInfo(GC.getGameINLINE().getStartEra()).getStartingExploreUnits();
iFreeCount += GC.getHandicapInfo(getHandicapType()).getStartingExploreUnits();
if (!isHuman())
{
iFreeCount += GC.getHandicapInfo(GC.getGameINLINE().getHandicapType()).getAIStartingExploreUnits();
}
if (iFreeCount > 0)
{
addFreeUnitAI(UNITAI_SCOUT, iFreeCount);
}
}
}
void CvPlayer::initImmigration()
{
FAssert(getParent() != NO_PLAYER);
m_aDocksNextUnits.clear();
m_aDocksNextUnits.reserve(CivEffect()->getNumUnitsOnDock());
verifyImmigration();
}
// Make sure the number of units on the dock matches what the player is supposed to have by add/remove units.
// Also replace any unit, which is no longer able to be available on the dock.
void CvPlayer::verifyImmigration()
{
FAssert(!this->isEurope());
FAssert(!this->isNative());
// remove units, which should no longer be there.
for (unsigned int i = 0; i < m_aDocksNextUnits.size(); ++i)
{
UnitTypes eUnit = m_aDocksNextUnits[i];
if (eUnit != NO_UNIT && (!CivEffect()->canUseUnit(eUnit) || !CivEffect()->canUseImmigrant(eUnit)))
{
if (eUnit != NO_UNIT && (!CivEffect()->canUseUnit(eUnit) || !CivEffect()->canUseImmigrant(eUnit)))
{
m_aDocksNextUnits.erase(m_aDocksNextUnits.begin() + i);
--i; // compensate for ++i as next iteration needs to use the same value for i
}
}
}
if (m_aDocksNextUnits.size() > CivEffect()->getNumUnitsOnDock())
{
// Too many units on the dock.
// Remove from the right to match the number of units requested.
m_aDocksNextUnits.resize(CivEffect()->getNumUnitsOnDock());
}
else
{
// Add units until the requested amount of units have appeared.
while (m_aDocksNextUnits.size() < CivEffect()->getNumUnitsOnDock())
{
m_aDocksNextUnits.push_back(pickBestImmigrant());
}
}
}
void CvPlayer::addFreeUnitAI(UnitAITypes eUnitAI, int iCount)
{
UnitTypes eBestUnit = NO_UNIT;
ProfessionTypes eBestProfession = NO_PROFESSION;
int iBestValue = 0;
for (int iI = 0; iI < GC.getNumUnitClassInfos(); iI++)
{
UnitTypes eLoopUnit = (UnitTypes)GC.getCivilizationInfo(getCivilizationType()).getCivilizationUnits(iI);
if (eLoopUnit != NO_UNIT)
{
if (canTrain(eLoopUnit))
{
int iValue = AI_unitValue(eLoopUnit, eUnitAI, NULL);
if (iValue > iBestValue)
{
eBestUnit = eLoopUnit;
iBestValue = iValue;
eBestProfession = GC.getUnitInfo(eLoopUnit).getDefaultProfession();
}
}
}
}
if (eBestUnit != NO_UNIT)
{
for (int iI = 0; iI < iCount; iI++)
{
addFreeUnit(eBestUnit, eBestProfession, eUnitAI);
}
}
}
CvUnit* CvPlayer::addFreeUnit(UnitTypes eUnit, ProfessionTypes eProfession, UnitAITypes eUnitAI)
{
CvPlot* pStartingPlot = getStartingPlot();
if (pStartingPlot != NULL)
{
pStartingPlot->setImprovementType(NO_IMPROVEMENT);//R&R, ray, bugfix for starting on Water Goody
CvUnit* pUnit = initUnit(eUnit, eProfession, pStartingPlot->getX_INLINE(), pStartingPlot->getY_INLINE(), eUnitAI);
return pUnit;
}
return NULL;
}
int CvPlayer::startingPlotRange() const
{
int iRange;
// PatchMod: Spread out start locs START
iRange = (GC.getMapINLINE().maxStepDistance() + 60);
// iRange = (GC.getMapINLINE().maxStepDistance() + 40);
// PatchMod: Spread out start locs END
iRange *= GC.getDefineINT("STARTING_DISTANCE_PERCENT");
iRange /= 100;
iRange *= (GC.getMapINLINE().getLandPlots() / (GC.getWorldInfo(GC.getMapINLINE().getWorldSize()).getTargetNumCities() * GC.getGameINLINE().countCivPlayersAlive()));
iRange /= NUM_CITY_PLOTS;
iRange += std::min(((GC.getMapINLINE().getNumAreas() + 1) / 2), GC.getGameINLINE().countCivPlayersAlive());
long lResult=0;
if (gDLL->getPythonIFace()->callFunction(gDLL->getPythonIFace()->getMapScriptModule(), "minStartingDistanceModifier", NULL, &lResult))
{
iRange *= std::max<int>(0, (lResult + 100));
iRange /= 100;
}
return std::max(iRange, GC.getDefineINT("MIN_CIV_STARTING_DISTANCE"));
}
int CvPlayer::startingPlotDistanceFactor(CvPlot* pPlot, PlayerTypes ePlayer, int iRange) const
{
PROFILE_FUNC();
FAssert(ePlayer != getID());
CvPlot* pStartingPlot;
int iValue = 1000;
pStartingPlot = getStartingPlot();
if (pStartingPlot != NULL)
{
if (GC.getGameINLINE().isTeamGame())
{
if (GET_PLAYER(ePlayer).getTeam() == getTeam())
{
iRange *= GC.getDefineINT("OWN_TEAM_STARTING_MODIFIER");
iRange /= 100;
}
else
{
iRange *= GC.getDefineINT("RIVAL_TEAM_STARTING_MODIFIER");
iRange /= 100;
}
}
int iDistance = stepDistance(pPlot->getX_INLINE(), pPlot->getY_INLINE(), pStartingPlot->getX_INLINE(), pStartingPlot->getY_INLINE());
if (pStartingPlot->getArea() != pPlot->getArea())
{
iDistance *= 4;
iDistance /= 3;
}
iValue *= iDistance;
iValue /= iRange ;
}
return std::max(1, iValue);
}
// Returns the id of the best area, or -1 if it doesn't matter:
int CvPlayer::findStartingArea() const
{
PROFILE_FUNC();
long result = -1;
if (gDLL->getPythonIFace()->pythonFindStartingArea(getID(), &result) && !gDLL->getPythonIFace()->pythonUsingDefaultImpl()) // Python override
{
if (result == -1 || GC.getMapINLINE().getArea(result) != NULL)
{
return result;
}
else
{
FAssertMsg(false, "python findStartingArea() must return -1 or the ID of a valid area");
}
}
//not useful for water starts
if(GC.getCivilizationInfo(getCivilizationType()).isWaterStart())
{
return -1;
}
int iBestValue = 0;
int iBestArea = -1;
int iValue;
int iLoop = 0;
CvArea *pLoopArea = NULL;
// find best land area
for(pLoopArea = GC.getMapINLINE().firstArea(&iLoop); pLoopArea != NULL; pLoopArea = GC.getMapINLINE().nextArea(&iLoop))
{
if (!(pLoopArea->isWater()))
{
// iNumPlayersOnArea is the number of players starting on the area, plus this player
int iNumPlayersOnArea = (pLoopArea->getNumStartingPlots() + 1);
int iTileValue = ((pLoopArea->calculateTotalBestNatureYield() + (pLoopArea->countCoastalLand() * 2) + pLoopArea->getNumRiverEdges() + (pLoopArea->getNumTiles())) + 1);
iValue = iTileValue / iNumPlayersOnArea;
iValue *= std::min(NUM_CITY_PLOTS + 1, pLoopArea->getNumTiles() + 1);
iValue /= (NUM_CITY_PLOTS + 1);
if (iNumPlayersOnArea <= 2)
{
iValue *= 4;
iValue /= 3;
}
if (iValue > iBestValue)
{
iBestValue = iValue;
iBestArea = pLoopArea->getID();
}
}
}
return iBestArea;
}
CvPlot* CvPlayer::findStartingPlot(bool bRandomize)
{
PROFILE_FUNC();
long result = -1;
if (gDLL->getPythonIFace()->pythonFindStartingPlot(getID(), &result) && !gDLL->getPythonIFace()->pythonUsingDefaultImpl()) // Python override
{
CvPlot *pPlot = GC.getMapINLINE().plotByIndexINLINE(result);
if (pPlot != NULL)
{
return pPlot;
}
else
{
FAssertMsg(false, "python findStartingPlot() returned an invalid plot index!");
}
}
CvPlot* pLoopPlot;
int iBestArea = -1;
int iI;
bool bNew = false;
if (getStartingPlot() != NULL)
{
iBestArea = getStartingPlot()->getArea();
setStartingPlot(NULL, true);
bNew = true;
}
AI_updateFoundValues(true);//this sets all plots found values to -1
if (!bNew)
{
iBestArea = findStartingArea();
}
//flood fill land distances O(numPlots)
std::vector<short> aLandDistances(GC.getMapINLINE().numPlotsINLINE(), MAX_SHORT);
{
PROFILE("CvPlayer::findStartingPlot::landDistances");
//initialize
std::deque<int> aFillQueue;
for(int i=0;i<GC.getMapINLINE().numPlotsINLINE();i++)
{
CvPlot* pLoopPlot = GC.getMapINLINE().plotByIndexINLINE(i);
if(!pLoopPlot->isWater())
{
aLandDistances[i] = 0;
aFillQueue.push_back(i);
}
}
//fill
while(!aFillQueue.empty())
{
int iLoopIndex = aFillQueue.front();
aFillQueue.pop_front();
CvPlot* pLoopPlot = GC.getMapINLINE().plotByIndexINLINE(iLoopIndex);
int iLoopDistance = aLandDistances[iLoopIndex] + 1;
for(int iDirection=0;iDirection<NUM_DIRECTION_TYPES;iDirection++)
{
CvPlot* pAdjacentPlot = plotDirection(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), (DirectionTypes) iDirection);
if(pAdjacentPlot != NULL)
{
int iAdjacentIndex = GC.getMapINLINE().plotNumINLINE(pAdjacentPlot->getX_INLINE(), pAdjacentPlot->getY_INLINE());
if(iLoopDistance < aLandDistances[iAdjacentIndex])
{
aLandDistances[iAdjacentIndex] = iLoopDistance;
aFillQueue.push_back(iAdjacentIndex);
}
}
}
}
}
bool bStartOnWater = GC.getCivilizationInfo(getCivilizationType()).isWaterStart();
std::deque<bool> abEuropeStart;
if (bStartOnWater)
{
for (int i = 0; i < GC.getNumEuropeInfos(); ++i)
{
abEuropeStart.push_back(GC.getEuropeInfo((EuropeTypes) i).isStart());
gDLL->getPythonIFace()->pythonIsEuropeStart((EuropeTypes) i, &abEuropeStart[i]);
}
}
for(int iPass = 0; iPass < 2; iPass++)
{
int iBestValue = 0;
int iBestIndex = -1;
std::vector<int> aiWeights(GC.getMapINLINE().numPlotsINLINE(), 0);
for (iI = 0; iI < GC.getMapINLINE().numPlotsINLINE(); iI++)
{
int iValue = 0;
pLoopPlot = GC.getMapINLINE().plotByIndexINLINE(iI);
if ((iBestArea == -1) || (pLoopPlot->getArea() == iBestArea) || (iPass > 0))
{
if (bStartOnWater)
{
EuropeTypes eEurope = pLoopPlot->getEurope();
if (eEurope != NO_EUROPE && abEuropeStart[eEurope])
{
iValue = 1;
switch ((CardinalDirectionTypes)GC.getEuropeInfo(eEurope).getCardinalDirection())
{
case CARDINALDIRECTION_EAST:
case CARDINALDIRECTION_WEST:
iValue *= 1 + std::min(::plotDistance(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), pLoopPlot->getX_INLINE(), 0), startingPlotRange());
iValue *= 1 + std::min(::plotDistance(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), pLoopPlot->getX_INLINE(), GC.getMapINLINE().getGridHeightINLINE() - 1), startingPlotRange() / 2);
break;
case CARDINALDIRECTION_NORTH:
case CARDINALDIRECTION_SOUTH:
iValue *= 1 + std::min(::plotDistance(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), 0, pLoopPlot->getY_INLINE()), startingPlotRange());
iValue *= 1 + std::min(::plotDistance(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), GC.getMapINLINE().getGridWidthINLINE() - 1, pLoopPlot->getY_INLINE()), startingPlotRange() / 2);
break;
default:
break;
}
int iMinPlotDistance = startingPlotRange();
for (int iPlayer = 0; iPlayer < MAX_PLAYERS; iPlayer++)
{
CvPlayer& otherPlayer = GET_PLAYER((PlayerTypes) iPlayer);
if ((iPlayer != getID()) && otherPlayer.isAlive())
{
CvPlot* pOtherPlot = otherPlayer.getStartingPlot();
if(pOtherPlot != NULL)
{
if(GC.getCivilizationInfo(otherPlayer.getCivilizationType()).isWaterStart())
{
int iPlotDistance = plotDistance(pLoopPlot->getX_INLINE(), pLoopPlot->getY_INLINE(), pOtherPlot->getX_INLINE(), pOtherPlot->getY_INLINE());
if (iPlotDistance < iMinPlotDistance)
{
iMinPlotDistance = iPlotDistance;
}
}
}
}
}
iValue *= 1 + iMinPlotDistance;
iValue /= (aLandDistances[iI] + 1);
}
}
else
{
//the distance factor is now done inside foundValue
iValue = pLoopPlot->getFoundValue(getID());
}
if (iValue > iBestValue)
{
iBestValue = iValue;
iBestIndex = iI;
}
}
FAssert(iValue >= 0);
aiWeights[iI] = std::max(0, iValue);
}
FAssert(std::accumulate(aiWeights.begin(), aiWeights.end(), 0) > 0);
if (bRandomize)
{
iBestIndex = GC.getGameINLINE().getSorenRand().pickValue(aiWeights, "Randomizing start");
}
return GC.getMapINLINE().plotByIndexINLINE(iBestIndex);
FAssertMsg(iPass != 0, "CvPlayer::findStartingPlot - could not find starting plot in first pass.");
}
FAssertMsg(false, "Could not find starting plot.");
return NULL;
}
CvCity* CvPlayer::initCity(int iX, int iY, bool bBumpUnits)
{
PROFILE_FUNC();
CvCity* pCity = addCity();
FAssertMsg(pCity != NULL, "City is not assigned a valid value");
FAssertMsg(!(GC.getMapINLINE().plotINLINE(iX, iY)->isCity()), "No city is expected at this plot when initializing new city");
pCity->init(pCity->getID(), getID(), iX, iY, bBumpUnits);
return pCity;
}
void CvPlayer::acquireCity(CvCity* pOldCity, bool bConquest, bool bTrade)
{
CLLNode<IDInfo>* pUnitNode;
CvCity* pNewCity;
CvUnit* pLoopUnit;
CvPlot* pCityPlot;
CvPlot* pLoopPlot;
bool* pabHasRealBuilding;
int* paiBuildingOriginalOwner;
int* paiBuildingOriginalTime;
CvWString szBuffer;
CvWString szName;
bool abEverOwned[MAX_PLAYERS];
int aiCulture[MAX_PLAYERS];
PlayerTypes eOldOwner;
PlayerTypes eOriginalOwner;
PlayerTypes eHighestCulturePlayer;
BuildingTypes eBuilding;
bool bForceUnowned;
bool bRecapture;
bool bRaze;
bool bGift;
int iRange;
int iCaptureGold;
int iGameTurnFounded;
int iPopulation;
int iHighestPopulation;
int iOccupationTimer;
int iTeamCulturePercent;
int iDamage;
int iDX, iDY;
int iI;
CLinkList<IDInfo> oldUnits;
pCityPlot = pOldCity->plot();
pUnitNode = pCityPlot->headUnitNode();
while (pUnitNode != NULL)
{
oldUnits.insertAtEnd(pUnitNode->m_data);
pUnitNode = pCityPlot->nextUnitNode(pUnitNode);
}
pUnitNode = oldUnits.head();
while (pUnitNode != NULL)
{
pLoopUnit = ::getUnit(pUnitNode->m_data);