/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-01-2012 10:09:13pm
Filepath: scripts\events and scenarios\Halloween2011\Mobiles\LadyOfTheMists.cs
Lines of code: 162

Description: 

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


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;

//Ability breakdown:
//- Bleed immune
//- Low level poison immunity
//- Life drain (Takes 1 hp every half second for 12 ticks)
//- Teleport (Able to teleport to attacker when damage is taken)
//- Tiring blow (Takes 20 stamina from the target)

namespace Server.Mobiles
{
	[CorpseName( "a misty corpse" )]
	public class LadyOfTheMist : BaseCreature
	{
		[Constructable]
		public LadyOfTheMist() : base( AIType.AI_Super, FightMode.Closest, 10, 1, 0.2, 0.4 )
		{
			Name = "a lady of the mists";
			Body = 252;
			Hue = 1150; 
			BaseSoundID = 0x482;

			SetStr( 160, 200 );
			SetDex( 126, 183 ); 
			SetInt( 110, 143 );

			SetHits( 258, 282 ); 

			SetDamage( 16, 21 ); 

			SetDamageType( ResistanceType.Energy, 50 );
			SetDamageType( ResistanceType.Cold, 50 );

			SetResistance( ResistanceType.Physical, 40, 50);
			SetResistance( ResistanceType.Fire, 5, 10 );
			SetResistance( ResistanceType.Cold, 50, 100 );
			SetResistance( ResistanceType.Poison, 5, 10 );
			SetResistance( ResistanceType.Energy, 50, 100 );

			SetSkill( SkillName.Tactics, 65.3, 69.1 );
			SetSkill( SkillName.Wrestling, 110.0, 120.0 );

			Fame = 6000;
			Karma = -6000;

			VirtualArmor = 28;
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.Rich, 6 );
		}
		
		public override void OnDamagedBySpell(Mobile attacker)
		{
		//Wight may teleport when damaged by a spell
			if ( 0.5 > Utility.RandomDouble() && this.InLOS( attacker ) && attacker != null)
			{
			this.Location = attacker.Location; 
			this.FixedParticles( 0x376A, 9, 32, 0x13AF, EffectLayer.Waist );
			attacker.PlaySound( 0x1FE );
			}
		}
		
		public override void OnGotMeleeAttack(Mobile attacker)
		{
		//Wight may teleport when damaged by a long range melee attack (archery)
			if ( 0.5 > Utility.RandomDouble() && !attacker.InRange( this, 1 ) && this.InLOS( attacker ) && attacker.Alive && attacker != null)
			{
			this.Location = attacker.Location; 
			this.FixedParticles( 0x376A, 9, 32, 0x13AF, EffectLayer.Waist );
			attacker.PlaySound( 0x1FE );
			}
		}
		
		public override void OnGaveMeleeAttack(Mobile defender)
 		{
			base.OnGaveMeleeAttack (defender);
			
			if ( 0.5 > Utility.RandomDouble() ) //Special Ability: Life drain
			{
				if ( !IsBeingDrained( defender ) )
				{
					defender.SendMessage("The lady's withering touch drains your life");
					defender.FixedParticles( 0x373A, 1, 15, 9913, 552, 7, EffectLayer.Head );
					defender.PlaySound( 0x1F8 ); 
					BeginLifeDrain( defender, this );
				}
			}
			else if (0.6 > Utility.RandomDouble() ) //Special Ability: Stamina drain
			{
			defender.SendMessage("You are being drained of your stamina");
			defender.Stam -= 20; 
			}
		}

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

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

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

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

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

			if(m_Table.Contains(m))	m_Table.Remove( m );

			m.SendMessage("You are no longer being drained"); 
		}

		public override bool BleedImmune{ get{ return true; } }
		public override Poison PoisonImmune{ get{ return Poison.Lethal; } }
		public override bool Unprovokable{ get{ return true; } }
		public override bool CantbeSupreme { get { return true;} }
		public override bool CantbeParagon { get { return true;} }		

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

		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()
			{
				if(m_Mobile==null || m_From == null)
				{			
				this.Stop();
				return;
				}
				
				DrainLife( m_Mobile, m_From );

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

	}
}