public static void FloodFillWorldItemArray(ref WorldItem[] Items, int ItemsPerRow, int StartIndex, WorldItem OriginalItem, WorldItem NewItem) { int Rows = Items.Length / ItemsPerRow; Stack <Point> LocationStack = new Stack <Point>(); int[] PreviousPoints = new int[Items.Length]; int X = StartIndex % ItemsPerRow; int Y = StartIndex / ItemsPerRow; LocationStack.Push(new Point(X, Y)); while (LocationStack.Count > 0) { Point p = LocationStack.Pop(); int Idx = p.X + p.Y * ItemsPerRow; if (p.X < ItemsPerRow && p.X > -1 && p.Y < Rows && p.Y > -1 && PreviousPoints[Idx] == 0) // Make sure we stay within bounds { WorldItem i = Items[Idx]; if (i.Equals(OriginalItem)) { Items[Idx] = new WorldItem(NewItem.ItemID, NewItem.Flag1, NewItem.Flag2, i.Index); if (p.X - 1 > -1) { LocationStack.Push(new Point(p.X - 1, p.Y)); } if (p.X + 1 < ItemsPerRow) { LocationStack.Push(new Point(p.X + 1, p.Y)); } if (p.Y - 1 > -1) { LocationStack.Push(new Point(p.X, p.Y - 1)); } if (p.Y + 1 < Rows) { LocationStack.Push(new Point(p.X, p.Y + 1)); } } } PreviousPoints[Idx] = 1; } }