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


/*
	adg1Mission

	Just add water!!
*/


class adg1Mission : public SPMission {
public:
	adg1Mission(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;
	}
	~adg1Mission()
	{
	}

	void Update(void);

	void AddObject(Handle h);

	void ProcessCommand(unsigned long crc);

private:
	void Setup(void);
	void Execute(void);
	void handlePlayerMistakes();
	void sendEnemies(int type, Handle target);
	void playEndCin();
	void adg1Mission::CommandShab();


	// bools
	bool
		b_first,
		startDone, recyDeployed, scavBuilt, powerBuilt,wallsBuilt, BeginCin, BeginSeq,
		ruinsDiscovered, creaturesAttack, shabOutOfVehicle, relicFound,
		shabHurt, troopsAttacked, constBuilt, gunTowBuilt, getNewOrder, shabHasOrder,
		playerOnFoot, justHoped, winMission,
		b_last;

	// floats
	float
		f_first,
		time,
		next_unit,reminderTimer, timeToFail, errorRemTime1, errorRemTime2, 
		f_last;

	// handles
	Handle
		h_first,
		foe, spawn1, spawn2, spawn3, animal1, tree, ruins, tank1, tank2, tank3, rocket1, rocket2,
		rocket3, cons1, cons2, cons3, gtow1, gtow2, gtow3, turr1, turr2, turr3,
		player, enemy1, enemy2, enemy3, enemy4, shabEnemy1, shabEnemy2,
		shabayev, shabOnFoot, playerEnemy1,
		recy,
		h_last;

	// integers
	int
		i_first, ivtankCount, ivrcktCount, ivturrCount, ivscavCount, ibpgenCount,
		i, missionState, shabState,
		i_last;
};

DLLBase *BuildMission(void)
{
	return new adg1Mission();
}

void adg1Mission::Setup(void)
{
	/*
		Here's where you set the values at the start.  
	*/
	next_unit=false;
	startDone=false;
	recyDeployed = false;
	constBuilt = false;
	scavBuilt = false;
	powerBuilt = false;
	gunTowBuilt = false;
	creaturesAttack = false;
	wallsBuilt = false;
	ruinsDiscovered = false;
	shabOutOfVehicle = false;
	shabHasOrder = false;
	shabState = 0;
	relicFound = false;
	shabHurt = false;
	troopsAttacked = false;
	getNewOrder = false;
	BeginCin = false;
	BeginSeq = false;
	playerOnFoot = false;
	justHoped = false;
	winMission = false;
	reminderTimer = 0.0f;
	timeToFail = 0.0f;
	errorRemTime1 = 0.0f;
	errorRemTime2 = 0.0f;
	missionState = 0;
	ivtankCount = 0;
	ivrcktCount = 0;
	ivturrCount = 0;
	ivscavCount = 0;
	ibpgenCount = 0;
	player = NULL;
	shabayev = NULL;
	spawn1 = NULL;
	spawn2 = NULL;
	animal1 = NULL;
	spawn3 = NULL;
	ruins = NULL;
	tree = NULL;
	tank1 = NULL;
	tank2 = NULL;
	tank3 = NULL;
	rocket1 = NULL;
	rocket2 = NULL;
	rocket3 = NULL;
	cons1 = NULL;
	cons2 = NULL;
	cons3 = NULL;
	gtow1 = NULL;
	gtow2 = NULL;
	gtow3 = NULL;
	turr1 = NULL;
	turr2 = NULL;
	turr3 = NULL;
	enemy1 = NULL;
	enemy2 = NULL;
	enemy3 = NULL;
	enemy4 = NULL;
	shabEnemy1 = NULL;
	shabEnemy2 = NULL;
	shabOnFoot = NULL;
	playerEnemy1 = NULL;
}

void adg1Mission::AddObject(Handle h) {

	if ((!scavBuilt) && (IsOdf(h,"ivscav"))) {
		scavBuilt = true;
	}
	else if ((!powerBuilt) && (IsOdf(h,"ibpgen")))	{
		powerBuilt = true;
	}		
	else if ((!wallsBuilt) && (IsOdf(h,"ibwall"))) {
		wallsBuilt = true;
	}
		
	else if ((!constBuilt) && (IsOdf(h,"ivcons"))) {
		constBuilt = true;
	}
	else if ((!gunTowBuilt) && (IsOdf(h,"ibgtow"))) {
		gunTowBuilt = true;
	}
	else if ((!shabOutOfVehicle) && (IsOdf(h,"ispilo"))) {
		if (justHoped) {
			shabOnFoot = h;
			shabOutOfVehicle = true;
		}
	}
	else if ((shabOutOfVehicle) && (IsOdf(h,"asuser"))) {
		playerOnFoot = true;
	}

	if (IsOdf(h,"ivtank")) { //react to player building a tank
		ivtankCount++;
		if (tank1 == NULL) {
			tank1 = h;
			sendEnemies(0, tank1);
		}
		else if (tank2 == NULL) {
			tank2 = h;
			sendEnemies(0, tank2);
		}
		else if (tank3 == NULL) {
			tank3 = h;
			sendEnemies(0, tank3);
		}

	}
	
	else if (IsOdf(h,"ivrckt")) { //react to the player building a rocket tank
		ivrcktCount++;
		if (rocket1 == NULL) {
			rocket1 = h;
			sendEnemies(1, rocket1);
		}
		else if (rocket2 == NULL) {
			rocket2 = h;
			sendEnemies(1, rocket2);
		}
		else if (rocket3 == NULL) {
			rocket3 = h;
			sendEnemies(1, rocket3);
		}
	}
	
	else if (IsOdf(h,"ivcons")) { //react to the player building a construction rig
		ivturrCount++;
		if (cons1 == NULL) {
			cons1 = h;
			sendEnemies(2, cons1);
		}
		else if (cons2 == NULL) {
			cons2 = h;
			sendEnemies(2, cons2);
		}
		else if (cons3 == NULL) {
			cons3 = h;
			sendEnemies(2, cons3);
		}
	}
	
	else if (IsOdf(h,"ivturr")) { //react to the player building a turret
		ivscavCount++;
		if (turr1 == NULL) {
			turr1 = h;
			sendEnemies(3, turr1);
		}
		else if (turr2 == NULL) {
			turr2 = h;
			sendEnemies(3, turr2);
		}
		else if (turr3 == NULL) {
			turr3 = h;
			sendEnemies(3, turr3);
		}
	}
	
	else if (IsOdf(h,"ibgtow")) { //react to the player building a gun tower
		ibpgenCount++;
		if (gtow1 == NULL) {
			gtow1 = h;
			sendEnemies(4, gtow1);
		}
		else if (gtow2 == NULL) {
			gtow2 = h;
			sendEnemies(4, gtow2);
		}
		else if (gtow3 == NULL) {
			gtow3 = h;
			sendEnemies(4, gtow3);
		}
	}

}


void adg1Mission::sendEnemies(int type, Handle target) {
	switch (type) {
		case 0: //tank
			if (!IsAlive(enemy1)) {
				enemy1 = BuildObject("fvtank", 2, spawn1);
				Attack(enemy1, target, 1);
			}
			break;
		case 1: //rocket
			if (!IsAlive(enemy1)) {
				enemy1 = BuildObject("fvsent", 2, spawn1);
				Attack(enemy1, target, 1);
			}
			if (!IsAlive(enemy2)) {
				enemy2 = BuildObject("fvsent", 2, spawn2);
				Attack(enemy2, target, 1);
			}
			break;
		case 2: //constructor
			if (!IsAlive(enemy1)) {
				enemy1 = BuildObject("fvtank", 2, spawn1);
				Attack(enemy1, target, 1);
			}
			break;
		case 3: //turret
			if (!IsAlive(enemy1)) {
				enemy1 = BuildObject("fvarch", 2, spawn1);
				Attack(enemy1, target, 1);
			}
			if (!IsAlive(enemy2)) {
				enemy2 = BuildObject("fvarch", 2, spawn2);
				Attack(enemy2, target, 1);
			}
			break;
		case 4: //gun tower
			SetIndependence(enemy1, 0);
			SetIndependence(enemy2, 0);
			enemy1 = BuildObject("fvtank", 2, spawn1);
			enemy2 = BuildObject("fvtank", 2, spawn2);
			enemy3 = BuildObject("fvsent", 2, spawn3);
			Attack(enemy1, target, 1);
			Attack(enemy2, target, 1);
			Defend2(enemy3, enemy1, 1);
			break;
	}


}

 //Moved to cininterface file
void adg1Mission::ProcessCommand(unsigned long crc) {
	//CinProcessCommand(crc);
	if (crc == 3663682790) {
		BeginCin = true;
	}
	if (crc == 220803983) {
		BeginSeq = true;
	}
 } 

void checkWing(Handle wingName, Handle player) {
	// this is checking to make sure the player does not attack his wingman /////////

	if ((IsAlive(wingName)) && (IsAlive(player)))
	{
		if (GetTime() - GetLastFriendShot(wingName) < .2f)
		{
		//	AudioMessage("shotwing.wav"); // (shab) //watch your firing!
		}
	}
}

void adg1Mission::CommandShab() {
	switch (shabState) {
		case 0: //setup the first batch of enemies
			shabEnemy1 = BuildObject("fvsent", 2, spawn1);
			shabEnemy2 = BuildObject("fvsent", 2, spawn2);
			Attack(shabEnemy1, shabayev, 1);
			Attack(shabEnemy2, shabayev, 1);
			Attack(shabayev, shabEnemy1, 1);
			shabState++;
			break;
		case 1: //do nothing until the first enemy gets killed
			if (!IsAlive(shabEnemy1)) {
				if (IsAlive(shabEnemy2)) {
					Attack(shabayev, shabEnemy2, 1);
					shabState++;
				}
				else {
					shabState++;
				}
			}

			if (gunTowBuilt) { //If shab can't kill anybody do it for her
				Damage(shabEnemy1, 3000);
				if (IsAlive(shabEnemy2)) {
					Damage(shabEnemy2, 3000);
				}
				ClearObjectives();
				AddObjective("goRuins.otf", WHITE);
				Goto(shabayev, tree, 1);
				shabState = 3;
			}

			break;
		case 2: //go NEAR the ruin when all of the enemies are killed
			if ((gunTowBuilt) && (IsAlive(shabEnemy2))) { //If shab can't kill anybody do it for her
				Damage(shabEnemy2, 3000);
			}
			if ((!IsAlive(shabEnemy1)) && (!IsAlive(shabEnemy2)) && (gunTowBuilt)) {
				ClearObjectives();
				AddObjective("goRuins.otf", WHITE);
				Goto(shabayev, tree, 1);
				shabState++;
			}
			if ((!IsAlive(shabEnemy1)) && (!IsAlive(shabEnemy2)) && (!gunTowBuilt)) {
				shabEnemy2 = BuildObject("fvsent", 2, spawn1);
				Attack(shabayev, shabEnemy2, 1);
				Attack(shabEnemy2, shabayev, 1);

			}

			
			break;
		case 3: //hop out of the vehicle when near the ruin
			if (justHoped) {
				LookAt(shabOnFoot, ruins, 1);
				shabState++;
			}
			if (((GetDistance(shabayev, tree) < 10.0)) && (!IsAlive(enemy1)) && (!IsAlive(enemy2))) {
				ClearObjectives();
				AddObjective("checkThis.otf", WHITE);
				justHoped = true;
				HopOut(shabayev);
				SetObjectiveOn(shabOnFoot);
			}
			break;
		case 4: //order the player to get out of his vehicle once he gets close
			if ((GetDistance(shabOnFoot, player) < 50.0)) {
				ClearObjectives();
				AddObjective("getOut.otf", WHITE);
				shabState++;
			}
			break;
		case 5: //send shab to get the relic
			if (playerOnFoot) {
				Goto(shabOnFoot, ruins, 1);
				//we should start the patrols here
				shabState++;
			}
			break;
		case 6: //get shab hurt
			if ((GetDistance(shabOnFoot, ruins)) < 30.0f) {
				if (winMission == false) {
					SucceedMission(100);
					winMission = true;
				}
			}
			break;
		case 7: //hide shab
			
			break;
		case 8: //play closing cineractive

			break;
	}

	if (GetHealth(shabayev) < 0.7f) { //keep shab alive
		AddHealth(shabayev, 100);
	}

	if ((GetAmmo(shabayev)) < 0.5f) {
		AddAmmo(shabayev, 500);
	}
}

void adg1Mission::handlePlayerMistakes() {
	if (missionState < 6) {	//Not enough scrap
		if ((GetScrap(1) < 6) && (GetTime() > errorRemTime1)) {
			ClearObjectives();
			AddObjective("getScrap.otf", WHITE);
			errorRemTime1 = (GetTime() + 30.0f);
		}
		else if (GetScrap(1) > 4) {
			errorRemTime1 = 0;
		}

		if (((GetAmmo(player)) < 0.4f) && (GetTime() > errorRemTime2)) {
			AudioMessage("GetAmmo.wav");
			errorRemTime2 = (GetTime() + 30.0f);
		}
		
		else if (((GetHealth(player)) < 0.4f) && (GetTime() > errorRemTime2))  {
			AudioMessage("GetHealth.wav");
			errorRemTime2 = (GetTime() + 30.0f);
		}
		else if ((GetHealth(player) > 0.4f) && (GetAmmo(player) > 0.4f)) {
			errorRemTime2 = 0;
		}

	}

	if (!IsAlive(shabayev)) {	//kills shabayev
	//	AudioMessage("Demo0304.wav"); // "Cooke.  You're being shipped back to Earth where you'll face court marshall for killing a commanding officer.
	//	FailMission(GetTime() + 15.0f);
	}

/*	if (!IsAlive(player)) { //gets killed
	//	AudioMessage("Demo0304.wav"); // you loose go to shell screen
		FailMission(GetTime() + 15.0f);
	}*/

	if (missionState == 6) {//does not follow shabayev
		if ((GetDistance(player, shabayev) > 400.0f) && (GetTime() < timeToFail)) {
		//	AudioMessage("tooFar.wav"); //"Cooke, you're getting too far from my wing.  Fall in.
			timeToFail = GetTime() + 60.0f;
		}
		else if ((GetDistance(player, shabayev) > 400.0f) && (GetTime() > timeToFail)) {
		//	AudioMessage("fail.wav"); //"Cooke, I'm not interested in working with a wingman who won't take my wing.""
		}
		if (GetDistance(player, shabayev) < 400.0f) {
			timeToFail = 0.0f;
		}


	}
	if (missionState == 7) {//does not get out of his vehicle

	}
	if (missionState == 9) {//ignores shabayev's need for help
		if ((GetDistance(player, shabayev) > 40.0f) && (GetTime() < timeToFail)) {
		//	AudioMessage("tooFar.wav"); //"Cooke, you're getting too far from my wing.  Fall in.
			timeToFail = GetTime() + 60.0f;
		}
		if (GetDistance(player, shabayev) < 40.0f) {
			timeToFail = 0.0f;
		}

	}
}


void adg1Mission::playEndCin() {
}

void adg1Mission::Execute(void) {
/*	if (missionState == 0) {
		missionState++;
	}

	if (BeginCin == true) {
		bool sequence = false;
		BeginCin = PlaySequence(sequence);
	}

	if (BeginSeq == true) {
		bool sequence = true;
		BeginSeq = PlaySequence(sequence);
	} */

 /*
	//This section evaluates what has happened.
	checkWing(shabayev, player); //Did we hurt our friendly instructor.
	switch (missionState) {
		case 0: //This will do the setup for the mission.
			player = GetPlayerHandle();
			spawn1 = GetHandle("spawn_1");
			spawn2 = GetHandle("spawn_2");
			spawn3 = GetHandle("spawn_3");
			shabayev = GetHandle("shabayev");
			recy = GetHandle("recycler");
			ruins = GetHandle("ruins");
			tree = GetHandle("tree");
			SetScrap(1, 30);
			playerEnemy1 = BuildObject("fvtank", 2, spawn3);
			Attack(playerEnemy1, player, 1);
			CommandShab();
			missionState++;
			break;
		case 1: //Check to see if the player has deployed their recycler.
			if (!getNewOrder) {
				ClearObjectives();
				AddObjective("bldrec.otf", WHITE);
				reminderTimer = (GetTime() + 60.0f);
				if (!IsAlive(animal1)) {
					animal1 = BuildObject("manimal", 2, spawn3); //This will be changed to the animal
					Goto(animal1, recy);
					SetIndependence(animal1, 0);
				}
				getNewOrder = true;
			}
			recyDeployed = IsDeployed(recy);
			if (recyDeployed) {
				missionState++;
				getNewOrder = false;
			}	
			if ((!IsAlive(playerEnemy1)) && (!IsAlive(enemy1))) {
				playerEnemy1 = BuildObject("fvtank", 2, spawn3);
				Attack(playerEnemy1, player, 1);
			}
			break;
		case 2: //Check to see if the player has built a scavenger.
			if (!getNewOrder) {
				ClearObjectives();
				AddObjective("bldScav.otf", WHITE);
				reminderTimer = (GetTime() + 60.0f);
				getNewOrder = true;
			}
			if (scavBuilt) {
				missionState++;
				getNewOrder = false;
			}
			if ((!IsAlive(playerEnemy1)) && (!IsAlive(enemy1))) {
				playerEnemy1 = BuildObject("fvtank", 2, spawn3);
				Attack(playerEnemy1, player, 1);
			}

			break;
		case 3: //Check to see if the player has built a construction rig.
			if (!getNewOrder) {
				ClearObjectives();
				AddObjective("bldCons.otf", WHITE);
				reminderTimer = (GetTime() + 60.0f);
				getNewOrder = true;
			}
			if (constBuilt) {
				missionState++;
				getNewOrder = false;
			}
			break;
		case 4: //Check to see if the player has built a power plant.
			if (!getNewOrder) {
				ClearObjectives();
				AddObjective("bldPow.otf", WHITE);
				reminderTimer = (GetTime() + 60.0f);
				getNewOrder = true;
			}
			if (powerBuilt) {
				missionState++;
				getNewOrder = false;
			}
			if ((!IsAlive(playerEnemy1)) && (!IsAlive(enemy1))) {
				playerEnemy1 = BuildObject("fvtank", 2, spawn3);
				Attack(playerEnemy1, player, 1);
			}

			break;
		case 5: //Check to see if the player has built a gun tower
			if (!getNewOrder) {
				ClearObjectives();
				AddObjective("bldGunT.otf", WHITE);
				reminderTimer = (GetTime() + 60.0f);
				getNewOrder = true;
			}
			if (gunTowBuilt) {
				missionState++;
				getNewOrder = false;
			}
			break;
		//The remainder of this mission is controled in CommandShab() because
		//the player is just supposed to follow her. 
	}
	float currentTime = GetTime();
	if ((reminderTimer < currentTime) && (getNewOrder)) { //This checks to see if we should remind the player of his order.
		getNewOrder = false;
	}
	CommandShab();
	handlePlayerMistakes(); 
 */
 }
