/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\Mobiles\Special\Barracoon.cs
Lines of code: 304
***********************************************************/


using System;
using Server;
using Server.Items;
using System.Collections;
using Server.Spells;
using Server.Spells.Seventh;
using Server.Spells.Fifth;
using Server.Spells.Necromancy;
using Server.Spells.Ninjitsu;
using Server.Spells.Spellweaving;
using Server.Engines.CannedEvil;

namespace Server.Mobiles
{
	public class Barracoon : BaseChampion
	{
		public override ChampionSkullType SkullType{ get{ return ChampionSkullType.Greed; } }
		private DateTime lastability;

		[Constructable]
		public Barracoon() : base( AIType.AI_Super )
		{
			Name = "Barracoon";
			Title = "the piper";
			Body = 0x190;
			Hue = 0x83EC;

			SetStr( 305, 425 );
			SetDex( 72, 150 );
			SetInt( 505, 750 );

			SetHits( 4200 );
			SetStam( 102, 300 );
			SetMana( 505, 750 );

			SetDamage( 25, 35 );

			SetDamageType( ResistanceType.Physical, 100 );

			SetResistance( ResistanceType.Physical, 60, 70 );
			SetResistance( ResistanceType.Fire, 50, 60 );
			SetResistance( ResistanceType.Cold, 50, 60 );
			SetResistance( ResistanceType.Poison, 40, 50 );
			SetResistance( ResistanceType.Energy, 40, 50 );

			SetSkill( SkillName.MagicResist, 100.0 );
			SetSkill( SkillName.Tactics, 97.6, 100.0 );
			SetSkill( SkillName.Wrestling, 97.6, 100.0 );
			SetSkill( SkillName.Magery, 50.0, 55.0 );
			//SetSkill( SkillName.EvalInt, 10.6, 20.0 );
			SetSkill( SkillName.Meditation, 97.6, 100.0 );

			Fame = 22500;
			Karma = -22500;

			VirtualArmor = 70;

			AddItem( new FancyShirt( Utility.RandomGreenHue() ) );
			AddItem( new LongPants( Utility.RandomYellowHue() ) );
			AddItem( new JesterHat( Utility.RandomPinkHue() ) );
			AddItem( new Cloak( Utility.RandomPinkHue() ) );
			AddItem( new Sandals() );

			HairItemID = 0x203B; // Short Hair
			HairHue = 0x94;
			if(0.5>Utility.RandomDouble())	PackItem(new RandomTalisman());
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.UltraRich, 3 );
			
			if(0.05>Utility.RandomDouble())
			{
				switch(Utility.Random(2))
				{
					case 0:PackItem(new BarracoonsJesterHat());break;
					case 1:PackItem(new SashOfTheVerminHorde());break;			
				}
			}			
		}

		public override bool AlwaysMurderer{ get{ return true; } }
		public override bool AutoDispel{ get{ return true; } }
		public override double AutoDispelChance{ get{ return 0.8; } }
		public override bool BardImmune{ get{ return !Core.SE; } }
		public override bool Unprovokable{ get{ return Core.SE; } }
		public override bool Uncalmable{ get{ return Core.SE; } }
		public override Poison PoisonImmune{ get{ return Poison.Deadly; } }

		public override bool ShowFameTitle{ get{ return false; } }
		public override bool ClickTitle{ get{ return false; } }

		public void Polymorph( Mobile m )
		{
			if ( m.Name == "Barracoon" )
			{
				SpawnRatmen( this ); //double spawn for more ratmen
				SpawnRatmen( this );
				m.Name = NameList.RandomName( "ratman" );
				m.Title = "";
				
				foreach ( Mobile n in this.GetMobilesInRange( 100 ) )
				{
					if ( n is BaseCreature && (((BaseCreature)n).Controlled || ((BaseCreature)n).Summoned || ((BaseCreature)n).Team != this.Team) )
						n.Combatant = null;
					else if ( n.Player )
						n.Combatant = null;
				}
			
			}
			
			if (Spells.Necromancy.TransformationSpell.UnderTransformation( m )) //Removes necromancer form
			{
				TransformationSpell.RemoveContext( m, true );
			}
			
			if (Spells.Ninjitsu.AnimalForm.UnderTransformation( m )) //Removes animal form
			{
				AnimalForm.RemoveContext( m, true );
			}
			
			if (Spells.Spellweaving.ArcaneForm.UnderTransformation( m )) //Removes weaver forms
			{
				ArcaneForm.RemoveContext( m, true );
			}
			
			if ( !m.CanBeginAction( typeof( PolymorphSpell ) ) || !m.CanBeginAction( typeof( IncognitoSpell ) ) || m.IsBodyMod )
				return;

			IMount mount = m.Mount;

			if ( mount != null )
				mount.Rider = null;

			if ( m.Mounted )
				return;

			if ( m.BeginAction( typeof( PolymorphSpell ) ) )
			{
			
				Item disarm = m.FindItemOnLayer( Layer.OneHanded );

				if ( disarm != null && disarm.Movable )
					m.AddToBackpack( disarm );

				disarm = m.FindItemOnLayer( Layer.TwoHanded );

				if ( disarm != null && disarm.Movable )
					m.AddToBackpack( disarm );

				m.BodyMod = 42;
				m.HueMod = 0;

				new ExpirePolymorphTimer( m ).Start();
			}
		}

		private class ExpirePolymorphTimer : Timer
		{
			private Mobile m_Owner;

			public ExpirePolymorphTimer( Mobile owner ) : base( TimeSpan.FromMinutes( 1.0 ) )
			{
				m_Owner = owner;

				Priority = TimerPriority.OneSecond;
			}

			protected override void OnTick()
			{
				if ( !m_Owner.CanBeginAction( typeof( PolymorphSpell ) ) )
				{
					m_Owner.BodyMod = 0;
					m_Owner.HueMod = -1;
					m_Owner.EndAction( typeof( PolymorphSpell ) );
				}
			}
		}

		public void SpawnRatmen( Mobile target )
		{
			Map map = this.Map;

			if ( map == null )
				return;

			int rats = 0;

			foreach ( Mobile m in this.GetMobilesInRange( 10 ) )
			{
				if ( m is Ratman || m is RatmanArcher || m is RatmanMage )
					++rats;
			}

			if ( rats < 16 )
			{
				PlaySound( 0x3D );

				int newRats = Utility.RandomMinMax( 3, 5 );

				for ( int i = 0; i < newRats; ++i )
				{
					BaseCreature rat;

					switch ( Utility.Random( 5 ) )
					{
						default:
						case 0: case 1:	rat = new Ratman(); break;
						case 2: case 3:	rat = new RatmanArcher(); break;
						case 4:			rat = new RatmanMage(); break;
					}

					rat.Team = this.Team;
					rat.Fame=0;

					bool validLocation = false;
					Point3D loc = this.Location;
					
					int x, y, z; 
					
					if ( 0.7 >= Utility.RandomDouble() )
					{
						for ( int j = 0; !validLocation && j < 10; ++j )
						{
							x = X + Utility.Random( 3 ) - 1;
							y = Y + Utility.Random( 3 ) - 1;
							z = map.GetAverageZ( x, y );

							if ( validLocation = map.CanFit( x, y, this.Z, 16, false, false ) )
								loc = new Point3D( x, y, Z );
							else if ( validLocation = map.CanFit( x, y, z, 16, false, false ) )
								loc = new Point3D( x, y, z );
						}
					}
					else if (this.InLOS(target))
					{
						for ( int k = 0; !validLocation && k < 10; ++k )
						{
							x = target.X;
							y = target.Y;
						    z = target.Z;

							if ( validLocation = map.CanFit( x, y, this.Z, 16, false, false ) )
								loc = new Point3D( x, y, Z );
							else if ( validLocation = map.CanFit( x, y, z, 16, false, false ) )
								loc = new Point3D( x, y, z );
						}
					}

					rat.MoveToWorld( loc, map );
					
					if (target.Name == "Barracoon")
					{
						//no rat combattant to prevent barracoon from attacking himself
					}
					else			
					{			
						rat.Combatant = target;
					}
				}
			}
		}

		public void DoSpecialAbility( Mobile target )
		{
			if ( target == null || target.Deleted ) //sanity
				return;
				
			if ( 0.2 >= Utility.RandomDouble() ) //  polymorph attacker into a ratman
				Polymorph( target );

			if ( 0.05 >= Utility.RandomDouble() ) // 20% chance to more ratmen
				SpawnRatmen( target );
		}
		
		public override void OnThink()
		{
			if (this.Combatant != null && 0.02 >= Utility.RandomDouble() && this.Hits >= 1000 && (lastability+TimeSpan.FromSeconds(100))<DateTime.Now) //Random Polymorph
			{
				Mobile target = this.Combatant; 
				ArrayList mob = new ArrayList();
		
				foreach ( Mobile m in this.GetMobilesInRange( 8 ) )
				{
					if ( m == this || !CanBeHarmful( m ) || !this.InLOS( m ))
						continue;
					if ( m is BaseCreature && (((BaseCreature)m).Controlled || ((BaseCreature)m).Summoned || ((BaseCreature)m).Team != this.Team) )
						mob.Add( m );
					else if ( m.Player )
						mob.Add( m );
				}
			
				if(mob.Count<=0)return;
			
				Mobile n = mob[Utility.Random(mob.Count)] as Mobile;
				
				if (n != null)
				{		
					n.PlaySound( 0x3D );
					Polymorph(n);
					lastability=DateTime.Now;		
				}
			}	
			if ( this.Hits < 1000 && !IsBodyMod ) // Baracoon is low on life, polymorph into a ratman
					Polymorph( this );
			else if ( this.Body == 0x190 )
			{
				this.Name = "Barracoon";
				this.Title = "the piper";
			}
		}

		public override void OnGotMeleeAttack( Mobile attacker )
		{
			base.OnGotMeleeAttack( attacker );
			
			if (0.008>= Utility.RandomDouble()  && this.Hits > 800 &&  !attacker.InRange( this, 1 ) )
			{
				this.Location = attacker.Location;
				this.FixedParticles( 0x376A, 9, 32, 0x13AF, EffectLayer.Waist );
				attacker.PlaySound( 0x1FE );
			}
			else
			{
			DoSpecialAbility( attacker );
			}
		}

		public override void OnGaveMeleeAttack( Mobile defender )
		{
			base.OnGaveMeleeAttack( defender );

			DoSpecialAbility( defender );
		}
		
		public override void OnDamagedBySpell( Mobile caster )
		{
			base.OnGotMeleeAttack( caster );
		
			if ( 0.03> Utility.RandomDouble() && !caster.InRange( this, 1 )) 
				DoSpecialAbility( caster );
				
			if (0.01 >= Utility.RandomDouble() && this.Hits > 800 && !caster.InRange( this, 1 ) )
			{
				this.Location = caster.Location;
				this.FixedParticles( 0x376A, 9, 32, 0x13AF, EffectLayer.Waist );
				caster.PlaySound( 0x1FE );
			}
		}
		
		public override bool OnBeforeDeath()
		{
			Name = "Barracoon";
			Title = "the piper";
			this.BodyMod = 0;
			this.HueMod = -1;
			this.EndAction( typeof( PolymorphSpell ) );
			
			return base.OnBeforeDeath();
		}	

		public Barracoon( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
		}

	}
}