// Generates dungeons.
// Worthless Bums 2012.


function destroyMazeRoom ()
{//echo("destroyMazeRoom()");
	for (%i = 0; %i < $mazeDimension; %i++)
	{
		for (%j = 0; %j < $mazeDimension; %j++)
		{
			if (isObject($mazeRoom[%i, %j]))
			{
				$mazeRoom[%i, %j].safeDelete();
			}
		}
	}
}
function destroyMazeLiftedTiles ()
{//echo("destroyMazeLiftedTiles()");echo("$mazeDimension =" SPC $mazeDimension);
	for (%i = 0; %i < $mazeDimension; %i++)
	{
		for (%j = 0; %j < $mazeDimension; %j++)
		{
			if (isObject($mazeLiftedTiles[%i, %j]))
			{//echo("deleting lifted tile at " @ %i @ ", " @ %j);
				$mazeLiftedTiles[%i, %j].shadow.safeDelete();
				$mazeLiftedTiles[%i, %j].liftedTile.safeDelete();
				$mazeLiftedTiles[%i, %j].safeDelete();
			}
		}
	}
}
function destroyMapArr ()
{//echo("destroyMapArr()");
	for (%i = 0; %i < $mazeDimension; %i++)
	{
		for (%j = 0; %j < $mazeDimension; %j++)
		{
			if (isObject($mapArr[%i, %j]))
			{
				$mapArr[%i, %j].safeDelete();
			}
		}
	}
}
function destroyMapArrGeo ()
{//echo("destroyMapArrGeo()");
	for (%i = 0; %i < $mazeDimension; %i++)
	{
		for (%j = 0; %j < $mazeDimension; %j++)
		{
			if (isObject($mapArrGeo[%i, %j]))
			{
				$mapArrGeo[%i, %j].safeDelete();
			}
		}
	}
}
// Sets the elevator tile (when loading a save game!)
function setElevatorTile ()
{//echo("setElevatorTile()");
	%elevatorPosition = getWord(%elevatorPosition, 0) + $GlobalHorizontalShift SPC getWord(%elevatorPosition, 1) + $GlobalVerticalShift;
	%stairsTile = new Sprite("Stairs")
	{
		status = 1;		// Walkable.
		SceneLayer = $ElevatorLayer;
		position = $stairsPositionX SPC $stairsPositionY;
		scenegraph = levelSceneGraph;
		image = getElevatorSprite();
		visible = true;
		size = "4 4";
		item = null;
	};
	addObjectToScene(%stairsTile);
	$mazeRoom[$stairsXIndex, $stairsYIndex] = %stairsTile;
}
function getElevatorSprite ()
{//echo("getElevatorSprite()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienElevator";
	}
	return "Sprites:Elevator";
}
function getLockersClosedSprites ()
{//echo("getLockersClosedSprites()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienLockersClosed";
	}
	return "Sprites:LockersClosed";
}
function getLockersOpenedSprites ()
{//echo("getLockersOpenedSprites()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienLockersOpened";
	}
	return "Sprites:LockersOpened";
}
function getBigDispenserOnSprites ()
{//echo("getBigDispenserOnSprites()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienBigDispenserOn";
	}
	return "Sprites:DispenserBigOn";
}
function getBigDispenserOffSprites ()
{//echo("getBigDispenserOffSprites()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienBigDispenserOff";
	}
	return "Sprites:DispenserBigOff";
}
function getSmallDispenserOnSprites ()
{//echo("getSmallDispenserOnSprites()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienSmallDispenserOn";
	}
	return "Sprites:DispenserSmallOn";
}
function getSmallDispenserOffSprites ()
{//echo("getSmallDispenserOffSprites()");
	if ($overworldLocation $= "Alien Deck")
	{
		return "Sprites:AlienSmallDispenserOff";
	}
	return "Sprites:DispenserSmallOff";
}
function addStairsToDungeon ()	// This is infinitely looping sometimes!
{//echo("addStairsToDungeon()");
	if ($generateTutorialLevel)
	{
		%xIndex = 9;
		%yIndex = 9;
		%elevatorPosition = getTilePositionFromIndices(%xIndex, %yIndex);
		%elevatorPosition = getWord(%elevatorPosition, 0) + $GlobalHorizontalShift SPC getWord(%elevatorPosition, 1) + $GlobalVerticalShift;
		$mazeRoom[%xIndex, %yIndex] = new Sprite("Stairs")
		{
			status = 1;		// Walkable.
			SceneLayer = $ElevatorLayer;
			position = %elevatorPosition;
			scenegraph = levelSceneGraph;
			image = getElevatorSprite();
			visible = true;
			size = "4 4";
			item = null;
		};
		addObjectToScene($mazeRoom[%xIndex, %yIndex]);
		$stairsPositionX = getWord(%elevatorPosition, 0);
		$stairsPositionY = getWord(%elevatorPosition, 1);
		$stairsXIndex = %xIndex;
		$stairsYIndex = %yIndex;
		return;
	}
	
	$stairsPositionX = -1;
	$stairsPositionY = -1;
	%attempts = 0;
	%elevatorPlaced = false;
	do {//echo("%attempts =" SPC %attempts);
		%attempts++;
		%xIndex = getRandom(1, $mazeDimension - 2);
		%yIndex = getRandom(1, $mazeDimension - 2);
		//if (tileIsClear(%xIndex, %yIndex)	&&
		if (canPlaceElevator(%xIndex, %yIndex)	&&
			!isSpaceTile(%xIndex, %yIndex))
		{
			%elevatorPosition = getTilePositionFromIndices(%xIndex, %yIndex);
			%elevatorPosition = getWord(%elevatorPosition, 0) + $GlobalHorizontalShift SPC getWord(%elevatorPosition, 1) + $GlobalVerticalShift;
			$mazeRoom[%xIndex, %yIndex] = new Sprite("Stairs")
			{
				status = 1;		// Walkable.
				SceneLayer = $ElevatorLayer;
				position = %elevatorPosition;
				scenegraph = levelSceneGraph;
				image = getElevatorSprite();
				visible = true;
				size = "4 4";
				item = null;
			};
			addObjectToScene($mazeRoom[%xIndex, %yIndex]);
			$stairsPositionX = getWord(%elevatorPosition, 0);
			$stairsPositionY = getWord(%elevatorPosition, 1);
			$stairsXIndex = %xIndex;
			$stairsYIndex = %yIndex;
			%elevatorPlaced = true;
		}
	} while (%attempts < 200	&&
			 !%elevatorPlaced)
	if (!%elevatorPlaced)
	{
		%characterIndex = -1;
		if ($leaderPersist &&
			isObject($playerSquad[0]))
		{
			%characterIndex = 0;
		}
		else if ($scoutPersist &&
				 isObject($playerSquad[1]))
		{
			%characterIndex = 1;
		}
		else if ($grenadierPersist &&
				 isObject($playerSquad[2]))
		{
			%characterIndex = 2;
		}
		else if ($supportPersist &&
				 isObject($playerSquad[3]))
		{
			%characterIndex = 3;
		}
		%xIndex = $playerSquad[%characterIndex].xIndex;
		%yIndex = $playerSquad[%characterIndex].yIndex;
		%elevatorPosition = getTilePositionFromIndices(%xIndex, %yIndex);
		%elevatorPosition = getWord(%elevatorPosition, 0) + $GlobalHorizontalShift SPC getWord(%elevatorPosition, 1) + $GlobalVerticalShift;
		$mazeRoom[%xIndex, %yIndex] = new Sprite("Stairs")
		{
			status = 1;		// Walkable.
			SceneLayer = $ElevatorLayer;
			position = %elevatorPosition;
			scenegraph = levelSceneGraph;
			image = getElevatorSprite();
			visible = true;
			size = "4 4";
			item = null;
		};
		addObjectToScene($mazeRoom[%xIndex, %yIndex]);
		$stairsPositionX = getWord(%elevatorPosition, 0);
		$stairsPositionY = getWord(%elevatorPosition, 1);
		$stairsXIndex = %xIndex;
		$stairsYIndex = %yIndex;
		%elevatorPlaced = true;
	}
	if ($elevatorPowered)
	{
		createElevatorArrowAnimation();
	}
}
function getTallUnitTilePositionFromIndices (%i, %j)
{//echo("getTallUnitTilePositionFromIndices()");
	%position = "-1 -1";
	if (isObject($mazeLiftedTiles[%i, %j]))
	{
		%position = getTileShiftedLiftedPositionFromIndices(%i, %j);
	}
	else
	{
		%position = (%i * 4 + 2) SPC (%j * 4 + 2);
	}
	%position = getWord(%position, 0) SPC (getWord(%position, 1) - $TallUnitVerticalShift);
	return %position;
}

// Helper function to get a tile position from its indices.
function getTilePositionFromIndices (%i, %j)
{//echo("getTilePositionFromIndices()");
	if (isObject($mazeLiftedTiles[%i, %j]))
	{//echo("lifted!" SPC %i SPC %j);
		return getTileShiftedLiftedPositionFromIndices(%i, %j);
	}
	else
	{//echo("not lifted!" SPC %i SPC %j);
		return (%i * 4 + 2) SPC (%j * 4 + 2);
	}
}
function getTileShiftedPositionFromIndices (%i, %j)
{//echo("getTileShiftedPositionFromIndices()");
	if (isObject($mazeLiftedTiles[%i, %j]))
	{
		%positionX = %i * 4 + 2;
		%positionY = %j * 4 + 2 + $GlobalVerticalShift - $LiftedTileVerticalShift;
		return %positionX SPC %positionY;
	}
	else
	{
		%positionX = %i * 4 + 2;
		%positionY = %j * 4 + 2 + $GlobalVerticalShift;
		return %positionX SPC %positionY;
	}
}
function getLiftedTilePositionFromIndices (%i, %j)
{//echo("getLiftedTilePositionFromIndices()");
	%positionX = %i * 4 + 2;
	%positionY = %j * 4 + 2 - $LiftedTileVerticalShift;
	return %positionX SPC %positionY;
}
function getTileShiftedLiftedPositionFromIndices (%i, %j)
{//echo("getTileShiftedLiftedPositionFromIndices()");
	%positionX = %i * 4 + 2;
	%positionY = %j * 4 + 2 - $GlobalVerticalShift - $LiftedTileVerticalShift;
	return %positionX SPC %positionY;
}
function getTallIndicesFromTilePosition (%xPos, %yPos)
{//echo("getTallIndicesFromTilePosition()");
	%xIndex = (%xPos - 2) / 4;
	%yIndex = (%yPos - 2 - $LargeUnitVerticalShift) / 4;
	return mRound(%xIndex) SPC mRound(%yIndex);
}
function getIndicesFromTilePosition (%xPos, %yPos)
{//echo("getIndicesFromTilePosition()");
	%xIndex = (%xPos - 2) / 4;
	%yIndex = (%yPos - 2) / 4;
	return mRound(%xIndex) SPC mRound(%yIndex);
}
function getIndicesFromLiftedTilePosition (%xPos, %yPos)
{//echo("getIndicesFromLiftedTilePosition()");
	%xIndex = mRound((%xPos - 2) / 4);
	%yIndex = mRound((%yPos - 2 + $LiftedTileVerticalShift) / 4);
	return mRound(%xIndex) SPC mRound(%yIndex);
}
function getTileIndicesFromCursor (%xPos, %yPos)
{//echo("getTileIndicesFromCursor()");
	%xIndex = (%xPos + 2) / 4;
	%yIndex = (%yPos + 2) / 4;
	return mRound(%xIndex) SPC mRound(%yIndex);
}