/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\events and scenarios\misc\christmas\CoalOre.cs
Lines of code: 186
***********************************************************/


using System;
using Server.Items;
using Server.Mobiles;
using System.Collections;

namespace Server.Items
{

	public class CoalOre : Item
	{
		private DateTime m_BurnOutTime;
		private BurnTimer timer;
		private bool Ignited;
		private Mobile m_activator;
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public DateTime BurnOutTime
		{
			get
			{
				return m_BurnOutTime;
			}
			set
			{
				m_BurnOutTime = value;
			}
		}			
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public Mobile Activator
		{
			get
			{
				return m_activator;
			}
			set
			{
				m_activator = value;
			}
		}	
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public bool IsBurning
		{
		get{return (BurnOutTime>DateTime.Now);}
		}
		
	
		[Constructable]
		public CoalOre() : this( 1 )
		{
		}

		[Constructable]
		public CoalOre( int amount ) : base(0x19B9)
		{
		Hue=1175;
		Stackable=true;
		Name="Coal";
		Amount=amount;		
		}
		
		[Constructable]
		public CoalOre( int min,int max ) : this(min+Utility.Random(max-min))
		{
		}
		
		public override void OnDoubleClick( Mobile from )
		{
			if (IsBurning || !from.InRange(this,1))return;
			
			
			if (!this.Movable || IsChildOf( from.Backpack ) )
			{
				from.SendMessage("This must be on the ground and not locked down");
			}
			else if ( !from.CheckSkill( SkillName.Camping, 0.0, 100.0 ) )
			{
				from.SendMessage( "You fail to ignite the coal" );
			}
			else StartBurn(from);
		}		
		
		public override bool CanBeTriggered{get{return true;} }		
		
		public override bool OnTrigger(Mobile from,Item itemfrom,TriggerType type)
		{
		if(IsBurning)return false;
		
		if(type!=TriggerType.Heat && type!=TriggerType.Explosion)return false;
		
		if(type==TriggerType.Explosion && 0.16>Utility.RandomDouble())return false;
		
		if(0.4<Utility.RandomDouble())return false;
		
		StartBurn(from);		
		return true;
		}
		
		
		public void StartBurn(Mobile from)
		{
		if(IsBurning || !this.Movable)return;
		
		if(from!=null && from.InRange(this,5))from.SendMessage("The coal catches on fire!");
		
		if(timer!=null)timer.Stop();
		
		BurnOutTime=DateTime.Now+TimeSpan.FromMinutes(1)+TimeSpan.FromSeconds(this.Amount*90+Utility.Random(this.Amount*10));
		Hue=242;
		Name="Burning Coal";
		Movable=false;
		Ignited=true;
		Activator=from;		
		timer = new BurnTimer(this);
		timer.Start();
		}
				
		private void StopBurn()
		{
		if(timer!=null)timer.Stop();
		
		this.Delete();
		}
		
		
		public override bool StackWith( Mobile from, Item dropped, bool playSound )
		{
		if((this.Amount+dropped.Amount)>25)return false;
		
		
		return base.StackWith( from, dropped, playSound );
		}
		
		
		private class BurnTimer : Timer
		{
			private CoalOre m_Owner;

			public BurnTimer( CoalOre owner ) : base( TimeSpan.FromSeconds( 3.0 ), TimeSpan.FromSeconds( 1.0 ) )
			{
				m_Owner = owner;
				Priority = TimerPriority.OneSecond;
			}


			protected override void OnTick()
			{
			Effects.SendLocationParticles( EffectItem.Create( new Point3D(m_Owner.X,m_Owner.Y,m_Owner.Z+2), m_Owner.Map, EffectItem.DefaultDuration ), 6571, 1, 23, 0, 7, 9915, 0 );
			
				ArrayList toDamage = new ArrayList();

				foreach( Mobile m in m_Owner.GetMobilesInRange( 1 ) )
				{
					if(m_Owner.Activator!=null && m_Owner.Activator.ShouldHarm(m))
					{
						toDamage.Add( m );
					}
					else if(m_Owner.Activator==null && HarmItem.ShouldHarm(m))toDamage.Add(m);
				}

				for( int i = 0; i < toDamage.Count; i++ )
					m_Owner.DoDamage( (Mobile)toDamage[i] );				
					

				//randomly check for more ore to ignite
				
				if(0.25>Utility.RandomDouble())
				{
				ArrayList toignite = new ArrayList();

					foreach( Item m in m_Owner.GetItemsInRange( 1 ) )
					{			
				
							if(m.InRange(m_Owner,0) && 0.3>Utility.RandomDouble())
							{
								toignite.Add( m );
							}
							else if(m.InRange(m_Owner,1) && 0.1>Utility.RandomDouble())
							{
								toignite.Add( m );
							}						
							else if(m.InRange(m_Owner,2) && 0.02>Utility.RandomDouble())
							{
								toignite.Add( m );
							}								
					}

					for( int i = 0; i < toignite.Count; i++ )
					{
					if(toignite[i] is Item)((Item)toignite[i]).Trigger(m_Owner.Activator,m_Owner,TriggerType.Heat);
					}
				}			
			
			if(!m_Owner.IsBurning)m_Owner.StopBurn();
			}
		}		
		
		
		void DoDamage(Mobile m)
		{
		m.PlaySound( 0x208 );
		AOS.Damage(m, Activator,15+(this.Amount*(1+Utility.Random(5))), 0, 100, 0, 0, 0 );
		}
		
		

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

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 1 ); // version
			writer.Write(m_activator);
			writer.Write(m_BurnOutTime);
			writer.Write(Ignited);
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();
			
			if(version>=1)
			{
			m_activator=reader.ReadMobile();
			m_BurnOutTime=reader.ReadDateTime();
			Ignited=reader.ReadBool();
			
				if(Ignited)
				{
				timer = new BurnTimer(this);
				timer.Start();
				}
			}
		}
	}	
}