// Tracks experience gains and levelling for squad members.
// Worthless Bums 2012.


// Test function that grants the active squad member experience.
function giveXP (%xp)
{//echo("giveXP()");
	updateExperience("Gibbed, The Test Monster", %xp);
}

// Gives the entire squad experience.
function giveSquadXP (%xp)
{//echo("giveSquadXP()");
	if ($leaderPersist)
	{
		updateCharacterExperience(0, "", %xp);
	}
	if ($scoutPersist)
	{
		updateCharacterExperience(1, "", %xp);
	}
	if ($grenadierPersist)
	{
		updateCharacterExperience(2, "", %xp);
	}
	if ($supportPersist)
	{
		updateCharacterExperience(3, "", %xp);
	}
}
function giveSpottingXP ()
{//echo("giveSpottingXP()");
	%monsterName = "";
	if ($leaderPersist)
	{
		updateCharacterExperience(0, %monsterName, $dungeonLevel);
		createExperienceGainAnimation(getWord($playerSquad[0].getPosition(), 0), getWord($playerSquad[0].getPosition(), 1));
	}
	if ($scoutPersist)
	{
		updateCharacterExperience(1, %monsterName, $dungeonLevel);
		createExperienceGainAnimation(getWord($playerSquad[1].getPosition(), 0), getWord($playerSquad[1].getPosition(), 1));
	}
	if ($grenadierPersist)
	{
		updateCharacterExperience(2, %monsterName, $dungeonLevel);
		createExperienceGainAnimation(getWord($playerSquad[2].getPosition(), 0), getWord($playerSquad[2].getPosition(), 1));
	}
	if ($supportPersist)
	{
		updateCharacterExperience(3, %monsterName, $dungeonLevel);
		createExperienceGainAnimation(getWord($playerSquad[3].getPosition(), 0), getWord($playerSquad[3].getPosition(), 1));
	}
	playAudio("Audio:experienceGain", EffectsAudioVolume.value);
	updateSquadInfoGui();
}
function levelUpPointGain (%characterIndex)
{//echo("levelUpPointGain()");
	$playerSquad[%characterIndex].talentPoints++;
	$playerSquad[%characterIndex].statPoints++;
	$playerSquad[%characterIndex].skillPoints++;
}
function getExperienceLevelCap (%level)
{//echo("getExperienceLevelCap()");
	switch (%level)
	{
		case  1:	return $levelExperience2;		
		case  2:	return $levelExperience3;		
		case  3:	return $levelExperience4;		
		case  4:	return $levelExperience5;		
		case  5:	return $levelExperience6;		
		case  6:	return $levelExperience7;		
		case  7:	return $levelExperience8;		
		case  8:	return $levelExperience9;		
		case  9:	return $levelExperience10;
		case 10:	return $levelExperience11;
	}
}
function updateCharacterExperience (%character, %monsterName, %monsterLevel)
{//echo("updateCharacterExperience()");
	createExperienceGainAnimation(getWord($playerSquad[%character].getPosition(), 0), getWord($playerSquad[%character].getPosition(), 1));
	%multiplier = 1;
	%preExperience = $playerSquad[%character].experience;
	%bonusXP = 0;
	if ($playerSquad[$activeCharacter].perks $= "Spotter")	// "Spotter" perk grants extra spotting experience!
	{
		%bonusXP = 1;
	}
	$playerSquad[%character].experience += %monsterLevel * %multiplier + %bonusXP;
	%postExperience = $playerSquad[%character].experience;
	if (%preExperience < $levelExperience2 * getWordCount($playerSquad[%character].retraining)		&&
		%postExperience >= $levelExperience2 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 2" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience3 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience3 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 3" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience4 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience4 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 4" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience5 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience5 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 5" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience6 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience6 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 6" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience7 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience7 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 7" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience8 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience8 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 8" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience9 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience9 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 9" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience10 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience10 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 10" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
	else if (%preExperience < $levelExperience11 * getWordCount($playerSquad[%character].retraining)		&&
			 %postExperience >= $levelExperience11 * getWordCount($playerSquad[%character].retraining))
	{
		$playerSquad[%character].level++;
		updateSquadInfoGui();
		updateEventText($squadColor @ $playerSquad[%character].name SPC $friendlyActionColor @ "has reached level 11" @ $defaultColor @ "!");
		playLevelUpAnimation(%character);
		levelUpPointGain(%character);
		setPromotionIcon(%character);
	}
}
// Called whenever the active player kills a monster.
function updateExperience (%monsterName, %monsterLevel)
{//echo("updateExperience()");
	$GlobalStats.totalKills++;
	updateCharacterExperience($activeCharacter, %monsterName, %monsterLevel);
}
function getLevelFromExperience (%characterExperience)
{//echo("getLevelFromExperience()");
	%characterLevel = 1;
	if (%characterExperience >= $levelExperience2)
	{
		%characterLevel = 2;
	}
	if (%characterExperience >= $levelExperience3)
	{
		%characterLevel = 3;
	}
	if (%characterExperience >= $levelExperience4)
	{
		%characterLevel = 4;
	}
	if (%characterExperience >= $levelExperience5)
	{
		%characterLevel = 5;
	}
	if (%characterExperience >= $levelExperience6)
	{
		%characterLevel = 6;
	}
	if (%characterExperience >= $levelExperience7)
	{
		%characterLevel = 7;
	}
	if (%characterExperience >= $levelExperience8)
	{
		%characterLevel = 8;
	}
	if (%characterExperience >= $levelExperience9)
	{
		%characterLevel = 9;
	}
	if (%characterExperience >= $levelExperience10)
	{
		%characterLevel = 10;
	}
	if (%characterExperience >= $levelExperience11)
	{
		%characterLevel = 11;
	}
	return %characterLevel;
}
function getRankFromLevel (%characterLevel)
{//echo("getLevelFromExperience()");
	%characterRank = "Private";
	switch (%characterLevel)
	{
		case 0: %characterRank = "Pvt";
		case 1: %characterRank = "PFC";
		case 2: %characterRank = "LCpl";
		case 3: %characterRank = "Cpl";
		case 4: %characterRank = "Sgt";
		case 5: %characterRank = "GYSgt";
		case 6: %characterRank = "MSgt";
		case 7: %characterRank = "2ndLt";
		case 8: %characterRank = "1stLt";
	}
	return %characterRank;
}
function getRankName (%rank)
{//echo("getRankName()");
	switch (%rank)
	{
		case 0: %characterRank = "Pvt";
		case 1: %characterRank = "PFC";
		case 2: %characterRank = "LCpl";
		case 3: %characterRank = "Cpl";
		case 4: %characterRank = "Sgt";
		case 5: %characterRank = "GYSgt";
		case 6: %characterRank = "MSgt";
		case 7: %characterRank = "2ndLt";
		case 8: %characterRank = "1stLt";
	}
	return %characterRank;
}
function getRankBitmap (%rank)
{//echo("getRankBitmap()");
	%rankBitmap = "../../../Sprites/1/assets/images/Promotions/";
	switch$ (%rank)
	{
		case "Pvt": 	%rankBitmap = %rankBitmap @ "private";
		case "PFC": 	%rankBitmap = %rankBitmap @ "privateFirstClass";
		case "LCpl":	%rankBitmap = %rankBitmap @ "lanceCorporal";
		case "Cpl":	 	%rankBitmap = %rankBitmap @ "corporal";
		case "Sgt":	 	%rankBitmap = %rankBitmap @ "sergeant";
		case "GYSgt": 	%rankBitmap = %rankBitmap @ "gunnerySergeant";
		case "MSgt": 	%rankBitmap = %rankBitmap @ "masterSergeant";
		case "2ndLt": 	%rankBitmap = %rankBitmap @ "secondLieutenant";
		case "1stLt": 	%rankBitmap = %rankBitmap @ "firstLieutenant";
	}
	return %rankBitmap;
}