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


using System;
using Server;
using Server.Mobiles;
using System.Collections;
using System.Collections.Generic;

namespace Server
{
	public class DamageAura
	{		
		//note: when adding new flags use same names as in BaseCreature for consistency
		
		private int AuraDamageMin; 
		private int AuraDamageMax; 

		private int AuraEffectID;				
		private int AuraEffectType;				
		private int AuraEffectHue; 		
		private int AuraEffectRender;
		private int AuraEffectSound;			
		
		private TextDefinition AuraEffectText;			
		private int AuraRange=2;				
		private bool AuraIgnoreLOS;		
			
		private int AuraDamagePhys;
		private int AuraDamageFire;
		private int AuraDamageCold;
		private int AuraDamagePoison;
		private int AuraDamageEnergy;
		private int AuraDamageChaos;		
		private int AuraDamageDirect=100;	
		
		private int AuraPoisonLevel;
		
		private bool AuraLeechTake;
		private int AuraLeechPercentHits;
		private int AuraLeechPercentStam;
		private int AuraLeechPercentMana;
	
		public DamageAura()
		{
		}
		
		public void SetDamage(int min,int max)
		{
			AuraDamageMin=min;
			AuraDamageMax=max;
			
			if(AuraDamageMax<AuraDamageMin)AuraDamageMax=AuraDamageMin;
		}
		
		public void SetDamageType(int phys,int fire,int cold,int pois,int energy,int chaos,int direct)
		{
			 AuraDamagePhys=phys;
			 AuraDamageFire=fire;
			 AuraDamageCold=cold;
			 AuraDamagePoison=pois;
			 AuraDamageEnergy=energy;
			 AuraDamageChaos=chaos;
			 AuraDamageDirect=direct;
		}
		
		public void SetEffect(int id,int type,int hue,int rendermode,int sound)
		{
			AuraEffectID=id;		
			AuraEffectType=type;
			AuraEffectHue=hue; 			
			AuraEffectRender=rendermode;
			AuraEffectSound=sound;		
		}
		
		public void SetText(TextDefinition text)
		{
			AuraEffectText=text;
		}
		
		public void SetRange(int range)
		{
			SetRange(range,false);
		}
		
		public void SetRange(int range,bool ignorelos)
		{
			AuraRange=Math.Max(0,range);
			AuraIgnoreLOS=ignorelos;
		}
		
		public void SetPoison(int level)
		{
			AuraPoisonLevel=level;
		}
		
		public void SetLeech(int hits,int stam,int mana)
		{
			SetLeech(hits,stam,mana,false);
		}
		
		public void SetLeech(int hits,int stam,int mana,bool takestats)
		{
			AuraLeechPercentHits=hits;
			AuraLeechPercentStam=stam;
			AuraLeechPercentMana=mana;
			AuraLeechTake=takestats;			
		}
			

		private int GetDamage()
		{
			return AuraDamageMin + Utility.Random(AuraDamageMax-AuraDamageMin);
		}
		
		public int MobileDoAura(Mobile from)
		{
			bool isharmful=false;
			int affected=0;
			
			
			List<Mobile> mobs = new List<Mobile>();
			
			foreach ( Mobile m in from.GetMobilesInRange( AuraRange ) )
			{
				mobs.Add(m);
			}
			
						
			for(int i=0;i<mobs.Count;i++)
			{
				Mobile m = mobs[i];
				
				if ( m==null || m == from || !m.Alive || m.Deleted || !from.CanBeHarmful( m ) || !from.ShouldHarm(m))
					continue;
				
				if(!AuraIgnoreLOS && !from.InLOS(m))
					continue;

				int damage =GetDamage();
				
				if(!from.OnBeforeAuraTo(m,ref damage))
					continue;
					
				affected++;
					
				if(AuraEffectText.Number!=0)m.SendLocalizedMessage(AuraEffectText.Number);
				else if(AuraEffectText.String!="")m.SendMessage(AuraEffectText.String);
				
				
				
				if(damage>0)
				{
					isharmful=true;
					int damagegiven = AOS.Damage( m, from, damage, false, AuraDamagePhys, AuraDamageFire, AuraDamageCold, AuraDamagePoison, AuraDamageEnergy,AuraDamageChaos,AuraDamageDirect,false );
					
					from.OnAuraTo(m,damagegiven);
					
					if(AuraLeechPercentHits>0)
					{
						int toleech = (int)((double)damagegiven * (double)((double)AuraLeechPercentHits/100));
						m.Hits=m.Hits-toleech;
						
						if(AuraLeechTake)from.Hits=from.Hits+toleech;
					}
					if(AuraLeechPercentStam>0)
					{
						int toleech = (int)((double)damagegiven * (double)((double)AuraLeechPercentStam/100));
						m.Stam=m.Stam-toleech;
						
						if(AuraLeechTake)from.Stam=from.Stam+toleech;
					}
					if(AuraLeechPercentMana>0)
					{
						int toleech = (int)((double)damagegiven * (double)((double)AuraLeechPercentMana/100));
						m.Mana=m.Mana-toleech;
						
						if(AuraLeechTake)from.Mana=from.Mana+toleech;
					}					
					
					
				}
				
				if(AuraEffectID!=0)m.FixedParticles( AuraEffectID, 1, 15, AuraEffectType,AuraEffectHue, AuraEffectRender, EffectLayer.Waist );
									
				
				if(AuraEffectSound!=0)m.PlaySound(AuraEffectSound);
				
				if(AuraPoisonLevel>0)
				{
					isharmful=true;
					Poison newPoison = Poison.GetPoison(AuraPoisonLevel );				
					m.Poison=newPoison;
				}
				
				if(isharmful)from.DoHarmful( m );
			}
			
			return affected;
		}
		
		
		
		
		public int ItemDoAura(Item from)
		{
			int affected=0;		
			List<Mobile> mobs = new List<Mobile>();
			
			foreach ( Mobile m in from.GetMobilesInRange( AuraRange ) )
			{
				mobs.Add(m);
			}			
						
			for(int i=0;i<mobs.Count;i++)
			{
				Mobile m = mobs[i];
				if ( m==null || !m.Alive || m.Deleted || !HarmItem.ShouldHarm(m))
					continue;
				
				if(!AuraIgnoreLOS && !m.InLOS(from))
					continue;

				int damage =GetDamage();
				
				if(!from.OnBeforeAuraTo(m,ref damage))
					continue;
					
				affected++;
					
				if(AuraEffectText.Number!=0)m.SendNRLocalizedMessage(AuraEffectText.Number,2);
				else if(AuraEffectText.String!="")m.SendNRMessage(AuraEffectText.String,2);
				
				if(damage>0)
				{
					int damagegiven = AOS.Damage( m, m, damage, false, AuraDamagePhys, AuraDamageFire, AuraDamageCold, AuraDamagePoison, AuraDamageEnergy,AuraDamageChaos,AuraDamageDirect,false );
					
					from.OnAuraTo(m,damagegiven);
					
					if(AuraLeechPercentHits>0)
					{
						int toleech = (int)((double)damagegiven * (double)((double)AuraLeechPercentHits/100));
						m.Hits=m.Hits-toleech;
					}
					if(AuraLeechPercentStam>0)
					{
						int toleech = (int)((double)damagegiven * (double)((double)AuraLeechPercentStam/100));
						m.Stam=m.Stam-toleech;
					}
					if(AuraLeechPercentMana>0)
					{
						int toleech = (int)((double)damagegiven * (double)((double)AuraLeechPercentMana/100));
						m.Mana=m.Mana-toleech;
					}										
				}
				
				if(AuraEffectID!=0)m.FixedParticles( AuraEffectID, 1, 15, AuraEffectType,AuraEffectHue, AuraEffectRender, EffectLayer.Waist );
									
				
				if(AuraEffectSound!=0)m.PlaySound(AuraEffectSound);
				
				if(AuraPoisonLevel>0)
				{
					Poison newPoison = Poison.GetPoison(AuraPoisonLevel );				
					m.Poison=newPoison;
				}
				
			}	
			
			return affected;
		}
		

	
	
	}
}