/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on May-15-2012 07:15:46pm
Filepath: scripts\events and scenarios\Long Live the King 2012\Britain Undead Invasions\Evil Side\UndeadLord.cs
Lines of code: 217

Description: 

***********************************************************/


using System; 
using Server;
using Server.Items;
using Server.Targeting;
using Server.Network;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Server.Mobiles
{
	public class UndeadLord : BaseCreature
	{
		private DateTime lastability;
		private DateTime lastbroadcast;
		private bool m_SpawnedMinions;
		
		private ArrayList spawns = new ArrayList();

		public override WeaponAbility GetWeaponAbility()
		{
			switch ( Utility.Random( 2 ) )
			{
			default:				
			case 0: return WeaponAbility.CrushingBlow;
			case 1: return WeaponAbility.ConcussionBlow;
			}
		}	
		
		public static void GiveReward(Mobile boss)
		{
			ArrayList toGive = new ArrayList();
			List<DamageStore> rights = BaseCreature.GetLootingRights( boss.DamageEntries, boss.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;	
	
				Item gift = new UnderworldBone();	
						
				m.SendMessage(200,"You pull a strange bone from the corpse");
				m.GiveItem(gift);
			}
		}	

		public override void OnDeath( Container c )
		{
			base.OnDeath( c );
			
			if (0.1 >= Utility.RandomDouble());
				GiveReward(this);	
		}

		[Constructable]
		public UndeadLord() : base( AIType.AI_Melee, FightMode.Closest, 10, 1, 0.2, 0.4 )
		{
			Name = "An Undead Lord";
			Hue = 1175;
			Body = 308;
			BaseSoundID = 0x48D;

			SetStr( 1000 );
			SetDex( 151, 175 );
			SetInt( 171, 220 );

			SetHits( 10000 );

			SetDamage( 34, 36 );

			SetDamageType( ResistanceType.Physical, 50 );
			SetDamageType( ResistanceType.Cold, 50 );

			SetResistance( ResistanceType.Physical, 75 );
			SetResistance( ResistanceType.Fire, 60 );
			SetResistance( ResistanceType.Cold, 90 );
			SetResistance( ResistanceType.Poison, 100 );
			SetResistance( ResistanceType.Energy, 60 );

			SetSkill( SkillName.DetectHidden, 80.0 );
			SetSkill( SkillName.EvalInt, 77.6, 87.5 );
			SetSkill( SkillName.Magery, 77.6, 87.5 );
			SetSkill( SkillName.Meditation, 100.0 );
			SetSkill( SkillName.MagicResist, 50.1, 75.0 );
			SetSkill( SkillName.Tactics, 100.0 );
			SetSkill( SkillName.Wrestling, 100.0 );
			
			SetSkill( SkillName.Necromancy, 90, 100.0 );
			SetSkill( SkillName.SpiritSpeak,80, 100.0 );

			Fame = 20000;
			Karma = -20000;

			VirtualArmor = 44;
			if(0.1>Utility.RandomDouble())	PackItem(new RandomTalisman());
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.UltraRich, 8 );
			AddLoot( LootPack.FilthyRich, 2 );	
		}

		public override bool Unprovokable{ get{ return true; } }
		public override Poison PoisonImmune{ get{ return Poison.Greater; } }

		public override void OnDamage( int amount, Mobile from, bool willKill )
		{
			/*if (this.InLOS( from ) && !from.InRange( this, 3 ) && from.Alive && !from.Blessed && 0.05 >= Utility.RandomDouble()) {
				this.Location = from.Location; 
				this.PlaySound( 0x59B );
				this.Combatant = from;
				from.SendMessage( "Meraktus charges towards you!" );
			}*/
			
			if(GetLiveSpawns()!=0) {
				this.Hits = 200000; 
				from.SendMessage( "The minions must be killed first. There are " + GetLiveSpawns() + " minions remaining.");
			}
			
			base.OnDamage( amount, from, willKill );
		}
		
		public override bool OnBeforeDeath()
		{
			if (GetLiveSpawns()==0) {
				base.OnBeforeDeath();
				World.Broadcast(1151, true, "Britain is victorious in defeating the undead army");
				return true;
			}
			else {
				return false;
			}
		}
		
		public override void OnThink ()
		{
		
			if (lastability < DateTime.Now) {
				World.Broadcast(41, true, "Britain is under attack by an undead army!");
				lastability=DateTime.Now + TimeSpan.FromMinutes(3);
			}

			if ( !m_SpawnedMinions )
			{
				for(int i=0;i<Utility.RandomMinMax(20,40);i++)
				{
					Mobile spawn;
					Map map = this.Map;
				
					if (0.10 >= Utility.RandomDouble()) {
						spawn = new Skeleton();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new SkeletalMage();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new SkeletalKnight();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new BoneMagi();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new BoneKnight();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new Lich();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new LichLord();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new RottingCorpse();
					}
					else if (0.10 >= Utility.RandomDouble()) {
						spawn = new Zombie();
					}
					else {
						if (0.25 >= Utility.RandomDouble()) {
							spawn = new AncientLich();
						}
						else {
							spawn = new SkeletalKnight();
						}
					}

					//spawns.Add(spawn);
					
					//Mobile spawn = new PoisonIvySpawn();
					int x = this.X + Utility.RandomMinMax( -20, 20 );
					int y = this.Y + Utility.RandomMinMax( -20, 20 );
					int z = this.Z;
					if ( map.CanSpawnMobile( new Point3D( x, y, z) ) )
					{
						spawn.MoveToWorld( new Point3D( x, y, z ), Map );
						spawns.Add(spawn);
					}
					
				}
				m_SpawnedMinions = true;
			}
			else if (m_SpawnedMinions == true && 0.01 >= Utility.RandomDouble())
			{
				CheckMinions();
			}
		
			//if (lastability < DateTime.Now)
			//		GroundStomp();
					
			base.OnThink();	
		}
		
		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 void CheckMinions()
		{
			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;
				
				if((int)this.GetDistanceToSqrt(m) > 20) {
					Effects.SendLocationParticles( EffectItem.Create( m.Location, m.Map, EffectItem.DefaultDuration ), 0x3728, 10, 10, 2023 );
					m.Location = this.Location;
					Effects.SendLocationParticles( EffectItem.Create( this.Location, m.Map, EffectItem.DefaultDuration ), 0x3728, 10, 10, 5023 );
					m.PlaySound( 0x1FE );
				}
            }
		}
	
		public static readonly TimeSpan DefenderRemountDelay = TimeSpan.FromSeconds( 10.0 );
		
		/*public void GroundStomp()
		{
			ArrayList list = new ArrayList();

			foreach ( Mobile m in this.GetMobilesInRange( 10 ) )
			{
				if ( m == this || !CanBeHarmful( m ) || !ShouldHarm( m ))
					continue;

				if ( m is BaseCreature && (((BaseCreature)m).Controlled || ((BaseCreature)m).Summoned || ((BaseCreature)m).Team != this.Team) )
					list.Add( m );
				else if ( m.Player )
					list.Add( m );
			}

			foreach ( Mobile m in list )
			{
				DoHarmful( m );

				m.SendMessage( "Meraktus slams the ground with tremendous force" );
				
				if (m.Mounted)	
				{					
					m.Dismount();
					
					m.SendLocalizedMessage( 1060083 ); // You fall off of your mount and take damage!

					m.PlaySound( 0x59C );
					m.FixedParticles( 0x3728, 10, 15, 9955, EffectLayer.Waist );

					AOS.Damage( m, this, 30 * (12 - (int)this.GetDistanceToSqrt(m)), 100, 0, 0, 0, 0 );
					BaseMount.SetMountPrevention( m, BlockMountType.Dazed, DefenderRemountDelay );
				}
				else {
					m.PlaySound( 0x59C );
					m.FixedParticles( 0x3728, 10, 15, 9955, EffectLayer.Waist );

					AOS.Damage( m, this, 30 * (12 - (int)this.GetDistanceToSqrt(m)), 100, 0, 0, 0, 0 );
				}
			}
			lastability=DateTime.Now + TimeSpan.FromSeconds(Utility.RandomMinMax( 10, 35 ));
		}*/

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

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );
			writer.Write(m_SpawnedMinions);
			//SERIALIZE private ArrayList spawns = new ArrayList();
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			m_SpawnedMinions = reader.ReadBool();
			//DESERIALIZE private ArrayList spawns = new ArrayList();
		}
	}
}
