// Handles Tile Destruction.
// Worthless Bums 2012.


// Performs Grenadier destruction of the specified tile.
function grenadierDestroyTile (%xIndex, %yIndex)
{//echo("grenadierDestroyTile()");
	%tileImagemap = $mazeRoom[%xIndex, %yIndex].imagemap;
	%substring = getSubStr(%tileImagemap, 0, 5); 
	if (%substring $= "Space")
	{
		return;
	}
	if (%xIndex != $stairsXIndex	||
		%yIndex != $stairsYIndex)
	{
		$mazeRoom[%xIndex, %yIndex].safeDelete();
	}
	handleGrenadierExplosion(%xIndex, %yIndex);
	showAdjacentTileDestruction(%xIndex, %yIndex);
}

// Destroys a terrain tile.
function destroyTerrainTile (%x, %y)
{//echo("destroyTerrainTile()");echo(%x @ ", " @ %y);
	if (isObject($mazeRoom[%x, %y]))
	{//echo("     IS TILE OBJECT!");
		// Delete any lights or light sources on the destroyed wall tile.
		if ($mazeRoom[%x, %y].status == -2)		// Wall Tile.
		{//echo("          WALL TILE!");
			if (isObject($mazeRoom[%x, %y].light))
			{//echo("               LIGHT FOUND!");
				$mazeRoom[%x, %y].light.lightSource.safeDelete();
				$mazeRoom[%x, %y].light.safeDelete();
			}
			if (isObject($mazeRoom[%x, %y].painting))
			{
				($mazeRoom[%x, %y].painting).safeDelete();
			}
			if (isObject($mazeRoom[%x, %y].porthole))
			{
				($mazeRoom[%x, %y].porthole).safeDelete();
			}
		}
		// Delete any plastic explosive on the destroyed door.
		if ($mazeRoom[%x, %y].status == -3)		// Closed Door.
		{//echo("          DOOR TILE!");
			if (isObject($mazeRoom[%x, %y].plasticExplosive))
			{//echo("               PLASTIC EXPLOSIVE FOUND!");
				$mazeRoom[%x, %y].plasticExplosive.safeDelete();
			}
		}
	}
	%position = $mazeRoom[%x, %y].position;
	$mazeRoom[%x, %y].safeDelete();		// Because of the way safeDelete() is scheduled we don't know if it will be deleted prior to showFogOfWar() triggering. Null it on the next line!
	$mazeRoom[%x, %y] = null;
	%randomSfx = getRandom(1, 3);
	playRandomMetalWallCollapseSfx();
	showAdjacentTileDestruction(%x, %y);
}

// Given a specified (wall) tile that has been destroyed, applies destruction sprites to adjacent wall tiles.
function showAdjacentTileDestruction (%xIndex, %yIndex)
{//echo("showAdjacentTileDestruction()");
	if ($squadFoundStairs)
	{
		schedule(250, 0, "placeElevatorZone");
	}
	
	// Check north tile.
	if (%yIndex - 1 >= 0)
	{//echo("$mazeRoom[" @ %xIndex @ ", " @ %yIndex - 1 @ "].status =" SPC $mazeRoom[%xIndex, %yIndex - 1].status);
		if ($mazeRoom[%xIndex, %yIndex - 1].status == -2)		// Wall Tile.
		{//echo("NORTH DESTRUCTION!");
			%northDestructionTile = new Sprite("North Destruction")
			{
				status = -2;			// Same as a wall tile status; not used, just for consistency for now.
				SceneLayer = $TileDestructionLayer;
				position = getTilePositionFromIndices(%xIndex, %yIndex - 1);
				scenegraph = levelSceneGraph;
				image = "Sprites:DestructionNorth";
				visible = true;
				size = "4 4";
			};
			addObjectToScene(%northDestructionTile);
			//%northDestructionTile.mount($mazeRoom[%xIndex, %yIndex - 1], 0.0, 0.0, 50.0, true, true, true, true);
		}
	}
	
	// Check south tile.
	if (%yIndex + 1 < $mazeDimension)
	{
		if ($mazeRoom[%xIndex, %yIndex + 1].status == -2)		// Wall Tile.
		{//echo("SOUTH DESTRUCTION!");
			%southDestructionTile = new Sprite("South Destruction")
			{
				status = -2;			// Same as a wall tile status; not used, just for consistency for now.
				SceneLayer = $TileDestructionLayer;
				graph = $TileDestructionGraph;
				position = getTilePositionFromIndices(%xIndex, %yIndex + 1);
				scenegraph = levelSceneGraph;
				image = "Sprites:DestructionSouth";
				visible = true;
				size = "4 4";
			};
			addObjectToScene(%southDestructionTile);
			//%southDestructionTile.mount($mazeRoom[%xIndex, %yIndex + 1], 0.0, 0.0, 50.0, true, true, true, true);
		}
	}
	
	// Check East tile.
	if (%xIndex + 1 < $mazeDimension)
	{
		if ($mazeRoom[%xIndex + 1, %yIndex].status == -2)		// Wall Tile.
		{//echo("EAST DESTRUCTION!");
			%eastDestructionTile = new Sprite("East Destruction")
			{
				status = -2;			// Same as a wall tile status; not used, just for consistency for now.
				SceneLayer = $TileDestructionLayer;
				graph = $TileDestructionGraph;
				position = getTilePositionFromIndices(%xIndex + 1, %yIndex);
				scenegraph = levelSceneGraph;
				image = "Sprites:DestructionEast";
				visible = true;
				size = "4 4";
			};
			addObjectToScene(%eastDestructionTile);
			//%eastDestructionTile.mount($mazeRoom[%xIndex + 1, %yIndex], 0.0, 0.0, 50.0, true, true, true, true);
		}
	}
	
	// Check West tile.
	if (%xIndex - 1 >= 0)
	{
		if ($mazeRoom[%xIndex - 1, %yIndex].status == -2)		// Wall Tile.
		{//echo("WEST DESTRUCTION!");
			%westDestructionTile = new Sprite("West Destruction")
			{
				status = -2;			// Same as a wall tile status; not used, just for consistency for now.
				SceneLayer = $TileDestructionLayer;
				graph = $TileDestructionGraph;
				position = getTilePositionFromIndices(%xIndex - 1, %yIndex);
				scenegraph = levelSceneGraph;
				image = "Sprites:DestructionWest";
				visible = true;
				size = "4 4";
			};
			addObjectToScene(%westDestructionTile);
			//%westDestructionTile.mount($mazeRoom[%xIndex - 1, %yIndex], 0.0, 0.0, 50.0, true, true, true, true);
		}
	}
	
	if (%xIndex != $stairsXIndex	||
		%yIndex != $stairsYIndex)
	{
		%position = getTilePositionFromIndices(%xIndex, %yIndex);
		%position = getWord(%position, 0) + $GlobalHorizontalShift SPC getWord(%position, 1) + $GlobalVerticalShift;
		%destructionTile = new Sprite("Center Destruction")
		{
			status = -2;			// Same as a wall tile status; not used, just for consistency for now.
			SceneLayer = $TileDestructionLayer;
			graph = $TileDestructionGraph;
			position = %position;//getTilePositionFromIndices(%xIndex, %yIndex);
			scenegraph = levelSceneGraph;
			image = "Sprites:DestructionCenter";
			visible = true;
			size = "4 4";
		};
		addObjectToScene(%destructionTile);
	}
	$fogOfWar.schedule($wallDestructionFogDelay, "showFogOfWar");
	%currentDestroyedTile = new ScriptObject()
	{
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	$destroyedTileList.add(%currentDestroyedTile);
	autoTargetEnemyUnit($autoTargetDelay);
}