// Random Encounter Handler.
// Worthless Bums 2013.


// Triggers a random encounter/event.
// NOTE: Intended to be called at the start of player turn.
function triggerRandomEncounter ()
{//echo("triggerRandomEncounter()");
	if (getRandom(1, 100) <= $RandomPlayerStartTurnEncounterChance)
	{
		%encounterIndex = getRandom(0, 3);
		%characterIndex = getRandomMarineIndex();
		if (%characterIndex == -1)
		{
			return;
		}
		enableGuiInProgress();
		cancelButton.setText("");
		quitButton.position = "100 178";
		switch (%encounterIndex)
		{
			case 0: //echo("Space Weather!");
				switch ($CurrentWeather)
				{
					case 0:	// No current weather!
						disableGuiInProgress();
					case 1:	// Olivine Rain.
						%armorLoss = getRandom(1, 2);
						quitButton.setText("#$%@!");
						messageBoxOKCancel("Olivine Rain!",
										   "Olivine Rain Corrosion!"	NL
										  "\n" @  $playerSquad[%characterIndex].name @ " loses " @ %armorLoss @ " armor!",
										   "doNotQuit();",				// Right Button.
										   "doNotQuit();");				// Left Button.
						$playerSquad[%characterIndex].armor.armorRating -= %armorLoss;
						if ($playerSquad[%characterIndex].armor.armorRating < 0)
						{
							$playerSquad[%characterIndex].armor.armorRating = 0;
						}
					case 2:	// Meteor Shower.
						quitButton.setText("#$%@!");
						messageBoxOKCancel("Meteor Shower!",
										   "Meteor Shower Dodge!"		NL
										  "\n" @  $playerSquad[%characterIndex].name @ " faces in\nrandom direction!",
										   "doNotQuit();",				// Right Button.
										   "doNotQuit();");				// Left Button.
						setSquadMemberFacing(getRandom(1, 4), $playerSquad[%characterIndex]);
				}
			case 1:	//echo("Gamma Rays!");
				quitButton.setText("What.");
				messageBoxOKCancel("Gamma Rays Strike!",
								   "Gamma Rays Strike!"			NL
								   "\n" @ $playerSquad[%characterIndex].name @ " regains health,\nbut is sterilized!",
								   "doNotQuit();",				// Right Button.
								   "doNotQuit();");				// Left Button.
				$playerSquad[%characterIndex].currentHealth = $playerSquad[%characterIndex].maxHealth;
			case 2:	//echo("Particle Bombardment!");
				quitButton.setText("Haha!");
				messageBoxOKCancel("Particle Bombardment!",
								   "Particle Bombardment!"		NL
								  "\n" @  $playerSquad[%characterIndex].name @ " temporarily gains\n+4 action points!",
								   "doNotQuit();",				// Right Button.
								   "doNotQuit();");				// Left Button.
				$playerSquad[%characterIndex].currentActionPoints += 4;
			case 3:	//echo("Meteor Shards!");
				%armorGain = getRandom(1, 3);
				quitButton.setText("Crap.");
				messageBoxOKCancel("Meteor Shards!",
								   "Meteor Shards!"		NL
								  "\n" @  "Robot enemies on this\nlevel gain +" @ %armorGain @ " armor!",
								   "doNotQuit();",				// Right Button.
								   "doNotQuit();");				// Left Button.
				for (%i = 0; %i < $monsterList.getCount(); %i++)
				{
					%monster = $monsterList.getObject(%i);
					if (%monster.robot)
					{
						%monster.armorRating += %armorGain;
					}
				}
			case 4:	//echo("Gravity Well!");
				quitButton.setText("Mwah?");
				messageBoxOKCancel("Gravity Well!",
								   "Gravity Well!"		NL
								  "\n" @  "Movement and facing\nis free this turn\nbut requires 1+ AP!",
								   "doNotQuit();",				// Right Button.
								   "doNotQuit();");				// Left Button.
				$GravityWellOn = true;
		}
	}
	updateFloatingHealthPointText(%characterIndex);
	updateFloatingActionPointText(%characterIndex);
	initializeReadout();
}
function getRandomMarineIndex ()
{//echo("getRandomMarineIndex()");
	%characterIndex = -1;
	if ($leaderPersist		||
		$scoutPersist		||
		$grenadierPersist	||
		$supportPersist)
	{
		do
		{
			%characterIndex = getRandom(0, 3);
		} while (!isObject($playerSquad[%characterIndex]))
	}
	return %characterIndex;
}