Bazaar developer reference

CvMilitaryAI::FindBestUnitToScrap(bool, bool, int &)

Defined in
CvMilitaryAI.cpp, line 3594
Class
CvMilitaryAI
Hook name
CivHooks::CvMilitaryAI_FindBestUnitToScrap

Hooks

static bool BeforeCvMilitaryAI_FindBestUnitToScrap(CvMilitaryAI* self, bool bLand, bool bDeficitForcedDisband, int& iReturnedScore, UnitHandle& result);static void AfterCvMilitaryAI_FindBestUnitToScrap(CvMilitaryAI* self, bool bLand, bool bDeficitForcedDisband, int& iReturnedScore, UnitHandle& result);static UnitHandle ReplaceCvMilitaryAI_FindBestUnitToScrap(UnitHandle (*original)(CvMilitaryAI*, bool, bool, int&), CvMilitaryAI* self, bool bLand, bool bDeficitForcedDisband, int& iReturnedScore);CivRegisterBefore<CivHooks::CvMilitaryAI_FindBestUnitToScrap>(host, &BeforeCvMilitaryAI_FindBestUnitToScrap);CivRegisterAfter<CivHooks::CvMilitaryAI_FindBestUnitToScrap>(host, &AfterCvMilitaryAI_FindBestUnitToScrap);CivRegisterReplace<CivHooks::CvMilitaryAI_FindBestUnitToScrap>(host, &ReplaceCvMilitaryAI_FindBestUnitToScrap);

Code

3593/// Score the strength of the units for a domain; best candidate to scrap (with lowest score) is returned. Only supports land and naval units3594UnitHandle CvMilitaryAI::FindBestUnitToScrap(bool bLand, bool bDeficitForcedDisband, int& iReturnedScore)3595{3596	CvUnit* pLoopUnit;3597	int iUnitLoop;3598	UnitHandle pBestUnit;3599	int iScore;3600	int iBestScore = MAX_INT;36013602	for(pLoopUnit = m_pPlayer->firstUnit(&iUnitLoop); pLoopUnit != NULL; pLoopUnit = m_pPlayer->nextUnit(&iUnitLoop))3603	{3604		bool bSkipThisOne = false;36053606		if(!pLoopUnit->IsCombatUnit())3607		{3608			continue;3609		}36103611		if(bLand && pLoopUnit->getDomainType() != DOMAIN_LAND)3612		{3613			continue;3614		}36153616		if(!bLand && pLoopUnit->getDomainType() != DOMAIN_SEA)3617		{3618			continue;3619		}36203621		// Following checks are for the case where the AI is trying to decide if it is a good idea to disband this unit (as opposed to when the game is FORCING the player to disband one)3622		if(!bDeficitForcedDisband)3623		{3624			if(bLand && m_eLandDefenseState == DEFENSE_STATE_CRITICAL)3625			{3626				continue;3627			}3628			else if(!bLand && m_eNavalDefenseState == DEFENSE_STATE_CRITICAL)3629			{3630				continue;3631			}36323633			// Is it in an army?3634			if(pLoopUnit->getArmyID() != FFreeList::INVALID_INDEX)3635			{3636				continue;3637			}36383639			// Can I still build this unit? If so too new to scrap3640			if(bLand && m_pPlayer->canTrain(pLoopUnit->getUnitType(), false /*bContinue*/, false /*bTestVisible*/, true /*bIgnoreCost*/))3641			{3642				continue;3643			}36443645			// Is this a unit who has an obsolete tech that I have researched?3646			CvUnitEntry& pUnitInfo = pLoopUnit->getUnitInfo();3647			if((TechTypes)pUnitInfo.GetObsoleteTech() == NO_TECH)3648			{3649				continue;3650			}36513652			if(!GET_TEAM(m_pPlayer->getTeam()).GetTeamTechs()->HasTech((TechTypes)(pUnitInfo.GetObsoleteTech())))3653			{3654				continue;3655			}36563657			// Is this unit's INTRINSIC power less than half that of the best unit I can build for this domain?3658			if((pLoopUnit->getUnitInfo().GetPower() * 2) >= GetPowerOfStrongestBuildableUnit(pLoopUnit->getDomainType()))3659			{3660				continue;3661			}36623663			// Does this unit's upgrade require a resource?3664			UnitTypes eUpgradeUnit = pLoopUnit->GetUpgradeUnitType();3665			if(eUpgradeUnit != NO_UNIT)3666			{3667				CvUnitEntry* pUpgradeUnitInfo = GC.GetGameUnits()->GetEntry(eUpgradeUnit);3668				if(pUpgradeUnitInfo != NULL)3669				{3670					for(int iResourceLoop = 0; iResourceLoop < GC.getNumResourceInfos() && !bSkipThisOne; iResourceLoop++)3671					{3672						ResourceTypes eResource = (ResourceTypes) iResourceLoop;3673						int iNumResourceNeeded = pUpgradeUnitInfo->GetResourceQuantityRequirement(eResource);36743675						if(iNumResourceNeeded > 0)3676						{3677							if(m_pPlayer->getNumResourceTotal(eResource) > 0)3678							{3679								if(bLand && m_eLandDefenseState > DEFENSE_STATE_NEUTRAL)3680								{3681									bSkipThisOne  = true;    // We'll wait and try to upgrade this one, our unit count isn't that bad3682								}3683								else if(!bLand && m_eNavalDefenseState > DEFENSE_STATE_NEUTRAL)3684								{3685									bSkipThisOne  = true;    // We'll wait and try to upgrade this one, our unit count isn't that bad3686								}3687							}3688						}3689					}3690				}3691			}3692		}36933694		// Can I scrap this unit?3695		if(!bSkipThisOne && pLoopUnit->canScrap())3696		{3697			iScore = pLoopUnit->GetPower();36983699			if(iScore < iBestScore)3700			{3701				iBestScore = iScore;3702				iReturnedScore = iBestScore;3703				pBestUnit = pLoopUnit;3704			}3705		}3706	}37073708	return pBestUnit;3709}

Calls

Called by