Long time since my last question, but I'm back :) - Building Loop

Duke176

Warlord
Joined
Oct 19, 2006
Messages
241
Location
Turin - Italy
Hi all, long itme since last question, you thought you freed of me? :cool:

Here is my new noob question:

Target - make a loop throw the city buildings and make the city menu return the ammount of oil consumed for normal usages. Building have some set XML ordinary Consumption that should be added or decreased from normal city usage.

For ex. City - pop=5 - normal usage = 1 each turn
with Factory = normal usage + 5... so on.

All right so the game should, when you check a city, loop throw the buildings and get values of iOilConsumption from XML and add them all togheter for total.
(I know the part of getting infos from XML to SDK and the function I wan't to show you it's not complite)


Code:
int CvCity::getBonusHealth(BonusTypes eBonus) const
{
	int iHealth;
	int iI;

	iHealth = GC.getBonusInfo(eBonus).getHealth();

	for (iI = 0; iI < GC.getNumBuildingInfos(); iI++)
	{
		if (hasActiveBuilding((BuildingTypes)iI))
		{
			iHealth += GC.getBuildingInfo((BuildingTypes) iI).getBonusHealthChanges(eBonus);
		}
	}

	return iHealth;
}


//-------------- MAKE FUNCTIONS FOR OIL CONSUMPTION ----------------------
//get buildings oil consumption per turn
int CvCity::getOilConsumptionPerBuildings(BuildingTypes eBuilding) const
{

	int iOilCons;
	int iI;

	iOilCons = 0;

//	iOilCons = GC.getBuildingInfo(eBuilding).getOilConsumption();

	for (iI = 0; iI < GC.getNumBuildingInfos(); iI++)
	{
		if (hasActiveBuilding((BuildingTypes)iI))
		{
			iOilCons += GC.getBuildingInfo((BuildingTypes) iI).getOilConsumption();
		}
	}

	return iOilCons;

}

//--------------------- END ---------------------------------------

As you see I used the 1st function to create the new one.
Is this correct, is not, why? (i've always hated these kind of functions :D )

Thx. for attention and for help!
 
Your confusing me.

You want to make a function called getOilConsumption and add it to the building class? You can call functions from within another function but not make one. Is the getOilConsumptionPerBuildings() referenced in the header file? Also if you working on adjusting the city screen interface you in the wrong file.

But all in all I'm failing to understand what your question is.
 
That's not what you're after. When you call the function, you're passing it the BuildingTypes eBuilding, so you want something like this:

Code:
//-------------- MAKE FUNCTIONS FOR OIL CONSUMPTION ----------------------
//get buildings oil consumption per turn
int CvCity::getOilConsumptionPerBuildings(BuildingTypes eBuilding) const
{

	int iOilCons = 0;

	if (hasActiveBuilding(eBuilding)
	{
		iOilCons = GC.getBuildingInfo(eBuilding).getOilConsumption();
	}

	return iOilCons;
}

//--------------------- END
 
thx for fast answering. :bowdown: :bowdown: :bowdown: :bowdown:

I'll re-explain my target (sorry but I wrote the 1st post yesterday night at 3.00 so i hadn't time to read is again :)).

I would like to create a function that - when you open city window - calculate how much oil, the city, consume each turn. Consumes are made of a static value, in DefineINT, and other different values. these different values are taken from XML and building specifications (used stream...read...write...XMLChild...).

So i thought (that's why I choose that function): I make a loop throw the buildings of the city, without specifying the building to look at, just saying take those that have "iOilConsumption" value and add them toghter.
The output will be another prb and I'll mod interface in python.

Why the one I choosed is not good?
and which one could I take as model to modify the one I wrote?

Hope I've been clearer.
 
here is how i did it:

Code:
//-------------- MAKE FUNCTIONS FOR OIL CONSUMPTION ----------------------
//get buildings oil consumption per turn
int CvCity::getOilConsumptionPerBuildings() const
{

	int iAirportCons = 0;
	int iDryDockCons = 0;
	int iHospitalCons = 0;
	int iHydroplantCons = 0;
	int iNuclearPlantCons = 0;
	int iRecyclingCenterCons = 0;

	int iTotalConsumption = 0;
// basic city consumptio BCC from Define INT
	int iBCC = 0;
//	int iMedCitiesConsumption = 0;
//	int iBigCitiesConsumption = 0;
//	int iEnormousCitiesConsumption = 0;


	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_AIRPORT")))
	{
		iAirportCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_AIRPORT"))).getOilConsumption();
	}
	return iAirportCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_DRYDOCK")))
	{
		iDryDockCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_DRYDOCK"))).getOilConsumption();
	}
	return iDryDockCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_HOSPITAL")))
	{
		iHospitalCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_HOSPITAL"))).getOilConsumption();
	}
	return iHospitalCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_HYDROPLANT")))
	{
		iHydroplantCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_HYDROPLANT"))).getOilConsumption();
	}
	return iHydroplantCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_NUCLEAR_PLANT")))
	{
		iNuclearPlantCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_NUCLEAR_PLANT"))).getOilConsumption();
	}
	return iNuclearPlantCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_RECYCLING_CENTER")))
	{
		iRecyclingCenterCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_RECYCLING_CENTER"))).getOilConsumption();
	}
	return iRecyclingCenterCons;
// let's go for population
	if ((getPopulation() >= 1) && (getPopulation() <= 5))
	{
		iBCC = (GC.getDefineINT("LITTLE_CITIES_CONSUMPTION"));
	}
	else if ((getPopulation() >= 6) && (getPopulation() <= 10))
	{
		iBCC = (GC.getDefineINT("MED_CITIES_CONSUMPTION"));
	}
	else if ((getPopulation() >= 11) && (getPopulation() <= 15))
	{
		iBCC = (GC.getDefineINT("BIG_CITIES_CONSUMPTION"));
	}
	else if (getPopulation() >= 16)
	{
		iBCC = (GC.getDefineINT("ENORMOUS_CITIES_CONSUMPTION"));
	}
	return iBCC;



	iTotalConsumption = ((iBCC * getPopulation()) + ((iBCC * iAirportCons) / 100) + ((iBCC * iDryDockCons) / 100) + ((iBCC * iHospitalCons) / 100) + ((iBCC * iHydroplantCons) / 100) + ((iBCC * iNuclearPlantCons) / 100) + ((iBCC * iRecyclingCenterCons) / 100));

	return iTotalConsumption;
}

//--------------------- END ---------------------------------------

I just wanted to avoid if possible all those if...else if... if if if... from the 1st part where I get XML infos; maybe with a loop throw the buildings of city.
 
here is how i did it:

Code:
//-------------- MAKE FUNCTIONS FOR OIL CONSUMPTION ----------------------
//get buildings oil consumption per turn
int CvCity::getOilConsumptionPerBuildings() const
{

	int iAirportCons = 0;
	int iDryDockCons = 0;
	int iHospitalCons = 0;
	int iHydroplantCons = 0;
	int iNuclearPlantCons = 0;
	int iRecyclingCenterCons = 0;

	int iTotalConsumption = 0;
// basic city consumptio BCC from Define INT
	int iBCC = 0;
//	int iMedCitiesConsumption = 0;
//	int iBigCitiesConsumption = 0;
//	int iEnormousCitiesConsumption = 0;


	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_AIRPORT")))
	{
		iAirportCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_AIRPORT"))).getOilConsumption();
	}
	return iAirportCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_DRYDOCK")))
	{
		iDryDockCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_DRYDOCK"))).getOilConsumption();
	}
	return iDryDockCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_HOSPITAL")))
	{
		iHospitalCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_HOSPITAL"))).getOilConsumption();
	}
	return iHospitalCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_HYDROPLANT")))
	{
		iHydroplantCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_HYDROPLANT"))).getOilConsumption();
	}
	return iHydroplantCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_NUCLEAR_PLANT")))
	{
		iNuclearPlantCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_NUCLEAR_PLANT"))).getOilConsumption();
	}
	return iNuclearPlantCons;

	if (hasActiveBuilding((BuildingTypes)GC.getInfoTypeForString("BUILDING_RECYCLING_CENTER")))
	{
		iRecyclingCenterCons = GC.getBuildingInfo(BuildingTypes (GC.getInfoTypeForString("BUILDING_RECYCLING_CENTER"))).getOilConsumption();
	}
	return iRecyclingCenterCons;
// let's go for population
	if ((getPopulation() >= 1) && (getPopulation() <= 5))
	{
		iBCC = (GC.getDefineINT("LITTLE_CITIES_CONSUMPTION"));
	}
	else if ((getPopulation() >= 6) && (getPopulation() <= 10))
	{
		iBCC = (GC.getDefineINT("MED_CITIES_CONSUMPTION"));
	}
	else if ((getPopulation() >= 11) && (getPopulation() <= 15))
	{
		iBCC = (GC.getDefineINT("BIG_CITIES_CONSUMPTION"));
	}
	else if (getPopulation() >= 16)
	{
		iBCC = (GC.getDefineINT("ENORMOUS_CITIES_CONSUMPTION"));
	}
	return iBCC;



	iTotalConsumption = ((iBCC * getPopulation()) + ((iBCC * iAirportCons) / 100) + ((iBCC * iDryDockCons) / 100) + ((iBCC * iHospitalCons) / 100) + ((iBCC * iHydroplantCons) / 100) + ((iBCC * iNuclearPlantCons) / 100) + ((iBCC * iRecyclingCenterCons) / 100));

	return iTotalConsumption;
}

//--------------------- END ---------------------------------------

I just wanted to avoid if possible all those if...else if... if if if... from the 1st part where I get XML infos; maybe with a loop throw the buildings of city.

What you want to do is follow this tutorial:

http://forums.civfanatics.com/showthread.php?t=166935&highlight=xml+tags

Doing that, make a new XML tag for buildings that is an integer (so basically, you can use any of the integer tags that Kael creates in the tutorial as a base, just make sure you do it for building's instead of units).

Then, you would want to have a loop that goes through all the buildings. If I'm understanding you right, you want to determine the sum of all the building's oil needs. So, rather than define an integer for each one, just create one integer, and keep adding the the total to it.

For example:

Code:
int iSum = 0;


// Loop through all the buildings. If the player has that building in their city,
// then add the oil consumption to the accumulating "iSum" variable.
// getOilConsumption is the new function you created after following Kael's guide.
for (iI = 0; iI < GC.getNumBuildingInfos(); iI++)
{
	if (hasActiveBuilding((BuildingTypes)iJ))
	{
		iSum += GC.getBuildingInfo((BuildingTypes) iI).getOilConsumption();
	}
}

It looks like your math is a bit different, but you can do what you like for each building. Since if you follow the tutorial, you'll end up with building's having a default oil consumption of 0, having buildings with no oil consumption won't throw off the result, it will just add 0.

Also, because you would have exposed the GC.getBuildingInfo((BuildingTypes)iI).getOilConsumption() function to python (where you would call it by calling "gc.getBuildingInfo(iI).getOilConsumption()" ) you would be able to implement these into your python interface code.

Hope that helps.
 
thx thx thx very very very much!
Really.

In reality I made something else in my final math:
in spite of having a value fixed each building, each building determine a % in addiction or subtraction.

so you have

oilconsumption = (totalCityConsumption * oilforAirport(es.) / 100) and so on

Anyway thx.
I'm enountering some prb with XML tag addiction, a prb with XML loading phase in game loading, I've already found it, but I solved it without knowing how.
 
Back
Top Bottom