Bazaar developer reference

CvGame::GetCsGiftSpawnUnitType(PlayerTypes, bool)

Defined in
CvGame.cpp, line 8040
Class
CvGame
Hook name
CivHooks::CvGame_GetCsGiftSpawnUnitType

Hooks

static bool BeforeCvGame_GetCsGiftSpawnUnitType(CvGame* self, PlayerTypes ePlayer, bool bIncludeShips, UnitTypes& result);static void AfterCvGame_GetCsGiftSpawnUnitType(CvGame* self, PlayerTypes ePlayer, bool bIncludeShips, UnitTypes& result);static UnitTypes ReplaceCvGame_GetCsGiftSpawnUnitType(UnitTypes (*original)(CvGame*, PlayerTypes, bool), CvGame* self, PlayerTypes ePlayer, bool bIncludeShips);CivRegisterBefore<CivHooks::CvGame_GetCsGiftSpawnUnitType>(host, &BeforeCvGame_GetCsGiftSpawnUnitType);CivRegisterAfter<CivHooks::CvGame_GetCsGiftSpawnUnitType>(host, &AfterCvGame_GetCsGiftSpawnUnitType);CivRegisterReplace<CivHooks::CvGame_GetCsGiftSpawnUnitType>(host, &ReplaceCvGame_GetCsGiftSpawnUnitType);

Code

8038/// Pick a random a Unit type that is ranked by unit power and restricted to recon units available to ePlayer's technology8039#if defined(MOD_GLOBAL_CS_GIFT_SHIPS)8040UnitTypes CvGame::GetCsGiftSpawnUnitType(PlayerTypes ePlayer, bool bIncludeShips)8041#else8042UnitTypes CvGame::GetCsGiftSpawnUnitType(PlayerTypes ePlayer)8043#endif8044{8045	CvAssertMsg(ePlayer >= 0, "ePlayer is expected to be non-negative (invalid Index)");8046	CvAssertMsg(ePlayer < MAX_CIV_PLAYERS, "ePlayer is expected to be within maximum bounds (invalid Index)");80478048	CvWeightedVector<UnitTypes, SAFE_ESTIMATE_NUM_UNITS, true> veUnitRankings;80498050	// Loop through all Unit Classes8051	for (int iUnitLoop = 0; iUnitLoop < GC.getNumUnitInfos(); iUnitLoop++) {8052		const UnitTypes eLoopUnit = (UnitTypes) iUnitLoop;8053		CvUnitEntry* pkUnitInfo = GC.getUnitInfo(eLoopUnit);8054		if (pkUnitInfo == NULL) continue;8055		8056		bool bValid = (pkUnitInfo->GetUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_RECON", true));8057		bValid = bValid || (pkUnitInfo->GetUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_ARCHER", true));8058		bValid = bValid || (pkUnitInfo->GetUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_MOUNTED", true));8059		bValid = bValid || (pkUnitInfo->GetUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_HELICOPTER", true));80608061#if defined(MOD_GLOBAL_CS_GIFT_SHIPS)8062		// Exclude carrier units8063		if (pkUnitInfo->GetSpecialCargo() == (SpecialUnitTypes) GC.getInfoTypeForString("SPECIALUNIT_FIGHTER")) continue;80648065		// Include shipping8066		bValid = bValid || (bIncludeShips && (pkUnitInfo->GetUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_NAVALMELEE", true)));8067		bValid = bValid || (bIncludeShips && (pkUnitInfo->GetUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_NAVALRANGED", true)));8068#endif80698070		if (!bValid) continue;80718072		CvUnitClassInfo* pkUnitClassInfo = GC.getUnitClassInfo((UnitClassTypes) pkUnitInfo->GetUnitClassType());8073		if (pkUnitClassInfo == NULL) continue;80748075		// Exclude unique units8076		if (eLoopUnit != pkUnitClassInfo->getDefaultUnitIndex()) continue;80778078		// Must be able to train this thing8079		if (!GET_PLAYER(ePlayer).canTrain(eLoopUnit, false, false, false, /*bIgnoreUniqueUnitStatus*/ true)) continue;80808081		// CUSTOMLOG("CS Gift considering unit type %i", eLoopUnit);8082		veUnitRankings.push_back(eLoopUnit, pkUnitInfo->GetPower());8083	}80848085	// Choose from weighted unit types8086	veUnitRankings.SortItems();8087	int iNumChoices = GC.getUNIT_SPAWN_NUM_CHOICES();8088	RandomNumberDelegate randFn = MakeDelegate(&GC.getGame(), &CvGame::getJonRandNum);8089	UnitTypes eChosenUnit = veUnitRankings.ChooseFromTopChoices(iNumChoices, &randFn, "Choosing competitive unit from top choices");80908091	return eChosenUnit;8092}

Calls

Called by