// Handles Burrowed Monsters.
// Worthless Bums 2012.


// Handles burrowed monsters.
function handleBurrowedMonsters ()
{//echo("handleBurrowedMonsters()");
	if ($burrowedMonsters.getCount() <= 0)
	{
		return;
	}
	for (%i = 0; %i < $burrowedMonsters.getCount(); %i++)
	{
		%unburrow = getRandom(1, 100);
		%chanceToUnburrow = 5;
		if (%unburrow <= %chanceToUnburrow)
		{
			unburrowMonster(%i);
			// Should decrement %i now? Leave it for now, maybe it will act quirky and awesome when unburrowing.
		}
	}
}

// Unburrows a burrowed monster near the a squad member.
function unburrowMonster (%monsterIndex)
{//echo("unburrowMonster()");
	%burrowedMonster = $burrowedMonsters.getObject(%monsterIndex);
	do
	{
		if (isSquadDead())
		{
			return;
		}
		%characterIndex = getRandom(0, 3);
	} while(!isObject($playerSquad[%characterIndex]))			// Beware infinite loop if the squad is dead and this code hits! Should never happen, but still...
	%freeTile = getRandomFreeTile(%characterIndex);
	if (%freeTile $= "-1 -1")
	{
		return;
	}
	%burrowedMonster.xIndex = getWord(%freeTile, 0);
	%burrowedMonster.yIndex = getWord(%freeTile, 1);
	%burrowedMonster.setPosition(getTilePositionFromIndices(%burrowedMonster.xIndex, %burrowedMonster.yIndex));
	%burrowedMonster.setVisible(true);
	$monsterList.add(%burrowedMonster);
	$burrowedMonsters.remove($burrowedMonsters.getObject(%monsterIndex));
}

// Returns a random, unoccupied space near the given tile.
function getRandomFreeTile (%characterIndex)
{//echo("getRandomFreeTile()");
	%squadMember = $playerSquad[%characterIndex];
	if (!isObject(%squadMember))
	{
		return "-1 -1";
	}
	%xIndex = %squadMember.xIndex;
	%yIndex = %squadMember.yIndex;
	if (%yIndex - 1 >= 0							&&
		!isObject($mazeRoom[%xIndex, %yIndex - 1]))
	{
		%freeTile = %xIndex SPC %yIndex - 1;
		%tileIsFree = true;
		if (!tileClearOfSquadMembers(%xIndex, %yIndex - 1)	||
			!tileClearOfMonsters(%xIndex, %yIndex - 1))
		{
			%tileIsFree = false;
		}
		if (%tileIsFree)
		{
			return %freeTile;
		}
	}
	if (%yIndex + 1 < $mazeDimension					&&
		!isObject($mazeRoom[%xIndex, %yIndex + 1]))
	{
		%freeTile = %xIndex SPC %yIndex + 1;
		%tileIsFree = true;
		if (!tileClearOfSquadMembers(%xIndex, %yIndex + 1)	||
			!tileClearOfMonsters(%xIndex, %yIndex + 1))
		{
			%tileIsFree = false;
		}
		if (%tileIsFree)
		{
			return %freeTile;
		}
	}
	
	// Left bounds check.
	if (%xIndex - 1 >= 0								&&
		!isObject($mazeRoom[%xIndex - 1, %yIndex]))
	{
		%freeTile = %xIndex - 1 SPC %yIndex;
		%tileIsFree = true;
		if (!tileClearOfSquadMembers(%xIndex - 1, %yIndex)	||
			!tileClearOfMonsters(%xIndex - 1, %yIndex))
		{
			%tileIsFree = false;
		}
		if (%tileIsFree)
		{
			return %freeTile;
		}
	}
	if (%xIndex + 1 < $mazeDimension					&&
		!isObject($mazeRoom[%xIndex + 1, %yIndex]))
	{
		%freeTile = %xIndex + 1 SPC %yIndex;
		%tileIsFree = true;
		if (!tileClearOfSquadMembers(%xIndex + 1, %yIndex)	||
			!tileClearOfMonsters(%xIndex + 1, %yIndex))
		{
			%tileIsFree = false;
		}
		if (%tileIsFree)
		{
			return %freeTile;
		}
	}
	if (!%tileIsFree)
	{
		return "-1 -1";
	}
}