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


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

namespace Server.Mobiles
{
	[CorpseName( "a petralisk corpse" )]
	public class Petralisk : BaseCreature
	{
	
		public override WeaponAbility GetWeaponAbility()
		{
			switch ( Utility.Random( 4 ) )
			{
				default:
				case 0: return WeaponAbility.Dismount;
				case 1: return WeaponAbility.DoubleShot;
				case 2: return WeaponAbility.Dismount;
				case 3: return WeaponAbility.DoubleShot;
			}
		}
	
	
		[Constructable]
		public Petralisk() : base( AIType.AI_Melee, FightMode.Closest, 10, 1, 0.15, 0.18 )
		{
			Name = "a petralisk";
			Body = 0xCE;
			Hue = 2412;
			BaseSoundID = 362;

			SetStr( 111, 140 );
			SetDex( 401, 620 );
			SetInt( 1001, 1040 );

			SetHits( 480, 690 );

			SetDamage( 22, 26 );

			SetDamageType( ResistanceType.Physical, 125 );

			SetResistance( ResistanceType.Physical, 70, 90 );
			SetResistance( ResistanceType.Fire, 50, 65 );
			SetResistance( ResistanceType.Cold, 20, 25 );
			SetResistance( ResistanceType.Poison, 40, 55 );
			SetResistance( ResistanceType.Energy, 20, 25 );

			SetSkill( SkillName.EvalInt, 80.1, 100.0 );
			SetSkill( SkillName.Magery, 80.1, 100.0 );
			SetSkill( SkillName.Meditation, 100.0 );
			SetSkill( SkillName.MagicResist, 100.0 );
			SetSkill( SkillName.Tactics, 110, 120.0 );
			SetSkill( SkillName.Wrestling, 100.1, 120.0 );

			Fame = 15000;
			Karma = 15000;

			VirtualArmor = 36;
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.FilthyRich, 3 );
			AddLoot( LootPack.Gems, 6 );
		}

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

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

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

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

		public override int GetHurtSound()
		{
			return 0x2C3;
		}
		
		public override void OnThink()
		{
		
			if (this.Combatant != null && 0.05 >= Utility.RandomDouble())
			{
				Mobile target = this.Combatant; 
				DoRockAttack(target,false);
				target.SendMessage("The petralisk causes a rockstorm");
			}
		
			else if (this.Combatant != null && 0.01 >= Utility.RandomDouble() && 0.6 >= Utility.RandomDouble())
			{
				Mobile target = this.Combatant; 
				RockWhirlwind();
			}	
			else if (this.Combatant != null && 0.03 >= Utility.RandomDouble() && this.Hits >= 150)
			{
				Mobile target = this.Combatant; 
				Dart();
			}	
		}

		public override bool ReacquireOnMovement{ get{ return true; } }
		public override double BonusPetDamageScalar{ get{ return 4.5; } }

		public override bool AutoDispel{ get{ return true; } }
		public override HideType HideType{ get{ return HideType.Barbed; } }
		public override int Hides{ get{ return 20; } }
		public override int Meat{ get{ return 5; } }
		public override int Scales{ get{ return 6; } }
		public override ScaleType ScaleType{ get{ return ScaleType.Black; } }
		public override int TreasureMapLevel{ get{ return 5; } }
		
		public override bool BardImmune{ get{ return true; } }
		public override bool Unprovokable{ get{ return true; } }
		public override bool Uncalmable{ get{ return true; } }

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

		public override void OnGotMeleeAttack( Mobile attacker )
		{
			base.OnGotMeleeAttack( attacker );

			if ( !Core.SE && 0.2 > Utility.RandomDouble() && attacker is BaseCreature )
			{
				BaseCreature c = (BaseCreature)attacker;

				if ( c.Controlled && c.ControlMaster != null )
				{
					c.ControlTarget = c.ControlMaster;
					c.ControlOrder = OrderType.Attack;
					c.Combatant = c.ControlMaster;
				}
			}
		}
		
		
		private void DoRockAttack(Mobile attacker,bool critical)
		{
		if(attacker==null)attacker=this;
		Map map = attacker.Map;

		int fieldsize = 5 + Utility.Random(5);  //5-15 radius
		
				for ( int ix = - fieldsize; ix <=  fieldsize; ++ix )
				{
					for ( int iy = - fieldsize; iy <=  fieldsize; ++iy )
					{
					
							if(0.9>Utility.RandomDouble())continue;
					
						    double dist = Math.Sqrt(ix*ix+iy*iy);
						    if ( dist > fieldsize )continue;
										
							int tx = attacker.X+ix;
							int ty = attacker.Y+iy;

							if(map==null)return;
							LandTile t = map.Tiles.GetLandTile( tx, ty );

							Point3D loc = new Point3D(tx ,ty, t.Z);
						
							if ( map.CanSpawnMobile( new Point2D( tx, ty ), t.Z ) ) 
							{
								//create the tile
								Item item = new temprock(this);
								item.MoveToWorld( loc, map );
							}
					}
				}
		attacker.PlaySound( Utility.Random( 544, 3 ) );
		}
		
		private void Dart()
		{
			ArrayList mob = new ArrayList();
		
			foreach ( Mobile m in this.GetMobilesInRange( 8 ) )
			{
				if ( m == this || !CanBeHarmful( m ) || !this.InLOS( m ))
					continue;
				if ( m is BaseCreature && (((BaseCreature)m).Controlled || ((BaseCreature)m).Summoned || ((BaseCreature)m).Team != this.Team) )
					mob.Add( m );
				else if ( m.Player )
					mob.Add( m );
			}
			
				if(mob.Count<=0)return;
			
				Mobile n = mob[Utility.Random(mob.Count)] as Mobile;
				
				if (n != null)
				{				
				Effects.SendLocationParticles( EffectItem.Create( this.Location, this.Map, EffectItem.DefaultDuration ), 0x3728, 10, 10, 2023 );
				this.PlaySound( 0x512 );
				
				this.Location = n.Location;
				this.Combatant = n;
				}
		}
		
		private class temprock : Item
		{
			private Timer m_Timer;
			private DateTime m_End;

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

			public temprock(Mobile summoner)
			{
				Visible = true;
				Movable = false;
				
				Hue = 2412;
				ItemID=4944;
				Name="a rock";
				Weight = 0; 
			
					switch(Utility.Random(9))
					{
					case 0: ItemID= 4962;break;
					case 1: ItemID= 4955;break;
					case 2: ItemID= 4967;break;
					case 3: ItemID= 4963+Utility.Random(4);break;
					case 4: ItemID= 4955;break;
					case 5: ItemID= 4963;break;
					case 6: ItemID= 4971+Utility.Random(3);break;
					case 7: ItemID= 4952;break;
					case 8: ItemID= 4963;break;
					}


				m_Timer = new InternalTimer( this, TimeSpan.FromSeconds( 10 + Utility.Random(0, 50) ),summoner );
				m_Timer.Start();

				m_End = DateTime.Now + TimeSpan.FromSeconds( 30 + Utility.Random(0,60) );
			}
			
			
			public override bool OnMoveOver( Mobile m )
			{
			return false;
			}

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

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

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

				//writer.WriteDeltaTime( m_End );
			}

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

				int version = reader.ReadInt();

				if(version==1)	m_End = reader.ReadDeltaTime();
				
				
			this.Delete();   //lets just do a cleanup if server reboots, instead of starting all these timers	
			}

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

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

			private class InternalTimer : Timer
			{
				private temprock m_Item;
				private Mobile m_summoner;

				public InternalTimer( temprock item, TimeSpan duration, Mobile summoner  ) : base( duration )
				{
					Priority = TimerPriority.OneSecond;
					m_Item = item;
					m_summoner = summoner;
				}

				protected override void OnTick()
				{

				m_Item.Delete();
				
				Stop();
				}
			}
		}		
		
		private void RockWhirlwind()
		{
			ArrayList list = new ArrayList();

			foreach ( Mobile m in this.GetMobilesInRange( 15 ) )
			{
				if ( m == this || !CanBeHarmful( m ) || !this.InLOS( 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( "You are hit by the rock whirlwind");
				Effects.SendPacket( m, m.Map, new HuedEffect( EffectType.Moving, Serial.Zero, Serial.Zero, 4962, this.Location, m.Location, 10, 0, false, false, 2412, 0 ) );
				AOS.Damage( m, this, Utility.RandomMinMax( 55, 70 ), 100, 0, 0, 0, 0 );
			}
		}
		

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