/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\Mobiles\Monsters\Humanoid\Magic\AncientLich.cs
Lines of code: 230
***********************************************************/


using System;
using System.Collections;
using Server.Items;
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;

namespace Server.Mobiles
{
	[CorpseName( "an ancient liche's corpse" )]
	public class AncientLich : BaseCreature
	{
	
		private ArrayList spawns = new ArrayList();
		private static DateTime lastmorph;
	
		[Constructable]
		public AncientLich() : base( AIType.AI_Super, FightMode.Closest, 10, 1, 0.2, 0.4 )
		{
			Name = NameList.RandomName( "ancient lich" );
			Body = 78;
			BaseSoundID = 412;

			SetStr( 216, 305 );
			SetDex( 96, 115 );
			SetInt( 966, 1045 );

			SetHits( 560, 595 );

			SetDamage( 15, 27 );

			SetDamageType( ResistanceType.Physical, 20 );
			SetDamageType( ResistanceType.Cold, 40 );
			SetDamageType( ResistanceType.Energy, 40 );

			SetResistance( ResistanceType.Physical, 55, 65 );
			SetResistance( ResistanceType.Fire, 25, 30 );
			SetResistance( ResistanceType.Cold, 50, 60 );
			SetResistance( ResistanceType.Poison, 50, 60 );
			SetResistance( ResistanceType.Energy, 25, 30 );

			SetSkill( SkillName.EvalInt, 120.1, 130.0 );
			SetSkill( SkillName.Magery, 120.1, 130.0 );
			SetSkill( SkillName.Meditation, 100.1, 101.0 );
			SetSkill( SkillName.Poisoning, 100.1, 101.0 );
			SetSkill( SkillName.MagicResist, 175.2, 200.0 );
			SetSkill( SkillName.Tactics, 90.1, 100.0 );
			SetSkill( SkillName.Wrestling, 75.1, 100.0 );
			SetSkill( SkillName.Necromancy, 100, 120.0 );
			SetSkill( SkillName.SpiritSpeak,100, 120.0 );

			Fame = 23000;
			Karma = -23000;

			VirtualArmor = 60;
			PackNecroReg( 30, 275 );
			
		}

		public override OppositionGroup OppositionGroup
		{
			get{ return OppositionGroup.FeyAndUndead; }
		}

		public override int GetIdleSound()
		{
			return 0x19D;
		}

		public override int GetAngerSound()
		{
			return 0x175;
		}

		public override int GetDeathSound()
		{
			return 0x108;
		}

		public override int GetAttackSound()
		{
			return 0xE2;
		}

		public override int GetHurtSound()
		{
			return 0x28B;
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.FilthyRich, 3 );
			AddLoot( LootPack.MedScrolls, 2 );
		}
		
		private int GetLiveSpawns()
		{
		int live=0;
				for( int i = 0; i < spawns.Count; ++i )
                {
                Mobile m = spawns[i] as Mobile;

                if( m == null || m.Deleted || !m.Alive)continue;
				live++;							
                }
				
		return live;
		}
		
		public override void OnThink ()
		{
			if (!IsBodyMod && 0.001 > Utility.RandomDouble() && this.Alive && (lastmorph+TimeSpan.FromMinutes(5))<DateTime.Now) 
					Polymorph( this );
		}
		
		public void Polymorph( Mobile m )
		{
			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 );
					
				switch ( Utility.Random( 6 ) )
				{
					default:
					case 0: 
						m.NameMod = "a wraith";
						m.BodyMod = 26;
						break;
					case 1: 
						m.NameMod = "a zombie";
						m.BodyMod = 3; 
						break;
					case 2:
						m.NameMod = "a ghoul";
						m.BodyMod = 153;
						break;
					case 3: 
						m.NameMod = "a bone magi";
						m.BodyMod = 148;
						break;
					case 4: 
						m.NameMod = "a skeleton";
						m.BodyMod = 50;
						break;
					case 5: 
						m.NameMod = "a skeletal knight";
						m.BodyMod = 147;
						break;
				}

				m.HueMod = 0;

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

		private class ExpirePolymorphTimer : Timer
		{
			private Mobile m_Owner;

			public ExpirePolymorphTimer( Mobile owner ) : base( TimeSpan.FromMinutes( 10.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.NameMod = null;
					lastmorph = DateTime.Now;
					m_Owner.EndAction( typeof( PolymorphSpell ) );
				}
			}
		}
		
		
		public override void OnDamage( int amount, Mobile attacker, bool willKill ) 
		{

			if ( attacker != this && 0.07 > Utility.RandomDouble() && GetLiveSpawns()<8)
			{
				BaseCreature spawn;
				
				switch ( Utility.Random( 10 ) )
				{
					default:
					case 0: 
						spawn = new Skeleton(); 
						break;
					case 1: 
						spawn = new SkeletalKnight(); 
						break;
					case 2:
						spawn = new Wraith(); 
						break;
					case 3: 
						spawn = new Wight(); 
						break;
					case 4: 
						spawn = new BoneMagi(); 
						break;
					case 5: 
						spawn = new Lich(); 
						break;
					case 6:
						spawn = new LichLord(); 
						break;
					case 7: 
						spawn = new SkeletalMage(); 
						break;
					case 8:
						spawn = new Zombie(); 
						break;
					case 9: 
						spawn = new Ghoul(); 
						break;

				}

				spawn.Team = this.Team;
				spawn.MoveToWorld( this.Location, this.Map );
				spawn.Combatant = attacker;
				
				spawns.Add(spawn);
			}

			base.OnDamage( amount, attacker, willKill ) ;
		}
	
		public override bool Unprovokable{ get{ return true; } }
		public override bool BleedImmune{ get{ return true; } }
		public override Poison PoisonImmune{ get{ return Poison.Lethal; } }
		public override int TreasureMapLevel{ get{ return 5; } }

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