// Vehicle Handler.
// Worthless Bums 2013.


function isLargeUnit (%unit)
{//echo("isLargeUnit()");
	if (isSquadMember(%unit))
	{
		return (%unit.vehicle != -1);
	}
	else
	{
		return false;
	}
}
function setUnitInHulkSuit (%unit)
{//echo("setUnitInHulkSuit()");
	%unit.size = "8 8";
	%unit.originalSize = %unit.size;
	%unit.vehicle = 0;
	if (isObject(%unit.weaponsprite))
	{
		%unit.weaponsprite.setVisible(false);
	}
	setSquadMemberFacing(%unit.facing, %unit);
	%position = getTallUnitTilePositionFromIndices(%unit.xIndex, %unit.yIndex);
	%unit.position = %position;
}
function removeUnitHulkSuit (%characterIndex)
{//echo("removeUnitHulkSuit()");
	$playerSquad[%characterIndex].maxHealth	-= $hulkSuitHealth;		// Remove Hulk suit health.
	if ($playerSquad[%characterIndex].maxHealth <= 0)
	{
		$playerSquad[%characterIndex].maxHealth = 1;				// Floor max health at 1.
	}
	$playerSquad[%characterIndex].currentHealth = $playerSquad[%characterIndex].maxHealth;	// Restore max health.
	createHulkSuitExplosionAnimation($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex);
	playExplosionSfx();
	$playerSquad[%characterIndex].vehicle = -1;
	setSquadMemberVehicleStatus(%characterIndex);
	schedule(130, 0, "setUnitSize", $playerSquad[%characterIndex], "4 4");
	updateFloatingHealthPointText(%characterIndex);
}
function setUnitSize (%unit, %size)
{//echo("setUnitSize()");
	%unit.size = "4 4";
	%unit.originalSize = "4 4";
}
function setSquadToHulkSuit ()
{//echo("setSquadToHulkSuit()");
	setUnitInHulkSuit($playerSquad[0]);
	setUnitInHulkSuit($playerSquad[1]);
	setUnitInHulkSuit($playerSquad[2]);
	setUnitInHulkSuit($playerSquad[3]);
}
function updateVehicleStatuses ()
{//echo("updateVehicleStatuses()");
	if ($leaderPersist)
	{
		setSquadMemberVehicleStatus(0);
	}
	if ($scoutPersist)
	{
		setSquadMemberVehicleStatus(1);
	}
	if ($grenadierPersist)
	{
		setSquadMemberVehicleStatus(2);
	}
	if ($supportPersist)
	{
		setSquadMemberVehicleStatus(3);
	}
}
function setSquadMemberVehicleStatus (%characterIndex)
{//echo("setSquadMemberVehicleStatus()");
	if (isObject($playerSquad[%characterIndex]))
	{
		switch ($playerSquad[%characterIndex].vehicle)
		{
			case -1:	// Regular armor.
				$playerSquad[%characterIndex].size = "4 4";
				if (isObject($playerSquad[%characterIndex].weaponsprite))	// This might not be true on loading a save file!
				{
					//$playerSquad[%characterIndex].weaponsprite.setVisible(true);
					$playerSquad[%characterIndex].weaponsprite.setVisible(false);
				}
				setSquadMemberFacing($playerSquad[%characterIndex].facing, $playerSquad[%characterIndex]);
			case  0:	// Hulk Suit.
				setUnitInHulkSuit($playerSquad[%characterIndex]);
		}
	}
}
// Uses Hulk ranged weapon.
// NOTE: Change this to be a flame thrower! And let the real Hulk boss use it, too!
function useHulkRangedWeapon (%characterIndex, %guardMode)
{//echo("useHulkRangedWeapon()");echo("%characterIndex =" SPC %characterIndex);
	%direction = $playerSquad[%characterIndex].facing;
	if (!%guardMode)
	{
		if (!$playerSquad[%characterIndex].guardMode	&&
			$playerSquad[%characterIndex].currentActionPoints > 0)
		{
			if ($playerSquad[%characterIndex].perks $= "FireHazard")	// "Fire Hazard" perk causes Hulk Suit flamethrower to only cost 1 AP!
			{
				setCurrentActionPoints(%characterIndex, $playerSquad[%characterIndex].currentActionPoints - 1);
			}
			else
			{
				setCurrentActionPoints(%characterIndex, 0);			// Hulk ranged attack uses all remaining action points.
			}
		}
		else
		{
			return;
		}
	}
	lockFireControls();
	createHulkFlamethrowerAnimation($playerSquad[%characterIndex].xIndex, $playerSquad[%characterIndex].yIndex, $playerSquad[%characterIndex].facing);
	// Flamethrower has a range of $HulkFlamethrowerRange tiles!
	for (%i = 1; %i <= $HulkFlamethrowerRange; %i++)
	{
		%xIndex = -1;
		%yIndex = -1;
		switch (%direction)
		{
			case $northDirection:
				%xIndex = $playerSquad[%characterIndex].xIndex;
				%yIndex = $playerSquad[%characterIndex].yIndex - %i;
			case $southDirection:
				%xIndex = $playerSquad[%characterIndex].xIndex;
				%yIndex = $playerSquad[%characterIndex].yIndex + %i;
			case $eastDirection:
				%xIndex = $playerSquad[%characterIndex].xIndex + %i;
				%yIndex = $playerSquad[%characterIndex].yIndex;
			case $westDirection:
				%xIndex = $playerSquad[%characterIndex].xIndex - %i;
				%yIndex = $playerSquad[%characterIndex].yIndex;
		}
		if (canFireThrough(%xIndex, %yIndex))
		{
			%monsterIndex = getMonsterIndexOnTile(%xIndex, %yIndex);
			if (%monsterIndex != -1)
			{
				%monster = $monsterList.getObject(%monsterIndex);
				if (%monster.currentHealth <= $HulkFlamethrowerDamage)
				{
					%monster.currentHealth = 0;
				}
				else
				{
					%monster.currentHealth -= $HulkFlamethrowerDamage;
				}
				updateMonsterFloatingHealthPointText(%monsterIndex);
				monsterHit(%monsterIndex, $HulkFlamethrowerDamage, %direction, "fire");
			}
		}
		else
		{
			%i = $HulkFlamethrowerRange + 1;		// Break out.
		}
	}
	schedule(500, 0, "unlockFireControls");
	scheduleMonsterTurn();
}