Bazaar developer reference

CvPlayer::findNewCapital()

Defined in
CvPlayer.cpp, line 5893
Class
CvPlayer
Hook name
CivHooks::CvPlayer_findNewCapital
Lua events
GameEvents.CapitalChanged

Hooks

static bool BeforeCvPlayer_findNewCapital(CvPlayer* self);static void AfterCvPlayer_findNewCapital(CvPlayer* self);static void ReplaceCvPlayer_findNewCapital(void (*original)(CvPlayer*), CvPlayer* self);CivRegisterBefore<CivHooks::CvPlayer_findNewCapital>(host, &BeforeCvPlayer_findNewCapital);CivRegisterAfter<CivHooks::CvPlayer_findNewCapital>(host, &AfterCvPlayer_findNewCapital);CivRegisterReplace<CivHooks::CvPlayer_findNewCapital>(host, &ReplaceCvPlayer_findNewCapital);

Code

5893void CvPlayer::findNewCapital()5894{5895	CvCity* pOldCapital;5896	CvCity* pLoopCity;5897	CvCity* pBestCity;5898	BuildingTypes eCapitalBuilding;5899	int iValue;5900	int iBestValue;5901#if defined(MOD_BUGFIX_NO_PUPPET_CAPITALS)5902	int iLoop = 0;5903#else5904	int iLoop;5905#endif59065907	eCapitalBuilding = ((BuildingTypes)(getCivilizationInfo().getCivilizationBuildings(GC.getCAPITAL_BUILDINGCLASS())));59085909	if(eCapitalBuilding == NO_BUILDING)5910	{5911		return;5912	}59135914	pOldCapital = getCapitalCity();59155916	iBestValue = 0;5917	pBestCity = NULL;59185919	CUSTOMLOG("Hunting for a new capital city ...");5920	CUSTOMLOG("  ... 1st pass");59215922	for(pLoopCity = firstCity(&iLoop); pLoopCity != NULL; pLoopCity = nextCity(&iLoop))5923	{5924		if(pLoopCity != pOldCapital)5925		{5926			if(0 == pLoopCity->GetCityBuildings()->GetNumRealBuilding(eCapitalBuilding))5927			{5928#if defined(MOD_BUGFIX_NO_PUPPET_CAPITALS)5929				// First pass, exclude cities in resistance, puppets, and those burning to the ground5930				if (!(pLoopCity->IsResistance() || pLoopCity->IsPuppet() || pLoopCity->IsRazing()))5931				{5932#endif5933					iValue = (pLoopCity->getPopulation() * 4);59345935					int iYieldValueTimes100 = pLoopCity->getYieldRateTimes100(YIELD_FOOD, false);5936					iYieldValueTimes100 += (pLoopCity->getYieldRateTimes100(YIELD_PRODUCTION, false) * 3);5937					iYieldValueTimes100 += (pLoopCity->getYieldRateTimes100(YIELD_GOLD, false) * 2);5938					iValue += (iYieldValueTimes100 / 100);59395940					iValue += (pLoopCity->getNumGreatPeople() * 2);59415942					if (iValue > iBestValue)5943					{5944						iBestValue = iValue;5945						pBestCity = pLoopCity;5946						CUSTOMLOG("  ... considering %s", pBestCity->getName().c_str());5947					}5948#if defined(MOD_BUGFIX_NO_PUPPET_CAPITALS)5949				}5950				else5951				{5952					CUSTOMLOG("  ... ignoring %s", pLoopCity->getName().c_str());5953				}5954#endif5955			}5956		}5957	}59585959#if defined(MOD_BUGFIX_NO_PUPPET_CAPITALS)5960	if (pBestCity == NULL)5961	{5962		CUSTOMLOG("  ... 2nd pass");5963		iLoop = 0;59645965		for (pLoopCity = firstCity(&iLoop); pLoopCity != NULL; pLoopCity = nextCity(&iLoop))5966		{5967			if (pLoopCity != pOldCapital)5968			{5969				if (0 == pLoopCity->GetCityBuildings()->GetNumRealBuilding(eCapitalBuilding))5970				{5971					// Second pass, consider only those we ignored first time5972					if (pLoopCity->IsResistance() && !pLoopCity->IsPuppet())5973					{5974						CUSTOMLOG("  ... resisting %s", pLoopCity->getName().c_str());5975						// We'll take a city in resistance (ie one we have decided to assimilate) over all others5976						iValue = pLoopCity->getPopulation() + 500;5977					}5978					else if (pLoopCity->IsPuppet())5979					{5980						CUSTOMLOG("  ... puppet %s", pLoopCity->getName().c_str());5981						// We'll take a puppet city next, at least we're not burning it to the ground!5982						iValue = pLoopCity->getPopulation() + 250;5983					}5984					else if (pLoopCity->IsRazing())5985					{5986						CUSTOMLOG("  ... burning %s", pLoopCity->getName().c_str());5987						// Might be an idea to stop the burning!5988						iValue = pLoopCity->getPopulation();5989					}5990					else5991					{5992						iValue = iBestValue;5993					}59945995					if (iValue > iBestValue)5996					{5997						iBestValue = iValue;5998						pBestCity = pLoopCity;5999						CUSTOMLOG("  ... considering %s", pBestCity->getName().c_str());6000					}6001				}6002			}6003		}6004	}6005#endif60066007	if (pBestCity != NULL)6008	{6009		CUSTOMLOG("  ... new capital will be %s", pBestCity->getName().c_str());60106011		if (pOldCapital != NULL)6012		{6013			pOldCapital->GetCityBuildings()->SetNumRealBuilding(eCapitalBuilding, 0);6014		}6015		CvAssertMsg(!(pBestCity->GetCityBuildings()->GetNumRealBuilding(eCapitalBuilding)), "(pBestCity->getNumRealBuilding(eCapitalBuilding)) did not return false as expected");6016		pBestCity->GetCityBuildings()->SetNumRealBuilding(eCapitalBuilding, 1);60176018#if defined(MOD_EVENTS_CITY_CAPITAL)6019		if (MOD_EVENTS_CITY_CAPITAL) {6020			GAMEEVENTINVOKE_HOOK(GAMEEVENT_CapitalChanged, GetID(), pBestCity->GetID(), (pOldCapital ? pOldCapital->GetID() : -1));6021		}6022#endif60236024#if defined(MOD_BUGFIX_NO_PUPPET_CAPITALS)6025		if (pBestCity->IsPuppet())6026		{6027			gDLL->sendDoTask(pBestCity->GetID(), TASK_ANNEX_PUPPET, -1, -1, false, false, false, false);6028		}6029		else if (pBestCity->IsRazing() && !isHuman())6030		{6031			// For the AI, we'll stop burining our new capital!6032			gDLL->sendDoTask(pBestCity->GetID(), TASK_UNRAZE, -1, -1, false, false, false, false);6033		}6034#endif60356036#if defined(MOD_GLOBAL_NO_CONQUERED_SPACESHIPS)6037		if (MOD_GLOBAL_NO_CONQUERED_SPACESHIPS && !isMinorCiv() && !isBarbarian()) {6038			// Rebuild the spaceship launch pad6039			CvTeam& thisTeam = GET_TEAM(getTeam());60406041			if (thisTeam.getProjectCount((ProjectTypes)GC.getSPACE_RACE_TRIGGER_PROJECT()) == 1) {6042				if (isAlive()) {6043					CUSTOMLOG("Rebuilding launch pad at (%i, %i)", pBestCity->getX(), pBestCity->getY());6044					auto_ptr<ICvPlot1> pDllPlot(new CvDllPlot(pBestCity->plot()));6045					gDLL->GameplaySpaceshipEdited(pDllPlot.get(), 0x0001); // Display just the launch pad6046				}6047			}6048		}6049#endif6050	}6051#if defined(MOD_BUGFIX_NO_PUPPET_CAPITALS)6052	else6053	{6054		CUSTOMLOG("  ... new capital NOT found");6055	}6056#endif6057}

Calls

Called by