#include "..\Shared\SPMission.h"

class scion01 : public SPMission 
{
public:
	scion01(void)
	{
		b_count = &b_last - &b_first - 1;
		b_array = &b_first + 1;

		f_count = &f_last - &f_first - 1;
		f_array = &f_first + 1;

		h_count = &h_last - &h_first - 1;
		h_array = &h_first + 1;

		i_count = &i_last - &i_first - 1;
		i_array = &i_first + 1;
	}

	void Setup(void);
	void AddObject(Handle h);
	void Execute(void);

	// bools
	bool
		b_first,
		missionstart,
		rondevous1,
		convoygo1,
		escort1afarahead,escort1aclose,
		missionwon,
		engage_escorts,
		ambush1over,
		ambushers1killed,
		escort1a_look,
		escort1b_look,
		tug_moveout_nav2,
		playermorphed,playernotmorphed,
		vo1,vo2,vo3,vo6,vo7,
		warned1,warned2,morphdrill,
		shabgopatrol,
		baseattack1,vo8,shabdead,escortcall,vo10,toolong1,cooknotaround,vo16,objectifynav1,
	
		
		b_last;

	// floats
	float
		f_first,
		escort_reaction_time,
		delay1_time,
		vo1time,
		vo2time,
		vo6time,
		vo7time,
		toolongtomorph,
		toolongtomorph2,
		baseattack1time,
		escortcalltime,
		vo10time,
		toolongtime1,
		vo16time,
		f_last;

	// handles
	Handle
		h_first,
		nav1,nav2,escort1a,escort1b,tug1,player,
		ambush1tanka,ambush1tankb,shab,msg2,msg1,
		attack1tanka,attack1tankb,msg3,

		
		h_last;


	// integers
	int
		i_first,


		i_last;
};
DLLBase * BuildMission(void)
{
	return new scion01();
}

void scion01::Setup(void)
{
/*
	Here's where you set the values at the start.  
*/

//  bools
	missionstart = false;
	rondevous1 = false;
	convoygo1 = false;
	escort1afarahead = false;
	escort1aclose = false;	
	missionwon = false;
	engage_escorts = false;
	ambush1over = false;
	ambushers1killed = false;
	escort1a_look = false;
	escort1b_look = false;
	tug_moveout_nav2 = false;
	vo1 = false;
	vo2 = false;
	vo3 = false;
	playermorphed = false;
	playernotmorphed= false;
	warned1 = false;
	warned2 = false;
	morphdrill = false;
	vo6 = false;
	vo7 = false;
	shabgopatrol = false;
	baseattack1 = false;
	vo8 = false;
	shabdead = false;
	escortcall = false;
	vo10 = false;
	toolong1 = false;
	cooknotaround = false;
	vo16 = false;
	objectifynav1 = false;

//  floats
	escort_reaction_time = 999999.9f;
	delay1_time = 999999.9f;
	vo1time = 999999.9f;
	vo2time = 999999.9f;
	vo6time = 999999.9f;
	vo7time = 999999.9f;
	toolongtomorph = 999999.9f;
	toolongtomorph2 = 999999.9f;
	baseattack1time = 999999.9f;
	escortcalltime = 999999.9f;
	vo10time = 999999.9f;
	toolongtime1 = 999999.9f;
	vo16time = 999999.9f;
//  handles
	nav1 = GetHandle("nav1");
	nav2 = GetHandle("nav2");
	escort1a = GetHandle("escort1a");
	escort1b = GetHandle("escort1b");
	tug1 = GetHandle("tug1");
	shab = GetHandle("shab");
	attack1tanka = NULL;
	attack1tanka = NULL;
	player = NULL;
	ambush1tanka = NULL;
	ambush1tankb = NULL;
	msg2 = NULL;
	msg1 = NULL;
	msg3 = NULL;
//  integers

}

void scion01::AddObject(Handle h)
{
}

void scion01::Execute(void)
{
/*
	Here is where you put what happens every frame.  
*/
	player = GetPlayerHandle();
	

	if (!missionstart)
	{
		Stop(escort1a);
		Stop(escort1b);
		Stop(tug1);
		Stop(shab);
		LookAt(shab,player);
		SetScrap(1,40);
	//	Patrol(shab,"shab_patrol");

		SetObjectiveOn(shab);
		SetObjectiveName(shab,"Shabayev");
		vo1time = (GetTime() + 3);
		missionstart = true;
	}
// If Shabayev ever dies, mission is lost
	if ((!shabdead) && (!IsAlive(shab)))
	{
		FailMission(GetTime() + 10);
		shabdead = true;
	}

	
	
	if ((!vo1) && (vo1time < GetTime()))
	{
		msg1 = AudioMessage("scion0101.wav"); //(shab, I'll patrol while you build base)
		vo2time = (GetTime() + 45);
		vo1 = true;
	}

	if ((!shabgopatrol) && (vo1) && (IsAudioMessageDone(msg1)))
	{
		Patrol(shab,"shab_patrol");
		shabgopatrol = true;
	}

		
	if ((!vo2) && (vo2time < GetTime()))
	{
		msg2 = AudioMessage("scion0102.wav"); // (shab, Try morphing your ship)
		toolongtomorph = (GetTime() + 40);
		vo2 = true;
	}
	
	if (!morphdrill)
	{

		if ((!playermorphed) && (vo2) && (IsAudioMessageDone(msg2) && (IsDeployed(player))))
		{
			AudioMessage("scion0103.wav"); //shab, Good while morhped your ships can use different weapons and tactics.
			vo6time = (GetTime() + 35);
			morphdrill = true;
			playermorphed = true;
		}

		if ((!playermorphed) && (!warned1) && (toolongtomorph < GetTime()))
		{
			AudioMessage("scion0104.wav"); // shab, Cook can you hear me?  Morph your ship by pressing "K"!
			toolongtomorph2 = (GetTime() + 20);
			warned1 = true;
		}

		if ((!playermorphed) && (!warned2) && (toolongtomorph2 < GetTime()))
		{
			AudioMessage("scion0118.wav"); // shab "Sigh, I guess you just don't want to learn"
			warned2 = true;
			baseattack1time = (GetTime() + 45);
			morphdrill = true;
		}
	}

	if ((!vo6) && (vo6time < GetTime()))
	{
		AudioMessage("scion0106.wav"); // Shab, Explaining scion power rules
		baseattack1time = (GetTime() + 100);
		vo6 = true;
	}

	if ((!baseattack1) && (baseattack1time < GetTime()))
	{
		attack1tanka = BuildObject("ivtank",2,"attack1a");
		attack1tankb = BuildObject("ivtank",2,"attack1b");
		SetSkill(attack1tanka,2);
		SetSkill(attack1tankb,2); 
		Attack(shab,attack1tanka);
		Attack(attack1tankb,player);
		vo7time = (GetTime() + 3);
		baseattack1 = true;
	}

	if ((!vo7) && (vo7time < GetTime()))
	{
		AudioMessage("scion0107.wav"); // Hostiles!  John give me a hand! (shab)
		vo7 = true;
	}

	if ((!vo8) && (vo7) && (IsAlive(shab) && (!IsAround(attack1tanka) && (!IsAround(attack1tankb)))))
	{
		AudioMessage("scion0108.wav");// Good job
		Patrol(shab,"shab_patrol");
		escortcalltime = (GetTime() + 90);
		vo8 = true;
	}

	if ((!escortcall) && (escortcalltime < GetTime()))
	{
		AudioMessage("scion0109.wav");// Delta wing--We have the power source, ETA to rondevous is 3 minutes.
		vo10time = (GetTime() + 3);
		escortcall = true;
	}

	if ((!vo10) && (vo10time < GetTime()))
	{
		msg3 = AudioMessage("scion0110.wav"); // shab: Excellent, Delta wing.  Cook you're up.  Take a couple wingmen, and meet them at Nav 1.  Leave a few ships to help me defend the base. (SHAB)
		toolongtime1 = (GetTime() + 45);
		vo10 = true;
	}

	if ((!objectifynav1) && (vo10) && (IsAudioMessageDone(msg3)))
	{
		SetObjectiveOn(nav1);
		SetObjectiveName(nav1,"Nav 1");
		objectifynav1 = true;
	}

	if ((!toolong1) && (!convoygo1) && (toolongtime1 < GetTime()))
	{
		AudioMessage("scion0111.wav");	//shab:  Hurry up Cook!  They could run into trouble out there.
		toolong1 = true;
	}

	if (!rondevous1)
	{
		if ((!convoygo1) && (vo10) && (GetDistance(player,"rondevous1") < 250))
		{
			Retreat(escort1a,"rondevous1");
			Retreat(tug1,"rondevous1");
			Follow(escort1b,tug1);
			convoygo1 = true;
		}
		
		if ((!escort1afarahead) && (GetDistance(escort1a,tug1) > 100))
		{
			Stop(escort1a);
			LookAt(escort1a,tug1);
			escort1aclose = false;
			escort1afarahead = true;
		}

		if ((!escort1aclose) && (escort1afarahead) && (GetDistance(escort1a,tug1) < 50))
		{
			Retreat(escort1a,"rondevous1");
			escort1aclose = true;
			escort1afarahead = false;
		}

		if ((GetDistance(tug1,"rondevous1") < 75 && (GetDistance(tug1,player) < 125)))
		{
		//	Retreat(tug1,nav2);
	
		//	Stop(tug1);
		//	Stop(escort1b);
			AudioMessage("scion0113.wav"); //Delta wing here, we have Cook on radar. Lt. Shabayev, are you sure we can trust this man with the power source?   
			Stop(escort1a);
			SetObjectiveOff(nav1);
			ambush1tanka = BuildObject("ivscout",2,"ambush1a");
			ambush1tankb = BuildObject("ivscout",2,"ambush1b");
			SetSkill(ambush1tanka,3);
			SetSkill(ambush1tankb,3);
			Attack(ambush1tanka,tug1);
			Attack(ambush1tankb,escort1a);
			
			escort_reaction_time = (GetTime() + 5);

			rondevous1 = true;
		}
	}

	if ((!cooknotaround) && (!rondevous1) && (GetDistance(tug1,"rondevous1") < 75 && (GetDistance(tug1,player) > 130)))
	{
		AudioMessage("scion0112.wav"); // Delta wing here, we are at Nav 1 but Cook is not around!
		cooknotaround = true;
	}

	if ((!engage_escorts) && (escort_reaction_time < GetTime()))
	{
	//	SetTeamNum(escort1a,1);
	//	SetTeamNum(escort1b,1);
		SetObjectiveOff(nav1);
		Attack(escort1a,ambush1tanka);
		Attack(escort1b,ambush1tankb);
		AudioMessage("scion0114.wav");  //Delta wing: What the..?!  Ambush!  Protect the tug! 
		engage_escorts = true;
	}

	if (!ambush1over)
	{
		if ((!ambushers1killed) && (rondevous1) && (!IsAlive(ambush1tanka) && (!IsAlive(ambush1tankb))))
		{
			if (IsAlive(escort1a))
			{
				Retreat(escort1a, "escort1a_go1");
			}
			if (IsAlive(escort1b))
			{
				Retreat(escort1b, "escort1b_go1");
			}

			ambushers1killed = true;
			delay1_time = GetTime() + 6;
		}

		if ((!escort1a_look) && (IsAlive(escort1a) && (ambushers1killed) && (GetDistance(escort1a,"escort1a_go1") < 20)))
		{
			LookAt(escort1a,player);
			escort1a_look = true;
		}

		if ((!escort1b_look) && (IsAlive(escort1b) && (ambushers1killed) && (GetDistance(escort1b,"escort1b_go1") < 20)))
		{
			LookAt(escort1b,player);
			escort1b_look = true;
		}	

	}





	if ((!tug_moveout_nav2) && (delay1_time < GetTime()))
	{
	//	SetObjectiveOn(nav2);
	//	SetObjectiveName(nav2,"Nav 2");
		Stop(tug1,0);
		AudioMessage("scion0115.wav");// Delta wing:  We are handing the tug over to cook and heading to base
		vo16time = (GetTime() + 8);
	//	Retreat(tug1,"tug_path1");
		Retreat(escort1a,"escort1a_go2");
		Retreat(escort1b,"escort1b_go2");
		tug_moveout_nav2 = true;
	}

	if ((!vo16) && (vo16time < GetTime()))
	{
		AudioMessage("scion0116.wav"); //shab - Ok Cook, escort the tug to nav 2 where another wing will relieve you.  
		SetObjectiveOn(nav2);
		SetObjectiveName(nav2,"Nav 2");
		vo16 = true;
	}

	if ((!missionwon) && (GetDistance(tug1,nav2) < 50))
	{
		SucceedMission(GetTime() + 10.0f);
		SetObjectiveOff(nav2);
		missionwon = true;
	}
}

