/**********************************************************
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\TheDarkStar.cs
Lines of code: 214
***********************************************************/


using System;
using Server.Misc;
using Server.Network;
using System.Collections;
using Server.Items;
using Server.Targeting;

namespace Server.Mobiles
{
	public class TheDarkStar : basedemitel
	{
			
		private int form=1; //1=dark star 2=black hole

	    private Timer m_Timer;
		
		public override WeaponAbility GetWeaponAbility()
		{
		if(form==1)return null;
			switch ( Utility.Random( 6 ) )
			{
				default:
				case 0: return WeaponAbility.ConcussionBlow;
				case 1: return WeaponAbility.NerveStrike;
				case 2: return WeaponAbility.DoubleStrike;
				case 3: return WeaponAbility.DoubleStrike;
				case 4: return WeaponAbility.DoubleStrike;
				case 5: return WeaponAbility.WhirlwindAttack;
			}
		}
		
		
		//arty stuff ---------------------------------------
		private static Type[] artys = new Type[]
		{
		typeof( TheBlackhole ),typeof( TheBlackholeKryss ),typeof( TheBlackholeMace )
		};
		
		
		
		public override bool OnBeforeDeath()
		{
		if(form==1)
		{
			Effects.PlaySound(this, Map, GetDeathSound());
			Effects.SendLocationEffect( Location, Map, 0x3709, 30, 10, 0x835, 0 );		
		
			form++;
			Name = "a blackhole";
			Body = 272;
			Hue = 1;

			SetStr( 450, 550 );
			SetDex( 500 );
			SetInt( 100, 100 );

			SetHits(75000 );
			SetMana(500000);			

			SetDamage( 15, 25 );

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

			SetResistance( ResistanceType.Physical, 95, 100 );
			SetResistance( ResistanceType.Fire, 75, 85 );
			SetResistance( ResistanceType.Cold, 75, 85 );
			SetResistance( ResistanceType.Poison, 75, 100 );
			SetResistance( ResistanceType.Energy, 50, 60 );

			SetSkill( SkillName.Swords, 100, 120.0 );
			SetSkill( SkillName.Tactics, 100, 120.0 );
			SetSkill( SkillName.Meditation, 500.0, 500.0 );
			SetSkill( SkillName.MagicResist, 800.0 );

			BlackHoleBossVersion weapon = new BlackHoleBossVersion();
			weapon.Hue = 1175;
			weapon.Movable = false;
			AddItem( weapon );

			Fame = 200000;
			Karma = -200000;



			VirtualArmor = 60;
			
			CantWalk=true;
			m_Timer = new TeleportTimer( this );
			m_Timer.Start();
		
		return false;
		}
		
		
		GiveArtifact(artys);
		return base.OnBeforeDeath();
		}		
		
		
		public override void GenerateLoot()
		{
			AddLoot( LootPack.SuperBoss, 4 );
			AddLoot( LootPack.Gems, 4 );
		}	
		
		
		
		[Constructable]
		public TheDarkStar():base( AIType.AI_Super, FightMode.Closest, 0.2, 0.4 )
		{
			Body = 165;
			Name = "The Dark Star";

			SetStr( 500, 500 );
			SetDex( 100 );
			SetInt( 1500, 1500 );

			SetHits( 45000 );
			SetMana(500000);			

			SetDamage( 5, 15 );

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

			SetResistance( ResistanceType.Physical, 35, 40 );
			SetResistance( ResistanceType.Fire, 25, 30 );
			SetResistance( ResistanceType.Cold, 50, 60 );
			SetResistance( ResistanceType.Poison, 25, 35 );
			SetResistance( ResistanceType.Energy, 25, 35 );

			SetSkill( SkillName.Wrestling, 100.0 );
			SetSkill( SkillName.Tactics, 100.0 );
			SetSkill( SkillName.MagicResist, 400.0 );
			SetSkill( SkillName.Magery, 100.0 );
			SetSkill( SkillName.EvalInt, 200.0 );
			SetSkill( SkillName.Meditation, 120.1, 130.0 );

			VirtualArmor = 36;
			Fame = 25000;
			Karma = -20000;

		}

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

		
		
		public override void OnDamage( int amount, Mobile attacker, bool willKill )
		{

		base.OnDamage(amount,attacker,willKill);
		}			
		
		
		
		
		public TheDarkStar( Serial serial ) : base( serial )
		{
		}		

		private class TeleportTimer : Timer
		{
			private Mobile m_Owner;

			private static int[] m_Offsets = new int[]
			{
				-1, -1,
				-1,  0,
				-1,  1,
				0, -1,
				0,  1,
				1, -1,
				1,  0,
				1,  1
			};

			public TeleportTimer( Mobile owner ) : base( TimeSpan.FromSeconds( 1 ), TimeSpan.FromSeconds( 2.0 ) )
			{
				m_Owner = owner;
				Priority=TimerPriority.OneSecond;
			}

			protected override void OnTick()
			{
				if ( m_Owner.Deleted )
				{
					Stop();
					return;
				}

				Map map = m_Owner.Map;

				if ( map == null )
					return;

				if ( 0.25 < Utility.RandomDouble() )
					return;
					
				ArrayList mobs = new ArrayList();

				foreach ( Mobile m in m_Owner.GetMobilesInRange( 16 ) )
				{
					if ( m!=null && m != m_Owner && m_Owner.CanBeHarmful( m ) && m_Owner.CanSee( m ) )
					{
						mobs.Add(m);
					}
				}
			
				for ( int i = 0; i < mobs.Count; ++i )
				{
					if(mobs[i] is Mobile)DoTeleport((Mobile)mobs[i]);	
				}
			}
			
			
			
			private void DoTeleport(Mobile toTeleport)
			{
			if ( toTeleport == null )return;
				
			Map map = toTeleport.Map;
			
					int offset = Utility.Random( 8 ) * 2;

					Point3D to = m_Owner.Location;

					for ( int i = 0; i < m_Offsets.Length; i += 2 )
					{
						int x = m_Owner.X + m_Offsets[(offset + i) % m_Offsets.Length];
						int y = m_Owner.Y + m_Offsets[(offset + i + 1) % m_Offsets.Length];

						if ( map.CanSpawnMobile( x, y, m_Owner.Z ) )
						{
							to = new Point3D( x, y, m_Owner.Z );
							break;
						}
						else
						{
							int z = map.GetAverageZ( x, y );

							if ( map.CanSpawnMobile( x, y, z ) )
							{
								to = new Point3D( x, y, z );
								break;
							}
						}
					}

					Mobile m = toTeleport;

					Point3D from = m.Location;

					m.Location = to;

					Server.Spells.SpellHelper.Turn( m_Owner, toTeleport );
					Server.Spells.SpellHelper.Turn( toTeleport, m_Owner );

					m.ProcessDelta();

					Effects.SendLocationParticles( EffectItem.Create( from, m.Map, EffectItem.DefaultDuration ), 0x3728, 10, 10, 2023 );
					Effects.SendLocationParticles( EffectItem.Create(   to, m.Map, EffectItem.DefaultDuration ), 0x3728, 10, 10, 5023 );

					m.PlaySound( 0x1FE );

					if(toTeleport is PlayerMobile)	m_Owner.Combatant = toTeleport;
							
			}
			

		}
		
		
		

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

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

			int version = reader.ReadInt();
			
			form = reader.ReadInt();
			if(form==2)
			{
			m_Timer = new TeleportTimer( this );
			m_Timer.Start();
			}
		}
	}
}