Self Referencing CvUnitInfo function?

phungus420

Deity
Joined
Mar 1, 2003
Messages
6,296
I'm trying to get BUG's new experience code to display proper information for mods. I've actually accomplished this, but I'm left with a function in CvGameCoreUtils.cpp; while this works I think there has to be a better way.

To explain further; in the BtS gamerules CvUnit checks a unit to see if it is eligible for experience. This function looks over a unit, a specific unit (this is important), and tests it to make sure it has promotions available; if it does the function that does this test returns true, and BtS gives the unit experience; if not, the function returns false, and the unit does not receive experience. Now BUG adds a new functionality where a UnitInfo is checked for city display purposes to show how much experience a unit gets when built in a city. BUG's check simply checks for the UNITCOMBAT_TYPE of a unit, and then displays full experience for any unit that has any UnitCombatType, and skips the function if there is none. The problem with this is that for some mods this test does not work, as otherwise unpromotable, and thus non eligible for experience units show that they are built with experience.

I've functionally fixed this, by creating a new function: canAquireExperience, and put it in CvGameCoreUtils.cpp:
Spoiler :
Code:
bool canAcquireExperience(UnitTypes eUnit)
{
	FAssert(NO_UNIT != eUnit)
	if(eUnit == NO_UNIT)
	{
		return false;
	}
	
	PromotionTypes ePromotion;
	CvUnitInfo& kUnit = GC.getUnitInfo(eUnit);
	UnitCombatTypes eCombatType = (UnitCombatTypes)kUnit.getUnitCombatType();

	if(eCombatType == NO_UNITCOMBAT)
	{
		return false;
	}
	
	int iI;
	int iJ;

	for (iI = 0; iI < GC.getNumPromotionInfos(); iI++)
	{
		ePromotion = (PromotionTypes)iI;
		CvPromotionInfo& kPromotion = GC.getPromotionInfo(ePromotion);
		
		for (iJ = 0; iJ < GC.getNumUnitCombatInfos(); iJ++)
		{
			if (eCombatType == (UnitCombatTypes)iJ && kPromotion.getUnitCombat(iJ))
			{
				return true;
			}
		}
	}

	return false;
}

This works, however there has to be a better way. Instead of passing eUnit into the function, does anyone know how I could write this function in CvUnitInfo:: and then call it directly from a UnitInfos reference. Ie, instead of doing this:

canAquireExperience(eUnit)

Is there a way I could use:
kUnit.canAquireExperience()

Try as I might I can't wrap my brain around doing this. My main problem is, if I create this function in CvUnitInfo, How do I know it is referencing the correct unit type; in other words how do a I get an object inside
bool CvUnitInfo::canAquireExperience() const

That I know is referencing kUnit when called? Is this even possible to do from CvUnitInfo (it seems like it should be, since everything is inside kUnit that I need, I just don't know how to reference it).
 
Try as I might I can't wrap my brain around doing this. My main problem is, if I create this function in CvUnitInfo, How do I know it is referencing the correct unit type; in other words how do a I get an object inside
bool CvUnitInfo::canAquireExperience() const

That I know is referencing kUnit when called? Is this even possible to do from CvUnitInfo (it seems like it should be, since everything is inside kUnit that I need, I just don't know how to reference it).

you can use the this pointer (just google for some examples). In this case you don't need it though, just call getUnitCombatType() in CvUnitInfo::canAquireExperience()
 
What's to the left of that function call then? Will this just work?

Code:
bool CvUnitInfo::canAquireExperience() const
{
	eUnitCombatType = getUnitCombatType()
...rest of code that does the promotions loop
	{
		return true
	}		
	return false
}
 
Better yet:
Code:
bool CvUnitInfo::canAquireExperience() const
{
	return canAcquireExperience(getUnitInfo()->getUnitType());
}
(Written from memory, getUnitType may not be correct)
 
I don't know what Afforess is going for as you'd then need a second canAquireExperience() function that takes a CombatTypes parameter, but you can call functions on the current object just as if they were global functions as you've written. The compiler automatically puts "this->" in front.
 
Back
Top Bottom