// Manages player and monster turns.
// Worthless Bums 2012.


// If the current squad member is out of action points, find another squad member with action points and switch to him/her.
// It is this function's responsibility to check if the active member is out of action points, not the caller!
function autoSwitchSquadMember ()
{//echo("autoSwitchSquadMember()");
	// We avoid autoswitching right after loading a game.
	if ($loadedActiveCharacter != -1)
	{
		$loadedActiveCharacter = -1;
		return false;
	}
	else if ($playerSquad[$activeCharacter].currentActionPoints > 0)
	{
		return false;
	}
	// Don't have multiple auto switch events pending!
	if (!isEventPending($autoSwitchSquadMemberEvent))
	{//echo("SETTING AUTO-SWITCH EVENT!");
		cancel($autoSwitchSquadMemberEvent);
		if (isEventPending($SwitchSquadMemberEvent))
		{//$autoSwitchSquadMemberCount--;
			cancel($SwitchSquadMemberEvent);
		}
		$SwitchSquadMemberEvent = schedule($autoSwitchSquadMemberDelay, 0, "switchSquadmember");
		createAutoSwitchAnimation($playerSquad[$activeCharacter].xIndex, $playerSquad[$activeCharacter].yIndex);
		playAudio("Audio:outOfAP", EffectsAudioVolume.value);
	}
	updateCameraPosition();
	return true;
}
// If the current squad member is out of action points, find another squad member with action points and switch to him/her.
// It is this function's responsibility to check if the active member is out of action points, not the caller!
function switchSquadMember ()
{//echo("switchSquadMember()");
	cancelAutoSwitchEvents();
	// Check auto-switch squad member flag.
	if (!$autoSwitchCharacters)
	{
		return false;
	}
	// Check monster turn ongoing.
	if ($monsterTurnOngoing)
	{
		return false;
	}
	// Adds a small delay before autoswitching characters.
	while (isObject($rifleMuzzleFlashAnimation))
	{//echo("$rifleMuzzleFlashAnimation still exists!");
		$autoSwitchSquadMemberEvent = schedule(200, 0, "autoSwitchSquadmember");
		return;
	}
	enforceSquadPersistence();
	if (!($leaderPersist	||
		$scoutPersist		||
		$grenadierPersist	||
		$supportPersist))
	{
		return;
	}
	// Code to run if we just killed off a squad member. This means that the active character will point to a non-persistent character.
	if (!isObject($playerSquad[$activeCharacter]))
	{//echo("Non-persistent active character detected!");
		switch ($activeCharacter)
		{
			case 0:	// Leader.
				// Switch to Scout.
				if ($scoutPersist)
				{
					$activeCharacter = 1;
				}
				else if ($grenadierPersist)
				{
					$activeCharacter = 2;
				}
				else if ($supportPersist)
				{
					$activeCharacter = 3;
				}
				
			case 1: // Scout
				// Switch to Grenadier.
				if ($grenadierPersist)
				{
					$activeCharacter = 2;
				}
				else if ($supportPersist)
				{
					$activeCharacter = 3;
				}
				else if ($leaderPersist)
				{
					$activeCharacter = 0;
				}
				
			case 2: // Grenadier
				// Switch to Support.
				if ($supportPersist)
				{
					$activeCharacter = 3;
				}
				else if ($leaderPersist)
				{
					$activeCharacter = 0;
				}
				else if ($scoutPersist)
				{
					$activeCharacter = 1;
				}
				
			case 3: // Support
				// Switch to Leader.
				if ($leaderPersist)
				{
					$activeCharacter = 0;
				}
				else if ($scoutPersist)
				{
					$activeCharacter = 1;
				}
				else if ($grenadierPersist)
				{
					$activeCharacter = 2;
				}
		}
	}
	// Save this to compare later to see if we play the sound effect for character switching.
	%activeCharacter = $activeCharacter;
	if ($autoEndTurnSwitchCharacters)
	{//echo("$actionPointPool = 0");
		if ($leaderPersist)
		{//echo("0");
			$activeCharacter = 0;
		}
		else if ($scoutPersist)
		{//echo("1");
			$activeCharacter = 1;
		}
		else if ($grenadierPersist)
		{//echo("2");
			$activeCharacter = 2;
		}
		else if ($supportPersist)
		{//echo("3");
			$activeCharacter = 3;
		}
		else
		{//echo("BAD!");
			return false;
		}
		handleActiveCharacterIndicator();
	}
	if (playerHasActionPoints())
	{//echo("Player has AP.");
		%counter = 0;
		%counterMax = 32;
		// Check if the current member is out of action points.
		while ($playerSquad[$activeCharacter].currentActionPoints <= 0	&&
			   %counter < %counterMax)
		{//echo("$activeCharacter =" SPC $activeCharacter);
			%counter++;
			switch ($activeCharacter)
			{
				case 0:	// Leader.
					// Switch to Scout.
					if ($scoutPersist)
					{
						$activeCharacter = 1;
					}
					else if ($grenadierPersist)
					{
						$activeCharacter = 2;
					}
					else if ($supportPersist)
					{
						$activeCharacter = 3;
					}
					
				case 1: // Scout
					// Switch to Grenadier.
					if ($grenadierPersist)
					{
						$activeCharacter = 2;
					}
					else if ($supportPersist)
					{
						$activeCharacter = 3;
					}
					else if ($leaderPersist)
					{
						$activeCharacter = 0;
					}
					
				case 2: // Grenadier
					// Switch to Support.
					if ($supportPersist)
					{
						$activeCharacter = 3;
					}
					else if ($leaderPersist)
					{
						$activeCharacter = 0;
					}
					else if ($scoutPersist)
					{
						$activeCharacter = 1;
					}
					
				case 3: // Support
					// Switch to Leader.
					if ($leaderPersist)
					{
						$activeCharacter = 0;
					}
					else if ($scoutPersist)
					{
						$activeCharacter = 1;
					}
					else if ($grenadierPersist)
					{
						$activeCharacter = 2;
					}
			}
		}
		// Bad times...
		if (%counter >= %counterMax)
		{
		}
	}
	else
	{//echo("end turn!");
		scheduleMonsterTurn();
		return false;
	}
	if (isObject($playerSquad[$activeCharacter])	&&
		$sceneWindowMountParent != $playerSquad[$activeCharacter])
	{//echo("MOUNTING!");
		sceneWindow2D.mount($playerSquad[$activeCharacter], $sceneWindow2DxOffset SPC $sceneWindow2DyOffset, $cameraMountForce, false);		// Parameter list is: object to mount to, offset, force, do not send to mount!
		$sceneWindowMountParent = $playerSquad[$activeCharacter];
	}
	handleActiveCharacterIndicator();
	if (%activeCharacter != $activeCharacter)
	{
		updateTalentBarIcons();
		playAudio("Audio:switchSquadMember", EffectsAudioVolume.value);
	}
	updateActiveCharacterTutorial();
	updateCameraPosition();
	autoTargetEnemyUnit($autoTargetDelay);
}
// Auto-End Turn Cleanup.
function autoEndTurnCleanup ()
{//echo("autoEndTurnCleanup()");
	updateSquadStatusEffects();
	updateTalentBarIcons();
	if (isObject($autoTargetAnimation))
	{
		$autoTargetAnimation.safeDelete();
	}
	schedule(50, 0, "updateVehicleStatuses");
}
// Manual end player turn. Any unspent action points are lost.
function playerEndTurn ()
{//echo("playerEndTurn()");
	$playerManuallyEndedTurn = true;
	squadDepleteActionPoints();
	updateSquadInfoGui();
	updateEventText($whiteColor @ "Turn ended" @ $defaultColor @ ".");
	$fogOfWar.showFogOfWar();
	scheduleMonsterTurn();
	autoEndTurnCleanup();
}
// Determines if the player's turn is over by checking the squad's total action points.
function playerHasActionPoints ()
{//echo("playerHasActionPoints()");
	for (%i = 0; %i < 4; %i++)
	{
		if (isObject($playerSquad[%i])	&&
			$playerSquad[%i].currentActionPoints > 0)
		{
			return true;
		}
	}
	return false;
}