Bazaar developer reference

CvUnitProductionAI::RecommendUnit(UnitAITypes)

Defined in
CvUnitProductionAI.cpp, line 213
Class
CvUnitProductionAI
Hook name
CivHooks::CvUnitProductionAI_RecommendUnit

Hooks

static bool BeforeCvUnitProductionAI_RecommendUnit(CvUnitProductionAI* self, UnitAITypes eUnitAIType, UnitTypes& result);static void AfterCvUnitProductionAI_RecommendUnit(CvUnitProductionAI* self, UnitAITypes eUnitAIType, UnitTypes& result);static UnitTypes ReplaceCvUnitProductionAI_RecommendUnit(UnitTypes (*original)(CvUnitProductionAI*, UnitAITypes), CvUnitProductionAI* self, UnitAITypes eUnitAIType);CivRegisterBefore<CivHooks::CvUnitProductionAI_RecommendUnit>(host, &BeforeCvUnitProductionAI_RecommendUnit);CivRegisterAfter<CivHooks::CvUnitProductionAI_RecommendUnit>(host, &AfterCvUnitProductionAI_RecommendUnit);CivRegisterReplace<CivHooks::CvUnitProductionAI_RecommendUnit>(host, &ReplaceCvUnitProductionAI_RecommendUnit);

Code

212/// Recommend highest-weighted unit213UnitTypes CvUnitProductionAI::RecommendUnit(UnitAITypes eUnitAIType)214{215	int iUnitLoop;216	int iWeight;217	int iTurnsLeft;218219	if(eUnitAIType <= NO_UNITAI)220	{221		return NO_UNIT;222	}223224	// Reset list of all the possible units225	m_Buildables.clear();226227	// Loop through adding the available units228	for(iUnitLoop = 0; iUnitLoop < GC.GetGameUnits()->GetNumUnits(); iUnitLoop++)229	{230		const UnitTypes eUnit = static_cast<UnitTypes>(iUnitLoop);231		CvUnitEntry* pkUnitInfo = GC.getUnitInfo(eUnit);232		if(pkUnitInfo)233		{234			// Make sure this unit can be built now235			if(m_pCity->canTrain(eUnit))236			{237				// Make sure it matches the requested unit AI type238				if(eUnitAIType == NO_UNITAI || pkUnitInfo->GetUnitAIType(eUnitAIType))239				{240					// Update weight based on turns to construct241					iTurnsLeft = m_pCity->getProductionTurnsLeft(eUnit, 0);242					iWeight = CityStrategyAIHelpers::ReweightByTurnsLeft(m_UnitAIWeights.GetWeight(eUnit), iTurnsLeft);243					m_Buildables.push_back(iUnitLoop, iWeight);244				}245			}246		}247248	}249250	// Sort items and grab the first one251	if(m_Buildables.size() > 0)252	{253		m_Buildables.SortItems();254		LogPossibleBuilds(eUnitAIType);255		return (UnitTypes)m_Buildables.GetElement(0);256	}257258	// Unless we didn't find any259	else260	{261		return NO_UNIT;262	}263}

Calls

Called by