context: using System;
  using System.Collections.Generic;
  using Server.Items;
  using Server.Mobiles;
  
  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 bool m_AlwaysIgnoreDoors;
          private static bool m_IgnoreMovableImpassables;
          private static bool m_IgnoreSpellFields;
          private static Point3D m_Goal;
  
          public static bool AlwaysIgnoreDoors
          {
              get
              {
                  return m_AlwaysIgnoreDoors;
              }
              set
              {
                  m_AlwaysIgnoreDoors = value;
              }
          }
          public static bool IgnoreMovableImpassables
          {
              get
              {
                  return m_IgnoreMovableImpassables;
              }
              set
              {
                  m_IgnoreMovableImpassables = value;
              }
          }
          public static bool IgnoreSpellFields
          {
              get
              {
                  return m_IgnoreSpellFields;
              }
              set
              {
                  m_IgnoreSpellFields = value;
              }
          }
          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(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)))
                          continue;
  
                      if (ignoreSpellFields && (itemID == 0x82 || itemID == 0x3946 || itemID == 0x3956))
                          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, Mobile m, List<Item> items, List<Mobile> mobiles, int x, int y, int startTop, int startZ, bool canSwim, bool cantWalk, out int newZ)
          {
              newZ = 0;
  
              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 = (m_AlwaysIgnoreDoors || !m.Alive || m.Body.BodyID == 0x3DB || m.IsDeadBondedPet);
              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];
  				#region SA
                  if (m.Flying && (itemData.Name == "hover over"))
                  {
                      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
  
                  TileFlag flags = itemData.Flags;
  
                  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 - m.Z) - Math.Abs(newZ - m.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 (this.IsOk(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.Flying && (itemData.Name == "hover over"))
                  {
                      newZ = item.Z;
                      return true;
                  }
  				#endregion
                  if (!item.Movable && ((flags & ImpassableSurface) == TileFlag.Surface || (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 - m.Z) - Math.Abs(newZ - m.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 (this.IsOk(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)
                      testTop = ourZ + PersonHeight;
  
                  bool shouldCheck = true;
  
                  if (moveIsOk)
                  {
                      int cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
  
                      if (cmp > 0 || (cmp == 0 && ourZ > newZ))
                          shouldCheck = false;
                  }
  
                  if (shouldCheck && this.IsOk(ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
                  {
                      newZ = ourZ;
                      moveIsOk = true;
                  }
              }
  
              #region Mobiles
              if (moveIsOk)
              {
                  for (int i = 0; moveIsOk && i < mobiles.Count; ++i)
                  {
                      Mobile mob = mobiles[i];
  
                      if (mob != m && (mob.Z + 15) > newZ && (newZ + 15) > mob.Z && !this.CanMoveOver(m, mob))
                          moveIsOk = false;
                  }
              }
              #endregion
  
              return moveIsOk;
          }
  
          private bool CanMoveOver(Mobile m, Mobile t)
          {
              return (!t.Alive || !m.Alive || t.IsDeadBondedPet || m.IsDeadBondedPet) || (t.Hidden && t.IsStaff());
          }
  
          public bool CheckMovement(Mobile m, Map map, Point3D loc, Direction d, out int newZ)
          {
              if (map == null || map == Map.Internal)
              {
                  newZ = 0;
                  return false;
              }
  
              int xStart = loc.X;
              int yStart = loc.Y;
              int xForward = xStart, yForward = yStart;
              int xRight = xStart, yRight = yStart;
              int xLeft = xStart, yLeft = yStart;
  
              bool checkDiagonals = ((int)d & 0x1) == 0x1;
  
              this.Offset(d, ref xForward, ref yForward);
              this.Offset((Direction)(((int)d - 1) & 0x7), ref xLeft, ref yLeft);
              this.Offset((Direction)(((int)d + 1) & 0x7), ref xRight, ref yRight);
  
              if (xForward < 0 || yForward < 0 || xForward >= map.Width || yForward >= map.Height)
              {
                  newZ = 0;
                  return false;
              }
  
              int startZ, startTop;
  
              List<Item> itemsStart = this.m_Pools[0];
              List<Item> itemsForward = this.m_Pools[1];
              List<Item> itemsLeft = this.m_Pools[2];
              List<Item> itemsRight = this.m_Pools[3];
  
              bool ignoreMovableImpassables = m_IgnoreMovableImpassables;
              TileFlag reqFlags = ImpassableSurface;
  
              if (m.CanSwim)
                  reqFlags |= TileFlag.Wet;
  
              List<Mobile> mobsForward = this.m_MobPools[0];
              List<Mobile> mobsLeft = this.m_MobPools[1];
              List<Mobile> mobsRight = this.m_MobPools[2];
  
              bool checkMobs = (m is BaseCreature && !((BaseCreature)m).Controlled && (xForward != m_Goal.X || yForward != m_Goal.Y));
  
              if (checkDiagonals)
              {
                  Sector sectorStart = map.GetSector(xStart, yStart);
                  Sector sectorForward = map.GetSector(xForward, yForward);
                  Sector sectorLeft = map.GetSector(xLeft, yLeft);
                  Sector sectorRight = map.GetSector(xRight, yRight);
  
                  List<Sector> sectors = this.m_Sectors;
  
                  sectors.Add(sectorStart);
  
                  if (!sectors.Contains(sectorForward))
                      sectors.Add(sectorForward);
  
                  if (!sectors.Contains(sectorLeft))
                      sectors.Add(sectorLeft);
  
                  if (!sectors.Contains(sectorRight))
                      sectors.Add(sectorRight);
  
                  for (int i = 0; i < sectors.Count; ++i)
                  {
                      Sector sector = sectors[i];
  
                      for (int j = 0; j < sector.Items.Count; ++j)
                      {
                          Item item = sector.Items[j];
  
                          if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
                              continue;
  
                          if ((item.ItemData.Flags & reqFlags) == 0)
                              continue;
  
                          if (sector == sectorStart && item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsStart.Add(item);
                          else if (sector == sectorForward && item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsForward.Add(item);
                          else if (sector == sectorLeft && item.AtWorldPoint(xLeft, yLeft) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsLeft.Add(item);
                          else if (sector == sectorRight && item.AtWorldPoint(xRight, yRight) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsRight.Add(item);
                      }
  
                      if (checkMobs)
                      {
                          for (int j = 0; j < sector.Mobiles.Count; ++j)
                          {
                              Mobile mob = sector.Mobiles[j];
  
                              if (sector == sectorForward && mob.X == xForward && mob.Y == yForward)
                                  mobsForward.Add(mob);
                              else if (sector == sectorLeft && mob.X == xLeft && mob.Y == yLeft)
                                  mobsLeft.Add(mob);
                              else if (sector == sectorRight && mob.X == xRight && mob.Y == yRight)
                                  mobsRight.Add(mob);
                          }
                      }
                  }
  
                  if (this.m_Sectors.Count > 0)
                      this.m_Sectors.Clear();
              }
              else
              {
                  Sector sectorStart = map.GetSector(xStart, yStart);
                  Sector sectorForward = map.GetSector(xForward, yForward);
  
                  if (sectorStart == sectorForward)
                  {
                      for (int i = 0; i < sectorStart.Items.Count; ++i)
                      {
                          Item item = sectorStart.Items[i];
  
                          if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
                              continue;
  
                          if ((item.ItemData.Flags & reqFlags) == 0)
                              continue;
  
                          if (item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsStart.Add(item);
                          else if (item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsForward.Add(item);
                      }
                  }
                  else
                  {
                      for (int i = 0; i < sectorForward.Items.Count; ++i)
                      {
                          Item item = sectorForward.Items[i];
  
                          if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
                              continue;
  
                          if ((item.ItemData.Flags & reqFlags) == 0)
                              continue;
  
                          if (item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsForward.Add(item);
                      }
  
                      for (int i = 0; i < sectorStart.Items.Count; ++i)
                      {
                          Item item = sectorStart.Items[i];
  
                          if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
                              continue;
  
                          if ((item.ItemData.Flags & reqFlags) == 0)
                              continue;
  
                          if (item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
                              itemsStart.Add(item);
                      }
                  }
  
                  if (checkMobs)
                  {
                      for (int i = 0; i < sectorForward.Mobiles.Count; ++i)
                      {
                          Mobile mob = sectorForward.Mobiles[i];
  
                          if (mob.X == xForward && mob.Y == yForward)
                              mobsForward.Add(mob);
                      }
                  }
              }
  
              this.GetStartZ(m, map, loc, itemsStart, out startZ, out startTop);
  
              bool moveIsOk = this.Check(map, m, itemsForward, mobsForward, xForward, yForward, startTop, startZ, m.CanSwim, m.CantWalk, out newZ);
  
              if (moveIsOk && checkDiagonals)
              {
                  int hold;
  
                  if (m.Player && m.AccessLevel < AccessLevel.GameMaster)
                  {
                      if (!this.Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk, out hold) || !this.Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim, m.CantWalk, out hold))
                          moveIsOk = false;
                  }
                  else
                  {
                      if (!this.Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk, out hold) && !this.Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim, m.CantWalk, out hold))
                          moveIsOk = false;
                  }
              }
  
              for (int i = 0; i < (checkDiagonals ? 4 : 2); ++i)
              {
                  if (this.m_Pools[i].Count != 0)
                      this.m_Pools[i].Clear();
              }
  
              for (int i = 0; i < (checkDiagonals ? 3 : 1); ++i)
              {
                  if (this.m_MobPools[i].Count != 0)
                      this.m_MobPools[i].Clear();
              }
  
              if (!moveIsOk)
                  newZ = startZ;
  
              return moveIsOk;
          }
  
          public bool CheckMovement(Mobile m, Direction d, out int newZ)
          {
              return this.CheckMovement(m, m.Map, m.Location, d, out newZ);
          }
  
          private void GetStartZ(Mobile m, Map map, Point3D loc, List<Item> itemList, out int zLow, out int zTop)
          {
              int xCheck = loc.X, yCheck = loc.Y;
  
              LandTile landTile = map.Tiles.GetLandTile(xCheck, yCheck);
              int landZ = 0, landCenter = 0, landTop = 0;
              bool landBlocks = (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Impassable) != 0;
  
              if (landBlocks && m.CanSwim && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) != 0)
                  landBlocks = false;
              else if (m.CantWalk && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) == 0)
                  landBlocks = true;
  
              map.GetAverageZ(xCheck, yCheck, ref landZ, ref landCenter, ref landTop);
  
  
gt:             bool considerLand = !landTile.Ignored;