// Mission Handler.
// Worthless Bums 2012.


/*
 * The Mission Handler operates by setting primary and secondary objectives. The former the player must complete to progress, the latter are optional for bonus rewards.
*/

// Initialize Mission Objectives.
function initializeMissionObjectives ()
{//echo("initializeMissionObjectives()");
	// Set label text.
	//$missionObjectivesLabel = "\c2" @ $overworldLocation @ ", " @ "level " @ $dungeonLevel @ " Mission Objectives:";
	$missionObjectivesLabel = "\c2" @ "Level " @ $dungeonLevel @ " Mission Objectives:";
	
	// Generate mission objectives.
	$primaryMissionObjective = "\c6" /*@ "Primary: "*/ @ generatePrimaryObjective();
	$secondaryMissionObjective = "\c6" /*@ "Secondary: "*/ @ generateSecondaryObjective();
	
	// Clear any previous mission objective text.
	//objectivesText.setText("");
	
	// Inform the player of the objectives.
	/*objectivesText.setText($missionObjectivesLabel		NL
						   $primaryMissionObjective		NL
						   $secondaryMissionObjective);*/
	
	// Update position.
	//objectivesText.position = 0 SPC (getWord($Game::Resolution, 1) / 2);
}

// Update Mission Objectives Text.
function updateMissionObjectivesText ()
{//echo("updateMissionObjectivesText()");
	// Set label text.
	//$missionObjectivesLabel = "\c2" @ $overworldLocation @ ", " @ "level " @ $dungeonLevel @ " Mission Objectives:";
	$missionObjectivesLabel = "\c2Level " @ $dungeonLevel @ " Objectives";
	
	// Update with mission objective statuses.
	$primaryMissionObjective = "\c6" @ /*"Primary: " @*/ getPrimaryObjectiveByID($primaryMissionID);
	$secondaryMissionObjective = "\c6" @ /*"Secondary: " @*/ getSecondaryObjectiveByID($secondaryMissionID);
	switch ($primaryObjectiveStatus)
	{
		case 0:		$primaryMissionObjective = "\c6" @ $primaryMissionObjective;
		case 1: 	$primaryMissionObjective = "\c6" @ "<COMPLETED>";
		case -1: 	$primaryMissionObjective = "\c6" @ "<FAILED>";
	}
	switch ($secondaryObjectiveStatus)
	{
		case 0:		$secondaryMissionObjective = "\c6" @ $secondaryMissionObjective;
		case 1: 	$secondaryMissionObjective = "\c6" @ "<COMPLETED>";
		case -1: 	$secondaryMissionObjective = "\c6" @ "<FAILED>";
	}
	
	// Clear any previous mission objective text.
	//objectivesText.setText("");
	
	// Inform the player of the objectives.
	/*objectivesText.setText($missionObjectivesLabel		NL
						   $primaryMissionObjective		NL
						   $secondaryMissionObjective);*/
	
	// Update position.
	//objectivesText.position = 0 SPC (getWord($Game::Resolution, 1) / 2);
}

// Gets a primary objective by ID.
function getPrimaryObjectiveByID (%id)
{//echo("getPrimaryObjectiveByID()");
	// Dispatch by ID.
	switch (%id)
	{
		case 0:
			return "Descend using the elevator.";
			
		case 1:
			return "Kill all enemies.";
			
		case 2:
			return "Keep the entire squad alive.";
	}
}

// Gets a secondary objective by ID.
function getSecondaryObjectiveByID (%id)
{//echo("getSecondaryObjectiveByID()");
	// Dispatch by ID.
	switch (%id)
	{
		case 0:
			return "Keep the entire squad alive.";
			
		case 1:
			return "Use no grenades.";
			
		case 2:
			return "Knock an enemy into space.";
			
		case 3:
			return "Kill an enemy with melee.";
	}
}

// Generate a primary objective.
function generatePrimaryObjective ()
{//echo("generatePrimaryObjective()");
	// Randomize a primary objective.
	%objectiveRoll = getRandom(1, 2);
	
	// Make sure we get a certain primary objective if this is the starting level!
	if ($dungeonLevel == 1	&&
		$overworldLocation $= "Command Deck")
	{
		%objectiveRoll = 0;		// Default to "Descend using the elevator."
	}
	
	// Dispatch by roll.
	switch (%objectiveRoll)
	{
		case 0:
			$primaryMissionObjective = getPrimaryObjectiveByID(0);
			$primaryMissionID = 0;
			
		case 1:
			$primaryMissionObjective = getPrimaryObjectiveByID(1);
			$primaryMissionID = 1;
			
		case 2:
			$primaryMissionObjective = getPrimaryObjectiveByID(2);
			$primaryMissionID = 2;
	}
	
	// Set primary mission not completed by default.
	// 0 = not completed.
	// 1 = completed.
	// -1 = failed.
	$primaryObjectiveStatus = 0;
}

// Generate a secondary objective.
function generateSecondaryObjective ()
{//echo("generatePrimaryObjective()");
	// Randomize a secondary objective.
	%objectiveRoll = getRandom(1, 3);
	
	// Make sure we get a certain secondary objective if this is the starting level!
	if ($dungeonLevel == 1	&&
		$overworldLocation $= "Command Deck")
	{
		%objectiveRoll = 0;		// Default to "Keep the entire squad alive."
	}
	
	// Dispatch by roll.
	switch (%objectiveRoll)
	{
		case 0:
			$secondaryMissionObjective = getSecondaryObjectiveByID(0);
			$secondaryMissionID = 0;
			
		case 1:
			$secondaryMissionObjective = getSecondaryObjectiveByID(1);
			$secondaryMissionID = 1;
			
		case 2:
			$secondaryMissionObjective = getSecondaryObjectiveByID(2);
			$secondaryMissionID = 2;
			
		case 3:
			$secondaryMissionObjective = getSecondaryObjectiveByID(3);
			$secondaryMissionID = 3;
	}
	
	// Set secondary mission not completed by default.
	// 0 = not completed.
	// 1 = completed.
	// -1 = failed.
	$secondaryObjectiveStatus = 0;
}

// Update mission objectives.
function updateMissionObjectives ()
{//echo("updateMissionObjectives()");
return;		// Not used anymore!
	// Update primary mission objectives.
	switch ($primaryMissionID)
	{
		case 0:	// Descend using elevator warrants no checking (for now)!
			if ($descendingElevator			&&
				$primaryObjectiveStatus == 0)
			{
				// Increment primary mission objectives completed!
				$GlobalStats.totalPrimaryMissionObjectivesCompleted++;
				
				// Flag primary objective as completed.
				$primaryObjectiveStatus = 1;
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Primary Objective" @ $defaultColor @ "!");
			}
		
		case 1: // Kill all enemies.
			if ($monsterList.getCount() <= 0	&&
				$primaryObjectiveStatus == 0)
			{
				// Increment primary mission objectives completed!
				$GlobalStats.totalPrimaryMissionObjectivesCompleted++;
				
				// Flag primary objective as completed.
				$primaryObjectiveStatus = 1;
				giveSquadXP($dungeonLevel * 30);
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Primary Objective" @ $defaultColor @ "!");
			}
			else if ($descendingElevator			&&
					 $monsterList.getCount() > 0	&&
					 $primaryObjectiveStatus == 0)
			{
				// Flag primary objective as failed.
				$primaryObjectiveStatus = -1;
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "failed" SPC $itemColor @ "Primary Objective" @ $defaultColor @ "!");
			}
			
		case 2: // Keep the entire squad alive.
			if ($descendingElevator			&&
				!$squadMemberDiedThisLevel	&&
				$primaryObjectiveStatus == 0)
			{
				// Increment primary mission objectives completed!
				$GlobalStats.totalPrimaryMissionObjectivesCompleted++;
				
				// Flag primary objective as completed.
				$primaryObjectiveStatus = 1;
				
				// Grant mission objective experience.
				giveSquadXP($dungeonLevel * 30);
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Primary Objective" @ $defaultColor @ "!");
			}
			else if ($squadMemberDiedThisLevel	&&
					 $primaryObjectiveStatus == 0)
			{
				// Flag primary objective as failed.
				$primaryObjectiveStatus = -1;
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "failed" SPC $itemColor @ "Primary Objective" @ $defaultColor @ "!");
			}
	}
	
	// Update secondary mission objectives.
	switch ($secondaryMissionID)
	{
		case 0:	// Keep the entire squad alive.
			if ($descendingElevator			&&
				!$squadMemberDiedThisLevel	&&
				$secondaryObjectiveStatus == 0)
			{
				// Increment secondary mission objectives completed!
				$GlobalStats.totalSecondaryMissionObjectivesCompleted++;
				
				// Flag secondary objective as completed.
				$secondaryObjectiveStatus = 1;
				
				// Grant mission objective experience.
				giveSquadXP($dungeonLevel * 20);
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
			else if ($squadMemberDiedThisLevel	&&
					 $secondaryObjectiveStatus == 0)
			{
				// Flag secondary objective as failed.
				$secondaryObjectiveStatus = -1;
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "failed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
		
		case 1: // Use no grenades.
			if ($descendingElevator			&&
				$grenadesUsed <= 0			&&
				$secondaryObjectiveStatus == 0)
			{
				// Increment secondary mission objectives completed!
				$GlobalStats.totalSecondaryMissionObjectivesCompleted++;
				
				// Flag secondary objective as completed.
				$secondaryObjectiveStatus = 1;
				
				// Grant mission objective experience.
				giveSquadXP($dungeonLevel * 20);
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
			else if ($grenadesUsed > 0		&&
					 $secondaryObjectiveStatus == 0)
			{
				// Flag secondary objective as failed.
				$secondaryObjectiveStatus = -1;
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "failed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
			
		case 2: // Knock an enemy into space.
			if ($enemiesKnockedIntoSpace > 0	&&
				$secondaryObjectiveStatus == 0)
			{
				// Increment secondary mission objectives completed!
				$GlobalStats.totalSecondaryMissionObjectivesCompleted++;
				
				// Flag secondary objective as completed.
				$secondaryObjectiveStatus = 1;
				
				// Grant mission objective experience.
				giveSquadXP($dungeonLevel * 20);
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
			else if ($descendingElevator			&&
					 $enemiesKnockedIntoSpace <= 0	&&
					 $secondaryObjectiveStatus == 0)
			{
				// Flag secondary objective as failed.
				$secondaryObjectiveStatus = -1;
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "failed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
			
		case 3: // Kill an enemy with melee.
			if ($enemiesKilledInMelee > 0	&&
				$secondaryObjectiveStatus == 0)
			{
				// Increment secondary mission objectives completed!
				$GlobalStats.totalSecondaryMissionObjectivesCompleted++;
				
				// Flag secondary objective as completed.
				$secondaryObjectiveStatus = 1;
				
				// Grant mission objective experience.
				giveSquadXP($dungeonLevel * 20);
				
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "completed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
			else if ($descendingElevator		&&
					 $enemiesKilledInMelee <= 0	&&
					 $secondaryObjectiveStatus == 0)
			{
				// Flag secondary objective as failed.
				$secondaryObjectiveStatus = -1;
				// Update event log.
				updateEventText($squadColor @ "Squad" SPC $friendlyActionColor @ "failed" SPC $itemColor @ "Secondary Objective" @ $defaultColor @ "!");
			}
	}
	
	// Update mission objectives.
	updateMissionObjectivesText();
	
	// Update Deck Stats text.
	updateDeckStats();
}