//===========================================================================//
// File:	mechai.hpp																	     //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
//	07/13/99  AHF	Created File                  //
//---------------------------------------------------------------------------//
// Copyright (C) 1999, Microsoft										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#pragma once

#include "aiutils.hpp"
#include "rail_move.hpp"
#include "vehicle.hpp"
#include "path.hpp"
#include "aiMoveData.hpp"
#include "combatai.hpp"

namespace MechWarrior4
{
	
	class Airplane;

	typedef MechWarrior4::CombatAI__CreateMessage			PlaneAI__CreateMessage;

	//##########################################################################
	//###########################   PlaneAI    #################################
	//##########################################################################

	class PlaneAI : public MechWarrior4::CombatAI
	{
		typedef CombatAI inherited;
		friend class CMoveData;

	public:
		static void InitializeClass();
		static void TerminateClass();

		static ClassData *DefaultData;

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Inheritance Support
	//
	public:
		typedef PlaneAI__CreateMessage					CreateMessage;


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Run-time Construction and Destruction Support
	//
	public:
		virtual void Save (MemoryStream *stream) {CombatAI::Save(stream);}
		virtual void Load (MemoryStream *stream) {CombatAI::Load(stream);}

		static PlaneAI* Make(CreateMessage *message,ReplicatorID *base_id);
	
	protected:
		Airplane *m_Airplane;
		PlaneAI(ClassData *class_data,CreateMessage *message,ReplicatorID *base_id,ElementRenderer::Element *element);

		virtual ~PlaneAI();

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Message Support
	//
	public:

		enum {
			DebugTextMessageID = MechWarrior4::CombatAI::NextMessageID,
			NextMessageID
		};

		void DebugTextMessageHandler(Adept::ReceiverDataMessageOf<int> *message);

		protected:
			static const MessageEntry MessageEntries[];

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Execution Support
	//
	protected:

		int m_InMove;
		int m_InMoveDir;
		void ExecutePath (Stuff::Time till);
		void ExecuteMoveCommand (Stuff::Time till);
		void ExecuteMoveHead (Stuff::Time till);
		void PathDone (Stuff::Time till,bool failed=false);
		virtual void TurnOn (void);
		virtual void TurnOff (void);

	public:
		void PreCollisionExecute(Stuff::Time till);


	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Obstacle Support
	//
		protected:
			struct AvoidData
			{
				PlaneAI *who;
				Stuff::Time active;
				AvoidData (PlaneAI *p1,Stuff::Time p2)
				{ who = p1;  active = p2; }
			};
			stlport::vector<MW4AI::Rect4D *> m_AirLockedRects;
			stlport::vector<AvoidData> m_AvoidList;
			Stuff::Time m_EndStart;
			bool m_AttackRun,m_Aborting;
			Stuff::Point3D m_AttackPoint;
			bool m_FinishedFlyBy;
			Stuff::Point3D m_ContinuousFlybySwingPoint;

			virtual void AttackRunDone (void);
			virtual void AbortAttackRun (void);
			bool ValidAttackPoint (const Point3D& point);
			void EndAttackRun (void);

			Point3D FindAvoidPoint (MW4AI::Rect4D *rect);
			bool Avoiding (PlaneAI *who);
			void AddAvoid (PlaneAI *who);
			void RemoveAvoid (PlaneAI *who);
			void KillAvoidList (void);
			void CleanAvoidList (void);
			virtual void NotifyNoPath(void);
			virtual void NotifyReachedDestination(void);
			virtual void NotifyCollided(Entity *who);
			bool AddLockRect (MW4AI::Rect4D *p1,bool carecollide,Stuff::Time till);

			virtual void HandleObstacle (MW4AI::Rect4D *p1,Adept::Entity *p2);
			void HandleObstacle (MW4AI::Rect4D *p1)
			{ HandleObstacle (p1,NULL); }
			void HandleObstacle (Adept::Entity *p1)
			{ HandleObstacle (NULL,p1); }
			void HandleObstacle (void)
			{ HandleObstacle (NULL,NULL); }

		public:
			virtual bool FlyBy (const Point3D& point);
			virtual void ContinuousFlyBy (const Point3D& point);
			virtual bool StartAttackRun (const Stuff::Point3D& loc);
			virtual bool PointIsOK(const Stuff::Point3D& point) const;
			virtual void ClearPathLock (bool quitting,bool fromram);
			virtual bool PathLock (bool secondpass,bool fromram);

		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		// Order Support
		//
		public:

		virtual void 	orderMoveLookOut (void);
		virtual void 	orderMoveSit (bool shutdown);
		virtual bool	orderTakeOff(int distance);
		virtual bool	orderLand(void);
		virtual bool	orderDoorOpen(void);
		virtual bool	orderDoorClose(void);
		void hellteleport (void);



		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		// Command Support
		//

	protected:

		virtual void ExecuteMoveCommand (MW4AI::CommandEntry *command);

	public:

	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Test Support
	//
	public:
		void TestInstance() const;
	};
}

