#include "..\Shared\SPMission.h"
#include <math.h>


/*
	demo01Mission
	Just add water!!
*/

#define NUM_SENTRIES 1
#define SENT_WAVE 4
#define NUM_FVTANKS 1
#define TANK_WAVE 4
#define NUM_FVTURRS 9 //12 total.  8 For first section of mission.
#define NUM_SQUAD1 2
#define NUM_SQUAD2 2
#define NUM_FRIENDS (NUM_SQUAD1 + NUM_SQUAD2) //Sum of friend vehicles should not exceed this number
#define NUM_ANIMALS 4
#define COMMAND 0
#define TEST_END 1

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

	bool PostLoad(bool missionSave);

	void Update(void);

	void AddObject(Handle h);
	void DeleteObject(Handle h);


private:
	void Setup(void);
	void Execute(void);
	Handle demo01Mission::FindTurrTarget();
	Handle demo01Mission::FindSentTarget();
	void demo01Mission::DoService(Handle truck);
	void demo01Mission::CheckLoseConditions();
	void demo01Mission::CommandMortarBikes();
	void demo01Mission::SkipCin();
	void demo01Mission::SetDifficulty(Handle object, int level);
	void demo01Mission::ProcessCommand(unsigned long crc);
	// bools
	bool
		b_first, moviePlaying, stateSetup, turrDead, firstTry, needTarget,
		sentDead, tankDead, serviceBusy, followPlayer, moveVehicle, peopleRescued,
		landing, close, camObj, out_of_ship, resetCam, lost, timer,
		powerUpSent, skipCin, gunWarning, firstStrike, b_last;

	// floats
	float
		f_first, delayTime, delayTime2, maxFrames, r, remindTime,
		f_last;

	// handles
	Handle
		h_first, player, enemyTurrs[NUM_FVTURRS], enemySentries[NUM_SENTRIES],
		friendHandle[NUM_FRIENDS], tug, data, dropship, enemyTanks[NUM_FVTANKS],
		activeTargets[NUM_FRIENDS], service, target, crashsite, droptemp, droptemp2,
		animal[NUM_ANIMALS], person1, person2, person3,oldplayer, guntow1, guntow2, apc, p1, p2, p3, p4,
		shootHere, playerKiller, playerVehicle, smoke, h_last;

	// integers
	int
		i_first, missionState,animod,tankmod, dropCount, shipCount,sentDeathCount,tankDeathCount,
		currentObjective, loopCount, tankWave, sentWave, movieState,
		tankTimer,sentTimer,counter, height, missionCommand, deadTurrs, difficulty,
		i_last;
};

int saveCrash;
Vector dropPosition;
char *objectBuffer;
unsigned long bufferSize;

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

void demo01Mission::Setup(void)
{
	/*
		Here's where you set the values at the start.  
	*/
	oldplayer=NULL;
	missionCommand = 0;
	missionState = 0;
	IFace_SetInteger("script.mission.state", missionState);
	IFace_CreateString("script.demo.intro", "");
	IFace_CreateString("script.demo.vehicle", "");
	IFace_CreateInteger("script.mission.command", missionCommand);
	missionCommand = IFace_GetInteger("script.mission.command");
	IFace_CreateCommand("script.skip.cin");
	IFace_SetInteger("script.mission.command", missionCommand);
	difficulty = IFace_GetInteger("options.play.difficulty");
	animod=0;
	tankmod=0;
	delayTime = 0.0;
	shipCount = 0;
	camObj = true;
	sentDeathCount = 0;
	sentDead = true;
	sentTimer = 0;
	sentWave = 0;
	tankDeathCount = 0;
	tankDead = true;
	tankWave = 0;
	tankTimer = 0;
	currentObjective = -1; //DEBUG
	dropCount=10; //70
	moviePlaying = false;
	stateSetup = false;
	firstTry = true;
	turrDead = false;
	serviceBusy = false;
	followPlayer = true;
	needTarget = false;
	landing = true;
	peopleRescued = false;
	timer = false;
	firstStrike = false;
	maxFrames = 0.0f;
	loopCount = 0;
	counter = 0;
	movieState = 0;
	close = false;
	resetCam = false;
	lost = false;
	powerUpSent = false;
	skipCin = false;
	gunWarning = false;
	apc = NULL;
	r = 0.0;
	height = 0;
	deadTurrs = 0;
	PreloadODF("fvsent");
	PreloadODF("fvtank");
	PreloadODF("ivmbike");
	PreloadODF("ivserv");
	PreloadODF("ivstas");
	PreloadODF("ivapc");
	PreloadODF("ispilo");
	PreloadODF("mcjak01");
	PreloadODF("jak_kill");
	PreloadODF("ivdrop_sh");
	PreloadODF("ivdrop_fly");
	PreloadODF("ivdrop2_ld");
	PreloadODF("ivdrop2_fl");
	PreloadODF("crashsite");
}

bool demo01Mission::PostLoad(bool missionSave)
{
	if (!missionSave)
	{
		IFace_SetInteger("script.mission.state", missionState);
		IFace_CreateInteger("script.mission.difficulty", 1);
		IFace_CreateInteger("script.mission.command", 0);
		IFace_CreateInteger("script.mission.ammo", 1);
		IFace_CreateInteger("script.mission.repair", 1);
		IFace_CreateString("script.mission.vehicle", "");
		IFace_CreateString("script.demo.intro", ""); 
	}
	return SPMission::PostLoad(missionSave);
}

void demo01Mission::AddObject(Handle h) {


	if (IsOdf(h,"satchel")) {
		StartCockpitTimer(30, 15, 5);
		timer = true;
	}

/*	if ((!scavBuilt) && (IsOdf(h,"ivscav"))) {
		scavBuilt = true;
	}*/


}

void demo01Mission::ProcessCommand(unsigned long crc) {

	if (crc == 3271508811) {
		skipCin = true;		
	}
	
} 


void demo01Mission::DeleteObject(Handle h) {
	if (IsOdf(h,"fvturr")) {
		turrDead = true;
		if (missionState == 10) {
			deadTurrs++;
		}
	}
	
	if (IsOdf(h,"fbspir")) {
		turrDead = true;
	}

	if (IsOdf(h,"ivmbike")) {
		CheckLoseConditions();
	}

	if (IsOdf(h,"ivserv")) {
		CheckLoseConditions();
		if (currentObjective != -1) {
			turrDead = true;
			currentObjective = -1;
		}
	}
	
	if (IsOdf(h,"ivtank") && (missionState >= 10)) {
		if (!IsAround(playerVehicle)) {
			playerKiller = BuildObject("fvtank", 2, "spawn2");
			player = GetPlayerHandle();
			Attack(playerKiller, player);
		}
	}

	if (IsOdf(h,"ivstas")) {
		CheckLoseConditions();
	}
	
	if (IsOdf(h,"fvsent")) {
		if (sentDeathCount < NUM_SENTRIES) {
			sentDeathCount++;
		}
		if (sentDeathCount == NUM_SENTRIES) {
			if ((sentWave < SENT_WAVE) && (GetDistance(player, "spawn2") > 75.0)) {
				sentDead = true;
				sentDeathCount = 0;
				sentTimer=600;
				sentWave++;
			}
		}
	}
	if (IsOdf(h,"fvtank")) {
		if (tankDeathCount < NUM_FVTANKS) {
			tankDeathCount++;
		}
		if (tankDeathCount == NUM_FVTANKS) {
			if ((tankWave < TANK_WAVE) && (GetDistance(player, "spawn2") > 75.0)) {
				tankDead = true;
				tankDeathCount = 0;
				tankTimer=500;
				tankWave++;
			}
		}
	}
}

		/*if (moveVehicle) {
			moveVehicle = PlaybackVehicle("oscar");
		} */


void demo01Mission::Execute(void) {


	missionState = IFace_GetInteger("script.mission.state");
	
	if (missionState >= 10) {
		player = GetPlayerHandle();
		if (IsOdf(player,"isuser"))
		{
			if (!out_of_ship) GiveWeapon(player, "igsatc");
			out_of_ship = true;
		}
		else
		{
			out_of_ship = false;
		}

	}

	if (timer) { //Monitor the cockpit timer for the satchel charge.
		if (GetCockpitTimer() == 0.0) {
			StopCockpitTimer();
			HideCockpitTimer();
			timer = false;
		}
	}


	switch (missionState) {
	case 0: //Setup configuration sceen.
		if (IsRecording())
		{
			missionState = 4;
			IFace_SetInteger("script.mission.state", missionState);
			char scriptString[32] = "";
			IFace_GetString("script.string", scriptString, sizeof(scriptString));
			if (scriptString[0] == 'c')
			{
				missionCommand = 1;
				IFace_SetInteger("script.mission.command", missionCommand);
			}
			break;

		}
		SaveObjects(objectBuffer, bufferSize);
		FreeCamera();
		Vector pos, dir;
		GetCameraPosition(pos, dir);
		pos.x += 10;
		SetCameraPosition(pos, dir);
		IFace_SetInteger("script.mission.state", missionState);
		IFace_Exec("demo01_setup.cfg");
		IFace_Exec("demo01_intro.cfg");
		IFace_Activate("Demo01Intro");
		stateSetup = false;
		missionState++;
		break;
	case 1: //Loop while in intro screen
		if (counter == 0) {
				animal[0] = BuildObject("mcjak01", 0, "animSpawn");
				Vector pos;
				pos = GetPosition(animal[0]);
				pos.x += 30;
				pos.z += 30;
				animal[1] = BuildObject("mcjak01", 0, pos);
				counter++;
		}
		break;
	case 2: //Loop while in configuration screen.
		if (!stateSetup) {
			IFace_Activate("Demo01Setup");
			stateSetup = true;
		}
		break;
	case 3: //Cleanup configuration screen.
		LoadObjects(objectBuffer, bufferSize);
		FreeFinish();
		missionState++;
		stateSetup = false;
		break;
	case 4: //"This touch down site is hot. We're going in."
		if (!stateSetup) { //Essential mission setup.
			player = GetPlayerHandle();
			playerVehicle = GetPlayerHandle();
			missionCommand = IFace_GetInteger("script.mission.command");
			guntow2 = GetHandle("guntow2");
			SetSkill(guntow2, 0);
			for (int i=0; i < NUM_FVTURRS; i++) {
				char name[20];
				sprintf(name, "turr%d", i);
				enemyTurrs[i] = GetHandle(name);
				SetDifficulty(enemyTurrs[i], difficulty);
			}
			droptemp = BuildObject("ivdrop_fly", 1, "droptemp");
			SetAnimation(droptemp, "fly", 0);
			droptemp2 = BuildObject("ivdrop2_fl", 1, "droptemp");
			SetAnimation(droptemp2, "fly", 0);
			shootHere = droptemp;//BRAD
			Vector pos;
			pos = GetPosition(droptemp);
			pos.y += 50;
			pos.z -= 70;
			SetVectorPosition(droptemp, pos);
			SetVectorPosition(shootHere, pos);
			pos.z -= 170;
			pos.y -= 20;
			pos.z += 30;
			SetVectorPosition(droptemp2, pos);
			enemySentries[0] = BuildObject("fvsentCin", 2, "crashSite");
			{ //BRAD
				//Stop(enemySentries[0]);
				//Annoy(droptemp, enemySentries[0]);
				SetSkill(enemySentries[0], 3);
				LookAt(enemySentries[0], shootHere);
				SetSkill(enemyTurrs[NUM_FVTURRS-1], 3);
				LookAt(enemyTurrs[NUM_FVTURRS-1], droptemp);
				SetSkill(enemyTurrs[NUM_FVTURRS-2], 3);
				LookAt(enemyTurrs[NUM_FVTURRS-2], droptemp);
			}
			CameraReady();
			stateSetup = true;
			delayTime = (GetTime() + 5);
			close = false;
			movieState = -2;
			counter = 0;
			AudioMessage("bzmix.wav", true);
			//AudioMessage("command1.wav", true);
			//AudioMessage("pilot1.wav", true);
			resetCam = false;
			break;
		}
		if (stateSetup) {
			AddHealth(shootHere, 1000);
			switch (movieState) {
			case -2: 
				Vector pos;
				pos = GetPosition(droptemp);
				pos.z += 2;
				SetVectorPosition(droptemp, pos);
				SetVectorPosition(shootHere, pos);
				pos = GetPosition(droptemp2);
				pos.z += 2;
				SetVectorPosition(droptemp2, pos);
				if (resetCam) {
					Vector pos, dir;
					GetCameraPosition(pos, dir);
					pos.z -= 1;
					SetCameraPosition(pos,dir);
				}
				else if (!resetCam) {
					CameraObject(droptemp, 20, -10, 120, droptemp);
					ResetCameraPosition();
					resetCam = true;
				}
				counter++;
				if (counter == 60) {
					movieState++;
					resetCam = false;
					counter = 0;
					r = 6.0f;
				}
				break;
			case -1: 
				{
				Vector pos;
				pos = GetPosition(droptemp);
				if (counter == 0)
					pos.z -= 80;
				SetVectorPosition(droptemp, pos);
				SetVectorPosition(shootHere, pos);
				pos = GetPosition(droptemp2);
				if (counter == 0)
					pos.z -= 80;
				SetVectorPosition(droptemp2, pos);
				if (counter < 35) {
					CameraObject(enemyTurrs[NUM_FVTURRS-2], float (12.0f* (sin(r))), 2.0f, float (12.0f* (cos(r))), enemyTurrs[NUM_FVTURRS-2]);
					r += .001f;
					if (!resetCam) {
						//AudioMessage("pilot2.wav", true);
						//AudioMessage("command3.wav", true);
						//AudioMessage("pilot3.wav", true);
						ResetCameraPosition();
						resetCam = true;
					}
					counter++;
				}
				else if (counter == 35) {
					movieState++;
					counter = 0;
					delayTime = (GetTime() + 5);
					SetAnimation(droptemp2, "bank", 1);
					resetCam = false;
					r = 0;

				}
				break;
				}
			case 0: 
				{
					Vector pos;
					pos = GetPosition(droptemp);
					pos.z += 3;
					SetVectorPosition(droptemp, pos);
					SetVectorPosition(shootHere, pos);
					pos = GetPosition(droptemp2);
					pos.z += 3;
					SetVectorPosition(droptemp2, pos);
					CameraObject(droptemp, float (80*(sin(r))), float (1.0f + (r*50.0f)), float (80*(cos(r))), droptemp);
					r += .01f;
					FireAt(enemyTurrs[NUM_FVTURRS-1], shootHere, true);
					FireAt(enemyTurrs[NUM_FVTURRS-2], shootHere, true);
					if (!resetCam) {
						//AudioMessage("pilot4.wav");
						//AudioMessage("command4.wav", true);
						//AudioMessage("pilot4.wav", true);
						ResetCameraPosition();
						resetCam = true;
					}
					if (delayTime < GetTime()) {
						movieState++;
						delayTime = (GetTime() + 3);
						resetCam = false;
					}
					break;
				}
			case 1: 
				{
				Vector pos;
				pos = GetPosition(droptemp);
				pos.z += 1;
				if (!resetCam) {
					RemoveObject(droptemp);
					droptemp = BuildObject("ivdrop_sh", 1, pos);
					shootHere = droptemp;
					Attack(enemySentries[0], droptemp);
				}
				SetVectorPosition(droptemp, pos);
				SetVectorPosition(shootHere, pos);
				FireAt(enemyTurrs[NUM_FVTURRS-1], shootHere, true);
				FireAt(enemyTurrs[NUM_FVTURRS-2], shootHere, true);
				CameraObject(enemySentries[0], -4, 1, -7, droptemp);
				if (!resetCam) {
					SetAnimation(droptemp, "shake", 1);
					ResetCameraPosition();
					resetCam = true;
				}
				if (delayTime < GetTime()) {
					movieState++;
					resetCam = false;
					delayTime = (GetTime() + 3);
					r = 1.0;
				}
				break;
				}
			case 2: 
				{
					Vector pos;
					pos = GetPosition(droptemp);
					pos.z += 1;
					SetVectorPosition(droptemp, pos);
					SetVectorPosition(shootHere, pos);
					FireAt(enemyTurrs[NUM_FVTURRS-1], shootHere, true);
					FireAt(enemyTurrs[NUM_FVTURRS-2], shootHere, true);
					CameraObject(droptemp, float(40*(sin(r))), 40.0f, float(-100*(cos(r))), droptemp);
					r += .001f;
					if (!resetCam) {
						ResetCameraPosition();
						resetCam = true;
					}
					if (delayTime < GetTime()) {
						crashsite = BuildObject("crashsite", 1, "crashSite");
						MaskEmitter(crashsite, 0);
						//ResetCameraPosition();
						resetCam = false;
						counter = 0;
						maxFrames = 0;
						movieState++;
					}
					break;
				}
			case 3:
				{
				Vector pos;
				pos = GetPosition(droptemp);
				pos.z += 1;
				SetVectorPosition(droptemp, pos);
				SetVectorPosition(shootHere, pos);
				FireAt(enemyTurrs[NUM_FVTURRS-1], shootHere, true);
				FireAt(enemyTurrs[NUM_FVTURRS-2], shootHere, true);
				if (maxFrames == 0) {
					maxFrames = SetAnimation(crashsite, "crash",1);
					//AudioMessage("command5.wav", true);
				}
				float curFrame = GetAnimationFrame(crashsite, "crash");

				if (curFrame < 8) {
					CameraObject(droptemp, float(40*(sin(r))), 40.0f, float(-100*(cos(r))), droptemp);
					r += .001f;
				}
				else {
					movieState++;
					Goto(enemySentries[0], "spawn2", 0);
					CameraObject(enemySentries[0], -5, 1, -10,droptemp);
					ResetCameraPosition();
					RemoveObject(droptemp);
					RemoveObject(shootHere);
				}
				if (curFrame == 1) {
					StartEmitter(crashsite, 7);
					StartEmitter(crashsite, 8);
				}
				else if (curFrame == 24) {
					StartEmitter(crashsite, 1);
					StartEmitter(crashsite, 2);
				}
				break;
				}
			case 4: 
				{
					float curFrame = GetAnimationFrame(crashsite, "crash");

					/* MANAGE THE SMOKE EMITTERS. */

					if (curFrame == 1) {
						StartEmitter(crashsite, 7);
						StartEmitter(crashsite, 8);
					}
				
					else if (curFrame == 24) {
						StartEmitter(crashsite, 1);
						StartEmitter(crashsite, 2);
					}

					else if (curFrame == 27) {
						StartEmitter(crashsite, 3);
						StartEmitter(crashsite, 4);
					} 

					else if (curFrame == 38) {
						StartEmitter(crashsite, 5);
						StartEmitter(crashsite, 6);
					}

					else if (curFrame == 42) {
						StopEmitter(crashsite, 5);
						StopEmitter(crashsite, 6);
					}

					if (curFrame < 10) {
						CameraObject(enemySentries[0], -5, 1, -10,droptemp);
					}
					else if (curFrame < (maxFrames - 2)) {
						Vector pos, dir;
						GetCameraPosition(pos, dir);
						pos.z += 1;
						SetCameraPosition(pos, dir);
						delayTime = (GetTime() + 2.0f);
					}
					else {
						Vector pos, dir;
						GetCameraPosition(pos, dir);
						SetCameraPosition(pos, dir);
						if (delayTime < GetTime()) 
							movieState++;
					}
					break;
				}
			case 5: 
				{
					Vector pos, dir;
					GetCameraPosition(pos, dir);
					pos.z += 1.0f;	
					SetCameraPosition(pos, dir);
					missionState++;
					delayTime = (GetTime() + 3);
					animal[0] = BuildObject("mcjak01", 0 , "spawn");
					Defend(animal[0], animal[0]);
					ClearIdleAnims(animal[0]);
					AddIdleAnim(animal[0], "eat1");
			//		SetAnimation(animal[0], "eat1", 0);
					RemoveObject(droptemp2);
					dropship = BuildObject("ivdrop2_ld", 1, "spawn2");
					resetCam = false;
				break;
				}
			}
		}
		break;
	case 5: { //Create the animal and setup mission.
		if (delayTime > GetTime()) {	
			CameraObject(animal[0], 1, 3, 10, animal[0]);
			if (!resetCam) {
				RemoveObject(crashsite);
				RemoveObject(enemySentries[0]);
				ResetCameraPosition();
				resetCam = true;
			}
		}
		else {
			counter = 0;
			missionState++;
		}
		break;
	}
	case 6: //Pull camera away from animal.
		if (counter == 7) {
			Goto(animal[0], "spawn");
			//AudioMessage("command6.wav", true);
		}
		
		if (counter < 50) {
			Vector pos, dir;
			GetCameraPosition(pos, dir);
			pos.z += 2.0f;
			pos.y += .4f;
			pos.x += .2f;
			dir.y += .01f;
			SetCameraPosition(pos, dir);
			counter++;
		}

		if (counter == 30) {
			maxFrames = SetAnimation(dropship, "land",1);
			SetPosition(dropship, "spawn");
			landing = true;
			stateSetup = true;
		}

		if (counter == 50) {
			missionState++;
		}

		break;
	case 7: //Land the dropship.
		if ((landing) && (stateSetup))  {		
			Vector pos, dir;
			GetCameraPosition(pos, dir);
			SetCameraPosition(pos, dir);
			counter++;
			float curFrame = GetAnimationFrame(dropship, "land");
			if (counter == 70) {
				smoke = BuildObject("kickup", 0, "spawn");
			}

			if (curFrame == (maxFrames - 5)) {
				RemoveObject(smoke);
			}

			if (curFrame == (maxFrames-1)) {
				RemoveObject(dropship);
				dropship = BuildObject("ivdrop2_op", 1, "spawn");
				maxFrames = SetAnimation(dropship, "deploy",1);
				stateSetup = true;
				landing = false;
			}
		}
		else if ((!landing) && (stateSetup)) {
			if (camObj) {
				CameraObject(dropship, 50, 15, 40, dropship);
				camObj = false;
			}

			CameraObject(dropship, 50, 15, 40, dropship);
			float curFrame = GetAnimationFrame(dropship, "deploy");
			if (curFrame == (maxFrames-1)) {
				stateSetup = false;
				RemoveObject(animal[0]);
				missionState++;
			}
		}
		break;
	case 8: //Spawn service truck.
		CameraObject(dropship, 50, 15, 40, dropship);
		if (!stateSetup) {
			service = BuildObject("ivserv", 1, "spawn");
			//Goto(service, "spawn");
			Goto(service, "service");
			delayTime = (GetTime() + 5.0f);
			stateSetup = true;
			Vector pos;
			pos = GetPosition(service);
			pos.y += 5;
			SetVectorPosition(service, pos);

		}
		else if ((stateSetup) && (delayTime < GetTime())) { 
			stateSetup = false;
			missionState++;
			//AudioMessage("command7.wav", true);
		}
		break;
	case 9: //Unload dropship cargo, assign it to groups and give targets if player does NOT take commands.
		if (shipCount < NUM_FRIENDS) {
			if (GetTime() > delayTime) {
				int &h = friendHandle[shipCount];
				h = BuildObject("ivmbike",1,"spawn");
				Vector pos;
				pos = GetPosition(h);
				pos.y += 5;
				SetVectorPosition(h, pos);
				Goto(h, "spawn");
				SetIndependence(h, 0);
				delayTime = (GetTime() + 2.0f);
				shipCount++;
				if ((shipCount == 2) || (shipCount == 4))
					resetCam = false;
			}
			if (shipCount == 1) { // Pathing hack to try and get service truck out of the way.
				//Goto(service, "service");
			}

			if ((shipCount != 2) && (shipCount != 3)) {
				CameraObject(dropship, 50, 15, 40, dropship);
				if (!resetCam) {
					ResetCameraPosition();
					resetCam = true;
				}
			}

			if ((shipCount == 2) || (shipCount == 3))  {
				CameraObject(friendHandle[0], 1, 2, 9, friendHandle[0]);
				if (!resetCam) {
					ResetCameraPosition();
					resetCam = true;
				}
			}
			Vector pos;
			pos = GetPosition(friendHandle[shipCount]);
			pos.y += 5;
			SetVectorPosition(friendHandle[shipCount], pos);
		}

		if (shipCount == NUM_FRIENDS) {
			if (!missionCommand) { //pathing hack
			}
			else if (missionCommand) {
			}
		}
		
		if ((shipCount == NUM_FRIENDS) && (GetTime() > delayTime)){
			shipCount = 0;
			SetPosition(player, "spawn");
			moveVehicle = true;
			counter = 0;
			missionState++;
			if (!missionCommand) { //pathing hack
				Attack(friendHandle[0], enemyTurrs[0]);
				SetIndependence(friendHandle[0], 1);
				SetVerbose(friendHandle[0], true);
				Attack(friendHandle[1], enemyTurrs[0]);
				SetIndependence(friendHandle[1], 1);
				SetVerbose(friendHandle[1], true);
				Attack(friendHandle[2], enemyTurrs[1]);
				SetIndependence(friendHandle[2], 1);
				SetVerbose(friendHandle[2], true);
				Attack(friendHandle[3], enemyTurrs[1]);
				SetIndependence(friendHandle[3], 1);
				SetVerbose(friendHandle[3], true);
			}
			
		}
		break;
	case 10: //Main body of the mission.  Clear path to the crash site.
		/*The Following Section Commands Your Wingmen. */
	// determining if the player is out of his ship

		//Give the player health and ammo.
		if ((GetHealth(player) < 0.7f) || (GetAmmo(player) < 0.5f)) {
			if ((!IsAlive(apc)) && (!IsAlive(p1)) && (!IsAlive(p2))) {
				apc = BuildObject("ivapc", 1, "apc1");
				Goto(apc, "apc1");
				//ClearObjectives();
				//AddObjective("demo01f.otf", WHITE, 5);
				SetObjectiveOn(apc);
				SetObjectiveName(apc, "Supply Ship");
				powerUpSent = true;
			}
			
			if (powerUpSent) {
				if (GetDistance(player, apc) < 150) {
					Vector pos;
					pos = GetPosition(apc);
					p1 = BuildObject("apammo", 1, pos);
					pos.x += 4;
					p2 = BuildObject("aprepa", 1, pos);
					SetObjectiveOff(apc);
					SetObjectiveOn(p2);
					SetObjectiveName(p2, "Supplies");
					pos.x -= 4;
					powerUpSent = false;
					height = 0;
				}
			}
		}

		if (((IsAlive(apc)) && (!powerUpSent)) || (GetDistance(apc, "spawn") < 175)) { //Remove the apc.
			if ((GetDistance(apc, player) > 175) || (GetDistance(apc, "spawn") < 175)) {
				RemoveObject(apc);
			}
		}


		if ((player!=oldplayer) && (!missionCommand))
		{
			oldplayer=player;
			Follow(tug, player);
		}


		if (GetHealth(service) < 0.7f) { //Keep the service truck alive.
			AddHealth(service, 10);
		}

		if (moveVehicle) {
			moveVehicle = PlaybackVehicle("exit");
			if ((142 - counter) > 80){
				Vector target;
				target = GetPosition(player);
				GetCameraPosition(pos, dir);
				dir.x = (target.x - pos.x);
				dir.y = (target.y - pos.y);
				dir.z = (target.z - pos.z);
				SetCameraPosition(pos, dir);
			}
			else if ((142 - counter) > 24) { //Set to zoom in on the ship for the player to take conrol.
				CameraObject(player, 0, 10, -24, player);
			}
			else if (((142 - counter) <= 24) && ((142 - counter) > 8)) {
				int dist = (142 - counter);
				CameraObject(player, 0, float(.5*dist), float(-1*dist), player);
			}
			else if ((142 - counter) == 8) {
				tug = BuildObject("ivstas", 1, "spawn");
				if (!missionCommand) {
					Follow(tug, player);
					SetGroup(tug, -1);
					SetGroup(service, -1);
				}
				else if (missionCommand) {
					SetGroup(tug, 3);
					Follow(tug, player, 0);
					SetIndependence(tug, 1);

					SetGroup(service, 2);
					Follow(service, player, 0);
					SetIndependence(service, 1);
					
					SetGroup(friendHandle[0], 0);
					Follow(friendHandle[0], player, 0);
					SetIndependence(friendHandle[0], 1);
					//
					SetGroup(friendHandle[1], 0);
					Follow(friendHandle[1], player, 0);
					SetIndependence(friendHandle[1], 1);
					//
					SetGroup(friendHandle[2], 1);
					Follow(friendHandle[2], player, 0);
					SetIndependence(friendHandle[2], 1);
					//
					SetGroup(friendHandle[3], 1);
					Follow(friendHandle[3], player, 0);
					SetIndependence(friendHandle[3], 1);
				}
				crashsite = BuildObject("crashdrop", 1, "crashSite");
				//MaskEmitter(crashsite, 0);
				SetObjectiveOn(crashsite);
				SetObjectiveName(crashsite, "Crash Site");
				SetObjectiveOn(service);
				SetObjectiveName(service, "Service Truck");
				sentTimer = 20;
				tankTimer = 0;
				CameraFinish();
				if (!missionCommand) {
					ClearObjectives();
					AddObjective("demo01a.otf", WHITE, 10);
				}
				else if (missionCommand) {
					ClearObjectives();
					AddObjective("demo01a-c.otf", WHITE, 10);
				}
			}
			counter++; 	
		}
		else {
			if (!missionCommand) { //Command the mortar bikes.
				if (turrDead) {
					if (!firstTry) {
						CommandMortarBikes();
					}
				}
				if ((firstTry) && (turrDead)) {
					firstTry = false;
				}
			}

			if (sentTimer>0) //Dispatch enemy sentries.
			{
				sentTimer--;
			} else
			{
				if (sentDead) {
					for (int i = 0; i < NUM_SENTRIES; i++) {
						enemySentries[i] = BuildObject("fvsent",2,"spawn2");
						Attack(enemySentries[i],player,0);
						SetDifficulty(enemySentries[i], difficulty);
					}
					sentDead = false;
				}
			}

			if (tankTimer>0) //Dispatch enemy tanks.
			{
				tankTimer--;
			}
			else
			{
				if (tankDead) {
					for (int i = 0; i < NUM_FVTANKS; i++) {
						enemyTanks[i] = BuildObject("fvtank",2,"spawn2");
						Attack(enemyTanks[i],service,0);
						SetDifficulty(enemyTanks[i], difficulty);
					}
					tankDead = false;
				}
			}
			if (missionCommand) {
				if (deadTurrs == 9) {
					missionState++;
				}
			}
		


			if ((IsAlive(service)) && (!missionCommand))
				DoService(service); //Make sure the service truck has an order.
		}
		break; 
	case 11: //Send the player up to the crash site.
		
		if (!stateSetup) {
			ClearObjectives();
			AddObjective("demo01b.otf", WHITE, 10);
			animal[3] = BuildObject("jak_kill", 0, "animal3");
			r = .2f;
			stateSetup = true;
			remindTime = (GetTime() + 40.0f);
		}

		if (remindTime < GetTime()) { //Remind and then fail player if they do not get to crash site.
			if (!firstStrike) {
				ClearObjectives();
				AddObjective("demo01b.otf", WHITE, 10);
				firstStrike = true;
				remindTime = (GetTime() + 60.0f);
			}
			else {
				FailMission(5, "demo01_L4");
			}
		}

		else if (stateSetup) {
			if (GetDistance(player, crashsite) < 200.0) {
				if (IsAlive(enemySentries[0])) {
					RemoveObject(enemySentries[0]);
				}
				stateSetup = false;
				person1 = BuildObject("ispilo", 1, "animal0");
				Goto(person1, "crashSite");
				maxFrames = SetAnimation(animal[3], "eat1", 0); //Animal eats soldier
				AudioMessage("bz2dem01b.wav", true);
				CameraReady();
				CameraObject(animal[3], float(3*(cos(r))), .5f, float(3*(sin(r))), animal[3]);
				r += .015f;
				missionState++;
			}
		}
		
		if (player!=oldplayer)
		{
			oldplayer=player;
			if (IsOdf(player, "isuser")) {
				Attack(animal[1],player);
				Attack(animal[2], player);
				Attack(animal[3], player);
			}
			
		}


		if ((IsAlive(service)) && (!missionCommand))
			DoService(service); //Make sure the service truck has an order.
			break;
	case 12:{
		if (!stateSetup) {
			float curFrame = GetAnimationFrame(animal[3], "eat1");
			if (curFrame < (maxFrames - 1)) {
				CameraObject(animal[3], float(3*(sin(r))), .5f, float(3*(cos(r))), animal[3]);
				r += .015f;
			}
			else {
				CameraFinish();
				if (!missionCommand) {
					Stop(tug);
					LookAt(tug, crashsite);
				}
				else if (missionCommand) {
					Stop(tug, 0);
					LookAt(tug, crashsite, 0);
				}
				RemoveObject(person1);
				AudioMessage("command9.wav", true);
				ClearObjectives();
				AddObjective("demo01e.otf", WHITE, 10); //Clear the animals.
				stateSetup = true;
				RemoveObject(animal[3]);
				animal[3] = BuildObject("mcjak01", 2, "animal4");
				Attack(animal[3], player);
				animal[1] = BuildObject("mcjak01", 2, "animal1");
				Attack(animal[1], player);
				animal[2] = BuildObject("mcjak01", 2, "animal2");
				Attack(animal[2], player);
				moviePlaying = false;
				remindTime = (GetTime() + 40.0f);
				firstStrike = false;
			}
		}
		else {
			if (!moviePlaying) {
				if (remindTime < GetTime()) { //Remind and then fail player if they do not get to crash site.
					if (!firstStrike) {
						ClearObjectives();
						AddObjective("demo01e.otf", WHITE, 10);
						firstStrike = true;
						remindTime = (GetTime() + 60.0f);
					}
					else {
						FailMission(5, "demo01_L5");
					}
				}
				peopleRescued = true;
				moviePlaying = true;
				for (int i = 0; i < NUM_ANIMALS; i++) {
					if (IsAlive(animal[i])) {
						i = NUM_ANIMALS + 1;
						peopleRescued = false;
						moviePlaying = false;
						camObj = false;
					}
				}
				if (peopleRescued) {
					AudioMessage("apc2.wav", true);
					SetPosition(tug, "animal3");
					Goto(tug, "animal3");
					delayTime2 = (GetTime() + 2);
					break;
				}

			}
			if ((peopleRescued) && (GetTime() > delayTime2)) {
				if (moviePlaying) {
					if (!camObj) {
						Vector pos;
						SetPosition(player, "animal2");
						SetPosition(service, "spawn2");
						pos = GetPosition(crashsite);
						person1 = BuildObject("ispilo", 1, pos);
						Goto(person1, tug);
						pos.x += 5;
						person2 = BuildObject("ispilo", 1, pos);
						Goto(person2, tug);
						pos.z += 5;
						person3 = BuildObject("ispilo", 1, pos);
						Goto(person3, tug);
						camObj = true;
						r = 1.0f;
						counter = 0;
					}
					else {
						if (counter == 2) {
							RemoveObject(crashsite);
							crashsite = BuildObject("crashdrop2", 1, "crashSite");
							//MaskEmitter(crashsite, 0);
							CameraReady();
							CameraObject(tug,  float(10*(sin(r))), 4.0f, float(10*(cos(r))), tug);
							ResetCameraPosition();
							Stop(tug);
							r += .01f; 
						}
						else if (counter == 90) {
				
						}
						
						if (counter < 3) {
							counter++;
							delayTime = (GetTime() + 5);
						}
						else if (counter < 50) {
							CameraObject(tug,  float(10*(sin(r))), 4.0f, float(10*(cos(r))), tug);
							r += .01f; 
							counter++;
							delayTime = (GetTime() + 5);
						}

						else if (counter < 90) {
							CameraObject(person1,  4.0f, 2.0f, 4.0f, person1);
							r += .01f; 
							counter++;
							delayTime = (GetTime() + 5);
						}
						else {
							CameraObject(tug,  float(10*(sin(r))), 4.0f, float(10*(cos(r))), tug);
							r += .01f; 
							counter++;
						}

						if (((GetDistance(person1, tug) < 20)) || (GetTime() > delayTime)) {
							RemoveObject(person1);
							RemoveObject(person2);
							RemoveObject(person3);
							CameraFinish();
							RemoveObject(crashsite);
							crashsite = BuildObject("crashdrop", 1, "crashSite");
							//MaskEmitter(crashsite, 0);
							stateSetup = false;
							moviePlaying = false;
							AudioMessage("apc3.wav", true);
							AudioMessage("command10.wav", true);
							ClearObjectives();
							AddObjective("demo01d.otf", WHITE, 10); //Scuttle dropship
							missionState++;
							SetAvoidType(tug, AVD_PLAN);
							if (!missionCommand) {
								Goto(tug, "spawn2");
							}
							else if (missionCommand) {
								Goto(tug, "spawn2", 0);
							}
							animal[1] = BuildObject("mcjak01", 0, "animal1");
							Attack(animal[1], player);
							//ResurectPilot(tug);
						}
					}
				}
			}
		}
		if (player!=oldplayer)
		{
			oldplayer=player;
			if (IsOdf(player, "isuser")) {
				Attack(animal[1],player);
				Attack(animal[2], player);
				Attack(animal[3], player);
			}
			
		}

		break;
			}
	case 13: //Add scuttle ship code here.
		if (player!=oldplayer)
		{
			oldplayer=player;
			Attack(animal[1],player);
		}

		saveCrash = crashsite;
		if (!IsAlive(crashsite)) {
			dropship = BuildObject("ivdrop2", 1, "dustoff");
			AudioMessage("command8.wav", true);
			ClearObjectives();
			AddObjective("demo01c.otf", WHITE, 10);
			SetObjectiveOff(crashsite);
			SetObjectiveOff(service);
			SetObjectiveOn(dropship);
			SetObjectiveName(dropship, "Dust Off");
			SetObjectiveOn(tug);
			SetObjectiveName(tug, "Transport");
			stateSetup = false;
			delayTime = (GetTime() + 7);
			missionState++;
			if (missionCommand) {
				Follow(tug, player);
			}
			else if (missionCommand) {
				Follow(tug, player, 0);
			}
		}
		break;
	case 14: //Get to the dustoff site.
		//Give the player health and ammo.
		if ((GetHealth(player) < 0.7f) || (GetAmmo(player) < 0.5f)) {
			if ((!IsAlive(apc)) && (!IsAlive(p1)) && (!IsAlive(p2))) {
				apc = BuildObject("ivapc", 1, "apc2");
				Goto(apc, "apc2");
				//ClearObjectives();
				//AddObjective("demo01f.otf", WHITE, 5);
				SetObjectiveOn(apc);
				SetObjectiveName(apc, "Supply Ship");
				powerUpSent = true;
			}
			
			if (powerUpSent) {
				if (GetDistance(player, apc) < 150) {
					Vector pos;
					pos = GetPosition(apc);
					p1 = BuildObject("apammo", 1, pos);
					pos.x += 2;
					p2 = BuildObject("aprepa", 1, pos);
					powerUpSent = false;
					SetObjectiveOff(apc);
					SetObjectiveOn(p2);
					SetObjectiveName(p2, "Supplies");
					height = 0;
				}
			}

		}

		if (((IsAlive(apc)) && (!powerUpSent)) || (GetDistance(apc, "apcend") < 175)) { //Remove the apc.
			if ((GetDistance(apc, player) > 175) || (GetDistance(apc, "apcend") < 175)) {
				RemoveObject(apc);
			}
		}

		if ((!IsAlive(enemySentries[0])) && (!IsAlive(enemyTanks[0])) 
		&& (IsAlive(guntow2)) && (!gunWarning)) {
			AudioMessage("command11.wav", true);
			gunWarning = true;
		}

		if (!stateSetup) {
			enemySentries[0] = BuildObject("fvsent", 2, "spawn3");
			Attack(enemySentries[0], tug);
			SetDifficulty(enemySentries[0], difficulty);
			enemyTanks[0] = BuildObject("fvarch", 2, "spawn4");
			Attack(enemyTanks[0], player);
			SetDifficulty(enemyTanks[0], difficulty);
			oldplayer=player;
			for (int i=9; i < 15; i++) {
				char name[20];
				sprintf(name, "turr%d", i);
				enemyTurrs[i - 9] = GetHandle(name);
				SetDifficulty(enemyTurrs[i - 9], difficulty);
			}
			turrDead = true;
			firstTry = false;
			stateSetup = true;
			if (!missionCommand) {
				Follow(tug, player);
			}
			else if (missionCommand) {
				Follow(tug, player, 0);
			}
		}
		else if (stateSetup) {
			if (!missionCommand) {
				if (turrDead) {
					if (!firstTry) {
						CommandMortarBikes();
					}
				}
				if ((firstTry) && (turrDead)) {
					firstTry = false;
				}
			}
		}
		if (player!=oldplayer)
		{
			oldplayer=player;
			Attack(enemyTanks[0],player);
			if (!missionCommand) {
				Follow(tug, player);
			}
		}
			/*if ((GetDistance(player, guntow1)) < 220) {
			SetAnimation(guntow1, "deploy", 1);
		}

		if ((GetDistance(player, guntow2)) < 120) {
			SetAnimation(guntow2, "deploy", 1);
		}*/

		if ((GetDistance(player, dropship)) < 100) {
			missionState++;
			CameraReady();
			delayTime = (GetTime() + 4);
			r = 0;
		}
		if ((IsAlive(service)) && (!missionCommand))
				DoService(service); //Make sure the service truck has an order.
			break;
	case 15:
		if (delayTime > GetTime()) {
			CameraObject(dropship, float(50*(sin(r))), float(1 + (r*50)), float(50*(cos(r))), dropship);
			r += .01f;
			if (!resetCam) {
				ResetCameraPosition();
				resetCam = true;
			}
		}
		else {
			SucceedMission(5.0f+GetTime(),"demo01-W1.otf");
			missionState++;
			AudioMessage("command12.wav", true);
		}

		break;
	case 16: 
		{
		Vector pos, dir;
		GetCameraPosition(pos, dir);
		SetCameraPosition(pos, dir);
		break;
		}
	}
	if ((skipCin) && (missionState <= 10)) {
		SkipCin();
		skipCin = false;
	}

	IFace_SetInteger("script.mission.state", missionState);
}

void demo01Mission::CommandMortarBikes() {
	for (int i = 0; i < NUM_FRIENDS; i++) { //Assemble a list of active targets
		if (IsAlive(friendHandle[i])) {
			AiCommand what = GetCurrentCommand(friendHandle[i]);
			if (what == CMD_ATTACK) {
				activeTargets[i] = GetCurrentWho(friendHandle[i]);
			}
			else if (what == CMD_NONE) {
				activeTargets[i] = NULL;
				needTarget = true;
			}
		}
		else {
			activeTargets[i] = NULL;
		}
	}

	Handle target;

	if (((GetDistance(player, guntow2)) < 200) && (missionState == 14)) {
		target = guntow2;
	}
	/*else if (((GetDistance(player, guntow1)) < 200) && (missionState == 14)) {
		target = guntow1;
	}*/
	else {
		target = FindTurrTarget(); //Identify the next target
	}
	
	if (needTarget) {
		if (target != NULL) { //Tell units to attack the new target
			for (int i = 0; i < NUM_FRIENDS; i++) {
				AiCommand what = GetCurrentCommand(friendHandle[i]);
				if ((what == CMD_NONE) && (i != currentObjective)) {
					Attack(friendHandle[i], target);
					turrDead = false;
					firstTry = true;
					needTarget = false;
				}
			}
		}

	}
	else if (!needTarget) { //Handle case where turret dies but no new target is needed.
		firstTry = true;
		turrDead = false;
	}
	if (target == NULL) {
		if (missionState <= 12) {
			if (IsAlive(apc)) {
				Damage(apc, 10001);
			}			
			stateSetup = false;
			missionState++;
		} 
	}
}

void demo01Mission::DoService(Handle truck) {
	for (int i = 0; i < NUM_FRIENDS; i++) {
		AiCommand what = GetCurrentCommand(friendHandle[i]);
		if (what == CMD_ATTACK) {
			if ((GetHealth(friendHandle[i]) < 0.5f) || (GetAmmo(friendHandle[i]) < 0.5f)) {
				Follow(friendHandle[i],player);
			}
		}	
	}	
	if (!serviceBusy) {
		for (int i = 0; i < NUM_FRIENDS; i++) {
			if ((IsAlive(friendHandle[i])) && (((GetHealth(friendHandle[i]) < 0.5f) || (GetAmmo(friendHandle[i]) < 0.5f)))) {
				Service(truck, friendHandle[i]);
				Follow(friendHandle[i],truck);
				//SetObjectiveOn(friendHandle[i]); //Debug
				currentObjective = i; //Use currentObjective to know what to turn off later.
				serviceBusy = true;
				followPlayer = false;
				i = NUM_FRIENDS + 1;
				close = false;
			}
		}
	}



	else if (serviceBusy) {
		AiCommand what = GetCurrentCommand(truck);
		if (what == CMD_NONE) { //Service is done
			serviceBusy = false;
			//SetObjectiveOff(friendHandle[currentObjective]); //DEBUG
			Stop(friendHandle[currentObjective]);
			currentObjective = -1;
			close = false;
			followPlayer = true; //Tell service truck to follow player.
			turrDead = true; //Trigger new target for mortar bike
		}
		/*if ((GetDistance(truck, friendHandle[currentObjective]) < 50.0) && (!close)) {
			Stop(friendHandle[currentObjective]);
			close = true;
		}*/
	}

	if (followPlayer) {
		Follow(truck,player);
		followPlayer = false;
	}
}

void demo01Mission::CheckLoseConditions() {
	if (missionState <= 12) {
	/*	bool friendAlive = false;
		for (int i = 0; i < NUM_FRIENDS; i++) {
			if (IsAlive(friendHandle[i])) {
				friendAlive = true;
			}
		}
		if (!friendAlive) {
			AddObjective("demo01-L1.otf", WHITE, 5);
			FailMission(10.0+GetTime(),"demo01-L1.otf");
		} */

		if (!IsAlive(service)) {
			if (!lost) {
				ClearObjectives();
				AddObjective("demo01-L2.otf", WHITE, 5);
				FailMission(10.0f+GetTime(),"demo01-L2.otf");
				lost = true;
			}
		}
	}
	if (!IsAlive(tug)) {
		if (!lost) {
			ClearObjectives();
			AddObjective("demo01-L3.otf", WHITE, 5);
			FailMission(10.0f+GetTime(), "demo01-L3.otf");
			lost = true;
		}
	}
}

Handle demo01Mission::FindTurrTarget() {
	for (int i = 0; i < NUM_FVTURRS; i++) { //Try to pick a turret target that is not being attacked.
		if ((IsAlive(enemyTurrs[i])) && (enemyTurrs[i] != NULL)) { //Check to see target is alive
			bool goodTarget = true;
			for (int t = 0; t < NUM_FRIENDS; t++) { //Compare target to currently engaged targets
				if ((enemyTurrs[i] == activeTargets[t])) { // && (activeTargets[t] != NULL)){
					goodTarget = false;
				}
			}
			if (goodTarget) {
				return enemyTurrs[i];
			}
		}
	}
	for (i = 0; i < NUM_FVTURRS; i++) { //Check to see if there is any turret to attack.
		if (IsAlive(enemyTurrs[i])) {
			return enemyTurrs[i];
		}
	}
	return NULL;
}

Handle demo01Mission::FindSentTarget() {
	for (int i = 0; i < NUM_FRIENDS; i++) { //loop through all of the turret targets
		bool goodTarget = false;
		if ((IsAlive(friendHandle[i])) && (friendHandle[i] != NULL)) { //Check to see target is alive
			return friendHandle[i];
		}
	}
	return NULL;
}


void demo01Mission::SkipCin() {
	if (IsAlive(enemySentries[0])) {
		RemoveObject(enemySentries[0]);
	}

	if (IsAlive(droptemp)) {
		RemoveObject(droptemp);
	}
	if (IsAlive(crashsite)) {
		RemoveObject(crashsite);
	}

	if (!IsAlive(friendHandle[0])) {
		friendHandle[0] = BuildObject("ivmbike", 1, "spawn");
	}

	Vector pos;
	pos = GetPosition(friendHandle[0]);

	if (!IsAlive(friendHandle[1])) {
		pos.z += 40;
		friendHandle[1] = BuildObject("ivmbike", 1, pos);
	}

	if (!IsAlive(friendHandle[2])) {
		pos.z += 40;
		friendHandle[2] = BuildObject("ivmbike", 1, pos);
	}

	if (!IsAlive(friendHandle[3])) {
		pos.z += 40;
		friendHandle[3] = BuildObject("ivmbike", 1, pos);
	}

	if (!missionCommand) { //Make sure all of the units have an initial order.
		Attack(friendHandle[0], enemyTurrs[0]);
		Attack(friendHandle[1], enemyTurrs[0]);
		Attack(friendHandle[2], enemyTurrs[1]);
		Attack(friendHandle[3], enemyTurrs[1]);

	}

	else if (missionCommand) {
		SetGroup(friendHandle[0], 0);
		Attack(friendHandle[0], enemyTurrs[0]);
		SetGroup(friendHandle[1], 0);
		Attack(friendHandle[1], enemyTurrs[0]);
		SetGroup(friendHandle[2], 1);
		Attack(friendHandle[2], enemyTurrs[1]);
		SetGroup(friendHandle[3], 1);
		Attack(friendHandle[3], enemyTurrs[1]);
	}

	if (!IsAlive(service)) {
		service = BuildObject("ivserv", 1, "service");
	}

	if (!missionCommand) {
		SetGroup(service, -1);
		Follow(service, player);
	}

	else if (missionCommand) {
		SetGroup(service, 2);
		Follow(service, player);
	}

	pos = GetPosition(service);
	if (!IsAlive(tug)) {
		pos.z -= 40;
		tug = BuildObject("ivstas", 1, pos);
	}

	if (!missionCommand) {
		Follow(tug, player);
		SetGroup(tug, -1);
	}
	else if (missionCommand) {
		Follow(tug, player);
		SetGroup(tug, 3);
	}

	crashsite = BuildObject("crashdrop", 1, "crashSite");
	SetObjectiveOn(crashsite);
	SetObjectiveName(crashsite, "Crash Site");
	SetObjectiveOn(service);
	SetObjectiveName(service, "Service Truck");
	sentTimer = 20;
	tankTimer = 0;
	moveVehicle = false;
	missionState = 10;
	CameraFinish();

	if (!missionCommand) { //SetO bjectives
		ClearObjectives();
		AddObjective("demo01a.otf", WHITE, 10);
	}
	else if (missionCommand) {
		ClearObjectives();
		AddObjective("demo01a-c.otf", WHITE, 10);
	}
}

void demo01Mission::SetDifficulty(Handle object, int level) {
	switch (level) {
	case 0:
		SetSkill(object, 0);
		break;
	case 1:
		SetSkill(object, 1);
		break;
	case 2:
		SetSkill(object, 3);
		break;
	}
}