// Geomorph Splitter.
// Worthless Bums 2012.


// Compile $mapArr[] and $mapArrGeo[], giving the first array priority.
// NOTE: For geomorph treasure rooms give the second array priority!
function compileMapArrs (%isTreasureRoom)
{//echo("compileMapArrs()");
	if (%isTreasureRoom)
	{
		for (%i = 0; %i < $mazeDimension; %i++)
		{
			for (%j = 0; %j < $mazeDimension; %j++)
			{
				if ($mapArrGeo[%j, %i] != 1)		// Not Blank;
				{
					$mapArr[%j, %i] = $mapArrGeo[%j, %i];
				}
			}
		}
		plugDestroyedFloorTiles();
		plugDeadEndDoors();
	}
	else
	{
		for (%i = 0; %i < $mazeDimension; %i++)
		{
			for (%j = 0; %j < $mazeDimension; %j++)
			{
				if ($mapArrGeo[%j, %i] != 1	&&	// Not Blank.
					$mapArr[%j, %i] == 1)		// Blank.
				{
					$mapArr[%j, %i] = $mapArrGeo[%j, %i];
				}
			}
		}
		plugDestroyedFloorTiles();
		plugDeadEndDoors();
	}
}
// Plug destroyed floor tiles exposed to space.
function plugDestroyedFloorTiles ()
{//echo("plugDestroyedFloorTiles()");
	for (%i = 0; %i < $mazeDimension; %i++)
	{
		for (%j = 0; %j < $mazeDimension; %j++)
		{
			if ($mapArr[%j, %i] == 0)				// Floor.
			{
				// If any nearby (all eight directions) tile is space, turn them to wall tiles.
				if ($mapArr[%j, %i - 1] == 1)		// West.
				{
					$mapArr[%j, %i - 1] = 2;
				}
				if ($mapArr[%j, %i + 1] == 1)		// East.
				{
					$mapArr[%j, %i + 1] = 2;
				}
				if ($mapArr[%j - 1, %i] == 1)		// North.
				{
					$mapArr[%j - 1, %i] = 2;
				}
				if ($mapArr[%j + 1, %i] == 1)		// South.
				{
					$mapArr[%j + 1, %i] = 2;
				}
				if ($mapArr[%j - 1, %i - 1] == 1)	// NW.
				{
					$mapArr[%j - 1, %i - 1] = 2;
				}
				if ($mapArr[%j - 1, %i + 1] == 1)	// NE.
				{
					$mapArr[%j - 1, %i + 1] = 2;
				}
				if ($mapArr[%j + 1, %i - 1] == 1)	// SW.
				{
					$mapArr[%j + 1, %i - 1] = 2;
				}
				if ($mapArr[%j + 1, %i + 1] == 1)	// SE.
				{
					$mapArr[%j + 1, %i + 1] = 2;
				}
			}
		}
	}
}
// Plug dead end doors.
function plugDeadEndDoors ()
{//echo("plugDeadEndDoors()");
	for (%i = 0; %i < $mazeDimension; %i++)
	{
		for (%j = 0; %j < $mazeDimension; %j++)
		{
			if ($mapArr[%j, %i] == 4)				// Door.
			{
				// If the door leads nowhere, turn it into a wall.
				if (!($mapArr[%j, %i - 1] == 0	&&	// West.
					  $mapArr[%j, %i + 1] == 0)	&&	// East.
					!($mapArr[%j - 1, %i] == 0	&&	// North.
					  $mapArr[%j + 1, %i] == 0))	// South.
				{
					$mapArr[%j, %i] = 2;			// Wall.
				}
			}
		}
	}
}