// Handles Status Effects.
// Worthless Bums 2012.


function updateAllStatusEffects ()
{//echo("updateAllStatusEffects()");
	// NOTE: Should unroll this for all statuses later!
	updateFireStatusEffects();
	removeStasisFromEnemyUnits();
}
function removeStasisFromEnemyUnits ()
{//echo("removeStasisFromEnemyUnits()");
	%playStasisBreak = false;
	for (%i = 0; %i < $monsterList.getCount(); %i++)
	{
		%monster = $monsterList.getObject(%i);
		if (%monster.stasis)
		{
			%monster.stasis = false;
			(%monster.stasisAnimation).safeDelete();
			if (!%playStasisBreak)
			{
				playAudio("Audio:stasisBreak", EffectsAudioVolume.value);
				%playStasisBreak = true;
			}
		}
	}
}
function clearSquadStatusEffects ()
{//echo("clearSquadStatusEffects()");
	$leaderStatus = 0;		// 0 means no status effect.
	$leaderStatusAnimation.safeDelete();
	$scoutStatus = 0;		// 0 means no status effect.
	$scoutStatusAnimation.safeDelete();
	$grenadierStatus = 0;	// 0 means no status effect.
	$grenadierStatusAnimation.safeDelete();
	$supportStatus = 0;		// 0 means no status effect.
	$supportStatusAnimation.safeDelete();
}
function clearMonsterStatusEffects ()	// NOTE: This is never called!
{//echo("clearMonsterStatusEffects()");
	// Nothing here yet!
}
function isSquadMemberParalyzed (%characterIndex)
{//echo("isSquadMemberParalyzed()");
	%characterStatus = -1;
	switch ($playerSquad[%characterIndex].characterClass)
	{
		case 0:	%characterStatus = $leaderStatus;
		case 1:	%characterStatus = $scoutStatus;
		case 2:	%characterStatus = $grenadierStatus;
		case 3:	%characterStatus = $supportStatus;
		case 4:	%characterStatus = $engineerStatus;
	}
	if (%characterStatus == $paralyzeEffect)
	{
		return true;
	}
	else
	{
		return false;
	}
}
function paralyzeSquadMember (%characterIndex)
{//echo("paralyzeSquadMember()");
	switch (%characterIndex)
	{
		case 0:	$leaderStatus = $paralyzeEffect;
		case 1:	$scoutStatus = $paralyzeEffect;
		case 2:	$grenadierStatus = $paralyzeEffect;
		case 3:	$supportStatus = $paralyzeEffect;
	}
	$playerSquad[%characterIndex].currentActionPoints = 0;
	updateReadouts();
	updateEventText($squadColor @ $lastHitSquadMemberName @ $hostileActionColor @ " has been paralyzed" @ $defaultColor @ "!");
}
function removeParalyzeSquadMember (%characterIndex)
{//echo("removeParalyzeSquadMember()");
	switch ($playerSquad[%characterIndex].characterClass)
	{
		case 0:
			$leaderStatus = $noffect;
			$leaderStatusAnimation.safeDelete();
		case 1:
			$scoutStatus = $noffect;
			$scoutStatusAnimation.safeDelete();
		case 2:
			$grenadierStatus = $noffect;
			$grenadierStatusAnimation.safeDelete();
		case 3:
			$supportStatus = $noffect;
			$supportStatusAnimation.safeDelete();
		case 3:
			$engineerStatus = $noffect;
			$engineerStatusAnimation.safeDelete();
	}
	updateReadouts();
}
// NOTE: This isn't really a status effect - does this belong in a different file?
function reduceArmorSquadMember (%characterIndex)
{//echo("reduceArmorSquadMember()");
	if (($playerSquad[%characterIndex].armor).armorRating > 0)
	{
		($playerSquad[%characterIndex].armor).armorRating--;
	}
	playAudio("Audio:acid", EffectsAudioVolume.value);
	updateEventText($squadColor @ $playerSquad[%characterIndex].name @ $hostileActionColor @ " loses 1 armor rating" @ $defaultColor @ "!");
	updateSquadInfoGui();
}
// Reduces next turn's action points of specified squad member.
// NOTE: This isn't really a status effect - does this belong in a different file?
function reduceActionPointSquadMember (%characterIndex)
{//echo("reduceActionPointSquadMember()");
	if ($playerSquad[%characterIndex].currentActionPoints > 0)
	{
		$playerSquad[%characterIndex].currentActionPoints--;
	}
	playAudio("Audio:acid", EffectsAudioVolume.value);
	updateEventText($squadColor @ $playerSquad[%characterIndex].name @ $hostileActionColor @ " loses 1 action point" @ $defaultColor @ "!");
	updateSquadInfoGui();
}
function updateSquadStatusEffects ()
{//echo("updateSquadStatusEffects()");
	if ($leaderPersist && isSquadMemberParalyzed(0))
	{
		%paralyzeRemoveChance = 25;
		%paralyzeRoll = getRandom(1, 100);
		if (%paralyzeRoll <= %paralyzeRemoveChance)
		{
			$leaderStatus = 0;
			$leaderStatusAnimation.safeDelete();
		}
	}
	if ($scoutPersist && isSquadMemberParalyzed(1))
	{
		%paralyzeRemoveChance = 25;
		%paralyzeRoll = getRandom(1, 100);
		if (%paralyzeRoll <= %paralyzeRemoveChance)
		{
			$scoutStatus = 0;
			$scoutStatusAnimation.safeDelete();
		}
	}
	if ($grenadierPersist && isSquadMemberParalyzed(2))
	{
		%paralyzeRemoveChance = 25;
		%paralyzeRoll = getRandom(1, 100);
		if (%paralyzeRoll <= %paralyzeRemoveChance)
		{
			$grenadierStatus = 0;
			$grenadierStatusAnimation.safeDelete();
		}
	}
	if ($supportPersist && isSquadMemberParalyzed(3))
	{
		%paralyzeRemoveChance = 25;
		%paralyzeRoll = getRandom(1, 100);
		if (%paralyzeRoll <= %paralyzeRemoveChance)
		{
			$supportStatus = 0;
			$supportStatusAnimation.safeDelete();
		}
	}
}
function checkWeaponStatusEffects (%monsterIndex)
{//echo("checkWeaponStatusEffects()");
	if (hasWeaponName(($playerSquad[$activeCharacter].weapon).name, "Inferno"))
	{
		createFireEffect(%monsterIndex);
	}
	else if (hasWeaponName(($playerSquad[$activeCharacter].weapon).name, "Frozen"))
	{
		createFrostEffect(%monsterIndex);
	}
	else if (hasWeaponName(($playerSquad[$activeCharacter].weapon).name, "Acid"))
	{
		createAcidEffect(%monsterIndex);
	}
}
function updateFireStatusEffects ()
{//echo("updateFireStatusEffects()");
	for (%i = 0; %i < $monsterList.getCount(); %i++)
	{
		%monster = $monsterList.getObject(%i);
		if (isObject(%monster.onFireEffect))
		{//echo("%monster.onFireEffect =" SPC %monster.onFireEffect);
			// Monster is on fire, so calculate chance for fire to dissipate.
			// NOTE: Should this be based on monster level?
			%fireDissipationChance = 25;
			%fireRoll = getRandom(1, 100);
			if (%fireRoll <= %fireDissipationChance)
			{
				(%monster.onFireEffect).safeDelete();
				%monster.onFireEffect = null;
				// Don't update event log for this sort of event?
			}
			else
			{
				%fireEffectMultiplier = getMonsterStatusEffectMultiplier(%monster.name, $fireEffect);
				// NOTE: Should give more indication to player about monster's strong/weak to effect instead of just a log update. Different animation? Sound effect? BIG MESSAGE???
				%fireDamage = getRandom(1, 5) * %fireEffectMultiplier;
				scrollFloatingText(%monster, %fireDamage, "damage");
				if (%monster.currentHealth <= %fireDamage)
				{
					%monster.currentHealth = 0;
					if (%monster.isRobot)
					{
						triggerArmorRepair();
					}
					updateKillRecord($activeCharacter, %monster.name, %monster.level);
					handleMonsterDeath($activeCharacter, %i);
					updateEventText($squadColor @ "Fire Effect " @ $damageColor @ "kills" SPC $monsterColor @ %monster.name @ $defaultColor @ "!");
				}
				else
				{
					scrollFloatingText(%monster, %fireDamage, "damage");
					%monster.currentHealth -= %fireDamage;
					updateEventText($squadColor @ "Fire Effect " @ $friendlyActionColor @ "burns" SPC $monsterColor @ %monster.name SPC $defaultColor @ "for" SPC $damageColor @ %fireDamage SPC "damage" @ $defaultColor @ "!");
				}
				handleHealthIndicators();
			}
		}
	}
}
function getMonsterStatusEffectMultiplier (%monsterName, %effectType)
{//echo("getMonsterStatusEffectMultiplier()");
	%effectMultiplier = 1.0;
	switch$ (%monsterName)
	{
		case "Fire Elemental":
			switch (%effectType)
			{
				case $fireEffect:	%effectMultiplier = 0.0;		// Fire Elementals are immune to fire!
					
				case $iceEffect:	%effectMultiplier = 3.0;		// Fire Elementals are vulnerable to ice!
			}
		case "Mummy":
			switch (%effectType)
			{
				case $fireEffect:	%effectMultiplier = 4.0;		// Mummies are vulnerable to fire!
			}
	}
	return %effectMultiplier;
}