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

/*
	profile01Mission

	Just add water!!
*/

#define MAX_SENTRIES 2
#define MAX_FVTANKS 3
#define NUM_ENEMIES (MAX_SENTRIES + MAX_FVTANKS) //Sum of alien vehicles should not exceed this number
#define MAX_IVTANKS 2
#define MAX_IVRCKTS 2
#define NUM_FRIENDS (MAX_IVTANKS + MAX_IVRCKTS) //Sum of friend vehicles should not exceed this number
#define NUM_SPAWNS 10


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

	void Update(void);

	void AddObject(Handle h);

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

	// bools
	bool
		b_first, moviePlaying,
		b_last;

	// floats
	float
		f_first,
		f_last;

	// handles
	Handle
		h_first, player, enemyHandle[NUM_ENEMIES], spawnHandle[NUM_SPAWNS],
		friendHandle[NUM_FRIENDS],	
		h_last;

	// integers
	int
		i_first, missionState,animod,tankmod,
		i_last;
};

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

void profile01Mission::Setup(void)
{
	/*
		Here's where you set the values at the start.  
	*/
	missionState = 0;
	animod=0;
	tankmod=0;
//	moviePlaying = true;
}

void profile01Mission::AddObject(Handle h) {

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


}

void profile01Mission::Execute(void) {
//	missionState = 2;

/* The above is needed for editing cineractives. */

	switch (missionState) {
	case 0:
		player = GetPlayerHandle();
		int i;
		for (i = 0; i < NUM_SPAWNS; i++) { //Create the array of handles to spawn points
			char varName1[20];
			sprintf(varName1, "spawn%d", i + 1);
			spawnHandle[i] = GetHandle(varName1);
		}
		//spawn in a bunch of enemies
		for (i = 0; i < MAX_SENTRIES; i++)
		{
			enemyHandle[i] = BuildObject("fvtank", 2, spawnHandle[i]);
			Attack(enemyHandle[i], player);
		}
		for (i = MAX_SENTRIES; i < (MAX_SENTRIES + MAX_FVTANKS); i++)
		{
			enemyHandle[i] = BuildObject("fvsent", 2, spawnHandle[i]);
			Attack(enemyHandle[i], player);
		}
		 //Spawn in a bunch of friends
		for (i = 0; i < MAX_IVTANKS; i++) 
		{
			Vector buildOffset = GetPosition(spawnHandle[i]);
			buildOffset.x = (buildOffset.x + 10);
			friendHandle[i] = BuildObject("ivtank", 1, buildOffset);
			Defend(friendHandle[i], player);
		}
		for (i = MAX_IVTANKS; i < (MAX_IVTANKS + MAX_IVRCKTS); i++); 
		{
			Vector buildOffset = GetPosition(spawnHandle[i]);
			buildOffset.x = (buildOffset.x + 10);
			friendHandle[i] = BuildObject("ivrckt", 1, buildOffset);
			Defend(friendHandle[i], player);
		}
		missionState++;
		break;
	case 1:
		//Replace dead enemies.
		for (i = 0; i < MAX_SENTRIES; i++)
		{
			if (!IsAlive(enemyHandle[i])) {
				enemyHandle[i] = BuildObject("fvtank", 2, spawnHandle[i]);
				Attack(enemyHandle[i], player);
			}
		}
		for (i = MAX_SENTRIES; i < (MAX_SENTRIES + MAX_FVTANKS); i++)
		{
			if (!IsAlive(enemyHandle[i])) {
				enemyHandle[i] = BuildObject("fvsent", 2, spawnHandle[i]);
				Attack(enemyHandle[i], player);
				animod++;
				if (animod%3==0)
				{
					Vector buildOffset=GetPosition(spawnHandle[i]);
					buildOffset.z=buildOffset.z+15;
					BuildObject("mcjak01",0,buildOffset);
				}
			}
		}
		 //Replace dead friends
		for (i = 0; i < MAX_IVTANKS; i++) {
			if (!IsAlive(friendHandle[i])) {
				Vector buildOffset = GetPosition(spawnHandle[i]);
				buildOffset.x = (buildOffset.x + 10);
				friendHandle[i] = BuildObject("ivtank", 1, buildOffset);
				Defend(friendHandle[i], player);
			}
		}
		for (i = MAX_IVTANKS; i < (MAX_IVTANKS + MAX_IVRCKTS); i++) {
			if (!IsAlive(friendHandle[i])) {
				Vector buildOffset = GetPosition(spawnHandle[i]);
				buildOffset.x = (buildOffset.x + 10);
				tankmod++;
				if (tankmod%3==0) {
					friendHandle[i] = BuildObject("ivmbike", 1, buildOffset);
				} else {
					friendHandle[i] = BuildObject("ivrckt", 1, buildOffset);
				}
				Defend(friendHandle[i], player);
			}
		}

		break;
	case 2:
/*		if (moviePlaying) {
			moviePlaying = PlayMovie("temp");
		}
*/		break;
	}

	
	//Give the player health and ammo.
	if (GetHealth(player) < 0.7f) {
		AddHealth(player, 1000);
	}
	if (GetAmmo(player) < 0.5f) {
		AddAmmo(player, 500);
	}
}
