void CvPlayerAI::AI_considerAnnex()
{
AI_PERF("AI-perf.csv", "AI_ considerAnnex");
// if the empire is very unhappy, don't consider annexing
if (IsEmpireVeryUnhappy())
{
return;
}
// if we're going for a culture victory, don't consider annexing
if (GetDiplomacyAI()->IsGoingForCultureVictory())
{
return;
}
// for Venice
if (GetPlayerTraits()->IsNoAnnexing())
{
return;
}
// if their capital city is puppeted, annex it
CvCity* pCity = getCapitalCity();
if (pCity && pCity->IsPuppet())
{
// we should only annex one city a turn, and sense this is one, we're done!
pCity->DoAnnex();
return;
}
//annex holy city for faith use
if (GetReligions()->GetCurrentReligion(false) != NO_RELIGION && pCity->GetCityReligions()->IsHolyCityForReligion(GetReligions()->GetCurrentReligion(false)))
{
pCity->DoAnnex();
return;
}
BuildingClassTypes eCourthouseType = NO_BUILDINGCLASS;
// find courthouse
for (int eBuildingType = 0; eBuildingType < GC.getNumBuildingInfos(); eBuildingType++)
{
const BuildingTypes eBuilding = static_cast<BuildingTypes>(eBuildingType);
CvBuildingEntry* buildingInfo = GC.getBuildingInfo(eBuilding);
if (buildingInfo)
{
if (buildingInfo->IsNoOccupiedUnhappiness() && canConstruct(eBuilding))
{
eCourthouseType = (BuildingClassTypes)buildingInfo->GetBuildingClassType();
break;
}
}
}
//Can't build a courthouse? Abort!
if (eCourthouseType == NO_BUILDINGCLASS)
return;
int iLoop = 0;
for (pCity = firstCity(&iLoop); pCity != NULL; pCity = nextCity(&iLoop))
{
//if we're already converting one, stop!
if (pCity->IsOccupied() && !pCity->IsNoOccupiedUnhappiness())
return;
}
std::vector<CityAndProduction> aCityAndProductions;
pCity = NULL;
for(pCity = firstCity(&iLoop); pCity != NULL; pCity = nextCity(&iLoop))
{
//simple check to stop razing "good" cities
if (pCity->IsRazing() && pCity->HasAnyWonder() && !IsEmpireVeryUnhappy())
unraze(pCity);
if (pCity->IsResistance())
continue;
if (!pCity->IsPuppet())
continue;
//Original City and puppeted? Stop!
if(pCity->getOriginalOwner() == GetID())
{
pCity->DoAnnex();
return;
}
if (pCity->IsOriginalMajorCapital())
{
pCity->DoAnnex();
return;
}
if (pCity->isBorderCity() || pCity->isCoastal())
{
pCity->DoAnnex();
return;
}
CityAndProduction kEval;
kEval.pCity = pCity;
kEval.iProduction = pCity->getYieldRateTimes100(YIELD_PRODUCTION, false);
aCityAndProductions.push_back(kEval);
}
std::stable_sort(aCityAndProductions.begin(), aCityAndProductions.end(), CityAndProductionEval());
CvCity* pTargetCity = NULL;
float fCutoffValue = /*0.55*/ GC.getNORMAL_ANNEX();
bool bCourthouseImprovement = false;
if (eCourthouseType != NO_BUILDINGCLASS)
{
if (GetPlayerPolicies()->GetBuildingClassProductionModifier(eCourthouseType) > 0)
{
bCourthouseImprovement = true;
}
}
if (bCourthouseImprovement)
{
fCutoffValue = /*0.8*/ GC.getAGGRESSIVE_ANNEX();
}
uint uiCutOff = (uint)(aCityAndProductions.size() * fCutoffValue);
for (uint ui = 0; ui < uiCutOff; ui++)
{
if (aCityAndProductions[ui].pCity->IsPuppet())
{
pTargetCity = aCityAndProductions[ui].pCity;
break;
}
#if defined(MOD_BALANCE_CORE)
if(aCityAndProductions[ui].pCity->IsRazing())
{
pTargetCity = aCityAndProductions[ui].pCity;
break;
}
#endif
}
if (pTargetCity)
{
if (pTargetCity->IsRazing())
unraze(pTargetCity);
if (!pTargetCity->IsResistance())
pTargetCity->DoAnnex();
}
}