//////////////////////////////////////////////////////////////////////////////
//
// AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire.h
//
// Copyright (C) 2008 Rebellion. All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

// Core
#include "Core/Asura.h"

// This files header
#include "Behaviour/BehaviourTypes/Human/AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire.h"

// Asura
#include "Animation/Asura_Animation.h"
#include "Animation/Asura_AnimationController_Animation.h"

#ifndef ASURA_NO_DEBUG_OUTPUT
#include "Rendering/Asura_Font_System.h"
#endif

// Axon
#include "Behaviour/BehaviourTypes/AVP_Behaviour_Types.h"

// AVP
#include "Animation/AVP_AnimFlags.h"
#include "Entity/PhysicalObject/Actor/Vertebrates/Human/AVP_ClientActor_Human.h"
#include "Entity/PhysicalObject/Actor/Vertebrates/Human/AVP_ServerActor_Human.h"
#include "Moves/AVP_Moves.h"
#include "Inventory/AVP_Inventory_Group.h"
#include "Inventory/AVP_Inventory_System.h"
#include "Inventory/AVP_InventoryItem_SubType.h"
#include "Inventory/Items/AVP_InventoryItem_Weapon.h"
#include "Inventory/Items/Weapons/AVP_InventoryWeapon_RangedWeapon.h"


//////////////////////////////////////////////////////////////////////////////
// Function Implementations
//////////////////////////////////////////////////////////////////////////////

//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire::AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire( AVP_Brain_Actor* pxBrain )
: PARENT( AXON_BEHAVIOUR_TYPE_HUMAN_GS2SPLINE_ACTOR_WITH_GUNFIRE, pxBrain )
{

}

//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire::ReadFromChunkStream( Asura_Chunk_Stream& xStream )
{
	int iVersion;
	xStream >> iVersion;

	PARENT::ReadFromChunkStream( xStream );
}

//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire::WriteToChunkStream( Asura_Chunk_Stream& xStream ) const
{
	xStream << uCURRENT_CHUNK_VERSION;

	PARENT::WriteToChunkStream( xStream );
}

//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
void AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire::PerFrameUpdate( const float fTimeslice )
{
	PARENT::PerFrameUpdate( fTimeslice );

	AVP_ServerActor_Human* pxServerActor = static_cast<AVP_ServerActor_Human*>( GetActorBrain()->GetActor() );
	AVP_ClientActor_Human* pxClientActor = AVP_ClientActor_Human::FindEntity( GetBrain()->GetOwnerGuid() );

	if ( !pxClientActor )
	{
		return;
	}

	const Asura_AnimationController_Animation*	pxAACA				= NULL;
	const AVP_Move*								pxCurrentGSMove		= NULL;

	if ( pxClientActor->GetMovementSystem() && pxClientActor->GetMovementSystem()->GetCurrentMove() && pxClientActor->GetMovementSystem()->GetCurrentMove()->GetHash() == AVP_Move_GS2RigidAnim::GetStaticHash() )
	{
		pxCurrentGSMove = static_cast< const AVP_Move* >( pxClientActor->GetMovementSystem()->GetCurrentMove() );
	}

	if ( pxCurrentGSMove && pxCurrentGSMove->GetAVPAnimationData() )
	{
		pxAACA = pxCurrentGSMove->GetAVPAnimationData()->GetAnimationController();
	}
	else
	{
		const Asura_Animation* const pxClientAnimation = pxClientActor->GetShapeInstance().GetAnimation();

		if ( pxClientAnimation )
		{
			pxAACA = pxClientAnimation->GetControllerAnimationFromHash( pxClientActor->GetCurrentGamesceneAnimControllerHash() );
		}
	}

	if ( pxAACA )
	{
		u_int uFlags = pxAACA->GetFlagsForLastFrame();

		if ( uFlags & AVP_ANIMFLAG_ACTION_1 )
		{
			// fire gun!
			AVP_Inventory_System*		pxInventorySystem	= pxServerActor->GetInventorySystem();
			AVP_InventoryItem_Weapon*	pxCurrentWeapon		= static_cast<AVP_InventoryItem_Weapon*>(pxInventorySystem ? pxInventorySystem->GetCurrentItem(uAVP_INVENTORY_GROUPHASH_WEAPON) : NULL);

			if (pxCurrentWeapon != NULL)
			{
				if (pxCurrentWeapon->GetSubType(0)->GetCurrentQuantity() > 0)
				{
					if ( pxCurrentWeapon->IsIdling( true ) )
					{
						pxCurrentWeapon->RequestBeginUsage( AVP_InventoryWeapon_RangedWeapon::UT_N_BULLET_BURST_PRIMARY );
					}
				}
			}
		}
	}
}

//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------
AVP_Brain_Actor* AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire::GetActorBrain( void ) const
{
	return static_cast< AVP_Brain_Actor* >( GetBrain() );
}
#ifndef ASURA_NO_DEBUG_OUTPUT
void AVP_Behaviour_GS2Spline_Human_CoreSplineWithGunFire::RenderDebugInfo()
{
	AVP_ServerActor_Human* pxOurActor = static_cast<AVP_ServerActor_Human*>( GetActorBrain()->GetActor() );

	if (pxOurActor)
	{
		const Asura_Guid				uActorGuid			= pxOurActor->GetGuid();
		const AVP_ClientActor_Human*	pxClient			= AVP_ClientActor_Human::FindEntity(uActorGuid);
		const AVP_Inventory_System*		pxClientSystem		= (pxClient == NULL) ? NULL : pxClient->GetInventorySystem();
		const AVP_InventoryItem_Weapon*	pxClientWeapon		= static_cast<const AVP_InventoryItem_Weapon*>(pxClientSystem ? pxClientSystem->GetCurrentItem(uAVP_INVENTORY_GROUPHASH_WEAPON) : NULL);

		const AVP_Inventory_System*		pxServerSystem		= pxOurActor->GetInventorySystem();
		const AVP_InventoryItem_Weapon*	pxServerWeapon		= static_cast<const AVP_InventoryItem_Weapon*>(pxServerSystem ? pxServerSystem->GetCurrentItem(uAVP_INVENTORY_GROUPHASH_WEAPON) : NULL);

		if (pxClientWeapon != NULL)
		{
			Asura_Char	szClientTemp[256]	= "CLNT: ";

			pxClientWeapon->CreateDebugString(250, &szClientTemp[6]);

			Asura_Font_System::Print( GetDbgOutputPosX(), GetDbgOutputPosY(), 0.5f, 0xff77ff77, ASURA_TEXTFORMAT_CENTREJUSTIFIED | ASURA_TEXTFORMAT_FORMATSTRING, szClientTemp );
			GetActorBrain()->GetDebugOutputPos().y += 10.0f;
		}

		if (pxServerWeapon != NULL)
		{
			Asura_Char	szServerTemp[256]	= "SRVR: ";

			pxServerWeapon->CreateDebugString(250, &szServerTemp[6]);

			Asura_Font_System::Print( GetDbgOutputPosX(), GetDbgOutputPosY(), 0.5f, 0xff77ff77, ASURA_TEXTFORMAT_CENTREJUSTIFIED | ASURA_TEXTFORMAT_FORMATSTRING, szServerTemp );

			GetActorBrain()->GetDebugOutputPos().y += 10.0f;
		}
	}
}
#endif

/// END OF FILE