/**********************************************************
RunUO AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Death on Apr-06-2012 04:35:42pm
Filepath: scripts\events and scenarios\Long Live the King 2012\Britain Undead Invasions\HazardsAndCollisions\BurningOil.cs
Lines of code: 155

Description: 

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


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

namespace Server.Items
{

	public class BurningOil : Item
	{
		public DateTime m_BurnOutTime;
		public BurnTimer timer;
		public bool Ignited;
		public 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 BurningOil( ) : base(Utility.RandomMinMax(4650, 4654 ))
		{
			Hue=1175;
			Name="Oil";
			Movable=false;		
			Timer.DelayCall( TimeSpan.FromMinutes( 5.0 ), new TimerCallback( Delete ) );
		}
		
		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)return false;
		
		StartBurn(from);		
		return true;
		}
		
		
		public void StartBurn(Mobile from)
		{
		if(IsBurning)return;
		
		if(from!=null && from.InRange(this,5))from.SendMessage("The oil ignites with flame!");
		
		if(timer!=null)timer.Stop();
		
		BurnOutTime=DateTime.Now+TimeSpan.FromMinutes(1)+TimeSpan.FromSeconds(30+Utility.Random(40));
		Hue=0;
		ItemID = Utility.RandomList(14732, 14742);
		Name="Burning Oil";
		Movable=false;
		Ignited=true;
		Activator=from;		
		timer = new BurnTimer(this);
		timer.Start();
		}
				
		public void StopBurn()
		{
		if(timer!=null)timer.Stop();
		
		this.Delete();
		}
			
		public class BurnTimer : Timer
		{
			public BurningOil m_Owner;

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


			protected override void OnTick()
			{
			
				ArrayList toDamage = new ArrayList();

				foreach( Mobile m in m_Owner.GetMobilesInRange( 1 ) )
				{
					toDamage.Add( m );
				}

				for( int i = 0; i < toDamage.Count; i++ )
					m_Owner.DoDamage( (Mobile)toDamage[i] );				
				
				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,35+(this.Amount*(1+Utility.Random(5))), 0, 100, 0, 0, 0 );
		}
		
		

		public BurningOil( 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();
				}
			}
			
			this.Delete();
		}
	}	
}