//
// 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:
//      Raptor Script Object Class
//

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Raptor
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokRaptor
==============================================================
*/

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

	// Smoke39
	void InitHeight()
	{
		self.Height() += 23 * self.Scale().z;
	}
	void FixHeight()
	{
		self.Height() = 110 * self.Scale().z + (self.Height() - InitialHeight);
	}

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

	kVec3 FlameRadii()
	{
		return TurokEnemy::FlameRadii() * kVec3( 0.4f, 1, 1 );
	}

	kVec3 DazeOffset()
	{
		if ( self.Flags() & AF_STATIONARY != 0 )
			return kVec3( 0, -135, 130 );
		return kVec3( 0, -115, 130 );
	}

	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( -55, -90, 30 ) );
	}

    /*
    ==============================================================
    FireRaptorMissile
    ==============================================================
    */

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

        self.SpawnFx("fx/generic_178.kfx", kVec3(x, y, z));
    }

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

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

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

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

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

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

//-----------------------------------------------------------------------------
//
// Dimetrodon
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokDimetrodon
==============================================================
*/

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

	// Smoke39
	void InitHeight()
	{
		self.Height() += 23 * self.Scale().z;
	}
	void FixHeight()
	{
		self.Height() = 75 * self.Scale().z + (self.Height() - InitialHeight);
	}

	bool Headshot( kVec3&in hitLoc )
	{
		// can't hit head from behind
		if ( ( hitLoc - self.Origin() ).Dot( kVec3(0,1,0) * kQuat( -self.Yaw(), 0,0,1 ) ) / self.Radius() < 0.75f )
			return false;
		return (hitLoc.z - self.Origin().z) / self.Height() > 0.35f;
	}
	void SpawnHeadLine()
	{
		// c = a/h
		float angle = Math::ACos( 0.75f );
		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() * 0.35f )
				+ kVec3( 0, self.Radius(), 0 ) * kQuat( angle - i * delta - self.Yaw(), 0,0,1 ), -1 );
		}
	}

	kVec3 TrackPoint()
	{
		return self.Origin() + FlameOffset() * self.Rotation();
	}

	kVec3 DazeOffset()
	{
		if ( self.Flags() & AF_STATIONARY != 0 )
			return kVec3( 0, -155, 65 );
		return kVec3( 0, -125, 65 );
	}

	void KnockoutAnim( int&out anim, float&out rate, float&out tween )
	{
		if ( self.AnimState().CheckAnimID( anim_AI_Knockout ) )
		{  anim = anim_AI_Knockout;     rate = self.AnimSpeed() * 1.5f;  tween = 15;    }
		else
		{  anim = anim_aiDeathViolent;  rate = self.AnimSpeed() * 1.5f;  tween = rate;  }
	}
	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( 30, -115, 45 ) );
	}

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

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

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

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

	// Smoke39 - less knockback for tanky dimetrodons
	void TransferMomentum( float momentumTransfer, bool bExplode )
	{
		TurokEnemy::TransferMomentum( 0.5f * momentumTransfer, bExplode );
	}
}
