Exemplo n.º 1
0
        public void floodFill()
        {
            // Use a stack instead of recursion
            Stack <Tile> stack = new Stack <Tile>();

            for (int x = 0; x < WorldParameters.theRegionSize; x++)
            {
                for (int y = 0; y < WorldParameters.theRegionSize; y++)
                {
                    Tile t = myTiles[x, y];

                    //Tile already flood filled, skip
                    if (t.myFloodFilled)
                    {
                        continue;
                    }

                    // Land
                    if (t.myIsLand)
                    {
                        TileGroup group = new TileGroup();
                        group.myType = TileGroupType.Land;
                        stack.Push(t);

                        while (stack.Count > 0)
                        {
                            floodFill(stack.Pop(), ref group, ref stack);
                        }

                        if (group.myTiles.Count > 0)
                        {
                            myLands.Add(group);
                        }
                    }
                    // Water
                    else
                    {
                        TileGroup group = new TileGroup();
                        group.myType = TileGroupType.Water;
                        stack.Push(t);

                        while (stack.Count > 0)
                        {
                            floodFill(stack.Pop(), ref group, ref stack);
                        }

                        if (group.myTiles.Count > 0)
                        {
                            myWaters.Add(group);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        void floodFill(Tile tile, ref TileGroup tiles, ref Stack <Tile> stack)
        {
            // Validate
            if (tile == null)
            {
                return;
            }
            if (tile.myFloodFilled)
            {
                return;
            }
            if (tiles.myType == TileGroupType.Land && !tile.myIsLand)
            {
                return;
            }
            if (tiles.myType == TileGroupType.Water && tile.myIsLand)
            {
                return;
            }

            // Add to TileGroup
            tiles.myTiles.Add(tile);
            tile.myFloodFilled = true;

            // floodfill into neighbors
            Tile t = getTop(tile);

            if (t != null && !t.myFloodFilled && tile.myIsLand == t.myIsLand)
            {
                stack.Push(t);
            }
            t = getBottom(tile);
            if (t != null && !t.myFloodFilled && tile.myIsLand == t.myIsLand)
            {
                stack.Push(t);
            }
            t = getLeft(tile);
            if (t != null && !t.myFloodFilled && tile.myIsLand == t.myIsLand)
            {
                stack.Push(t);
            }
            t = getRight(tile);
            if (t != null && !t.myFloodFilled && tile.myIsLand == t.myIsLand)
            {
                stack.Push(t);
            }
        }