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

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Insect
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokInsect
==============================================================
*/

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

    // Smoke39
    bool IsBeetle()
    {
		return GetString( self.Definition(), "defaultEditorModel" ).Contains( "beetle.bin" );
    }

	bool Headshot( kVec3&in hitLoc )
	{
		// can't hit head from behind
		if ( Math::Fabs( self.GetTurnYaw( hitLoc ) ) > Math::Deg2Rad( 90 ) )
			return false;
		float f = (hitLoc.z - self.Origin().z) / self.Height();
		if ( IsBeetle() )
			return f < 0.65f;
		// else fly
		return f > 0.625f && f < 0.75f;
	}
	void SpawnHeadLine()
	{
		if ( IsBeetle() )
		{
			for ( int i = 0;  i < 4;  i++ )
				Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin()
					+ kVec3( 0, 0, self.Height() * 0.65f )
					+ kVec3( 0, self.Radius(), 0 )
					* kQuat( Math::Deg2Rad( 90 - i * 180.0f / 3 ) - self.Yaw(), 0,0,1 ), -1 );
		}
		else // fly
		{
			for ( int i = 0;  i < 6;  i++ )
			{
				Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin()
					+ kVec3( 0, 0, self.Height() * 0.625f)
					+ kVec3( 0, self.Radius(), 0 )
					* kQuat( Math::Deg2Rad( 90 - i * 180.0f / 5 ) - self.Yaw(), 0,0,1 ), -1 );
				Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin()
					+ kVec3( 0, 0, self.Height() * 0.75f )
					+ kVec3( 0, self.Radius(), 0 )
					* kQuat( Math::Deg2Rad( 90 - i * 180.0f / 5 ) - self.Yaw(), 0,0,1 ), -1 );
			}
		}
	}

	void InitHeight()
	{
		if ( IsBeetle() )
			self.Height() += 23 * self.Scale().z;
		else // fly
			self.Height() += 87 * self.Scale().z;
	}
	// modifying dragonfly collision seemed to break hit detection, so leave it alone
	void FixHeight()
	{
		if ( IsBeetle() )
			self.Height() = 20 * self.Scale().z + (self.Height() - InitialHeight);
//		else
//			self.Height() *= 0.5f;
	}
	void FixRadius()
	{
		if ( IsBeetle() )
			self.Radius() *= 0.5f;
//		else
//			self.Radius() *= 0.35;
	}

	kVec3 DazeOffset()
	{
		if ( IsBeetle() )
		{
			if ( self.Flags() & AF_STATIONARY != 0 )
				return kVec3( 0, -42, 10 );
			return kVec3( 0, -22, 10 );
		}
		// else dragonfly
		if ( self.Flags() & AF_STATIONARY != 0 )
			return kVec3( 0, -59, 145 );
		return kVec3( 0, -28, 145 );
	}
	// don't make dragonflies flap faster when dazed (makes no sense, and increases attack rate)
	// beetles also don't have much of an idle anim; speeding it up just makes them look kinda twitchy
	void SetDazeAnim()
	{
		Incapacitate( anim_aiStanding, self.AnimSpeed() );
	}

	bool TranqDealsDamage()
	{
		return true;
	}

	kVec3 TrackPoint()
	{
		if ( !IsBeetle() ) return TurokEnemy::TrackPoint();
		kVec3 v = FlameOffset();
		v.z -= 0.2f * self.Height();
		return self.Origin() + v * self.Rotation();
	}

	// beetles don't have a "running" state, so always consider "agitated," to prevent slow initial pursuit
	bool IsAgitated()
	{
		if ( IsBeetle() )
			return true;
		return TurokEnemy::IsAgitated();
	}

	bool UpdateStun( bool bSuperceded )
	{
		if ( StunTime > 0 && !IsBeetle() )
		{
			self.InflictGenericDamage( self, int( StunTime ) );
			StunTime = 0;
			return false;
		}
		return TurokEnemy::UpdateStun( bSuperceded );
	}

    /*
    ==============================================================
    OnDeath
    ==============================================================
    */

    void OnDeath(kActor @killer, kDictMem @damageDef)
    {
        TurokEnemy::OnDeath(killer, damageDef);
        self.AnimSpeed() = 4.0f;
    }
}
