/**
*
* \file AVP_ServerEntity_InteractionTrigger.cpp
*
* Created by Chris Wallace
*
* Copyright (C) 2007 Rebellion, All Rights Reserved.
*
**/

#include "Core/Asura.h"

#include "Entity/Triggers/AVP_ServerEntity_InteractionTrigger.h"

#include "ChunkSystem/Asura_Chunk_Stream.h"
#include "Entity/PhysicalObject/Asura_ServerEntity_PhysicalObject.h"
#include "Input/Asura_ControlMap.h"

#include "Actor/Vertebrates/Predator/AVP_Predator_InputHandler.h"
#include "Entity/AVP_Messages.h"
#include "Entity/Triggers/AVP_Trigger_Messages.h"
#include "Entity/PhysicalObject/Actor/Vertebrates/Predator/AVP_ServerActor_Predator.h"
#include "Entity/PhysicalObject/Actor/Vertebrates/Predator/AVP_ClientActor_Predator.h"
#include "Entity/AVP_Entity_Classification.h"

#ifdef ASURA_TOOLS
const char* AVP_ServerEntity_InteractionTrigger::s_aszAVPVolumeTypes[NUM_AVP_INTERACTIONTRIGGEREDBY_TYPES] = 
{
	"Specific Object",
	"Any Alien",
	"Any Human",
	"Any Predator",
};
#endif //ASURA_TOOLS

Asura_Hashed_List<AVP_ServerEntity_InteractionTrigger> AVP_ServerEntity_InteractionTrigger::s_xList;

AVP_ServerEntity_InteractionTrigger::AVP_ServerEntity_InteractionTrigger(Asura_Guid uGuid)
: Asura_ServerEntity_AdvancedVolumeTrigger(uGuid)
{
	Classification = AVP_ENTITYCLASS_INTERACTIONTRIGGER;

	s_xList.AddToList(this);
}

AVP_ServerEntity_InteractionTrigger::~AVP_ServerEntity_InteractionTrigger()
{
	s_xList.RemoveFromList(this);
}

bool AVP_ServerEntity_InteractionTrigger::IsVolumeOccupied()
{
	for( Asura_Hashed_List_ConstIt<AVP_ServerActor_Predator> xIt(AVP_ServerActor_Predator::GetList()); !xIt.Done(); xIt.Next() )
		{
			const AVP_ServerActor_Predator* pxPredator = xIt.GetCurrent();
			if(Contains(pxPredator->GetPosition()))
			{
				return true;
			}
		}
	
	return false;
}

bool AVP_ServerEntity_InteractionTrigger::IsTriggered(void)
{
	return false;
}

bool AVP_ServerEntity_InteractionTrigger::IsUnTriggered(void)
{
	return false;
}

bool AVP_ServerEntity_InteractionTrigger::HandleMessage(const Asura_Message* pxMessage)
{
	if(!pxMessage) return false;

	// Process a message from the server
	switch(pxMessage->GetMsgID())
	{
	case ASURA_MSG_TRIGGER :
		{
			OnTrigger();
		}
	default:
		{
			return(ASURA_PARENT::HandleMessage(pxMessage));
		}
	}
}

void AVP_ServerEntity_InteractionTrigger::WriteToChunkStream(Asura_Chunk_Stream& xStream) const
{
	const int iVersion = 0;

	PARENT::WriteToChunkStream	(xStream);

	xStream << iVersion;
}

void AVP_ServerEntity_InteractionTrigger::ReadFromChunkStream(Asura_Chunk_Stream& xStream)
{
	int iVersion;
	
	PARENT::ReadFromChunkStream(xStream);

	xStream >> iVersion;

	SetAlwaysUpdateClient(true);
}

void AVP_ServerEntity_InteractionTrigger::MakeClientUpdateMessage (void)
{
	Asura_Message*	pxMessage	= Asura_Network::CreateMessage (ASURA_MSG_CLIENTUPDATE, sizeof(AVP_Message_InteractionTrigger_ClientUpdate), GetClassification());

	if(pxMessage != NULL)
	{
		AVP_Message_InteractionTrigger_ClientUpdate*	pxData	= static_cast<AVP_Message_InteractionTrigger_ClientUpdate*>(pxMessage->GetData());

		// Fill in the message...
		pxData->FillVolume(this);

		// Send the message...
		Send(pxMessage, ASURA_GUID_MY_CLIENT_ENTITIES);
	}
	SetAlwaysUpdateClient(false);
}
