/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by Red Squirrel on Jan-02-2010 02:31:32pm
Filepath: scripts\Items\Containers\BaseTreasureChest.cs
Lines of code: 367
***********************************************************/


using Server;
using Server.Items;
using Server.Network;
using System;
using System.Collections;
using Server.Engines.CannedEvil;
using System.Collections.Generic;
using Server.Targeting;

namespace Server.Items
{
	public class BaseTreasureChest : LockableContainer
	{
		private TreasureLevel m_TreasureLevel;
		private short m_MaxSpawnTime = 60;
		private short m_MinSpawnTime = 10;
		private TreasureResetTimer m_ResetTimer;
		public override bool BlockMageryUnlock{ get{ return true; } }
		public virtual bool DeleteOnReset {get{return false;}}
		public DateTime m_spawneddate;		
		public override bool CanBeTriggered{get{return true;} }		

		[CommandProperty( AccessLevel.GameMaster )]
		public TreasureLevel Level
		{
			get
			{
				return m_TreasureLevel;
			}
			set
			{
				m_TreasureLevel = value;Modify();
			}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public short MaxSpawnTime
		{
			get
			{
				return m_MaxSpawnTime;
			}
			set
			{
				m_MaxSpawnTime = value;
				if(m_MaxSpawnTime<m_MinSpawnTime)m_MaxSpawnTime=m_MinSpawnTime;
				Modify();
			}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public short MinSpawnTime
		{
			get
			{
				return m_MinSpawnTime;
			}
			set
			{
				m_MinSpawnTime = value;Modify();
			}
		}

		[CommandProperty( AccessLevel.GameMaster )]
		public DateTime SpawnedDate
		{
			get
			{
				return m_spawneddate;
			}
			set
			{
				m_spawneddate = value;Modify();
			}
		}		
		
		
		
		public override bool IsDecoContainer
		{
			get{ return false; }
		}

		public BaseTreasureChest( int itemID ) : this( itemID, (TreasureLevel)(1+Utility.Random(3)) )
		{
		}

		public BaseTreasureChest( int itemID, TreasureLevel level ) : base( itemID )
		{
			m_TreasureLevel = level;
			Locked = true;
			Movable = false;

			SetLockLevel();
			GenerateTreasure();
			
			Visible=false;		

			m_spawneddate=DateTime.Now;
		}
		
		
		public override void OnAfterSpawn()
		{
			CheckLocation();
			if(!this.Deleted)Visible=true;
		}
		
		
		public override bool OnTrigger(Mobile from,Item itemfrom,TriggerType type)
		{
			if((type==TriggerType.Explosion || type==TriggerType.Impact) && 0.1>Utility.RandomDouble())
			{
				Effects.SendLocationEffect( Location, Map, 0x36BD, 15, 10 );
				
				Destroy(0.75);				
				return true;
			}
		return false;
		}
		
		
		//deletes the chest if it did not spawn in a legal location
		public void CheckLocation()
		{
			bool invalid=false;
			foreach( object o in this.GetObjectsInRange( 2 ) )
			{		
				if (o is Moongate || o is Teleporter || o is BaseDoor)
				{
					invalid=true;
					break;
				}
			}
			

			
			for(int x=-1;x<=1;x++)
			{
				for(int y=-1;y<=1;y++)
				{
					int tx=X+x, ty=Y+y;
					int avz = Map.GetAverageZ( tx, ty );
				
					if(((avz-2)>Z)||(((avz+2)<Z)))
					{
						invalid=true;
						break;
					}			
				}
			}

			if(invalid)Delete();			
		}
		

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

		public override string DefaultName
		{
			get
			{
				if ( this.Locked )
					return "a locked treasure chest";

				return "a treasure chest";
			}
		}

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

			writer.Write( (int) 1 );
			writer.Write( (byte) m_TreasureLevel );
			writer.Write( m_MinSpawnTime );
			writer.Write( m_MaxSpawnTime );
			writer.Write(m_spawneddate);
		}

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

			int version = reader.ReadInt();

			m_TreasureLevel = (TreasureLevel)reader.ReadByte();
			m_MinSpawnTime = reader.ReadShort();
			m_MaxSpawnTime = reader.ReadShort();
			if(version>=1)m_spawneddate=reader.ReadDateTime();
			
			if((m_spawneddate+TimeSpan.FromDays(3))<DateTime.Now)this.Delete();

			if( !Locked )
				StartResetTimer();
		}

		protected virtual void SetLockLevel()
		{
			switch( m_TreasureLevel )
			{
				case TreasureLevel.Level1:
					this.RequiredSkill = this.LockLevel = 5;
					break;

				case TreasureLevel.Level2:
					this.RequiredSkill = this.LockLevel = 20;
					break;

				case TreasureLevel.Level3:
					this.RequiredSkill = this.LockLevel = 50;
					break;

				case TreasureLevel.Level4:
					this.RequiredSkill = this.LockLevel = 70;
					break;

				case TreasureLevel.Level5:
					this.RequiredSkill = this.LockLevel = 90;
					break;

				case TreasureLevel.Level6:
					this.RequiredSkill = this.LockLevel = 100;
					break;
			}
		}

		private void StartResetTimer()
		{
		if(MinSpawnTime==0)return;		
		
			if( m_ResetTimer == null )
				m_ResetTimer = new TreasureResetTimer( this );
			else
				m_ResetTimer.Delay = TimeSpan.FromMinutes(  m_MinSpawnTime + Utility.Random(m_MaxSpawnTime - m_MinSpawnTime));

			m_ResetTimer.Start();
		}

		protected virtual void GenerateTreasure()
		{
			int MinGold = 10;
			int MaxGold = 20;

			switch( m_TreasureLevel )
			{
				case TreasureLevel.Level1:
					MinGold = 100;
					MaxGold = 300;
					
					if(0.05>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(new BagOfReagents(20+Utility.Random(91)));
					if(0.1>Utility.RandomDouble())DropItem(new BagOfNecroReagents(20+Utility.Random(91)));				
					break;

				case TreasureLevel.Level2:
					MinGold = 300;
					MaxGold = 600;
					if(0.1>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(new BagOfReagents(60+Utility.Random(91)));
					if(0.1>Utility.RandomDouble())DropItem(new BagOfNecroReagents(60+Utility.Random(91)));					
					break;

				case TreasureLevel.Level3:
					MinGold = 600;
					MaxGold = 900;
					if(0.15>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.05>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(new BagOfReagents(100+Utility.Random(91)));
					if(0.1>Utility.RandomDouble())DropItem(new BagOfNecroReagents(100+Utility.Random(91)));					
					if(0.01>Utility.RandomDouble())DropItem(new BagOfCommonMPResources(2));
					break;

				case TreasureLevel.Level4:
					MinGold = 900;
					MaxGold = 1200;
					if(0.2>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(new BagOfReagents(100+Utility.Random(91)));
					if(0.1>Utility.RandomDouble())DropItem(new BagOfNecroReagents(100+Utility.Random(91)));		
					if(0.25>Utility.RandomDouble())DropItem(new RandomTalisman());
					if(0.015>Utility.RandomDouble())DropItem(new BagOfCommonMPResources(2));
					break;

				case TreasureLevel.Level5:
					MinGold = 1200;
					MaxGold = 5000;
					if(0.25>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.2>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(new BagOfReagents(100+Utility.Random(91)));
					if(0.1>Utility.RandomDouble())DropItem(new BagOfNecroReagents(100+Utility.Random(91)));	
					if(0.5>Utility.RandomDouble())DropItem(new RandomTalisman());
					if(0.05>Utility.RandomDouble())DropItem(new BagOfCommonMPResources(3));
					if(0.01>Utility.RandomDouble())DropItem(new BagOfUncommonMPResources(2));
					if(0.02>Utility.RandomDouble())DropItem(new HoodedShroudOfShadows());	
					if(0.02>Utility.RandomDouble())DropItem(new LuckyNecklace());	
					if (0.02>Utility.RandomDouble())
					{
						switch (Utility.RandomMinMax(0, 2 ))
						{
							case 0: DropItem(Loot.RandomMPWeapon(70,120, 0));break;
							case 1: DropItem(Loot.RandomMPWeapon(70,120, 1));break;
							case 2: DropItem(Loot.RandomMPWeapon(70,120, 2));break;      		
						}
					}
					break;

				case TreasureLevel.Level6:
					MinGold = 5000;
					MaxGold = 9000;
					if(0.3>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.2>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(RandomWand.CreateTChestWand((int)m_TreasureLevel));	
					if(0.1>Utility.RandomDouble())DropItem(new BagOfReagents(100+Utility.Random(91)));
					if(0.1>Utility.RandomDouble())DropItem(new BagOfNecroReagents(100+Utility.Random(91)));	
					DropItem(new RandomTalisman());
					if(0.1>Utility.RandomDouble())DropItem(new BagOfCommonMPResources(3));
					if(0.02>Utility.RandomDouble())DropItem(new BagOfUncommonMPResources(3));
					if(0.02>Utility.RandomDouble())DropItem(new HoodedShroudOfShadows());	
					if(0.02>Utility.RandomDouble())DropItem(new LuckyNecklace());	
					if (0.05>Utility.RandomDouble())
					{
						switch (Utility.RandomMinMax(0, 2 ))
						{
							case 0: DropItem(Loot.RandomMPWeapon(70,120, 0));break;
							case 1: DropItem(Loot.RandomMPWeapon(70,120, 1));break;
							case 2: DropItem(Loot.RandomMPWeapon(70,120, 2));break;      		
						}
					}
					if (0.03>Utility.RandomDouble())
					{
						switch ( Utility.Random( 6 ))
						{                                   
							case 0: DropItem( new ThiefsArms() ); break;
							case 1: DropItem( new ThiefsChest() ); break;
							case 2: DropItem( new ThiefsGloves () ); break;
							case 3: DropItem( new ThiefsGorget () ); break;       
							case 4: DropItem( new ThiefsBandana () ); break;		
							case 5: DropItem( new ThiefsLegs () ); break;				
						}
					}
					break;
			}

			DropItem( new Gold( MinGold, MaxGold ) );
			
					
			
			//generate loot
			for(int i=0;i<(int)((int)Level*Utility.Random(8));i++)
			{		
			Item item = Loot.RandomArmorOrShieldOrJewelry(0);
			
			int attributeCount=1+(int)Level;
			if(attributeCount<1)attributeCount=1;
			if(attributeCount>5)attributeCount=5;
			
			int min=15+Utility.Random((int)Level*5);
			int max=50+min+Utility.Random(50+((int)Level*5));
			if(max>100)max=100;
			
				if ( item is BaseWeapon )
				{
					BaseWeapon weapon = (BaseWeapon)item;

				BaseRunicTool.ApplyAttributesTo( weapon,true,0, attributeCount, min, max );
				}
				else if ( item is BaseArmor )
				{
					BaseArmor armor = (BaseArmor)item;

				BaseRunicTool.ApplyAttributesTo( armor,true,0, attributeCount, min, max );
				}
				else if ( item is BaseJewel )
				{
					BaseRunicTool.ApplyAttributesTo( (BaseJewel)item,true,0, attributeCount, min, max );
				}
				
		DropItem( item );
		}			
			
			
		}

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

			StartResetTimer();
		}

		public void ClearContents()
		{
			for ( int i = Items.Count - 1; i >= 0; --i )
			{
				if ( i < Items.Count )
					Items[i].Delete();
			}
		}

		public void Reset()
		{
			if( m_ResetTimer != null )
			{
				if( m_ResetTimer.Running )
					m_ResetTimer.Stop();
			}
			
			if(DeleteOnReset)this.Delete();

			Locked = true;
			ClearContents();
			GenerateTreasure();
		}

		public enum TreasureLevel
		{
			Level1, 
			Level2, 
			Level3, 
			Level4, 
			Level5,
			Level6,
		}; 

		private class TreasureResetTimer : Timer
		{
			private BaseTreasureChest m_Chest;

			public TreasureResetTimer( BaseTreasureChest chest ) : base ( TimeSpan.FromMinutes( Utility.Random( chest.MinSpawnTime, chest.MaxSpawnTime ) ) )
			{
				m_Chest = chest;
				Priority = TimerPriority.OneMinute;
			}

			protected override void OnTick()
			{
				m_Chest.Reset();
			}
		}
	}
}ÿ