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


using System;
using Server;
using Server.Misc;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using System.Collections;
using Server.Targeting;
using Server.Spells.First;
using Server.Spells.Second;
using Server.Spells.Fourth;
using Server.Spells.Eighth;

namespace Server.Mobiles
{
	[CorpseName( "a fairy corpse" )]
	public class Fairy : BaseCreature
	{
		public override bool InitialInnocent{ get{ return true; } }
		private Mobile lastcombatant = null;
		private Mobile healtarget;	
		private DateTime lastheal;

		[Constructable]
		public Fairy() : base( AIType.AI_Super, FightMode.Evil, 10, 1, 0.2, 0.4 )
		{
			Name = "A fairy"; 
			Body = 176;
			BaseSoundID = 0x467;
			Hue = Utility.RandomList(1150, 1153, 1154, 1151, 1165); 

			SetStr( 221, 230 );
			SetDex( 501, 600 );
			SetInt( 401, 450 );

			SetHits( 337, 489 );

			SetDamage( 15, 22 );

			SetDamageType( ResistanceType.Physical, 100 );

			SetResistance( ResistanceType.Physical, 80, 90 );
			SetResistance( ResistanceType.Fire, 40, 50 );
			SetResistance( ResistanceType.Cold, 40, 50 );
			SetResistance( ResistanceType.Poison, 60, 70 );
			SetResistance( ResistanceType.Energy, 70, 80 );

			SetSkill( SkillName.EvalInt, 105.1, 111.0 );
			SetSkill( SkillName.Magery, 105.0, 111.0 );
			SetSkill( SkillName.Meditation, 100.1, 110.0 );
			SetSkill( SkillName.MagicResist, 120.5, 160.0 );
			SetSkill( SkillName.Tactics, 25.1, 40.0 );
			SetSkill( SkillName.Wrestling, 25.1, 40.5 );

			Fame = 11000;
			Karma = 11000;

			VirtualArmor = 100;
		}

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

		public override HideType HideType{ get{ return HideType.Spined; } }
		public override int Hides{ get{ return 15; } }
		public override int Meat{ get{ return 4; } }

		public Fairy( Serial serial ) : base( serial )
		{
		}
		
		public void DoHeal()
		{
		if((lastheal+TimeSpan.FromSeconds(4))>DateTime.Now)return;
		lastheal=DateTime.Now;
		
		if(healtarget==null)return;
		if(!healtarget.InRange(this,2))return;
		
		if(Target!=null && healtarget==Combatant)
		{
		this.Target.Cancel( this, TargetCancelType.Canceled );
		return;
		}
		
		if(healtarget.Poison!=null)new CureSpell( this, null ).Cast();
		else if(healtarget.Alive && healtarget.Hits<healtarget.HitsMax)new HealSpell( this, null ).Cast();
		else if(!healtarget.Alive)new ResurrectionSpell( this, null ).Cast();
		}
		
		public override void OnThink()
        {	
			base.OnThink();
			
			if (this.Combatant != null && 0.05 >= Utility.RandomDouble() && 0.2 >= Utility.RandomDouble() && 0.3 >= Utility.RandomDouble() && this.Combatant.Player && !this.Combatant.Blessed && this.Combatant.InRange( this, 20 ))
			{
			Mobile Target = this.Combatant; 
			HolySpell(Target);	
			}
			
			if(Combatant!=null)return;
		
			//check if beneficial target is pending (most likely a heal - but slight chance to pickup any other spell it happened to be casting)
			Target targ = this.Target;
			
			if(targ!=null && ((targ.Flags & TargetFlags.Beneficial) != 0) && healtarget!=null)
			{
			if(healtarget!=null)targ.Invoke( this, healtarget );			
			else this.Target.Cancel( this, TargetCancelType.Canceled );
			}
			
		
			if(this.Spell!=null && this.Spell.IsCasting)return;
		
			if (ControlOrder==OrderType.Stay || ControlOrder==OrderType.Come || ControlOrder==OrderType.Follow)
			{

				if(this.ControlMaster!=null)
				{
				healtarget=this.ControlMaster;
					if(healtarget.Hits<healtarget.HitsMax)
					{
					DoHeal();
					return;
					}
				}
				else if(this.SummonMaster!=null)
				{
				healtarget=this.SummonMaster;
					if(healtarget.Hits<healtarget.HitsMax)
					{
					DoHeal();
					return;
					}
				}
				
			}
			
			if(healtarget!=null && InRange(healtarget,2) && healtarget.Hits<healtarget.HitsMax && healtarget.Karma>=0)DoHeal();
									
			
			if(Hits<(HitsMax/3))	healtarget=this;
			
			if(Combatant!=null)lastcombatant=Combatant;			
		}
		
		
		public override void OnMovement( Mobile m, Point3D oldLocation )
		{
		if((lastheal+TimeSpan.FromSeconds(5))>DateTime.Now)return;

			if ( !m.Frozen && InRange( m, 2 ) && !InRange( oldLocation, 2 ) && !InRange( m, 1 ) && InLOS( m ) && m.Player && m!=ControlMaster && m!=SummonMaster)
			{

				if (!m.Alive && m==lastcombatant )
				{
				Say("You tried to hurt me and deserve to remain dead!");
				return;				
				}
				else if(!m.Alive && m.Karma<=0)
				{
				Say("You may rot for your evil ways!");
				return;
				}


			
				if(m.Hits<m.HitsMax)
				{
				healtarget=m;
				DoHeal();
				return;
				}
			}
		}

		public override OppositionGroup OppositionGroup
		{
			get{ return OppositionGroup.FeyAndUndead; }
		}

		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();
		}
		
		public void HolySpell (Mobile defender)
		{
		
			if ( !this.CanBeHarmful( defender, false ) )
				return;

			this.DoHarmful( defender );

			int damage;
			
			if (defender.Karma < 0)
			{
				damage = (int)(defender.Karma/-500);
				if (damage < 10)
				{
					damage = 10;
				}
			}
			else if (defender.Karma >= 15000)
			{
				damage = 1;
			}
			else if (defender.Karma >= 7000)
			{
				damage = 5;
			}
			else
			{
				damage = 10;
			}
			
			Effects.SendLocationParticles( EffectItem.Create( defender.Location, defender.Map, EffectItem.DefaultDuration ), 0x36B0, 1, 20, 1153, 7, 9915, 0 );
			defender.FixedParticles( 0x37C4, 1, 8, 9502, 1153, 4, EffectLayer.Head );
			defender.PlaySound( 758 );

			AOS.Damage( defender, this, damage, true, 100, 0, 0, 0, 0 );
			defender.SendMessage("You've been hit by holy energy");
		}
	}
}