
///////////////////////////////////////////////////////////////////////////////
//
// File   : AVP_ConnectionState_MapChange.cpp
// Author : Steve Jones
// Date   : 22-Mar-2007
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

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

#include "Core/Asura.h"

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

// Asura independent header-files...
#include "Core/Resources/Asura_Resource_System.h"
#include "GUIMenu/Asura_GUIMenu_System.h"
#include "Network/Online/Asura_Lobby.h"

// Game independent header-files...
#include "Actor/Vertebrates/Alien/AVP_Alien_PlayerAnimStateData.h"
#include "Actor/Vertebrates/Human/AVP_Human_PlayerAnimStateData.h"
#include "Actor/Vertebrates/Predator/AVP_Predator_PlayerAnimStateData.h"
#include "Core/AVP_Connection.h"
#include "Core/AVP_Core.h"
#include "Core/AVP_Game_Core.h"
#include "FrontEnd/AVP_GameLobby.h"
#include "LoadAndSave/AVP_WeaponData_System.h"
#include "Multiplayer/AVP_Multiplayer_GameCore.h"

////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

void AVP_ConnectionState_MapChange::Enter(const u_int uPreviousState)
{
#if defined(AVP_USE_GAME_LOBBY)
	// TODO - work out what that next map should be. Depends on if this is single player, if we're the host, or if we're a client.
	if (Asura_Lobby_System::IsInitialised())
	{
		const Asura_Lobby* pxLobby = Asura_Lobby_System::GetLobby();
		if (pxLobby)
		{
			// get lobby attributes
			//int iGametype = pxLobby->GetAttributes().GetIntAttribute(AVP_LOBBY_ATTRIBUTE_GAMETYPE);
			int iLevel = AVP_GameLobby::GetMapIndexByHash(pxLobby->GetAttributes().GetIntAttribute(AVP_LOBBY_ATTRIBUTE_MAP));
			const char* szLevelName = AVP_GameLobby::GetMapFilename(iLevel);

			//AVP_Settings::SetGameType( static_cast<AVP_Settings::GameType>(iGametype) );
			AVP_Settings::SetLevelName(szLevelName);
		}
	}
#endif // #if defined(AVP_USE_GAME_LOBBY)
}

void AVP_ConnectionState_MapChange::Update()
{
	// Reset all of the systems...
	AVP_Game_Core::Shutdown();
	AVP_Core::Reset();

	AVP_Alien_PlayerAnimStateData::GetSingleton().ResetStorage();
	AVP_Human_PlayerAnimStateData::GetSingleton().ResetStorage();
	AVP_Predator_PlayerAnimStateData::GetSingleton().ResetStorage();

	// Remove all resources associated with the game...
	Asura_Resource_System::DestroyResourceBank(uAVP_RESOURCE_BANK_HASH_LEVEL);
	Asura_Resource_System::DestroyResourceBank(uAVP_RESOURCE_BANK_HASH_PLAYER_PREDATOR);
	Asura_Resource_System::DestroyResourceBank(uAVP_RESOURCE_BANK_HASH_PLAYER_ALIEN);
	Asura_Resource_System::DestroyResourceBank(uAVP_RESOURCE_BANK_HASH_PLAYER_MARINE);
	Asura_Resource_System::DestroyResourceBank(uAVP_RESOURCE_BANK_HASH_LOADING);
	
	for ( u_int uCharItr = 0; uCharItr < AVP_Game_Core::NUM_CHARACTERTYPES; ++uCharItr )
	{
		AVP_Game_Core::CharacterType eCharType = static_cast < AVP_Game_Core::CharacterType >( uCharItr );
		const u_int uNumWeapons = AVP_WeaponData_System::GetNumWeaponsForCharacterType( eCharType );
		for ( u_int uWeaponItr = 0; uWeaponItr < uNumWeapons; ++uWeaponItr )
		{
			if ( AVP_WeaponData_System::GetShouldLoadWeapon( uWeaponItr, eCharType ) )
			{
				const char* szWeaponName = AVP_WeaponData_System::GetWeaponNameFromIndex( uWeaponItr, eCharType );
				Asura_Hash_ID uWeaponHash = Asura_GetHashID( szWeaponName );
				Asura_Resource_System::DestroyResourceBank( uWeaponHash );
			}
		}
	}
	AVP_WeaponData_System::ResetShouldBeLoadedData();

	//AVP_Core::ShutdownNetwork();
	//AVP_Core::ShutdownEffects();

	// Reload the loading screens
	const AVP_Settings::Species	eSpecies	= AVP_Settings::GetPlayerSpecies();
	switch(eSpecies)
	{
		case AVP_Settings::AVP_SPECIES_PREDATOR:
		{
			Asura_Resource_System::CreateResourceBank(uAVP_RESOURCE_BANK_HASH_LOADING, szAVP_RESOURCE_BANK_NAME_LOADING, uAVP_RESOURCE_BANK_TRG_COMMON);
			AVP_Core::LoadFile("GUIMenu\\Predator_Loading." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
		}
		break;

		case AVP_Settings::AVP_SPECIES_ALIEN:
		{
			Asura_Resource_System::CreateResourceBank(uAVP_RESOURCE_BANK_HASH_LOADING, szAVP_RESOURCE_BANK_NAME_LOADING, uAVP_RESOURCE_BANK_TRG_COMMON);
			AVP_Core::LoadFile("GUIMenu\\Alien_Loading." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
		}
		break;

		default:
		{
			// We have got this far and no character type has been either selected or found
			// Load in a marine for now and set the type.
			Asura_Assert(false, "Unknown character type.");
			AVP_Settings::SetPlayerSpecies(AVP_Settings::AVP_SPECIES_HUMAN);
		}
		// fall through to human

		case AVP_Settings::AVP_SPECIES_HUMAN:
		{
			Asura_Resource_System::CreateResourceBank(uAVP_RESOURCE_BANK_HASH_LOADING, szAVP_RESOURCE_BANK_NAME_LOADING, uAVP_RESOURCE_BANK_TRG_COMMON);
			AVP_Core::LoadFile("GUIMenu\\Marine_Loading." ASURA_GUIMENU_STANDARD_FILE_EXTENSION);
		}
		break;
	}

	GetParentMachine().ChangeState(AVP_Connection::STATE_LOADING);
}

/// End of file. ///
