// Handles stairs dialog.
// Worthless Bums 2012.


// Handles squad uncovering the stairs.
function squadFoundStairs ()
{//echo("squadFoundStairs()");
	if (!$squadFoundStairs)
	{
		if ($generateTutorialLevel)
		{
			if ($tutorialSection <= 8)
			{
				tutorialMessage9();
			}
		}
		$squadFoundStairs = true;
		giveSpottingXP();
		updateEventText($whiteColor @ "At last you spot the elevator.");
		placeElevatorZone();
		// NOTE: Dungeon levels specific to a deck may change so don't
		//		 just wrap a switch statement in an if statement here.
		if ($overworldLocation $= "Command Deck"			&&
			$dungeonLevel == 5)
		{
			hulkIntro();
		}
		else if ($overworldLocation $= "Engineering Deck"	&&
				 $dungeonLevel == 5)
		{
			revenantIntro();
		}
		else if ($overworldLocation $= "Medical Deck"		&&
				 $dungeonLevel == 5)
		{
			brainienIntro();
		}
		else if ($overworldLocation $= "Armory Deck"		&&
				 $dungeonLevel == 5)
		{
			gunshipIntro();
		}
		else if ($overworldLocation $= "Bridge Deck"		&&
				 $dungeonLevel == 5)
		{
			matrixIntro();
		}
		else if ($overworldLocation $= "Alien Deck"			&&
				 $dungeonLevel == 5)
		{
			patriarchIntro();
		}
	}
}

// Hulk intro.
function hulkIntro ()
{//echo("hulkIntro()");
	schedule(10, 0, "engageCharacterActionLock");
	schedule(1000, 0, "moveCameraToPosition", $stairsPositionX, $stairsPositionY, 3);
	schedule(3500, 0, "spawnHulk");
	schedule(5000, 0, "remountCameraToCharacter", $activeCharacter);
	schedule(6000, 0, "releaseCharacterActionLock");
	fadeOut(0);
	schedule(1000, 0, "stopBGMOgg");
	schedule(4500, 0, "fadeIn", 1);
	schedule(4500, 0, "playBGMOgg", "Audio:actionLoop", MusicAudioVolume.value);
}
// Revenant intro.
function revenantIntro ()
{//echo("revenantIntro()");
	schedule(10, 0, "engageCharacterActionLock");
	schedule(1000, 0, "moveCameraToPosition", $stairsPositionX, $stairsPositionY, 3);
	schedule(3500, 0, "spawnRevenant");
	schedule(5000, 0, "remountCameraToCharacter", $activeCharacter);
	schedule(6000, 0, "releaseCharacterActionLock");
	fadeOut(0);
	schedule(1000, 0, "stopBGMOgg");
	schedule(4500, 0, "fadeIn", 1);
	schedule(4500, 0, "playBGMOgg", "Audio:actionLoop", MusicAudioVolume.value);
}
// Brainien intro.
function brainienIntro ()
{//echo("brainienIntro()");
	schedule(10, 0, "engageCharacterActionLock");
	schedule(1000, 0, "moveCameraToPosition", $stairsPositionX, $stairsPositionY, 3);
	schedule(3500, 0, "spawnBrainien");
	schedule(5000, 0, "remountCameraToCharacter", $activeCharacter);
	schedule(6000, 0, "releaseCharacterActionLock");
	fadeOut(0);
	schedule(1000, 0, "stopBGMOgg");
	schedule(4500, 0, "fadeIn", 1);
	schedule(4500, 0, "playBGMOgg", "Audio:actionLoop", MusicAudioVolume.value);
}
// Gunship intro.
function gunshipIntro ()
{//echo("gunshipIntro()");
	schedule(10, 0, "engageCharacterActionLock");
	schedule(1000, 0, "moveCameraToPosition", $stairsPositionX, $stairsPositionY, 3);
	schedule(3500, 0, "spawnGunship");
	schedule(5000, 0, "remountCameraToCharacter", $activeCharacter);
	schedule(6000, 0, "releaseCharacterActionLock");
	fadeOut(0);
	schedule(1000, 0, "stopBGMOgg");
	schedule(4500, 0, "fadeIn", 1);
	schedule(4500, 0, "playBGMOgg", "Audio:actionLoop", MusicAudioVolume.value);
}
// Matrix intro.
function matrixIntro ()
{//echo("matrixIntro()");
	schedule(10, 0, "engageCharacterActionLock");
	schedule(1000, 0, "moveCameraToPosition", $stairsPositionX, $stairsPositionY, 3);
	schedule(3500, 0, "spawnMatrix");
	schedule(5000, 0, "remountCameraToCharacter", $activeCharacter);
	schedule(6000, 0, "releaseCharacterActionLock");
	fadeOut(0);
	schedule(1000, 0, "stopBGMOgg");
	schedule(4500, 0, "fadeIn", 1);
	schedule(4500, 0, "playBGMOgg", "Audio:actionLoop", MusicAudioVolume.value);
}
// Patriarch intro.
function patriarchIntro ()
{//echo("patriarchIntro()");
	schedule(10, 0, "engageCharacterActionLock");
	schedule(1000, 0, "moveCameraToPosition", $stairsPositionX, $stairsPositionY, 3);
	schedule(3500, 0, "spawnPatriarch");
	schedule(5000, 0, "remountCameraToCharacter", $activeCharacter);
	schedule(6000, 0, "releaseCharacterActionLock");
	fadeOut(0);
	schedule(1000, 0, "stopBGMOgg");
	schedule(4500, 0, "fadeIn", 1);
	schedule(4500, 0, "playBGMOgg", "Audio:actionLoop", MusicAudioVolume.value);
}
// Moves the camera to the specified location at the specified speed.
function moveCameraToPosition (%positionX, %positionY, %speed)
{//echo("moveCameraToPosition()");
	sceneWindow2D.dismount();
	sceneWindow2D.setTargetCameraPosition(%positionX, %positionY);
	sceneWindow2D.startCameraMove(%speed);
}
// Moves and remounts the camera to the specified character.
function remountCameraToCharacter (%characterIndex)
{//echo("remountCameraToCharacter()");
	sceneWindow2D.mount($playerSquad[%characterIndex], $sceneWindow2DxOffset SPC $sceneWindow2DyOffset, $cameraMountForce, false);
	$sceneWindowMountParent = $playerSquad[%characterIndex];
}

// Continue in the dungeon.
function stairsDialogDescendButton ()
{//echo("stairsDialogDescendButton()");
	popStairsDialog();
	playAudio("Audio:mainMenuButton", EffectsAudioVolume.value);
	syncDungeonLevels();
	Canvas.hideCursor();
	descendStairsCamera();
	schedule(1, 0, "pushLoadingDungeon");
	schedule(1250, 0, "cameraReset");
	$nextDungeonLevelEvent = schedule(1500, 0, "nextDungeonLevel");
	waitDungeonLevelEvent();
}

// Leave dungeon and go to the Overworld map.
function stairsDialogOverworldButton ()
{//echo("stairsDialogOverworldButton()");
	popStairsDialog();
	playRandomFootstepSfx();
	pushOverworld();
}

// Initialize Stairs Dialog.
function initializeStairsDialog ()
{//echo("initializeStairsDialog()");
	// Need separate dungeon levels!
	stairsDialogText.setText("You smell fresh air . . .\n\nDo you wish to descend further into the dungeon or return to the Overworld?");
}

// Pushes stairs dialog.
function pushStairsDialog ()
{//echo("pushStairsDialog()");
	// Play stairs dialog sound effect.
	playAudio("Audio:mainMenuButton", EffectsAudioVolume.value);
	
	// Initialize stairs dialog.
	// NOTE: Should initialization be handled here or before this function call?
	initializeStairsDialog();
	$timescale = 0;
	Canvas.pushDialog(stairsDialog);
	$stairsDialogPushed = true;
}

// Pops stairs dialog.
function popStairsDialog ()
{//echo("popStairsDialog()");
	$timescale = 1;
	Canvas.popDialog(stairsDialog);
	$stairsDialogPushed = false;
}

// Synchronizes dungeon levels as the player progresses deeper into dungeons.
function syncDungeonLevels ()
{//echo("syncDungeonLevels()");//echo("$overworldLocation =" SPC $overworldLocation);
	// Dispatch by dungeon specified in $overworldLocation.
	switch$ ($overworldLocation)
	{
		case "Forgotten Ruins":	$dungeonLevel = $forgottenRuinsLevel;	// Obsolete?!?
		case "Command Deck":	$dungeonLevel = $commandDeckLevel;
		case "Volcano Lair":	$dungeonLevel = $volcanoLairLevel;		// Obsolete?!?
		case "Ice Fortress":	$dungeonLevel = $iceFortressLevel;		// Obsolete?!?
	}
}

// Increments and Synchronizes dungeon levels as the player progresses deeper into dungeons.
function incrementAndSyncDungeonLevels ()
{//echo("incrementAndSyncDungeonLevels()");
//echo("$overworldLocation =" SPC $overworldLocation);
	// Dispatch by dungeon specified in $overworldLocation.
	switch$ ($overworldLocation)
	{
		case "Command Deck":
			$commandDeckLevel++;
			$dungeonLevel = $commandDeckLevel;
			
			// Switch decks after so many levels!
			if ($dungeonLevel > $levelsPerDeck)
			{
				$commandDeckLevel--;			// Hackerish... best to refactor this code later!
				$dungeonLevel = 1;
				//$engineeringDeckLevel = 1;
				$overworldLocation = "Engineering Deck";
			}
			
		case "Engineering Deck":
			$engineeringDeckLevel++;
			$dungeonLevel = $engineeringDeckLevel;
			
			// Switch decks after so many levels!
			if ($dungeonLevel > $levelsPerDeck)
			{
				$engineeringDeckLevel--;		// Hackerish... best to refactor this code later!
				$dungeonLevel = 1;
				//$medicalDeckLevel = 1;
				$overworldLocation = "Medical Deck";
			}
			
		case "Medical Deck":
			$medicalDeckLevel++;
			$dungeonLevel = $medicalDeckLevel;
			
			// Switch decks after so many levels!
			if ($dungeonLevel > $levelsPerDeck)
			{
				$medicalDeckLevel--;		// Hackerish... best to refactor this code later!
				$dungeonLevel = 1;
				//$armoryDeckLevel = 1;
				$overworldLocation = "Armory Deck";
			}
			
		case "Armory Deck":
			$armoryDeckLevel++;
			$dungeonLevel = $armoryDeckLevel;
			
			// Switch decks after so many levels!
			if ($dungeonLevel > $levelsPerDeck)
			{
				$armoryDeckLevel--;		// Hackerish... best to refactor this code later!
				$dungeonLevel = 1;
				//$bridgeDeckLevel = 1;
				$overworldLocation = "Bridge Deck";
			}
			
		case "Bridge Deck":
			$bridgeDeckLevel++;
			$dungeonLevel = $bridgeDeckLevel;
			
			// Switch decks after so many levels!
			if ($dungeonLevel > $levelsPerDeck)
			{
				$bridgeDeckLevel--;		// Hackerish... best to refactor this code later!
				$dungeonLevel = 1;
				//$bridgeDeckLevel = 1;
				$overworldLocation = "Alien Deck";
			}
			
		case "Alien Deck":
			$alienDeckLevel++;
			$dungeonLevel = $alienDeckLevel;
			
			// Switch decks after so many levels!
			if ($dungeonLevel > $levelsPerDeck)
			{
				$alienDeckLevel--;		// Hackerish... best to refactor this code later!
				$dungeonLevel = 1;
				//$alienDeckLevel = 1;
				$overworldLocation = "I.S.S. Orion";
			}
	}
}

// Reset deck levels for New Game Plus.
function resetNewGamePlusDeckLevels ()
{//echo("resetNewGamePlusDeckLevels()");
	$overworldLocation = "Command Deck";
	$dungeonLevel = 1;
	$commandDeckLevel = 1;
	$engineeringDeckLevel = 1;
	$medicalDeckLevel = 1;
	$armoryDeckLevel = 1;
	$bridgeDeckLevel = 1;
	$alienDeckLevel = 1;
}

// Places "elevator tiles" close to the elevator.
function placeElevatorZone ()
{//echo("placeElevatorZone()");
	if (isObject($elevatorZone))
	{
		for (%i = 0; %i < $elevatorZone.getCount(); %i++)
		{
			($elevatorZone.getObject(%i)).safeDelete();
		}
	}
	$elevatorZone = new SimSet();
	
	for (%i = -1 * $elevatorZoneTileRange + $stairsXIndex; %i <= $elevatorZoneTileRange + $stairsXIndex; %i++)
	{
		for (%j = -1 * $elevatorZoneTileRange + $stairsYIndex; %j <= $elevatorZoneTileRange + $stairsYIndex; %j++)
		{
			if (!isObject($mazeRoom[%i, %j])	||
				$mazeRoom[%i, %j].status == -3	||
				$mazeRoom[%i, %j].status == -4)
			{
				%elevatorZoneAnimation = createElevatorZoneAnimation();
				scheduleObjectMove(%elevatorZoneAnimation, getTileShiftedPositionFromIndices(%i, %j), 0);
				%elevatorZoneAnimation.xIndex = %i;
				%elevatorZoneAnimation.yIndex = %j;
				$elevatorZone.add(%elevatorZoneAnimation);
			}
		}
	}
}

// Checks if the player is on stairs. Called by enterLevel() in input.cs.
function isPlayerOnStairs ()
{//echo("isPlayerOnStairs()");
	// NOTE: This should probably be done by tile index rather than position! Both should work, however.
	// Check the current player position against the stairs position.
	if (mFloor($playerSquad[$activeCharacter].getPositionX()) == mFloor($stairsPositionX - $GlobalHorizontalShift)	&&
		mFloor($playerSquad[$activeCharacter].getPositionY()) == mFloor($stairsPositionY - $GlobalVerticalShift))
	{
		if ($elevatorPowered)
		{
			return true;
		}
		else
		{
			updateEventText($whiteColor @ "Elevator not powered" @ $defaultColor @ "!");
			return false;
		}
	}
	else
	{
		return false;
	}
}

// Returns true iff the specified indices is the stairs position.
function isTileStairs (%xIndex, %yIndex)
{//echo("isTileStairs()");
	if ($stairsXIndex == %xIndex	&&
		$stairsYIndex == %yIndex)
	{
		return true;
	}
	return false;
}