/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Aug-08-2011 09:29:13pm
Filepath: scripts\Items\artifacts\Artifacts of Assimilation\greater\staffofmoses.cs
Lines of code: 244

Description: 

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


using Server;
using System;
using Server.Items;
using Server.Misc;
using Server.Regions;

namespace Server.Items
{
	
	public class staffofmoses : GnarledStaff
	{
		public override int InitMinHits{ get{ return 255; } }
		public override int InitMaxHits{ get { return 255; } }
		

		[Constructable]
		public staffofmoses() : base()
		{
		Name="Staff of Moses";
		Hue=493;
		}
		
		
		public bool IsWater(int id,int type)
		{
		if(type==0)return( (id >= 168 && id <= 171) || id == 310|| id == 311);	 //land tiles
		if(type==1)return((id>=6038 && id<=6066) || (id>=13422 && id<=13525) || (id>=22423 && id<=22428));//item (static) tiles
		return false;
		}
		
		
		
		public override void OnDoubleClick( Mobile from )
		{
		if(ParentEntity!=from)
		{
		from.SendMessage("You must equip the staff to open the waters");
		return;
		}		
		
		if(from.Mana<15)
		{
		from.SendMessage("You lack the mana to perform this action");
		return;
		}
		
		if(from.Region.IsPartOf( typeof( DungeonRegion ))||from.Region.IsPartOf( typeof( PeerlessRegion )))
		{
		from.SendMessage("This ability cannot be used here");
		return;
		}
		
		
		if(UseDelaySystem.CheckContext(from, DelayContextType.StaffOfMoses))
		{
		from.SendMessage("You must wait before using this ability again");
		return;
		}
		
		
		
				Map map = from.Map;
				
				bool playsound = false;
		
				for ( int ix = -13; ix <= 13; ix++ )
				{		
					for ( int iy = -13; iy <= 13; iy++ )
					{
					double dist = Math.Sqrt(ix*ix+iy*iy);
						if ( dist <= 13 )
						{
						int tx = from.X+ix;
						int ty = from.Y+iy;
						bool added=false;
						
						
							//check for land tiles
							LandTile t = map.Tiles.GetLandTile( tx, ty );
							if (IsWater(t.ID,0))
							{
							Point3D loc = new Point3D(tx ,ty,t.Z);			

							playsound=true;
							//create dispellable tile
							Item item = new InternalItem( loc, from.Map, from);		
							//add random misc item (rock, etc)
							if(Utility.Random(100)==50)new InternalItem2( loc, from.Map, from);			
							added=true;
							}
							
							
							//check for static tiles
							if(!added)
							{
					            StaticTile[] tiles = map.Tiles.GetStaticTiles( tx,ty );

					            for( int i = 0; i < tiles.Length; ++i )
					            {
					                StaticTile st = tiles[i];
									

					                if( IsWater( st.ID,1 ) )
									{	
										Point3D loc = new Point3D(tx ,ty,st.Z);
										playsound=true;
										//create dispellable tile
										Item item = new InternalItem( loc, from.Map, from);		
										//add random misc item (rock, etc)
										if(Utility.Random(100)==50)new InternalItem2( loc, from.Map, from);			
										added=true;									
										
										added=true;
										break;
									}
					                    
					            }							
							}			
						}
					}
			   }
		
		
			if(playsound)
			{
			from.SendMessage( "You create a passage over the water!" );
			from.PlaySound( 542 );
			from.Mana-=15;
			UseDelaySystem.AddContext(from, DelayContextType.StaffOfMoses, TimeSpan.FromSeconds(10));
			}
		
		//Console.WriteLine("facing: {0}",(int)from.Direction);
		}
		
		
		
		
		
		

		
		[DispellableField]
		private class InternalItem : Item
		{
			private Timer m_Timer;
			private DateTime m_End;

			public override bool BlocksFit{ get{ return true; } }

			public InternalItem( Point3D loc, Map map, Mobile caster ) : base( 0x53b )
			{
				Visible = false;
				Movable = false;


				MoveToWorld( loc, map );
				Z++;

				if ( caster.InLOS( this ) )
					Visible = true;
				else
					Delete();

				if ( Deleted )
					return;

				m_Timer = new InternalTimer( this, TimeSpan.FromSeconds( 120 + Utility.Random(60) ) ); //2-3min
				m_Timer.Start();

				m_End = DateTime.Now + TimeSpan.FromSeconds( 120 + Utility.Random(60) );
			}

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

			public override void Serialize( GenericWriter writer )
			{
				base.Serialize( writer );

				writer.Write( (int) 1 ); // version

			}

			public override void Deserialize( GenericReader reader )
			{
				base.Deserialize( reader );

				int version = reader.ReadInt();

				m_Timer = new InternalTimer( this, TimeSpan.FromMinutes(1+Utility.Random(20)) );
				m_Timer.Start();
			}

			public override void OnAfterDelete()
			{
				base.OnAfterDelete();

				if ( m_Timer != null )
					m_Timer.Stop();
			}

			private class InternalTimer : Timer
			{
				private InternalItem m_Item;

				public InternalTimer( InternalItem item, TimeSpan duration ) : base( duration )
				{
					Priority = TimerPriority.OneSecond;
					m_Item = item;
				}

				protected override void OnTick()
				{
					m_Item.Delete();
				}
			}
		}
		
		
		
		
			[DispellableField]
		private class InternalItem2 : Item
		{
			private Timer m_Timer;
			private DateTime m_End;

			public override bool BlocksFit{ get{ return true; } }

			public InternalItem2( Point3D loc, Map map, Mobile caster )
			{
				Visible = false;
				Movable = false;
				
				
			ItemID=4968;
			
							switch(Utility.Random(9))
							{
							case 0: ItemID= 4970;break;
							case 1: ItemID= 4972;break;
							case 2: ItemID= 3350;break;
							case 3: ItemID= 3353;break;
							case 4: ItemID= 3340;break;
							case 5: ItemID= 2321;break;
							case 6: ItemID= 2322;break;
							case 7: ItemID= 7052;break;
							case 8: ItemID= 12252;break;
							}


				MoveToWorld( loc, map );

				if ( caster.InLOS( this ) )
					Visible = true;
				else
					Delete();

				if ( Deleted )
					return;

				m_Timer = new InternalTimer( this, TimeSpan.FromSeconds( 60 + Utility.Random(0,300) ) );
				m_Timer.Start();

				m_End = DateTime.Now + TimeSpan.FromSeconds( 30 + Utility.Random(0,60) );
			}

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

			public override void Serialize( GenericWriter writer )
			{
				base.Serialize( writer );

				writer.Write( (int) 1 ); // version
			}

			
			public override void Deserialize( GenericReader reader )
			{
				base.Deserialize( reader );

				int version = reader.ReadInt();

				m_Timer = new InternalTimer( this,TimeSpan.FromMinutes(1+Utility.Random(20)) );
				m_Timer.Start();
			}

			public override void OnAfterDelete()
			{
				base.OnAfterDelete();

				if ( m_Timer != null )
					m_Timer.Stop();
			}

			private class InternalTimer : Timer
			{
				private InternalItem2 m_Item;

				public InternalTimer( InternalItem2 item, TimeSpan duration ) : base( duration )
				{
					Priority = TimerPriority.OneSecond;
					m_Item = item;
				}

				protected override void OnTick()
				{
					m_Item.Delete();
				}
			}
		}
		
		
		
		public staffofmoses( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 1 );
		}
		
		public override void Deserialize(GenericReader reader)
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();
			
			
			if(version==0)
			{
				Attributes.AttackChance = 0;
				Attributes.WeaponDamage=0;
				Attributes.WeaponSpeed=0;
				WeaponAttributes.HitColdArea=0;
				
				Slayer = SlayerName.None;			
			}

		}
	}
	
}