context: using Server.Items;
  using Server.Mobiles;
  using System;
  using System.Collections.Generic;
  
  namespace Server.Movement
  {
      public class MovementImpl : IMovementImpl
      {
          private const int PersonHeight = 16;
          private const int StepHeight = 2;
  
          private const TileFlag ImpassableSurface = TileFlag.Impassable | TileFlag.Surface;
  
          private static Point3D m_Goal;
          public static Point3D Goal
          {
              get
              {
                  return m_Goal;
              }
              set
              {
                  m_Goal = value;
              }
          }
  
          public static void Configure()
          {
              Movement.Impl = new MovementImpl();
          }
  
          private MovementImpl()
          {
          }
  
          private bool IsOk(Mobile m, bool ignoreDoors, bool ignoreSpellFields, int ourZ, int ourTop, StaticTile[] tiles, List<Item> items)
          {
              for (int i = 0; i < tiles.Length; ++i)
              {
                  StaticTile check = tiles[i];
                  ItemData itemData = TileData.ItemTable[check.ID & TileData.MaxItemValue];
  
                  if ((itemData.Flags & ImpassableSurface) != 0) // Impassable || Surface
                  {
                      int checkZ = check.Z;
                      int checkTop = checkZ + itemData.CalcHeight;
  
                      if (checkTop > ourZ && ourTop > checkZ)
                          return false;
                  }
              }
  
              for (int i = 0; i < items.Count; ++i)
              {
                  Item item = items[i];
                  int itemID = item.ItemID & TileData.MaxItemValue;
                  ItemData itemData = TileData.ItemTable[itemID];
                  TileFlag flags = itemData.Flags;
  
                  if ((flags & ImpassableSurface) != 0) // Impassable || Surface
                  {
                      if (ignoreDoors && ((flags & TileFlag.Door) != 0 || itemID == 0x692 || itemID == 0x846 || itemID == 0x873 || (itemID >= 0x6F5 && itemID <= 0x6F6)))
                      {
                          if (item is BaseHouseDoor && m != null && !((BaseHouseDoor)item).CheckAccess(m))
                          {
                              return false;
                          }
                          else
                          {
                              continue;
                          }
                      }
  
                      if (ignoreSpellFields && (itemID == 0x82 || itemID == 0x3946 || itemID == 0x3956))
                      {
                          continue;
                      }
  
                      // hidden containers, per EA
                      if ((flags & TileFlag.Container) != 0 && !item.Visible)
                      {
                          continue;
                      }
  
                      int checkZ = item.Z;
                      int checkTop = checkZ + itemData.CalcHeight;
  
                      if (checkTop > ourZ && ourTop > checkZ)
                          return false;
                  }
              }
  
              return true;
          }
  
          private readonly List<Item>[] m_Pools = new List<Item>[4]
          {
              new List<Item>(), new List<Item>(),
              new List<Item>(), new List<Item>(),
          };
  
          private readonly List<Mobile>[] m_MobPools = new List<Mobile>[3]
          {
              new List<Mobile>(), new List<Mobile>(),
              new List<Mobile>(),
          };
  
          private readonly List<Sector> m_Sectors = new List<Sector>();
  
          private bool Check(Map map, IPoint3D p, List<Item> items, List<Mobile> mobiles, int x, int y, int startTop, int startZ, bool canSwim, bool cantWalk, out int newZ)
          {
              newZ = 0;
              Mobile m = p as Mobile;
  
              StaticTile[] tiles = map.Tiles.GetStaticTiles(x, y, true);
              LandTile landTile = map.Tiles.GetLandTile(x, y);
  
              bool landBlocks = (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Impassable) != 0;
              bool considerLand = !landTile.Ignored;
  
              if (landBlocks && canSwim && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) != 0)	//Impassable, Can Swim, and Is water.  Don't block it.
                  landBlocks = false;
              else if (cantWalk && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) == 0)	//Can't walk and it's not water
                  landBlocks = true;
  
              int landZ = 0, landCenter = 0, landTop = 0;
  
              map.GetAverageZ(x, y, ref landZ, ref landCenter, ref landTop);
  
              bool moveIsOk = false;
  
              int stepTop = startTop + StepHeight;
              int checkTop = startZ + PersonHeight;
  
              bool ignoreDoors = AlwaysIgnoreDoors(p);
              bool ignoreSpellFields = m is PlayerMobile && map != Map.Felucca;
  
              #region Tiles
              for (int i = 0; i < tiles.Length; ++i)
              {
                  StaticTile tile = tiles[i];
                  ItemData itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
                  TileFlag flags = itemData.Flags;
  
                  #region SA
                  if (m != null && m.Flying && (itemData.Name == "hover over" || (flags & TileFlag.HoverOver) != 0))
                  {
                      newZ = tile.Z;
                      return true;
                  }
                  else if (m is StygianDragon && map == Map.TerMur)
                  {
                      if (x >= 307 && x <= 354 && y >= 126 && y <= 192)
                      {
                          if (tile.Z > newZ)
                              newZ = tile.Z;
  
                          moveIsOk = true;
                      }
                      else if (x >= 42 && x <= 89)
                      {
                          if ((y >= 333 && y <= 399) || (y >= 531 && y <= 597) || (y >= 739 && y <= 805))
                          {
                              if (tile.Z > newZ)
                                  newZ = tile.Z;
  
                              moveIsOk = true;
                          }
                      }
                  }
                  #endregion
  
                  if ((flags & ImpassableSurface) == TileFlag.Surface || (canSwim && (flags & TileFlag.Wet) != 0)) // Surface && !Impassable
                  {
                      if (cantWalk && (flags & TileFlag.Wet) == 0)
                          continue;
  
                      int itemZ = tile.Z;
                      int itemTop = itemZ;
                      int ourZ = itemZ + itemData.CalcHeight;
                      int ourTop = ourZ + PersonHeight;
                      int testTop = checkTop;
  
                      if (moveIsOk)
                      {
                          int cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
  
                          if (cmp > 0 || (cmp == 0 && ourZ > newZ))
                              continue;
                      }
  
                      if (ourZ + PersonHeight > testTop)
                          testTop = ourZ + PersonHeight;
  
                      if (!itemData.Bridge)
                          itemTop += itemData.Height;
  
                      if (stepTop >= itemTop)
                      {
                          int landCheck = itemZ;
  
                          if (itemData.Height >= StepHeight)
                              landCheck += StepHeight;
                          else
                              landCheck += itemData.Height;
  
                          if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
                              continue;
  
                          if (IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
                          {
                              newZ = ourZ;
                              moveIsOk = true;
                          }
                      }
                  }
              }
              #endregion
  
              #region Items
              for (int i = 0; i < items.Count; ++i)
              {
                  Item item = items[i];
                  ItemData itemData = item.ItemData;
                  TileFlag flags = itemData.Flags;
  
                  #region SA
                  if (m != null && m.Flying && (itemData.Name == "hover over" || (flags & TileFlag.HoverOver) != 0))
                  {
                      newZ = item.Z;
                      return true;
                  }
                  #endregion
                  if (!item.Movable && ((flags & ImpassableSurface) == TileFlag.Surface || (m != null && m.CanSwim && (flags & TileFlag.Wet) != 0))) // Surface && !Impassable && !Movable
                  {
                      if (cantWalk && (flags & TileFlag.Wet) == 0)
                          continue;
  
                      int itemZ = item.Z;
                      int itemTop = itemZ;
                      int ourZ = itemZ + itemData.CalcHeight;
                      int ourTop = ourZ + PersonHeight;
                      int testTop = checkTop;
  
                      if (moveIsOk)
                      {
                          int cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
  
                          if (cmp > 0 || (cmp == 0 && ourZ > newZ))
                              continue;
                      }
  
                      if (ourZ + PersonHeight > testTop)
                          testTop = ourZ + PersonHeight;
  
                      if (!itemData.Bridge)
                          itemTop += itemData.Height;
  
                      if (stepTop >= itemTop)
                      {
                          int landCheck = itemZ;
  
                          if (itemData.Height >= StepHeight)
                              landCheck += StepHeight;
                          else
                              landCheck += itemData.Height;
  
                          if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
                              continue;
  
                          if (IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
                          {
                              newZ = ourZ;
                              moveIsOk = true;
                          }
                      }
                  }
              }
              #endregion
  
              if (considerLand && !landBlocks && stepTop >= landZ)
              {
                  int ourZ = landCenter;
                  int ourTop = ourZ + PersonHeight;
                  int testTop = checkTop;
  
                  if (ourZ + PersonHeight > testTop)
  
gt:                     testTop = ourZ + PersonHeight;