
///////////////////////////////////////////////////////////////////////////////
//
// File   : AVP_Messages_Human.cpp
// Author : Chris Wallace
// Date   : 5-Feb-2007
//
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

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

#include "Core/Asura.h"

// This file's header...
#include "Entity/PhysicalObject/Actor/Vertebrates/Human/AVP_Messages_Human.h"

// Asura independent header-files...
#include "Core/Asura_BitPacker.h"
#include "Network/Asura_NetMsg.h"

// Asura platform header-files...

// Game independent header-files...
#include "Core/BrainTypes/AVP_Brain_Actor_Marine.h"
#include "Core/BrainTypes/AVP_Brain_Actor_Civilian.h"
#include "Entity/AVP_Entity_Classification.h"
#include "Entity/AVP_Messages.h"
#include "Entity/PhysicalObject/Actor/Vertebrates/Human/AVP_ClientActor_Human.h"
#include "Inventory/AVP_Inventory_System.h"
#include "Knowledge/AVP_Knowledge_System_Actor.h"

// Game platform header-files...

// Tools header-files...


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


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


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////


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

////////////////////////////////////////////////////////////////////////////////
// Function     : AVP_Message_HumanClientUpdate::FillHuman
// Author       : Andy Tate
// Date         : 6-Oct-2006
////////////////////////////////////////////////////////////////////////////////

void AVP_Message_HumanClientUpdate::FillHuman(const AVP_ServerActor_Human& xHuman)
{
	FillVertebrate(xHuman);

	if (xHuman.GetClassification() != AVP_ENTITYCLASS_CIVILIAN)
	{
		// Initialise in case we dont get a brain (true for players in multiplayer)
		m_uAlertLevel = AVP_Knowledge_System_Actor::ACTIVE_ALERTNESS;

		AVP_Brain_Actor_Marine* pxMarineBrain = (AVP_Brain_Actor_Marine*)xHuman.GetActorBrain();

		if ((pxMarineBrain) && (pxMarineBrain->GetKnowledgeSystem()))
		{
			m_uAlertLevel = pxMarineBrain->GetKnowledgeSystem()->GetAlertLevel();
		}
	}
	else
	{
		// Initialise in case we dont get a brain (true for players in multiplayer)
		m_uAlertLevel = AVP_Knowledge_System_Actor::PASSIVE_ALERTNESS;

		AVP_Brain_Actor_Civilian* pxMarineBrain = (AVP_Brain_Actor_Civilian*)xHuman.GetActorBrain();

		if ((pxMarineBrain) && (pxMarineBrain->GetKnowledgeSystem()))
		{
			m_uAlertLevel = pxMarineBrain->GetKnowledgeSystem()->GetAlertLevel();
		}
	}

	m_bCrouching = xHuman.IsCrouching();
	m_bAiming = xHuman.IsAiming();
	m_eCoverType = xHuman.GetCoverType();
}

/// End of function 'AVP_Message_HumanClientUpdate::FillHuman'. ///


#ifdef ASURA_SUPPORT_NETMSG_SYSTEM
void AVP_Message_HumanClientUpdate::RegisterFields(u_short usMsgID, u_short usEntityClassification)
{
	Asura_Assert(usMsgID == ASURA_MSG_CLIENTUPDATE, "invalid msg ID");
	Asura_Assert(!Asura_NetMsg::FindNetMsgObject(usMsgID, usEntityClassification), "already exists");
	Asura_Assert
	(
		usEntityClassification == AVP_ENTITYCLASS_HUMAN		|| 
		usEntityClassification == AVP_ENTITYCLASS_ANDROID	||
		usEntityClassification == AVP_ENTITYCLASS_MARINE	||
		usEntityClassification == AVP_ENTITYCLASS_CIVILIAN,
		"Unexpected entity classification."
	); // get rid of the reference to android if/when it gets it's own update message
	
	u_char ucNumFields = GetNumFields();

	Asura_NetMsg* pxNetMsg = new Asura_NetMsg(usMsgID, usEntityClassification, ucNumFields, sizeof(AVP_Message_HumanClientUpdate));
	RegisterFields(pxNetMsg);
	pxNetMsg->EndRegistration();
}


void AVP_Message_HumanClientUpdate::RegisterFields(Asura_NetMsg* pxNetMsg)
{
	const AVP_Message_HumanClientUpdate xDummy;
	PARENT::RegisterFields(pxNetMsg);

	Asura_NetMsg_RegisterField(pxNetMsg, xDummy, Asura_FieldType_uchar, m_bCrouching, 1, Asura_Threshold_AnyChange, 0.0f);
	Asura_NetMsg_RegisterField(pxNetMsg, xDummy, Asura_FieldType_uchar, m_bAiming, 1, Asura_Threshold_AnyChange, 0.0f);
	Asura_NetMsg_RegisterField(pxNetMsg, xDummy, Asura_FieldType_uint, m_eCoverType, 1, Asura_Threshold_AnyChange, 0.0f);
	Asura_NetMsg_RegisterIntFieldBits(pxNetMsg, xDummy, Asura_FieldType_uint_bits, 2, m_uAlertLevel, 1, Asura_Threshold_AnyChange);
}
#endif//#ifdef ASURA_SUPPORT_NETMSG_SYSTEM

/// End of file. ///

AVP_Message_HumanPlayerCommands::AVP_Message_HumanPlayerCommands( const AVP_ClientActor_Human& xHuman )
{
	FillHuman( xHuman );  
}

AVP_Message_HumanPlayerCommands::AVP_Message_HumanPlayerCommands( Asura_BitUnpacker& xUnpacker )
{
	Unpack( xUnpacker ); 
}

void AVP_Message_HumanPlayerCommands::FillHuman( const AVP_ClientActor_Human& xHuman )
{
	PARENT::FillVertebrate( xHuman );

	m_bCrouching = xHuman.IsCrouching();
	m_bAiming = xHuman.IsAiming();
}

void AVP_Message_HumanPlayerCommands::Pack( Asura_BitPacker& xPacker ) const
{
	PARENT::Pack( xPacker ); 

	xPacker.PackBool( IsCrouching() ); 
	xPacker.PackBool( IsAiming() ); 
}

void AVP_Message_HumanPlayerCommands::Unpack( Asura_BitUnpacker& xUnpacker ) 
{
	PARENT::Unpack( xUnpacker ); 

	m_bCrouching = xUnpacker.UnpackBool(); 
	m_bAiming = xUnpacker.UnpackBool(); 
}
