Bazaar developer reference

CvCity::setPopulation(int, bool)

Defined in
CvCity.cpp, line 7839
Class
CvCity
Hook name
CivHooks::CvCity_setPopulation
Lua events
GameEvents.SetPopulation

Hooks

static bool BeforeCvCity_setPopulation(CvCity* self, int iNewValue, bool bReassignPop);static void AfterCvCity_setPopulation(CvCity* self, int iNewValue, bool bReassignPop);static void ReplaceCvCity_setPopulation(void (*original)(CvCity*, int, bool), CvCity* self, int iNewValue, bool bReassignPop);CivRegisterBefore<CivHooks::CvCity_setPopulation>(host, &BeforeCvCity_setPopulation);CivRegisterAfter<CivHooks::CvCity_setPopulation>(host, &AfterCvCity_setPopulation);CivRegisterReplace<CivHooks::CvCity_setPopulation>(host, &ReplaceCvCity_setPopulation);

Code

7836//	Be very careful with setting bReassignPop to false.  This assumes that the caller7837//  is manually adjusting the worker assignments *and* handling the setting of7838//  the CityCitizens unassigned worker value.7839void CvCity::setPopulation(int iNewValue, bool bReassignPop /* = true */)7840{7841	VALIDATE_OBJECT7842	int iOldPopulation;7843	7844#if defined(MOD_BUGFIX_CITY_CENTRE_WORKING)7845	// To fix the "not working the centre tile" bug always call GetCityCitizens()->SetWorkingPlot(plot(), true, false); here7846	GetCityCitizens()->SetWorkingPlot(plot(), true, false);7847#endif78487849	iOldPopulation = getPopulation();7850	int iPopChange = iNewValue - iOldPopulation;78517852	if(iOldPopulation != iNewValue)7853	{7854		// If we are reducing population, remove the workers first7855		if(bReassignPop)7856		{7857			if(iPopChange < 0)7858			{7859				// Need to Remove Citizens7860				for(int iNewPopLoop = -iPopChange; iNewPopLoop--;)7861				{7862#if defined(MOD_GLOBAL_CITY_AUTOMATON_WORKERS)7863					GetCityCitizens()->DoRemoveWorstCitizen(false, NO_SPECIALIST, iNewValue + getAutomatons());7864#else7865					GetCityCitizens()->DoRemoveWorstCitizen(false, NO_SPECIALIST, iNewValue);7866#endif7867				}78687869				// Fixup the unassigned workers7870				int iUnassignedWorkers = GetCityCitizens()->GetNumUnassignedCitizens();7871				CvAssert(iUnassignedWorkers >= -iPopChange);7872				GetCityCitizens()->ChangeNumUnassignedCitizens(std::max(iPopChange, -iUnassignedWorkers));7873			}7874		}78757876		m_iPopulation = iNewValue;78777878		CvAssert(getPopulation() >= 0);78797880		GET_PLAYER(getOwner()).invalidatePopulationRankCache();78817882		if(getPopulation() > getHighestPopulation())7883		{7884			setHighestPopulation(getPopulation());7885		}78867887		area()->changePopulationPerPlayer(getOwner(), (getPopulation() - iOldPopulation));7888		GET_PLAYER(getOwner()).changeTotalPopulation(getPopulation() - iOldPopulation);7889		GET_TEAM(getTeam()).changeTotalPopulation(getPopulation() - iOldPopulation);7890		GC.getGame().changeTotalPopulation(getPopulation() - iOldPopulation);78917892		plot()->updateYield();78937894		UpdateReligion(GetCityReligions()->GetReligiousMajority());78957896		ChangeBaseYieldRateFromMisc(YIELD_SCIENCE, (iNewValue - iOldPopulation) * GC.getSCIENCE_PER_POPULATION());78977898		if(iPopChange > 0)7899		{7900			// Give new Population something to do in the City7901			if(bReassignPop)7902			{7903				GetCityCitizens()->ChangeNumUnassignedCitizens(iPopChange);79047905				// Need to Add Citizens7906				for(int iNewPopLoop = 0; iNewPopLoop < iPopChange; iNewPopLoop++)7907				{7908					GetCityCitizens()->DoAddBestCitizenFromUnassigned();7909				}7910			}7911		}79127913		setLayoutDirty(true);7914		{7915			auto_ptr<ICvCity1> pkDllCity(new CvDllCity(this));7916			gDLL->GameplayCityPopulationChanged(pkDllCity.get(), iNewValue);7917		}79187919		plot()->plotAction(PUF_makeInfoBarDirty);79207921		if((getOwner() == GC.getGame().getActivePlayer()) && isCitySelected())7922		{7923			DLLUI->setDirty(SelectionButtons_DIRTY_BIT, true);7924			DLLUI->setDirty(CityScreen_DIRTY_BIT, true);7925		}79267927		// Update Unit Maintenance for the player7928		GET_PLAYER(getOwner()).UpdateUnitProductionMaintenanceMod();79297930		GET_PLAYER(getOwner()).DoUpdateHappiness();79317932		//updateGenericBuildings();7933		updateStrengthValue();79347935		DLLUI->setDirty(CityInfo_DIRTY_BIT, true);7936	}79377938	ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();7939	if(pkScriptSystem)7940	{7941		CvLuaArgsHandle args;7942		args->Push(getX());7943		args->Push(getY());7944		args->Push(iOldPopulation);7945		args->Push(iNewValue);79467947		bool bResult;7948		LuaSupport::CallHook(pkScriptSystem, "SetPopulation", args.get(), bResult);7949	}7950}

Calls

Called by