// Monster Group AI.
// Worthless Bums 2013.


// Enemies that are active and out of sight can sneak up on the squad!
function sneakUpOnSquad (%monster)
{//echo("sneakUpOnSquad()");
	// Only initiate this code on Normal difficulty or harder!
	if ($difficultyLevel <= 0)
	{
		return;
	}
	
	/*
	// Monster "teleport" code!
	// Move the monster to fully covered (not player explored!) tile near the squad!
	%coveredTile = findCoveredTileNearSquad();
	%xIndex = getWord(%coveredTile, 0);
	%yIndex = getWord(%coveredTile, 1);
	// Make sure we have a valid tile position!
	if (%xIndex == -1	&&
		%yIndex == -1)
	{
		// Bad tile! Abort!
		return false;
	}
	else
	{//echo("%xIndex =" SPC %xIndex);
	//echo("%yIndex =" SPC %yIndex);
		// Move the monster.
		%monster.xIndex = %xIndex;
		%monster.yIndex = %yIndex;
		%position = getTilePositionFromIndices(%xIndex, %yIndex);
		%monsterPositionX = getWord(%position, 0);
		%monsterPositionY = getWord(%position, 1);
		moveUnit(%monster, %monsterPositionX, %monsterPositionY, 99999, true, false, true, 0.0, false);
		return true;
	}
	*/
	
	// Grab monster to act.
	if (%monster.currentActionPoints <= 0)
	{
		// Reduce AP to 0.
		%monster.currentActionPoints = 0;
		return;
	}
	// Check stasis flag.
	if (%monster.stasis)
	{
		// Remove stasis at the END of monster turn!
		// Reduce AP to 0.
		%monster.currentActionPoints = 0;
		return;
	}
	monsterAction(%monster);
	%delay = 125.0;
	schedule(%delay + 120, 0, "updateMonsterFloatingText");
	schedule(%delay + 250, 0, "executeGuardModeActions", %monster);		// *THIS MUST OCCUR BEFORE...
	// Update sentry turrets.
	schedule(%delay + 400, 0, "updateSentryTurrets");
}
function findCoveredTileNearSquad (%monster)
{//echo("findCoveredTileNearSquad()");
	// Pick a random, surviving squad member to check around.
	%characterIndex = getRandom(0, 3);
	%xIndex = $playerSquad[%characterIndex].xIndex;
	%yIndex = $playerSquad[%characterIndex].yIndex;
	if (isObject($playerSquad[%characterIndex]))
	{
		// Look around the character for a covered tile.
		for (%i = %xIndex + 1; %i < %xIndex + $MonsterSneakRadius; %i++)
		{
			// Quadrant 4.
			for (%j = %yIndex + 1; %j < %yIndex + $MonsterSneakRadius; %j++)
			{
				if ($fogOfWar.tileHasFullFog(%i, %j)	&&
					tileIsClearOfObstructions(%i, %j))
				{
					if (getRandom(1, 100) <= $MonsterChanceToSneak)
					{
						return %i SPC %j;
					}
				}
			}
			// Quadrant 1.
			for (%j = %yIndex - $MonsterSneakRadius; %j > %yIndex; %j--)
			{
				if ($fogOfWar.tileHasFullFog(%i, %j)	&&
					tileIsClearOfObstructions(%i, %j))
				{
					if (getRandom(1, 100) <= $MonsterChanceToSneak)
					{
						return %i SPC %j;
					}
				}
			}
		}
		for (%i = %xIndex - 1; %i > %yIndex - $MonsterSneakRadius; %i--)
		{
			// Quadrant 3.
			for (%j = %yIndex + 1; %j < %yIndex + $MonsterSneakRadius; %j++)
			{
				if ($fogOfWar.tileHasFullFog(%i, %j)	&&
					tileIsClearOfObstructions(%i, %j))
				{
					if (getRandom(1, 100) <= $MonsterChanceToSneak)
					{
						return %i SPC %j;
					}
				}
			}
			// Quadrant 2.
			for (%j = %yIndex - $MonsterSneakRadius; %j > %yIndex; %j--)
			{
				if ($fogOfWar.tileHasFullFog(%i, %j)	&&
					tileIsClearOfObstructions(%i, %j))
				{
					if (getRandom(1, 100) <= $MonsterChanceToSneak)
					{
						return %i SPC %j;
					}
				}
			}
		}
	}
	return -1 SPC -1;	// "Error code".
}