//
// Copyright(C) 2014-2015 Samuel Villarreal
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// DESCRIPTION:
//      Alien Script Object Class
//

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Alien
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokAlien
==============================================================
*/

final class TurokAlien : TurokEnemy
{
	TurokAlien(kActor @actor)
	{
		super(actor);
	}

	void InitHeight()
	{
		self.Height() += 40 * self.Scale().z;
	}
	void FixDeadHeight()
	{
		self.Height() = 25 * self.Scale().z + (self.Height() - self.StepHeight());
	}

	bool Headshot( kVec3&in hitLoc )
	{
		// can't hit head from behind
		if ( Math::Fabs( Math::Rad2Deg( self.GetTurnYaw( hitLoc ) ) ) > 90 )
			return false;
		return (hitLoc.z - self.Origin().z) / self.Height() > (self.Flags() & AF_STATIONARY == 0 ? 0.6f : 0.7f);
	}
	void SpawnHeadLine()
	{
		// c = a/h
		float angle = Math::ACos( 0 );
		float height = self.Height() * (self.Flags() & AF_STATIONARY == 0 ? 0.6f : 0.7f);
		float delta = 2 * angle / 7;
		for ( int i = 0;  i < 8;  i++ )
		{
			Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin() + kVec3( 0, 0, height )
				+ kVec3( 0, self.Radius(), 0 ) * kQuat( angle - i * delta - self.Yaw(), 0,0,1 ), -1 );
		}
	}

	// Smoke39
	kVec3 DazeOffset()
	{
		if ( self.Flags() & AF_STATIONARY != 0 )
			return kVec3( -11, -53, 113 );
		return kVec3( -4, -68, 113 );
	}

	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( -10, 75, 45 ) );
	}

	/*
	==============================================================
	GunFire
	==============================================================
	*/

	void GunFire(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		if(self.GetTarget() is null)
		{
			return;
		}

		self.PlaySound("sounds/shaders/tek_weapon_1.ksnd");

		self.SpawnProjectile("fx/enemy_plasma1.kfx", kVec3(x, y, z),
			self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
	}

	/*
	==============================================================
	DeathSound
	==============================================================
	*/

	void DeathSound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.PlaySound("sounds/shaders/alien_death.ksnd");
	}

	/*
	==============================================================
	ViolentSound
	==============================================================
	*/

	void ViolentSound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.PlaySound("sounds/shaders/alien_violent_death.ksnd");
	}

	// Smoke39 - actually play death sound when blown away
	void OnDeath( kActor@ killer, kDictMem@ damageDef )
	{
		TurokEnemy::OnDeath( killer, damageDef );

		if ( damageDef !is null && GetBool( damageDef, "bExplode" ) && !GetBool( damageDef, "bNoAIBlast" ) )
			self.PlaySound( "sounds/shaders/TPlus/Alien_Death_Explosive.ksnd" );
	}
}

//-----------------------------------------------------------------------------
//
// TurokStalker
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokStalker
==============================================================
*/

final class TurokStalker : TurokEnemy
{
	// Smoke39 - used to prevent stacking melee hit sounds
	bool bPlayedMeleeHit = false;

	TurokStalker(kActor @actor)
	{
		super(actor);
	}

	// Smoke39
	int EnemyTier()
	{
		// small: 10;  medium: 20;  large: 60
		return Int( self.SpawnParams( SP_HEALTH ) >= 15 ) + Int( self.SpawnParams( SP_HEALTH ) >= 50 );
	}

	// Smoke39
	void InitHeight()
	{
		self.Height() += 23 * self.Scale().z;
	}
	void FixHeight()
	{
		self.Height() = 50 * self.Scale().z + (self.Height() - InitialHeight);
	}
	void FixDeadHeight()
	{
		self.Height() = 20 * self.Scale().z + (self.Height() - self.StepHeight());
	}
	void FixRadius()
	{
		if ( self.Scale().z > 1.0f )
			self.Radius() *= 0.75f;
	}

	bool Headshot( kVec3&in hitLoc )
	{
		// can't hit head from behind
//		if ( Math::Fabs( Math::Rad2Deg( self.GetTurnYaw(hitLoc) ) ) > 90 )
		float f = ( hitLoc - self.Origin() ).Dot( kVec3(0,1,0) * kQuat( -self.Yaw(), 0,0,1 ) ) / self.Radius();
		if ( f < ( self.Scale().z > 1 ? 0.975f : 0.75f ) )
			return false;
		f = (hitLoc.z - self.Origin().z) / self.Height();
		if ( self.Scale().z > 1 )
			return f > 0.2f && f < 0.6f;
		return f > 0.25f && f < 0.75f;
	}
	void SpawnHeadLine()
	{
		// c = a/h
		float angle = Math::ACos( self.Scale().z > 1 ? 0.975f : 0.75f );
		float top = self.Scale().z > 1 ? 0.6f : 0.75f;
		float bot = self.Scale().z > 1 ? 0.2f : 0.25f;
		float delta = 2 * angle / 3;
		for ( int i = 0;  i < 4;  i++ )
		{
			Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin() + kVec3( 0, 0, self.Height() * top )
				+ kVec3( 0, self.Radius(), 0 ) * kQuat( angle - i * delta - self.Yaw(), 0,0,1 ), -1 );
			Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin() + kVec3( 0, 0, self.Height() * bot )
				+ kVec3( 0, self.Radius(), 0 ) * kQuat( angle - i * delta - self.Yaw(), 0,0,1 ), -1 );
		}
	}

	kVec3 DazeOffset()
	{
		if ( self.Flags() & AF_STATIONARY != 0 )
			return kVec3( 1, -64, 29 );
		return kVec3( 1, -55, 29 );
	}

	void KnockoutAnim( int&out anim, float&out rate, float&out tween )
	{
		anim = self.AnimState().CheckAnimID( anim_AI_Knockout ) ? anim_AI_Knockout : anim_aiDeathStand;
		rate = self.AnimSpeed() * 2;
		tween = self.AnimSpeed() * 2;
	}
	bool SnoozeTime()
	{
		return self.AnimState().TrackTime() > 0.65f;
	}
	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( 0, -50, 15 ) );
	}

	// Smoke39 - really big leapers take slightly reduced knockback
	// small: 0.625
	// medium: 0.833333
	// large: 1.25
	void TransferMomentum( float momentumTransfer, bool bExplode )
	{
		if ( self.Scale().z > 1.0f )
			momentumTransfer *= 0.75f;
		TurokEnemy::TransferMomentum( momentumTransfer, bExplode );
	}

	//==========================================================================
	// Smoke39 - override TurokEnemy melee functions with new melee method

	// 2-handed underwater swipe
	void MeleeBluntWeak( kActor@ instigator, const float w, const float x, const float y, const float z )
	{
		kVec3 pos = self.GetTransformedVector( kVec3(x,y,z) );
		self.InteractActorsAtPosition( pos, "SmartMelee_Weak", pos.x, pos.y, pos.z, w );
	}
	// right hand swipe
	void MeleeBluntWimpy( kActor@ instigator, const float w, const float x, const float y, const float z )
	{
		kVec3 pos = self.GetTransformedVector( kVec3(x,y,z) );
		self.InteractActorsAtPosition( pos, "SmartMelee_Wimpy", pos.x, pos.y, pos.z, w );
	}

	void SmartMelee_Weak( kActor@ Other, const float x, const float y, const float z, const float radius )
	{
		SmartMelee( 3, Other, kVec3(x,y,z), radius );
	}
	void SmartMelee_Wimpy( kActor@ Other, const float x, const float y, const float z, const float radius )
	{
		SmartMelee( 2, Other, kVec3(x,y,z), radius );
	}
	void SmartMelee( int damage, kActor@ Other, kVec3&in pos, float radius )
	{
		if ( Other is null || Other.IsStale() || Other.Flags() & (AF_SOLID|AF_DEAD) != AF_SOLID ) return;
		kVec3 v = Other.Origin();
		v.z += Other.Height() * 0.5f;
		if ( (v - pos).Unit() - Other.Radius() > (radius + 1) * 2 * GAME_SCALE )
			return;
		if ( Math::Fabs( (Other.Origin() - self.Origin()).ToYaw() - self.Yaw() ) < Math::Deg2Rad(135) )
		{
			Other.InflictGenericDamage( self, damage );
			if ( bPlayedMeleeHit ) return;
			self.PlaySound( "sounds/shaders/generic_81_kick_impact.ksnd" );
			bPlayedMeleeHit = true;
		}
	}

	void OnTick()
	{
		TurokEnemy::OnTick();
		bPlayedMeleeHit = false;
	}

	/*
	==============================================================
	DeathSound
	==============================================================
	*/

	void DeathSound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.PlaySound("sounds/shaders/leaper_death.ksnd");
	}

	/*
	==============================================================
	InjurySound
	==============================================================
	*/

	void InjurySound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.PlaySound("sounds/shaders/leaper_injury.ksnd");
	}

	/*
	==============================================================
	ViolentSound
	==============================================================
	*/

	void ViolentSound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.PlaySound("sounds/shaders/leaper_violent_death.ksnd");
	}
}
