Bazaar developer reference

CvGame::GetRandomSpawnUnitType(PlayerTypes, bool, bool)

Defined in
CvGame.cpp, line 7815
Class
CvGame
Hook name
CivHooks::CvGame_GetRandomSpawnUnitType

Hooks

static bool BeforeCvGame_GetRandomSpawnUnitType(CvGame* self, PlayerTypes ePlayer, bool bIncludeUUs, bool bIncludeRanged, UnitTypes& result);static void AfterCvGame_GetRandomSpawnUnitType(CvGame* self, PlayerTypes ePlayer, bool bIncludeUUs, bool bIncludeRanged, UnitTypes& result);static UnitTypes ReplaceCvGame_GetRandomSpawnUnitType(UnitTypes (*original)(CvGame*, PlayerTypes, bool, bool), CvGame* self, PlayerTypes ePlayer, bool bIncludeUUs, bool bIncludeRanged);CivRegisterBefore<CivHooks::CvGame_GetRandomSpawnUnitType>(host, &BeforeCvGame_GetRandomSpawnUnitType);CivRegisterAfter<CivHooks::CvGame_GetRandomSpawnUnitType>(host, &AfterCvGame_GetRandomSpawnUnitType);CivRegisterReplace<CivHooks::CvGame_GetRandomSpawnUnitType>(host, &ReplaceCvGame_GetRandomSpawnUnitType);

Code

7814/// Determine a random Unit type7815UnitTypes CvGame::GetRandomSpawnUnitType(PlayerTypes ePlayer, bool bIncludeUUs, bool bIncludeRanged)7816{7817	UnitTypes eBestUnit = NO_UNIT;7818	int iBestValue = 0;7819	int iValue = 0;7820	int iBonusValue;78217822	// Loop through all Unit Classes7823	for(int iUnitLoop = 0; iUnitLoop < GC.getNumUnitInfos(); iUnitLoop++)7824	{7825		bool bValid = false;7826		const UnitTypes eLoopUnit = static_cast<UnitTypes>(iUnitLoop);7827		CvUnitEntry* pkUnitInfo = GC.getUnitInfo(eLoopUnit);78287829		if(pkUnitInfo != NULL)7830		{7831			iBonusValue = 0;78327833			CvUnitClassInfo* pkUnitClassInfo = GC.getUnitClassInfo((UnitClassTypes)pkUnitInfo->GetUnitClassType());7834			if(pkUnitClassInfo)7835			{7836				// If this is NOT a UU, add extra value so that the default unit is more likely to get picked7837				if(eLoopUnit == pkUnitClassInfo->getDefaultUnitIndex())7838					iBonusValue += 700;7839				else if(!bIncludeUUs)7840					continue;7841			}78427843			bValid = (pkUnitInfo->GetCombat() > 0);7844			if(bValid)7845			{7846				// Unit has combat strength, make sure it isn't only defensive (and with no ranged combat ability)7847				if(pkUnitInfo->GetRange() == 0)7848				{7849					for(int iLoop = 0; iLoop < GC.getNumPromotionInfos(); iLoop++)7850					{7851						const PromotionTypes ePromotion = static_cast<PromotionTypes>(iLoop);7852						CvPromotionEntry* pkPromotionInfo = GC.getPromotionInfo(ePromotion);7853						if(pkPromotionInfo)7854						{7855							if(pkUnitInfo->GetFreePromotions(iLoop))7856							{7857								if(pkPromotionInfo->IsOnlyDefensive())7858								{7859									bValid = false;7860									break;7861								}7862							}7863						}7864					}7865				}7866			}78677868			if(!bValid)7869				continue;78707871			// Avoid Recon units7872			if(pkUnitInfo->GetDefaultUnitAIType() == UNITAI_EXPLORE)7873				continue;78747875			// No Ranged units?7876			if(!bIncludeRanged && pkUnitInfo->GetRangedCombat() > 0)7877				continue;78787879			// Must be land Unit7880			if(pkUnitInfo->GetDomainType() != DOMAIN_LAND)7881				continue;78827883#if defined(MOD_BUGFIX_NO_HOVERING_REBELS)7884			// Must NOT be a hovering unit7885			for(int iI = 0; iI < GC.getNumPromotionInfos(); iI++)7886			{7887				if(pkUnitInfo->GetFreePromotions(iI))7888				{7889					if(GC.getPromotionInfo((PromotionTypes) iI)->IsHoveringUnit())7890						continue;7891				}7892			}7893#endif78947895			// Must be able to train this thing7896			if(!GET_PLAYER(ePlayer).canTrain(eLoopUnit, false, false, false, /*bIgnoreUniqueUnitStatus*/ true))7897				continue;78987899			// Random weighting7900			iValue = (1 + GC.getGame().getJonRandNum(1000, "Minor Civ Unit spawn Selection"));7901			iValue += iBonusValue;79027903			if(iValue > iBestValue)7904			{7905				eBestUnit = eLoopUnit;7906				iBestValue = iValue;7907			}7908		}7909	}79107911	return eBestUnit;7912}

Calls

Called by