// Handles Environment Effects.
// Worthless Bums 2012.


function executeRandomWeatherEvent ()
{//echo("executeRandomWeatherEvent()");
	if (getRandom(1, 100) <= $WeatherChangeChance)
	{//echo("$WeatherChangeChance =" SPC $WeatherChangeChance);
		if (isEventPending($WeatherEvent))
		{
			cancel($WeatherEvent);
		}
		switch (getRandom(0, 2))
		{
			case 0:	cancel($WeatherEvent);		// Redundant!
					$CurrentWeather = 0;
			case 1:	executeOlivineCrystalRain();
					$CurrentWeather = 1;
			case 2: executeMeteorShower();
					$CurrentWeather = 2;
		}
	}
}
function executeOlivineCrystalRain ()
{//echo("executeOlivineCrystalRain()");
	%squadMemberIndex = getRandom(0, 3);
	if (isObject($playerSquad[%squadMemberIndex]))
	{
		createOlivineAnimation($playerSquad[%squadMemberIndex].xIndex + getRandom(-2, 2), $playerSquad[%squadMemberIndex].yIndex + getRandom(-2, 2));
	}
	$WeatherEvent = schedule(500 + getRandom(-150, 150), 0, "executeOlivineCrystalRain");
}
function executeMeteorShower ()
{//echo("executeMeteorShower()");
	%squadMemberIndex = getRandom(0, 3);
	if (isObject($playerSquad[%squadMemberIndex]))
	{
		createMeteorShowerAnimation($playerSquad[%squadMemberIndex].xIndex + getRandom(-2, 2), $playerSquad[%squadMemberIndex].yIndex + getRandom(-2, 2));
	}
	$WeatherEvent = schedule(500 + getRandom(-150, 150), 0, "executeMeteorShower");
}
function updateEnvironmentEffects ()
{//echo("updateEnvironmentEffects()");
	if (isObject($environmentEffects))
	{
		for (%i = 0; %i < $environmentEffects.getCount(); %i++)
		{
			%effect = $environmentEffects.getObject(%i);
			%effect.turns--;
			if (%effect.turns <= 0)
			{
				%effect.safeDelete();
			}
		}
	}
}
function hasObscuringEffect (%xIndex, %yIndex)
{//echo("hasObscuringEffect()");
	for (%i = 0; %i < $environmentEffects.getCount(); %i++)
	{
		%effect = $environmentEffects.getObject(%i);
 		if (%effect.effectType $= "smoke")
		{
			%tilePosition = getTilePositionFromIndices(%x0, %y0);
			if (%tilePosition == %effect.getPosition())
			{
				return true;
			}
		}
	}
	return false;
}
function isSquadMember (%unit)
{//echo("isSquadMember()");
	if (($leaderPersist		&&	%unit == $playerSquad[0])	||
		($scoutPersist		&&	%unit == $playerSquad[1])	||
		($grenadierPersist	&&	%unit == $playerSquad[2])	||
		($supportPersist	&&	%unit == $playerSquad[3]))
	{
		return true;
	}
	return false;
}
function getSquadMemberIndex (%unit)
{//echo("getSquadMemberIndex()");
	if ($leaderPersist		&&
		%unit == $playerSquad[0])
	{
		return 0;
	}
	if ($scoutPersist		&&
		%unit == $playerSquad[1])
	{
		return 1;
	}
	if ($grenadierPersist		&&
		%unit == $playerSquad[2])
	{
		return 2;
	}
	if ($supportPersist		&&
		%unit == $playerSquad[3])
	{
		return 3;
	}
	return -1;
}
function dealEnvironmentalDamage (%objectDamaged, %damage, %canKill)
{//echo("dealEnvironmentalDamage()");
	if (!isMonster(%objectDamaged))
	{
		$GlobalStats.totalDamageTaken += %damage;
	}
	%xIndex = %objectDamaged.xIndex;
	%yIndex = %objectDamaged.yIndex;
	%minimumHealth = 1;
	if (%canKill)
	{
		%minimumHealth = 0;
	}
	if (isSquadMember(%objectDamaged))
	{
		%damage -= (%objectDamaged.armor).explosiveResistance;
		if (%damage < 0)
		{
			%damage = 0;
		}
	}
	if (%objectDamaged.currentHealth <= %damage)
	{
		%objectDamaged.currentHealth = %minimumHealth;
	}
	else
	{
		%objectDamaged.currentHealth -= %damage;
	}
	handleGrenadierExplosion(%xIndex, %yIndex);
	scrollFloatingText(%objectDamaged, %damage, "damage");
	initializeReadout();
	updateSquadMemberFloatingHealthPoints();
	schedule(100, 0, "updateSquadMemberFloatingHealthPoints");
	if (%canKill	&&
		%objectDamaged.currentHealth <= 0)
	{
		if (%objectDamaged == $playerSquad[0])
		{
			executeSquadMemberDeath(0, true, "[Explosion]");
		}
		else if (%objectDamaged == $playerSquad[1])
		{
			executeSquadMemberDeath(1, true, "[Explosion]");
		}
		else if (%objectDamaged == $playerSquad[2])
		{
			executeSquadMemberDeath(2, true, "[Explosion]");
		}
		else if (%objectDamaged == $playerSquad[3])
		{
			executeSquadMemberDeath(3, true, "[Explosion]");
		}
		else
		{
			%objectDamaged.safeDelete();
		}
	}
}
function destroyMinesInRadius (%xIndex, %yIndex, %radius)
{//echo("destroyMinesInRadius()");
	for (%x = %xIndex - %radius; %x <= %xIndex + %radius; %x++)
	{
		for (%y = %yIndex - %radius; %y <= %yIndex + %radius; %y++)
		{
			for (%i = 0; %i < $trapList.getCount(); %i++)
			{
				%trap = $trapList.getObject(%i);
				if (%x == %trap.xIndex	&&
					%y == %trap.yIndex)
				{
					$trapList.remove(%trap);
					%trap.safeDelete();
				}
			}
		}
	}
}
function addBloodSmear (%xIndex, %yIndex)
{//echo("addBloodSmear()");
	%bloodSmear = new Sprite()
	{
		SceneLayer = $BloodSmearLayer;
		graph = $BloodSmearGraph;
		scenegraph = levelSceneGraph;
		image = "Sprites:BloodSmear";
		canSaveDynamicFields = "1";
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		size = "4.000 4.000";
		xIndex = %xIndex;
		yIndex = %yIndex;
	};
	addObjectToScene(%bloodSmear);
}