// Steampunk Spaceship Deck Generator.
// Worthless Bums 2013.


// Flag status per tile.
// 0 = room/corridor; walkable tile
// 1 = outer space
// 2 = wall
// 3/4/5 = door/special


// Generate a ship deck level.
function generateShipDeckLevel ()
{//echo("generateShipDeckLevel()");
	// Initialize.
	initializeDeckGenerationParameters();
	// Generate some rooms.
	%numberOfRooms = 16;
	%nearbyX = 32;
	%nearbyY = 32;
	for (%i = 0; %i < %numberOfRooms; %i++)
	{
		%roomPosition = createDeckRoom(%nearbyX, %nearbyY,
									   12, 64,
									   5, 9,
									   5, 9,
									   64, 64);
		if (%roomPosition !$= "-1 -1")
		{
			%nearbyX = getWord(%roomPosition, 0);
			%nearbyY = getWord(%roomPosition, 1);
		}
	}
}

// Initializes deck generation parameters.
function initializeDeckGenerationParameters ()
{//echo("initializeDeckGenerationParameters()");
	// Tile statuses.
	$floorTileStatus = 		0;
	$outerSpaceTileStatus = 1;
	$wallTileStatus = 		2;
	$doorTileStatus = 		3;
	// Initialize $mapArr.
	initializeMapArr(64, 64);
}

// Initialize $mapArr.
function initializeMapArr (%xSize, %ySize)
{//echo("initializeMapArr()");
	for (%i = 0; %i < %xSize; %i++)
	{
		for (%j = 0; %j < %ySize; %j++)
		{
			$mapArr[%i, %j] = 1;	// Default to an outer space tile.
		}
	}
}

// Create deck room.
// NOTE: Don't check parameters. This is the caller's responsibility.
function createDeckRoom (%nearbyX, %nearbyY,
						 %roomRangeLimit, %maxPlacementAttempts,
						 %roomWidthMin, %roomWidthMax,
						 %roomHeightMin, %roomHeightMax,
						 %mapSizeX, %mapSizeY)
{//echo("createDeckRoom()");
	// Generate a random room size.
	// NOTE: These include the room walls!
	%width = getRandom(%roomWidthMin, %roomWidthMax);
	%height = getRandom(%roomHeightMin, %roomWidthMax);
	
	// Check to see if this room collides with anything!
	%placementAttempts = 0;
	do
	{
		%placementAttempts++;
		%canPlaceRoom = true;
		
		// Generate a position.
		// NOTE: This is the upper left hand corner of the room.
		%xPos = getRandom(2, %mapSizeX - 2 - %roomWidthMax);
		%yPos = getRandom(2, %mapSizeY - 2 - %roomHeightMax);
		
		// Make sure the room isn't placed too far away.
		if (VectorDist(%nearbyX SPC %nearbyY, %xPos SPC %yPos) <= %roomRangeLimit)
		{
			for (%i = %xPos; %i < %xPos + %width; %i++)
			{
				for (%j = %yPos; %j < %yPos + %height; %j++)
				{
					if ($mapArr[%i, %j] != $outerSpaceTileStatus)
					{
						%canPlaceRoom = false;
						break;
					}
				}
			}
		}
		else
		{
			%canPlaceRoom = false;
		}
	} while(!%canPlaceRoom	&&
			%placementAttempts < %maxPlacementAttempts)
	
	// If no collision was detected...
	if (%canPlaceRoom)
	{
		// ... place the room.
		for (%i = %xPos; %i < %xPos + %width; %i++)
		{
			for (%j = %yPos; %j < %yPos + %height; %j++)
			{
				if (%i == %xPos					||		// West wall.
					%i == %xPos + %width - 1	||		// East wall.
					%j == %yPos					||		// North wall.
					%j == %yPos + %height - 1)			// South wall.
				{
					// Mark tile as a wall.
					$mapArr[%i, %j] = $wallTileStatus;
				}
				else
				{
					// Mark tile as a floor.
					$mapArr[%i, %j] = $floorTileStatus;
				}
			}
		}
		
		// All rooms need at least one door!
		%numberOfDoors = getRandom(1, 2);
		for (%i = 0; %i < %numberOfDoors; %i++)
		{
			// Get a random wall tile.
			if (getRandom(0, 1))
			{
				if (getRandom(0, 1))
				{
					%x = %xPos;
				}
				else
				{
					%x = %xPos + %width - 1;
				}
				%y = getRandom(%yPos + 1, %yPos + %height - 2);
			}
			else
			{
				if (getRandom(0, 1))
				{
					%y = %yPos;
				}
				else
				{
					%y = %yPos + %height - 1;
				}
				%x = getRandom(%xPos + 1, %xPos + %width - 2);
			}
			// Mark tile as a door.
			$mapArr[%x, %y] = $doorTileStatus;
		}
		
		
		return %xPos SPC %yPos;
	}
	else
	{
		
		return "-1 -1";
	}
}

// Creates corridors from door to door.
function connectDeckRooms ()
{//echo("connectDeckRooms()");
	// Nothing here yet!
}