#include "MW4Headers.hpp"

#include "LBXWeaponSub.hpp"
#include "LBXMover.hpp"
#include "SubsystemClassData.hpp"

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

//#############################################################################
//###############################    LBXWeaponSub    ##########################
//#############################################################################

LBXWeaponSub::ClassData*
	LBXWeaponSub::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	LBXWeaponSub::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			LBXWeaponSubClassID,
			"MechWarrior4::LBXWeaponSub",
			ProjectileWeapon::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(
		LBXWeaponSub__GameModel,
		DisipationStart,
		m_disipationStart,
		Scalar
	);

	DIRECT_GAME_MODEL_ATTRIBUTE(
		LBXWeaponSub__GameModel,
		MaxDisipation,
		m_maxDisipation,
		Scalar
	);
}

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
LBXWeaponSub::~LBXWeaponSub()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	LBXWeaponSub::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;
		ReplicatorID target_id = ReplicatorID::Null;
		Normal3D target_normal;

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

		//
		//-------------------------
		//Initialize the velocities
		//-------------------------
		//
		Check_Object(sitePointer);
		site_local_to_world = sitePointer->GetLocalToWorld();
		Vector3D projectile_direction;
		Point3D target_point;
		targetQuery.m_line->FindEnd(&target_point);
		projectile_direction.Subtract(
			target_point,
			site_local_to_world
		);
		Check_Object(sitePointer);
		site_local_to_world = sitePointer->GetLocalToWorld();

		LinearMatrix4D direction_matrix = LinearMatrix4D::Identity;
		direction_matrix.BuildTranslation((Stuff::Point3D)site_local_to_world);
		direction_matrix.AlignLocalAxisToWorldVector(projectile_direction, Z_Axis, Y_Axis, X_Axis);
		
		Vector3D initial_scale = model->initialLinearVelocity;
		if(GetParentAsVehicle())
		{
			initial_scale.z += GetParentAsVehicle()->currentSpeedMPS;
		}

		initial_velocity.linearMotion.Multiply(
			initial_scale,
			direction_matrix
		);

		initial_acceleration.linearMotion = model->initialLinearAcceleration;

		//
		//------------------------------------
		//Create the WeaponMover CreateMessage
		//------------------------------------
		//

		LBXMover::CreateMessage projectile_create_message(
			sizeof(WeaponMover::CreateMessage),
			DefaultEventPriority,
			CreateMessage::DefaultFlags,
			class_ID,
			WeaponMover::DefaultFlags,
			model->projectileModelResource,
			direction_matrix,
			0.0f,
			LBXMover::ExecutionStateEngine::StraightLineMotionState,
			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,
			model->m_disipationStart,
			model->m_maxDisipation
		);
		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);
		if (vehicleInterface)
		{
			vehicleInterface->PlayForce (VehicleInterface::FireProjectileForce);
		}
	}
}

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

