
///////////////////////////////////////////////////////////////////////////////
//
// File   : AVP_Predator_AIAnimStateData.cpp
// Author : Will Plano
// Date   : 27/09/2007
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

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

#include "Core/Asura.h"

// This file's header...
#include "Actor/Vertebrates/Predator/AVP_Predator_AIAnimStateData.h"

// Asura
#include "Blueprint/Asura_Blueprint_System.h"
#include "Core/Asura_Hash.h"
#include "Dynamics/Asura_Collision_Object.h"

// AVP
#include "Actor/AVP_Actor_AnimStateData.h"
#include "Blueprint/AVP_Blueprint_HashIDs.h"
#include "Physics/AVP_Collision_Descriptor.h"

#ifdef ASURA_TOOLS
#include "Blueprint/ToolsAVP_Blueprint_AnimSpreadsheetExporter.h"
#endif

Asura_TemplateHashTree< AVP_Predator_AIAnimStateData::AVP_Predator_AIAnimWeaponStateData > AVP_Predator_AIAnimStateData::s_xHashTree;

AVP_Predator_AIAnimStateData::AVP_Predator_AIAnimWeaponStateData::AVP_Predator_AIAnimWeaponStateData()
{
	for ( u_int uStateItr = 0; uStateItr < AVP_Predator_AIAnimStateMachine::PREDATOR_STATE_END; ++uStateItr )
	{
		m_apxAnimStateData[ uStateItr ] = NULL;
	}
}

AVP_Predator_AIAnimStateData::AVP_Predator_AIAnimWeaponStateData::~AVP_Predator_AIAnimWeaponStateData()
{
	for ( u_int uStateItr = 0; uStateItr < AVP_Predator_AIAnimStateMachine::PREDATOR_STATE_END; ++uStateItr )
	{
		delete m_apxAnimStateData[ uStateItr ];
	}
}

void AVP_Predator_AIAnimStateData::AVP_Predator_AIAnimWeaponStateData::InitialiseFromBlueprint( const Asura_Hash_ID uAnimStateBlueprintType, const Asura_Hash_ID uWeaponBlueprintHash )
{
	const Asura_Blueprint* pxWeaponBlueprint = Asura_Blueprint_System::GetBlueprintByHashID( uAVP_BLUEPRINT_TYPE_INVENTORY_ITEMS, uWeaponBlueprintHash );
	Asura_Assertf( pxWeaponBlueprint, ( "Unable to get weapon blueprint 0x%08x", uWeaponBlueprintHash ) );
	if ( pxWeaponBlueprint )
	{
		Asura_Value xValue;
		if ( pxWeaponBlueprint->GetParameterValueByHashID( uAVP_BLUEPRINT_PARAM_ANIMSTATE_BLUEPRINT_TP, xValue ) )
		{
			Asura_Hash_ID uAIAnimStateBPHash = xValue.GetHashIDValue();

			const Asura_Blueprint* pxStateBlueprint = Asura_Blueprint_System::GetBlueprintByHashID( uAnimStateBlueprintType, uAIAnimStateBPHash );
			Asura_Assertf( pxStateBlueprint, ( "Unable to get state blueprint 0x%08x", uAIAnimStateBPHash ) );
			if ( pxStateBlueprint )
			{
#ifdef ASURA_TOOLS
				if ( ToolsAVP_Blueprint_AnimSpreadsheetExporter::IsActive() )
				{
					ToolsAVP_Blueprint_AnimSpreadsheetExporter::CreateFile( ( Asura_String( "Preadtor_" ) + pxStateBlueprint->GetName() + ".csv" ).GetString() );
				}
#endif
				u_int uStateItr = 0;
				for ( ; uStateItr < AVP_Predator_AIAnimStateMachine::ACTOR_STATE_IDLE; ++uStateItr )
				{
					m_apxAnimStateData[ uStateItr ] = NULL;
				}

				for ( ; uStateItr < AVP_Predator_AIAnimStateMachine::PREDATOR_STATE_END; ++uStateItr )
				{
					if ( AVP_Predator_AIAnimStateMachine::IsDirectionalState( uStateItr ) )
					{
						m_apxAnimStateData[ uStateItr ] = new AVP_Actor_AnimStateData_Directional();
						m_apxAnimStateData[ uStateItr ]->InitialiseFromBlueprint( uAnimStateBlueprintType, *pxStateBlueprint, AVP_Predator_AIAnimStateMachine::GetStateDescriptionStatic( uStateItr ), AVP_Vertebrate_AIAnimStateMachine::IsStateRequired( uStateItr ) );
					}
					else
					{
						m_apxAnimStateData[ uStateItr ] = new AVP_Actor_AnimStateData_NonDirectional();
						m_apxAnimStateData[ uStateItr ]->InitialiseFromBlueprint( uAnimStateBlueprintType, *pxStateBlueprint, AVP_Predator_AIAnimStateMachine::GetStateDescriptionStatic( uStateItr ), AVP_Vertebrate_AIAnimStateMachine::IsStateRequired( uStateItr ) );
					}
#ifdef ASURA_TOOLS
					if ( ToolsAVP_Blueprint_AnimSpreadsheetExporter::IsActive() )
					{
						ToolsAVP_Blueprint_AnimSpreadsheetExporter::WriteState( AVP_Predator_AIAnimStateMachine::GetStateDescriptionStatic( uStateItr ), m_apxAnimStateData[ uStateItr ], pxStateBlueprint, uAnimStateBlueprintType );
					}
#endif
				} // end state loop
			}
		}
#ifdef ASURA_ASSERT
		else
		{
			Asura_Assertf( false, ( "Unable to get actor state blueprint parameter from weapon blueprint 0x%08x", uWeaponBlueprintHash ) );
		}
#endif
	}
}

const AVP_Actor_AnimStateData* AVP_Predator_AIAnimStateData::GetAnimStateData( const Asura_Hash_ID uAnimStateBlueprintType, const Asura_Hash_ID uCurrentWeaponHash, u_int uState )
{
	Asura_Assert( uState < AVP_Predator_AIAnimStateMachine::PREDATOR_STATE_END, "Invalid state" );
	if ( uState < AVP_Predator_AIAnimStateMachine::PREDATOR_STATE_END )
	{
		const AVP_Predator_AIAnimWeaponStateData* pxWeaponData = GetWeaponDataByHash( uAnimStateBlueprintType, uCurrentWeaponHash );
		Asura_Assert( pxWeaponData != NULL, "No weapon state data" );
		if ( pxWeaponData != NULL )
		{
			return pxWeaponData->m_apxAnimStateData[ uState ];
		}
	} // else
	return NULL;
}

const AVP_Predator_AIAnimStateData::AVP_Predator_AIAnimWeaponStateData* AVP_Predator_AIAnimStateData::GetWeaponDataByHash( const Asura_Hash_ID uAnimStateBlueprintType, const Asura_Hash_ID uHash )
{
	AVP_Predator_AIAnimWeaponStateData* pxData = s_xHashTree.GetDataByHash( uHash, false );
	if ( pxData == NULL && uHash != ASURA_HASH_ID_UNSET )
	{
		pxData = s_xHashTree.AddNode( uHash );
		Asura_Assert( pxData != NULL, "Unable to add new anim state data to tree" );
		if ( pxData != NULL )
		{
			pxData->InitialiseFromBlueprint( uAnimStateBlueprintType, uHash );
		}
	}
	return pxData;
}

/// End of file. ///
