//===========================================================================//
// File:	LRMWeaponSubsystem.cpp										
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 05/18/99  DPB  Created File
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "LRMWeaponSubsystem.hpp"
#include "Missile.hpp"
#include "Vehicle.hpp"
#include "SubsystemClassData.hpp"

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

//#############################################################################
//############################  LRMWeaponSubsystem    #########################
//#############################################################################

LRMWeaponSubsystem::ClassData*
	LRMWeaponSubsystem::DefaultData = NULL;

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
LRMWeaponSubsystem::~LRMWeaponSubsystem()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void 
	LRMWeaponSubsystem::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();
		Vector3D initial_scale = model->initialLinearVelocity;
		if(GetParentAsVehicle())
		{
			initial_scale.z += GetParentAsVehicle()->currentSpeedMPS;
		}

		initial_velocity.linearMotion.Multiply(
			initial_scale,
			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()
//			);
			target_point = m_lockedTargetEntityPartOffset;
		}
		else
		{
			targetQuery.m_line->FindEnd(&target_point);
		}

		//
		//--------------------------------
		//Create the Missile CreateMessage
		//--------------------------------
		//
		int flags = WeaponMover::DefaultFlags;

		if(numberEffectedByAMS > 0)
		{
			numberEffectedByAMS --;
			flags |= Missile::DoesAMSDestroyFlag;
		}

		Missile::CreateMessage projectile_create_message(
			sizeof(Missile::CreateMessage),
			DefaultEventPriority,
			CreateMessage::DefaultFlags,
			class_ID,
			flags,
			model->projectileModelResource,
			sitePointer->GetLocalToWorld(),
			0.0f,
			Missile::ExecutionStateEngine::LRMMotionState,
			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
	LRMWeaponSubsystem::TestInstance() const
{
	Verify(IsDerivedFrom(DefaultData));
}

