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

#include "scripts/common.txt"

//-----------------------------------------------------------------------------
//
// Enemy Base Class
//
//-----------------------------------------------------------------------------

/*
==============================================================
TurokGrunt
==============================================================
*/

class TurokGrunt : TurokEnemy
{
	// Smoke39
	bool bBackstabbed = false;
	int8 DartRevenge = 0; // blow gun warrior was killed by poison, or while tranquilized

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

	//==========================================================================
	// Smoke39 - Turok+ Stuff

	void Reset()
	{
		TurokEnemy::Reset();
		bBackstabbed = false;
		DartRevenge = 0;
	}

	void InitHeight()
	{
		self.Height() += 23 * self.Scale().z;
	}
	void FixHeight()
	{
		self.Height() = 115 * self.Scale().z + (self.Height() - InitialHeight);
	}
	void FixDeadHeight()
	{
		self.Height() = 30 * self.Scale().z + (self.Height() - self.StepHeight());
	}
	void FixRadius()
	{
		// weighted average that favors x width over y depth, and whichever is larger
		self.Radius() = 40 * ( self.Scale().x * 2 + self.Scale().y + Math::Max( self.Scale().x, self.Scale().y ) ) / 4;
	}

	bool Headshot( kVec3&in hitLoc )
	{
		return (hitLoc.z - self.Origin().z) / self.Height() > 0.8f;
	}
	void SpawnHeadLine()
	{
		for ( int i = 0;  i < 16;  i++ )
			Game.SpawnFx( "fx/TPlus/Debug_HeadLine.kfx", self.Origin() + kVec3( 0, 0, self.Height() * 0.8f )
				+ kVec3( 0, self.Radius(), 0 ) * kQuat( Math::Deg2Rad( i * 22.5f ), 0,0,1 ), -1 );
	}

	kVec3 DazeOffset()
	{
		kVec3 v;
		if ( self.Flags() & AF_STATIONARY != 0 )
		{
			v.Set( -5, -15, 125 );
			if ( self.AnimState().IsPlaying( anim_aiGruntStandAlt ) )
			{
				v.x -= 3;
				v.y += 1;
			}
		}
		else
		{
			v.Set( -5, -24, 125 );
			if ( self.AnimState().IsPlaying( anim_aiGruntStandAlt ) )
			{
				v.x -= 1;
				v.y += 1;
			}
		}
		return v;
	}

	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();  tween = 15;    }
		else
		{  anim = anim_aiDeathViolent;  rate = self.AnimSpeed();  tween = rate;  }
	}
	kVec3 SnoozeLoc()
	{
		return self.GetTransformedVector( kVec3( 20, -50, 30 ) );
	}

	// proto-pulse rifle
	void DropItem( kActor@ instigator, const float w, const float x, const float y, const float z )
	{
		if ( m_bDroppedItem )
			return;

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

		if ( self.ModelVariation() == GV_COMMANDER_RIFLE4 && Opt::bProtoPulse && !bExternalWeaponPack
			&& m_bMortallyWounded && bBackstabbed && !Player.HasWeapon( TW_WEAPON_PULSERIFLE ) )
		{
			TossItem( "ProtoPulsePickup", org.x, org.y, org.z );
			// hide the held gun
			self.RenderModel().HideSection( 17, 0, true );
		}

		if ( DartRevenge & 1 != 0 )
			TossItem( "Ammo_TranqDart_Pickup", org.x, org.y, org.z );
		if ( DartRevenge & 2 != 0 )
			TossItem( "Ammo_PoisonDart_Pickup", org.x, org.y, org.z );

		TurokEnemy::DropItem( instigator, w, x, y, z );
	}

	// blow gun warriors drop tranqs if killed while unconscious
	void OnDeath( kActor@ killer, kDictMem@ damDef )
	{
		if ( self.SpawnFlags2() & ASF2_PROJECTILEATTACK6 != 0 && !IsBoss() && bSedated )
			DartRevenge |= 1;
		TurokEnemy::OnDeath( killer, damDef );
	}

	// poison darts kill blow gun warriors instantly
	void HandlePoisonDamage( kDictMem@ damDef, bool bPlayer )
	{
		if ( self.SpawnFlags2() & ASF2_PROJECTILEATTACK6 != 0 && !IsBoss() && GetBool( damDef, "bPoison" ) )
		{
			BonusDamage += self.Health() * 2;
			self.RunFxEvent( "PoisonDeath_Instant" );
			DartRevenge |= 2; // let DropItem() know to drop a poison dart pickup
		}
		else
			TurokEnemy::HandlePoisonDamage( damDef, bPlayer );
	}

	/*
	==============================================================
	HighPriestSpellAttack - yellow fireball
	==============================================================
	*/

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

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

	/*
	==============================================================
	HighPriestSpellSound
	==============================================================
	*/

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

	/*
	==============================================================
	SwooshTrail1
	==============================================================
	*/

	void SwooshTrail1(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		switch(self.ModelVariation())
		{
		case GV_GENERIC_AXE:
		case GV_DEMON_AXE:
			self.RenderModel().AddTrailEffect("Trail_Axe", 17);
			break;

		case GV_GENERIC_CLUB:
		case GV_SHAMAN:
			self.RenderModel().AddTrailEffect("Trail_Mace", 17);
			break;

		case GV_COMMANDER_SPEAR:
		case GV_WARRIOR_SPEAR:
		case GV_DEMON_SPEAR:
			self.RenderModel().AddTrailEffect("Trail_Pole1", 17);
			break;

		case GV_COMMANDER_RIFLE1:
		case GV_COMMANDER_RIFLE2:
		case GV_COMMANDER_RIFLE3:
		case GV_COMMANDER_RIFLE4:
		case GV_POACHER_RIFLE:
		case GV_CYBORG_PULSE_RIFLE:
			self.RenderModel().AddTrailEffect("Trail_Shotgun", 17);
			break;

		case GV_POACHER_KNIFE:
			self.RenderModel().AddTrailEffect("Trail_Sword", 17);
			break;

		case GV_POACHER_PISTOL1:
		case GV_POACHER_PISTOL2:
			self.RenderModel().AddTrailEffect("Trail_Pistol", 17);
			break;

		case GV_POACHER_SHOTGUN:
			self.RenderModel().AddTrailEffect("Trail_Shotgun", 17);
			break;

		case GV_WARRIOR_BONE:
			self.RenderModel().AddTrailEffect("Trail_Bone", 17);
			break;

		case GV_WARRIOR_DART:
			self.RenderModel().AddTrailEffect("Trail_DartGun", 17);
			break;
		}
	}

	/*
	==============================================================
	GunFire
	==============================================================
	*/

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

		// Smoke39 - new proj for cyborg sergeants
		if( self.ModelVariation() == GV_COMMANDER_RIFLE4 )
		{
			self.PlaySound( "sounds/shaders/machine_gun_shot_2.ksnd" );
			SpawnProj( "fx/enemy_longhunter.kfx", kVec3(x,y,z) );
			return;
		}
		if( self.ModelVariation() == GV_CYBORG_PULSE_RIFLE )
		{
			self.PlaySound( "sounds/shaders/machine_gun_shot_2.ksnd" );
			SpawnProj( "fx/TPlus/Enemy_Pulse_Blue.kfx", kVec3(x,y,z) );
			return;
		}

		self.PlaySound("sounds/shaders/generic_8_enemy_human_gunfire.ksnd");
		self.SpawnProjectile("fx/enemy_bullet.kfx", kVec3(x, y, z),
			self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
	}

	/*
	==============================================================
	ThrowGrenade
	==============================================================
	*/

	void ThrowGrenade(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.SpawnFx("fx/generic_114.kfx", kVec3(x, y, z));
	}

	/*
	==============================================================
	DemonAttack
	==============================================================
	*/

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

		// Smoke39
//		self.SpawnProjectile("fx/generic_256.kfx", kVec3(x, y, z),
//			self.GetTarget().Origin(), Math::Deg2Rad(45.0f));
		kStr str = "fx/generic_256";
		switch ( Game.GetDifficulty() )
		{
			case DIFFICULTY_HARD: if ( !Opt::bEnemySpeedup ) str += "_hard";     break;
			case DIFFICULTY_HARDCORE:                        str += "_hardcore";
		}
		if ( Opt::bEnemySpeedup ) str += "_fast";
		self.SpawnProjectile( str + ".kfx", kVec3( x, y, z ),
			self.GetTarget().Origin(), Math::Deg2Rad( 45.0f ) );
	}

	/*
	==============================================================
	TribalDart1 - dart
	==============================================================
	*/

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

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

	/*
	==============================================================
	TribalDart2 - puff
	==============================================================
	*/

	void TribalDart2(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		self.SpawnFx("fx/generic_194.kfx", kVec3(x, y, z));
	}

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

	void DeathSound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
			self.ModelVariation() <= GV_CYBORG_SPEAR)
		{
			self.PlaySound("sounds/shaders/ancient_warrior_death.ksnd");
			return;
		}
		else if(self.ModelVariation() >= GV_DEMON_AXE &&
				self.ModelVariation() <= GV_DEMON_LORD)
		{
			switch(Math::RandMax(3))
			{
			case 0:
				self.PlaySound("sounds/shaders/demon_death_scream_1.ksnd");
				break;
			case 1:
				self.PlaySound("sounds/shaders/demon_death_scream_2.ksnd");
				break;
			case 2:
				self.PlaySound("sounds/shaders/demon_death_scream_3.ksnd");
				break;
			}
			return;
		}

		switch(Math::RandMax(3))
		{
		case 0:
			self.PlaySound("sounds/shaders/human_death_scream_1.ksnd");
			break;
		case 1:
			self.PlaySound("sounds/shaders/human_death_scream_2.ksnd");
			break;
		case 2:
			self.PlaySound("sounds/shaders/human_death_scream_3.ksnd");
			break;
		}
	}

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

	void InjurySound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
			self.ModelVariation() <= GV_CYBORG_SPEAR)
		{
			self.PlaySound("sounds/shaders/ancient_warrior_death.ksnd");
			return;
		}
		else if(self.ModelVariation() >= GV_DEMON_AXE &&
				self.ModelVariation() <= GV_DEMON_LORD)
		{
			switch(Math::RandMax(3))
			{
			case 0:
				self.PlaySound("sounds/shaders/demon_effort_injury_grunt_1.ksnd");
				break;
			case 1:
				self.PlaySound("sounds/shaders/demon_effort_injury_grunt_2.ksnd");
				break;
			case 2:
				self.PlaySound("sounds/shaders/demon_effort_injury_grunt_3.ksnd");
				break;
			}
			return;
		}

		switch(Math::RandMax(3))
		{
		case 0:
			self.PlaySound("sounds/shaders/human_effort_injury_grunt_1.ksnd");
			break;
		case 1:
			self.PlaySound("sounds/shaders/human_effort_injury_grunt_2.ksnd");
			break;
		case 2:
			self.PlaySound("sounds/shaders/human_effort_injury_grunt_3.ksnd");
			break;
		}
	}

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

	void ViolentSound(kActor @instigator, const float w, const float x, const float y, const float z)
	{
		if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
			self.ModelVariation() <= GV_CYBORG_SPEAR)
		{
			self.PlaySound("sounds/shaders/ancient_warrior_death.ksnd");
			return;
		}
		else if(self.ModelVariation() >= GV_DEMON_AXE &&
				self.ModelVariation() <= GV_DEMON_LORD)
		{
			switch(Math::RandMax(3))
			{
			case 0:
				self.PlaySound("sounds/shaders/demon_violent_death_1.ksnd");
				break;
			case 1:
				self.PlaySound("sounds/shaders/demon_violent_death_2.ksnd");
				break;
			case 2:
				self.PlaySound("sounds/shaders/demon_violent_death_3.ksnd");
				break;
			}
			return;
		}

		switch(Math::RandMax(3))
		{
		case 0:
			self.PlaySound("sounds/shaders/human_violent_death_1.ksnd");
			break;
		case 1:
			self.PlaySound("sounds/shaders/human_violent_death_2.ksnd");
			break;
		case 2:
			self.PlaySound("sounds/shaders/human_violent_death_3.ksnd");
			break;
		}
	}

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

	void OnBeginLevel(void)
	{
		kRenderModel @model = self.RenderModel();

		if(model is null)
		{
			return;
		}

		if( self.ModelVariation() >= GV_CYBORG_PULSE_RIFLE &&
			self.ModelVariation() <= GV_CYBORG_SPEAR)
		{
			self.ImpactType() = IT_METAL;
		}

		// TODO: this could really benefit from a skin definition or something...

		switch(self.ModelVariation())
		{
		// common soldier
		case GV_GENERIC:
		case GV_GENERIC_AXE:
		case GV_GENERIC_CLUB:
		// Smoke39 - randomize face for hanging bodies, too
		case GV_GENERIC_HANGING:
			// face
			model.SetTexture(19, Math::SysRand() % 12);
			break;

		// commander
		case GV_COMMANDER_SPEAR:
		case GV_COMMANDER_RIFLE1:
		case GV_COMMANDER_RIFLE2:
		case GV_COMMANDER_RIFLE3:
		case GV_COMMANDER_RIFLE4:
			// chest
			model.SetTexture(0, 1);
			model.SetTexture(9, 1);

			// arms
			model.SetTexture(10, 1);
			model.SetTexture(11, 1);
			model.SetTexture(12, 1);
			model.SetTexture(13, 1);
			model.SetTexture(14, 1);
			model.SetTexture(15, 1);
			model.SetTexture(16, 1);
			model.SetTexture(17, 1);

			// face
			model.SetTexture(19, Math::SysRand() % 12);
			break;

		// poacher
		case GV_POACHER_KNIFE:
		case GV_POACHER_PISTOL1:
		case GV_POACHER_PISTOL2:
		case GV_POACHER_RIFLE:
		case GV_POACHER_SHOTGUN:
			// chest
			model.SetTexture(0, 5);
			model.SetTexture(9, 5);

			// legs
			model.SetTexture(1, 2);
			model.SetTexture(3, 2);
			model.SetTexture(4, 2);
			model.SetTexture(5, 2);
			model.SetTexture(7, 2);
			model.SetTexture(8, 2);

			// shin
			model.SetTexture(2, 2);
			model.SetTexture(6, 2);

			// face
			model.SetTexture(19, Math::SysRand() % 12);
			break;

		// warrior
		case GV_WARRIOR_BONE:
		case GV_WARRIOR_DART:
		case GV_WARRIOR_SPEAR:
			// hands
			model.SetTexture(17, 2);

			// chest
			model.SetTexture(0, 2);
			model.SetTexture(9, 2);

			// arms
			model.SetTexture(10, 2);
			model.SetTexture(11, 2);
			model.SetTexture(12, 2);
			model.SetTexture(13, 2);
			model.SetTexture(14, 2);
			model.SetTexture(15, 2);
			model.SetTexture(16, 2);

			// neck
			model.SetTexture(18, 1);
			break;

		// shaman
		case GV_SHAMAN:
			// chest
			model.SetTexture(0, 3);
			model.SetTexture(9, 3);

			// neck
			model.SetTexture(18, 2);

			// legs
			model.SetTexture(1, 1);
			model.SetTexture(3, 1);
			model.SetTexture(4, 1);
			model.SetTexture(5, 1);
			model.SetTexture(7, 1);
			model.SetTexture(8, 1);

			// shin
			model.SetTexture(2, 1);
			model.SetTexture(6, 1);

			// hands
			model.SetTexture(17, 3);

			// arms
			model.SetTexture(10, 3);
			model.SetTexture(11, 3);
			model.SetTexture(12, 3);
			model.SetTexture(13, 3);
			model.SetTexture(14, 3);
			model.SetTexture(15, 3);
			model.SetTexture(16, 3);
			break;

		// cyborg guard
		case GV_CYBORG_PULSE_RIFLE:
			// legs
			model.SetTexture(1, 1);
			model.SetTexture(3, 1);
			model.SetTexture(4, 1);
			model.SetTexture(5, 1);
			model.SetTexture(7, 1);
			model.SetTexture(8, 1);

			// shin
			model.SetTexture(2, 1);
			model.SetTexture(6, 1);

			// chest
			model.SetTexture(0, 4);
			model.SetTexture(9, 4);

			// arms
			model.SetTexture(10, 4);
			model.SetTexture(11, 4);
			model.SetTexture(12, 4);
			model.SetTexture(13, 4);
			model.SetTexture(14, 4);
			model.SetTexture(15, 4);
			model.SetTexture(16, 4);

			// neck
			model.SetTexture(18, 3);
			break;

		// Smoke39 - restore yellow texture variant
		case GV_CYBORG_KNIFE: SetCyborgTextures( 1 ); break; // yellow
		case GV_CYBORG_SPEAR: SetCyborgTextures( 0 ); break; // red

		// demon
		case GV_DEMON_AXE:
		case GV_DEMON_SPEAR:
		case GV_DEMON_HANDS:
			// hands
			model.SetTexture(17, 6);
			model.SetTexture(13, 6);

			// legs
			model.SetTexture(1, 2);
			model.SetTexture(3, 2);
			model.SetTexture(4, 2);
			model.SetTexture(5, 2);
			model.SetTexture(7, 2);
			model.SetTexture(8, 2);

			// shin
			model.SetTexture(2, 2);
			model.SetTexture(6, 2);

			// chest
			model.SetTexture(0, 6);
			model.SetTexture(9, 6);

			// arms
			model.SetTexture(10, 6);
			model.SetTexture(11, 6);
			model.SetTexture(12, 6);
			model.SetTexture(13, 6);
			model.SetTexture(14, 6);
			model.SetTexture(15, 6);
			model.SetTexture(16, 6);

			// neck
			model.SetTexture(18, 4);
			break;

		// demon lord
		case GV_DEMON_LORD:
			// hands
			model.SetTexture(17, 7);
			model.SetTexture(13, 7);

			// legs
			model.SetTexture(1, 3);
			model.SetTexture(3, 3);
			model.SetTexture(4, 3);
			model.SetTexture(5, 3);
			model.SetTexture(7, 3);
			model.SetTexture(8, 3);

			// shin
			model.SetTexture(2, 3);
			model.SetTexture(6, 3);

			// chest
			model.SetTexture(0, 7);
			model.SetTexture(9, 7);

			// arms
			model.SetTexture(10, 7);
			model.SetTexture(11, 7);
			model.SetTexture(12, 7);
			model.SetTexture(13, 7);
			model.SetTexture(14, 7);
			model.SetTexture(15, 7);
			model.SetTexture(16, 7);

			// neck
			model.SetTexture(18, 5);
			break;
		}
	}

	// Smoke39
	void SetCyborgTextures( int t )
	{
		kRenderModel @model = self.RenderModel();
		model.SetTexture( 19, 0 ); // make sure visor animates
		switch ( self.SpawnParams( SP_PARAMS1 ) )
		{
			case 25: t = 0; break; // red
			case 26: t = 1; break; // yellow
			// else keep default for this model variation
		}
		model.SetTexture(  9, t ); // chest
		model.SetTexture(  0, t ); // hips
		model.SetTexture( 12, t ); // left forearm
		model.SetTexture( 16, t ); // right forearm
		model.SetTexture(  2, t ); // left calf
		model.SetTexture(  6, t ); // right calf
		// these don't have any color, but include them in case of custom textures
		model.SetTexture(  3, t ); // left foot
		model.SetTexture(  7, t ); // right foot
	}
};
