/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\mobiles\ml\Changeling.cs
Lines of code: 319
***********************************************************/



using System;
using Server;
using Server.Misc;
using Server.Items;
using System.Collections;

namespace Server.Mobiles
{
	[CorpseName( "a changeling corpse" )]
	public class Changeling : BaseCreature
	{
		public override InhumanSpeech SpeechType{ get{ return InhumanSpeech.Wisp; } }
		public override bool ShowFameTitle{ get{ return false; } }
		
		public virtual string OriginalName {get{ return "a changeling";}}
		public virtual int OriginalHue {get{return 0;}}		
		public override bool InitialInnocent{ get{ return true; } }
		public override double SwitchWeaponChance{ get{ return 0.2; } }		
		public override double TargetTamerChance { get { return 0.05; }}		
		public override int SuperAI_DelayMax{ get{ return 15; } }
		
		public override int GetMaxResistance( ResistanceType type )
		{	
		return 70;
		}
		
		
	    private DateTime lastpolymorph;
		private static TimeSpan polymorphtime=TimeSpan.FromMinutes(1);
		private Mobile lastperson;
		
		[Constructable]
		public Changeling() : base( AIType.AI_Super, FightMode.Closest, 10, 1, 0.1, 0.25 )
		{
			Name = "a Changeling";
			Body = 264;

			SetStr( 36, 105 );
			SetDex( 212, 262 );
			SetInt( 317, 399 );

			SetStam( 212, 262 ); 
			SetMana( 317, 399 );
			SetHits( 201, 211 );

			SetDamage( 18, 29 );				

			SetResistance( ResistanceType.Physical, 81, 90 );
			SetResistance( ResistanceType.Fire, 40, 50 );
			SetResistance( ResistanceType.Cold, 40, 49 );
			SetResistance( ResistanceType.Poison, 41, 50 );
			SetResistance( ResistanceType.Energy, 43, 50 );

			Fame = 7500;
			Karma = -7500; 

			VirtualArmor = 55;

			if(0.1<Utility.RandomDouble())PackItem( new DarkSource() );
			
			ResetSkills(true);
			
			//need instrument for barding skills, here we randomize one so its not always same sound
			switch(Utility.Random(7))
			{
			case 0: PackItem(new BambooFlute());break;
			case 1: PackItem(new Drums());break;
			case 2: PackItem(new Harp());break;
			case 3: PackItem(new LapHarp());break;
			case 4: PackItem(new Lute());break;
			case 5: PackItem(new Tambourine());break;
			case 6: PackItem(new TambourineTassel());break;			
			}
		}
		
		public override int GetAngerSound()
		{
			return 1100;
		}

		public override int GetIdleSound()
		{
			return 1101;
		}

		public override int GetAttackSound()
		{
			return 1103;
		}

		public override int GetHurtSound()
		{
			return 1102;
		}

		public override int GetDeathSound()
		{
			return 1104;
		}		
		
		public override bool OnBeforeDeath()
 		{
			ReversePolymorh();		
		
			return base.OnBeforeDeath();
	 	}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.FilthyRich,1 );
			AddLoot( LootPack.Average );
		}
		
		private void ClearLayer(Layer layer)
		{
		Item tmp =  this.FindItemOnLayer(layer); 
		if(tmp!=null)tmp.Delete();			
		}
		
		

		public void DupeLayer(Layer layer,Mobile from)
		{
			Item fi = from.FindItemOnLayer(layer);	 //from item
			
			//delete existing own items
			ClearLayer(layer);
		
			if(fi!=null)  //make sure the item exists
			{
				Item copy = Server.Commands.Dupe.DuplicateItem(fi);
				
				if(copy==null)
				{				
					copy = new Item();
					copy.ItemID=fi.ItemID;
					copy.Hue=fi.Hue;		
				}
				
				
				if(copy is BaseWeapon)
				{
					copy.Movable=true;
					copy.LootType=LootType.Blessed;
				}
				else copy.Movable=false;
				
				copy.Layer=layer;
				copy.Visible=true;	
				AddItem(copy);
			}
			else  //if its null we delete our own item of this layer
			{
				Item oi = this.FindItemOnLayer(layer); //own item
				
				if(oi!=null)oi.Delete();			
			}
		}

		
		private void DoPolymorph(Mobile from)
		{
			if((lastpolymorph+TimeSpan.FromSeconds(30))>DateTime.Now)return;
			
			this.PlaySound( 1386 );
			
			lastperson=from;
			lastpolymorph=DateTime.Now;
			
			System.Debugger.Write("changeling","{0} turning into {1}",this,from);
			
			Female = from.Female;	
			Race = from.Race;			
			
			BodyValue = from.BodyValue; 
			Body = from.Body;
			BodyMod = from.BodyMod;
			if(!IsParagon && !IsSupreme)Hue = from.Hue; 
			Name = from.Name;
			
			BaseSoundID = 0;
			
			HairItemID=from.HairItemID;
			HairHue=from.HairHue;
			FacialHairItemID=from.FacialHairItemID;	
			FacialHairHue=from.FacialHairHue;
			
			//dupe layers		
			DupeLayer(Layer.FirstValid,from);
			DupeLayer(Layer.OneHanded,from);
			DupeLayer(Layer.TwoHanded,from);
			DupeLayer(Layer.Shoes,from);
			DupeLayer(Layer.Pants,from);
			DupeLayer(Layer.Shirt,from);
			DupeLayer(Layer.Helm,from);		
			DupeLayer(Layer.Gloves,from);
			DupeLayer(Layer.Neck,from);
			DupeLayer(Layer.Hair,from);
			DupeLayer(Layer.InnerTorso,from);
			DupeLayer(Layer.FacialHair,from);
			DupeLayer(Layer.MiddleTorso,from);
			DupeLayer(Layer.Earrings,from);
			DupeLayer(Layer.Arms,from);
			DupeLayer(Layer.Cloak,from);
			DupeLayer(Layer.OuterTorso,from);
			DupeLayer(Layer.OuterLegs,from);
			DupeLayer(Layer.InnerLegs,from);
			DupeLayer(Layer.Waist,from);
			DupeLayer(Layer.Talisman,from);
			DupeLayer(Layer.Ring,from);		
			DupeLayer(Layer.Bracelet,from);		
			
			
			ResetSkills(false);
			
			//dupe skills	
			SetSkill( SkillName.Magery, from.Skills.Magery.Base );
			SetSkill( SkillName.EvalInt, from.Skills.EvalInt.Base );
			SetSkill( SkillName.Necromancy, from.Skills.Necromancy.Base );
			SetSkill( SkillName.SpiritSpeak, from.Skills.SpiritSpeak.Base );
			SetSkill( SkillName.Meditation, from.Skills.Meditation.Base );
			SetSkill( SkillName.DetectHidden, from.Skills.DetectHidden.Base );
			SetSkill( SkillName.Focus, from.Skills.Focus.Base );
			SetSkill( SkillName.MagicResist, from.Skills.MagicResist.Base );
			SetSkill( SkillName.Tactics, from.Skills.Tactics.Base );
			SetSkill( SkillName.Anatomy, from.Skills.Anatomy.Base );
			SetSkill( SkillName.Wrestling, from.Skills.Wrestling.Base );
			SetSkill( SkillName.Swords, from.Skills.Swords.Base );
			SetSkill( SkillName.Macing, from.Skills.Macing.Base );
			SetSkill( SkillName.Fencing, from.Skills.Fencing.Base );
			SetSkill( SkillName.Archery, from.Skills.Archery.Base );
			SetSkill( SkillName.Stealth, from.Skills.Stealth.Base );
			SetSkill( SkillName.Hiding, from.Skills.Hiding.Base );
			SetSkill( SkillName.Parry, from.Skills.Parry.Base );
			SetSkill( SkillName.Bushido, from.Skills.Bushido.Base );
			SetSkill( SkillName.Ninjitsu, from.Skills.Ninjitsu.Base );
			SetSkill( SkillName.Inscribe, from.Skills.Inscribe.Base );	
			SetSkill( SkillName.Musicianship, from.Skills.Musicianship.Base );	
			SetSkill( SkillName.Discordance, from.Skills.Discordance.Base );	
			SetSkill( SkillName.Peacemaking, from.Skills.Peacemaking.Base );	
			SetSkill( SkillName.Provocation, from.Skills.Provocation.Base );	
			
			SetResistance( ResistanceType.Physical,Math.Max(40,from.BasePhysicalResistance) );
			SetResistance( ResistanceType.Fire, Math.Max(40,from.BaseFireResistance) );
			SetResistance( ResistanceType.Cold, Math.Max(40,from.BaseColdResistance) );
			SetResistance( ResistanceType.Poison, Math.Max(40,from.BasePoisonResistance) );
			SetResistance( ResistanceType.Energy, Math.Max(40,from.BaseEnergyResistance) );		

			BoltEffect( 0 );	
				
			InvalidateProperties();//refresh title and stuff
		}
		
		
		public void ReversePolymorh()
		{
			Body = 264;
			BodyValue = 264;
			BodyMod = 0;			
			
			Name = OriginalName;
			if(!IsParagon)Hue = OriginalHue;
			
			BaseSoundID = 0x451;
			
			ClearLayer(Layer.OneHanded);
			ClearLayer(Layer.TwoHanded);
			ClearLayer(Layer.Shoes);
			ClearLayer(Layer.Pants);
			ClearLayer(Layer.Shirt);
			ClearLayer(Layer.Helm);		
			ClearLayer(Layer.Gloves);
			ClearLayer(Layer.Neck);
			ClearLayer(Layer.Hair);
			ClearLayer(Layer.InnerTorso);
			ClearLayer(Layer.FacialHair);
			ClearLayer(Layer.MiddleTorso);
			ClearLayer(Layer.Earrings);
			ClearLayer(Layer.Arms);
			ClearLayer(Layer.Cloak);
			ClearLayer(Layer.OuterTorso);
			ClearLayer(Layer.OuterLegs);
			ClearLayer(Layer.InnerLegs);
			ClearLayer(Layer.Waist);

			SetResistance( ResistanceType.Physical, 81, 90 ); 
			SetResistance( ResistanceType.Fire, 40, 50 );
			SetResistance( ResistanceType.Cold, 40, 49 );
			SetResistance( ResistanceType.Poison, 41, 50 );
			SetResistance( ResistanceType.Energy, 43, 50 );			
			
			ResetSkills(true);	
			
			InvalidateProperties();
			
			lastperson=null;
		}
		
		
		public virtual void ResetSkills(bool dobaseskills)
		{
		SetSkill( SkillName.Necromancy, 0 );
		SetSkill( SkillName.SpiritSpeak, 0 );
		SetSkill( SkillName.Meditation, 0 );
		SetSkill( SkillName.DetectHidden, 0 );
		SetSkill( SkillName.Focus,0 );
		SetSkill( SkillName.Tactics, 0 );
		SetSkill( SkillName.Anatomy,0 );
		SetSkill( SkillName.Swords, 0 );
		SetSkill( SkillName.Macing,0 );
		SetSkill( SkillName.Fencing, 0 );
		SetSkill( SkillName.Archery, 0 );
		SetSkill( SkillName.Stealth, 0 );
		SetSkill( SkillName.Hiding, 0 );
		SetSkill( SkillName.Parry, 0 );
		SetSkill( SkillName.Bushido, 0);
		SetSkill( SkillName.Ninjitsu, 0 );
		SetSkill( SkillName.Inscribe,0 );	
		SetSkill( SkillName.Musicianship,0 );	
		SetSkill( SkillName.Discordance, 0 );	
		SetSkill( SkillName.Peacemaking, 0 );	
		SetSkill( SkillName.Provocation, 0 );		
		SetSkill( SkillName.Spellweaving, 0 );		
		
		
			if(dobaseskills)
			{		
			SetSkill( SkillName.MagicResist, 123, 132 );
			SetSkill( SkillName.Tactics, 12, 19 );
			SetSkill( SkillName.Meditation, 91, 98 ); //*Added Meditation*
			SetSkill( SkillName.Wrestling, 10, 12 );
			SetSkill( SkillName.Magery, 90, 95.0 );
			SetSkill( SkillName.Spellweaving, 91, 99.5 );
			SetSkill( SkillName.EvalInt, 91, 98 );		
			}
		}
		
		
		
        public override void OnThink()
        {
			if((lastpolymorph+polymorphtime)<DateTime.Now)
			{
			bool dontpolymorphback=false;
				//only polymorph back if no one is around
				foreach ( Mobile m in this.GetMobilesInRange(16) )
				{
					if ( m is PlayerMobile)
					{
					dontpolymorphback=true;
					break;
					}
				}
				
				lastpolymorph=DateTime.Now;
				
				if(!dontpolymorphback) ReversePolymorh();					
			}
		}		
		
		
		public override void OnDamage( int amount, Mobile from, bool willKill )
		{
		base.OnDamage(amount,from,willKill);
		
			if (from == null)return;
				
			Mobile changeto = from;
			if(changeto is BaseCreature && changeto.Alive)
			{
			BaseCreature bc = changeto as BaseCreature;
			
			if(bc.SummonMaster!=null)changeto=bc.SummonMaster;
			else if(bc.ControlMaster!=null)changeto=bc.ControlMaster;
			}
			
			if(changeto==lastperson || !changeto.Alive)return;
			

			if((BodyValue != changeto.BodyValue || Name!=changeto.Name) && (0.2>Utility.RandomDouble())) DoPolymorph(changeto);				
		}

		public override bool AutoDispel{ get{ return true; } }
		public override Poison PoisonImmune{ get{ return Poison.Lethal; } }
		public override bool IsMLCreature { get { return true;} }
		public override int ArcaneLevel{ get{ return 3; } }

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

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
		}
	}
}