// Handles Weapon Swap GUI Controls.
// Worthless Bums 2012.


// Switches to the next valid squad member that can use the selected weapon.
function weaponSwapNextSquadMember ()
{//echo("weaponSwapNextSquadMember()");
	%weapon = $swapWeaponItem;
	// Form list of squad members who can use this weapon!
	if ($weaponSwapSquadList.getCount() > $swapWeaponSquadIndex + 1)
	{
		$swapWeaponSquadIndex++;
	}
	else
	{
		$swapWeaponSquadIndex = 0;
	}
	
	// Special flag so that hitting the main menu bind key (default "keyboard escape") does NOT break out of this dialog!
	$weaponSwapPushed = true;
	
	// Pause the game.
	$timescale = 0;
	
	// Do some initialization before pushing dialog.
	%weaponName = %weapon.name;
	%squadMemberName = ($weaponSwapSquadList.getObject($swapWeaponSquadIndex)).name;
	%weaponDamage = %weapon.damage;
	%weaponRange = %weapon.range;
	%weaponAccuracy = %weapon.accuracy;
	%weaponImageMap = %weapon.imageMapName;
	%currentWeapon = ($weaponSwapSquadList.getObject($swapWeaponSquadIndex)).weapon;
	
	// Font colors.
	%labelColor = 		"";//"\c6";
	%statColor =  		"";//"\c4";
	%ammoColor = 		"";//"\c3";
	%projectileColor =  "";//"\c7";
	
	// Weapon's Squad Member Name.
	squadMemberNameWeaponSwap.setText(%squadMemberName @ " (" @getCharacterClassName(($weaponSwapSquadList.getObject($swapWeaponSquadIndex)).characterClass) @ ")");
	// Weapon description text.
	newWeaponText.setText(%labelColor 		@ 	%weaponName 		NL
												%projectileColor @ "   (" @ getProjectileTypeName(%weapon.projectileType) @ ")"					NL
						  %statColor		@	"Damage: "			@	%weapon.damage	NL//(%weapon.damage - %weapon.damageRange) @ "-" @ (%weapon.damage + %weapon.damageRange)		NL
						  %statColor		@	"Range: "			@ 	%weapon.range		NL
						  %statColor		@	"Accuracy: "		@	%weapon.accuracy	NL
						  %ammoColor		@	"Ammo to Fire: "	@	%weapon.ammo		NL
						  %ammoColor		@	"Capacity: "		@	%weapon.capacity);
	currentWeaponText.setText(%labelColor 		@ 	%currentWeapon.name NL
							  %projectileColor 	@ 	"   (" @ getProjectileTypeName(%currentWeapon.projectileType) @ ")"														NL
							  %statColor		@	"Damage: "			@	%currentWeapon.damage	NL//(%currentWeapon.damage - %currentWeapon.damageRange) @ "-" @ (%currentWeapon.damage + %currentWeapon.damageRange)		NL
							  %statColor		@	"Range: "			@ 	%currentWeapon.range		NL
							  %statColor		@	"Accuracy: "		@	%currentWeapon.accuracy		NL
							  %ammoColor		@	"Ammo to Fire: "	@	%currentWeapon.ammo			NL
							  %ammoColor		@	"Capacity: "		@	%currentWeapon.capacity);
	// Weapon icons.
	newWeaponIcon.setBitmap(getWeaponBitmapFromImagemap(%weaponImageMap));
	currentWeaponIcon.setBitmap(getWeaponBitmapFromImagemap(%currentWeapon.imageMapName));
	
	// Push weapon swap dialog.
	Canvas.pushDialog(weaponSwapControl);
	
	return true;
}
// Converts an armor imagemap string to a bitmap path for weapons.
function getArmorBitmapFromImagemap (%imageMap)
{//echo("getArmorBitmapFromImagemap()");
//echo("%imageMap =" SPC %imageMap);
	%bitmap = strreplace(%imageMap, "ImageMap" , "");
	%bitmap = "../../../Sprites/1/assets/images/Squad Members/Armors/" @ %bitmap @ ".png";
	return %bitmap;
}
// Converts an weapon imagemap string to a bitmap path for weapons.
function getWeaponBitmapFromImagemap (%imageMap)
{//echo("getWeaponBitmapFromImagemap()");
//echo("%imageMap =" SPC %imageMap);
	%bitmap = strreplace(%imageMap, "ImageMap" , "");
	%bitmap = "../../../Sprites/1/assets/images/Weapons/" @ %bitmap @ ".png";
	return %bitmap;
}
// Pushes the weapon swap gui with the specified weapon object information.
function pushWeaponSwapGuiFromInventory (%weapon)
{//echo("pushWeaponSwapGuiFromInventory()");
	// Form list of squad members who can use this weapon!
	$weaponSwapSquadList = new SimSet();
	$swapWeaponSquadIndex = 0;
	$swapWeaponItem = %weapon;
	if ($leaderPersist	&&
		%weapon.squadMember == $playerSquad[0].characterClass)
	{
		$weaponSwapSquadList.add($playerSquad[0]);
	}
	if ($scoutPersist	&&
		%weapon.squadMember == $playerSquad[1].characterClass)
	{
		$weaponSwapSquadList.add($playerSquad[1]);
	}
	if ($grenadierPersist	&&
		%weapon.squadMember == $playerSquad[2].characterClass)
	{
		$weaponSwapSquadList.add($playerSquad[2]);
	}
	if ($supportPersist	&&
		%weapon.squadMember == $playerSquad[3].characterClass)
	{
		$weaponSwapSquadList.add($playerSquad[3]);
	}
	// Make sure there is at least one squad member who can use this weapon, otherwise leave!
	if ($weaponSwapSquadList.getCount() <= 0)
	{
		return false;
	}
	
	// Special flag so that hitting the main menu bind key (default "keyboard escape") does NOT break out of this dialog!
	$weaponSwapPushed = true;
	
	// Pause the game.
	$timescale = 0;
	
	// Do some initialization before pushing dialog.
	%weaponName = %weapon.name;
	%squadMemberName = ($weaponSwapSquadList.getObject($swapWeaponSquadIndex)).name;//%squadMemberName = $playerSquad[%weapon.squadMember].name;
	%weaponDamage = %weapon.damage;
	%weaponRange = %weapon.range;
	%weaponAccuracy = %weapon.accuracy;
	%weaponImageMap = %weapon.imageMapName;
	%currentWeapon = ($weaponSwapSquadList.getObject($swapWeaponSquadIndex)).weapon;//%currentWeapon = $playerSquad[%weapon.squadMember].weapon;
	
	// Font colors.
	%labelColor = 		"";//"\c6";
	%statColor =  		"";//"\c4";
	%ammoColor = 		"";//"\c3";
	%projectileColor =  "";//"\c7";
	
	// Weapon's Squad Member Name.
	squadMemberNameWeaponSwap.setText(%squadMemberName @ " (" @getCharacterClassName(($weaponSwapSquadList.getObject($swapWeaponSquadIndex)).characterClass) @ ")");
	// Weapon description text.
	newWeaponText.setText(%labelColor 		@ 	%weaponName 		NL
						  %projectileColor 	@ 	"   (" @ getProjectileTypeName(%weapon.projectileType) @ ")"					NL
						  %statColor		@	"Damage: "			@	%weapon.damage	NL//(%weapon.damage - %weapon.damageRange) @ "-" @ (%weapon.damage + %weapon.damageRange)		NL
						  %statColor		@	"Range: "			@ 	%weapon.range		NL
						  %statColor		@	"Accuracy: "		@	%weapon.accuracy	NL
						  %ammoColor		@	"Ammo to Fire: "	@	%weapon.ammo		NL
						  %ammoColor		@	"Capacity: "		@	%weapon.capacity);
	currentWeaponText.setText(%labelColor 		@ 	%currentWeapon.name NL
							  %projectileColor 	@ 	"   (" @ getProjectileTypeName(%currentWeapon.projectileType) @ ")"														NL
							  %statColor		@	"Damage: "			@	%currentWeapon.damage	NL//(%currentWeapon.damage - %currentWeapon.damageRange) @ "-" @ (%currentWeapon.damage + %currentWeapon.damageRange)		NL
							  %statColor		@	"Range: "			@ 	%currentWeapon.range		NL
							  %statColor		@	"Accuracy: "		@	%currentWeapon.accuracy		NL
							  %ammoColor		@	"Ammo to Fire: "	@	%currentWeapon.ammo			NL
							  %ammoColor		@	"Capacity: "		@	%currentWeapon.capacity);
	// Weapon icons.
	newWeaponIcon.setBitmap(getWeaponBitmapFromImagemap(%weaponImageMap));
	currentWeaponIcon.setBitmap(getWeaponBitmapFromImagemap(%currentWeapon.imageMapName));
	
	// Push weapon swap dialog.
	Canvas.pushDialog(weaponSwapControl);
	
	return true;
}
// Pushes the weapon swap gui with the specified weapon code information.
function pushWeaponSwapGui (%weaponCode)
{//echo("pushWeaponSwapGui()");
	// Special flag so that hitting the main menu bind key (default "keyboard escape") does NOT break out of this dialog!
	$weaponSwapPushed = true;
	$currentWeaponCode = %weaponCode;
	
	// Pause the game.
	$timescale = 0;
	
	// Do some initialization before pushing dialog.
	%weaponName = getWord(%weaponCode, 0);
	%squadMemberName = $playerSquad[getWord(%weaponCode, 1)].name;
	%weaponDamage = getWord(%weaponCode, 2);
	%weaponRange = getWord(%weaponCode, 3);
	%weaponAccuracy = getWord(%weaponCode, 4);
	%weaponImageMap = getWord(%weaponCode, 5);
	%currentWeapon = $playerSquad[getWord(%weaponCode, 1)].weapon;
	
	// Weapon's Squad Member Name.
	squadMemberNameWeaponSwap.setText(%squadMemberName @ " (" @getCharacterClassName($playerSquad[getWord(%weaponCode, 1)].characterClass) @ ")");
	// Weapon description text.
	newWeaponText.setText(%weaponName					NL
						  %weaponDamage SPC "Damage" 	NL
						  %weaponRange SPC "Range" 		NL
						  %weaponAccuracy SPC "Accuracy");
	currentWeaponText.setText(%currentWeapon.name					NL
							  %currentWeapon.damage SPC "Damage" 	NL
							  %currentWeapon.range SPC "Range" 		NL
							  %currentWeapon.accuracy SPC "Accuracy");
	// Weapon icons.
	newWeaponIcon.setBitmap(getWeaponBitmapFromImagemap(%weaponImageMap));
	currentWeaponIcon.setBitmap(getWeaponBitmapFromImagemap(%currentWeapon.imageMapName));
	
	// Push weapon swap dialog.
	Canvas.pushDialog(weaponSwapControl);
}
// Clear weapon swap data.
function clearWeaponSwapDialog ()
{//echo("clearWeaponSwapDialog()");
	$weaponSwapPushed = false;
	// Weapon's Squad Member Name.
	squadMemberNameWeaponSwap.setText("");
	// Weapon description text.
	newWeaponText.setText("");
	currentWeaponText.setText("");
	// Weapon icons.
	newWeaponIcon.setBitmap("");
	currentWeaponIcon.setBitmap("");
	// Clear weapon code.
	$currentWeaponCode = "";
}
// Keep Weapon Button clicked.
function keepWeaponButton ()
{//echo("keepWeaponButton()");
	// Play weapon swap sound effect.
	playAudio("Audio:weaponSwap", EffectsAudioVolume.value);
	// Unflag weapon swap dialog.
	$weaponSwapPushed = false;
	// Nothing happens to gear so just unpause the game...
	$timescale = 1;
	// ...and pop the weapon swap dialog.
	Canvas.popDialog(weaponSwapControl);
}
// Swap Weapon Button clicked.
function swapWeaponFromInventoryButton ()
{//echo("swapWeaponFromInventoryButton()");
	// Play weapon swap sound effect.
	playAudio("Audio:weaponSwap", EffectsAudioVolume.value);
	
	// Get the squad member index this weapon is for.
	%characterIndex = -1;
	if ($leaderPersist	&&
		$playerSquad[0] == $weaponSwapSquadList.getObject($swapWeaponSquadIndex))
	{
		%characterIndex = 0;
	}
	if ($scoutPersist	&&
		$playerSquad[1] == $weaponSwapSquadList.getObject($swapWeaponSquadIndex))
	{
		%characterIndex = 1;
	}
	if ($grenadierPersist	&&
		$playerSquad[2] == $weaponSwapSquadList.getObject($swapWeaponSquadIndex))
	{
		%characterIndex = 2;
	}
	if ($supportPersist	&&
		$playerSquad[3] == $weaponSwapSquadList.getObject($swapWeaponSquadIndex))
	{
		%characterIndex = 3;
	}
	
	// Save the currently equipped weapon.
	%oldWeapon = $playerSquad[%characterIndex].weapon;
	// Assign the new weapon to the character.
	%currentWeapon = $currentWeaponCode;
	$playerSquad[%characterIndex].weapon = %currentWeapon;
	// Remove the newly equipped weapon from the inventory.
	%weaponToRemove = $squadInventory.getObject($currentItemSlot);
	$squadInventory.remove(%weaponToRemove);
	%weaponToRemove.safeDelete();
	// Place the old weapon in the inventory.
	$squadInventory.add(%oldWeapon);
	
	// Mount weapon to squad member.
	mountWeaponToCharacter(%currentWeapon, %characterIndex);
	
	// Unflag weapon swap dialog.
	$weaponSwapPushed = false;
	// Pop the weapon swap dialog.
	Canvas.popDialog(weaponSwapControl);
	// Unpause game.
	$timescale = 1;
	// Clear the weapon swap dialog.
	clearWeaponSwapDialog();
	// Update squad info gui.
	updateSquadInfoGui();
	// Update inventory tab information.
	updateInventoryTabInformation();
	
	// Update facing sprite!
	setSquadMemberFacing($playerSquad[$activeCharacter].facing, $playerSquad[$activeCharacter]);

	// Check to trigger Laser Squad achievement!
	checkLaserSquadAchievement();

	// Call to monster AI code.
	scheduleMonsterTurn();
}
// Keep Weapon Button clicked.
function keepWeaponFromInventoryButton ()
{//echo("keepWeaponFromInventoryButton()");
	// Play weapon swap sound effect.
	playAudio("Audio:weaponSwap", EffectsAudioVolume.value);
	// Unflag weapon swap dialog.
	$weaponSwapPushed = false;
	// Nothing happens to gear so just unpause the game...
	$timescale = 1;
	// ...and pop the weapon swap dialog.
	Canvas.popDialog(weaponSwapControl);
	// Update inventory tab information.
	updateInventoryTabInformation();
}
function checkLaserSquadAchievement ()
{//echo("checkLaserSquadAchievement()");
	if ($leaderPersist 		&& isObject($playerSquad[0])	&&
		$scoutPersist 		&& isObject($playerSquad[1])	&&
		$grenadierPersist 	&& isObject($playerSquad[2])	&&
		$supportPersist 	&& isObject($playerSquad[3]))
	{
		if ($playerSquad[0].weapon.projectileType == $weaponProjectileLaserType	&&
			$playerSquad[1].weapon.projectileType == $weaponProjectileLaserType	&&
			$playerSquad[2].weapon.projectileType == $weaponProjectileLaserType	&&
			$playerSquad[3].weapon.projectileType == $weaponProjectileLaserType)
		{
			triggerLaserSquad();
		}
	}
}