/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\engines\ML Peerless + misc areas\Jormundgand\Mobiles\Named\Charybdis.cs
Lines of code: 220
***********************************************************/


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

namespace Server.Mobiles
{
	[CorpseName( "a charybdis corpse" )]
	public class Charybdis : BaseCreature
	{
	
		public override WeaponAbility GetWeaponAbility()
		{
			switch ( Utility.Random( 6 ) )
			{
				default:
				case 0: return WeaponAbility.ParalyzingBlow;
				case 1: return WeaponAbility.WhirlwindAttack;
				case 2: return WeaponAbility.ParalyzingBlow;
				case 3: return WeaponAbility.WhirlwindAttack;
				case 4: return WeaponAbility.ParalyzingBlow;
				case 5: return WeaponAbility.WhirlwindAttack;
			}
		}
		
		private ArrayList spawns = new ArrayList();
	
		[Constructable]
		public Charybdis() : base( AIType.AI_Melee, FightMode.Closest, 10, 1, 0.2, 0.4 )
		{
			Name = "Charybdis";
			Body = 789;
			Hue = 88; 
			BaseSoundID = 353;

			SetStr( 756, 780 );
			SetDex( 800, 900 );
			SetInt( 100, 200 );

			SetHits( 600, 868 );
			SetMana( 400 );

			SetDamage( 10, 17 );

			SetDamageType( ResistanceType.Physical, 70 );
			SetDamageType( ResistanceType.Cold, 30 );

			SetResistance( ResistanceType.Physical, 45, 55 );
			SetResistance( ResistanceType.Fire, 30, 40 );
			SetResistance( ResistanceType.Cold, 30, 40 );
			SetResistance( ResistanceType.Poison, 20, 30 );
			SetResistance( ResistanceType.Energy, 10, 20 );

			SetSkill( SkillName.MagicResist, 15.1, 20.0 );
			SetSkill( SkillName.Meditation, 200, 400 );
			SetSkill( SkillName.Tactics, 120 );
			SetSkill( SkillName.Wrestling, 120 );

			Fame = 14000;
			Karma = -14000;

			VirtualArmor = 50;

			CanSwim = true;
			CantWalk = false;
		}
		
		public override void OnDeath( Container c )
		{
			if ( Utility.Random( 3 ) == 0 )
			{		
				Item item;

				switch ( Utility.Random( 1 ))
				{
								default:
					case 1: item = new CharybdisKey(); break;
							}

				c.DropItem( item );
			}

			base.OnDeath( c );
		}
		
		public override void OnThink()
		{

			if (this.Combatant != null && 0.03 >= Utility.RandomDouble() && 0.5 >= Utility.RandomDouble()  )
			{
			
			Map map = this.Map;
			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.Damage(10);
				m.Location = this.Location;
				this.PlaySound( 18 );
				Effects.SendLocationParticles( EffectItem.Create( this.Location, map, EffectItem.DefaultDuration ), 0x37CC, 1, 40, 1154, 3, 9917, 0 );
				m.SendMessage( "Charybdis draws you into a whirlwind" );
			}
			}
			
			base.OnThink();
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.FilthyRich, 4 );
		}
						
		public override bool BardImmune{ get{ return true; } }	
		public override bool Unprovokable{ get{ return true; } }
		public override bool Uncalmable{ get{ return true; } }
		public override bool ReacquireOnMovement{ get{ return true; } }
		public override bool BleedImmune{ get{ return true; } }
		public override Poison PoisonImmune{ get{ return Poison.Lethal; } }
		public override double BonusPetDamageScalar{ get{ return 1.5; } }
		public override bool CantbeSupreme{ get{ return true; } }	
		public override bool IsMLCreature { get { return true;} } 
		public override bool IsMLBoss { get { return true;} }	
		
		public override void OnDamage( int amount, Mobile attacker, bool willKill ) 
		{	
			if(attacker!=null && attacker is BaseCreature && 0.6 >= Utility.RandomDouble())
			{
				string[] toSays = new string[]
				{
					"*Charybdis rips the creature apart*",
				};
				
				this.Say( true, String.Format( toSays[Utility.Random( toSays.Length )], this.Name ) );
				attacker.Kill(); 
			}	
		base.OnDamage(amount, attacker, willKill );	
		}
		
		public override void OnGotMeleeAttack( Mobile attacker )
		{
			if ( 0.35 >= Utility.RandomDouble() && this.Hits > (this.HitsMax / 4))
			{
				SpawnWaterVortex( attacker );
			}
			else if ( 0.4 >= Utility.RandomDouble() )
			{
				BorrowVortex();
			}
			
			base.OnGaveMeleeAttack( attacker );
		}
		
		public override void OnDamagedBySpell(Mobile attacker)
		{
			if ( 0.35 >= Utility.RandomDouble() && this.Hits > (this.HitsMax / 4) && !attacker.InRange( this, 2 ))
			{
				SpawnWaterVortex( attacker );
			}
			else if ( 0.4 >= Utility.RandomDouble() )
			{
				BorrowVortex();
			}
			
			base.OnDamagedBySpell( attacker );
		}
		
		public void SpawnWaterVortex( Mobile m )
		{
			if ( GetLiveSpawns() < 5 )
			{
				Map map = this.Map;

				if ( map == null )
					return;

				CharybdisWaterVortex spawned = new CharybdisWaterVortex();

				spawned.Team = this.Team;

				bool validLocation = false;
				Point3D loc = this.Location;
	
				for ( int j = 0; !validLocation && j < 4; ++j )
				{
					int x = X + Utility.Random( 3 ) - 1;
					int y = Y + Utility.Random( 3 ) - 1;
					int z = map.GetAverageZ( x, y );

					if ( validLocation = map.CanFit( x, y, this.Z, 16, false, false ) )
						loc = new Point3D( x, y, Z );
					else if ( validLocation = map.CanFit( x, y, z, 16, false, false ) )
						loc = new Point3D( x, y, z );
				}

				spawned.MoveToWorld( loc, map );
				spawns.Add(spawned);
				spawned.Combatant = m;
			}
			else
				BorrowVortex();
		}
		
		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 BorrowVortex()
		{
			ArrayList toHeal = new ArrayList();
  
			foreach ( Mobile m in this.GetMobilesInRange( 15 ) )
			{
				if ( m is WaterVortex || m is CharybdisWaterVortex)
					toHeal.Add( m );
			}

			if ( toHeal.Count > 0 )
			{	
				string[] toSay = new string[]
				{
					"*Charybdis borrows energy from the vortexes*",
				};
				
				this.Say( true, String.Format( toSay[Utility.Random( toSay.Length )], this.Name ) );

				foreach ( Mobile m in toHeal )
				{
					Hits += (m.Hits / 2);
				}
			}
		}

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