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

#define ENEMY_FORCE_A 3
#define ENEMY_FORCE_B 3
#define MAX_SPAWNS 3
#define ATTACK_SPAWNS 3
#define HARASS_FORCE 1
#define HARASS_INTERVAL 100.0f

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, pGen, gTow,
		b_last;

	// floats
	float
		f_first, harassTime,
		f_last;

	// handles
	Handle
		h_first, forceA[ENEMY_FORCE_A], forceB[ENEMY_FORCE_B], attackSpawn[ATTACK_SPAWNS],
		player, spawn[MAX_SPAWNS], harass[HARASS_FORCE], recycler, constructor, bridge,
		pGenerator,
		h_last;

	// integers
	int
		i_first, missionState, 

		i_last;
};

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

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

//  bools
	pGen = false;
	gTow = false;

//  floats


//  handles
	player = GetPlayerHandle();
	spawn[0] = GetHandle("spawn1");
	spawn[1] = GetHandle("spawn2");
	spawn[2] = GetHandle("spawn3");
	recycler = GetHandle("recycler");
	attackSpawn[0] = GetHandle("spawn5");
	attackSpawn[1] = GetHandle("spawn6");
	attackSpawn[2] = GetHandle("spawn7");
	bridge = GetHandle("bridge");
	constructor = GetHandle("construction");
//  integers
	missionState = 0;
	harassTime = 0.0;
	pGenerator = NULL;



	IFace_CreateInteger("script.mission.state", 0);
}

void Misn04Mission::AddObject(Handle h)
{
	if (IsOdf(h,"ibpgen")) {
		pGen = true;
		pGenerator = h;
	}
	else if (IsOdf(h,"ibgtow")) {
		gTow = true;
	}
}


void Misn04Mission::Execute(void)
{
/*
	Here is where you put what happens every frame.  
*/
	missionState = IFace_GetInteger("script.mission.state");

	switch (missionState) {
	int i;
	case 0: // create enemies and have them attack the player.
		for (i = 0; i < ENEMY_FORCE_A; i++) {
			forceA[i] = BuildObject("fvsent", 1, spawn[i]);
			Attack(forceA[i], player, 1);
		}
		ClearObjectives();
		AddObjective("stratDem1.otf",WHITE,20.0f); //clear area and build a base
		IFace_SetInteger("script.mission.state", 1);
		harassTime = (GetTime() + HARASS_INTERVAL);
		break;
	case 1: {//check to see that the initial wave of enemies are dead.
		bool allKilled = true;
		for (i = 0; i < ENEMY_FORCE_A; i++) {
			if (IsAlive(forceA[i])) {
				allKilled = false;
			}
		}
		if (allKilled) {
			SetGroup(recycler, 0);
			SetGroup(constructor, 1);
 			ClearObjectives();
			AddObjective("stratDem2.otf",WHITE,20.0f); //clear area and build a base
 			IFace_SetInteger("script.mission.state", 2);
		}				 
		break;	 }
	case 2: //Wait for the Player to build a power generator
		if (pGen) {
			IFace_SetInteger("script.mission.state", 3);
		}
		break;
	case 3:	 //send in a bunch of enemies
		for (i = 0; i < HARASS_FORCE; i++) {
			forceA[i] = BuildObject("ivapc", 1, attackSpawn[i]);
			Attack(forceA[i], pGenerator, 1);
		}
		IFace_SetInteger("script.mission.state", 4);
		break;
	case 4:	//Wait for the Player to build a gun tower.
		if (gTow) {
			IFace_SetInteger("script.mission.state", 5);
		}
		break;
	case 5: //Send a big force to attack the Player
		for (i = 0; i < ENEMY_FORCE_B; i++) {
			forceB[i] = BuildObject("fvtank", 1, attackSpawn[i]);
			Attack(forceB[i], player, 1);
		}
		IFace_SetInteger("script.mission.state", 6);
		break;
	case 6:
		break;
	}
	
	//Send in a quick force to keep it it interesting.
	if (harassTime < GetTime()) {
		bool allKilled = true;
		int i;
		for (i = 0; i < ENEMY_FORCE_A; i++) {
			if (IsAlive(forceA[i])) {
				allKilled = false;
			}
		}
		for (i = 0; i < ENEMY_FORCE_B; i++) {
			if (IsAlive(forceB[i])) {
				allKilled = false;
			}
		}
		for (i = 0; i < HARASS_FORCE; i++) {
			if (IsAlive(harass[i])) {
				allKilled = false;
			}
		}

 		if (allKilled) {
			for (i = 0; i < HARASS_FORCE; i++) {
				harass[i] = BuildObject("fvtank", 1, attackSpawn[i]);
				Attack(harass[i], player, 1);
				harassTime = (GetTime() + HARASS_INTERVAL);
			}
		} 
	}


	if (GetScrap(2) < 50) {
		SetScrap(2, 50);
	}

	if (GetHealth(player) < 0.7f) {
		AddHealth(player, 1000);
	}
	if (GetAmmo(player) < 0.5f) {
		AddAmmo(player, 500);
	}



}
