Code
5859/// Mark units that can damage key items as priority targets5860void CvTacticalAI::IdentifyPriorityTargets()5861{5862 CvCity* pLoopCity;5863 int iCityLoop;5864 FFastVector<CvTacticalTarget> possibleAttackers;5865 int iExpectedDamage;5866 int iExpectedTotalDamage;58675868 // Loop through each of our cities5869 for(pLoopCity = m_pPlayer->firstCity(&iCityLoop); pLoopCity != NULL; pLoopCity = m_pPlayer->nextCity(&iCityLoop))5870 {5871 // Compile a list of units that can attack it this turn and what their expected damage is5872 possibleAttackers.clear();5873 iExpectedTotalDamage = 0;58745875 CvTacticalTarget* pTarget;5876 pTarget = GetFirstUnitTarget();5877 while(pTarget != NULL)5878 {5879 CvPlot* pPlot = GC.getMap().plot(pTarget->GetTargetX(), pTarget->GetTargetY());5880 UnitHandle pEnemyUnit = pPlot->getVisibleEnemyDefender(m_pPlayer->GetID());5881 if(pEnemyUnit)5882 {5883 iExpectedDamage = 0;58845885 if(pEnemyUnit->IsCanAttackRanged() && pEnemyUnit->GetMaxRangedCombatStrength(NULL, /*pCity*/ NULL, true, true) > pEnemyUnit->GetMaxAttackStrength(NULL, pLoopCity->plot(), NULL))5886 {5887 if(plotDistance(pEnemyUnit->getX(), pEnemyUnit->getY(), pLoopCity->getX(), pLoopCity->getY()) <= pEnemyUnit->GetRange())5888 {5889 if(pEnemyUnit->canEverRangeStrikeAt(pLoopCity->getX(), pLoopCity->getY()))5890 {5891 iExpectedDamage = pEnemyUnit->GetRangeCombatDamage(NULL, pLoopCity, false);5892 }5893 }5894 }5895 else if(CanReachInXTurns(pEnemyUnit, pLoopCity->plot(), 1))5896 {5897 int iAttackerStrength = pEnemyUnit->GetMaxAttackStrength(NULL, pLoopCity->plot(), NULL);5898 int iDefenderStrength = pLoopCity->getStrengthValue();5899 CvUnit* pFireSupportUnit = CvUnitCombat::GetFireSupportUnit(pLoopCity->getOwner(), pLoopCity->getX(), pLoopCity->getY(), pEnemyUnit->getX(), pEnemyUnit->getY());5900 int iDefenderFireSupportCombatDamage = 0;5901 if(pFireSupportUnit != NULL)5902 {5903 iDefenderFireSupportCombatDamage = pFireSupportUnit->GetRangeCombatDamage(pEnemyUnit.pointer(), NULL, false);5904 }5905 iExpectedDamage = pEnemyUnit->getCombatDamage(iAttackerStrength, iDefenderStrength, pEnemyUnit->getDamage() + iDefenderFireSupportCombatDamage, /*bIncludeRand*/ false, /*bAttackerIsCity*/ false, /*bDefenderIsCity*/ true);5906 }5907 if(iExpectedDamage > 0)5908 {5909 iExpectedTotalDamage += iExpectedDamage;5910 possibleAttackers.push_back(*pTarget);5911 }5912 }5913 pTarget = GetNextUnitTarget();5914 }59155916 // If they can take the city down and they are a melee unit, then they are a high priority target5917 if(iExpectedTotalDamage > (pLoopCity->GetMaxHitPoints() - pLoopCity->getDamage()))5918 {5919 unsigned int iAttackerIndex = 0;59205921 // Loop until we've found all the attackers in the unit target list5922 pTarget = GetFirstUnitTarget();5923 while(iAttackerIndex < possibleAttackers.size() && pTarget != NULL)5924 {5925 // Match based on X, Y5926 if(pTarget->GetTargetX() == possibleAttackers[iAttackerIndex].GetTargetX() &&5927 pTarget->GetTargetY() == possibleAttackers[iAttackerIndex].GetTargetY())5928 {5929 CvPlot* pPlot = GC.getMap().plot(pTarget->GetTargetX(), pTarget->GetTargetY());5930 UnitHandle pEnemyUnit = pPlot->getVisibleEnemyDefender(m_pPlayer->GetID());5931 if(pEnemyUnit && pEnemyUnit->IsCanAttackRanged())5932 {5933 pTarget->SetTargetType(AI_TACTICAL_TARGET_MEDIUM_PRIORITY_UNIT);5934 }5935 else5936 {5937 pTarget->SetTargetType(AI_TACTICAL_TARGET_HIGH_PRIORITY_UNIT);5938 }5939 iAttackerIndex++;5940 }5941 pTarget = GetNextUnitTarget();5942 }5943 }59445945 // If they can damage a city they are a medium priority target5946 else if(possibleAttackers.size() > 0)5947 {5948 unsigned int iAttackerIndex = 0;59495950 // Loop until we've found all the attackers in the unit target list5951 pTarget = GetFirstUnitTarget();5952 while(iAttackerIndex < possibleAttackers.size() && pTarget != NULL)5953 {5954 // Match based on X, Y5955 if(pTarget->GetTargetX() == possibleAttackers[iAttackerIndex].GetTargetX() &&5956 pTarget->GetTargetY() == possibleAttackers[iAttackerIndex].GetTargetY())5957 {5958 if(pTarget->GetTargetType() != AI_TACTICAL_TARGET_HIGH_PRIORITY_UNIT)5959 {5960 pTarget->SetTargetType(AI_TACTICAL_TARGET_MEDIUM_PRIORITY_UNIT);5961 }5962 iAttackerIndex++;5963 }5964 pTarget = GetNextUnitTarget();5965 }5966 }5967 }5968}