
#include "MW4Headers.hpp"

#include "LongTomWeapon.hpp"
#include "Missile.hpp"
#include "MWObject.hpp"
#include "SubsystemClassData.hpp"
#include "Torso.hpp"

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

//#############################################################################
//############################  LongTomWeaponSubsystem    #####################
//#############################################################################

LongTomWeaponSubsystem::ClassData*
	LongTomWeaponSubsystem::DefaultData = NULL;

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
LongTomWeaponSubsystem::~LongTomWeaponSubsystem()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void 
	LongTomWeaponSubsystem::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;

		//
		//-----------------------------------------
		//We need to get the forward of the eyesite
		//-----------------------------------------
		//
		
		
	

		UnitVector3D direction;

		
		if (GetParentVehicle()->GetTorso() != 0)
		{
			//GetParentVehicle()->GetTorso()->GetTwistJointMatrix().GetLocalForwardInWorld(&direction);

			LinearMatrix4D pitch_matrix = LinearMatrix4D::Identity;
			pitch_matrix.BuildRotation(YawPitchRoll(GetParentVehicle()->GetTorso()->yawValue, GetParentVehicle()->GetTorso()->pitchValue, 0.0f));


			//UnitVector3D temp_direction;
			//GetParentVehicle()->GetLocalToWorld().GetLocalForwardInWorld(&temp_direction);

			//direction.Multiply(temp_direction, pitch_matrix);
	
			LinearMatrix4D direction_mat;

			direction_mat.Multiply(pitch_matrix, GetParentVehicle()->GetLocalToWorld());

			//SPEWALWAYS(("jerryeds", "%f %f %f", temp_direction.x, temp_direction.y, temp_direction.z));


			direction_mat.GetLocalForwardInWorld(&direction);

		}
		else
		{
			GetParentVehicle()->GetLocalToWorld().GetLocalForwardInWorld(&direction);
		}
	

		//
		//-------------------------
		//Initialize the velocities
		//-------------------------
		//
		//
		initial_velocity.linearMotion.Multiply(
			model->initialLinearVelocity,
			direction
		);
		initial_acceleration.linearMotion.x = 0.0f;
		initial_acceleration.linearMotion.y = -g_Gravity;
		initial_acceleration.linearMotion.z = 0.0f;

		ReplicatorID target_id;
		target_id = ReplicatorID::Null;
		Point3D target_point;
		//
		//------------------------------------
		//Create the Missile CreateMessage
		//------------------------------------
		//

		WeaponMover::CreateMessage projectile_create_message(
			sizeof(Missile::CreateMessage),
			DefaultEventPriority,
			CreateMessage::DefaultFlags,
			class_ID,
			WeaponMover::DefaultFlags,
			model->projectileModelResource,
			sitePointer->GetLocalToWorld(),
			0.0f,
			WeaponMover::ExecutionStateEngine::LinearDragMotionState,
			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
		);
		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);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Scalar
	LongTomWeaponSubsystem::GetDistanceWithAngle(Scalar angle)
{
	Check_Object(this);

	const GameModel *model = GetGameModel();
	Check_Object(model);

	if(angle < 0.0f)
		return 0.0f;

	Scalar muzzle = model->initialLinearVelocity.y;
	Scalar value;
	angle *= angle;
	Scalar temp = angle * angle;
	value = 2 * muzzle * muzzle * (Sqrt(Fabs(angle - temp)));
	value /= g_Gravity;

	return value;
}

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

