// Enforces Game States.
// Worthless Bums 2012.


// Function to enforce squad death and proceed to end game state.
// NOTE: Only run once on new/load game.
function enforceGameState ()
{//echo("enforceGameState()");
	// Purge unused memory!
	purgeResources();
	
	// Check squad status - enter squad death mode if required!
	if (!$leaderPersist		&&
		!$scoutPersist		&&
		!$grenadierPersist	&&
		!$supportPersist)
	{
		pushEndLevelScreenGuiAsDeathScreen();
		cancel($enforceGameStateEvent);
	}
	
	// Enforce monster positions!
	if (!$monsterTurnOngoing	&&
		$enemyImpulse)		// HACK to allow impulse of enemy units when hit!
	{
		enforceMonsterPositions();
	}
	else
	{
		// NOTE: Moved to function onPositionTarget() since monsters trigger callback for function moveTo() now!
		$fogOfWar.updateMonsterVisibility();
	}
	updateElevatorArrowAnimation();
}
// Enforces monster positions.
function enforceMonsterPositions ()
{//echo("enforceMonsterPositions()");
	for (%i = 0; %i < $monsterList.getCount(); %i++)
	{
		enforceTargetMonsterPosition($monsterList.getObject(%i));
	}
}
// Enforces target monster positions.
function enforceTargetMonsterPosition (%monster)
{//echo("enforceTargetMonsterPosition()");
	if (isObject(%monster))
	{//echo("%monster.name =" SPC %monster.name);
		setMonsterFacing(%monster.facing, %monster);
		//%monster.position = getUnitPosition(%monster);	// Causing monster positioning issues?
	}
}
// Gets a unit's position.
function getUnitPosition (%unit)
{//echo("getUnitPosition()");
	%position = getTilePositionFromIndices(%unit.xIndex, %unit.yIndex);
	if (%unit.name $= "Hulk")
	{
		%position = getWord(%position, 0) SPC (getWord(%position, 1) - $TallUnitVerticalShift);
	}
	if (%unit.name $= "Revenant"	||
		%unit.name $= "Revenion")
	{
		%position = getWord(%position, 0) SPC (getWord(%position, 1) + $RevenantUnitVerticalShift);
	}
	if (%unit.name $= "Gunship")
	{
		%position = getWord(%position, 0) SPC (getWord(%position, 1) - $GunshipUnitVerticalShift);
	}
	if (%unit.name $= "Matrix")
	{
		%position = getWord(%position, 0) SPC (getWord(%position, 1) - $MatrixUnitVerticalShift);
	}
	return %position;
}
// Enforces monster indices.
function enforceMonsterIndices ()
{//echo("enforceMonsterIndices()");
	for (%i = 0; %i < $monsterList.getCount(); %i++)
	{
		%monster = $monsterList.getObject(%i);
		%indices = getIndicesFromTilePosition(getWord(%monster.getPosition(), 0), getWord(%monster.getPosition(), 1));
		%monster.xIndex = mRound(getWord(%indices, 0));
		%monster.yIndex = mRound(getWord(%indices, 1));
		handleLiftedTileMarker(%monster);
	}
}
// Enforces elevator powered state!
function enforceElevatorPoweredState ()
{
	if ($multipassUsed)
	{
		$elevatorPowered = true;
	}
	else
	{
		if (!isHulkSpawned()		&&
			!isRevenantSpawned()	&&
			!isBrainienSpawned()	&&
			!isGunshipSpawned()		&&
			!isElevatorDisrupted())
		{
			$elevatorPowered = true;
		}
		else
		{
			$elevatorPowered = false;
		}
	}
}