context: using System;
  using System.Collections.Generic;
  using Server.Commands;
  using Server.Mobiles;
  using Server.Network;
  using Server.Spells;
  
  /*
  this is From me to you, Under no terms, Conditions...   K?  to apply you
  just simply Unpatch/delete, Stick these in, Same location.. Restart
  */
  namespace Server.Engines.Doom
  {
      public class LeverPuzzleController : Item
      {
          public static string[] Msgs = 
          {
              "You are pinned down by the weight of the boulder!!!", // 0
              "A speeding rock hits you in the head!", // 1
              "OUCH!"							// 2
          };
          /* font&hue for above msgs. index matches */
          public static int[][] MsgParams = 
          {
              new int[] { 0x66d, 3 },
              new int[] { 0x66d, 3 },
              new int[] { 0x34, 3 }
          };
          /* World data for items */
          public static int[][] TA = 
          {
              new int[] { 316, 64, 5 },
              /* 3D Coords for levers */ new int[] { 323, 58, 5 },
              new int[] { 332, 63, 5 },
              new int[] { 323, 71, 5 },
              new int[] { 324, 64 },
              /* 2D Coords for standing regions */ new int[] { 316, 65 },
              new int[] { 324, 58 },
              new int[] { 332, 64 },
              new int[] { 323, 72 },
              new int[] { 468, 92, -1 }, new int[] { 0x181D, 0x482 },
              /* 3D coord, itemid+hue for L.R. teles */ new int[] { 469, 92, -1 }, new int[] { 0x1821, 0x3fd },
              new int[] { 470, 92, -1 }, new int[] { 0x1825, 0x66d },
              new int[] { 319, 70, 18 }, new int[] { 0x12d8 },
              /* 3D coord, itemid for statues */ new int[] { 329, 60, 18 }, new int[] { 0x12d9 },
              new int[] { 469, 96, 6 }
          /* 3D Coords for Fake Box */ };
          /* CLILOC data for statue "correct souls" messages */
          public static int[] Statue_Msg = { 1050009, 1050007, 1050008, 1050008 };
          /* Exit & Enter locations for the lamp room */
          public static Point3D lr_Exit = new Point3D(353, 172, -1);
          public static Point3D lr_Enter = new Point3D(467, 96, -1);
          /* "Center" location in puzzle */
          public static Point3D lp_Center = new Point3D(324, 64, -1);
          /* Lamp Room Area */
          public static Rectangle2D lr_Rect = new Rectangle2D(465, 92, 10, 10);
          /* Lamp Room area Poison message data */
          public static int[][] PA = 
          {
              new int[] { 0, 0, 0xA6 },
              new int[] { 1050001, 0x485, 0xAA },
              new int[] { 1050003, 0x485, 0xAC },
              new int[] { 1050056, 0x485, 0xA8 },
              new int[] { 1050057, 0x485, 0xA4 },
              new int[] { 1062091, 0x23F3, 0xAC }
          };
          public static Poison[] PA2 = 
          {
              Poison.Lesser,
              Poison.Regular,
              Poison.Greater,
              Poison.Deadly,
              Poison.Lethal,
              Poison.Lethal
          };
          /* SOUNDS */
          private static readonly int[] fs = { 0x144, 0x154 };
          private static readonly int[] ms = { 0x144, 0x14B };
          private static readonly int[] fs2 = { 0x13F, 0x154 };
          private static readonly int[] ms2 = { 0x13F, 0x14B };
          private static readonly int[] cs1 = { 0x244 };
          private static readonly int[] exp = { 0x307 };
          private static bool installed;
          private bool m_Enabled;
          private UInt16 m_MyKey;
          private UInt16 m_TheirKey;
          private List<Item> m_Levers;
          private List<Item> m_Teles;
          private List<Item> m_Statues;
          private List<LeverPuzzleRegion> m_Tiles;
          private Mobile m_Successful;
          private LampRoomBox m_Box;
          private Region m_LampRoom;
          private Timer m_Timer;
          private Timer l_Timer;
          public LeverPuzzleController()
              : base(0x1822)
          {
              this.Movable = false;
              this.Hue = 0x4c;
              installed = true;
              int i = 0;
  
              this.m_Levers = new List<Item>();	/* codes are 0x1 shifted left x # of bits, easily handled here */
              for (; i < 4; i++)
                  this.m_Levers.Add(AddLeverPuzzlePart(TA[i], new LeverPuzzleLever((ushort)(1 << i), this)));
  
              this.m_Tiles = new List<LeverPuzzleRegion>();
              for (; i < 9; i++)
                  this.m_Tiles.Add(new LeverPuzzleRegion(this, TA[i]));
  
              this.m_Teles = new List<Item>();
              for (; i < 15; i++)
                  this.m_Teles.Add(AddLeverPuzzlePart(TA[i], new LampRoomTeleporter(TA[++i])));
  
              this.m_Statues = new List<Item>();
              for (; i < 19; i++)
                  this.m_Statues.Add(AddLeverPuzzlePart(TA[i], new LeverPuzzleStatue(TA[++i], this)));
  
              if (!installed)
                  this.Delete();
              else
                  this.Enabled = true;
  
              this.m_Box = (LampRoomBox)AddLeverPuzzlePart(TA[i], new LampRoomBox(this));
              this.m_LampRoom = new LampRoomRegion(this);
              this.GenKey();
          }
  
          public LeverPuzzleController(Serial serial)
              : base(serial)
          {
          }
  
          [CommandProperty(AccessLevel.GameMaster)]
          public UInt16 MyKey
          {
              get
              {
                  return this.m_MyKey;
              }
              set
              {
                  this.m_MyKey = value;
              }
          }
          [CommandProperty(AccessLevel.GameMaster)]
          public UInt16 TheirKey
          {
              get
              {
                  return this.m_TheirKey;
              }
              set
              {
                  this.m_TheirKey = value;
              }
          }
          [CommandProperty(AccessLevel.GameMaster)]
          public bool Enabled
          {
              get
              {
                  return this.m_Enabled;
              }
              set
              {
                  this.m_Enabled = value;
              }
          }
          public Mobile Successful
          {
              get
              {
                  return this.m_Successful;
              }
          }
          public bool CircleComplete
          {
              get	/* OSI: all 5 must be occupied */
              {
                  for (int i = 0; i < 5; i++)
                  {
                      if (this.GetOccupant(i) == null)
                      {
                          return false;
                      }
                  }
                  return true;
              }
          }
          public static void Initialize()
          {
              CommandSystem.Register("GenLeverPuzzle", AccessLevel.Administrator, new CommandEventHandler(GenLampPuzzle_OnCommand));
          }
  
          [Usage("GenLeverPuzzle")]
          [Description("Generates lamp room and lever puzzle in doom.")]
          public static void GenLampPuzzle_OnCommand(CommandEventArgs e)
          {
              foreach (Item item in Map.Malas.GetItemsInRange(lp_Center, 0))
              {
                  if (item is LeverPuzzleController)
                  {
                      e.Mobile.SendMessage("Lamp room puzzle already exists: please delete the existing controller first ...");
                      return;
                  }
              }
              e.Mobile.SendMessage("Generating Lamp Room puzzle...");
              new LeverPuzzleController().MoveToWorld(lp_Center, Map.Malas);
  
              if (!installed)
                  e.Mobile.SendMessage("There was a problem generating the puzzle.");
              else
                  e.Mobile.SendMessage("Lamp room puzzle successfully generated.");
          }
  
          public static Item AddLeverPuzzlePart(int[] Loc, Item newitem)
          {
              if (newitem == null || newitem.Deleted)
              {
                  installed = false;
              }
              else
              {
                  newitem.MoveToWorld(new Point3D(Loc[0], Loc[1], Loc[2]), Map.Malas);
              }
              return newitem;
          }
  
          public static void NukeItemList(List<Item> list)
          {
              if (list != null && list.Count != 0)
              {
                  foreach (Item item in list)
                  {
                      if (item != null && !item.Deleted)
                      {
                          item.Delete();
                      }
                  }
              }
          }
  
          public static void MoveMobileOut(Mobile m)
          {
              if (m != null)
              {
                  if (m is PlayerMobile && !m.Alive)
                  {
                      if (m.Corpse != null && !m.Corpse.Deleted)
                      {
                          m.Corpse.MoveToWorld(lr_Exit, Map.Malas);
                      }
                  }
                  BaseCreature.TeleportPets(m, lr_Exit, Map.Malas);
                  m.Location = lr_Exit;
                  m.ProcessDelta();
              }
          }
  
          public static bool AniSafe(Mobile m)
          {
              return (m != null && !TransformationSpellHelper.UnderTransformation(m) && m.BodyMod == 0 && m.Alive);
          }
  
          public static IEntity ZAdjustedIEFromMobile(Mobile m, int ZDelta)
          {
              return new Entity(Serial.Zero_sr, new Point3D(m.X, m.Y, m.Z + ZDelta), m.Map);
          }
  
          public static void DoDamage(Mobile m, int min, int max, bool poison)
          {
              if (m != null && !m.Deleted && m.Alive)
              {
                  int damage = Utility.Random(min, max);
                  AOS.Damage(m, damage, (poison) ? 0 : 100, 0, 0, (poison) ? 100 : 0, 0);
              }
          }
  
          public static Point3D RandomPointIn(Point3D point, int range)
          {
              return RandomPointIn(point.X - range, point.Y - range, range * 2, range * 2, point.Z);
          }
  
          public static Point3D RandomPointIn(Rectangle2D rect, int z)
          {
              return RandomPointIn(rect.X, rect.Y, rect.Height, rect.Width, z);
          }
  
          public static Point3D RandomPointIn(int x, int y, int x2, int y2, int z)
          {
              return new Point3D(Utility.Random(x, x2), Utility.Random(y, y2), z);
          }
  
          public static void PlaySounds(Point3D location, int[] sounds)
          {
              foreach (int soundid in sounds)
                  Effects.PlaySound(location, Map.Malas, soundid);
          }
  
          public static void PlayEffect(IEntity from, IEntity to, int itemid, int speed, bool explodes)
          {
              Effects.SendMovingParticles(from, to, itemid, speed, 0, true, explodes, 2, 0, 0);
          }
  
          public static void SendLocationEffect(IPoint3D p, int itemID, int speed, int duration, int hue)
          {
              Effects.SendPacket(p, Map.Malas, new LocationEffect(p, itemID, speed, duration, hue, 0));
          }
  
          public static void PlayerSendASCII(Mobile player, int index)
          {
              player.Send(new AsciiMessage(Serial.MinusOne_sr, 0xFFFF, MessageType.Label, MsgParams[index][0], MsgParams[index][1], null, Msgs[index]));
          }
  
          /* I cant find any better way to send "speech" using fonts other than default */
          public static void POHMessage(Mobile from, int index) 
          {
              Packet p = new AsciiMessage(from.Serial, from.Body, MessageType.Regular, MsgParams[index][0], MsgParams[index][1], from.Name, Msgs[index]);
              p.Acquire();
              foreach (NetState state in from.Map.GetClientsInRange(from.Location))
                  state.Send(p);
  
              Packet.Release(p);
          }
  
          public override void OnDelete()
          {
              this.KillTimers();
              base.OnDelete();
          }
  
          public override void OnAfterDelete()
          {
              NukeItemList(this.m_Teles);
              NukeItemList(this.m_Statues);
              NukeItemList(this.m_Levers);
  
              if (this.m_LampRoom != null)
              {
                  this.m_LampRoom.Unregister();
              }
              if (this.m_Tiles != null)
              {
                  foreach (Region region in this.m_Tiles)
                  {
                      region.Unregister();
                  }
              }
              if (this.m_Box != null && !this.m_Box.Deleted)
              {
                  this.m_Box.Delete();
              }
          }
  
          public virtual PlayerMobile GetOccupant(int index)
          {
              LeverPuzzleRegion region = (LeverPuzzleRegion)this.m_Tiles[index];
  
  
gt:             if (region != null)