Bazaar developer reference

CvMinorCivAI::DoSpawnUnit(PlayerTypes, bool, bool)

Defined in
CvMinorCivAI.cpp, line 8453
Class
CvMinorCivAI
Hook name
CivHooks::CvMinorCivAI_DoSpawnUnit

Hooks

static bool BeforeCvMinorCivAI_DoSpawnUnit(CvMinorCivAI* self, PlayerTypes eMajor, bool bLocal, bool bExplore, CvUnit*& result);static void AfterCvMinorCivAI_DoSpawnUnit(CvMinorCivAI* self, PlayerTypes eMajor, bool bLocal, bool bExplore, CvUnit*& result);static CvUnit* ReplaceCvMinorCivAI_DoSpawnUnit(CvUnit* (*original)(CvMinorCivAI*, PlayerTypes, bool, bool), CvMinorCivAI* self, PlayerTypes eMajor, bool bLocal, bool bExplore);CivRegisterBefore<CivHooks::CvMinorCivAI_DoSpawnUnit>(host, &BeforeCvMinorCivAI_DoSpawnUnit);CivRegisterAfter<CivHooks::CvMinorCivAI_DoSpawnUnit>(host, &AfterCvMinorCivAI_DoSpawnUnit);CivRegisterReplace<CivHooks::CvMinorCivAI_DoSpawnUnit>(host, &ReplaceCvMinorCivAI_DoSpawnUnit);

Code

8451/// Create a unit8452#if defined(MOD_GLOBAL_CS_GIFTS)8453CvUnit* CvMinorCivAI::DoSpawnUnit(PlayerTypes eMajor, bool bLocal, bool bExplore)8454#else8455void CvMinorCivAI::DoSpawnUnit(PlayerTypes eMajor)8456#endif8457{8458#if defined(MOD_GLOBAL_CS_GIFTS)8459	CvUnit* pSpawnUnit = NULL;8460#endif84618462	CvAssertMsg(eMajor >= 0, "eMajor is expected to be non-negative (invalid Index)");8463	CvAssertMsg(eMajor < MAX_MAJOR_CIVS, "eMajor is expected to be within maximum bounds (invalid Index)");8464#if defined(MOD_GLOBAL_CS_GIFTS)8465	if(eMajor < 0 || eMajor >= MAX_MAJOR_CIVS) return NULL;8466#else8467	if(eMajor < 0 || eMajor >= MAX_MAJOR_CIVS) return;8468#endif84698470	if(!IsUnitSpawningDisabled(eMajor))8471	{8472		// Minor must have Capital8473		CvCity* pMinorCapital = GetPlayer()->getCapitalCity();8474		if(pMinorCapital == NULL)8475		{8476			FAssertMsg(false, "MINOR CIV AI: Trying to spawn a Unit for a major civ but the minor has no capital. Please send Jon this with your last 5 autosaves and what changelist # you're playing.");8477#if defined(MOD_GLOBAL_CS_GIFTS)8478			return NULL;8479#else8480			return;8481#endif8482		}8483#if defined(MOD_GLOBAL_CS_GIFTS_LOCAL_XP)8484		CvCity* pSpawnCity = pMinorCapital;8485#endif8486		CvPlot* pMinorCapitalPlot = pMinorCapital->plot();8487		if(pMinorCapitalPlot == NULL)8488		{8489			CvAssertMsg(false, "MINOR CIV AI: Trying to spawn a Unit for a major civ but the minor's capital has no plot. Please send Anton your save file and version.");8490#if defined(MOD_GLOBAL_CS_GIFTS)8491			return NULL;8492#else8493			return;8494#endif8495		}84968497		CvCity* pMajorCity = GET_PLAYER(eMajor).GetClosestFriendlyCity(*pMinorCapitalPlot, MAX_INT);84988499		int iX = pMinorCapital->getX();8500		int iY = pMinorCapital->getY();8501#if defined(MOD_GLOBAL_CS_GIFTS)8502		if(!bLocal && pMajorCity != NULL)8503#else8504		if(pMajorCity != NULL)8505#endif8506		{8507			iX = pMajorCity->getX();8508			iY = pMajorCity->getY();8509			8510#if defined(MOD_GLOBAL_CS_GIFTS_LOCAL_XP)8511			pSpawnCity = pMajorCity;8512#endif8513		}85148515		// Pick Unit type8516		UnitTypes eUnit = NO_UNIT;8517		if (GetAlly() == eMajor)8518		{	8519			// Should we give our unique unit?8520			bool bUseUniqueUnit = false;8521			UnitTypes eUniqueUnit = GetUniqueUnit();8522			if (eUniqueUnit != NO_UNIT)8523			{8524				CvUnitEntry* pkUnitInfo = GC.getUnitInfo(eUniqueUnit);8525				if (pkUnitInfo)8526				{8527					// Ally must have unit's prereq tech8528					TechTypes ePrereqTech = (TechTypes) pkUnitInfo->GetPrereqAndTech();8529					if (ePrereqTech == NO_TECH || GET_TEAM(GET_PLAYER(eMajor).getTeam()).GetTeamTechs()->HasTech(ePrereqTech))8530					{8531						// Ally must NOT have unit's obsolete tech8532						TechTypes eObsoleteTech = (TechTypes) pkUnitInfo->GetObsoleteTech();8533						if (eObsoleteTech == NO_TECH || !GET_TEAM(GET_PLAYER(eMajor).getTeam()).GetTeamTechs()->HasTech(eObsoleteTech))8534						{8535							bUseUniqueUnit = true;8536						}8537					}8538				}8539			}8540			8541#if defined(MOD_GLOBAL_CS_GIFTS)8542			if (bExplore) {8543#if defined(MOD_GLOBAL_CS_GIFT_SHIPS)8544				eUnit = GC.getGame().GetCsGiftSpawnUnitType(eMajor, pMinorCapitalPlot->isCoastalLand() && MOD_GLOBAL_CS_GIFT_SHIPS);8545#else8546				eUnit = GC.getGame().GetCsGiftSpawnUnitType(eMajor);8547#endif8548			} else {8549#endif8550				if (bUseUniqueUnit)8551				{8552					eUnit = eUniqueUnit;8553				}8554				else8555				{8556#if defined(MOD_GLOBAL_CS_GIFT_SHIPS)8557					eUnit = GC.getGame().GetCompetitiveSpawnUnitType(eMajor, /*bIncludeUUs*/ false, /*bIncludeRanged*/true, MOD_GLOBAL_CS_GIFT_SHIPS);8558#else8559					eUnit = GC.getGame().GetCompetitiveSpawnUnitType(eMajor, /*bIncludeUUs*/ false, /*bIncludeRanged*/true);8560#endif8561				}8562#if defined(MOD_GLOBAL_CS_GIFTS)8563			}8564#endif8565		}8566		else8567		{8568#if defined(MOD_GLOBAL_CS_GIFTS)8569			if (bExplore) {8570#if defined(MOD_GLOBAL_CS_GIFT_SHIPS)8571				eUnit = GC.getGame().GetCsGiftSpawnUnitType(eMajor, pMinorCapitalPlot->isCoastalLand() && MOD_GLOBAL_CS_GIFT_SHIPS);8572#else8573				eUnit = GC.getGame().GetCsGiftSpawnUnitType(eMajor);8574#endif8575			} else {8576#endif8577#if defined(MOD_GLOBAL_CS_GIFT_SHIPS)8578				eUnit = GC.getGame().GetCompetitiveSpawnUnitType(eMajor, /*bIncludeUUs*/ false, /*bIncludeRanged*/true, MOD_GLOBAL_CS_GIFT_SHIPS);8579#else8580				eUnit = GC.getGame().GetCompetitiveSpawnUnitType(eMajor, /*bIncludeUUs*/ false, /*bIncludeRanged*/true);8581#endif8582#if defined(MOD_GLOBAL_CS_GIFTS)8583			}8584#endif8585		}85868587		// Spawn Unit8588		if(eUnit != NO_UNIT)8589		{8590			CvUnit* pNewUnit = GET_PLAYER(eMajor).initUnit(eUnit, iX, iY);85918592			// If player trait is to enhance minor bonuses, give this unit some free experience8593			if(GET_PLAYER(eMajor).GetPlayerTraits()->GetCityStateBonusModifier() > 0)8594			{8595#if defined(MOD_UNITS_XP_TIMES_100)8596				pNewUnit->changeExperienceTimes100(100 * GC.getMAX_EXPERIENCE_PER_COMBAT());8597#else8598				pNewUnit->changeExperience(GC.getMAX_EXPERIENCE_PER_COMBAT());8599#endif8600			}86018602			if (pNewUnit->jumpToNearestValidPlot())8603			{8604#if defined(MOD_BUGFIX_MINOR)8605				// We tested for "GetPlayer()->getCapitalCity() != NULL" way, way up there!!!8606#if defined(MOD_GLOBAL_CS_GIFTS_LOCAL_XP)8607				if (MOD_GLOBAL_CS_GIFTS_LOCAL_XP) 8608					pSpawnCity->addProductionExperience(pNewUnit);8609				else8610#endif8611					pMinorCapital->addProductionExperience(pNewUnit);8612#else8613				if(GetPlayer()->getCapitalCity())8614					GetPlayer()->getCapitalCity()->addProductionExperience(pNewUnit);8615#endif86168617				Localization::String strMessage = Localization::Lookup("TXT_KEY_NOTIFICATION_CITY_STATE_UNIT_SPAWN");8618				strMessage << GetPlayer()->getNameKey();8619				Localization::String strSummary = Localization::Lookup("TXT_KEY_NOTIFICATION_SUMMARY_CITY_STATE_UNIT_SPAWN");8620				strSummary << GetPlayer()->getNameKey();86218622				AddNotification(strMessage.toUTF8(), strSummary.toUTF8(), eMajor, pNewUnit->getX(), pNewUnit->getY());86238624#if defined(MOD_GLOBAL_CS_GIFTS)8625				pSpawnUnit = pNewUnit;8626#endif8627			}8628			else8629				pNewUnit->kill(false);	// Could not find a spot!8630		}8631	}86328633	// Reseed counter8634	DoSeedUnitSpawnCounter(eMajor);86358636#if defined(MOD_GLOBAL_CS_GIFTS)8637	return pSpawnUnit;8638#endif8639}

Calls

Called by