#include "MW4Headers.hpp"

#include "HeatSink.hpp"
#include "HeatManager.hpp"
#include "SubsystemClassData.hpp"

//#############################################################################
//###############################    HeatSink    ##############################
//#############################################################################

HeatSink::ClassData*
	HeatSink::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	HeatSink::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			HeatSinkClassID,
			"MechWarrior4::HeatSink",
			Subsystem::DefaultData,
			0, NULL,
			(Entity::Factory)Make,
			(Entity::CreateMessage::Factory)CreateMessage::ConstructCreateMessage,
			ExecutionStateEngine::DefaultData,
			(Entity::GameModel::Factory)GameModel::ConstructGameModel,
			NULL,
			(Entity::GameModel::ReadAndVerifier)GameModel::ReadAndVerify,
			(Entity::GameModel::ModelWrite)GameModel::WriteToText,
			(Entity::GameModel::ModelSave)GameModel::SaveGameModel,
			(Subsystem::StreamCreate) CreateStream
		);
	Register_Object(DefaultData);

	DIRECT_GAME_MODEL_ATTRIBUTE(
		HeatSink__GameModel,
		HeatSinkPoints,
		m_heatSinkPoints,
		Scalar
	);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	HeatSink::TerminateClass()
{
	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
HeatSink*
	HeatSink::Make(
		CreateMessage *message,
		ReplicatorID *base_id
	)
{
	Check_Object(message);
	gos_PushCurrentHeap(Heap);
	HeatSink *new_entity = 
		new HeatSink(DefaultData, message, base_id, NULL);
	gos_PopCurrentHeap();
	Check_Object(new_entity);
	return new_entity;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
HeatSink::HeatSink(
	ClassData *class_data,
	CreateMessage *message,
	ReplicatorID *base_id,
	ElementRenderer::Element *element
):
	Subsystem(class_data, message, base_id, element)
{
	Check_Object(message);
	heatSinkObject = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
HeatSink::~HeatSink()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	HeatSink::TakeCriticalHit()
{
	Check_Object(this);

	//Don't do anything here
	return false;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	HeatSink::DestroySubsystem()
{
	Check_Object(this);
#if 0	
	MWObject *mw_object = GetParentVehicle();
	Check_Object(mw_object);
	if(mw_object->m_heatManager)
	{
		if (Network::GetInstance()->AmIServer())
			mw_object->m_heatManager->heatObjects.Remove(heatSinkObject);
	}
#endif
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	HeatSink::ConnectHeatManager(HeatManager *heat_manager)
{
	Check_Object(this);
	Check_Object(heat_manager);

	if(heat_manager)
	{
		if (Network::GetInstance()->AmIServer())
		{
			const GameModel *model = GetGameModel();
			Check_Object(model);
			heatSinkObject = heat_manager->AddHeatObject(-model->m_heatSinkPoints, true);
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	HeatSink::TestInstance() const
{
	Verify(IsDerivedFrom(DefaultData));
}

