#include "MW4Headers.hpp"

// Created 5/16/2002 by ASH - Advanced Gyro

#include "AdvancedGyro.hpp"
#include "SubsystemClassData.hpp"
#include "Mech.hpp"

//#############################################################################
//############################    AdvancedGyro    ###########################
//#############################################################################

AdvancedGyro::ClassData*
	AdvancedGyro::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	AdvancedGyro::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			AdvancedGyroClassID,
			"MechWarrior4::AdvancedGyro",
			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);
}

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
AdvancedGyro::~AdvancedGyro()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	AdvancedGyro::ConnectSubsystem()
{
	Check_Object(this);
	Verify(GetParentVehicle()->IsDerivedFrom(Mech::DefaultData));

	m_parentMech = Cast_Pointer(Mech*, GetParentVehicle());
	const Mech::GameModel *mech_model = m_parentMech->GetGameModel();
	Check_Object(mech_model);

	bool return_value = false;
	if(m_parentMech->DoesHaveAvailableTonage(mech_model->m_advancedGyroTonnage))
		return_value = BaseClass::ConnectSubsystem();

	if(return_value)
		m_parentMech->AddTonage(mech_model->m_advancedGyroTonnage);

	return return_value;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//
bool
	AdvancedGyro::DisconnectSubsystem()
{
	Check_Object(this);
	Verify(GetParentVehicle()->IsDerivedFrom(Mech::DefaultData));
	
	m_parentMech = Cast_Pointer(Mech*, GetParentVehicle());
	const Mech::GameModel *mech_model = m_parentMech->GetGameModel();
	Check_Object(mech_model);

	bool return_value = false;
	return_value = BaseClass::DisconnectSubsystem();

	if(return_value)
		m_parentMech->SubtractTonage(mech_model->m_advancedGyroTonnage);

	return return_value;
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	AdvancedGyro::DestroySubsystem()
{
	Check_Object(this);

	MWObject *mw_object = GetParentVehicle();
	Check_Object(mw_object);
	Verify(mw_object->IsDerivedFrom(Mech::DefaultData));
	Mech *mech = Cast_Object(Mech *, mw_object);
	mech->DestroyAdvancedGyro();

	m_parentMech = Cast_Pointer(Mech*, GetParentVehicle());
	const Mech::GameModel *mech_model = m_parentMech->GetGameModel();
	Check_Object(mech_model);
	m_parentMech->SubtractTonage(mech_model->m_advancedGyroTonnage);
}


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