//
// AVP_ServerEntity_GameMode.cpp
//
// Created by JG - 04 July 2008
//
// Copyright (C) 2008 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

// This file's header.
#include "Entity/GameMode/AVP_ServerEntity_GameMode.h"

// Engine
#include "ChunkSystem/Asura_Chunk_Stream.h"

// Project 
#include "Entity/AVP_Entity_Classification.h"

// Static variables...
Asura_Hashed_List<AVP_ServerEntity_GameMode> AVP_ServerEntity_GameMode::s_xList( 2 );

AVP_ServerEntity_GameMode::AVP_ServerEntity_GameMode( Asura_Guid uGuid )
: Asura_ServerEntity_ActivatableWithPosition( uGuid )
, m_eGameMode( AVP_Settings::AVP_GAMETYPE_DEATH_MATCH )
{ 
	Classification = AVP_ENTITYCLASS_GAMEMODE;

	// Game mode entities always start inactive
	SetInternalActiveFlag(false);

    SetNeverUpdateClient();
    SetNeverUpdates( true );

	s_xList.AddToList(this); 
}

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

AVP_ServerEntity_GameMode* AVP_ServerEntity_GameMode::CreateFromChunkStream(Asura_Guid uGuid, Asura_Chunk_ChunkBuffer_Stream& xStream)
{
	AVP_ServerEntity_GameMode* pxGameMode = new AVP_ServerEntity_GameMode( uGuid );

	if ( pxGameMode )
	{
		pxGameMode->ReadFromChunkStream( xStream );
	}

	return pxGameMode;
}

void AVP_ServerEntity_GameMode::ReadFromChunkStream(Asura_Chunk_Stream& xStream)
{
	u_int uVersion;

	xStream >> uVersion;

	if (uVersion > uGAMEMODE_CHUNK_VERSION)
	{
		Asura_Assert(0, "Unhandled chunk version in AVP_ServerEntity_GameMode::ReadFromChunkStream");
		return;
	}

	PARENT::ReadFromChunkStream(xStream);

	//keep each version's code together, to make the current version more readable, and allow obsolete versions to be ifdef'd to tools only
	if (uVersion == 0)
	{
		int iGameMode;
		xStream >> iGameMode;
		m_eGameMode = static_cast<AVP_Settings::GameType>(iGameMode);
		xStream >> m_xPosition;
	}
}

void AVP_ServerEntity_GameMode::WriteToChunkStream(Asura_Chunk_Stream& xStream) const
{
	xStream << uGAMEMODE_CHUNK_VERSION;

	PARENT::WriteToChunkStream(xStream);

	xStream << static_cast<int>(m_eGameMode);
	xStream << m_xPosition;
}

bool AVP_ServerEntity_GameMode::HandleMessage(const Asura_Message *pxMessage)
{
	return PARENT::HandleMessage( pxMessage ); 
}

void AVP_ServerEntity_GameMode::SendActivateMessage(void)
{
	PARENT::OnEntityActivateMessage(); 
}