// Handles Squad Member Promotions.
// Worthless Bums 2012.


// Promotions are stored in strings with 0 meaning talent is not taken and 1 meaning talent is taken.

// Set promotion icon.
function setPromotionIcon (%characterIndex)
{//echo("setPromotionIcon()");
	if ($playerSquad[%characterIndex].rank == 4)
	{
		triggerReachSergeant();
		%allSgts = true;
		for (%i = 0; %i < 3; %i++)
		{
			if (isObject($playerSquad[%i]))
			{
				if ($playerSquad[%i].rank < 4)
				{
					%allSgts = false;
					%i = 9999;
				}
			}
		}
		if (%allSgts)
		{
			triggerFullSergeant();
		}
	}
	// Create the promotion animation.
	%promotionAnimation = createPromotionAnimation(%characterIndex);
}
// Pushes the Promotion GUI.
function pushPromotionGui ()
{//echo("pushPromotionGui()");
	// Remove promotion animation if any.
	if (isObject($playerSquad[$activeCharacter].promotionAnimation))
	{
		($playerSquad[$activeCharacter].promotionAnimation).safeDelete();
	}
	
	// Play Promotion GUI sound effect.
	//playAudio("Audio:togglePromotions", EffectsAudioVolume.value);
	
	// Set active character promotions.
	setActiveCharacterPromotions();
	
	//$timescale = 0;
	initializePromotionGUI();
	
	// Juice the push!
	// Calculate the center of the screen.
	%xSize = getWord($Game::Resolution, 0);
	%ySize = getWord($Game::Resolution, 1);
	%endPosX = (%xSize - getWord(promotionPanelScreen.extent, 0)) / 2 - 47;
	%endPosY = (%ySize - getWord(promotionPanelScreen.extent, 1)) / 2;
	promotionPanelScreen.position = -800 SPC %endPosY;	// Start here.
	// Tween movement.
	%delay = 8.0;
	%totalSlices = 100;
	for (%i = 1; %i <= %totalSlices; %i++)
	{//echo("%i =" SPC %i);
		%slice = mRound((%xSize + 800) / %totalSlices);
		%position = (%slice * %i - 800) SPC %endPosY;
		schedule(%i * %delay, 0, "setPromotionPanelScreenPosition", %position);
		if ((%slice * %i - 800) >= %endPosX)
		{
			// UI tween camera shake.
			schedule(%i * %delay, 0, "uiTweenCameraShake");
			%i = %totalSlices + 1;
		}
	}
	
	Canvas.pushDialog(promotionGui);
	$promotionGuiPushed = true;
}
function setPromotionPanelScreenPosition (%position)
{//echo("setPromotionPanelScreenPosition()");
	promotionPanelScreen.position = %position;
}
// Pops the Promotion GUI.
function popPromotionGui ()
{//echo("popPromotionGui()");
	// Juice the pop!
	// Calculate the center of the screen.
	%xSize = getWord($Game::Resolution, 0);
	%ySize = getWord($Game::Resolution, 1);
	%endPosX = -800;
	%endPosY = (%ySize - getWord(promotionPanelScreen.extent, 1)) / 2;
	promotionPanelScreen.position = -800 SPC %endPosY;	// Start here.
	// Tween movement.
	%delay = 8.0;
	%totalSlices = 100;
	for (%i = 1; %i <= %totalSlices; %i++)
	{//echo("%i =" SPC %i);
		%slice = mRound((%xSize + 800) / %totalSlices);
		%position = (-1 * %slice * %i) SPC %endPosY;
		schedule(%i * %delay, 0, "setPromotionPanelScreenPosition", %position);
		if (-1 * %slice * %i <= %endPosX)
		{
			// UI tween camera shake.
			//schedule(%i * %delay, 0, "uiTweenCameraShake");
			%i = %totalSlices + 1;
		}
	}
	
	// Pop the squad info screen gui.
	Canvas.schedule(400, "popDialog", promotionGui);
	$promotionGuiPushed = false;
}
// Pushes the Promotion Confirmation GUI.
function pushPromotionConfirmationGui ()
{//echo("pushPromotionConfirmationGui()");
//echo("$selectedTalent =" SPC $selectedTalent);
	// Set text to describe talent to be taken by confirming.
	%text = "";
	switch ($selectedTalent)
	{
		case 0:	%text = pfcTalent1.tooltip;
		case 1:	%text = pfcTalent2.tooltip;
		case 2:	%text = lcplTalent1.tooltip;
		case 3:	%text = lcplTalent2.tooltip;
		case 4:	%text = cplTalent1.tooltip;
		case 5:	%text = cplTalent2.tooltip;
		case 6:	%text = sgtTalent1.tooltip;
		case 7:	%text = sgtTalent2.tooltip;
	}
	//echo("$selectedTalent =" SPC $selectedTalent);
	//echo("%text =" SPC %text);
	talentToConfirmDescription.setText(%text);
	// Play Promotion GUI sound effect.
	playAudio("Audio:togglePromotions", EffectsAudioVolume.value);
	Canvas.pushDialog(promotionConfirmationGui);
	$promotionConfirmationGuiPushed = true;
}
// Pops the Promotion Confirmation GUI.
function popPromotionConfirmationGui ()
{//echo("popPromotionConfirmationGui()");
	// Play Promotion GUI sound effect.
	playAudio("Audio:togglePromotions", EffectsAudioVolume.value);
	
	Canvas.popDialog(promotionConfirmationGui);
	$promotionConfirmationGuiPushed = false;
}
// Initialize the Promotion GUI based on active character.
function initializePromotionGUI ()
{//echo("initializePromotionGUI()");
	$selectedTalent = -1;
	switch ($playerSquad[$activeCharacter].characterClass)
	{
		case 0:	// Leader.
			initializeLeaderTalents();
			
		case 1:	// Scout.
			initializeScoutTalents();
			
		case 2:	// Grenadier.
			initializeGrenadierTalents();
			
		case 3:	// Support.
			initializeSupportTalents();
			
		case 4:	// Engineer.
			initializeEngineerTalents();
	}
	initializeRetraining();
	initializeRankHighlights();
}
// Set active character's promotion talents.
function setActiveCharacterPromotions ()
{//echo("setActiveCharacterPromotions()");
	switch ($playerSquad[$activeCharacter].characterClass)
	{
		case 0:	initializeLeaderTalents();
		case 1:	initializeScoutTalents();
		case 2:	initializeGrenadierTalents();
		case 3:	initializeSupportTalents();
		case 4:	initializeEngineerTalents();
	}
	initializeRetraining();
}
// Helper function to set stat and skill promotion text.
function setCharacterStatAndSkillPromotionText (%characterIndex)
{
	// Set Stat Info Text.
	characterPowerStat.setText(			"Power..............."	@	$playerSquad[%characterIndex].power);
	characterSpeedStat.setText(			"Speed..............."	@	$playerSquad[%characterIndex].speed);
	characterEnduranceStat.setText(		"Strength........." 	@	$playerSquad[%characterIndex].endurance);
	// Set Skill Info Text.
	characterBashSkill.setText(			"Smasher.........."		@	$playerSquad[%characterIndex].bash);
	characterDemolitionsSkill.setText(	"Bomb Tech......" 		@	$playerSquad[%characterIndex].demolitions);
	characterArmsSkill.setText(			"Precision........." 	@	$playerSquad[%characterIndex].arms);
}
function setCharacterPromotionInfo (%infoText)
{//echo("setCharacterPromotionInfo()");
	//if (isObject(characterPromotionInfo))
	//{
		//characterPromotionInfo.setText(%infoText);	// Fuck it. It's always the $activeCharacter so just parse it here!
		
		%statsText = %statColor @ "Stat Points..."		@	$playerSquad[$activeCharacter].statPoints;
		statPointPoolPromotionInfo.setText(%statsText);
		
		%skillsText = %skillColor @ "Skill Points...."	@	$playerSquad[$activeCharacter].skillPoints;
		skillPointPoolPromotionInfo.setText(%skillsText);
	//}
}
// Initializes retraining in the Promotion GUI.
function initializeRetraining ()
{//echo("initializeRetraining()");
	if ($playerSquad[$activeCharacter].rank >= 4)
	{
		retrainingPromotionsButton.visible = true;
		RetrainingButtons.visible = true;
	}
	else
	{
		retrainingPromotionsButton.visible = false;
		RetrainingButtons.visible = false;
	}
}
// Initializes rank highlighting in the Promotion GUI.
function initializeRankHighlights ()
{//echo("initializeRankHighlights()");
	PromotionRankPFCHighlight.setVisible(0);
	PromotionRankLCPLHighlight.setVisible(0);
	PromotionRankCPLHighlight.setVisible(0);
	PromotionRankSGTHighlight.setVisible(0);
	switch ($playerSquad[$activeCharacter].rank)
	{
		case 1:	// PFC.
			PromotionRankPFCHighlight.setVisible(1);
			PromotionRankLCPLHighlight.setVisible(0);
			PromotionRankCPLHighlight.setVisible(0);
			PromotionRankSGTHighlight.setVisible(0);
		case 2:	// PFC.
			PromotionRankPFCHighlight.setVisible(1);
			PromotionRankLCPLHighlight.setVisible(1);
			PromotionRankCPLHighlight.setVisible(0);
			PromotionRankSGTHighlight.setVisible(0);
		case 3:	// PFC.
			PromotionRankPFCHighlight.setVisible(1);
			PromotionRankLCPLHighlight.setVisible(1);
			PromotionRankCPLHighlight.setVisible(1);
			PromotionRankSGTHighlight.setVisible(0);
		case 4:	// PFC.
			PromotionRankPFCHighlight.setVisible(1);
			PromotionRankLCPLHighlight.setVisible(1);
			PromotionRankCPLHighlight.setVisible(1);
			PromotionRankSGTHighlight.setVisible(1);
	}
}
// Clears rank highlighting in the Promotion GUI (i.e. for New Game!)
function clearRankHighlights ()
{//echo("clearRankHighlights()");
	switch ($playerSquad[$activeCharacter].rank)
	{
		case 1:	// PFC.
			PromotionRankPFCHighlight.setVisible(0);
		case 2:	// PFC.
			PromotionRankPFCHighlight.setVisible(0);
			PromotionRankLCPLHighlight.setVisible(0);
		case 3:	// PFC.
			PromotionRankPFCHighlight.setVisible(0);
			PromotionRankLCPLHighlight.setVisible(0);
			PromotionRankCPLHighlight.setVisible(0);
		case 4:	// PFC.
			PromotionRankPFCHighlight.setVisible(0);
			PromotionRankLCPLHighlight.setVisible(0);
			PromotionRankCPLHighlight.setVisible(0);
			PromotionRankSGTHighlight.setVisible(0);
	}
}
// Initializes the Promotion GUI with the Leader talent tree.
function initializeLeaderTalents ()
{//echo("initializeLeaderTalents()");
	// Text colors.
	%statColor = "\c6";
	%skillColor = "\c5";
	// Compose Info Text.
	%infoText = //"Talent Points: " 	@	$playerSquad[$activeCharacter].talentPoints		NL
				%statColor @ "Stat Points: "		@	$playerSquad[$activeCharacter].statPoints		NL
				%skillColor @ "Skill Points: "	@	$playerSquad[$activeCharacter].skillPoints;
	// Set Leader Information Text.
	setCharacterPromotionInfo(%infoText);
	
	// Set Stat Info Text.
	setCharacterStatAndSkillPromotionText($activeCharacter);
	
	// Set Talent Bitmaps.
	if (getWord($playerSquad[$activeCharacter].talents, 0) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Charge_h";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/IronResolve_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 1) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Charge_i";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/IronResolve_h";
	}
	else
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Charge";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/IronResolve";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 2) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/SnapShots_h";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/FlakRounds_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 3) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/SnapShots_i";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/FlakRounds_h";
	}
	else
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/SnapShots";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/FlakRounds";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 4) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Radar_h";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Firefighter_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 5) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Radar_i";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Firefighter_h";
	}
	else
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Radar";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Firefighter";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 6) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Stim_h";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Vigilance_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Stim_i";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Vigilance_h";
	}
	else
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Stim";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Vigilance";
	}
	
	// Set Talent Tooltips.
	pfcTalent1.tooltip = "Charge: Charges up to 3 tiles away and can knock back (" @ $chargeAPCost @ " AP)";
	pfcTalent2.tooltip = $LeaderPfcPassiveTalent;	// Iron Resolve.
	
	lcplTalent1.tooltip = "Snap Shots: Fire at all enemies in range (" @ $snapShotsAPCost @ " AP)";
	lcplTalent2.tooltip = $LeaderLCplPassiveTalent;	// Flak Rounds.
	
	cplTalent1.tooltip = "Radar: Reveals nearby enemies. (" @ $radarAPCost @ " AP)";
	cplTalent2.tooltip = $LeaderCplPassiveTalent;	// Firefighter
	
	sgtTalent1.tooltip = "Stim: Increases squad ranged damage by " @ $squadStimDamageBoost @ " and accuracy by " @ $squadStimAccuracyBoost @ "% (" @ $stimAPCost @ " AP)";
	sgtTalent2.tooltip = $LeaderSgtPassiveTalent;	// Vigilance
}

// Initializes the Promotion GUI with the Scout talent tree.
function initializeScoutTalents ()
{//echo("initializeScoutTalents()");
	// Compose Info Text.
	%infoText = //"Talent Points: " 	@	$playerSquad[$activeCharacter].talentPoints		NL
				"Stat Points: "		@	$playerSquad[$activeCharacter].statPoints		NL
				"Skill Points: "		@	$playerSquad[$activeCharacter].skillPoints;
	// Set Scout Information Text.
	setCharacterPromotionInfo(%infoText);
	
	// Set Stat Info Text.
	setCharacterStatAndSkillPromotionText($activeCharacter);
	
	// Set Talent Bitmaps.
	if (getWord($playerSquad[$activeCharacter].talents, 0) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Scoped_h";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/SecondSkin_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 1) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Scoped_i";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/SecondSkin_h";
	}
	else
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Scoped";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/SecondSkin";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 2) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/CalledShot_h";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Stealthy_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 3) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/CalledShot_i";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Stealthy_h";
	}
	else
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/CalledShot";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Stealthy";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 4) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Garrote_h";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Autoloader_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 5) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Garrote_i";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Autoloader_h";
	}
	else
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Garrote";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Autoloader";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 6) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Stasis_h";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Biohazard_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Stasis_i";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Biohazard_h";
	}
	else
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Stasis";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Biohazard";
	}
	
	// Set Talent Tooltips.
	pfcTalent1.tooltip = "Scoped: Shot with +" @ $scopedAccuracyBoost @ "% accuracy and +" @ $scopedDamageBoost @ " damage (" @ $scopedAPCost  @" AP)";
	pfcTalent2.tooltip = $ScoutPfcPassiveTalent;	// Second Skin.
	
	lcplTalent1.tooltip = "Called Shot: Shot that reduces target AP by " @ $caledShotAPDeduction @ " (" @ $calledShotAPCost @ " AP)";
	lcplTalent2.tooltip = $ScoutLCplPassiveTalent;	// Stealthy.
	
	cplTalent1.tooltip = "Garrote: Deal " @ $garroteDamage @ " damage to an adjacent target if facing away. Ignores armor. (" @ $garroteAPCost @ " AP)";
	cplTalent2.tooltip = $ScoutCplPassiveTalent;	// Autoloader.
	
	sgtTalent1.tooltip = "Stasis: Shot that prevents action to and from the target (" @ $stasisAPCost @ " AP)";
	sgtTalent2.tooltip = $ScoutSgtPassiveTalent;	// Biohazard.
}

// Initializes the Promotion GUI with the Grenadier talent tree.
function initializeGrenadierTalents ()
{//echo("initializeGrenadierTalents()");
	// Compose Info Text.
	%infoText = //"Talent Points: " 	@	$playerSquad[$activeCharacter].talentPoints		NL
				"Stat Points: "		@	$playerSquad[$activeCharacter].statPoints		NL
				"Skill Points: "		@	$playerSquad[$activeCharacter].skillPoints;
	// Set Grenadier Information Text.
	setCharacterPromotionInfo(%infoText);
	
	// Set Stat Info Text.
	setCharacterStatAndSkillPromotionText($activeCharacter);
	
	// Set Talent Bitmaps.
	if (getWord($playerSquad[$activeCharacter].talents, 0) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Napalm_h";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/Annihilation_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 1) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Napalm_i";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/Annihilation_h";
	}
	else
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/Napalm";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/Annihilation";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 2) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Batter_h";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/MineSweeper_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 3) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Batter_i";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/MineSweeper_h";
	}
	else
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Batter";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/MineSweeper";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 4) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Pincer_h";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/AblativeArmor_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 5) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Pincer_i";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/AblativeArmor_h";
	}
	else
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Pincer";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/AblativeArmor";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 6) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/EMP_h";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/SmartBombs_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/EMP_i";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/SmartBombs_h";
	}
	else
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/EMP";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/SmartBombs";
	}
	
	// Set Talent Tooltips.
	pfcTalent1.tooltip = "Napalm: Sets targets on fire. (" @ $napalmAPCost @ " AP)";
	pfcTalent2.tooltip = $GrenadierPfcPassiveTalent;	// Annihilation.
	
	lcplTalent1.tooltip = "Batter: Knock back adjacent enemies. (" @ $batterAPCost @ " AP)";
	lcplTalent2.tooltip = $GrenadierLCplPassiveTalent;	// Minesweeper.
	
	cplTalent1.tooltip = "Pincer: Melee attack that reduces armor by " @ $pincerARReduction @ ". (" @ $pincerAPCost @ " AP)";
	cplTalent2.tooltip = $GrenadierCplPassiveTalent;	// Ablative Armor.
	
	sgtTalent1.tooltip = "EMP: Deals " @ $EmpDamage @ " damage to all nearby robots (" @ $empAPCost @" AP)";
	sgtTalent2.tooltip = $GrenadierSgtPassiveTalent;	// Smart Bombs.
}

// Initializes the Promotion GUI with the Support talent tree.
function initializeSupportTalents ()
{//echo("initializeSupportTalents()");
	// Compose Info Text.
	%infoText = //"Talent Points: " 	@	$playerSquad[$activeCharacter].talentPoints		NL
				"Stat Points: "		@	$playerSquad[$activeCharacter].statPoints		NL
				"Skill Points: "	@	$playerSquad[$activeCharacter].skillPoints;
	// Set Support Information Text.
	setCharacterPromotionInfo(%infoText);
	
	// Set Stat Info Text.
	setCharacterStatAndSkillPromotionText($activeCharacter);
	
	// Set Talent Bitmaps.
	if (getWord($playerSquad[$activeCharacter].talents, 0) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/PointBlank_h";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/Hulk_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 1) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/PointBlank_i";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/Hulk_h";
	}
	else
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/PointBlank";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/Hulk";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 2) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/HailMary_h";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/SharkRounds_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 3) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/HailMary_i";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/SharkRounds_h";
	}
	else
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/HailMary";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/SharkRounds";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 4) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Bulwark_h";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Sentry_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 5) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Bulwark_i";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Sentry_h";
	}
	else
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Bulwark";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Sentry";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 6) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Turret_h";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Flanker_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Turret_i";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Flanker_h";
	}
	else
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Turret";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/Flanker";
	}
	
	// Set Talent Tooltips.
	pfcTalent1.tooltip = "Point Blank: Normal damage and shreds " @ $pointBlankArmorReduction @ "% armor. (" @ $pointBlankAPCost @ " AP)";
	pfcTalent2.tooltip = $SupportPfcPassiveTalent;		// Hulk.
	
	lcplTalent1.tooltip = "Hail Mary: Increases accuracy by " @ $hailMaryAccuracyBoost @ "% and range by " @ $hailMaryRangeBoost @ ". (" @ $hailMaryAPCost @ " AP)";
	lcplTalent2.tooltip = $SupportLCplPassiveTalent;	// Shark Rounds.
	
	cplTalent1.tooltip = "Bulwark: Places a wall tile on a free space. (" @ $bulwarkAPCost @ " AP)";
	cplTalent2.tooltip = $SupportCplPassiveTalent;		// Sentry.
	
	sgtTalent1.tooltip = "Turret: Drops a sentry turret. Costs " @ $squadTurretAmmoCost @ " magazines. (" @ $turretAPCost @ " AP)";
	sgtTalent2.tooltip = $SupportSgtPassiveTalent;		// Flanker.
}

// Initializes the Promotion GUI with the Engineer talent tree.
function initializeEngineerTalents ()
{//echo("initializeEngineerTalents()");
	// Compose Info Text.
	%infoText = //"Talent Points: " 	@	$playerSquad[$activeCharacter].talentPoints		NL
				"Stat Points: "		@	$playerSquad[$activeCharacter].statPoints		NL
				"Skill Points: "	@	$playerSquad[$activeCharacter].skillPoints;
	// Set Support Information Text.
	setCharacterPromotionInfo(%infoText);
	
	// Set Stat Info Text.
	setCharacterStatAndSkillPromotionText($activeCharacter);
	
	// Set Talent Bitmaps.
	if (getWord($playerSquad[$activeCharacter].talents, 0) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/TargetScan_h";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/StunShots_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 1) == 1)
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/TargetScan_i";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/StunShots_h";
	}
	else
	{
		pfcTalent1.bitmap = "^Sprites/assets/images/UI/talents/TargetScan";
		pfcTalent2.bitmap = "^Sprites/assets/images/UI/talents/StunShots";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 2) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/MagFist_h";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Hackery_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 3) == 1)
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/MagFist_i";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Hackery_h";
	}
	else
	{
		lcplTalent1.bitmap = "^Sprites/assets/images/UI/talents/MagFist";
		lcplTalent2.bitmap = "^Sprites/assets/images/UI/talents/Hackery";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 4) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Weld_h";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/ActiveTargeting_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 5) == 1)
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Weld_i";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/ActiveTargeting_h";
	}
	else
	{
		cplTalent1.bitmap = "^Sprites/assets/images/UI/talents/Weld";
		cplTalent2.bitmap = "^Sprites/assets/images/UI/talents/ActiveTargeting";
	}
	
	if (getWord($playerSquad[$activeCharacter].talents, 6) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Ammosmith_h";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/TechUpgrade_i";
	}
	else if (getWord($playerSquad[$activeCharacter].talents, 7) == 1)
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Ammosmith_i";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/TechUpgrade_h";
	}
	else
	{
		sgtTalent1.bitmap = "^Sprites/assets/images/UI/talents/Ammosmith";
		sgtTalent2.bitmap = "^Sprites/assets/images/UI/talents/TechUpgrade";
	}
	
	// Set Talent Tooltips.
	pfcTalent1.tooltip  = "Target Scan: Reveals target info. (" @ $targetScanAPCost @ " AP)";
	pfcTalent2.tooltip  = $EngineerPfcPassiveTalent;	// Stun Shots.
	
	lcplTalent1.tooltip = "Mag Fist: Melee that turns target around. (" @ $magFistAPCost @ " AP)";
	lcplTalent2.tooltip = $EngineerLCplPassiveTalent;	// Hackery.
	
	cplTalent1.tooltip  = "Weld: Convert " @ $weldARCost @ " Armor Rating to " @ $weldHealthGain @ " Health. (" @ $weldAPCost @ " AP)";
	cplTalent2.tooltip  = $EngineerCplPassiveTalent;	// Active Targeting.
	
	sgtTalent1.tooltip  = "Ammosmith: Convert " @ $ammosmithGrenadeCost @ " grenades into " @ $ammosmithAmmoGain @ " ammo crate. (" @ $ammosmithAPCost @ " AP)";
	sgtTalent2.tooltip  = $EngineerSgtPassiveTalent;	// Tech Upgrade.
}


//-----------------------------------------------------------------------------------------
// Mouse Events for talent buttons.
//-----------------------------------------------------------------------------------------
// Talent Button on the Promotion Panel was clicked.
function talentButtonClicked (%talentIndex)
{//echo("talentButtonClicked()");
	// Determine what rank this talent is on.
	%rank = -1;
	switch (%talentIndex)
	{
		// PFC.
		case 0:	%rank = 1;
		case 1:	%rank = 1;
		// LCpl.
		case 2:	%rank = 2;
		case 3:	%rank = 2;
		// Cpl.
		case 4:	%rank = 3;
		case 5:	%rank = 3;
		// Sgt.
		case 6:	%rank = 4;
		case 7:	%rank = 4;
	}
	// Sanity check: The rank must be one in the switch statement!
	if (%rank <= -1)
	{
		
		return;
	}
	
	// Determine the two talent indices to compare (one must obviously be the selected talent).
	%compareTalent = -1;
	if (%talentIndex % 2 == 0)
	{
		%compareTalent = %talentIndex + 1;
	}
	else
	{
		%compareTalent = %talentIndex - 1;
	}
	
	// When the player clicks a talent we need to check a few things:
	// 1) Check that he can actually access that rank of talent.
	if ($playerSquad[$activeCharacter].rank >= %rank)
	{
		// 2) Check if any of this rank's talents are already taken.
		%talents = $playerSquad[$activeCharacter].talents;
		// PFC talents.
		if (getWord(%talents, %talentIndex)		== 1	||	// Rank Talent 1
			getWord(%talents, %compareTalent) 	== 1)		// Rank Talent 2
		{
			// Talent already choosen! Don't let the player select another from this rank!
			// Play can't take talent sound effect.
			playAudio("Audio:cantTakeTalent", EffectsAudioVolume.value);
			
			return;
		}
		else
		{
			// We're good to go. Select this talent and pop up the appropriate confirmation box.
			$selectedTalent = %talentIndex;
			// Push Promotion Confirmation GUI.
			pushPromotionConfirmationGui();
		}
	}
}

// PFC Buttons.
function pfcTalent1::onMouseEnter ()
{//echo("pfcTalent1::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(pfcTalent1.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function pfcTalent1::onMouseLeave ()
{//echo("pfcTalent1::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
function pfcTalent2::onMouseEnter ()
{//echo("pfcTalent2::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(pfcTalent2.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function pfcTalent2::onMouseLeave ()
{//echo("pfcTalent2::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
// LCpl Buttons.
function lcplTalent1::onMouseEnter ()
{//echo("lcplTalent1::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(lcplTalent1.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function lcplTalent1::onMouseLeave ()
{//echo("lcplTalent1::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
function lcplTalent2::onMouseEnter ()
{//echo("lcplTalent2::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(lcplTalent2.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function lcplTalent2::onMouseLeave ()
{//echo("lcplTalent2::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
// Cpl Buttons.
function cplTalent1::onMouseEnter ()
{//echo("cplTalent1::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(cplTalent1.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function cplTalent1::onMouseLeave ()
{//echo("cplTalent1::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
function cplTalent2::onMouseEnter ()
{//echo("cplTalent2::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(cplTalent2.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function cplTalent2::onMouseLeave ()
{//echo("cplTalent2::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
// Sgt Buttons.
function sgtTalent1::onMouseEnter ()
{//echo("sgtTalent1::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(sgtTalent1.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function sgtTalent1::onMouseLeave ()
{//echo("sgtTalent1::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}
function sgtTalent2::onMouseEnter ()
{//echo("sgtTalent2::onMouseEnter()");
	// 
	//mousedOverTalentDescription.setVisible(true);
	//mousedOverTalentDescription.setText(sgtTalent2.tooltip);
	//selectedTalentDescription.setVisible(false);
}
function sgtTalent2::onMouseLeave ()
{//echo("sgtTalent2::onMouseLeave()");
	// 
	//mousedOverTalentDescription.setVisible(false);
	//selectedTalentDescription.setVisible(true);
}

function retrainingAsClass (%classType)
{//echo("retrainingAsClass()");echo("%classType =" SPC %classType);
	if (%classType >= 0)
	{
		$RetrainingClass = %classType;
		switch ($RetrainingClass)
		{
			case 0:	// Leader.
				RetrainingIndicatorButton.Visible = true;
				RetrainingIndicatorButton.position = getWord(LeaderRetrainingButton.position, 0) SPC getWord(LeaderRetrainingButton.position, 1) + 32;
				
			case 1:	// Scout.
				RetrainingIndicatorButton.Visible = true;
				RetrainingIndicatorButton.position = getWord(ScoutRetrainingButton.position, 0) SPC getWord(ScoutRetrainingButton.position, 1) + 32;
				
			case 2:	// Grenadier.
				RetrainingIndicatorButton.Visible = true;
				RetrainingIndicatorButton.position = getWord(GrenadierRetrainingButton.position, 0) SPC getWord(GrenadierRetrainingButton.position, 1) + 32;
				
			case 3:	// Support.
				RetrainingIndicatorButton.Visible = true;
				RetrainingIndicatorButton.position = getWord(SupportRetrainingButton.position, 0) SPC getWord(SupportRetrainingButton.position, 1) + 32;
				
			case 4:	// Engineer.
				RetrainingIndicatorButton.Visible = true;
				RetrainingIndicatorButton.position = getWord(EngineerRetrainingButton.position, 0) SPC getWord(EngineerRetrainingButton.position, 1) + 32;
		}
		playAudio("Audio:experienceGain", EffectsAudioVolume.value);
	}
}
function retraining ()
{//echo("retraining()");
	if ($RetrainingClass >= 0)
	{
		// Set new class for active character.
		$playerSquad[$activeCharacter].characterClass = $RetrainingClass;
		// Change armor and weapon classes!
		$playerSquad[$activeCharacter].armor.squadMember = $RetrainingClass;
		$playerSquad[$activeCharacter].weapon.squadMember = $RetrainingClass;
		// Set retraining.
		$playerSquad[$activeCharacter].retraining = $playerSquad[$activeCharacter].retraining SPC $RetrainingClass;
		// Reset rank and talents.
		$playerSquad[$activeCharacter].rank = 0;
		$playerSquad[$activeCharacter].talents = "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0";
		$playerSquad[$activeCharacter].experience = 0;
		// Set new sprite animation.
		$playerSquad[$activeCharacter].Animation = "Sprites:" @ getUnitIdleAnimation($playerSquad[$activeCharacter]);
		
		// Clean up.
		initializeReadout();
		popPromotionGui();
		$RetrainingClass = -1;
	}
}