// Armor Handler.
// Worthless Bums 2012.


function triggerArmorRepair ()
{//echo("triggerArmorRepair()");
	%repair = ($playerSquad[$activeCharacter].armor).repair;
	if (%repair > 0)
	{
		$playerSquad[$activeCharacter].currentHealth += %repair;
		if ($playerSquad[$activeCharacter].currentHealth > $playerSquad[$activeCharacter].maxHealth)
		{
			$playerSquad[$activeCharacter].currentHealth = $playerSquad[$activeCharacter].maxHealth;
		}
	}
	scrollFloatingText($playerSquad[$activeCharacter], "+" @ %repair @ " HP", "regen");
}
function generateArmorCode ()
{//echo("generateArmorCode()");
	%firstLetter = getRandom(1, 26);
	%secondLetter = getRandom(1, 26);
	switch (%firstLetter)
	{
		case  1: %firstLetter = "A";
		case  2: %firstLetter = "B";
		case  3: %firstLetter = "C";
		case  4: %firstLetter = "D";
		case  5: %firstLetter = "E";
		case  6: %firstLetter = "F";
		case  7: %firstLetter = "G";
		case  8: %firstLetter = "H";
		case  9: %firstLetter = "I";
		case 10: %firstLetter = "J";
		case 11: %firstLetter = "K";
		case 12: %firstLetter = "L";
		case 13: %firstLetter = "M";
		case 14: %firstLetter = "N";
		case 15: %firstLetter = "O";
		case 16: %firstLetter = "P";
		case 17: %firstLetter = "Q";
		case 18: %firstLetter = "R";
		case 19: %firstLetter = "S";
		case 20: %firstLetter = "T";
		case 21: %firstLetter = "U";
		case 22: %firstLetter = "V";
		case 23: %firstLetter = "X";
		case 24: %firstLetter = "X";
		case 25: %firstLetter = "Y";
		case 26: %firstLetter = "Z";
	}
	switch (%secondLetter)
	{
		case  1: %secondLetter = "A";
		case  2: %secondLetter = "B";
		case  3: %secondLetter = "C";
		case  4: %secondLetter = "D";
		case  5: %secondLetter = "E";
		case  6: %secondLetter = "F";
		case  7: %secondLetter = "G";
		case  8: %secondLetter = "H";
		case  9: %secondLetter = "I";
		case 10: %secondLetter = "J";
		case 11: %secondLetter = "K";
		case 12: %secondLetter = "L";
		case 13: %secondLetter = "M";
		case 14: %secondLetter = "N";
		case 15: %secondLetter = "O";
		case 16: %secondLetter = "P";
		case 17: %secondLetter = "Q";
		case 18: %secondLetter = "R";
		case 19: %secondLetter = "S";
		case 20: %secondLetter = "T";
		case 21: %secondLetter = "U";
		case 22: %secondLetter = "V";
		case 23: %secondLetter = "X";
		case 24: %secondLetter = "X";
		case 25: %secondLetter = "Y";
		case 26: %secondLetter = "Z";
	}
	return %firstLetter @ %secondLetter;
}

// Generates a random armor amount for an armor given the squad member and dungeon level.
function generateRandomArmorRating (%squadMember, %dungeonLevel)
{//echo("generateRandomArmorRating()");
	%armorRating = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	switch (%squadMember)
	{
		case 0:	// Leader.
			%baseArmor = 4;
			%effectiveDungeonLevel = %dungeonLevel;
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			%bonusArmor1 = getRandom(0, %effectiveDungeonLevel);
			%bonusArmor2 = getRandom(0, %effectiveDungeonLevel);
			if (%bonusArmor1 >= %bonusArmor2)
			{
				%armorRating = %bonusArmor1 - %bonusArmor2;
			}
			else
			{
				%armorRating = %bonusArmor2 - %bonusArmor1;
			}
			%armorRating += %baseArmor;
		
		case 1:	// Scout.
			%baseArmor = 2;
			%effectiveDungeonLevel = %dungeonLevel;
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			%armorRating = mRound(getRandom(0, %effectiveDungeonLevel / 2));
			%armorRating += %baseArmor;
		
		case 2:	// Grenadier.
			%baseArmor = 6;
			%effectiveDungeonLevel = %dungeonLevel;
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			%armorRating = mRound(getRandom(0, %effectiveDungeonLevel / 3));
			%armorRating += %baseArmor;
		
		case 3:	// Support.
			%baseArmor = 3;
			%effectiveDungeonLevel = %dungeonLevel;
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			%armorRating = mRound(getRandom(0, %effectiveDungeonLevel / 3));
			%armorRating += %baseArmor;
		
		case 4:	// Engineer.
			%baseArmor = 5;
			%effectiveDungeonLevel = %dungeonLevel;
			if (%effectiveDungeonLevel > %effectiveDungeonCap)
			{
				%effectiveDungeonLevel = %effectiveDungeonCap;
			}
			%armorRating = mRound(getRandom(0, %effectiveDungeonLevel / 3));
			%armorRating += getRandom(0, 4);
			%armorRating += %baseArmor;
	}
	
	return %armorRating;
}

// Generates a random speed value for an armor given the squad member and dungeon level.
function generateRandomSpeed (%squadMember, %dungeonLevel)
{//echo("generateRandomSpeed()");
	%speed = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%speed = getRandom(0, 2);
			
		case 1:	// Scout.
			%speed = getRandom(1, 3);
			
		case 2:	// Grenadier.
			%speed = getRandom(0, 1);
			
		case 3:	// Support.
			%speed = getRandom(0, 1);
			
		case 4:	// Engineer.
			%speed = getRandom(0, 1);
	}
	
	return %speed;
}

// Generates a random repair value for an armor given the squad member and dungeon level.
function generateRandomRepair (%squadMember, %dungeonLevel)
{//echo("generateRandomRepair()");
	%repair = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%repair = getRandom(0, 1);
			
		case 1:	// Scout.
			%repair = getRandom(0, 1);
			
		case 2:	// Grenadier.
			%repair = getRandom(0, 1);
			
		case 3:	// Support.
			%repair = getRandom(0, 1);
			
		case 4:	// Engineer.
			%repair = getRandom(1, 3);
	}
	
	return %repair;
}

// Generates a random fire resistance value for an armor given the squad member and dungeon level.
function generateRandomFireResistance (%squadMember, %dungeonLevel)
{//echo("generateRandomFireResistance()");
	%resistance = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = mRound(%dungeonLevel / 2);
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 1:	// Scout.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 2:	// Grenadier.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 3:	// Support.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 4:	// Engineer.
			%resistance = getRandom(0, %effectiveDungeonCap);
	}
	
	return %resistance;
}

// Generates a random frost resistance value for an armor given the squad member and dungeon level.
function generateRandomFrostResistance (%squadMember, %dungeonLevel)
{//echo("generateRandomFrostResistance()");
	%resistance = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = mRound(%dungeonLevel / 2);
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 1:	// Scout.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 2:	// Grenadier.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 3:	// Support.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 4:	// Engineer.
			%resistance = getRandom(0, %effectiveDungeonCap);
	}
	
	return %resistance;
}

// Generates a random acid resistance value for an armor given the squad member and dungeon level.
function generateRandomAcidResistance (%squadMember, %dungeonLevel)
{//echo("generateRandomAcidResistance()");
	%resistance = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = mRound(%dungeonLevel / 2);
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 1:	// Scout.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 2:	// Grenadier.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 3:	// Support.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 4:	// Engineer.
			%resistance = getRandom(0, %effectiveDungeonCap);
	}
	
	return %resistance;
}

// Generates a random explosive resistance value for an armor given the squad member and dungeon level.
function generateRandomExplosiveResistance (%squadMember, %dungeonLevel)
{//echo("generateRandomExplosiveResistance()");
	%resistance = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = mRound(%dungeonLevel / 2);
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 1:	// Scout.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 2:	// Grenadier.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 3:	// Support.
			%resistance = getRandom(0, %effectiveDungeonCap);
			
		case 4:	// Engineer.
			%resistance = getRandom(0, %effectiveDungeonCap);
	}
	
	return %resistance;
}

// Generates a random oxygen value for an armor given the squad member and dungeon level.
function generateRandomOxygen (%squadMember, %dungeonLevel)
{//echo("generateRandomOxygen()");
	%oxygen = 0;
	%effectiveDungeonCap = 100;
	if (%dungeonLevel < %effectiveDungeonCap)
	{
		%effectiveDungeonCap = %dungeonLevel;
	}
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%oxygen = getRandom(3, 6) + getRandom(0, %effectiveDungeonCap);
			
		case 1:	// Scout.
			%oxygen = getRandom(3, 8) + getRandom(0, %effectiveDungeonCap);
			
		case 2:	// Grenadier.
			%oxygen = getRandom(3, 4) + getRandom(0, %effectiveDungeonCap);
			
		case 3:	// Support.
			%oxygen = getRandom(3, 4) + getRandom(0, %effectiveDungeonCap);
			
		case 4:	// Engineer.
			%oxygen = getRandom(3, 4) + getRandom(0, %effectiveDungeonCap);
	}
	
	return %oxygen;
}

// Generates a random armor name.
function generateRandomArmorName ()
{//echo("generateRandomArmorName()");
	%firstWord = getRandom(0, 41);
	%secondWord = getRandom(0, 19);
	
	switch (%firstWord)
	{
		case  0:	%firstWord = "Battle";
		case  1:	%firstWord = "Combat";
		case  2:	%firstWord = "Rough";
		case  3:	%firstWord = "Tough";
		case  4:	%firstWord = "Evader";
		case  5:	%firstWord = "Heavy";
		case  6:	%firstWord = "Light";
		case  7:	%firstWord = "Rugged";
		case  8:	%firstWord = "Ever";
		case  9:	%firstWord = "Grunt";
		case 10:	%firstWord = "Tuff";
		case 11:	%firstWord = "Ready";
		case 12:	%firstWord = "Worn";
		case 13:	%firstWord = "Shell";
		case 14:	%firstWord = "Turtle";
		case 15:	%firstWord = "Tin";
		case 16:	%firstWord = "Bunker";
		case 17:	%firstWord = "Personal";
		case 18:	%firstWord = "Loose";
		case 19:	%firstWord = "Tight";
		case 20:	%firstWord = "Layered";
		case 21:	%firstWord = "Chained";
		case 22:	%firstWord = "Chain";
		case 23:	%firstWord = "Lock";
		case 24:	%firstWord = "Locked";
		case 25:	%firstWord = "Locked";
		case 26:	%firstWord = "Guard";
		case 27:	%firstWord = "Sentry";
		case 28:	%firstWord = "Bronze";
		case 29:	%firstWord = "Bronzed";
		case 30:	%firstWord = "Long";
		case 31:	%firstWord = "Short";
		case 32:	%firstWord = "Marine";
		case 33:	%firstWord = "Navy";
		case 34:	%firstWord = "Army";
		case 35:	%firstWord = "Space";
		case 36:	%firstWord = "Calm";
		case 37:	%firstWord = "Rage";
		case 38:	%firstWord = "Mad";
		case 39:	%firstWord = "Angry";
		case 40:	%firstWord = "Hate";
		case 41:	%firstWord = "Blunt";
	}
	
	switch (%secondWord)
	{
		case  0:	%secondWord = "Bulwark";
		case  1:	%secondWord = "Shield";
		case  2:	%secondWord = "Casing";
		case  3:	%secondWord = "Plate";
		case  4:	%secondWord = "Matrix";
		case  5:	%secondWord = "Geared";
		case  6:	%secondWord = "Jumper";
		case  7:	%secondWord = "Hopper";
		case  8:	%secondWord = "Robo";
		case  9:	%secondWord = "Gear";
		case 10:	%secondWord = "Suit";
		case 11:	%secondWord = "Exo";
		case 12:	%secondWord = "Bastion";
		case 13:	%secondWord = "Plating";
		case 14:	%secondWord = "Defense";
		case 15:	%secondWord = "Defense";
		case 16:	%secondWord = "Absorb";
		case 17:	%secondWord = "Beater";
		case 18:	%secondWord = "Mesh";
		case 19:	%secondWord = "Armor";
	}
	
	// Return the two word name.
	return %firstWord @ %secondWord;
}

// Generates a random weapon image map for the squad member weapon type.
function generateRandomArmorImagemap (%squadMember, %dungeonLevel)
{//echo("generateRandomArmorImagemap()");
	%imageMap = 0;
	switch (%squadMember)
	{
		case 0:	// Leader.
			%randomArmor = 0;
			switch (%randomArmor)
			{
				case 0:	%imageMap = "Leader_South";//"Sprites:LeaderArmor";
			}
			
		case 1:	// Scout.
			%randomArmor = 0;
			switch (%randomArmor)
			{
				case 0:	%imageMap = "Leader_South";//"Sprites:ScoutArmor";
			}
			
		case 2:	// Grenadier.
			%randomArmor = 0;
			switch (%randomArmor)
			{
				case 0:	%imageMap = "Leader_South";//"Sprites:GrenadierArmor";
			}
			
		case 3:	// Support.
			%randomArmor = 0;
			switch (%randomArmor)
			{
				case 0:	%imageMap = "Leader_South";//"Sprites:SupportArmor";
			}
			
		case 4:	// Engineer.
			%randomArmor = 0;
			switch (%randomArmor)
			{
				case 0:	%imageMap = "Engineer_South";//"Sprites:EngineerArmor";
			}
	}
	return %imageMap;
}

// Creates a random armor.
function createRandomArmor (%squadMember, %dungeonLevel)
{//echo("createRandomArmor()");
	%armorCode = generateArmorCode();
	%armorCode = %armorCode @ "-";
	%armorCode = %armorCode @ generateRandomArmorName();
	%armorCode = %armorCode SPC %squadMember;
	
	// Armor Rating.
	%armorCode = %armorCode SPC generateRandomArmorRating(%squadMember, %dungeonLevel);
	// Speed.
	%armorCode = %armorCode SPC generateRandomSpeed(%squadMember, %dungeonLevel);
	// Image Map.
	%armorCode = %armorCode SPC generateRandomArmorImagemap(%squadMember, %dungeonLevel);
	// Repair.
	%armorCode = %armorCode SPC generateRandomRepair(%squadMember, %dungeonLevel);
	// Fire Resistance.
	%armorCode = %armorCode SPC generateRandomFireResistance(%squadMember, %dungeonLevel);
	// Frost Resistance.
	%armorCode = %armorCode SPC generateRandomFrostResistance(%squadMember, %dungeonLevel);
	// Acid Resistance.
	%armorCode = %armorCode SPC generateRandomAcidResistance(%squadMember, %dungeonLevel);
	// Explosive Resistance.
	%armorCode = %armorCode SPC generateRandomExplosiveResistance(%squadMember, %dungeonLevel);
	// Oxygen.
	%armorCode = %armorCode SPC generateRandomOxygen(%squadMember, %dungeonLevel);
	
	return %armorCode;
}

// Creates a new squad armor.
function createNewArmor (%armorType)
{//echo("createNewArmor()");
//echo("%armorType =" SPC %armorType);
	%name = getWord(%armorType, 0);
	%squadMember = getWord(%armorType, 1);
	%armorRating = getWord(%armorType, 2);
	%speed = getWord(%armorType, 3);
	%imageMap = getWord(%armorType, 4);
	%repair = getWord(%armorType, 5);
	%fireResistance = getWord(%armorType, 6);
	%frostResistance = getWord(%armorType, 7);
	%acidResistance = getWord(%armorType, 8);
	%explosiveResistance = getWord(%armorType, 9);
	%oxygen = getWord(%armorType, 10);
	
	switch (%squadMember)
	{
		case 0:	// Leader.
			%imageMap = "Leader_SouthImageMap";
			%imageMapName = "Leader_SouthImageMap";
			
		case 1:	// Scout.
			%imageMap = "Scout_SouthImageMap";
			%imageMapName = "Scout_SouthImageMap";
			
		case 2:	// Grenadier.
			%imageMap = "Grenadier_SouthImageMap";
			%imageMapName = "Grenadier_SouthImageMap";
			
		case 3:	// Support.
			%imageMap = "Support_SouthImageMap";
			%imageMapName = "Support_SouthImageMap";
			
		case 4:	// Engineer.
			%imageMap = "Engineer_SouthImageMap";
			%imageMapName = "Engineer_SouthImageMap";
	}
	
	%armor = new ScriptObject("armor")
	{
		itemType = -2;
		squadMember = %squadMember;
		
		armorType = %armorType;
		name = %name;
		imageMap = %imageMapName;
		imageMapName = %imageMap;
		
		armorRating = %armorRating;
		speed = %speed;
		repair = %repair;
		fireResistance = %fireResistance;
		frostResistance = %frostResistance;
		acidResistance = %acidResistance;
		explosiveResistance = %explosiveResistance;
		oxygen = %oxygen;
	};
	
	return %armor;
}