context: using System;
  using System.Collections;
  using Server;
  using Server.Gumps;
  using Server.Multis;
  using Server.Mobiles;
  using Server.Targeting;
  
  namespace Server.Items
  {
  	public class HousePlacementTool : Item
  	{
  		public override int LabelNumber{ get{ return 1060651; } } // a house placement tool
  
  		[Constructable]
  		public HousePlacementTool() : base( 0x14F6 )
  		{
  			Weight = 3.0;
  			LootType = LootType.Blessed;
  		}
  
  		public override void OnDoubleClick( Mobile from )
  		{
  			if ( IsChildOf( from.Backpack ) )
  				from.SendGump( new HousePlacementCategoryGump( from ) );
  			else
  				from.SendLocalizedMessage( 1042001 ); // That must be in your pack for you to use it.
  		}
  
  		public HousePlacementTool( Serial serial ) : base( serial )
  		{
  		}
  
  		public override void Serialize( GenericWriter writer )
  		{
  			base.Serialize( writer );
  
  			writer.Write( (int) 0 ); // version
  		}
  
  		public override void Deserialize( GenericReader reader )
  		{
  			base.Deserialize( reader );
  
  			/*int version = */reader.ReadInt();
  
  			if ( Weight == 0.0 )
  				Weight = 3.0;
  		}
  	}
  
  	public class HousePlacementCategoryGump : Gump
  	{
  		private Mobile m_From;
  
  		private const int LabelColor = 0x7FFF;
  		private const int LabelColorDisabled = 0x4210;
  
  		public HousePlacementCategoryGump( Mobile from ) : base( 50, 50 )
  		{
  			m_From = from;
  
  			from.CloseGump( typeof( HousePlacementCategoryGump ) );
  			from.CloseGump( typeof( HousePlacementListGump ) );
  
  			AddPage( 0 );
  
  			AddBackground( 0, 0, 270, 145, 5054 );
  
  			AddImageTiled( 10, 10, 250, 125, 2624 );
  			AddAlphaRegion( 10, 10, 250, 125 );
  
  			AddHtmlLocalized( 10, 10, 250, 20, 1060239, LabelColor, false, false ); // <CENTER>HOUSE PLACEMENT TOOL</CENTER>
  
  			AddButton( 10, 110, 4017, 4019, 0, GumpButtonType.Reply, 0 );
  			AddHtmlLocalized( 45, 110, 150, 20, 3000363, LabelColor, false, false ); // Close
  
  			AddButton( 10, 40, 4005, 4007, 1, GumpButtonType.Reply, 0 );
  			AddHtmlLocalized( 45, 40, 200, 20, 1060390, LabelColor, false, false ); // Classic Houses
  
  			AddButton( 10, 60, 4005, 4007, 2, GumpButtonType.Reply, 0 );
  			AddHtmlLocalized( 45, 60, 200, 20, 1060391, LabelColor, false, false ); // 2-Story Customizable Houses
  
  			AddButton( 10, 80, 4005, 4007, 3, GumpButtonType.Reply, 0 );
  			AddHtmlLocalized( 45, 80, 200, 20, 1060392, LabelColor, false, false ); // 3-Story Customizable Houses
  		}
  
  		public override void OnResponse( Server.Network.NetState sender, RelayInfo info )
  		{
  			if ( !m_From.CheckAlive() )
  				return;
  
  			switch ( info.ButtonID )
  			{
  				case 1: // Classic Houses
  				{
  					m_From.SendGump( new HousePlacementListGump( m_From, HousePlacementEntry.ClassicHouses ) );
  					break;
  				}
  				case 2: // 2-Story Customizable Houses
  				{
  					m_From.SendGump( new HousePlacementListGump( m_From, HousePlacementEntry.TwoStoryFoundations ) );
  					break;
  				}
  				case 3: // 3-Story Customizable Houses
  				{
  					m_From.SendGump( new HousePlacementListGump( m_From, HousePlacementEntry.ThreeStoryFoundations ) );
  					break;
  				}
  			}
  		}
  	}
  
  	public class HousePlacementListGump : Gump
  	{
  		private Mobile m_From;
  		private HousePlacementEntry[] m_Entries;
  
  		private const int LabelColor = 0x7FFF;
  		private const int LabelHue = 0x480;
  
  		public HousePlacementListGump( Mobile from, HousePlacementEntry[] entries ) : base( 50, 50 )
  		{
  			m_From = from;
  			m_Entries = entries;
  
  			from.CloseGump( typeof( HousePlacementCategoryGump ) );
  			from.CloseGump( typeof( HousePlacementListGump ) );
  
  			AddPage( 0 );
  
  			AddBackground( 0, 0, 520, 420, 5054 );
  
  			AddImageTiled( 10, 10, 500, 20, 2624 );
  			AddAlphaRegion( 10, 10, 500, 20 );
  
  			AddHtmlLocalized( 10, 10, 500, 20, 1060239, LabelColor, false, false ); // <CENTER>HOUSE PLACEMENT TOOL</CENTER>
  
  			AddImageTiled( 10, 40, 500, 20, 2624 );
  			AddAlphaRegion( 10, 40, 500, 20 );
  
  			AddHtmlLocalized( 50, 40, 225, 20, 1060235, LabelColor, false, false ); // House Description
  			AddHtmlLocalized( 275, 40, 75, 20, 1060236, LabelColor, false, false ); // Storage
  			AddHtmlLocalized( 350, 40, 75, 20, 1060237, LabelColor, false, false ); // Lockdowns
  			AddHtmlLocalized( 425, 40, 75, 20, 1060034, LabelColor, false, false ); // Cost
  
  			AddImageTiled( 10, 70, 500, 280, 2624 );
  			AddAlphaRegion( 10, 70, 500, 280 );
  
  			AddImageTiled( 10, 360, 500, 20, 2624 );
  			AddAlphaRegion( 10, 360, 500, 20 );
  
  			AddHtmlLocalized( 10, 360, 250, 20, 1060645, LabelColor, false, false ); // Bank Balance:
  			AddLabel( 250, 360, LabelHue, Banker.GetBalance( from ).ToString() );
  
  			AddImageTiled( 10, 390, 500, 20, 2624 );
  			AddAlphaRegion( 10, 390, 500, 20 );
  
  			AddButton( 10, 390, 4017, 4019, 0, GumpButtonType.Reply, 0 );
  			AddHtmlLocalized( 50, 390, 100, 20, 3000363, LabelColor, false, false ); // Close
  
  			for ( int i = 0; i < entries.Length; ++i )
  			{
  				int page = 1 + (i / 14);
  				int index = i % 14;
  
  				if ( index == 0 )
  				{
  					if ( page > 1 )
  					{
  						AddButton( 450, 390, 4005, 4007, 0, GumpButtonType.Page, page );
  						AddHtmlLocalized( 400, 390, 100, 20, 3000406, LabelColor, false, false ); // Next
  					}
  
  					AddPage( page );
  
  					if ( page > 1 )
  					{
  						AddButton( 200, 390, 4014, 4016, 0, GumpButtonType.Page, page - 1 );
  						AddHtmlLocalized( 250, 390, 100, 20, 3000405, LabelColor, false, false ); // Previous
  					}
  				}
  
  				HousePlacementEntry entry = entries[i];
  
  				int y = 70 + (index * 20);
  
  				AddButton( 10, y, 4005, 4007, 1 + i, GumpButtonType.Reply, 0 );
  				AddHtmlLocalized( 50, y, 225, 20, entry.Description, LabelColor, false, false );
  				AddLabel( 275, y, LabelHue, entry.Storage.ToString() );
  				AddLabel( 350, y, LabelHue, entry.Lockdowns.ToString() );
  				AddLabel( 425, y, LabelHue, entry.Cost.ToString() );
  			}
  		}
  
  		public override void OnResponse( Server.Network.NetState sender, RelayInfo info )
  		{
  			if ( !m_From.CheckAlive() )
  				return;
  
  			int index = info.ButtonID - 1;
  
  			if ( index >= 0 && index < m_Entries.Length )
  			{
  				if ( m_From.AccessLevel < AccessLevel.GameMaster && BaseHouse.HasAccountHouse( m_From ) )
  					m_From.SendLocalizedMessage( 501271 ); // You already own a house, you may not place another!
  				else
  					m_From.Target = new NewHousePlacementTarget( m_Entries, m_Entries[index] );
  			}
  			else
  			{
  				m_From.SendGump( new HousePlacementCategoryGump( m_From ) );
  			}
  		}
  	}
  
  	public class NewHousePlacementTarget : MultiTarget
  	{
  		private HousePlacementEntry m_Entry;
  		private HousePlacementEntry[] m_Entries;
  
  		private bool m_Placed;
  
  		public NewHousePlacementTarget( HousePlacementEntry[] entries, HousePlacementEntry entry ) : base( entry.MultiID, entry.Offset )
  		{
  			Range = 14;
  
  			m_Entries = entries;
  			m_Entry = entry;
  		}
  
  		protected override void OnTarget( Mobile from, object o )
  		{
  			if ( !from.CheckAlive() )
  				return;
  
  			IPoint3D ip = o as IPoint3D;
  
  			if ( ip != null )
  			{
  				if ( ip is Item )
  					ip = ((Item)ip).GetWorldTop();
  
  				Point3D p = new Point3D( ip );
  
  				Region reg = Region.Find( new Point3D( p ), from.Map );
  
  				if ( from.AccessLevel >= AccessLevel.GameMaster || reg.AllowHousing( from, p ) )
  
gt: 					m_Placed = m_Entry.OnPlacement( from, p );