bool DetectBounds() { List <Rectangle> boundss = new List <Rectangle>(); List <Tuple <Point, bool, bool> > adjacentTiles = new List <Tuple <Point, bool, bool> >(); Point size = new Point(4, 4); int length = size.X * 2 + size.Y * 2 + 4; int x = 0; int y = 0; bool lastTileOfType = false; for (int i = 0; i < length; i++) { bool scanHoriz = true; bool scanVert = true; if (i == 0) { } else if (i > 0 && i < size.X + 2) { x++; scanHoriz = !lastTileOfType; } else if (i < size.X + 2 + size.Y + 1) { y++; scanVert = !lastTileOfType; } else if (i < size.X + 2 + size.Y + 1 + size.X + 1) { x--; scanHoriz = !lastTileOfType; } else { y--; scanVert = !lastTileOfType; } Point tilePos = new Point(Position.X - 1 + x, Position.Y - 1 + y); Tile tile = Main.tile[tilePos.X, tilePos.Y]; int? tileType = PoMUtil.GetTileType(tile); if (tileType.HasValue && tileType == ModContent.TileType <MapBorder>()) { adjacentTiles.Add(new Tuple <Point, bool, bool>(tilePos, scanHoriz, scanVert)); lastTileOfType = true; } else { lastTileOfType = false; } } foreach (var tilePos in adjacentTiles) { PoMUtil.FindAdjacentBounds(tilePos.Item1, boundss, tilePos.Item2, tilePos.Item3); } Rectangle tileBounds = new Rectangle(Position.X, Position.Y, size.X - 1, size.Y - 1); Rectangle tileBoundsInflated = tileBounds; tileBoundsInflated.Inflate(2, 2); for (int i = boundss.Count - 1; i >= 0; i--) { Rectangle bound = boundss[i]; bool intersecnt = tileBoundsInflated.X < bound.X + bound.Width && bound.X < tileBoundsInflated.X + tileBoundsInflated.Width && tileBoundsInflated.Y < bound.Y + bound.Height && bound.Y < tileBoundsInflated.Y + tileBoundsInflated.Height; if (!bound.Intersects(tileBoundsInflated) || bound.Contains(tileBounds) || MapBorder.InteresectsOrContainsActiveBounds(bound)) { boundss.RemoveAt(i); } } if (boundss.Count > 0) { bounds = boundss.Aggregate((b1, b2) => b1.Width * b1.Height > b2.Width * b2.Height ? b1 : b2); return(true); } else { bounds = null; return(false); } }