-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCvBuilderTaskingAI.cpp
2304 lines (1996 loc) · 57.8 KB
/
CvBuilderTaskingAI.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 "ICvDLLUserInterface.h"
#include "CvGameCoreUtils.h"
#include "CvBuilderTaskingAI.h"
#include "CvPlayer.h"
#include "CvAStar.h"
#include "CvImprovementClasses.h"
#include "CvCityConnections.h"
// include after all other headers
#include "LintFree.h"
CvWeightedVector<BuilderDirective, 100> CvBuilderTaskingAI::m_aDirectives;
FStaticVector<int, SAFE_ESTIMATE_NUM_EXTRA_PLOTS, true, c_eCiv5GameplayDLL, 0> CvBuilderTaskingAI::m_aiNonTerritoryPlots; // plots that we need to evaluate that are outside of our territory
/// Constructor
CvBuilderTaskingAI::CvBuilderTaskingAI(void)
{
Uninit();
}
/// Destructor
CvBuilderTaskingAI::~CvBuilderTaskingAI(void)
{
Uninit();
}
/// Init
void CvBuilderTaskingAI::Init (CvPlayer* pPlayer)
{
m_pPlayer = pPlayer;
m_eRepairBuild = GetRepairBuild();
m_eFalloutFeature = GetFalloutFeature();
m_eFalloutRemove = GetFalloutRemove();
m_aiNonTerritoryPlots.clear();
m_bLogging = GC.getLogging() && GC.getAILogging() && GC.GetBuilderAILogging();
m_iNumCities = -1;
m_pTargetPlot = NULL;
}
/// Uninit
void CvBuilderTaskingAI::Uninit (void)
{
m_eRepairBuild = NO_BUILD;
m_pPlayer = NULL;
m_bLogging = false;
m_iNumCities = -1;
m_pTargetPlot = NULL;
}
/// Serialization read
void CvBuilderTaskingAI::Read (FDataStream& kStream)
{
// Version number to maintain backwards compatibility
uint uiVersion;
kStream >> uiVersion;
kStream >> m_eRepairBuild;
uint uiNumSlots;
// non-territory plots
kStream >> uiNumSlots;
m_aiNonTerritoryPlots.resize(uiNumSlots);
for (uint ui = 0; ui < uiNumSlots; ui++)
{
kStream >> m_aiNonTerritoryPlots[ui];
}
m_iNumCities = -1; //Force everyone to do an CvBuilderTaskingAI::Update() after loading
m_pTargetPlot = NULL; //Force everyone to recalculate current yields after loading.
}
/// Serialization write
void CvBuilderTaskingAI::Write(FDataStream& kStream)
{
// Current version number
uint uiVersion = 1;
kStream << uiVersion;
kStream << m_eRepairBuild;
// non-territory plots
kStream << m_aiNonTerritoryPlots.size();
for (uint ui = 0; ui < m_aiNonTerritoryPlots.size(); ui++)
{
kStream << m_aiNonTerritoryPlots[ui];
}
}
/// Update
void CvBuilderTaskingAI::Update (void)
{
UpdateRoutePlots();
m_iNumCities = m_pPlayer->getNumCities();
int iLoop;
CvCity *pCity;
for (pCity = m_pPlayer->firstCity(&iLoop); pCity != NULL; pCity = m_pPlayer->nextCity(&iLoop))
{
pCity->GetCityStrategyAI()->UpdateBestYields();
}
if (m_bLogging)
{
bool bShowOutput = m_pPlayer->isHuman();
if (m_pPlayer->IsEmpireUnhappy())
{
CvString str = "// Empire Unhappy! //";
LogInfo(str, m_pPlayer, bShowOutput);
}
// show crisis states
CvCity* pLoopCity;
for (pLoopCity = m_pPlayer->firstCity(&iLoop); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iLoop))
{
CvString str;
str += "// ";
CvString strCityName;
strCityName = pLoopCity->getName();
str += strCityName;
str += " \\\\";
LogInfo(str, m_pPlayer, bShowOutput);
for (uint ui = 0; ui < NUM_YIELD_TYPES; ui++)
{
//double fYield = pLoopCity->GetCityStrategyAI()->GetYieldAverage((YieldTypes)ui);
//double fYieldDeficient = pLoopCity->GetCityStrategyAI()->GetDeficientYieldValue((YieldTypes)ui);
CvString strYield;
switch (ui)
{
case YIELD_FOOD:
strYield = "food ";
break;
case YIELD_PRODUCTION:
strYield = "production ";
break;
case YIELD_SCIENCE:
strYield = "science ";
break;
case YIELD_GOLD:
strYield = "gold ";
break;
}
CvString strNumbers;
strNumbers.Format("%d, %d", pLoopCity->GetCityStrategyAI()->GetBestYieldAverageTimes100((YieldTypes)ui), pLoopCity->GetCityStrategyAI()->GetYieldDeltaTimes100((YieldTypes)ui));
//int iYieldAdjusted = (int)workerround(fYield * 100);
//int iYieldDeficientAdjacent = (int)workerround(fYieldDeficient * 100);
//strNumbers.Format("%d / %d", iYieldAdjusted, iYieldDeficientAdjacent);
strYield += strNumbers;
if (ui == pLoopCity->GetCityStrategyAI()->GetFocusYield())
{
strYield += " *";
}
//if (iYieldAdjusted < iYieldDeficientAdjacent)
//{
//if (GetDeficientYield(pLoopCity, false) != GetDeficientYield(pLoopCity, true))
//{
// strYield += " Problem, but happiness over is overriding it";
//}
//else
//{
// strYield += " PROBLEM!!";
//}
//}
LogInfo(strYield, m_pPlayer, bShowOutput);
}
str = "\\\\ end ";
str += strCityName;
str += " //";
LogInfo(str, m_pPlayer, bShowOutput);
}
}
}
int GetPlotYield (CvPlot* pPlot, YieldTypes eYield)
{
if (pPlot->getTerrainType() == NO_TERRAIN)
{
return 0;
}
return pPlot->calculateNatureYield(eYield, NO_TEAM);;
}
void CvBuilderTaskingAI::ConnectCities (CvCity* pPlayerCapital, CvCity* pTargetCity, RouteTypes eRoute)
{
bool bMajorMinorConnection = false;
if (pTargetCity->getOwner() != pPlayerCapital->getOwner())
{
bMajorMinorConnection = true;
}
//CvPlayer* pMajorOwner = m_pPlayer;
//if (bMajorMinorConnection)
//{
// if (m_pPlayer->isMinorCiv())
// {
// if (!GET_PLAYER(pPlayerCapital->getOwner()).isMinorCiv())
// {
// pMajorOwner = &(GET_PLAYER(pPlayerCapital->getOwner()));
// }
// else
// {
// pMajorOwner = &(GET_PLAYER(pTargetCity->getOwner()));
// }
// }
//}
bool bIndustrialRoute = false;
if (GC.getGame().GetIndustrialRoute() == eRoute)
{
bIndustrialRoute = true;
}
// if we already have a connection, bail out
if (bIndustrialRoute && pTargetCity->IsIndustrialRouteToCapital())
{
return;
}
else if (m_pPlayer->IsCapitalConnectedToCity(pTargetCity, eRoute))
{
return;
}
int iGoldForRoute = 0;
if (!bMajorMinorConnection)
{
iGoldForRoute = m_pPlayer->GetTreasury()->GetRouteGoldTimes100(pTargetCity) / 100;
}
CvRouteInfo* pRouteInfo = GC.getRouteInfo(eRoute);
if (!pRouteInfo)
{
return;
}
int iMaintenancePerTile = pRouteInfo->GetGoldMaintenance();
if (iMaintenancePerTile < 0) // div by zero check
{
return;
}
// build a path between the two cities
int iPathfinderFlags = m_pPlayer->GetID();
int iRouteValue = eRoute + 1;
// assuming that there are fewer than 256 players
iPathfinderFlags |= (iRouteValue << 8);
bool bFoundPath = GC.GetBuildRouteFinder().GeneratePath(pPlayerCapital->plot()->getX(), pPlayerCapital->plot()->getY(), pTargetCity->plot()->getX(), pTargetCity->plot()->getY(), iPathfinderFlags);
// if no path, then bail!
if (!bFoundPath)
{
return;
}
// walk the path
CvPlot* pPlot = NULL;
// go through the route to see how long it is and how many plots already have roads
int iRoadLength = 0;
int iPlotsNeeded = 0;
CvAStarNode* pNode = GC.GetBuildRouteFinder().GetLastNode();
while (pNode)
{
pPlot = GC.getMap().plotCheckInvalid(pNode->m_iX, pNode->m_iY);
pNode = pNode->m_pParent;
if (!pPlot)
{
break;
}
CvCity* pCity = pPlot->getPlotCity();
if (pCity && pCity->getTeam() == m_pPlayer->getTeam())
{
continue;
}
if (pPlot->getRouteType() == eRoute && !pPlot->IsRoutePillaged())
{
// if this is already a trade route or someone else built it, we can count is as free
if (pPlot->IsTradeRoute(m_pPlayer->GetID()) || pPlot->GetPlayerResponsibleForRoute() != m_pPlayer->GetID())
{
continue;
}
iRoadLength++;
}
else
{
iRoadLength++;
iPlotsNeeded++;
}
}
// This is very odd
if (iRoadLength <= 0 || iPlotsNeeded <= 0)
{
return;
}
short sValue = -1;
int iProfit = iGoldForRoute - (iRoadLength * iMaintenancePerTile);
if (bIndustrialRoute)
{
if (iProfit >= 0)
{
sValue = MAX_SHORT;
}
else if (m_pPlayer->calculateGoldRate() + iProfit >= 0)
{
sValue = pTargetCity->getYieldRate(YIELD_PRODUCTION) * GC.getINDUSTRIAL_ROUTE_PRODUCTION_MOD();
}
else
{
return;
}
}
else if (bMajorMinorConnection)
{
sValue = min(GC.getMINOR_CIV_ROUTE_QUEST_WEIGHT() / iPlotsNeeded, MAX_SHORT);
}
else // normal route
{
// is this route worth building?
if (iProfit < 0)
{
return;
}
int iValue = (iGoldForRoute * 100) / (iRoadLength * (iMaintenancePerTile + 1));
iValue = (iValue * iRoadLength) / iPlotsNeeded;
sValue = min(iValue, MAX_SHORT);
}
pPlot = NULL;
pNode = GC.GetBuildRouteFinder().GetLastNode();
int iGameTurn = GC.getGame().getGameTurn();
while (pNode)
{
pPlot = GC.getMap().plotCheckInvalid(pNode->m_iX, pNode->m_iY);
pNode = pNode->m_pParent;
if (!pPlot)
{
break;
}
if (pPlot->getRouteType() == eRoute && !pPlot->IsRoutePillaged())
{
continue;
}
// if we already know about this plot, continue on
if (pPlot->GetBuilderAIScratchPadTurn() == iGameTurn && pPlot->GetBuilderAIScratchPadPlayer() == m_pPlayer->GetID())
{
if (sValue > pPlot->GetBuilderAIScratchPadValue())
{
pPlot->SetBuilderAIScratchPadValue(sValue);
pPlot->SetBuilderAIScratchPadRoute(eRoute);
}
continue;
}
// mark nodes and reset values
pPlot->SetBuilderAIScratchPadTurn(iGameTurn);
pPlot->SetBuilderAIScratchPadPlayer(m_pPlayer->GetID());
pPlot->SetBuilderAIScratchPadValue(sValue);
pPlot->SetBuilderAIScratchPadRoute(eRoute);
// add nodes that are not in territory to extra list
// minors should not build out of their borders when they are doing a major/minor connection, only when connecting their two cities
if (!(m_pPlayer->isMinorCiv() && bMajorMinorConnection))
{
if (pPlot->getOwner() != m_pPlayer->GetID())
{
m_aiNonTerritoryPlots.push_back(GC.getMap().plotNum(pPlot->getX(), pPlot->getY()));
}
}
}
}
/// Looks at city connections and marks plots that can be added as routes by EvaluateBuilder
void CvBuilderTaskingAI::UpdateRoutePlots (void)
{
m_aiNonTerritoryPlots.clear();
// if there are fewer than 2 cities, we don't need to run this function
if (m_pPlayer->GetCityConnections()->GetNumConnectableCities() < 2)
{
return;
}
RouteTypes eBestRoute = m_pPlayer->getBestRoute();
if (eBestRoute == NO_ROUTE)
{
return;
}
// find a builder, if I don't have a builder, bail!
CvUnit* pBuilder = NULL;
CvUnit* pLoopUnit;
int iLoopUnit;
for (pLoopUnit = m_pPlayer->firstUnit(&iLoopUnit); pLoopUnit != NULL; pLoopUnit = m_pPlayer->nextUnit(&iLoopUnit))
{
if (pLoopUnit->AI_getUnitAIType() == UNITAI_WORKER)
{
pBuilder = pLoopUnit;
break;
}
}
// If there's no builder, bail!
if (!pBuilder)
{
return;
}
// updating plots that are part of the road network
CvCityConnections* pCityConnections = m_pPlayer->GetCityConnections();
for (int i = 0; i < GC.getNumBuildInfos(); i++)
{
BuildTypes eBuild = (BuildTypes)i;
CvBuildInfo* pkBuild = GC.getBuildInfo(eBuild);
if (!pkBuild)
{
continue;
}
RouteTypes eRoute = (RouteTypes)pkBuild->getRoute();
if (eRoute == NO_ROUTE)
{
continue;
}
if (GC.getBuildInfo(eBuild)->getTechPrereq() != NO_TECH)
{
bool bHasTech = GET_TEAM(m_pPlayer->getTeam()).GetTeamTechs()->HasTech((TechTypes)GC.getBuildInfo(eBuild)->getTechPrereq());
if (!bHasTech)
{
continue;
}
}
for (uint uiFirstCityIndex = 0; uiFirstCityIndex < pCityConnections->m_aiCityPlotIDs.size(); uiFirstCityIndex++)
{
for (uint uiSecondCityIndex = uiFirstCityIndex + 1; uiSecondCityIndex < pCityConnections->m_aiCityPlotIDs.size(); uiSecondCityIndex++)
{
// get the two cities
CvCity* pFirstCity = pCityConnections->GetCityFromIndex(uiFirstCityIndex);
CvCity* pSecondCity = pCityConnections->GetCityFromIndex(uiSecondCityIndex);
CvCity* pPlayerCapitalCity = NULL;
CvCity* pTargetCity = NULL;
if (!pFirstCity || !pSecondCity)
{
continue;
}
// only need to build roads to the capital
if (!pFirstCity->isCapital() && !pSecondCity->isCapital())
{
continue;
}
if (pFirstCity->isCapital() && pFirstCity->getOwner() == m_pPlayer->GetID())
{
pPlayerCapitalCity = pFirstCity;
pTargetCity = pSecondCity;
}
else
{
pPlayerCapitalCity = pSecondCity;
pTargetCity = pFirstCity;
}
ConnectCities(pPlayerCapitalCity, pTargetCity, eRoute);
}
}
}
}
int CorrectWeight (int iWeight)
{
if (iWeight < -1000)
{
return MAX_INT;
}
else
{
return iWeight;
}
}
/// Use the flavor settings to determine what the worker should do
bool CvBuilderTaskingAI::EvaluateBuilder ( CvUnit* pUnit, BuilderDirective* paDirectives, UINT uaDirectives, bool bOnlyKeepBest, bool bOnlyEvaluateWorkersPlot)
{
// number of cities has changed mid-turn, so we need to re-evaluate what workers should do
if (m_pPlayer->getNumCities() != m_iNumCities)
{
Update();
}
CvAssertMsg(uaDirectives > 0, "Need more than one directive");
for (uint ui = 0; ui < uaDirectives; ui++)
{
paDirectives[ui].m_eDirective = BuilderDirective::NUM_DIRECTIVES;
}
m_aDirectives.clear();
// check for no brainer bail-outs
// if the builder is already building something
if (pUnit->getBuildType() != NO_BUILD)
{
paDirectives[0].m_eDirective = BuilderDirective::BUILD_IMPROVEMENT;
paDirectives[0].m_eBuild = pUnit->getBuildType();
paDirectives[0].m_sX = pUnit->getX();
paDirectives[0].m_sY = pUnit->getY();
//inDirective.m_sGoldCost = 0;
paDirectives[0].m_sMoveTurnsAway = 0;
return true;
}
m_aiPlots.clear();
if (bOnlyEvaluateWorkersPlot)
{
// can't build on plots others own
PlayerTypes eOwner = pUnit->plot()->getOwner();
if (eOwner == m_pPlayer->GetID())
{
m_aiPlots.push_back(pUnit->plot()->GetPlotIndex());
}
}
else
{
m_aiPlots = m_pPlayer->GetPlots();
}
// go through all the plots the player has under their control
for (uint uiPlotIndex = 0; uiPlotIndex < m_aiPlots.size(); uiPlotIndex++)
{
// when we encounter the first plot that is invalid, the rest of the list will be invalid
if (m_aiPlots[uiPlotIndex] == -1)
{
if (m_bLogging)
{
CvString strLog = "end of plot list";
LogInfo(strLog, m_pPlayer);
}
break;
}
CvPlot* pPlot = GC.getMap().plotByIndex(m_aiPlots[uiPlotIndex]);
if (!ShouldBuilderConsiderPlot(pUnit, pPlot))
{
continue;
}
// distance weight
// find how many turns the plot is away
int iMoveTurnsAway = FindTurnsAway(pUnit, pPlot);
if (iMoveTurnsAway < 0)
{
if (m_bLogging)
{
CvString strLog;
strLog.Format("unitx: %d unity: %d, plotx: %d ploty: %d, can't find path", pUnit->getX(), pUnit->getY(), pPlot->getX(), pPlot->getY());
LogInfo(strLog, m_pPlayer);
}
continue;
}
UpdateCurrentPlotYields(pPlot);
//AddRepairDirectives(pUnit, pPlot, iMoveTurnsAway);
AddRouteDirectives(pUnit, pPlot, iMoveTurnsAway);
AddImprovingResourcesDirectives(pUnit, pPlot, iMoveTurnsAway);
AddImprovingPlotsDirectives(pUnit, pPlot, iMoveTurnsAway);
AddChopDirectives(pUnit, pPlot, iMoveTurnsAway);
AddScrubFalloutDirectives(pUnit, pPlot, iMoveTurnsAway);
// only AIs have permission to remove roads
if (!m_pPlayer->isHuman())
{
//AddRemoveUselessRoadDirectives(pUnit, pPlot, iMoveTurnsAway);
}
}
// we need to evaluate the tiles outside of our territory to build roads
for (uint ui = 0; ui < m_aiNonTerritoryPlots.size(); ui++)
{
//FAssertMsg(!m_pPlayer->isMinorCiv(), "MinorCivs should have no nonterritory plots");
CvPlot* pPlot = GC.getMap().plotByIndex(m_aiNonTerritoryPlots[ui]);
CvAssertMsg(pPlot != NULL, "Plot should not be NULL");
if (!pPlot)
continue;
if (bOnlyEvaluateWorkersPlot)
{
if (pPlot != pUnit->plot())
{
continue;
}
}
if (!ShouldBuilderConsiderPlot(pUnit, pPlot))
{
continue;
}
// distance weight
// find how many turns the plot is away
int iMoveTurnsAway = FindTurnsAway(pUnit, pPlot);
if (iMoveTurnsAway < 0)
{
if (m_bLogging)
{
CvString strLog;
strLog.Format("unitx: %d unity: %d, plotx: %d ploty: %d, Evaluating out of territory plot, can't find path", pUnit->getX(), pUnit->getY(), pPlot->getX(), pPlot->getY());
LogInfo(strLog, m_pPlayer);
}
continue;
}
if (m_bLogging)
{
CvString strLog;
strLog.Format("x: %d y: %d, Evaluating out of territory plot", pPlot->getX(), pPlot->getY());
LogInfo(strLog, m_pPlayer);
}
//AddRepairDirectives(pUnit, pPlot, iMoveTurnsAway);
AddRouteDirectives(pUnit, pPlot, iMoveTurnsAway);
}
m_aDirectives.SortItems();
int iBestWeight = 0;
int iAssignIndex = 0;
for (int i = 0; i < m_aDirectives.size(); i++)
{
// If this target was far away, we only estimated the time to get there. We need to be sure we have a real path there
CvPlot *pTarget = GC.getMap().plot(m_aDirectives.GetElement(i).m_sX, m_aDirectives.GetElement(i).m_sY);
CvAssertMsg(pTarget != NULL, "Not expecting the target to be NULL");
if (!pTarget)
continue;
int iPlotDistance = plotDistance(pUnit->getX(), pUnit->getY(), pTarget->getX(), pTarget->getY());
if (iPlotDistance >= GC.getAI_HOMELAND_ESTIMATE_TURNS_DISTANCE())
{
if (TurnsToReachTarget(pUnit, pTarget) == MAX_INT)
{
// No path, need to pick a new directive
continue;
}
}
if (iBestWeight == 0)
{
iBestWeight = m_aDirectives.GetWeight(i);
}
if (bOnlyKeepBest)
{
int iWeight = m_aDirectives.GetWeight(i);
if (iWeight < iBestWeight * 3 / 4)
{
break;
}
}
BuilderDirective directive = m_aDirectives.GetElement(i);
paDirectives[iAssignIndex] = directive;
iAssignIndex++;
// if we shouldn't copy over any more directives, then break
if (iAssignIndex >= (int)uaDirectives)
{
break;
}
}
if (m_bLogging)
{
if (m_aDirectives.size() > 0)
{
LogFlavors(NO_FLAVOR);
}
LogDirectives(pUnit);
}
//if (m_aDirectives.size() > 0 && iAssignIndex > 0)
if (iAssignIndex > 0)
{
if (m_bLogging)
{
LogDirective(paDirectives[0], pUnit, -1, true /*bChosen*/);
}
return true;
}
return false;
}
/// Evaluating a plot to see if we can build resources there
void CvBuilderTaskingAI::AddImprovingResourcesDirectives (CvUnit* pUnit, CvPlot* pPlot, int iMoveTurnsAway)
{
ImprovementTypes eExistingPlotImprovement = pPlot->getImprovementType();
// if we have a great improvement in a plot that's not pillaged, DON'T DO NOTHIN'
if (eExistingPlotImprovement != NO_IMPROVEMENT && GC.getImprovementInfo(eExistingPlotImprovement)->IsCreatedByGreatPerson() && !pPlot->IsImprovementPillaged())
{
return;
}
// check to see if a resource is here. If not, bail out!
ResourceTypes eResource = pPlot->getResourceType(m_pPlayer->getTeam());
if (eResource == NO_RESOURCE)
{
return;
}
CvResourceInfo* pkResource = GC.getResourceInfo(eResource);
if (pkResource == NULL || pkResource->getResourceUsage() == RESOURCEUSAGE_BONUS)
{
// evaluate bonus resources as normal improvements
return;
}
// loop through the build types to find one that we can use
BuildTypes eBuild;
BuildTypes eOriginalBuild;
int iBuildIndex;
for (iBuildIndex = 0; iBuildIndex < GC.getNumBuildInfos(); iBuildIndex++)
{
eBuild = (BuildTypes)iBuildIndex;
CvBuildInfo* pkBuild = GC.getBuildInfo(eBuild);
if(pkBuild == NULL)
continue;
eOriginalBuild = eBuild;
ImprovementTypes eImprovement = (ImprovementTypes)pkBuild->getImprovement();
if (eImprovement == NO_IMPROVEMENT)
{
continue;
}
CvImprovementEntry* pkImprovementInfo = GC.getImprovementInfo(eImprovement);
if (pkImprovementInfo == NULL || !pkImprovementInfo->IsImprovementResourceTrade(eResource))
{
continue;
}
if (eImprovement == eExistingPlotImprovement)
{
if (pPlot->IsImprovementPillaged())
{
eBuild = m_eRepairBuild;
}
else
{
// this plot already has the appropriate improvement to use the resource
break;
}
}
else
{
// if the plot has an unpillaged great person's creation on it, DO NOT DESTROY
if (eExistingPlotImprovement != NO_IMPROVEMENT)
{
CvImprovementEntry* pkExistingPlotImprovementInfo = GC.getImprovementInfo(eExistingPlotImprovement);
if(pkExistingPlotImprovementInfo && pkExistingPlotImprovementInfo->IsCreatedByGreatPerson())
{
continue;
}
}
}
if (!pUnit->canBuild(pPlot, eBuild))
{
break;
}
BuilderDirective::BuilderDirectiveType eDirectiveType = BuilderDirective::BUILD_IMPROVEMENT_ON_RESOURCE;
int iWeight = GC.getBUILDER_TASKING_BASELINE_BUILD_RESOURCE_IMPROVEMENTS();
if (eBuild == m_eRepairBuild)
{
eDirectiveType = BuilderDirective::REPAIR;
iWeight = GC.getBUILDER_TASKING_BASELINE_REPAIR();
}
iWeight = GetBuildCostWeight(iWeight, pPlot, eBuild);
// this is to deal with when the plot is already improved with another improvement that doesn't enable the resource
int iInvestedImprovementTime = 0;
if (eExistingPlotImprovement != NO_IMPROVEMENT)
{
BuildTypes eExistingBuild = NO_BUILD;
BuildTypes eBuild2 = NO_BUILD;
for (int iBuildIndex2 = 0; iBuildIndex2 < GC.getNumBuildInfos(); iBuildIndex2++)
{
eBuild2 = (BuildTypes)iBuildIndex2;
CvBuildInfo* pkBuild2 = GC.getBuildInfo(eBuild2);
if (pkBuild2 && pkBuild2->getImprovement() == eExistingPlotImprovement)
{
eExistingBuild = eBuild2;
break;
}
}
if (eExistingBuild != NO_BUILD)
{
iInvestedImprovementTime = pPlot->getBuildTime(eExistingBuild, m_pPlayer->GetID());
}
}
int iBuildTimeWeight = GetBuildTimeWeight(pUnit, pPlot, eBuild, DoesBuildHelpRush(pUnit, pPlot, eBuild), iInvestedImprovementTime + iMoveTurnsAway);
iWeight += iBuildTimeWeight;
iWeight = CorrectWeight(iWeight);
iWeight += GetResourceWeight(eResource, eImprovement, pPlot->getNumResource());
iWeight = CorrectWeight(iWeight);
UpdateProjectedPlotYields(pPlot, eBuild);
int iScore = ScorePlot();
if (iScore > 0)
{
iWeight *= iScore;
iWeight = CorrectWeight(iWeight);
}
{
CvCity* pLogCity = NULL;
int iProduction = pPlot->getFeatureProduction(eBuild, pUnit->getOwner(), &pLogCity);
if (DoesBuildHelpRush(pUnit, pPlot, eBuild))
{
iWeight += iProduction; // a nominal benefit for choosing this production
if (m_bLogging)
{
CvString strLog;
strLog.Format("Helps rush, %d", iProduction);
LogInfo(strLog, m_pPlayer);
}
}
}
if (iWeight <= 0)
{
continue;
}
BuilderDirective directive;
directive.m_eDirective = eDirectiveType;
directive.m_eBuild = eBuild;
directive.m_eResource = eResource;
directive.m_sX = pPlot->getX();
directive.m_sY = pPlot->getY();
//directive.m_iGoldCost = m_pPlayer->getBuildCost(pPlot, eBuild);
directive.m_sMoveTurnsAway = iMoveTurnsAway;
if (m_bLogging)
{
CvString strTemp;
strTemp.Format("%d, Build Time Weight, %d, Weight, %d", pUnit->GetID(), iBuildTimeWeight, iWeight);
LogInfo(strTemp, m_pPlayer);
}
m_aDirectives.push_back(directive, iWeight);
}
}
/// Evaluating a plot to determine what improvement could be best there
void CvBuilderTaskingAI::AddImprovingPlotsDirectives (CvUnit* pUnit, CvPlot* pPlot, int iMoveTurnsAway)
{
ImprovementTypes eExistingImprovement = pPlot->getImprovementType();
// if we have a great improvement in a plot that's not pillaged, DON'T DO NOTHIN'
if (eExistingImprovement != NO_IMPROVEMENT && GC.getImprovementInfo(eExistingImprovement)->IsCreatedByGreatPerson() && !pPlot->IsImprovementPillaged())
{
return;
}
// if it's not within a city radius
if (!pPlot->isWithinTeamCityRadius(pUnit->getTeam()))
{
return;
}
// check to see if a non-bonus resource is here. if so, bail out!
ResourceTypes eResource = pPlot->getResourceType(m_pPlayer->getTeam());
if (eResource != NO_RESOURCE)
{
if (GC.getResourceInfo(eResource)->getResourceUsage() != RESOURCEUSAGE_BONUS)
{
return;
}
}
CvCity* pCity = GetWorkingCity(pPlot);
if (!pCity)
{
return;
}
// loop through the build types to find one that we can use
BuildTypes eBuild;
BuildTypes eOriginalBuildType;
int iBuildIndex;
for (iBuildIndex = 0; iBuildIndex < GC.getNumBuildInfos(); iBuildIndex++)
{
eBuild = (BuildTypes)iBuildIndex;
eOriginalBuildType = eBuild;
CvBuildInfo* pkBuild = GC.getBuildInfo(eBuild);
if(pkBuild == NULL)
{
continue;
}
ImprovementTypes eImprovement = (ImprovementTypes)pkBuild->getImprovement();
if (eImprovement == NO_IMPROVEMENT)
{
continue;
}
// if this improvement has a defense modifier, ignore it for now
CvImprovementEntry *pImprovement = GC.getImprovementInfo(eImprovement);
if (pImprovement->GetDefenseModifier() > 0)
{
continue;
}
// for bonus resources, check to see if this is the improvement that connects it
if (eResource != NO_RESOURCE)
{
if (!pImprovement->IsImprovementResourceTrade(eResource))
{
continue;
}
}
if (eImprovement == pPlot->getImprovementType())
{
if (pPlot->IsImprovementPillaged())
{
eBuild = m_eRepairBuild;
}
else
{
continue;
}
}
else
{
// if the plot has an unpillaged great person's creation on it, DO NOT DESTROY
if (eExistingImprovement != NO_IMPROVEMENT)
{
if (pImprovement->IsCreatedByGreatPerson() || GET_PLAYER(pUnit->getOwner()).isOption(PLAYEROPTION_SAFE_AUTOMATION))
{
continue;
}
}
}
// Only check to make sure our unit can build this after possibly switching this to a repair build in the block of code above
if (!pUnit->canBuild(pPlot, eBuild))
{
continue;
}
if (GET_PLAYER(pUnit->getOwner()).isOption(PLAYEROPTION_LEAVE_FORESTS))
{
FeatureTypes eFeature = pPlot->getFeatureType();
if (eFeature != NO_FEATURE)
{