/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\events and scenarios\Thanksgiving 2008\MadAlchemist.cs
Lines of code: 331
***********************************************************/


using System;
using Server;
using Server.Items;
using System.Collections;
using Server.Misc;
using Server.Network;
using Server.Targeting;
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;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Multis;

namespace Server.Mobiles
{
	public class MadAlchemist : BaseChampion
	{
		public override bool CantbeSupreme { get { return true;} }
		public override ChampionSkullType SkullType{ get{ return ChampionSkullType.Greed; } }
		private DateTime lastability;
		//private DateTime lastBigSpawn;
		
		 //arty stuff
		private static Type[] artys = new Type[]
		{
		typeof( ThanksgivingBag ),typeof( ThanksgivingBag )
		};

		[Constructable]
		public MadAlchemist() : base( AIType.AI_Super )
		{
			Name = "Demitry";
			Title = "the mad alchemist";
			Body = 0x190;
			Hue = 0x83EC;

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

			SetHits( 20000 );
			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.Meditation, 97.6, 100.0 );

			Fame = 22500;
			Karma = -22500;

			VirtualArmor = 70;

			AddItem( new HoodedShroudOfShadows( 1864 ) );
			AddItem( new Cloak( 1864 ) );
			AddItem( new Sandals( 1864 ) );

			HairItemID = 0x203B; // Short Hair
			HairHue = 1864;
			FacialHairItemID = 8255; // Facial Short Hair
			FacialHairHue = 1864;
			PackItem(new RandomTalisman());
			PackItem(new RandomTalisman());
			PackItem(new RandomTalisman());
			PackItem(new RandomTalisman());
			PackItem(new RandomTalisman());
			PackItem(new RandomTalisman());
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.SuperBoss, 10 );		
		}

		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 == "Demitry" )
			{
				SpawnRatmen( this ); //double spawn for more ratmen
				SpawnRatmen( this );
				Name = "A Mad Turkey";
				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 = 30;
				m.HueMod = Utility.RandomMinMax(1864, 1872) ;

				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;
					
					rat = new LesserMadTurkey();
					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 == "Demitry")
					{
						//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.1 >= Utility.RandomDouble() ) //  polymorph attacker into a turkey
				Polymorph( target );

			if ( 0.2 >= Utility.RandomDouble() ) // 20% chance to more turkeys
				SpawnRatmen( target );
		}
		
		public override void OnThink()
		{
		
			/*if ( this.Hits >= 10000 && (lastBigSpawn+TimeSpan.FromMinutes(2))<DateTime.Now && this.Combatant != null) 
			{
				BaseCreature rat;
					
				rat = new SuperMadTurkey();
				rat.Team = this.Team;
				rat.MoveToWorld( this.Location, this.Map );	
				
				lastBigSpawn=DateTime.Now;
			}*/
			
			if (this.Combatant != null && 0.02 >= Utility.RandomDouble() && this.Hits >= 10000 && (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 < 10000 && !IsBodyMod ) {// Demitry is low on life, polymorph into a harpy
					Polymorph( this );
			}
			else if ( this.Body == 0x190 )
			{
				this.Name = "Demitry";
				this.Title = "the mad alchemist";
			}
		}

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

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

			DoSpecialAbility( defender );
		}
		
		public override void OnDamagedBySpell( Mobile caster )
		{
			base.OnGotMeleeAttack( caster );
		
			if ( 0.05> Utility.RandomDouble() && !caster.InRange( this, 1 )) 
				DoSpecialAbility( caster );
				
			if (0.03 >= Utility.RandomDouble() && this.Hits > 1000 && !caster.InRange( this, 1 ) )
			{
				this.Location = caster.Location;
				this.FixedParticles( 0x376A, 9, 32, 0x13AF, EffectLayer.Waist );
				caster.PlaySound( 0x1FE );
			}
		}
		
		public override bool OnBeforeDeath()
		{
			Name = "Demitry";
			Title = "the mad alchemist";
			this.BodyMod = 0;
			this.HueMod = -1;
			this.EndAction( typeof( PolymorphSpell ) );
			
			GiveArtifact(artys);
			return base.OnBeforeDeath();
		}	
		
		//main arty stuff ---------------------------------------
		public void GiveArtifact(Type[] artylist)
		{
			if(this.Region!=null && !this.Region.CanSpawnArtifacts())
				return;
			
			ArrayList toGive = new ArrayList();
			List<DamageStore> rights = BaseCreature.GetLootingRights( this.DamageEntries, this.HitsMax );

			for ( int i = rights.Count - 1; i >= 0; --i )
			{
				DamageStore ds = rights[i];
				if ( ds.m_HasRight )	toGive.Add( ds.m_Mobile );
			}
			if ( rights.Count == 0 )return;			
			
			for(int i=0;i<rights.Count;i++)
			{
			Mobile m = rights[i].m_Mobile;			
			PlayerMobile pm = m as PlayerMobile;
			
			//int luck=m.Luck;
			//if(luck>1500)luck=1500;
			
			//double chance=0.01+((double)luck/10000); //1% with no luck, 16% with 1500 luck
			//if(chance>0.3)chance=0.3;	

			//if(pm.showartychance)pm.SendMessage(52,"Demitel artifact chance: {0}%" ,Math.Round(chance*100,3)); 
						
				//if(chance>Utility.RandomDouble()) 
				//{
				Item artifact =  Loot.Construct( artylist[Utility.Random(artylist.Length)] );					
				m.SendMessage(200,"As reward for defeating the mad alchemist, a special gift has been bestoyed to you.");
				Container pack = m.Backpack;
				if ( pack == null || !pack.TryDropItem( m, artifact, false ) )
				m.BankBox.DropItem( artifact );			
				//}
			}
			
		}//end main arty stuff------------------------------------------

		public MadAlchemist( 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();
		}

	}
}