#include "MW4Headers.hpp"
#include "Adept\AdeptHeaders.hpp"

#include "mw4.hpp"
#include "moverai.hpp"
#include "Adept\Interface.hpp"
#include "Adept\Application.hpp"
#include "Adept\player.hpp"
#include "torso.hpp"

#include "aimovedata.hpp"

using namespace MechWarrior4;
using namespace MW4AI;
#pragma warning (push)
#include <algorithm>
#pragma warning (pop)

void CMoveData::Save (MemoryStream *stream)
{
	Verify (m_AI);
	*stream << m_AI->objectID;
	*stream << ((int) m_MoveType);
	*stream << m_Speed;
	if (m_PathRequest)
	{
		*stream << true;
		m_PathRequest->Save (stream);
	}
	else
	{
		*stream << false;
	}
	*stream << m_DidCommand;
	*stream << m_Executing;
	*stream << m_Done;
	*stream << m_ClearEnd;
	*stream << m_Usage;
}

void CMoveData::Load (MemoryStream *stream)
{
	ObjectID temp;
	int tempint;
	bool flag;

	*stream >> temp;
	Check_Object (m_AI);
	Verify (temp == m_AI->objectID);
	*stream >> tempint;
	Verify (m_MoveType == tempint);
	*stream >> flag;
	delete m_PathRequest;
	m_PathRequest = NULL;
	if (flag)
	{
		gos_PushCurrentHeap (g_RailHeap);
		m_PathRequest = new CPathRequest (g_MissionGraph);
		gos_PopCurrentHeap ();
		Check_Pointer (m_PathRequest);
		m_PathRequest->Load (stream);
	}
	*stream >> m_DidCommand;
	*stream >> m_Executing;
	*stream >> m_Done;
	*stream >> m_ClearEnd;
	*stream >> m_Usage;
}

bool CMoveData::operator== (const CMoveData& p1)
{

	if (m_Speed != p1.m_Speed)
		return false;
	if (m_Graph != p1.m_Graph)
		return false;
	return true;
}

int CMoveData::LocationSize (void) const
{
	return 0;
}
Stuff::Point3D CMoveData::Location (int id)
{
	return Stuff::Point3D (-1,-1,-1);
}

void CMoveData::CreatePlaneRequests (const Stuff::Point3D& start)
{
	m_PathRequest = NULL;
}

void CMoveData::CreateRequests (const Stuff::Point3D& start,int usageflags)
{
	Check_Pointer (m_Graph);

	if (m_PathRequest)
	{
		g_PathManager->RemoveRequest (m_PathRequest);
		delete m_PathRequest;
		m_PathRequest = NULL;
	}

	m_Usage = usageflags;
	m_Executing = false;
	m_PathRequest = NULL;
}

CMoveData::~CMoveData (void)
{
	if (m_PathRequest)
	{
		g_PathManager->RemoveRequest (m_PathRequest);
		delete m_PathRequest;
		m_PathRequest = NULL;
	}
}

CMoveData::CMoveData (MW4AI::CRailGraph *graph,MoverAI *owner)
{
	Check_Pointer (graph);
	m_Graph = graph;
	m_DidCommand = false;
	m_Executing = false;
	m_PathRequest = NULL;
	m_Done = false;
	m_Speed = 0;
	m_ClearEnd = false;
	m_AI = owner;
}

void CMoveData::InsertPoint (const Stuff::Point3D& loc)
{
}
void CMoveData::InsertObject (Entity *who)
{
}

Stuff::Scalar CMoveData::Speed (void) const
{
	if (Done ())
		return 0;
	return m_Speed;
}

void CMoveData::StartExecuting (MoverAI *ai,Stuff::Time till)
{

	m_AI = ai;
	Verify (Executing () == false);

	Executing (true);
}

void CMoveData::Execute (MoverAI *ai,Stuff::Time till)
{
	Verify (Executing ());
}


void CMoveData::PathDone (MoverAI *ai,Stuff::Time till,bool failed)
{
	if (!failed)
		Verify (Executing ());
	m_AI = ai;
}

