//
// 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:
//      Purlin Script Object Classes
//

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Purlin
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokPurlin
==============================================================
*/

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

	// Smoke39
	// regular scale: 0.549451
	// hugenormous: 0.824176
	bool Giant()
	{
		return self.Scale().z >= 0.6f;
	}

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

	bool Headshot( kVec3&in hitLoc )
	{
		// can't hit head from behind
		if ( Math::Fabs( self.GetTurnYaw(hitLoc) ) > Math::Deg2Rad( 90 ) )
			return false;
		// considerably lower height threshold for hunched over mobile pur-lin
		if ( self.Flags() & AF_STATIONARY == 0 )
			return (hitLoc.z - self.Origin().z) / self.Height() > 0.6f;
		// somewhat lower threshold than humans
		return (hitLoc.z - self.Origin().z) / self.Height() > 0.7f;
	}
	void SpawnHeadLine()
	{
		float height = self.Height();
		if ( self.Flags() & AF_STATIONARY == 0 ) height *= 0.6f;
		else                                     height *= 0.7f;
		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( Math::Deg2Rad( 90 - i * 180.0f / 7 ) - self.Yaw(), 0,0,1 ), -1 );
	}

	kVec3 DazeOffset()
	{
		if ( self.Flags() & AF_STATIONARY != 0 )
			return kVec3( -38, -11, 152 );
		return kVec3( -39, -72, 152 );
	}

	bool SnoozeTime()
	{
		return self.AnimState().TrackTime() > 0.85f;
	}
	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( -75, -95, 65 ) );
	}

	// Smoke39 - less knockback for regular size, none for hugenormous (I think they're always stationary in the base game, but just in case for custom maps)
	void TransferMomentum( float momentumTransfer, bool bExplode )
	{
		if ( !Giant() )
			TurokEnemy::TransferMomentum( 0.5f * momentumTransfer, bExplode );
	}

	/*
	==============================================================
	PurlinStomp
	==============================================================
	*/

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

	/*
	==============================================================
	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/explosion_1.ksnd");

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

	/*
	==============================================================
	OnActivate
	==============================================================
	*/

	void OnActivate(void)
	{
		self.PlaySound("sounds/shaders/hulk_alert.ksnd");
	}

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

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

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

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

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

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

	/*
	==============================================================
	Shockwave
	==============================================================
	*/

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

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

	/*
	==============================================================
	ShockwaveDamage
	==============================================================
	*/

	void ShockwaveDamage(kActor @instigator, const float r, const float x, const float y, const float z)
	{
		kActor @targ;
		kVec3 pos;

		if(self.GetTarget() is null)
		{
			return;
		}

		@targ = self.GetTarget();

		if(!targ.OnGround())
		{
			return;
		}

		pos = self.GetTransformedVector(kVec3(x, y, z));

		if(Math::Fabs(targ.Origin().z - pos.z) > 30.72f)
		{
			return;
		}

		float range = targ.DistanceToPoint(pos);
		float radius = r * 10.24f * r * 10.24f;

		if(range >= radius)
		{
			return;
		}

		radius = (radius - range) / radius;
		targ.InflictGenericDamage(instigator, int(10.0f * radius));
	}
};
