#include "MW4Headers.hpp"

#include "FlameMover.hpp"
#include "MWDamageObject.hpp"
#include "MWApplication.hpp"

#include <Adept\Map.hpp>
#include <Adept\Effect.hpp>
#include <Adept\EntityManager.hpp>
#include <Adept\CollisionVolume.hpp>



//#############################################################################
//###############################    FlameMover    ############################
//#############################################################################

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
FlameMover::ClassData*
	FlameMover::DefaultData = NULL;

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
FlameMover*
	FlameMover::Make(
		CreateMessage *message,
		ReplicatorID *base_id
	)
{
	Check_Object(message);
	gos_PushCurrentHeap(Heap);
	Check_Object(EntityManager::GetInstance());
	FlameMover *new_entity;
	Entity *entity = EntityManager::GetInstance()->RequestFromArmory(message->dataListID);
	if (entity)
	{
		new_entity = Cast_Object(FlameMover*, entity);
		new_entity->Reuse(message, base_id);
	}
	else
		new_entity = new FlameMover(DefaultData, message, base_id, NULL);
	gos_PopCurrentHeap();
	Check_Object(new_entity);
	return new_entity;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
FlameMover::FlameMover(
	ClassData *class_data,
	CreateMessage *message,
	ReplicatorID *base_id,
	ElementRenderer::Element *element
):
	WeaponMover(class_data, message, base_id, element)
{
	Check_Pointer(this);
	Check_Object(message);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
FlameMover::~FlameMover()
{
	DESTRUCTOR("FlameMover");
	Check_Object(this);
	
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	FlameMover::PostCollision(const CollisionData *data)
{
	Check_Object(this);

	//---------------------------------
	// Apply damage to the thing we hit
	//---------------------------------
	//

// MSL 5.06 Armor Mode
	MWApplication *m_App;
	m_App = MWApplication::GetInstance ();
	m_ArmorMode = m_App->GetLocalNetParams()->m_armormodeOn;

	Entity::TakeDamageMessage damage_message(
		data->m_otherEntity->GetReplicatorID(),
		inflictingEntityID,
		damageAmount,
		m_ArmorMode,
		HeatDamageType,		//Telling it that I am a heat type person
		data->m_normal,
		data->m_worldIntersectionPoint,
		heatAmount,
		false,
		itemID
	);
	
	data->m_otherEntity->Receive(&damage_message);
	SetNewLocalToParent(GetLocalToWorld());

	//
	//---------------------------------
	// Create the hit and damage decals
	//---------------------------------
	//
	CreateHitEffect(
		data->m_normal,
		data->m_worldIntersectionPoint,
		data->m_otherVolume->m_material
	);
}

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

