/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\items\special\HornetNest.cs
Lines of code: 133
***********************************************************/


using System;
using Server;
using Server.Network;
using Server.Regions;
using Server.Mobiles;
using System.Collections;
using Server.Items;
using Server.Targeting;

namespace Server.Items
{
	public class HornetNest : Item
	{
		[Constructable]
		public HornetNest() : base(2330)
		{
		Name="a hornet nest";
		
		new ExpireTimer(this).Start();
		}

		
		public HornetNest( 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();
			
			this.Delete();
		}
		
		
		
		private class ExpireTimer : Timer
		{
			private HornetNest nest;
			bool spawned=false;
			DateTime expire;
			int spawns=0;

			public ExpireTimer( HornetNest pnest ) : base( TimeSpan.FromSeconds( 1.0 ), TimeSpan.FromSeconds( 1 ) )
			{
				nest=pnest;

				Priority = TimerPriority.OneSecond;
			}

			protected override void OnTick()
			{
			
					ArrayList targets = new ArrayList();

					foreach ( Mobile m in nest.GetMobilesInRange( 1 ) )
					{
					if(m.AccessLevel>AccessLevel.Player && !m.Hidden && !m.Player)continue;
							targets.Add( m );
							break;
					}									
			
					if(((0.08>Utility.RandomDouble() || !spawned) || targets.Count>0 )&& spawns<10)
					{
					spawned=true;
					spawns++;
					new HornetSwarm().MoveToWorld(new Point3D(nest.X+(-1+Utility.Random(3)),nest.Y+(-1+Utility.Random(3)),nest.Z),nest.Map);		
					}
					else if(0.25>Utility.RandomDouble())
					{
						foreach ( Mobile m in nest.GetMobilesInRange( 10 ) )
						{
								targets.Add( m );
								break;
						}				
						
						if(targets.Count<=0)
						{								
						Stop();
						nest.Delete();
						}
					}
			}
		}				
	}
	
	
	

	public class HornetSwarm : BaseCreature
	{
		public override bool DeleteCorpseOnDeath { get { return true; } }
		public override bool AlwaysMurderer { get { return true; } }
		public DateTime expire;

		[Constructable]
		public HornetSwarm() : base( AIType.AI_Melee, FightMode.Closest, 4, 1, 0.02, 0.02 )
		{
			Name = "a swarm of hornets";
			Body = 0x33;
			Hue = 0x4001;

			SetStr( 150 );
			SetDex( 450 );
			SetInt( 100 );

			SetHits( 180 );
			SetStam( 500 );
			SetMana( 0 );

			SetDamage( 6, 18 );

			SetDamageType( ResistanceType.Physical, 100 );

			SetResistance( ResistanceType.Physical, 90 );

			SetSkill( SkillName.Wrestling, 130.0 );
			SetSkill( SkillName.MagicResist, 170.0 );
			SetSkill( SkillName.Tactics, 130.0 );

			Fame = 4000;
			Karma = 0;
			
			expire=DateTime.Now + TimeSpan.FromSeconds(10+Utility.Random(70));
		}

		public override void MoveToWorld( Point3D loc, Map map )
		{
			base.MoveToWorld( loc, map );
			Timer.DelayCall( TimeSpan.FromSeconds( 0.1 ), DoEffects );
		}

		public void DoEffects()
		{
			FixedParticles( 0x91C, 10, 180, 0x2543, 0, 0, EffectLayer.Waist );
			PlaySound( 0xE );
			PlaySound( 0x1BC );

			if( Alive && !Deleted )
				Timer.DelayCall( TimeSpan.FromSeconds( 7.0 ), DoEffects );
				
		if(expire<DateTime.Now)this.Delete();
		
		if(Combatant!=null)Combatant.Combatant=null; 
		}

				public override bool ReacquireOnMovement{ get{ return !Controlled; } }
		
		public HornetSwarm( 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();
			this.Delete();
		}
	}	
}