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

class Misn04Mission : public SPMission 
{
public:
	Misn04Mission(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,
		send_pilot1,move_out,
		ship1rdy,ship2rdy,ship3rdy,shipsrdy,
		ship1_look,ship2_look,ship3_look,
		ship1_look2, ship2_look2, ship3_look2,
		ship1_look3, ship2_look3, ship3_look3,
		ship1_look4, ship2_look4, ship3_look4,
		arrive_point1,
		fight1,
		ship1go,ship2ship3go,
		ship1defeat,
		ship2defeat,
		ship3defeat,
		ship4defeat,ship4defeata,
		alldefeated,
		ally,
		ship1goinghome,
		ship2goinghome,
		ship3goinghome,
		ship1gobase,
		ship2gobase,
		ship3gobase,
		playerdead,playerdead2,
		ship1look3,ship2look3,ship3look3,
		commspoke,
		finalmsg,
		ship1defeatedearly,ship2defeatedearly,ship3defeatedearly,
		ship1goback2,ship2goback2,ship3goback2,
		taunt1,
		b_last;

	// floats
	float
		f_first,
		move_time,
		wait_time1,
		wait_time2,
		wait_time3,
		wait_time4,
		f_last;

	// handles
	Handle
		h_first,
		player,
		pilot1,pilot2,pilot3,
		ship1,ship2,ship3,ship4,
		lookbunk,
		h_last;


	// integers
	int
		i_first,
		playerhealth,
		ship1health,ship2health,ship3health,ship4health,
		i_last;
};
DLLBase * BuildMission(void)
{
	return new Misn04Mission();
}

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

//  bools
	send_pilot1 = false;
	move_out = false;
	ship1rdy = false;
	ship2rdy = false;
	ship3rdy = false;
	arrive_point1 = false;
	fight1 = false;
	ship2ship3go = false;
	ship1defeat = false;
	ship2defeat = false;
	ship3defeat = false;
	ship4defeat = false;
	ship4defeata = false;
	ship1go = false;
	alldefeated = false;
	ally = false;
	shipsrdy = false;
	ship1goinghome = false;
	ship2goinghome = false;
	ship3goinghome = false;
	ship1gobase = false;
	ship2gobase = false;
	ship3gobase = false;
	ship1_look = false;
	ship2_look = false;
	ship3_look = false;
	ship1_look2 = false;
	ship2_look2 = false;
	ship3_look2 = false;
	ship1_look3 = false;
	ship2_look3 = false;
	ship3_look3 = false;
	playerdead = false;
	playerdead2 = false;
	ship1look3 = false;
	ship2look3 = false;
	ship3look3 = false;
	commspoke = false;
	finalmsg = false;
	ship1defeatedearly = false;
	ship2defeatedearly = false;
	ship3defeatedearly = false;
	ship1goback2 = false;
	ship2goback2 = false;
	ship3goback2 = false;
	ship1_look4 = false;
	ship2_look4 = false;
    ship3_look4 = false;
	taunt1 = false;
	
//  floats
	move_time = 6.0f;
    wait_time1 = 999999.9f;
	wait_time2 = 999999.9f;
	wait_time3 = 999999.9f;
	wait_time4 = 999999.9f;
	
	
//  handles
	player = GetHandle("player");
	pilot1 = GetHandle("pilot1");
	pilot2 = GetHandle("pilot2");
	pilot3 = GetHandle("pilot3");
	ship1 = GetHandle("ship1");
	ship2 = GetHandle("ship2");
	ship3 = GetHandle("ship3");
	ship4 = GetHandle("ship4");
	lookbunk = GetHandle("lookbunk");


//  integers
	playerhealth = 2000;
	ship1health = 2000;
	ship2health = 2000;
	ship3health = 2000;
	ship4health = 2000;




}

void Misn04Mission::AddObject(Handle h)
{
}

void Misn04Mission::Execute(void)
{
/*
	Here is where you put what happens every frame.  
*/

	player = GetPlayerHandle();

	// this holds the pilots until I'm ready
	

	if (!ally)
	{
		Ally(0,1);
		ClearObjectives();
		AddObjective("isdf0101.otf",WHITE,20.0f);
		SetCurAmmo(ship4,0);
		ally = true;
	}

	if (!move_out)
	{
		if (IsAlive(pilot1))
		{
			Stop(pilot1);
		}
			
		if (IsAlive(pilot2))
		{
			Stop(pilot2);
		}

		if (IsAlive(pilot3))
		{
			Stop(pilot3);
		}
	}
	
	// this is the time when I move the pilots
	if ((move_time < GetTime()) && (!move_out))
	{
		Goto(pilot1,ship1);  
		Goto(pilot2,ship2);
		Goto(pilot3,ship3);
		AudioMessage("wohoo.wav");
		ClearObjectives();	
		AddObjective("isdf0105.otf",WHITE,20.0f);
		move_out = true;
		
	}

	// this will tell me when ships are ready to move
	if (!ship1rdy)
	{
		Handle temp = ship1;

		if (IsAlive(temp))
		{
			AddHealth(ship1,2000);
			ClearObjectives();

			ship1rdy = true;
		}
	}

	
	
		if (!ship2rdy)
		{
			Handle temp = ship2;

			if (IsAlive(temp))
			{
				ship2rdy = true;
			}
		}
		
	
	
	if (!ship3rdy)
	{
		Handle temp = ship3;

		if (IsAlive(temp))
		{
			ship3rdy = true;
		}
	}

//Telling the ships to move from base to fighting area

		if (!shipsrdy)
		{
			if ((ship1rdy) && (ship2rdy) && (ship3rdy))
			{
				Goto(ship1,"ship1_path1");
				Goto(ship2,"ship2_path1");
				Goto(ship3,"ship3_path1");
				shipsrdy = true;
			}
		}


	if (shipsrdy)
	{
		if ((!ship1_look) && (GetDistance(ship1,"ship1_stop1") < 15.0f))
		{
			LookAt (ship1,lookbunk);
			ship1_look = true;
		}

		if ((!ship2_look) && (GetDistance(ship2,"ship2_stop1") < 15.0f))
		{
			LookAt(ship2,lookbunk);
			ship2_look = true;
		}

		if ((!ship3_look) && (GetDistance(ship3,"ship3_stop1") < 15.0f))
		{
			LookAt (ship3,lookbunk);
			ship3_look = true;
		}

		if (!arrive_point1) 
		{
			if ((ship1_look) && (ship2_look) && (ship3_look))
			{				
				wait_time1 = GetTime() + 3.0f;
				wait_time4 = GetTime() + 25.0f;
				arrive_point1 = true;
			}
		}
	}

//if player doesn't arrive to fight point within 45'
	if ((!taunt1) && (!fight1) && (wait_time4 < GetTime()))

	{
		ClearObjectives();	
		AddObjective("isdf0111.otf",WHITE,20.0f);
		taunt1 = true;
	}




	//This is the battle between ship 1 and player

	

	if ((!fight1) && (wait_time1 < GetTime()) && (GetDistance(player,ship1) < 125.0f))
	{
		SetPerceivedTeam(player,3);
		UnAlly(0,1);
		SetCurAmmo(ship4,1600);
		Attack(ship1,player);
		ClearObjectives();
		AddObjective("isdf0103.otf",WHITE,20.0f);
		fight1 = true;
	}

	
	if ((fight1) && (!ship2ship3go))
	{
		Stop (ship2);
		Stop (ship3);
	}

	//when ship 1 is defeated...
	if ((fight1) && (IsAlive(ship1)))
		{
			ship1health = GetCurHealth(ship1);
		}
	
	if ((fight1) && (!ship1defeat) && (ship1health < 150) && (!playerdead))
		{
			
			ClearObjectives();	
			SetCurHealth(player, 2500);
			
			if (!ship2ship3go)
				{
				
					AddObjective("isdf0104.otf",WHITE,20.0f);

				}
			ship1defeat = true;
		}

	//makes  invulneraable
	if  (ship1defeat)
		{
			SetCurHealth(ship1,70);
		}
//ship1going back to base
	if ((!ship1goinghome) && (ship1defeat) && (!playerdead))
		
	{
		if (!ship1gobase)
		{
			AudioMessage("wohoo.wav");
			Retreat(ship1,"ship1defeatpoint");
			ship1gobase = true;
		}

		if ((!ship1_look2) && (GetDistance(ship1,"ship1defeatpoint")) < 25.0f)
		{
			LookAt(ship1,lookbunk);
			ship1_look2 = true;
			ship1goinghome = true;
		}	
	
	}



	//these are the conditions under which ships 1, 2, 3 will attack player


	
	if ((fight1) && (!ship2ship3go) && (ship1defeat) && (!ship4defeat))
	{
		Attack(ship2,player);
		Attack(ship3,player);
		ship2ship3go = true; 
	}	

	if ((fight1) && (!ship2ship3go) && (GetHealth(ship2) < .75) && (!ship4defeat))
	{
		Attack(ship2,player);
		Attack(ship3,player);
		ClearObjectives();	
		AddObjective("isdf0106.otf",WHITE,20.0f);
		ship2ship3go = true;
	}

	
     if ((fight1) && (!ship2ship3go) && (GetHealth(ship3) < .75) && (!ship4defeat))
	{
		Attack(ship2,player);
		Attack(ship3,player);
		ClearObjectives();	
		AddObjective("isdf0106.otf",WHITE,20.0f);
		ship2ship3go = true;
	}



	 //when ship2 is defeated...

	if ((fight1) && (IsAlive(ship2)))
	
		{
			ship2health = GetCurHealth(ship2);
		}
	
	if ((!ship2defeat) && (ship2health < 150))
	
		{
			ship2defeat = true;
		}

	
	if  (ship2defeat)
	
	
		{
			SetCurHealth(ship2,70);
		}

	if ((!ship2goinghome) && (ship2defeat) && (!playerdead))
		
	{
		if (!ship2gobase)
		{
			AudioMessage("wohoo.wav");
			Retreat(ship2,"ship2defeatpoint");
			ship2gobase = true;
		}

		if ((!ship2_look2) && (GetDistance(ship2,"ship2defeatpoint")) < 25.0f)
		{
			LookAt(ship2,lookbunk);
			ship2_look2 = true;
			ship2goinghome = true;
		}	
	
	}


	//WHEN SHIP 3 IS DEFEATED...

		if ((fight1) && (IsAlive(ship3)))
	
		{
			ship3health = GetCurHealth(ship3);
		}
	
	if ((!ship3defeat) && (ship3health < 150))
	
		{
			ship3defeat = true;
		}

	
	if  (ship3defeat)
	
	
		{
			SetCurHealth(ship3,70);
		}

	if ((!ship3goinghome) && (ship3defeat) && (!playerdead))
		
	{
		if (!ship3gobase)
		{
			AudioMessage("wohoo.wav");
			Retreat(ship3,"ship3defeatpoint");
			ship3gobase = true;
		}

		if ((!ship3_look2) && (GetDistance(ship3,"ship3defeatpoint")) < 25.0f)
		{
			LookAt(ship3,lookbunk);
			ship3_look2 = true;
			ship3goinghome = true;
		}	
	}


//WHEN ALL SHIPS DEFEATED
	if ((!alldefeated) && (ship1defeat) && (ship2defeat) && (ship3defeat) && (!playerdead))
	{
		SetPerceivedTeam(player,1);
		Ally(0,1);
		ClearObjectives();	
		SetCurAmmo(ship4,0);
		SetCurHealth(ship4,2000);
		AddObjective("isdf0107.otf",WHITE,20.0f);
		wait_time2 = GetTime() + 6.0f;
		wait_time3 = GetTime() + 14.0f;
		alldefeated = true;

	}

	if ((!commspoke) && (wait_time2 < GetTime()))
	{
		ClearObjectives();	
		AddObjective("isdf0108.otf",WHITE,20.0f);
		commspoke = true;
	}

	


	// when player returns to base
	if ((!finalmsg) && (!playerdead) && (commspoke) && (wait_time3 < GetTime()))
	{
		ClearObjectives();	
		AddObjective("isdf0109.otf",WHITE,20.0f);
		finalmsg = true;
	}

		if ((!finalmsg) && (playerdead) && (commspoke) && (wait_time3 < GetTime()))
	{
		ClearObjectives();	
		AddObjective("isdf0109.otf",WHITE,20.0f);
		finalmsg = true;
	}

//IF THE PLAYER IS DEFEATED....
	if ((fight1) && (IsAlive(ship4)))
	
		{
			ship4health = GetCurHealth(ship4);
		}
	
	if ((!ship4defeat) && (ship4health < 150))
	
		{
			playerdead = true;
			ship4defeat = true;
			
		}

	
	if  (ship4defeat)
	
	
		{
			SetCurHealth(ship4,70);
				
			if (!ship4defeata)
			{
				SetPerceivedTeam(player,1);
				Ally(0,1);
				ship2ship3go = true;
				Retreat(ship1,"ship1defeatpoint");
				Retreat(ship2,"ship2defeatpoint");
				Retreat(ship3,"ship3defeatpoint");
				ClearObjectives();
				AddObjective("isdf0102.otf",WHITE,20.0f);
				wait_time2 = GetTime() + 6;
				wait_time3 = GetTime() + 14.0f;
				SetCurAmmo(ship4,0);
				ship2ship3go = true;
				ship4defeata = true;
			}
	
			if ((!ship1look3) && (GetDistance(ship1,"ship1defeatpoint") < 20.0f))
			{
				LookAt(ship1,lookbunk);
				ship1look3 = true;
			}
		
			
			if ((!ship2look3) && (GetDistance(ship2,"ship2defeatpoint") < 20.0f))
			{
				LookAt(ship2,lookbunk);
				ship2look3 = true;
			}
	


			if ((!ship3look3) && (GetDistance(ship3,"ship3defeatpoint") < 20.0f))
			{
				LookAt(ship3,lookbunk);
				ship3look3 = true;
			}
	}





}