-- Lua for assiging a random promotion.
---------------------------------------------------------------------------------------
LuaEvents.AssignArchetype.Add(
--------------
-- function to select a random promotion, uses the UnitPromotions_RandomArchetypes table
function(pUnit, pPromotion, bRandom, pCombatType)
local tRandomArchetypes = {};
local iNumInTable = -1; --Adjusted for for loop.
local ePromotionToBeGained;
local ePromotion = "PromotionType = '" .. GameInfo.UnitPromotions[pPromotion].Type .. "'";
local eCombatType = false;
if pCombatType then
eCombatType = "UnitCombatType = '" .. GameInfo.UnitCombatTypes[pCombatType].Type .. "'";
end
-- Fill the Array
for row in GameInfo.UnitPromotions_Archetypes[ePromotion] do
iNumInTable = iNumInTable+1;
tRandomArchetypes[iNumInTable] = {};
tRandomArchetypes[iNumInTable][0] = row.NewPromotion;
tRandomArchetypes[iNumInTable][1] = row.UnitCombatType;
tRandomArchetypes[iNumInTable][2] = row.Weight;
end
local arrayMax = #tRandomArchetypes;
-- For Adapatation.
if eCombatType then
for row = 0, arrayMax do
if eCombatType == tRandomArchetypes[row][1] then
ePromotionToBeGained = tRandomArchetypes[row][0];
end
end
end
-- No Weights.
if not bRandom and not eCombatType then
local randomPromotion = math.random(0, iNumInTable);
ePromotionToBeGained = tRandomArchetypes[randomPromotion][0];
end
-- Use the Weights
while bRandom and not eCombatType do
local randomPromotion = math.random(0, iNumInTable);
local weight = tRandomArchetypes[randomPromotion][2];
local randomValue = (math.random(0, 100) + math.random(0, 100))/2;
local randomTest = math.random(60, 140);
if(weight+randomValue) >= randomTest then
bRandom = false;
ePromotionToBeGained = tRandomArchetypes[randomPromotion][0];
end
end
local pPromotionToBeGained = GameInfoTypes[ePromotionToBeGained];
pUnit:SetHasPromotion(pPromotion, false);
pUnit:SetHasPromotion(pPromotionToBeGained, true);
print("JFCMFunctions -|- AssignArchetype -|- UnitType: ",pUnit:GetUnitType()," Promotion: ",ePromotionToBeGained);
end)
---------------------------------------------------------------------------------------