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

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Rider
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokRider
==============================================================
*/

final class TurokRider : TurokEnemy
{
	bool bRiderless = false;

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

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

	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.8f )
			return false;
		f = (hitLoc.z - self.Origin().z) / self.Height();
		return f > 0.4f && f < 0.6f;
	}
	void SpawnHeadLine()
	{
		// c = a/h
		float angle = Math::ACos( 0.8f );
		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.6f )
				+ 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.4f )
				+ kVec3( 0, self.Radius(), 0 ) * kQuat( angle - i * delta - self.Yaw(), 0,0,1 ), -1 );
		}
	}

	// triceratops give no fucks about knockback
	void TransferMomentum( float momentumTransfer, bool bExplode )
	{
	}

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

	void DazeEffects()
	{
		float y = self.Flags() & AF_STATIONARY != 0 ? -75 : 0;
		// rider
		if ( !bRiderless )
			self.SpawnFx( "fx/T2/Sunfire_Daze.kfx", kVec3( 0, -62 + y, 295 ) );
		// dino
		self.SpawnFx( "fx/T2/Sunfire_Daze.kfx", kVec3( 0, -205 + y, 158 ) );
	}

	void KnockoutAnim( int&out anim, float&out rate, float&out tween )
	{
		if ( !self.AnimState().CheckAnimID( anim_AI_Knockout ) )
		{
			anim = anim_aiDeathStand;
			tween = self.AnimSpeed() * 1.5f;
			float scale = Game.GetGameSpeed();
			if ( scale < 1 && scale > 0 ) rate = 4 * ( (1 + scale) / 2 );
			else                          rate = self.AnimSpeed() * 1.5f;
			return;
		}
		anim = anim_AI_Knockout;
		int i = int( self.AnimState().TrackTime() * self.AnimState().NumFrames() - 0.5f );
		// start slow, and ramp up until start of shifting weight into fall
		if ( !self.AnimState().IsPlaying(anim) ) rate = 2;
		else if ( i <  16 ) rate = 2 - (i -  1.0f) / (16 -  1);
		// start slump slow, and ramp up through the end
		else if ( i >= 42 ) rate = 2 - (i - 42.0f) / (55 - 42);
		// full speed while shifting weight, through fall, and kicking before slumping
		else                rate = 1;
		rate *= self.AnimSpeed();
		// speed up in spirit mode to make sure long anim finishes before sedation runs out
		float scale = Game.GetGameSpeed();
		if ( scale < 1 && scale > 0 ) rate *= (1 + scale) / 2;
		tween = 15;
	}
	bool SnoozeTime()
	{
		return self.AnimState().CycleCompleted();
	}
	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( -75, -200, 15 ) );
	}

	// most enemies recoil on explosive death, even if they don't get blown into the air
	// triceratops just has the one death anim, which doesn't look good,
	//   so block it by not passing explosive damage def
	void CancelIncapacitation( kDictMem@ deathDamDef )
	{
		TurokEnemy::CancelIncapacitation( null );
	}

	// rider death anim callbacks
	void Unused23( kActor@ instigator, const float w, const float x, const float y, const float z )
	{
		if ( !bRiderless ) self.PlaySoundWithLookupID( int(w) );
	}
	void Unused60( kActor@ instigator, const float w, const float x, const float y, const float z )
	{
		if ( !bRiderless ) FootStepPuff( instigator, w, x, y, z );
	}
	void Unused94( kActor@ instigator, const float w, const float x, const float y, const float z )
	{
		if ( bRiderless ) return;
		bRiderless = true;

		if ( self.RenderModel() is null ) return;

		kActor@ a = Spawn( "FallenRider", self.Origin(), self.Yaw(), self.SectorIndex() );
		if ( a is null ) return;
		a.Flags() |= AF_DEAD;
		a.CastToAI().AIFlags() |= AIF_NOTHINK;
		a.Health() = 0; // allows game to remove actor when dormant, instead of reverting to idle
		a.Scale() = self.Scale();
		a.Height() = 60 * a.Scale().z;
		a.Rotation() = self.Rotation();
		// hide triceratops
		for ( int n = 0;  n <= 43;  n++ )
			a.RenderModel().SetVisibility( n, n >= 8 && n <= 27 );
		a.AnimState().Set( self.AnimState().PlayingID(), self.AnimSpeed(), ANF_ROOTMOTION );
		a.AnimState().SetLastFrame();

		// hide rider
		for ( int n = 8;  n <= 27;  n++ )
			self.RenderModel().SetVisibility( n, false );
	}

	/*
	==============================================================
	RiderFireMissiles
	==============================================================
	*/

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

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

	/*
	==============================================================
	RiderFireShots - human gunfire - fires straight ahead, with -z in the kfx
	==============================================================
	*/

	// Smoke39 - skip if riderless, play sound here instead of in anim (so it can be conditioned on rider),
	// and use SpawnProj() instead of self.SpawnFx()
	void RiderFireShots(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		if ( self.GetTarget() is null || bRiderless ) return;
		self.PlaySoundWithLookupID( int( w ) );
		SpawnProj( "fx/generic_201.kfx", kVec3(x,y,z) );
	}

	/*
	==============================================================
	GunFire - unused?
	==============================================================
	*/

	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/generic_8_enemy_human_gunfire.ksnd");

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

	/*
	==============================================================
	OnBeginLevel
	==============================================================
	*/

	void OnBeginLevel(void)
	{
		if(Game.GetCurrentMapID() == 2)
		{
			self.Flags() |= AF_STATIONARY;
			self.Origin().x -= (3*GAME_SCALE);
		}
	}
}
