// Automated AI Handler.
// Worthless Bums 2012.


function createSentryTurret (%xIndex, %yIndex, %damage, %range, %accuracy, %direction)
{//echo("createSentryTurret()");
	%sentryTurret = new Sprite("Sentry Turret")
	{
		SceneLayer = $TurretLayer;
		graph = $TurretGraph;
		position = getTilePositionFromIndices(%xIndex, %yIndex);
		scenegraph = levelSceneGraph;
		Animation = "Sprites:TurretIdle" @ getDirectionName(%direction);
		visible = true;
		size = "4 4";
		projectileType = "Ballistic";
		damage = %damage;
		range = %range;
		accuracy = %accuracy;
		ammo = 15;
		ammoPerShot = 3;
		xIndex = %xIndex;
		yIndex = %yIndex;
		facing = %direction;
	};
	addObjectToScene(%sentryTurret);
	$sentryTurretList.add(%sentryTurret);
	playAudio("Audio:plasmaMod", EffectsAudioVolume.value);
}
function updateSentryTurrets ()
{//echo("updateSentryTurrets()");
	for (%i = 0; %i < $sentryTurretList.getCount(); %i++)
	{
		%targetMonster = null;
		%targetDistance = 99999;
		%sentryTurret = $sentryTurretList.getObject(%i);
		for (%j = 0; %j < $monsterList.getCount(); %j++)
		{
			%monster = $monsterList.getObject(%j);
			if (%sentryTurret.xIndex == %monster.xIndex	||
				%sentryTurret.yIndex == %monster.yIndex)
			{
				%distance = VectorDist(%sentryTurret.getPosition(), %monster.getPosition());
				if (%distance <= %sentryTurret.range * 8.0	&&		// 8 pixels per each internal Torque distance unit! Range is in tile units!
					%distance < %targetDistance)
				{
					%targetMonster = %monster;
					%targetDistance = %distance;
				}
			}
		}
		// Make sure turret has line of sight!
		if (isObject(%targetMonster))
		{
			%los = targetsInLineOfSight(%sentryTurret.xIndex, %sentryTurret.yIndex, %targetMonster.xIndex, %targetMonster.yIndex);
			if (%los)
			{
				turretFireOnTarget(%sentryTurret, %targetMonster);
			}
		}
	}
}
function setSpriteAnimation (%sprite, %animation)
{//echo("setSpriteAnimation()");
	%sprite.Animation = %animation;
}
function turretFireOnTarget (%sentryTurret, %monster)
{//echo("turretFireOnTarget()");
	// Handle turret firing and return to idle animation.
	%sentryTurret.facing = getDirectionBetweenUnits(%sentryTurret, %monster);
	%sentryTurret.Animation = "Sprites:TurretFire" @ getDirectionName(%sentryTurret.facing);
	schedule(100, 0, "setSpriteAnimation", %sentryTurret, "Sprites:TurretIdle" @ getDirectionName(%sentryTurret.facing));
	// Turret projectiles.
	//createTurretFireProjectileAnimation(%sentryTurret);
	createTurretShotAnimation(%sentryTurret, %sentryTurret.xIndex, %sentryTurret.yIndex, %monster.xIndex, %monster.yIndex, %sentryTurret.facing, %sentryTurret.range, %sentryTurret.projectileType);
	
	playMachineGunSfx();
	objectHit(%monster, %sentryTurret.damage);
	updateEventText($squadColor @ "Sentry Turret" SPC $friendlyActionColor @ "shoots" SPC $monsterColor @ %monster.name SPC $defaultColor @ "for" SPC $damageColor @ %sentryTurret.damage SPC "damage" @ $defaultColor @ "!");
	%sentryTurret.ammo -= %sentryTurret.ammoPerShot;
	if (%sentryTurret.ammo <= 0)
	{
		%sentryTurret.safeDelete();
	}
	if (%monster.currentHealth > %sentryTurret.damage)
	{
		%monster.currentHealth -= %sentryTurret.damage;
	}
	else
	{
		%monster.currentHealth = 0;
		handleMonsterDeath($activeCharacter, %i);
		updateEventText($monsterColor @ %monster.name SPC $damageColor @ "dies" @ $defaultColor @ "!");
		$GlobalStats.totalKills++;
	}
	$GlobalStats.totalAmmunitionExpended += 3;		// Should all sentry turrets expend 3 rounds per firing?
	$GlobalStats.totalDamageDealt += %sentryTurret.damage;
	updateAllMonstersFloatingActionPointText();
}
//------------------------
function createTurretShotAnimation (%turret, %srcXIndex, %srcYIndex, %dstXIndex, %dstYIndex, %direction, %range, %projectileType)
{//echo("createTurretShotAnimation()");echo("%characterIndex =" SPC %characterIndex);
	%shotAnimation = createTurretFireProjectileAnimation(%turret);
	%speed = 90.0;
	%position = getTilePositionFromIndices(%srcXIndex, %srcYIndex);
	%xPos = getWord(%position, 0);
	%yPos = getWord(%position, 1);
	%effectiveRange = 0;
	if (%dstXIndex == -9999	&&
		%dstYIndex == -9999)
	{
		%effectiveRange = %range;
	}
	else
	{
		%effectiveRange = mRound(VectorDist(%srcXIndex SPC %srcYIndex, %dstXIndex SPC %dstYIndex));
	}
	%tileMagnitude = 4.0;
	switch (%direction)
	{
		case $northDirection:	%shotAnimation.moveTo(%xPos, %yPos - %effectiveRange * %tileMagnitude, %speed, true, false);
		case $southDirection:	%shotAnimation.moveTo(%xPos, %yPos + %effectiveRange * %tileMagnitude, %speed, true, false);
		case $eastDirection:	%shotAnimation.moveTo(%xPos + %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
		case $westDirection:	%shotAnimation.moveTo(%xPos - %effectiveRange * %tileMagnitude, %yPos, %speed, true, false);
	}
	%lifetimeModifier = 0.10;
//echo("%lifetimeModifier * %effectiveRange =" SPC %lifetimeModifier * %effectiveRange);
	%shotAnimation.setLifetime(%lifetimeModifier * %effectiveRange);
	playMachineGunSfx(%projectileType);
}
function createTurretFireProjectileAnimation (%turret)
{//echo("createTurretFireProjectileAnimation()");
	%x = getWord(%turret.position, 0);
	%y = getWord(%turret.position, 1);
	%offset = 3.5;
	%FlipX = 0;
	%FlipY = 0;
	switch (%turret.facing)
	{
		case $northDirection:
			%y -= %offset;
			%rotation = -90.0;
		case $southDirection:
			%y += %offset;
			%rotation = 90.0;
			%FlipY = 1;
		case $westDirection:
			%x -= %offset;
			%rotation = 0;
			%FlipX = 1;
		case $eastDirection:
			%x += %offset;
			%rotation = 0.0;
	}
	%shotAnimation = new Sprite("Projectile Animation")
	{
		SceneLayer = 0;
		scenegraph = levelSceneGraph;
		Animation = "Sprites:TurretProjectile";
		canSaveDynamicFields = "1";
		position = %x SPC %y;			// Place at character location with offset.
		size = "5.125 4.000";
		angle = %rotation;
		FlipX = %FlipX;
		FlipY = %FlipY;
	};
	addObjectToScene(%shotAnimation);
	return %shotAnimation;
}