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

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Sandworm
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokSandWorm
==============================================================
*/

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

	// Smoke39
	void InitHeight()
	{
		self.Height() += 23 * self.Scale().z;
	}
	void FixHeight()
	{
		self.Height() = 165 * self.Scale().z + (self.Height() - InitialHeight);
	}
	// can't really change radius, since worms lean so far forward in their volume
	// can't shift worm back, because then they slide around as they turn, and dust effects spawn forward of burrow point

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

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

	kVec3 TrackPoint()
	{
		kVec3 v = FlameOffset();
		v.y += 0.25f * self.Radius();
		return self.Origin() + v * self.Rotation();
	}

	// Smoke39 - no knockback; it's rooted to the ground
	void TransferMomentum( float momentumTransfer, bool bExplode )
	{
	}

	bool CanBeDazed()
	{
		return false;
	}

	bool TranqDealsDamage()
	{
		return true;
	}

	/*
	==============================================================
	SpitAcid
	==============================================================
	*/

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

		self.SpawnProjectile("fx/acidspit.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/subterranean_death.ksnd");
	}

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

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

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

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

    /*
    ==============================================================
    Unused23
    ==============================================================
    */

    void Unused23(kActor @instigator, const float w, const float x, const float y, const float z)
    {
        self.Flags() |= AF_NONSHOOTABLE;
    }
}
