SDK - CvPlayer.cpp

Duke176

Warlord
Joined
Oct 19, 2006
Messages
241
Location
Turin - Italy
hi all,
another question from a nooooooob modder :).

can anyone explain me this function from cvplayer.cpp?

int CvPlayer::getGold()
{
return m_iGold;
}


void CvPlayer::setGold(int iNewValue)
{
if (getGold() != iNewValue)
{
m_iGold = iNewValue;

if (getID() == GC.getGameINLINE().getActivePlayer())
{
gDLL->getInterfaceIFace()->setDirty(MiscButtons_DIRTY_BIT, true);
gDLL->getInterfaceIFace()->setDirty(SelectionButtons_DIRTY_BIT, true);
gDLL->getInterfaceIFace()->setDirty(GameData_DIRTY_BIT, true);
}
}
}


void CvPlayer::changeGold(int iChange)
{
setGold(getGold() + iChange);
}


I can really understand where iChange come and if iNewValue is == with (getGold() + iChange)? Shouldn't come for other functions to calculate goldperturn for ex.?

I can really understand it, it's almost a week I'm studying it but I cannot get it clear... :confused: :confused: :confused:


thx
 
Well, judging by its proximity to GoldperTurn, I would suggest that it is updating the total gold in your treasury. With my limited understanding, I am guessing that the first step is grabbing that particular element (gold), then the second step voids the existing value of that element, and resets it, then the third step establishes the new, changed value of the element. I could be wrong, but I think that does some it up pretty well.

Aussie_Lurker.
 
changeGold is called to update the player's gold amount.

getGold is called to see what the player's gold amount is.

setGold is called to (in simple terms) to check the value on screen with the value in the database. Since the DB is always the correct value, it sets the on screen value to the DB value and sets the relevant interface flags to dirty so they "redraw" to the new correct amount.

Dale
 
well, for what I understood it works like this:

int CvPlayer::getGold()
{
return m_iGold;
}

Here it take the absolute value of integer iGold that you decleare at the begin of file that is updated turn by turn.

void CvPlayer::setGold(int iNewValue)
{
if (getGold() != iNewValue)
{
m_iGold = iNewValue;

if (getID() == GC.getGameINLINE().getActivePlayer())
{
gDLL->getInterfaceIFace()->setDirty(MiscButtons_DIRTY_BIT, true);
gDLL->getInterfaceIFace()->setDirty(SelectionButtons_DIRTY_BIT, true);
gDLL->getInterfaceIFace()->setDirty(GameData_DIRTY_BIT, true);
}
}
}

Here you have void (= modify), and iNewValue integer variable declaration, than if you have iGold different from iNewValue, it makes iGold the same as iNewValue and than changes the graphic for the player.
But the 1st question is è where does iNewValue come from?
The answer is in the next function.

void CvPlayer::changeGold(int iChange)
{
setGold(getGold() + iChange);
}

Here we have again void (= modify) and a new integer variable decleared iChange. So the function recalls the previous fucntion "SetGold" and assign a real value to the "iNewValue" previusly decleared.
So here you have (getGold() + iChange) = iNewValue.

Until this point i think it's right, correct me if I'm wrong.

Here it comes the real prb: i understood that the integer variable iNewValue is explained in the last function that recalls the second (setGold) but this last function "changeGold" is not called elsewhere here so "iChange" seems not be associated to another function that returns it's value...
where does iChange come from?
Why? this is my real misunderstanding.

I hope I've been clear.
 
hey Lord,
thx for answer.

Yeah as title says I had this revelation - I'm, I was complitly new to SDK, c++ and XML 3 weeks ago, so I'm a complitely noob, I'm learning by doing, copying and asking... - pls correct me if I'm wrong.

The answer to my question was so easy I missed it.

when you call changeGold(....) you will write something like this for ex.:

Code:
int xxxxx()
{
     return changeGold(goldPerTurn());
}

than on calling it the function goldPerTurn() will fill iChange, write?

p.s.: I'm a lowyer that's why I can seem so idiot in programming, sorry for stupid questions :p
 
You would just call changeGold(-4); to remove 4 gold from that player instance. Or lets say you were in the CvUnit code and wanted to call it you would use something similiar to the following:

Code:
			iPillageGold = GC.getGameINLINE().getSorenRandNum(GC.getImprovementInfo(pPlot->getImprovementType()).getPillageGold(), "Pillage Gold");
			GET_PLAYER(getOwnerINLINE()).changeGold(iPillageGold);

The first line generates a random number based on the improvements pillage amount, the second line gives the player that much gold. Notice that when we aren't in the CvPlayer.cpp file we have to specifiy what player we want to call the function for.

You may want to check out this thread for samples of SDK changes: http://forums.civfanatics.com/showthread.php?t=166934
 
thx very much for help.
so if I understood:
one last thing - to work this code should be like this

Code:
[COLOR="Red"]//calculate Oil Prod per Turn
int CvPlayer::calculateOilProdPerTurn()
{
	int iChange = 0;

	iChange = (getNumAvailableWells() + getNumAvailablePlatforms()) * (getOilExtrectionPerTurn());
}[/COLOR]

//Modify Oil Prod Per Turn
//get
int CvPlayer::getOilProdPerTurn()
{
	return m_iOilProdPerTurn;
}

//set
void CvPlayer::setOilProdPerTurn(int iNewValue)
{
	if (getOilProdPerTurn() != iNewValue)
	{
		m_iOilProdPerTurn = iNewValue;
	}
}

void CvPlayer::changeOilProdPerTurn(int iChange)
{
	setOilProdPerTurn(getOilProdPerTurn() + iChange);
}

or like this?
Code:
[COLOR="Red"]//calculate Oil Prod per Turn
int CvPlayer::calculateOilProdPerTurn()
{
	changeOilProdPerTurn(getNumAvailableWells() + getNumAvailablePlatforms()) * (getOilExtrectionPerTurn());
}[/COLOR]

//Modify Oil Prod Per Turn
//get
int CvPlayer::getOilProdPerTurn()
{
	return m_iOilProdPerTurn;
}

//set
void CvPlayer::setOilProdPerTurn(int iNewValue)
{
	if (getOilProdPerTurn() != iNewValue)
	{
		m_iOilProdPerTurn = iNewValue;
	}
}

void CvPlayer::changeOilProdPerTurn(int iChange)
{
	setOilProdPerTurn(getOilProdPerTurn() + iChange);
}
 
Im not sure what you want your calculateOilProdPerTurn function to do for you. Typically it would be a function that you call externally to have it return the typical amount of Oil you produce per turn. If so it needs to have a return in it (right now you have it set as an Int function, meaning it is intended to return an int but you dont have it returning anything).

Something like the following would probably be better:

Code:
int CvPlayer::calculateOilProdPerTurn()
{
	iChange = (getNumAvailableWells() + getNumAvailablePlatforms()) * (getOilExtrectionPerTurn());
	return iChange;
}

Thats assuming you have player functions for getNumAvailableWells(), getNumAvailablePlatforms() and getOilExtrectionPerTurn().

Keep in mind that the calculateOilProdPerTurn as Ive written it doesn't do anything escept generate a number and return it to the requester. To actually use it you will probably want the call to look something like:

Code:
	setOilProdPerTurn(calculateOilProdPerTurn());

Starting out to modify C++ by adding new functions is a huge task. There are a lot of things to deal with when adding a new function, and I would recommend you get some time in modifying existing functions before trying something like this.
 
any idea about what's wrong with this?

Code:
//------------------------------------- OIL PROD PER TURN ----------------------------------
[B]//returns number = ammount of extraction per turn (from terraininfo.xml)

int CvPlayer::oilExtractionPerTurn() const
{
	return GC.getImprovementInfo(getImprovementType()).getOilExtractionPerTurn();
}[/B]

//returns number of wells
int CvPlayer::getNumAvailableWells()
{
	return (getNumAvailableBonuses((BonusTypes)GC.getInfoTypeForString("IMPROVEMENT_WELL")));
}

the compiler tells me this:

look at the shoot.


Even if it's all decleared and I already done something similar for UnitMovementCost.
OilExtractionPerTurn is a new value I give to Wells into Civ4improvementInfos.xml...

I've tryed to recheck all the code and to look all the codes I have that recalls other objcts from other files...
 

Attachments

  • bug compiling oil3.jpg
    bug compiling oil3.jpg
    195.7 KB · Views: 82
any idea about what's wrong with this?

Code:
//------------------------------------- OIL PROD PER TURN ----------------------------------
[B]//returns number = ammount of extraction per turn (from terraininfo.xml)

int CvPlayer::oilExtractionPerTurn() const
{
	return GC.getImprovementInfo(getImprovementType()).getOilExtractionPerTurn();
}[/B]

//returns number of wells
int CvPlayer::getNumAvailableWells()
{
	return (getNumAvailableBonuses((BonusTypes)GC.getInfoTypeForString("IMPROVEMENT_WELL")));
}

the compiler tells me this:

look at the shoot.


Even if it's all decleared and I already done something similar for UnitMovementCost.
OilExtractionPerTurn is a new value I give to Wells into Civ4improvementInfos.xml...

I've tryed to recheck all the code and to look all the codes I have that recalls other objcts from other files...

You are trying to call getImprovementType() against the current CvPlayer instance. CvPlayer doesn't have a getImprovementType() function.

If you don't define an object to make the call against it assumes the instance that is making the call is the object.
 
it happened something strange:
the compiler made it with this codification:

Code:
//returns number = ammount of extraction per turn (from terraininfo.xml)

int CvPlayer::oilExtractionPerTurn() const
{
	return ((GC.getImprovementInfo(ImprovementTypes())).getOilExtractionPerTurn());
}

can't get this point:
ImprovementTypes()

But improvementtypes() it's not a function it should be an argument of getImprovementType(...)...

I don't understand it...
 
update:

the compiler compiled everything with no prb but now when the game loads /Terrain/Civ4TerrainInfos.xml - that i haven't modified - it returns me an error....

In TerrainInfos.xml that I haven't touched...

it also says: "LoadXML call failed for GameInfo/Civ4TerrainInfos.xml...... and "allocating zero or less memory in CvXMLLoadUtility::SetVariableListTagPair...."


Couldn't be for ex. the position where I inserted the new "int value" inside ImprovementInfos.xml and TerrainSchema.xml that could create crush?
 
Back
Top Bottom