// Procedurally Generated Levels.
// Worthless Bums 2012.


/*
 *		Tile Statuses
 *
 *		0 = not walkable
 *		1 = walkable
 */

// Pick a random direction to walk the maze in.
function randomDirection (%current)
{//echo("randomDirection()");
	%random = getRandom(1, 4);
	//echo("%random =" SPC %random);
	
	// North.
	if (%random == 1)
	{
		return VectorAdd(%current, "0 1");
	}
	
	// South.
	if (%random == 2)
	{
		return VectorAdd(%current, "0 -1");
	}
	
	// East
	if (%random == 3)
	{
		return VectorAdd(%current, "1 0");
	}
	
	// West
	if (%random == 4)
	{
		return VectorAdd(%current, "-1 0");
	}	
}
// Creates a new (dungeon) maze.
function newMaze (%length)
{//echo("newMaze()");
//echo("%length =" SPC %length);
	$mazeRoom[0] = "0 0";
	$isRoom["0 0"] = true;
	$mazeLength = %length;
	//echo("Making maze of " @ %length @ " cells.");
	
	// For inner room loop.
	%dimension = 10;//$mazeLength / 16;
	// Outer room loop.
	for (%i = 0; %i < %length; %i++)
	{//echo("%i =" SPC %i);
		// Inner room loop.
		for (%j = 0; %j < %dimension; %j++)
		{//echo("%j =" SPC %j);
		//echo("%dimension =" SPC %dimension);
			// Make a new room.
			%newRoom = randomDirection($mazeRoom[%i]);
			if ($isRoom[%newRoom] $= "")
			{
				break;
			}
			
			// Make the last tile the way out.
			if (%j == %dimension - 1)
			{//echo("MARKING STAIRS!");
				// Mark the end of the maze - the player can leave by entering this tile!
				//%newRoom = VectorAdd($mazeRoom[%i], "0 -1");
				
				// Flag down tile.
				$downPiece[%i] = true;
				
				// Break out that was the last room in the maze.
				break;
			}
		}
		//echo("Room #" @ %i @ " located at " @ %newRoom);
		$mazeRoom[%i + 1] = %newRoom;	
		$isRoom[%newRoom] = true;
	}
	
	// Start next part.
	//mazeStage2();
	mazeStage3();
}
// Clears the current maze.
function clearMaze ()
{//echo("clearMaze()");
	for (%i = 0; %i < $mazeLength; %i++)
	{
		$isRoom[$mazeRoom[%i]] = "";
		$mazeRoom[%i] = "";	
	}
	
	// Start next part.
	$mazeLength = "";
}
function mazeStage2 ()
{//echo("mazeStage2()");
	for (%i = 0; %i <= $mazeLength; %i++)
	{
		%roomType = "";
		if ($isRoom[VectorAdd($mazeRoom[%i], "0 1")] !$= "")
		{
			%roomType = "n";	
		}
		if ($isRoom[VectorAdd($mazeRoom[%i], "0 -1")] !$= "")
		{
			%roomType = %roomType @ "s";	
		}
		if ($isRoom[VectorAdd($mazeRoom[%i], "1 0")] !$= "")
		{
			%roomType = %roomType @ "e";	
		}
		if ($isRoom[VectorAdd($mazeRoom[%i], "-1 0")] !$= "")
		{
			%roomType = %roomType @ "w";	
		}
		$roomType[%i] = %roomType @ ".dif";
		if ($downPiece[%i] == true)
		{
			$roomType[%i] = "down.dif";
		}
		if($roomType[%i] $= ".dif")
		{
			$roomType[%i] = "single.dif";
		}
	}
	
	// Start next part.
	mazeStage3();
}
function mazeStage3()
{//echo("mazeStage3()");
	for (%i = 0; %i < $mazeLength; %i++)
	{
		// Compose the proper room position.
		%vectorScale = 4;
		%positionX = getWord(VectorScale($mazeRoom[%i], %vectorScale), 0);
		%positionY = getWord(VectorScale($mazeRoom[%i], %vectorScale), 1);
		%positionX += 2;		// Offset.
		%positionY += 2;		// Offset.
		// Compose the final position.
		%position = %positionX SPC %positionY;
		
		// Set image map.
		%tileImagemap = "Bricks_Tile_32x32ImageMap";
		// Make the last tile as the stairs down.
		if (%i == $mazeLength - 1)
		{
			// Save this position.
			$stairsPositionX = %positionX;
			$stairsPositionY = %positionY;
			//echo("$stairsPositionX =" SPC $stairsPositionX);
			//echo("$stairsPositionY =" SPC $stairsPositionY);
			
			// Set to stairs image map.
			%tileImagemap = "ElevatorImageMap";//"Stairs_32x32ImageMap";
		}
		
		// Set the room static sprite.
		%tileRoom = new Sprite("Room" @ %i)
		{
			SceneLayer = 25;
			position = %position;
			scenegraph = levelSceneGraph;
			imagemap = %tileImagemap;
			visible = true;
			size = "4 4";
		};
		addObjectToScene(%tileRoom);
		//echo("Room" @ %i @ " placed at: " @ VectorScale($mazeRoom[%i], %vectorScale));
		//echo("%tileRoom.getPosition() =" SPC %tileRoom.getPosition());
	}
}