/**********************************************************
RunUO 2.0 AoV C# script file
Official Age of Valor Script :: www.uovalor.com
Last modified by 
Filepath: scripts\_custom\engines\ML Peerless + misc areas\demitel\mobiles\CrystalDaemon.cs
Lines of code: 255
***********************************************************/


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



namespace Server.Mobiles
{
	[CorpseName( "the remains of a crystal daemon" )]
	public class CrystalDaemon : basedemitel
	{
		
		private int Crystalizationcount=0;
		private bool criticaled=false;
		
		public override WeaponAbility GetWeaponAbility()
		{
			switch ( Utility.Random( 6 ) )
			{
				default:
				case 1: return WeaponAbility.CrushingBlow;
				case 2: return WeaponAbility.ConcussionBlow;
				case 3: return WeaponAbility.ParalyzingBlow;
				case 4: return WeaponAbility.BleedAttack;
				case 5: return WeaponAbility.ArmorIgnore;
				case 6: return WeaponAbility.ArmorIgnore;
			}
		}

	    private ArrayList spawns = new ArrayList();		
	
		//arty stuff ---------------------------------------
		private static Type[] artys = new Type[]
		{
		typeof( TunicOfCrystal ),typeof( CSteedSummoner ),typeof( CrystalineBonePiece )
		};
		
		
		
		public override bool OnBeforeDeath()
		{
		GiveArtifact(artys);
		
		return base.OnBeforeDeath();
		}
	
	

		[Constructable]
		public CrystalDaemon() : base( AIType.AI_Super, FightMode.Closest,  0.2, 0.4 )
		{
			Name = "a crystal daemon";
			Body = 308;
			Hue = 1195;
			BaseSoundID = 268;

			SetStr( 2000 );
			SetDex( 126, 145 );
			SetInt( 1000, 1000 );

			SetHits( 100000);
			SetMana(500000);

			SetDamage( 48 );

			SetDamageType( ResistanceType.Physical, 50 );
			SetDamageType( ResistanceType.Cold, 25 );
			SetDamageType( ResistanceType.Energy, 25 );

			SetResistance( ResistanceType.Physical, 15, 30 );
			SetResistance( ResistanceType.Fire, 10, 50 );
			SetResistance( ResistanceType.Cold, 100, 100 );
			SetResistance( ResistanceType.Poison, 50, 60 );
			SetResistance( ResistanceType.Energy, 100, 100 );

			SetSkill( SkillName.MagicResist, 250.0, 250.0 );
			SetSkill( SkillName.Tactics, 80.0, 100.0 );
			SetSkill( SkillName.Wrestling, 120.0, 120.0 );
			SetSkill(SkillName.Magery,1,10);
			
						

			Fame = 22500;
			Karma = -22500;


			VirtualArmor = 38;
			
		}

		public override void GenerateLoot()
		{
			AddLoot( LootPack.SuperBoss, 4 );
			AddLoot( LootPack.Gems, 4 );
		}
		
	/*	public override void OnThink()
		{
		
			int x = this.X;
			int y = this.Y; 
			int z = this.Z;
		
			Map map = this.Map;	
		
			Tile t = map.Tiles.GetLandTile( x, y );
		
			if (this.Combatant != null && this.Combatant.Alive && !this.Combatant.Blessed && (t.ID >= 543 && t.ID <= 560) && this.InLOS( this.Combatant ) && this.Combatant.InRange( this, 15 ))
			{
				Mobile m = this.Combatant;
				m.BoltEffect( 0 );
				AOS.Damage( m, this, 10, true, 100, 0, 0, 0, 0 );
			}		
		}*/
		
		
		private void DoCrystalization(Mobile attacker,bool critical)
		{
		if(attacker==null)attacker=this;
		Map map = attacker.Map;
		
		
		if(0.3>Utility.RandomDouble() && this.InRange(attacker,10) && this.InLOS(attacker))this.MoveToWorld(attacker.Location,Map);		

		int fieldsize=4+Utility.Random(15);
				
			//do chain lightning
			
				ArrayList targets = new ArrayList();

				if ( map != null )
				{
					IPooledEnumerable eable = map.GetMobilesInRange( new Point3D( attacker ), fieldsize );

					foreach ( Mobile m in eable )
					{

							if(this.ShouldHarm(m))	targets.Add( m );

					}

					eable.Free();
				}

				

				if ( targets.Count > 0 )
				{
					
					for ( int i = 0; i < targets.Count; ++i )
					{
						Mobile m = (Mobile)targets[i];
						
						m.SendMessage("The Crystal Daemon strikes with an area crystallization attack!");						

						double toDeal = Utility.Random(Utility.Random(100))+10;
						if(critical)toDeal=Utility.Random(20);

						this.DoHarmful( m );
						AOS.Damage(m, this, (int)toDeal, 0, 0, 0, 0, 100 );		
						
						m.BoltEffect( 0 );
						
						for(int ii=6;ii<10;ii++)
						{
								Item item = new tempcrystal(this);
								item.MoveToWorld( new Point3D(m.X-1+Utility.Random(2),m.Y-1+Utility.Random(2),m.Z), m.Map );						
						}					
					}
				}				
			}
		
		
		private class tempcrystal : Item
		{
			private Timer m_Timer;
			private DateTime m_End;
			private Mobile m_summoner;

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

			public tempcrystal(Mobile summoner)
			{
				Visible = true;
				Movable = false;
				m_summoner=summoner;
				
				Hue = 1195;
				ItemID=730;
				Name="Shattering Crystal";
			
					switch(Utility.Random(9))
					{
					case 0: ItemID= 480+Utility.Random(11);break;
					case 1: ItemID= 441;break;
					case 2: ItemID= 730;break;
					case 3: ItemID= 3159+Utility.Random(4);break;
					case 4: ItemID= 3340;break;
					case 5: ItemID= 3392;break;
					case 6: ItemID= 6001+Utility.Random(5);break;
					case 7: ItemID= 8710+Utility.Random(8);break;
					case 8: ItemID= 12252+Utility.Random(2);break;
					}


				m_Timer = new InternalTimer( this, TimeSpan.FromSeconds( 10 + Utility.Random(0,300) ),summoner );
				m_Timer.Start();

				m_End = DateTime.Now + TimeSpan.FromSeconds( 30 + Utility.Random(0,60) );
			}
			
			
			public override bool OnMoveOver( Mobile m )
			{
			return false;
			}
			
			public void Break()
			{
					if(m_summoner!=null && m_summoner.Alive)
					{			
						Map map = this.Map;				
						//chance to summon a crystal elemental when it breaks
						if(0.1>Utility.RandomDouble())
						{
						BaseCreature summon = new CrystalElemental();
						summon.Tamable=false;
						summon.Hue = 1195;
						
						BaseCreature.SetTempSummon(m_summoner,summon );
									
						summon.MoveToWorld( new Point3D( this.X, this.Y, this.Z ), map );
						}
						else if(0.025>Utility.RandomDouble())//... or a crystal dragon O_o
						{
						BaseCreature summon = new CrystalDragon();
						summon.Tamable=false;
						summon.Hue = 1195;
						BaseCreature.SetTempSummon(m_summoner,summon );
									
						summon.MoveToWorld( new Point3D( this.X, this.Y, this.Z ), map );				
						}
					}
				
				Effects.SendLocationParticles( EffectItem.Create( this.Location, this.Map, EffectItem.DefaultDuration ), 0x3728, 8, 20, 5042 );
				Effects.PlaySound( this, this.Map, 1390 );
				this.Delete();			
			}	
			public override bool CanBeTriggered{get{return true;} }
			
			public override bool OnTrigger(Mobile from,Item itemfrom,TriggerType type)
			{
				if(type == TriggerType.Impact && 0.3>Utility.RandomDouble())
				{
				Break();
				return true;
				}
				else if(type == TriggerType.Explosion && 0.2>Utility.RandomDouble())
				{
				Break();
				return true;
				}				
			
			return false;
			}
			
			
			

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

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

				//writer.WriteDeltaTime( m_End );
			}

			public override void Deserialize( GenericReader reader )
			{
				base.Deserialize( reader );
				int version = reader.ReadInt();			
				
			this.Delete();   //lets just do a cleanup if server reboots, instead of starting all these timers	
			}

			public override void OnAfterDelete()
			{
				base.OnAfterDelete();

				if ( m_Timer != null )
					m_Timer.Stop();
			}

			private class InternalTimer : Timer
			{
				private tempcrystal m_Item;
				private Mobile m_summoner;

				public InternalTimer( tempcrystal item, TimeSpan duration, Mobile summoner  ) : base( duration )
				{
					Priority = TimerPriority.OneSecond;
					m_Item = item;
					m_summoner = summoner;
				}

				protected override void OnTick()
				{
				m_Item.Break();				
				Stop();
				}
			}
		}		
		
		
		
		
		
		
		
		public override void OnDamage( int amount, Mobile attacker, bool willKill )
		{
		base.OnDamage(amount,attacker,willKill);
		if(attacker==null || attacker==this)return;
		
			if(0.05>Utility.RandomDouble() || (Crystalizationcount>0 && 0.3>Utility.RandomDouble()))
			{
			if(Crystalizationcount>0)Crystalizationcount--;
			else Crystalizationcount=Utility.Random(5);
			
			DoCrystalization(attacker,false);		
			}
			
			
			//critical hit
			if(Hits<5000 && 0.01>Utility.RandomDouble() && !criticaled)
			{
			criticaled=true;
			
			attacker.SendMessage("The Crystal Daemon strikes with a critical attack!");
			
			for(int i =0; i<Utility.Random(4)+3;i++)DoCrystalization(attacker,true);	
			}
			
		}
		

		public override bool BleedImmune{ get{ return true; } }
		public override int TreasureMapLevel{ get{ return 5; } }

		public override void CheckReflect( Mobile caster, ref bool reflect )
		{
			reflect = true; // Every spell is reflected back to the caster
		}

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

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

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