//---------------------------------------------------------------------------
// File			: AVP_Behaviour_Human_Grapple.cpp
// Created		: 
// Created By	: Gareth Thomas
// Copyright (C) 2007 Rebellion. All Rights Reserved.
//---------------------------------------------------------------------------

// INCLUDES ///////////////////////////////////////////////////////////

// core
#include "Core/Asura.h"
// header
#include "Behaviour/BehaviourTypes/Human/AVP_Behaviour_Human_Grapple.h"
// asura
#ifndef ASURA_NO_DEBUG_OUTPUT
#include "Rendering/Asura_Font_System.h"
#endif
#include "Network/Asura_Message.h"
// axon
// AVP
#include "Actor/Vertebrates/AVP_Vertebrate_MeleeCounter_System.h"
#include "Animation/AVP_AnimFlags.h"
#include "Core/AVP_Game_Core.h"
#include "Entity/AVP_Messages.h"
#include "Entity/PhysicalObject/Actor/Vertebrates/AVP_Vertebrate_Messages.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 "Network/AVP_Player.h"

// AVP axon
#include "Behaviour/BehaviourTypes/AVP_Behaviour_Types.h"
#include "Core/BrainTypes/AVP_Brain_Actor.h"

//---------------------------------------------------------------------------------
//*********************************************************************************
//---------------------------------------------------------------------------------
// Class		: AVP_Behaviour_Human_Grapple
// Description	: 
//---------------------------------------------------------------------------------
//*********************************************************************************
//---------------------------------------------------------------------------------

//---------------------------------------------------------------------------
// AVP_Behaviour_Human_Grapple :: Constructor
//---------------------------------------------------------------------------
AVP_Behaviour_Human_Grapple::AVP_Behaviour_Human_Grapple(AVP_Brain_Actor* pxBrain)
: PARENT(pxBrain, AVP_BEHAVIOUR_TYPE_HUMAN_GRAPPLE)
{

}

//---------------------------------------------------------------------------
// AVP_Behaviour_Human_Grapple :: HandleMessage
//---------------------------------------------------------------------------
void AVP_Behaviour_Human_Grapple::PerFrameUpdate(const float fTimeslice)
{
	AVP_ClientActor_Human* pxClient = AVP_ClientActor_Human::FindEntity( GetBrain()->GetOwnerGuid() );

// 	if ( pxClient )
// 	{
// 		u_int uAnimFlags = pxClient->GetFlagsForAllAnimsLastFrame();
// 
// 		if (uAnimFlags & AVP_ANIMFLAG_ALLOW_NEW_COUNTER)
// 		{
			// GBT: No chance at the moment design calls for it.
// 			if ( Asura_Random::GetFloat(0.0f, 1.0f) > 1.0f )
// 			{
// 				Asura_Message* pxMessage = Asura_Network::CreateMessage( AVP_MSGID_REQUEST_MELEE, sizeof( AVP_Message_Vertebrate_RequestMelee ) );
// 
// 				if (pxMessage)
// 				{
// 					AVP_Message_Vertebrate_RequestMelee* pxData = static_cast< AVP_Message_Vertebrate_RequestMelee* >( pxMessage->GetData() );
// 
// 					pxData->SetType( AVP_MELEEATTACK_TYPE_NORMAL );
// 					pxData->SetTargetGuid( ASURA_GUID_UNREGISTERED );
// 					pxMessage->SetToGuid( GetBrain()->GetOwnerGuid() );
// 					pxMessage->SetFromGuid( GetBrain()->GetOwnerGuid() );
// 					Asura_Network::Send(pxMessage);
// 				}
// 			}
// 		}
// 	}

	if ( pxClient )
	{
		AVP_Movement_System* pxMoveSystem = static_cast<AVP_Movement_System*>(pxClient->GetMovementSystem());

		if (pxMoveSystem)
		{
			const AVP_Move* pxMove = static_cast<const AVP_Move*>( pxMoveSystem->GetCurrentMove() );

			if ( pxMove && pxMove->GetHash() == AVP_Move_Puppet::GetStaticHash() )
			{
				Asura_Guid uAttackerGuid = (static_cast<const AVP_Move_Puppet*>(pxMove))->GetForcedMeleeTarget();

				const AVP_ServerEntity_Actor* pxMeleeActor = AVP_ServerEntity_Actor::FindEntity( uAttackerGuid );

				if ( pxMeleeActor )
				{
					Asura_Vector_3 xHeadPos;
					pxMeleeActor->GetHeadPosition( xHeadPos );

					AVP_Brain_Actor* pxActorBrain = static_cast<AVP_Brain_Actor*>( GetBrain() );

					if ( pxActorBrain )
					{
						pxActorBrain->SetLookAtPosition( xHeadPos );
					}

					if ( pxClient )
					{
						pxClient->GetAnimationHandler()->GetBodyRig()->SetEyeRestClose( 0.0f );
					}
				}
			}
		}
	}
}

//---------------------------------------------------------------------------
// AVP_Behaviour_Human_Grapple :: RenderDebugInfo
//---------------------------------------------------------------------------
#ifndef ASURA_NO_DEBUG_OUTPUT
void AVP_Behaviour_Human_Grapple::RenderDebugInfo()
{
	PARENT::RenderDebugInfo();

	Asura_Font_System::Print
	(
		GetProjectBrain()->GetDebugOutputPos().x,
		GetProjectBrain()->GetDebugOutputPos().y,
		0.5f, 
		0xffff0000,
		ASURA_TEXTFORMAT_CENTREJUSTIFIED, 
		"GRAPPLING!!"
	);

	// Offset the debug output position for other behaviours to render there own text
	GetProjectBrain()->GetDebugOutputPos().y += 10.0f;
}
#endif
/// End Of File 'AVP_Behaviour_Human_Grapple.cpp'
