/**********************************************************
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\demitel\Dark Palace\Mobiles\EmeraldKnight.cs
Lines of code: 162
***********************************************************/


using System;
using System.Collections;
using Server;
using Server.Network;
using Server.Items;
using System.Collections.Generic;
using Server.Targeting;
using Server.Mobiles;
using Server.Spells.Third;
using Server.Misc;
using Server.Regions;
using Server.SkillHandlers;

// This monster will drain mana from its target and target stunningly fast. It also has the ability to 
// summon lesser versions of itself to help.

namespace Server.Mobiles
{
	[CorpseName( "a crystaline corpse" )]
	public class EmeraldBlademaster : BaseCreature
	{
		public int MaxSpawns = 0; 
		public override bool IgnoreYoungProtection { get { return true; } }

		[Constructable]
		public EmeraldBlademaster() : base( AIType.AI_Melee, FightMode.Closest, 10, 1, 0.2, 0.4 )
		{
			Name = "an emerald blademaster";
			Body = 147;
			Hue = 2963;
			BaseSoundID = 451;

			SetStr( 250, 250 );
			SetDex( 150, 150 );
			SetInt( 100, 100 );

			SetHits( 4000, 4000 );

			SetDamage( 35, 40 );

			SetDamageType( ResistanceType.Physical, 85 );
			SetDamageType( ResistanceType.Poison, 15 );

			SetResistance( ResistanceType.Physical, 35, 45 );
			SetResistance( ResistanceType.Fire, 20, 20 );
			SetResistance( ResistanceType.Cold, 40, 75 );
			SetResistance( ResistanceType.Poison, 100, 100 );
			SetResistance( ResistanceType.Energy, 50, 80 );

			SetSkill( SkillName.MagicResist, 200.0 );
			SetSkill( SkillName.Tactics, 100.0 );
			SetSkill( SkillName.Wrestling, 120.0 );

			Fame = 250000;
			Karma = -25000;

			VirtualArmor = 40;

			PackItem( new Scimitar() );
			PackItem( new ChaosShield() );
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.SuperBoss );
			AddLoot( LootPack.Rich );
		}

		public override bool BleedImmune{ get{ return true; } }
		public override bool AutoDispel{ get{ return true; } }
		public override bool BardImmune{ get{ return true; } }
		public override bool ReacquireOnMovement{ get{ return true; } } 
		public override Poison PoisonImmune{ get{ return Poison.Lethal; } }
		public override bool AlwaysMurderer{ get{ return true; } }

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

		public override void OnGaveMeleeAttack(Mobile defender)
 		{
			base.OnGaveMeleeAttack (defender);
			
			if ( 0.5 > Utility.RandomDouble() )
			{
				if ( !IsBeingDrained( defender ) )
				{
					defender.SendMessage("The blademaster slashs your soul!");
					defender.FixedParticles( 0x373A, 1, 15, 9913, 1167, 7, EffectLayer.Head );
					defender.PlaySound( 0x1F8 ); 
					BeginLifeDrain( defender, this );
				}
			}
			else if (0.6 > Utility.RandomDouble() ) //Special Ability: Stamina drain
			{
			defender.SendMessage("The blademaster absorbs your mana!");
			defender.Mana -= 100; 
			}
		}

		private static Hashtable m_Table = new Hashtable();

		public static bool IsBeingDrained( Mobile m )
		{
			return m_Table.Contains( m );
		}

		public static void BeginLifeDrain( Mobile m, Mobile from )
		{
			Timer t = (Timer)m_Table[m];

			if ( t != null )
				t.Stop();

			t = new InternalTimer( from, m );
			m_Table[m] = t;

			t.Start();
		}

		private class InternalTimer : Timer
		{
			private Mobile m_From;
			private Mobile m_Mobile;
			private int m_Count;

			public InternalTimer( Mobile from, Mobile m ) : base( TimeSpan.FromSeconds( 0.5 ), TimeSpan.FromSeconds( 0.5 ) )
			{
				m_From = from;
				m_Mobile = m;
				Priority = TimerPriority.TwoFiftyMS;
			}

			protected override void OnTick()
			{
				DrainLife( m_Mobile, m_From );

				if ( ++m_Count == 10 )
					EndLifeDrain( m_Mobile );
			}
		}

		public static void DrainLife( Mobile m, Mobile from )
		{
			if ( m.Alive )
			{
				int damageGiven = AOS.Damage( m, from, 10, 100, 0, 0, 0, 0 ); 

				from.Mana += damageGiven;
			}
			else
		{
				EndLifeDrain( m );
			}
		}

		public static void EndLifeDrain( Mobile m )
		{
			Timer t = (Timer)m_Table[m];

			if ( t != null )
				t.Stop();

			m_Table.Remove( m );

			m.SendMessage("Your soul starts to recover itself."); 
		}

		public override void OnGotMeleeAttack( Mobile attacker )
		{
			Map map = attacker.Map;
			
			if (MaxSpawns < 5)
			{
				Mobile spawn = new EmeraldSkeleton();
				int x = this.X + Utility.RandomMinMax( -5, 5 );
				int y = this.Y + Utility.RandomMinMax( -5, 5 );
				int z = this.Z;
				if ( map.CanSpawnMobile( new Point3D( x, y, z) ) )
					{
					attacker.SendMessage( "The blademaster summons a lesser minion to its aid!" );
					spawn.MoveToWorld( new Point3D( x, y, z ), Map );
					spawn.Combatant = attacker;
					}
				MaxSpawns += 1; 
			}	
		}

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