
///////////////////////////////////////////////////////////////////////////////
//
// File   : AVP_CoreState_Tools.cpp
// Author : Anthony Gregson
// Date   : 21/11/07
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#if defined(ASURA_TOOLS)

// This file's header...
#include "Core/States/Core/AVP_CoreState_Tools.h"

// Asura independent header-files...
#include "Network/Online/Asura_Lobby.h"

// Game independent header-files...
#include "Core/AVP_Core.h"
#include "Core/AVP_Settings.h"
#include "Core/AVP_StateMachine_Core.h"
#include "FrontEnd/AVP_DevLobby.h"
#include "FrontEnd/AVP_GameLobby.h"
#include "Interface/Asura_CommandConsole.h"
#include "Multiplayer/AVP_Multiplayer_GameMessageSystem.h"

// Tools header-files...
#include "Tools_System.h"
#include "Load&Save/LevelExport/Tools_Level_Export.h"
#include "Load&Save/Tools_SaveAsura.h"

// AVP Tools header-files...
#include "ToolsAVP_System.h"


////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_Tools::Enter
// Author       : Andy Tate
// Date         : 12-Mar-2007
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_Tools::Enter(const u_int uPreviousState)
{

#ifdef ASURA_SUPPORT_COMMAND_CONSOLE					// EP. 17.Nov.2008. This is fix for bug: 0063879
	// Close the console before entering a tool.
	if ( Asura_CommandConsole::IsConsoleDown() )	
	{ 
		Asura_CommandConsole::Hide( true, true );
	}
#endif // #ifdef ASURA_SUPPORT_COMMAND_CONSOLE			// End of fix for bug 0063879

#if defined(ASURA_DEBUG_VARIABLE_SYSTEM)
	Asura_DebugVariableSystem::CloseDisplayDVS();
#endif	// defined(ASURA_DEBUG_VARIABLE_SYSTEM)

	if (!ToolsAVP_System::IsCancelQuitRequested())
	{
		const bool	bLoadEnvironment = (uPreviousState == AVP_StateMachine_Core::STATE_LOADINGTOOLS);

		AVP_Core::InitialiseEffects();
		AVP_Core::InitialiseNetwork(AVP_Settings::GetExternalNetworkActive());
		AVP_Core::SetToolsRequested(false);

#ifdef ASURA_SUPPORT_LOBBY
		if (bLoadEnvironment)
		{
			Asura_Lobby_System::AddConsoleCmdsAndVars();

#if defined(AVP_USE_DEV_LOBBY)
			AVP_DevLobby::AddConsoleCmdsAndVars();
#endif	//defined(AVP_USE_DEV_LOBBY)

#if defined(AVP_USE_GAME_LOBBY)
			AVP_GameLobby::AddConsoleCmdsAndVars();
#endif	//defined(AVP_USE_DEV_LOBBY)

			AVP_Multiplayer_GameMessageSystem::AddConsoleCmdsAndVars(); 
		}
#endif
		Tools_System::Initialise(bLoadEnvironment);
	}
	else
	{
		// Make sure the quit bool is cleared
		Tools_System::ReadyToQuitOut = false;
	}

	ToolsAVP_System::SetRequestCancelQuit(false);
}

/// End of function 'AVP_CoreState_Tools::Enter'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_Tools::Update
// Author       : Andy Tate
// Date         : 12-Mar-2007
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_Tools::Update()
{
	if(!Tools_System::MainLoop())
	{
		const bool	bQuitToGame	= !Tools_System::ShouldQuitToDesktop();

		if(bQuitToGame)
		{
			GetParentMachine().ChangeState(AVP_StateMachine_Core::STATE_GAME);
		}
		else
		{
			GetParentMachine().ChangeState(AVP_StateMachine_Core::STATE_SHUTTINGDOWN);
		}
	}
}

/// End of function 'AVP_CoreState_Tools::Update'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_CoreState_Tools::Exit
// Author       : Andy Tate
// Date         : 12-Mar-2007
////////////////////////////////////////////////////////////////////////////////

void AVP_CoreState_Tools::Exit(const u_int uNextState)
{
	ToolsAVP_System::SetRequestCancelQuit(false);

	if(Tools_SaveAsura::GetFileType()==AsuraFile_Environment || Tools_SaveAsura::GetFileType()==AsuraFile_Template)
	{
		if(!Tools_System::ShouldQuitToDesktop())
		{
			Tools_Modal_Overlay*	pxOverlay	= new ToolsAVP_QuitToGameOverlay;

			if(pxOverlay != NULL)
			{
				pxOverlay->ShowOverlay();
				delete(pxOverlay);
			}
		}
	}

	if (!ToolsAVP_System::IsCancelQuitRequested())
	{
		Tools_System::Shutdown();
		AVP_Core::ShutdownNetwork(); // The game will re-initialise this
		AVP_Core::ShutdownEffects();
	}
}

/// End of function 'AVP_CoreState_Tools::Exit'. ///

#endif	// defined(ASURA_TOOLS)


/// End of file. ///
