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


using System;
using Server;
using Server.Spells;
using Server.Items;
using Server.Network;
using Server.Misc;
using Server.Mobiles;
using System.Collections;


namespace Server.Items
{
	public class landmine : Item
	{
		private bool active=false;
		Mobile activator=null;

		public override bool CanBeTriggered{get{return true;} }

		[CommandProperty( AccessLevel.GameMaster )] 
		public bool isActive
		{
			get { return active; }
			set 
			{ 
				active = value;Modify();
				
			}
		}
		
		[CommandProperty( AccessLevel.GameMaster )] 
		public Mobile Activator
		{
			get { return activator; }
			set 
			{ 
				activator = value;	Modify();			
			}
		}		
		
		
		public override string DefaultName
		{
			get { return "Land Mine"; }
		}


		[Constructable]
		public landmine() :  base(7955)
		{
			Hue=38;
		}
		
	
		
		public override bool OnTrigger(Mobile from,Item itemfrom,TriggerType type)
		{
		if(!active)return false;
		
		bool totrigger=false;
		
		if((type==TriggerType.Explosion || type==TriggerType.Impact) && 0.3>Utility.RandomDouble())totrigger=true;
				
		if(totrigger)Explode(this.Location);
		return totrigger;
		}
		
		
		
		public void Explode(Point3D loc)
		{
			if(Map==null)return;
			
			Effects.SendLocationEffect( loc, Map, 0x36BD, 20 );
			Effects.PlaySound(loc,Map, 0x307 );
		
		
			IPooledEnumerable eable = Map.GetObjectsInRange( loc, 0 );
			ArrayList toExplode = new ArrayList();
			
			foreach ( object o in eable )
			{
				if ( o is Mobile || o is Item)
				{
					toExplode.Add( o );
				}
			}
			eable.Free();			
		
			for ( int i = 0; i < toExplode.Count; ++i )
			{
				if(toExplode[i] is Mobile)
				{
				Mobile m = toExplode[i] as Mobile;
				int damage = 30+Utility.Random(86);
				
				
					if(activator!=null && activator.ShouldHarm(m))
					{
					m.FixedParticles( 0x36BD, 20, 10, 5044, EffectLayer.Head );
					activator.DoHarmful( m );	
					AOS.Damage(m, activator, damage, 0, 100, 0, 0, 0 );
					}
					else if(activator==null && HarmItem.ShouldHarm(m))
					{
					m.FixedParticles( 0x36BD, 20, 10, 5044, EffectLayer.Head );
					AOS.Damage(m, null, damage, 0, 100, 0, 0, 0 );
					}
				
				}
				else if(toExplode[i] is Item)
				{
				((Item)toExplode[i]).Trigger(activator,this,TriggerType.Explosion);
				}
			}			
		this.Delete();
		}
		


		public override bool OnMoveOver( Mobile m )
		{
			if(!m.Alive || !active)return true;			
			
			if(activator==m)
			{
			m.SendMessage("You avoid a land mine placed by yourself");
			return true;
			}		
					
			
			if(activator!=null && activator.ShouldHarm(m))
			{
			m.SendMessage("You step on a land mine!");
			Explode(m.Location);
			}
			else if(activator==null && HarmItem.ShouldHarm(m))
			{
			m.SendMessage("You step on a land mine!");
			Explode(m.Location);
			}

			return true;
		}

		
		public override void OnDoubleClick( Mobile from )
		{
		Map map = from.Map;
		
			if ( IsChildOf( from.Backpack ) ) 
			{
				from.SendMessage("This must be on the ground to be activated.");
			return;
			}
			else if ( !from.InRange( this, 1 ) )
			{
				from.LocalOverheadMessage( MessageType.Regular, 0x3B2, 1019045 ); // I can't reach that.
				return;
			}
			else if(active)
			{
				from.SendMessage("This mine is already active!");
				return;
			}			
			
			else
			{
				from.SendMessage("You activate and hide the landmine.");
				active=true;
				activator = from;
				Visible=false;
				Movable=false;
			}
		}
		


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

		public override void Serialize( GenericWriter writer )
		{
					base.Serialize( writer );
					writer.Write( (int) 0 ); // version
					writer.Write( active );
					writer.Write(activator);
		}

		public override void Deserialize( GenericReader reader )
		{
							base.Deserialize( reader );
										int version = reader.ReadInt();
		active = reader.ReadBool();
		activator = reader.ReadMobile();
		}
	}
}
