/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on May-15-2012 07:15:47pm
Filepath: scripts\events and scenarios\Long Live the King 2012\Britain Undead Invasions\Evil Side\InvasionWight.cs
Lines of code: 227

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 wight corpse" )]
	public class InvasionWight : Wight
	{
		public static void GiveReward(Mobile boss)
		{
			ArrayList toGive = new ArrayList();
			List<DamageStore> rights = BaseCreature.GetLootingRights( boss.DamageEntries, boss.HitsMax );

			for ( int i = rights.Count - 1; i >= 0; --i )
			{
				DamageStore ds = rights[i];
				if ( ds.m_HasRight )	toGive.Add( ds.m_Mobile );
			}
			if ( rights.Count == 0 )return;	
			
			for(int i=0;i<rights.Count;i++)
			{
				Mobile m = rights[i].m_Mobile;			
				PlayerMobile pm = m as PlayerMobile;	
	
				Item gift = new UnderworldBone();	
						
				m.SendMessage(200,"You pull a strange bone from the corpse");
				m.GiveItem(gift);
			}
		}	

		public override void OnDeath( Container c )
		{
			base.OnDeath( c );
			
			if (0.03 >= Utility.RandomDouble())
				GiveReward(this);	
		}
	
		private DateTime lastability;
	
		[Constructable]
		public InvasionWight() : base()
		{
			Name = "an invading wight";
			Body = 3;   
			Hue = 0x96C; 
			BaseSoundID = 263; 

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

			SetHits( 158, 182 ); 

			SetDamage( 10, 13 ); 

			SetDamageType( ResistanceType.Physical, 50 );
			SetDamageType( ResistanceType.Poison, 50 );

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

			SetSkill( SkillName.EvalInt, 55.1, 70.0 );
			SetSkill( SkillName.Magery, 55.1, 70.0 );
			SetSkill( SkillName.MagicResist, 55.1, 70.0 );
			SetSkill( SkillName.Tactics, 65.3, 69.1 );
			SetSkill( SkillName.Wrestling, 75.0, 88.0 );

			Fame = 6000;
			Karma = -6000;

			VirtualArmor = 28;

			switch ( Utility.Random( 3 ))
			{
				case 0: PackItem( new Bone() ); break;
				case 1: PackItem( new RibCage() ); break;
				case 2: PackItem( new BonePile() ); break;
			}
			
			PackItem( new Boots( Utility.RandomNeutralHue() ) );
			PackItem( new ShortPants( Utility.RandomNeutralHue() ) );
			PackItem( new FancyShirt( Utility.RandomNeutralHue() ) );
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.Rich, 3 );
		}
		
		public override void OnDamagedBySpell(Mobile attacker)
		{
		//Wight may teleport when damaged by a spell
			if ( 0.5 > Utility.RandomDouble() )
			{
			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.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 wight's 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("The wight drains 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, 1, 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 AlwaysMurderer{ get{ return true; } }
		public override bool NoFlee { get { return true; } }

		public override OppositionGroup OppositionGroup
		{
			get{ return OppositionGroup.FeyAndUndead; }
		}
		
		public override void OnThink()
		{
			if (0.2 > Utility.RandomDouble() && lastability < DateTime.Now) {
				Steal();
			}

			base.OnThink();		
		}
		
		public void Steal()
		{
			ArrayList ilist = new ArrayList();
			ArrayList list = new ArrayList();
			
			foreach ( Item i in this.GetItemsInRange( 1 ) )
			{
				if ( i.Movable == false || i == null)
					continue;
				else
					ilist.Add( i );
			}
			
			foreach ( Item i in ilist )
			{
				this.GiveItem(i);
			}
			
			lastability=DateTime.Now + TimeSpan.FromSeconds(Utility.RandomMinMax( 40, 60 ));
		}

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

	}
}