//
// Skirmish Base Script.
//
// contains the rules for starting and ending a game.
// as well as warning messages.
// alex lee. pumpkin studios 99.
//

public	STRUCTURESTAT	command;			
public	STRUCTURESTAT	factory;			
public	STRUCTURESTAT	wall;			
public	STRUCTURESTAT	cornerWall;		
public	STRUCTURESTAT	oilDerrick;		
public	STRUCTURESTAT	powerGen;		
public	STRUCTURESTAT	research;
public	STRUCTURESTAT	commandrelay;
public 	STRUCTURESTAT	cybfac;
public 	STRUCTURESTAT	vtolfac;

////////
// starting technologies.
public	int				numCleanTech,	numBaseTech,	numDefTech;
public	RESEARCHSTAT	defTech[50];

// research topics
public	int				numBaseRes;
public	RESEARCHSTAT	baseRes[10];		

//	Other Stuff
private	INT				count;
private INT				playnum;
private	BOOL			gamewon;

// Base Under Attack  
private	STRUCTURE		hitStruc;
private	BASEOBJ			attackerObj;
private	int				t;
public	SOUND			attackSnd1;	

public	INTMESSAGE		endMsg, winMsg;

// /////////////////////////////////////////////////////////////////

trigger	endConditions(every, 100);	

// /////////////////////////////////////////////////////////////////
//this event is called once the game has initialised itself


event initialisedEvent(CALL_GAMEINIT)
{
	//set up the reticule buttons
	addReticuleButton(OPTIONS);
	addReticuleButton(CANCEL);
	addReticuleButton(BUILD);
	addReticuleButton(MANUFACTURE);
	addReticuleButton(RESEARCH);
	addReticuleButton(INTELMAP);
	addReticuleButton(DESIGN);

	playnum = 0;
	while (playnum < 8)
	{
		enableStructure(command		,playnum);		//make structures available to build
		enableStructure(factory		,playnum);
//		enableStructure(wall		,playnum);
//		enableStructure(cornerWall	,playnum);
		enableStructure(oilDerrick	,playnum);
		enableStructure(powerGen	,playnum);
		enableStructure(research	,playnum);
		
		setStructureLimits(factory,	5, playnum);	// set structure limits
		setStructureLimits(powerGen,5, playnum);
		setStructureLimits(research,3, playnum);
		setStructureLimits(command,	1, playnum);
		setStructureLimits(commandrelay,1, playnum);
		setStructureLimits(cybfac,	5, playnum);
		setStructureLimits(vtolfac,	5, playnum);
		playnum = playnum+ 1;	
	}

	applyLimitSet();	// set limit options
}

event initialisedEventTwo(CALL_GAMEINIT)
{
	
	playnum=0;
	while (playnum < 8)
	{
		count = 0;								
		while (count < numBaseRes)
		{
			enableResearch(baseRes[count], playnum);
			count = count +1;
		}

		if(multiPlayerBaseType == CAMP_CLEAN)
		{
			setPowerLevel(1300,playnum);	
			count = 0;								
			while (count < numCleanTech)
			{

				completeResearch(defTech[count], playnum);
				count = count +1;
			}
		}
		else if(multiPlayerBaseType == CAMP_BASE)
		{
			setPowerLevel(2500,playnum);	
			count = 0;								
			while (count < numBaseTech)
			{
				completeResearch(defTech[count], playnum);
				count = count +1;
			}
		
		}
		else
		{	

			setPowerLevel(2500,playnum);		
			count = 0;								
			while (count < numDefTech)
			{
				completeResearch(defTech[count], playnum);
				count = count +1;
			}
		}
	
		playnum = playnum+ 1;		
	}
}



// /////////////////////////////////////////////////////////////////
// END CONDITIONS
event checkEndConditions (endConditions)
{
	// Losing Conditions
	if(	   ( not anyDroidsLeft(selectedPlayer))
  	  and  ( not anyFactoriesLeft(selectedPlayer))	
	  )
	{
		gameOverMessage(endMsg, MISS_MSG, 0, FALSE);
		setEventTrigger(checkEndConditions, inactive);
	}
	
	// Winning Conditions. selected Player is the only remaining player
	count =0;
	gamewon = TRUE;

	while (count < multiPlayerMaxPlayers)
	{
		if ( (count != selectedPlayer) and not(isHumanPlayer(count))  )
		{
			if( anyDroidsLeft(count) or anyFactoriesLeft(count) ) 
			{
				gamewon = FALSE;
			}
		}
		count = count + 1;			
	}

	if(gamewon == TRUE)
	{
		gamewon = FALSE;
		if(dominatingAlliance())
		{
			gamewon = TRUE;
		}
	}

	if(gamewon == TRUE)
	{	
		gameOverMessage(winMsg, MISS_MSG, 0, TRUE);
		setEventTrigger(checkEndConditions, inactive);
	}	
}

// /////////////////////////////////////////////////////////////////
// WARNING MESSAGES 
// Base Under Attack 
event baseHit(CALL_STRUCT_ATTACKED, selectedPlayer, ref hitStruc, ref attackerObj)
{
	if (t >= 10)
	{
		t=0;
		if (hitStruc != NULLOBJECT)
		{
			playSoundPos(attackSnd1, selectedPlayer, hitStruc.x, hitStruc.y, hitStruc.z);	//show position if still alive
		}
		else
		{
			playSound(attackSnd1, selectedPlayer);
		}
	}
}

event everySec(every, 10)
{
	t=t+1;
}

//go to where the structure being attacked is on CTRL B
event seeBaseHit(CALL_MISSION_END)
{
	if (hitStruc!=NULLOBJECT)
	{
		centreView(hitStruc);
		t=0;							//flag known about!
	}
}

