/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\Items\Misc\Teleporter.cs
Lines of code: 509
***********************************************************/


using System;
using Server;
using Server.Network;
using Server.Engines.Quests;
using Server.Mobiles;

namespace Server.Items
{
	public class Teleporter : Item
	{
		private bool m_Active, m_Creatures;
		private Point3D m_PointDest;
		private Map m_MapDest;
		private bool m_SourceEffect;
		private bool m_DestEffect;
		private int m_SoundID;
		private TimeSpan m_Delay;
		
		//custom
		bool m_livemobsonly;       //if true, ghosts cannot enter
		bool m_deadmobsonly;     //if true, ghosts can enter only
		string m_requireditem;     //item required to enter, if specified
		string m_requiredquest;     //quest required to enter, if specified
		bool m_consumereqitem;  //if true, the required item is consumed upon entering
		string m_reqitemmsg;      //message to display if user does not have required item
		string m_reqquestmsg;      //message to display if user does not have required quest
		bool m_showcantpassmsg; //wether to show "only the dead may pass" etc
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public bool livemobsonly
		{
			get{ return m_livemobsonly; }
			set{ m_livemobsonly = value;  if(value==true)m_deadmobsonly=false;Modify();  }
		}
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public bool deadmobsonly
		{
			get{ return m_deadmobsonly; }
			set{ m_deadmobsonly = value; if(value==true)m_livemobsonly=false;Modify();  }
		}		

		[CommandProperty( AccessLevel.GameMaster )]
		public string requireditem
		{
			get{ return m_requireditem; }
			set{ m_requireditem = value;Modify();}
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public string requiredquest
		{
			get{ return m_requiredquest; }
			set{ m_requiredquest = value; Modify(); }
		}		

		[CommandProperty( AccessLevel.GameMaster )]
		public bool consumereqitem
		{
			get{ return m_consumereqitem; }
			set{ m_consumereqitem = value;Modify();  }
		}	

		[CommandProperty( AccessLevel.GameMaster )]
		public string reqitemmsg
		{
			get{ return m_reqitemmsg; }
			set{ m_reqitemmsg = value; Modify(); }
		}		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public string reqquestmsg
		{
			get{ return m_reqquestmsg; }
			set{ m_reqquestmsg = value; Modify(); }
		}		
		
		
		[CommandProperty( AccessLevel.GameMaster )]
		public bool SourceEffect
		{
			get{ return m_SourceEffect; }
			set{ m_SourceEffect = value; InvalidateProperties(); Modify();}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public bool DestEffect
		{
			get{ return m_DestEffect; }
			set{ m_DestEffect = value; InvalidateProperties(); Modify();}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int SoundID
		{
			get{ return m_SoundID; }
			set{ m_SoundID = value; InvalidateProperties(); Modify();}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public TimeSpan Delay
		{
			get{ return m_Delay; }
			set{ m_Delay = value; InvalidateProperties(); Modify();}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public bool Active
		{
			get { return m_Active; }
			set { m_Active = value; InvalidateProperties();Modify(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public Point3D PointDest
		{
			get { return m_PointDest; }
			set { m_PointDest = value; InvalidateProperties();Modify(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public Map MapDest
		{
			get { return m_MapDest; }
			set { m_MapDest = value; InvalidateProperties(); Modify();}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public bool Creatures
		{
			get { return m_Creatures; }
			set { m_Creatures = value; InvalidateProperties();Modify(); }
		}
		
		[CommandProperty( AccessLevel.GameMaster )]
		public bool showcantpassmsg
		{
			get { return m_showcantpassmsg; }
			set { m_showcantpassmsg = value; InvalidateProperties();Modify(); }
		}		

		
		
		
		public override int LabelNumber{ get{ return 1026095; } } // teleporter

		[Constructable]
		public Teleporter() : this( new Point3D( 0, 0, 0 ), null, false )
		{
		}

		[Constructable]
		public Teleporter( Point3D pointDest, Map mapDest ) : this( pointDest, mapDest, false )
		{
		}

		[Constructable]
		public Teleporter( Point3D pointDest, Map mapDest, bool creatures ) : base( 0x1BC3 )
		{
			Movable = false;
			Visible = false;

			m_Active = true;
			m_PointDest = pointDest;
			m_MapDest = mapDest;
			m_Creatures = creatures;
		}
		

		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );

			if ( m_Active )
				list.Add( 1060742 ); // active
			else
				list.Add( 1060743 ); // inactive

			if ( m_MapDest != null )
				list.Add( 1060658, "Map\t{0}", m_MapDest );

			if ( m_PointDest != Point3D.Zero )
				list.Add( 1060659, "Coords\t{0}", m_PointDest );

			list.Add( 1060660, "Creatures\t{0}", m_Creatures ? "Yes" : "No" );
		}

		public override void OnSingleClick( Mobile from )
		{
			base.OnSingleClick( from );

			if ( m_Active )
			{
				if ( m_MapDest != null && m_PointDest != Point3D.Zero )
					LabelTo( from, "{0} [{1}]", m_PointDest, m_MapDest );
				else if ( m_MapDest != null )
					LabelTo( from, "[{0}]", m_MapDest );
				else if ( m_PointDest != Point3D.Zero )
					LabelTo( from, m_PointDest.ToString() );
			}
			else
			{
				LabelTo( from, "(inactive)" );
			}
		}

		public virtual void StartTeleport( Mobile m )
		{
			if ( m_Delay == TimeSpan.Zero )
				DoTeleport( m );
			else
				Timer.DelayCall( m_Delay, new TimerStateCallback( DoTeleport_Callback ), m );
		}

		private void DoTeleport_Callback( object state )
		{
			DoTeleport( (Mobile) state );
		}

		public virtual void DoTeleport( Mobile m )
		{
			Map map = m_MapDest;

			if ( map == null || map == Map.Internal )
				map = m.Map;

			Point3D p = m_PointDest;

			if ( p == Point3D.Zero )
				p = m.Location;

			Server.Mobiles.BaseCreature.TeleportPets( m, p, map );

			bool sendEffect = ( !m.Hidden || m.AccessLevel == AccessLevel.Player );

			if ( m_SourceEffect && sendEffect )
				Effects.SendLocationEffect( m.Location, m.Map, 0x3728, 10, 10 );

			m.MoveToWorld( p, map );

			if ( m_DestEffect && sendEffect )
				Effects.SendLocationEffect( m.Location, m.Map, 0x3728, 10, 10 );

			if ( m_SoundID > 0 && sendEffect )
				Effects.PlaySound( m.Location, m.Map, m_SoundID );
		}

		public override bool OnMoveOver( Mobile m )
		{
			if ( m_Active )
			{
				if ( !m_Creatures && !m.Player )
					return true;
					
				if(!m.Alive && livemobsonly)
				{
					if(showcantpassmsg)m.SendNRMessage("Only the living may pass here!",2);
					return true;
				}
				
				if(m.Alive && deadmobsonly)
				{
					if(showcantpassmsg)m.SendNRMessage("Only the dead may pass here!",2);
					return true;				
				}
				
				if(requiredquest!=null && m is PlayerMobile)
				{
				PlayerMobile pm = m as PlayerMobile;
				Type t = ScriptCompiler.FindTypeByName((string)requiredquest );
					if(t!=null) //requires a quest to enter
					{
				
					QuestSystem qs = pm.Quest as QuestSystem;
					if(qs==null)
					{
					pm.SendMessage(reqquestmsg);
					return true;
					}

						if(qs.GetType() != t)
						{
						if(reqquestmsg!=null)pm.SendMessage(reqquestmsg);
						return true;
						}

					}
				}				

				if(requireditem!=null)
				{
				Type t = ScriptCompiler.FindTypeByName((string)requireditem );
					if(t!=null) //requires an item to enter
					{
						if(m.Backpack==null)
						{
						if(reqitemmsg!=null)m.SendNRMessage(reqitemmsg,1);
						return true;
						}
						int items = m.Backpack.GetAmount( t);
						if(items<1)
						{
						if(reqitemmsg!=null)m.SendNRMessage(reqitemmsg,1);
						return true;
						}
						//if we made it here, we have the item required, now we just need to check if it should be consumed
					
						if(consumereqitem)m.Backpack.ConsumeTotal(t,1 );
					}
				}
				
					
				StartTeleport( m );
				return false;
			}

			return true;
		}

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

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

			writer.Write( (int) 5 ); // version

			writer.Write( (bool) m_SourceEffect );
			writer.Write( (bool) m_DestEffect );
			writer.Write( (TimeSpan) m_Delay );
			writer.WriteEncodedInt( (int) m_SoundID );

			writer.Write( m_Creatures );

			writer.Write( m_Active );
			writer.Write( m_PointDest );
			writer.Write( m_MapDest );
			
			writer.Write(m_livemobsonly);
			writer.Write(m_requireditem);
			writer.Write(m_consumereqitem);		
			writer.Write(m_reqitemmsg);	
			
			writer.Write(m_deadmobsonly);
			writer.Write(m_requiredquest);
			writer.Write(m_reqquestmsg);
			
			writer.Write(m_showcantpassmsg);
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			m_SourceEffect = reader.ReadBool();
			m_DestEffect = reader.ReadBool();
			m_Delay = reader.ReadTimeSpan();
			m_SoundID = reader.ReadEncodedInt();

			m_Creatures = reader.ReadBool();

			m_Active = reader.ReadBool();
			m_PointDest = reader.ReadPoint3D();
			m_MapDest = reader.ReadMap();						

			if(version>2)
			{
				m_livemobsonly=reader.ReadBool();
				m_requireditem=reader.ReadString();
				m_consumereqitem=reader.ReadBool();
				m_reqitemmsg=reader.ReadString();					
			}
			
			if(version>3)
			{
				m_deadmobsonly=reader.ReadBool();
				m_requiredquest=reader.ReadString();
				m_reqquestmsg=reader.ReadString();
			}	

			if(version>4)
			{
				m_showcantpassmsg=reader.ReadBool();
			}
			
		}
	}

	public class SkillTeleporter : Teleporter
	{
		private SkillName m_Skill;
		private double m_Required;
		private string m_MessageString;
		private int m_MessageNumber;

		[CommandProperty( AccessLevel.GameMaster )]
		public SkillName Skill
		{
			get{ return m_Skill; }
			set{ m_Skill = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public double Required
		{
			get{ return m_Required; }
			set{ m_Required = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public string MessageString
		{
			get{ return m_MessageString; }
			set{ m_MessageString = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int MessageNumber
		{
			get{ return m_MessageNumber; }
			set{ m_MessageNumber = value; InvalidateProperties(); }
		}

		private void EndMessageLock( object state )
		{
			((Mobile)state).EndAction( this );
		}

		public override bool OnMoveOver( Mobile m )
		{
			if ( Active )
			{
				if ( !Creatures && !m.Player )
					return true;

				Skill sk = m.Skills[m_Skill];

				if ( sk == null || sk.Base < m_Required )
				{
					if ( m.BeginAction( this ) )
					{
						if ( m_MessageString != null )
							m.Send( new UnicodeMessage( Serial, ItemID, MessageType.Regular, 0x3B2, 3, "ENU", null, m_MessageString ) );
						else if ( m_MessageNumber != 0 )
							m.Send( new MessageLocalized( Serial, ItemID, MessageType.Regular, 0x3B2, 3, m_MessageNumber, null, "" ) );

						Timer.DelayCall( TimeSpan.FromSeconds( 5.0 ), new TimerStateCallback( EndMessageLock ), m );
					}

					return false;
				}

				StartTeleport( m );
				return false;
			}

			return true;
		}

		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );

			int skillIndex = (int)m_Skill;
			string skillName;

			if ( skillIndex >= 0 && skillIndex < SkillInfo.Table.Length )
				skillName = SkillInfo.Table[skillIndex].Name;
			else
				skillName = "(Invalid)";

			list.Add( 1060661, "{0}\t{1:F1}", skillName, m_Required );

			if ( m_MessageString != null )
				list.Add( 1060662, "Message\t{0}", m_MessageString );
			else if ( m_MessageNumber != 0 )
				list.Add( 1060662, "Message\t#{0}", m_MessageNumber );
		}

		[Constructable]
		public SkillTeleporter()
		{
		}

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

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

			writer.Write( (int) 0 ); // version

			writer.Write( (int) m_Skill );
			writer.Write( (double) m_Required );
			writer.Write( (string) m_MessageString );
			writer.Write( (int) m_MessageNumber );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			switch ( version )
			{
				case 0:
				{
					m_Skill = (SkillName)reader.ReadInt();
					m_Required = reader.ReadDouble();
					m_MessageString = reader.ReadString();
					m_MessageNumber = reader.ReadInt();

					break;
				}
			}
		}
	}

	public class KeywordTeleporter : Teleporter
	{
		private string m_Substring;
		private int m_Keyword;
		private int m_Range;

		[CommandProperty( AccessLevel.GameMaster )]
		public string Substring
		{
			get{ return m_Substring; }
			set{ m_Substring = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int Keyword
		{
			get{ return m_Keyword; }
			set{ m_Keyword = value; InvalidateProperties(); }
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public int Range
		{
			get{ return m_Range; }
			set{ m_Range = value; InvalidateProperties(); }
		}

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

		public override void OnSpeech( SpeechEventArgs e )
		{
			if ( !e.Handled && Active )
			{
				Mobile m = e.Mobile;

				if ( !Creatures && !m.Player )
					return;

				if ( !m.InRange( GetWorldLocation(), m_Range ) )
					return;

				bool isMatch = false;

				if ( m_Keyword >= 0 && e.HasKeyword( m_Keyword ) )
					isMatch = true;
				else if ( m_Substring != null && e.Speech.ToLower().IndexOf( m_Substring.ToLower() ) >= 0 )
					isMatch = true;

				if ( !isMatch )
					return;

				e.Handled = true;
				StartTeleport( m );
			}
		}

		public override bool OnMoveOver( Mobile m )
		{
			return true;
		}

		public override void GetProperties( ObjectPropertyList list )
		{
			base.GetProperties( list );

			list.Add( 1060661, "Range\t{0}", m_Range );

			if ( m_Keyword >= 0 )
				list.Add( 1060662, "Keyword\t{0}", m_Keyword );

			if ( m_Substring != null )
				list.Add( 1060663, "Substring\t{0}", m_Substring );
		}

		[Constructable]
		public KeywordTeleporter()
		{
			m_Keyword = -1;
			m_Substring = null;
		}

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

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

			writer.Write( (int) 0 ); // version

			writer.Write( m_Substring );
			writer.Write( m_Keyword );
			writer.Write( m_Range );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			switch ( version )
			{
				case 0:
				{
					m_Substring = reader.ReadString();
					m_Keyword = reader.ReadInt();
					m_Range = reader.ReadInt();

					break;
				}
			}
		}
	}
}