Can this be done?

LivingLikeLogan

Chieftain
Joined
Sep 14, 2015
Messages
80
Is there a way to add a building that every time you build a unit. It receives a random promotion? If this is possible. How would it be done?
 
I'm currently on a laptop that takes ages to start up Civ5, so I haven't tested this code, but I think it should work:

Code:
local pPromotionList = {GameInfoTypes.PROMOTION_X, GameInfoTypes.PROMOTION_Y, GameInfoTypes.PROMOTION_Z}

function GiveRandomPromotion(ePlayer, iCity, eUnit, bGold, bFaithOrCulture)
	local pCity = Players[ePlayer]:GetCityByID(iCity)
	local pUnit = Players[ePlayer]:GetUnitByID(eUnit)
	if pCity:GetNumBuilding(GameInfoTypes.BUILDING_X) > 0 then
		pUnit:SetHasPromotion(pPromotionList[math.random(1,#pPromotionList)], true)
	end
end

GameEvents.CityTrained.Add(GiveRandomPromotion)

Replace "GameInfoTypes.PROMOTION_X, GameInfoTypes.PROMOTION_Y, GameInfoTypes.PROMOTION_Z" with a list of the promotions you want to be available (each prefixed with "GameInfoTypes."), and BUILDING_X with the building in question.
 
There's also the old Kris Swordsman's mystic blade, which is almost, but not quite what you're looking for - but easy to do. It is a promotion that is automatically given to the unit and has this line:
Code:
<HasPostCombatPromotions>true</HasPostCombatPromotions>
...which means that there'll be a random promotion given after the first time it is used in combat.

The promotions are chosen from a pre-determined list. In the mystic blade's case it's this:
Code:
<UnitPromotions_PostCombatRandomPromotion>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_INVULNERABIILITY</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_SNEAK_ATTACK</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_HEROISM</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_AMBITION</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_RESTLESSNESS</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_RECRUITMENT</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_ENEMY_BLADE</NewPromotion>
		</Row>
		<Row>
			<PromotionType>PROMOTION_MYSTIC_BLADE</PromotionType>
			<NewPromotion>PROMOTION_EVIL_SPIRITS</NewPromotion>
		</Row>
	</UnitPromotions_PostCombatRandomPromotion>

Again, not quite what you asked for, but may suit your purposes and is easy to do.
 
I'm currently on a laptop that takes ages to start up Civ5, so I haven't tested this code, but I think it should work:

Code:
local pPromotionList = {GameInfoTypes.PROMOTION_X, GameInfoTypes.PROMOTION_Y, GameInfoTypes.PROMOTION_Z}

function GiveRandomPromotion(ePlayer, iCity, eUnit, bGold, bFaithOrCulture)
	local pCity = Players[ePlayer]:GetCityByID(iCity)
	local pUnit = Players[ePlayer]:GetUnitByID(eUnit)
	if pCity:GetNumBuilding(GameInfoTypes.BUILDING_X) > 0 then
		pUnit:SetHasPromotion(pPromotionList[math.random(1,#pPromotionList)], true)
	end
end

GameEvents.CityTrained.Add(GiveRandomPromotion)

Replace "GameInfoTypes.PROMOTION_X, GameInfoTypes.PROMOTION_Y, GameInfoTypes.PROMOTION_Z" with a list of the promotions you want to be available (each prefixed with "GameInfoTypes."), and BUILDING_X with the building in question.

Just a note, that if you go this route, you should use Game.Rand() instead of math.random(). That way, if someone creates a multiplayer pack with your mod, Game.Rand() will ensure that the same number is generated for each player (math.random() will generate different numbers, causing a desync).
 
Back
Top Bottom