#include "MW4Headers.hpp"

#include "BombWeaponSubsystem.hpp"
#include "MWObject.hpp"
#include "Missile.hpp"
#include "SubsystemClassData.hpp"

#include <Adept\Map.hpp>
#include <Adept\Site.hpp>
#include <MLR\MLRTexturePool.hpp>

//#############################################################################
//############################  BombWeaponSubsystem    ########################
//#############################################################################

BombWeaponSubsystem::ClassData*
	BombWeaponSubsystem::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	BombWeaponSubsystem::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			BombWeaponSubsystemClassID,
			"MechWarrior4::BombWeaponSubsystem",
			MissileWeapon::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
	BombWeaponSubsystem::TerminateClass()
{
	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
BombWeaponSubsystem::~BombWeaponSubsystem()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void 
	BombWeaponSubsystem::CreateProjectile(Time till)
{
	Check_Object(this);
	const GameModel *model = GetGameModel();
	Check_Object(model);

	if(model->projectileModelResource != ResourceID::Null)
	{
		//
		//---------------------------------
		//Get the ClassID from the Resource
		//---------------------------------
		//
		ClassID class_ID;
		class_ID = GetClassIDFromDataListID(model->projectileModelResource);
		Verify(class_ID != NullClassID);

		ReplicatorID site_id = ReplicatorID::Null;
		Normal3D target_normal;

		Motion3D initial_velocity = Motion3D::Identity;
		Motion3D initial_acceleration = Motion3D::Identity;
		LinearMatrix4D site_local_to_world;

		//--------------------------------
		//Initialize the velocities
		//--------------------------------
		//
		Check_Object(sitePointer);
		site_local_to_world = sitePointer->GetLocalToWorld();

//		Check_Object(sitePointer);
//		site_local_to_world = sitePointer->GetLocalToWorld();
//		initial_velocity.linearMotion.Multiply(
//			model->initialLinearVelocity,
//			site_local_to_world
//		);
		initial_acceleration.linearMotion = model->initialLinearAcceleration;

		ReplicatorID target_id;
		target_id = ReplicatorID::Null;
		Point3D target_point;
		Entity *ray_source = targetQueryEntity.GetCurrent();
		if(ray_source)
		{
			Check_Object(ray_source);
			target_id = ray_source->GetReplicatorID();
//			Point3D trans;
//			targetQuery.m_line->FindEnd(&trans);
		//	target_point.MultiplyByInverse(
		//		trans,
		//		ray_source->GetLocalToWorld()
		//	);
		}
//		else
//		{
			targetQuery.m_line->FindEnd(&target_point);
//		}

		Vector3D direction;
		direction.Subtract(target_point,(Stuff::Point3D)site_local_to_world);

		LinearMatrix4D direction_matrix = LinearMatrix4D::Identity;
		direction_matrix.BuildTranslation((Stuff::Point3D)site_local_to_world);
		direction_matrix.AlignLocalAxisToWorldVector(direction, Z_Axis, Y_Axis, X_Axis);

		initial_velocity.linearMotion.Multiply(model->initialLinearVelocity, direction_matrix);

		//
		//------------------------------------
		//Create the Missile CreateMessage
		//------------------------------------
		//

		Missile::CreateMessage projectile_create_message(
			sizeof(Missile::CreateMessage),
			DefaultEventPriority,
			CreateMessage::DefaultFlags,
			class_ID,
			WeaponMover::DefaultFlags,
			model->projectileModelResource,
			sitePointer->GetLocalToWorld(),
			0.0f,
			Missile::ExecutionStateEngine::BombMotionState,
			NameTable::NullObjectID,
			Entity::DefaultAlignment,
			initial_velocity,
			initial_acceleration,
			model->damageAmount,
			model->heatToDeal,
			model->maxDistance,
// MSL 5.03 Min Distance
//			model->minDistance,
			GetParentVehicle()->GetReplicatorID(),
			model->splashRadius,
			model->percentageOfDamageToDirectHit,
			model->minPercentageOfDamageToSphereHit,
			model->maxPercentageOfDamageToSphereHit,
			model->itemID,
			target_id,
			target_point,
			model->burnTime
		);
		MemoryStream stream(&projectile_create_message, projectile_create_message.messageLength);

		Entity *entity;
		ReplicatorID projectile_id = Connection::Hermit->GetNextReplicatorID();
		entity = CreateEntity(&stream, &projectile_id, false);
		Check_Object(entity);
		Map::GetInstance()->AddChild(entity);
	}
}

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

