// Overworld Handler.
// Worthless Bums 2012.


/*
 *	The Overworld should be a static map unlike dungeon levels.
 *	Planned features:
 *		- Towns/Quest hubs.
 *		- Dungeons you can enter/descend into.
 *		- Random encounters.
 *		- Exploration/Secrets.
 *		- Squad movement (as opposed to dungeon movement where each squad member moves individually).
 *		- Surface areas you can clear for rewards/quests.
 *		- Dynamic NPC population.
 *
 *	Need to keep track of all non-static locations/objects on the Overworld such as:
 *		- Enemies.
 *		- Trading caravans.
 *
 *
*/

// Tests Overworld Map functionality.
function to ()
{//echo("to()");
	pushOverworld();
}
// Tests static sprites on Overworld map gui.
function tss ()
{//echo("tss()");
	%t2dContainer = new guiT2DObjectCtrl()
	{
		RenderMargin = 3;
		extent = "100 100";
		position = "0 0";
		toolType = "t2dSceneObject";
		tooltip = "Scene Object";
		toolTipProfile = "EditorToolTipProfile";
	};
	
	%sceneGraph = new t2dSceneGraph();
	%sceneObject = new Sprite()
	{
		//scenegraph = %sceneGraph;
		//datablock = %datablock;
		SceneLayer = 0;
		position = "0 0";
		scenegraph = %sceneGraph;//OverworldMap;//levelSceneGraph;
		imagemap = "Spawning_HoleImageMap";
		visible = true;
		size = "32 32";
	};
	addObjectToScene(%sceneObject);
	//%sceneObject = new Sprite() { scenegraph = %Scenegraph; animation= %datablock};
	
	// Set scene object in the container.
	%t2dContainer.setSceneObject(%sceneObject);
	
	// Add the container to the gui control.
	OverworldMap.add(%t2dContainer);
}


// Set Overworld Map Text.
function setOverworldMapText ()
{//echo("setOverworldMapText()");
	//OverworldText.setText("Location: " @ $overworldLocation);
	
	// Dispatch by dungeon specified in $overworldLocation.
	switch$ ($overworldLocation)
	{
		case "Forgotten Ruins":
			OverworldText.setText("Location: " @ $overworldLocation	NL
								  "Dungeon Level: " @ $forgottenRuinsLevel);
			
		case "Command Deck":
			OverworldText.setText("Location: " @ $overworldLocation	NL
								  "Dungeon Level: " @ $commandDeckLevel);
			
		case "Volcano Lair":
			OverworldText.setText("Location: " @ $overworldLocation	NL
								  "Dungeon Level: " @ $volcanoLairLevel);
			
		case "Ice Fortress":
			OverworldText.setText("Location: " @ $overworldLocation	NL
								  "Dungeon Level: " @ $iceFortressLevel);
	}
}

// Enters the squad's current Overmap location.
function enterSquadOvermapLocation ()
{//echo("enterSquadOvermapLocation()");
	switch$ ($overworldLocation)
	{
		case "Forgotten Ruins":
			forgottenRuins();
			
		case "Command Deck":
			commandDeck();
			
		case "Volcano Lair":
			volcanoLair();
			
		case "Ice Fortress":
			iceFortress();
	}
}

// Sets the bitmap for the squad icon to the currently active character.
function setSquadIconBitmap ()
{//echo("setSquadIconBitmap()");
	%imageMap = ($playerSquad[$activeCharacter].armor).imageMapName;
	//echo("%imageMap =" SPC %imageMap);
	%bitmapLength = strlen(%imageMap);
	//echo("%bitmapLength =" SPC %bitmapLength);
	if (%bitmapLength - 8 >= 0)
	{
		%bitmap = getSubStr(%imageMap, 0, %bitmapLength - 8);// Trim "ImageMap" from the end.
		SquadIcon.bitmap = "../../../Sprites/1/assets/images/" @ %bitmap;
	}
	else
	{
		// Really bad. Should we default to a different avatar is this occurs?
		SquadIcon.bitmap = "../../../Sprites/1/assets/images/blank (1x1)";
	}
	SquadIcon.setVisible(true);
}

// Handles the Overworld squad.
function handleOverworldSquad ()
{//echo("handleOverworldSquad()");
//echo("$overworldLocation =" SPC $overworldLocation);
	// Grab the squad's current location position.
	switch$ ($overworldLocation)
	{
		case "Forgotten Ruins":
			%squadPositionX = getWord(ForgottenRuins.position, 0) + ForgottenRuins.xOffset;
			%squadPositionY = getWord(ForgottenRuins.position, 1) + ForgottenRuins.yOffset;
			
		case "Command Deck":
			%squadPositionX = getWord(CommandDeck.position, 0) + CommandDeck.xOffset;
			%squadPositionY = getWord(CommandDeck.position, 1) + CommandDeck.yOffset;
			
		case "Volcano Lair":
			%squadPositionX = getWord(VolcanoLair.position, 0) + VolcanoLair.xOffset;
			%squadPositionY = getWord(VolcanoLair.position, 1) + VolcanoLair.yOffset;
			
		case "Ice Fortress":
			%squadPositionX = getWord(IceFortress.position, 0) + IceFortress.xOffset;
			%squadPositionY = getWord(IceFortress.position, 1) + IceFortress.yOffset;
	}
	
	// Make sure the icon doesn't exist before proceeding.
	if (!isObject(SquadIcon))
	{
		%squadIcon = new GuiBitmapButtonCtrl(SquadIcon)
		{
			canSaveDynamicFields = "0";
			isContainer = "0";
			Profile = "GuiDefaultProfile";
			HorizSizing = "relative";
			VertSizing = "relative";
			Position = %squadPositionX SPC %squadPositionY;
			Extent = "32 32";//"16 16";
			MinExtent = "8 2";
			canSave = "1";
			Visible = "1";
			hovertime = "1000";
			groupNum = "-1";
			buttonType = "PushButton";
			useMouseEvents = "1";
			command = "enterSquadOvermapLocation();";
			bitmap = "../../../Sprites/1/assets/images/Leader (64x64)";
		};
		// Set new bitmap if necessary.
		setSquadIconBitmap();
		// Add the squad icon to the Overworld map.
		OverworldMap.add(%squadIcon);
	}
	else
	{
		// Set new bitmap if necessary.
		setSquadIconBitmap();
		// Set squad position on Overworld map.
		SquadIcon.setPosition(%squadPositionX, %squadPositionY);
	}
	
	// Set Overworld Map Text.
	setOverworldMapText();
	
	// Play overworld location selection sound effect.
	//playAudio("Audio:overworldLocationSelection", EffectsAudioVolume.value);
}

// Pushes Overworld.
function pushOverworld ()
{//echo("pushOverworld()");
	// Flag as being on Overworld map.
	$onOverworldMap = 1;
	
	// Save current game.
	//saveCurrentGame();
	
	// Set Overworld Map Text.
	setOverworldMapText();
	
	// Pop explore gui.
	Canvas.popDialog(exploreGui);		// Make sure to push this later!
	// Show cursor.
	Canvas.showCursor();
	
	// Stop any already playing music.
	stopBGMOgg();
	// Play Overworld music.
	playBGMOgg("Audio:Overworld", MusicAudioVolume.value);
	
	// Pause the game.
	$timescale = 0;
	
	// Handle the squad on the Overworld map.
	handleOverworldSquad();
	
	// Push the Overworld Map.
	Canvas.pushDialog(overworldMap);
}
// Pops Overworld.
function popOverworld ()
{//echo("popOverworld()");
	$onOverworldMap = 0;
	// Pop the Overworld Map.
	Canvas.popDialog(overworldMap);
}

// Overworld Button Commands.

// Forgotten Ruins pushed.
function forgottenRuins ()
{//echo("forgottenRuins()");
	// Clicking on the location the player is currently on enters that location.
	if ($overworldLocation $= "Forgotten Ruins")
	{
		enterForgottenRuins();
	}
	
	// No map connections atm - any location is accessible from all other locations.
	$overworldLocation = "Forgotten Ruins";
	//echo("Moving to Forgotten Ruins!");
	handleOverworldSquad();
	
	return;
	
	// Only move to the Forgotten Ruins if the player is at a connected location!
	switch$ ($overworldLocation)
	{
		// North.
		
		// South.
		
		// East.
		
		// West.
		case "Command Deck":
			$overworldLocation = "Forgotten Ruins";
			//echo("Moving to Forgotten Ruins!");
			handleOverworldSquad();
	}
}
// Enter the Forgotten Ruins.
function enterForgottenRuins ()
{//echo("enterForgottenRuins()");
	// Make sure this is set correctly.
	$overworldLocation = "Forgotten Ruins";
	testStairsFromOverworld();
}

// Command Deck pushed.
function commandDeck ()
{//echo("commandDeck()");
	// Clicking on the location the player is currently on enters that location.
	if ($overworldLocation $= "Command Deck")
	{
		enterCommandDeck();
	}
	
	// No map connections atm - any location is accessible from all other locations.
	$overworldLocation = "Command Deck";
	//echo("Moving to Command Deck!");
	handleOverworldSquad();
	
	return;
	
	// Only move to Command Deck if the player is at a connected location!
	switch$ ($overworldLocation)
	{
		// North.
		
		// South.
		
		// East.
		case "Forgotten Ruins":
			$overworldLocation = "Command Deck";
			//echo("Moving to Command Deck!");
			handleOverworldSquad();
			
		// West.
	}
}
// Enter Command Deck.
function enterCommandDeck ()
{//echo("enterCommandDeck()");
	// Make sure this is set correctly.
	$overworldLocation = "Command Deck";
	testStairsFromOverworld();
}

// Volcano Lair pushed.
function volcanoLair ()
{//echo("volcanoLair()");
	// Clicking on the location the player is currently on enters that location.
	if ($overworldLocation $= "Volcano Lair")
	{
		enterVolcanoLair();
	}
	
	// No map connections atm - any location is accessible from all other locations.
	$overworldLocation = "Volcano Lair";
	//echo("Moving to Volcano Lair!");
	handleOverworldSquad();
}
// Enter Volcano Lair.
function enterVolcanoLair ()
{//echo("enterVolcanoLair()");
	// Make sure this is set correctly.
	$overworldLocation = "Volcano Lair";
	testStairsFromOverworld();
}

// Ice Fortress pushed.
function iceFortress ()
{//echo("iceFortress()");
	// Clicking on the location the player is currently on enters that location.
	if ($overworldLocation $= "Ice Fortress")
	{
		enterIceFortress();
	}
	
	// No map connections atm - any location is accessible from all other locations.
	$overworldLocation = "Ice Fortress";
	//echo("Moving to Volcano Lair!");
	handleOverworldSquad();
}
// Enter Ice Fortress.
function enterIceFortress ()
{//echo("enterIceFortress()");
	// Make sure this is set correctly.
	$overworldLocation = "Ice Fortress";
	testStairsFromOverworld();
}