Loop through a player's free units

TC01

Deity
Joined
Jun 28, 2009
Messages
2,216
Location
Irregularly Online
So, how exactly do I do this?

I'm using the following code to do this for buildings:

Code:
pCiv = gc.getCivilizationInfo(pPlayer.getCivilizationType())
for iBuildingClass in range(gc.getNumBuildingClassInfos()):
	if iBuildingClass != gc.getInfoTypeForString("BUILDINGCLASS_CAPITOL"):
		if pCiv.isCivilizationFreeBuildingClass(iBuildingClass):
			iBuilding = pCiv.getCivilizationBuildings(iBuildingClass)
			pCity.setNumRealBuilding(iBuilding, pCity.getNumRealBuilding(iBuilding) + 1)
			pPlanet.setHasBuilding(iBuilding, true)

However, there is not a isCivilizationFreeUnitClass() function. Instead, there is a getCivilizationFreeUnitsClass() function. What I don't get is how to use it. What does it return? I get that I would do this:

Code:
for iUnitClass in range(gc.getNumUnitClassInfos()):
	val = pCiv.getCivilizationFreeUnitsClass(iUnitClass)

What I'm not sure about is how to translate val to the unit type of the free unit class. I'm pretty sure it's not just returning the unit type of the free unit class- since that doesn't seem to be working.
 
gc.getUnitClassInfo(iUnitClass).getDefaultUnitIndex() might work. I'm not sure if a "DefaultUnitIndex" is a unit type or something else though.
 
I don't think that's what I want- I want to look at the "free" units a player would get at the start of the game, not the default unit types for each unit class.

Unless I misunderstood what you posted?
 
From looking at the function CvPlayer::initFreeUnits() in the SDK, it appears that the returned value, "val" in your example, is the number of the passed unit class which that civ gets for free (the number in the iFreeUnits tag in the FreeUnitClass). Since you already know what the unit class you passed it was, iUnitClass, you should use the getCivilizationUnits on that class to get the specific unit type for the civ. So you'd presumably then create "val" number of units of the type that that civ gets for that class. Or you might not use "val" directly at this point...

That function in CvPlayer also shows how the number is modified by the start era's free unit multiplier and by the handicap info's AI starting unit multiplier (only for the AIs, of course).

It also does completely separate checks involving the era and handicap related starting defense units, starting worker units, and then starting explorer units.

There are various things, involving checking for the ability to actually build units and then finding the best plot on which to place them, going on the the addFreeUnit and addFreeUnitAI that it calls to do the adding too. You may be able to ignore all that, assuming this is for FF+ or something along those lines, since the starting units can just be piled on top of the star system.
 
From looking at the function CvPlayer::initFreeUnits() in the SDK, it appears that the returned value, "val" in your example, is the number of the passed unit class which that civ gets for free (the number in the iFreeUnits tag in the FreeUnitClass). Since you already know what the unit class you passed it was, iUnitClass, you should use the getCivilizationUnits on that class to get the specific unit type for the civ. So you'd presumably then create "val" number of units of the type that that civ gets for that class. Or you might not use "val" directly at this point...

That function in CvPlayer also shows how the number is modified by the start era's free unit multiplier and by the handicap info's AI starting unit multiplier (only for the AIs, of course).

It also does completely separate checks involving the era and handicap related starting defense units, starting worker units, and then starting explorer units.

There are various things, involving checking for the ability to actually build units and then finding the best plot on which to place them, going on the the addFreeUnit and addFreeUnitAI that it calls to do the adding too. You may be able to ignore all that, assuming this is for FF+ or something along those lines, since the starting units can just be piled on top of the star system.

Thanks God-Emperor.

It is indeed for FF+... I'm making the mapscripts get a player's starting units from the XML and add them on top of the solar system, to remove hardcoded units (well, unit) from the mapscripts.
 
It is indeed for FF+... I'm making the mapscripts get a player's starting units from the XML and add them on top of the solar system, to remove hardcoded units (well, unit) from the mapscripts.
I suspected as much.

It's a good addition, especially for mod-mods where the starting unit(s) are different but also just for FF+ itself. An extra bonus for some civ could be to start with more - like a PDS and a scout ship.
 
Back
Top Bottom