// Guard Mode Handler.
// Worthless Bums 2012.


// Test function that toggles Guard Mode on for all squad members.
function tgmo ()
{//echo("tgmo()");
	if ($leaderPersist)
	{
		turnOnGuardMode(0);
	}
	if ($scoutPersist)
	{
		turnOnGuardMode(1);
	}
	if ($grenadierPersist)
	{
		turnOnGuardMode(2);
	}
	if ($supportPersist)
	{
		turnOnGuardMode(3);
	}
}


// Returns true iff any member of the player squad is in Guard Mode.
function anyUnitInGuardMode ()
{//echo("anyUnitInGuardMode()");
	if ($leaderPersist	&&
		isObject($playerSquad[0]))
	{
		if ($playerSquad[0].guardMode)
		{
			return true;
		}
	}
	if ($scoutPersist	&&
		isObject($playerSquad[1]))
	{
		if ($playerSquad[1].guardMode)
		{
			return true;
		}
	}
	if ($grenadierPersist	&&
		isObject($playerSquad[2]))
	{
		if ($playerSquad[2].guardMode)
		{
			return true;
		}
	}
	if ($supportPersist	&&
		isObject($playerSquad[3]))
	{
		if ($playerSquad[3].guardMode)
		{
			return true;
		}
	}
	
	return false;
}

function clearSquadMemberLabels ()
{
	if ($leaderPersist	&&
		isObject($playerSquad[0]))
	{
		clearUnitLabel($playerSquad[0]);
	}
	if ($scoutPersist	&&
		isObject($playerSquad[1]))
	{
		clearUnitLabel($playerSquad[1]);
	}
	if ($grenadierPersist	&&
		isObject($playerSquad[2]))
	{
		clearUnitLabel($playerSquad[2]);
	}
	if ($supportPersist	&&
		isObject($playerSquad[3]))
	{
		clearUnitLabel($playerSquad[3]);
	}
}
function clearUnitLabel (%target)
{
	// Delete background text.
	if (isObject(%target.dialogueBackgroundPopup))
	{
		(%target.dialogueBackgroundPopup).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup2))
	{
		(%target.dialogueBackgroundPopup2).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup3))
	{
		(%target.dialogueBackgroundPopup3).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup4))
	{
		(%target.dialogueBackgroundPopup4).safeDelete();
	}
	// Delete main text.
	if (isObject(%target.dialoguePopup))
	{
		(%target.dialoguePopup).safeDelete();
	}
}
function setUnitLabel (%target, %text, %textType)
{//echo("setUnitLabel()");
//return;		// Uncomment for trailer footage!
	%xPosition = getWord(%target.getPosition(), 0);
	%yPosition = getWord(%target.getPosition(), 1);
	//%text = %target.name @ ": " @ %text;
	if (isObject(%target.dialoguePopup))
	{
		(%target.dialoguePopup).safedelete();
	}
	
	%verticalPadding = 6;
	%moveSpeed = 12.0;
	%lifetime = 4.0;
	%foregroundSceneLayer = 0;
	%foregroundTextColor = "255 255 255";
	%backgroundSceneLayer = 0;
	%backgroundTextColor = "0 0 255";
	// Delete background text.
	if (isObject(%target.dialogueBackgroundPopup))
	{
		(%target.dialogueBackgroundPopup).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup2))
	{
		(%target.dialogueBackgroundPopup2).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup3))
	{
		(%target.dialogueBackgroundPopup3).safeDelete();
	}
	if (isObject(%target.dialogueBackgroundPopup4))
	{
		(%target.dialogueBackgroundPopup4).safeDelete();
	}
	// Up.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText = new ImageFont();
			%backgroundFloatingText.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText.Image = $RenderedFontImage;
			%backgroundFloatingText.FontSize = $RenderedFontSize;
			%backgroundFloatingText.BlendColor = %backgroundTextColor;
			%backgroundFloatingText.Text = %text;
			%backgroundFloatingText.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0 - $RenderedFontOutlinePadding;
			addObjectToScene(%backgroundFloatingText);
			
			%target.dialogueBackgroundPopup = %backgroundFloatingText;
	}
	//%backgroundFloatingText.moveTo(%xPosition, %yPosition - 3.1 + %verticalPadding, %moveSpeed, true, false);
	// Down.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText2 = new ImageFont();
			%backgroundFloatingText2.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText2.Image = $RenderedFontImage;
			%backgroundFloatingText2.FontSize = $RenderedFontSize;
			%backgroundFloatingText2.BlendColor = %backgroundTextColor;
			%backgroundFloatingText2.Text = %text;
			%backgroundFloatingText2.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0 + $RenderedFontOutlinePadding;
			addObjectToScene(%backgroundFloatingText2);
			%target.dialogueBackgroundPopup2 = %backgroundFloatingText2;
	}
	//%backgroundFloatingText2.moveTo(%xPosition, %yPosition - 2.9 + %verticalPadding, %moveSpeed, true, false);
	//%backgroundFloatingText2.setLifetime(%lifetime);
	// Left.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText3 = new ImageFont();
			%backgroundFloatingText3.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText3.Image = $RenderedFontImage;
			%backgroundFloatingText3.FontSize = $RenderedFontSize;
			%backgroundFloatingText3.BlendColor = %backgroundTextColor;
			%backgroundFloatingText3.Text = %text;
			%backgroundFloatingText3.Position = getWord(%target.position, 0) - $RenderedFontOutlinePadding SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%backgroundFloatingText3);
			%target.dialogueBackgroundPopup3 = %backgroundFloatingText3;
	}
	//%backgroundFloatingText3.moveTo(%xPosition - 0.1, %yPosition - 3.0 + %verticalPadding, %moveSpeed, true, false);
	//%backgroundFloatingText3.setLifetime(%lifetime);
	// Right.
	switch$ (%textType)
	{
		case "dialogue":
			%backgroundFloatingText4 = new ImageFont();
			%backgroundFloatingText4.SceneLayer = $RenderedFontSceneLayer;
			%backgroundFloatingText4.Image = $RenderedFontImage;
			%backgroundFloatingText4.FontSize = $RenderedFontSize;
			%backgroundFloatingText4.BlendColor = %backgroundTextColor;
			%backgroundFloatingText4.Text = %text;
			%backgroundFloatingText4.Position = getWord(%target.position, 0) + $RenderedFontOutlinePadding SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%backgroundFloatingText4);
			%target.dialogueBackgroundPopup4 = %backgroundFloatingText4;
	}
	//%backgroundFloatingText4.moveTo(%xPosition + 0.1, %yPosition - 3.0 + %verticalPadding, %moveSpeed, true, false);
	
	switch$ (%textType)
	{
		case "dialogue":
			%floatingText = new ImageFont();
			%floatingText.SceneLayer = $RenderedFontSceneLayer;
			%floatingText.Image = $RenderedFontImage;
			%floatingText.FontSize = $RenderedFontSize;
			%floatingText.BlendColor = %foregroundTextColor;
			%floatingText.Text = %text;
			%floatingText.Position = getWord(%target.position, 0) SPC getWord(%target.position, 1) + 2.0;
			addObjectToScene(%floatingText);
			%target.dialoguePopup = %floatingText;
	}
	//%floatingText.moveTo(%xPosition, %yPosition - 3 + %verticalPadding, %moveSpeed, true, false);
}

// Turn on Guard Mode for the specified character.
function turnOnGuardMode (%characterIndex)
{//echo("turnOnGuardMode()");
	if ($playerSquad[%characterIndex].currentActionPoints > 0)
	{
		$GlobalStats.totalGuardModes++;
		$playerSquad[%characterIndex].guardMode = 1 + $playerSquad[%characterIndex].power;//$playerSquad[%characterIndex].currentActionPoints;
		setCurrentActionPoints(%characterIndex, 0);
		createGuardModeIndicator(%characterIndex);
		updateEventText($squadColor @ $playerSquad[$activeCharacter].name @ $defaultColor @ " enters " @ $squadColor @ "Guard Mode" @ $defaultColor @ ".");
		initializeReadout();
		scheduleMonsterTurn();
		setUnitLabel($playerSquad[%characterIndex], "Guarding", "dialogue");
		// "Bombast": Whenever a non-Grenadier enters Guard Mode all Grenadiers gain +1 AP!
		/*if ($playerSquad[%characterIndex].characterClass != 2)
		{
			%bombastAP = 1;
			if ($leaderPersist	&&
				$playerSquad[0].characterClass == 2)
			{
				$playerSquad[0].currentActionPoints += %bombastAP;
			}
			if ($scoutPersist	&&
				$playerSquad[1].characterClass == 2)
			{
				$playerSquad[1].currentActionPoints += %bombastAP;
			}
			if ($grenadierPersist	&&
				$playerSquad[2].characterClass == 2)
			{
				$playerSquad[2].currentActionPoints += %bombastAP;
			}
			if ($supportPersist	&&
				$playerSquad[3].characterClass == 2)
			{
				$playerSquad[3].currentActionPoints += %bombastAP;
			}
			updateSquadMemberFloatingText();
		}*/
	}
	else
	{
		updateEventText($squadColor @ $playerSquad[$activeCharacter].name @ $defaultColor @ " needs AP to enter " @ $squadColor @ "Guard Mode" @ $defaultColor @ "!");
	}
}

// Updates all Guard Mode Animations.
function updateGuardModeAnimations ()
{//echo("updateGuardModeAnimations()");
	clearGuardModeAnimations();
	if ($leaderPersist	&&
		$playerSquad[0].guardMode)
	{
		createGuardModeIndicator(0);
		setUnitLabel($playerSquad[0], "Guarding", "dialogue");
	}
	if ($scoutPersist	&&
		$playerSquad[1].guardMode)
	{
		createGuardModeIndicator(1);
		setUnitLabel($playerSquad[0], "Guarding", "dialogue");
	}
	if ($grenadierPersist	&&
		$playerSquad[2].guardMode)
	{
		createGuardModeIndicator(2);
		setUnitLabel($playerSquad[0], "Guarding", "dialogue");
	}
	if ($supportPersist	&&
		$playerSquad[3].guardMode)
	{
		createGuardModeIndicator(3);
		setUnitLabel($playerSquad[0], "Guarding", "dialogue");
	}
}

// Clears Guard Mode Animations.
function clearGuardModeAnimations ()
{//echo("clearGuardModeAnimations()");
	for (%i = 0; %i < $GuardModeAnimationList.getCount(); %i++)
	{
		// Grab guard mode animation.
		%gma = $GuardModeAnimationList.getObject(%i);
		// Delete animation.
		%gma.safeDelete();
	}
	$GuardModeAnimationList = new SimSet();
	//resetSquadGuardModes();
}

// Reset squad guard modes.
function resetSquadGuardModes ()
{//echo("resetSquadGuardModes()");
	if ($leaderPersist)
	{
		$playerSquad[0].guardMode = 0;
	}
	if ($scoutPersist)
	{
		$playerSquad[1].guardMode = 0;
	}
	if ($grenadierPersist)
	{
		$playerSquad[2].guardMode = 0;
	}
	if ($supportPersist)
	{
		$playerSquad[3].guardMode = 0;
	}
}

// Returns true iff the specified squad member has a valid target in range.
function squadMemberHasTarget (%characterIndex, %monster)
{//echo("squadMemberHasTarget()");echo("%characterIndex =" SPC %characterIndex);
	// Grab weapon range.
	%range = ($playerSquad[%characterIndex].weapon).range;
	if (isLargeUnit($playerSquad[%characterIndex]))
	{
		%range = $HulkFlamethrowerRange;
	}
	
	// Supports with Sentry always hit with Guard Mode shots!
	if ($playerSquad[%characterIndex].characterClass == 3	&&	// Support class.
		getWord($playerSquad[%characterIndex].talents, 5) == 1)
	{//echo("support has sentry!");
		$GuaranteedGuardModeHit = true;
	}
	
	// Switch by character facing.
	switch ($playerSquad[%characterIndex].facing)
	{
		case $northDirection:
			for (%i = 1; %i <= %range; %i++)
			{
				if (!canFireThrough($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex - %i))
				{//echo("GM North obstacle detected!");
					return false;
				}
				if (!tileClearOfMonsters($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex - %i))
				{//echo("GM fire North!");
					if (isLargeUnit($playerSquad[%characterIndex]))
					{
						useHulkRangedWeapon(%characterIndex, true);
					}
					else
					{
						characterFire(%characterIndex);
					}
					$playerSquad[%characterIndex].guardMode--;
					return true;
				}
			}
			
		case $southDirection:
			for (%i = 1; %i <= %range; %i++)
			{
				if (!canFireThrough($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex + %i))
				{//echo("GM South obstacle detected!");
					return false;
				}
				if (!tileClearOfMonsters($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex + %i))
				{//echo("GM fire South!");
					if (isLargeUnit($playerSquad[%characterIndex]))
					{
						useHulkRangedWeapon(%characterIndex, true);
					}
					else
					{
						characterFire(%characterIndex);
					}
					$playerSquad[%characterIndex].guardMode--;
					return true;
				}
			}
			
		case $eastDirection:
			for (%i = 1; %i <= %range; %i++)
			{
				if (!canFireThrough($playerSquad[%characterIndex].xIndex + %i, $playerSquad[%characterIndex].yIndex))
				{//echo("GM East obstacle detected!");
					return false;
				}
				if (!tileClearOfMonsters($playerSquad[%characterIndex].xIndex + %i, $playerSquad[%characterIndex].yIndex))
				{//echo("GM fire East!");
					if (isLargeUnit($playerSquad[%characterIndex]))
					{
						useHulkRangedWeapon(%characterIndex, true);
					}
					else
					{
						characterFire(%characterIndex);
					}
					$playerSquad[%characterIndex].guardMode--;
					return true;
				}
			}
			
		case $westDirection:
			for (%i = 1; %i <= %range; %i++)
			{
				if (!canFireThrough($playerSquad[%characterIndex].xIndex - %i, $playerSquad[%characterIndex].yIndex))
				{//echo("GM West obstacle detected!");
					return false;
				}
				if (!tileClearOfMonsters($playerSquad[%characterIndex].xIndex - %i, $playerSquad[%characterIndex].yIndex))
				{//echo("GM fire West!");
					if (isLargeUnit($playerSquad[%characterIndex]))
					{
						useHulkRangedWeapon(%characterIndex, true);
					}
					else
					{
						characterFire(%characterIndex);
					}
					$playerSquad[%characterIndex].guardMode--;
					return true;
				}
			}
	}
	return false;
}

// Executes Guard Mode actions for the specified squad member!
function executeGuardModeActions (%monster)
{//echo("executeGuardModeActions()");echo("$guardModeCharacter =" SPC $guardModeCharacter);echo("$playerSquad[0].guardMode =" SPC $playerSquad[0].guardMode);
	if ($leaderPersist	&&
		$playerSquad[0].guardMode > 0)
	{
		// Ammo check here!
		if (characterHasRequiredAmmo(0, ($playerSquad[0].weapon).ammo))
		{//echo("$guardModeCharacter =" SPC $guardModeCharacter);
			$guardModeCharacter = 0;
			squadMemberHasTarget(0, %monster);
		}
		else
		{
			setNoAmmoDialogue($playerSquad[0]);
		}
	}
	else
	{
		clearUnitLabel($playerSquad[0]);
	}
	if ($scoutPersist	&&
		$playerSquad[1].guardMode > 0)
	{
		// Ammo check here!
		if (characterHasRequiredAmmo(1, ($playerSquad[1].weapon).ammo))
		{//echo("$guardModeCharacter =" SPC $guardModeCharacter);
			$guardModeCharacter = 1;
			squadMemberHasTarget(1, %monster);
		}
		else
		{
			setNoAmmoDialogue($playerSquad[1]);
		}
	}
	else
	{
		clearUnitLabel($playerSquad[1]);
	}
	if ($grenadierPersist	&&
		$playerSquad[2].guardMode > 0)
	{
		// Ammo check here!
		if (characterHasRequiredAmmo(2, ($playerSquad[2].weapon).ammo))
		{//echo("$guardModeCharacter =" SPC $guardModeCharacter);
			$guardModeCharacter = 2;
			squadMemberHasTarget(2, %monster);
		}
		else
		{
			setNoAmmoDialogue($playerSquad[2]);
		}
	}
	else
	{
		clearUnitLabel($playerSquad[2]);
	}
	if ($supportPersist	&&
		$playerSquad[3].guardMode > 0)
	{
		// Ammo check here!
		if (characterHasRequiredAmmo(3, ($playerSquad[3].weapon).ammo))
		{//echo("$guardModeCharacter =" SPC $guardModeCharacter);
			$guardModeCharacter = 3;
			squadMemberHasTarget(3, %monster);
		}
		else
		{
			setNoAmmoDialogue($playerSquad[3]);
		}
	}
	else
	{
		clearUnitLabel($playerSquad[3]);
	}
}