// Weapon Mod Handler.
// Worthless Bums 2012.


function orientSquadMemberWeaponAttachments ()
{//echo("orientSquadMemberWeaponAttachments()");
	if ($leaderPersist)
	{
		mountSightToUnit($playerSquad[0]);
	}
	if ($scoutPersist)
	{
		mountSightToUnit($playerSquad[1]);
	}
	if ($grenadierPersist)
	{
		mountSightToUnit($playerSquad[2]);
	}
	if ($supportPersist)
	{
		mountSightToUnit($playerSquad[3]);
	}
}
function createLaserSprite ()
{//echo("createLaserSprite()");
	%laserSprite = new Sprite("Laser")
	{
		SceneLayer = $WeaponAttachmentLayer;
		position = "4000 4000";
		scenegraph = levelSceneGraph;
		image = "Sprites:Laser";
		visible = true;
		size = "4 4";
	};
	addObjectToScene(%laserSprite);
	return %laserSprite;
}

// NOTE: 0 = Laser, 1 = Flashlight.
function setLoadGameWeaponAttachmentUI ()
{
	if ($LeaderWeaponAttachment)
	{
		LeaderFirearmReadoutFlashlight.visible = false;
		LeaderFirearmReadoutLaser.visible = true;
	}
	else
	{
		LeaderFirearmReadoutFlashlight.visible = true;
		LeaderFirearmReadoutLaser.visible = false;
	}
	if ($ScoutWeaponAttachment)
	{
		ScoutFirearmReadoutFlashlight.visible = false;
		ScoutFirearmReadoutLaser.visible = true;
	}
	else
	{
		ScoutFirearmReadoutFlashlight.visible = true;
		ScoutFirearmReadoutLaser.visible = false;
	}
	if ($grenadierWeaponAttachment)
	{
		GrenadierFirearmReadoutFlashlight.visible = false;
		GrenadierFirearmReadoutLaser.visible = true;
	}
	else
	{
		GrenadierFirearmReadoutFlashlight.visible = true;
		GrenadierFirearmReadoutLaser.visible = false;
	}
	if ($SupportWeaponAttachment)
	{
		SupportFirearmReadoutFlashlight.visible = false;
		SupportFirearmReadoutLaser.visible = true;
	}
	else
	{
		SupportFirearmReadoutFlashlight.visible = true;
		SupportFirearmReadoutLaser.visible = false;
	}
}

// Toggle sight to unit.
function toggleSightToUnit (%unit)
{//echo("toggleSightToUnit()");echo("%unit.perks =" SPC %unit.perks);
	if (%unit.perks !$= "Sweeper")	// "Sweeper" perk grants free weapon cycling!
	{
		%actionPointsCost = getActionPointCost(13);
		if (%actionPointsCost > %unit.currentActionPoints)
		{
			updateEventText($whiteColor @ "Not enough AP to cycle weapon attachment!" @ $defaultColor @ "!");
			return;
		}
		%unit.currentActionPoints -= %actionPointsCost;
		// Sanity check.
		if (%unit.currentActionPoints < 0)
		{
			%unit.currentActionPoints = 0;
		}
	}
	if (%unit == $playerSquad[0])
	{
		%characterIndex = 0;
	}
	else if (%unit == $playerSquad[1])
	{
		%characterIndex = 1;
	}
	else if (%unit == $playerSquad[2])
	{
		%characterIndex = 2;
	}
	else if (%unit == $playerSquad[3])
	{
		%characterIndex = 3;
	}
	if ((%unit.laserSprite).image $= "Sprites:Flashlight")
	{
		mountLaserToUnit(%unit);
		updateEventText($itemColor @ "Laser" @ $defaultColor @ " grants " @ $laserSightAccuracyBonus @ "% accuracy" @ $defaultColor @ "!");
		switch ($playerSquad[%characterIndex].characterClass)
		{
			case 0:
				$leaderWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $leaderWeaponAttachment;
				LeaderFirearmReadoutFlashlight.visible = false;
				LeaderFirearmReadoutLaser.visible = true;
			case 1:
				$scoutWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $scoutWeaponAttachment;
				ScoutFirearmReadoutFlashlight.visible = false;
				ScoutFirearmReadoutLaser.visible = true;
			case 2:
				$grenadierWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $grenadierWeaponAttachment;
				GrenadierFirearmReadoutFlashlight.visible = false;
				GrenadierFirearmReadoutLaser.visible = true;
			case 3:
				$supportWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $supportWeaponAttachment;
				SupportFirearmReadoutFlashlight.visible = false;
				SupportFirearmReadoutLaser.visible = true;
			case 4:
				$engineerWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $engineerWeaponAttachment;
				EngineerFirearmReadoutFlashlight.visible = false;
				EngineerFirearmReadoutLaser.visible = true;
		}
		// Laser dialogue.
		setIndividualActionPopup($playerSquad[$activeCharacter], "Laser grants +" @ $laserSightAccuracyBonus @ "% accuracy", "dialogue");
	}
	else
	{
		mountFlashlightToUnit(%unit);
		updateEventText($itemColor @ "Flashlight" @ $defaultColor @ " grants +1 sight range" @ $defaultColor @ "!");
		switch ($playerSquad[%characterIndex].characterClass)
		{
			case 0:
				$leaderWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $leaderWeaponAttachment;
				LeaderFirearmReadoutFlashlight.visible = true;
				LeaderFirearmReadoutLaser.visible = false;
			case 1:
				$scoutWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $scoutWeaponAttachment;
				ScoutFirearmReadoutFlashlight.visible = true;
				ScoutFirearmReadoutLaser.visible = false;
			case 2:
				$grenadierWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $grenadierWeaponAttachment;
				GrenadierFirearmReadoutFlashlight.visible = true;
				GrenadierFirearmReadoutLaser.visible = false;
			case 3:
				$supportWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $supportWeaponAttachment;
				SupportFirearmReadoutFlashlight.visible = true;
				SupportFirearmReadoutLaser.visible = false;
			case 4:
				$engineerWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $engineerWeaponAttachment;
				EngineerFirearmReadoutFlashlight.visible = true;
				EngineerFirearmReadoutLaser.visible = false;
		}
		// Flashlight dialogue.
		setIndividualActionPopup($playerSquad[$activeCharacter], "Flashlight grants +1 sight range", "dialogue");
	}
	$fogOfWar.showFogOfWar();
	initializeReadout();
	scheduleMonsterTurn();
}
function mountSightToUnit (%unit)
{//echo("mountSightToUnit()");
	if (%unit == $playerSquad[0])
	{
		%characterIndex = 0;
	}
	else if (%unit == $playerSquad[1])
	{
		%characterIndex = 1;
	}
	else if (%unit == $playerSquad[2])
	{
		%characterIndex = 2;
	}
	else if (%unit == $playerSquad[3])
	{
		%characterIndex = 3;
	}
	switch ($playerSquad[%characterIndex].characterClass)
	{
		case 0:	%weaponAttachFlag = $leaderWeaponAttachment;
		case 1:	%weaponAttachFlag = $scoutWeaponAttachment;
		case 2:	%weaponAttachFlag = $grenadierWeaponAttachment;
		case 3:	%weaponAttachFlag = $supportWeaponAttachment;
		case 4:	%weaponAttachFlag = $engineerWeaponAttachment;
	}
	%attachmentSprite = null;
	if (%weaponAttachFlag == 0)
	{
		%attachmentSprite = mountFlashlightToUnit(%unit);
		switch ($playerSquad[%characterIndex].characterClass)
		{
			case 0:
				$leaderWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $leaderWeaponAttachment;
			case 1:
				$scoutWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $scoutWeaponAttachment;
			case 2:
				$grenadierWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $grenadierWeaponAttachment;
			case 3:
				$supportWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $supportWeaponAttachment;
			case 4:
				$engineerWeaponAttachment = 0;
				$playerSquad[%characterIndex].weaponAttachment = $engineerWeaponAttachment;
		}
	}
	else
	{
		%attachmentSprite = mountLaserToUnit(%unit);
		switch ($playerSquad[%characterIndex].characterClass)
		{
			case 0:
				$leaderWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $leaderWeaponAttachment;
			case 1:
				$scoutWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $scoutWeaponAttachment;
			case 2:
				$grenadierWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $grenadierWeaponAttachment;
			case 3:
				$supportWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $supportWeaponAttachment;
			case 4:
				$engineerWeaponAttachment = 1;
				$playerSquad[%characterIndex].weaponAttachment = $engineerWeaponAttachment;
		}
	}
	if (isObject($playerSquad[%characterIndex].weaponAttachmentSprite))
	{
		$playerSquad[%characterIndex].weaponAttachmentSprite.safeDelete();
	}
	$playerSquad[%characterIndex].weaponAttachmentSprite = %attachmentSprite;
}
function createFlashlightSprite ()
{//echo("createFlashlightSprite()");
	%flashlightSprite = new Sprite("Flashlight")
	{
		SceneLayer = $WeaponAttachmentLayer;
		position = "4000 4000";
		scenegraph = levelSceneGraph;
		image = "Sprites:Flashlight";
		visible = true;
		size = "8 6";
	};
	addObjectToScene(%flashlightSprite);
	return %flashlightSprite;
}
function mountLaserToUnit (%unit)
{//echo("mountLaserToUnit()");
	if (isObject(%unit.laserSprite))
	{
		(%unit.laserSprite).safeDelete();
	}
	%laserSprite = createLaserSprite();//createLaserAnimation();
	%xOffset = 0;
	%yOffset = 0;
	switch (%unit.facing)
	{
		case $northDirection:
			%laserSprite.angle = -90;
			%yOffset = -4;
		
		case $southDirection:
			%laserSprite.angle = 90;
			%yOffset = 4;
		
		case $eastDirection:
			%laserSprite.angle = 0;
			%xOffset = 4;
		
		case $westDirection:
			%laserSprite.angle = 180;
			%xOffset = -4;
	}
	%laserSprite.position = getWord(%unit.position, 0) + %xOffset SPC getWord(%unit.position, 1) + %yOffset;
	//%mountForce = 50;
	//%laserSprite.mount(%unit, %xOffset, %yOffset, %mountForce, true, true, true, false);
	%unit.laserSprite = %laserSprite;
}
function mountFlashlightToUnit (%unit)
{//echo("mountFlashlightToUnit()");
	if (isObject(%unit.laserSprite))
	{
		(%unit.laserSprite).safeDelete();
	}
	%laserSprite = createFlashlightSprite();
	%xOffset = 0;
	%yOffset = 0;
	switch (%unit.facing)
	{
		case $northDirection:
			%laserSprite.angle = -90;
			%yOffset = -4;
		
		case $southDirection:
			%laserSprite.angle = 90;
			%yOffset = 4;
		
		case $eastDirection:
			%laserSprite.angle = 0;
			%xOffset = 4;
		
		case $westDirection:
			%laserSprite.angle = 180;
			%xOffset = -4;
	}
	%laserSprite.position = getWord(%unit.position, 0) + %xOffset SPC getWord(%unit.position, 1) + %yOffset;
	%unit.laserSprite = %laserSprite;
}
function updateWeaponAttachmentPosition (%unit)
{//echo("updateWeaponAttachmentPosition()");
	%xOffset = 0;
	%yOffset = 0;
	switch (%unit.facing)
	{
		case $northDirection:
			%laserSprite.angle = -90;
			%yOffset = -4;
		
		case $southDirection:
			%laserSprite.angle = 90;
			%yOffset = 4;
		
		case $eastDirection:
			%laserSprite.angle = 0;
			%xOffset = 4;
		
		case $westDirection:
			%laserSprite.angle = 180;
			%xOffset = -4;
	}
	%unit.laserSprite.position = getWord(%unit.position, 0) + %xOffset SPC getWord(%unit.position, 1) + %yOffset;
}