//////////////////////////////////////////////////////////////////////////////
//
// AVP_Behaviour_Predator_Melee.h
//
// Copyright (C) 2007 Rebellion. All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////

// B E G I N   F I L E   W R A P P E R ///////////////////////////////////////

#ifdef ASURA_USE_PRAGMA_ONCE
#pragma once
#endif

#ifndef __AVP_BEHAVIOUR_PREDATOR_MELEE_H__
#define __AVP_BEHAVIOUR_PREDATOR_MELEE_H__ 1

// E N D   F I L E   W R A P P E R ///////////////////////////////////////////

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

// asura
#include "Core/Asura_LinkList.h"

// axon
#include "Behaviour/Axon_Project_Behaviour.h"

// F O R W A R D   D E C L A R A T I O N S ///////////////////////////////////

class AVP_Brain_Actor_Predator;
struct Axon_Knowledge;

//////////////////////////////////////////////////////////////////////////////
//
// AVP_Behaviour_Predator_Melee
//
//////////////////////////////////////////////////////////////////////////////

class AVP_Behaviour_Predator_Melee : public Axon_Project_Behaviour
{
	typedef Axon_Project_Behaviour PARENT;

public:
	AVP_Behaviour_Predator_Melee( AVP_Brain_Actor_Predator* pxBrain );
	virtual ~AVP_Behaviour_Predator_Melee();

	void Abort() { m_bAbort = true; }

	bool ShouldFlee() { return m_bShouldFlee; }

protected:
	virtual void Deactivate();
	virtual void PerFrameUpdate( const float fTimeslice );
	virtual void Update( const float fTimeslice );

	virtual bool HandleMessage( const Asura_Message* pxMessage );

#ifndef ASURA_NO_DEBUG_OUTPUT
	virtual void RenderDebugInfo();
#endif

private:
	enum State
	{
		UNSET,
		IDLE,
		RUNNING_IN,
		MELEE,
		FLINCHED,
		GRAPPLE,
		POUNCE,
		FOLLOW,
		FINISHED
	};
	void SwitchToState( const State eNextState );

	AVP_Brain_Actor_Predator* GetPredatorBrain() const;

	
	bool PlanMeleePath();
	bool PlanFollowPath();
	bool PlanPath();
	bool PlanPathInDirection();
	void UpdateMove(float fTimeslice);
	void UpdateFollow(float fTimeslice);
	void UpdateIdle(float fTimeslice);
	void UpdateMelee();
	bool RequestMelee(Asura_Hash_ID uMeleeType );
	void AimAtTarget();
	void AimAtPosition(const Asura_Vector_3& xPosition);
	void LateHandleMessage();
	bool CanGrapple(const AVP_ServerEntity_Actor* pxTarget)  const;
	bool CanMelee(const AVP_ServerEntity_Actor* pxTarget)  const;
	bool MustGrapple(const AVP_ServerEntity_Actor* pxTarget)  const;

	void UpdateIgnoreTarget(float fTimeslice);
	void UpdateNumberOfAttackers();
	bool SelectCurrentMeleeTarget();
	bool SelectCurrentFollowTarget();
	bool IsTargetIgnored(Asura_Guid uGuid) const;
	bool IsTargetReachable(const Axon_Knowledge* pxTargetKnowledge, float fDistance, u_short& usDestWPID, Asura_Vector_3& xPosition ) const;
	void IgnoreTarget(Asura_Guid uGuid, const Asura_Vector_3& xPosition);
	bool ShouldAbortAttack() const;
	u_int GetNumberOfAttackers() const { return m_uNumberOfAttackers; }
	void TryToFire(const Asura_Vector_3& xDirection, float fDistance);
	void UpdateAlert(float fTimeslice);
	void ResetAlert();

	const Axon_Knowledge* GetCurrentMeleeTargetKnowledge() const;
	const Axon_Knowledge* GetCurrentFollowTargetKnowledge() const;
	const Axon_Knowledge* GetTargetKnowledge(Asura_Guid uGuid) const;

	State m_eCurrentState;
	float m_fTimeInState;
	bool m_bNeedToAimAtTarget;
	float m_fTimeSinceLastPounce;

	Asura_Vector_3 m_xDestination;
	Asura_Vector_3 m_xTargetPosition;
	u_short m_usDestinationWPIndex;
	Asura_LinkList< int > m_xDelayedMsgList;

	bool m_bWasTargetKilled;

	Asura_Guid m_uCurrentMeleeTarget;
	Asura_Guid m_uCurrentMeleedTarget;
	Asura_Guid m_uCurrentFollowTarget;

	struct IgnoreTargetData
	{
		Asura_Guid m_uTarget;
		Asura_Vector_3 m_xPosition;
	};

	Asura_LinkList< IgnoreTargetData > m_xIgnoreTargets;

	bool m_bCurrentGrappleSucceed;
	bool m_bAbort;
	u_int m_uNumberOfAttacksSincePowerAttack;
	u_int m_uNumberOfAttackers;

	bool m_bIsCharging;
	float m_fTimeUntilFire;

	enum FollowMode 
	{
		FOLLOW_TOWARDS,
		FOLLOW_AWAY,
		FOLLOW_RANDOM,
		FOLLOW_STAND
	} m_eFollowMode;

	bool m_bShouldFlee;

	Asura_Vector_3 m_xAlertPosition;
	float m_fAlertAge;

};

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

#endif //__AVP_BEHAVIOUR_PREDATOR_MELEE_H__
