Exemplo n.º 1
0
    //Divides the areas into AreaGroups, which will have the same biome (tileset). (Flood-Fill algorithm)
    private void generateAreaGroups()
    {
        //Plan:
        //  Search in a floodfill pattern (Moving along paths), until no more Areas exist to flood fill.
        //  When an Area without an AreaGroup is found,
        //      Create a new AreaGroup Object, and add it to the list.
        //      Start floodfill to assign surrounding Areas to this group.

        bool isFinished = false;

        System.Random random = new System.Random(seed);

        int numOfTypes = System.Enum.GetNames(typeof(AreaType)).Length;

        int x = 0;
        int y = 0;

        Queue <Area> unset = new Queue <Area>();

        while (!isFinished)
        {
            Point bounds = getMapBounds();

            Area search = getArea(x, y);

            if (search.getBiome() == 0)
            {
                //Get the area.
                Area temp     = search;
                int  distance = random.Next(2, 4); //Get a random distance for this AreaGroup to expand.

                List <Area> array = new List <Area>();
                floodFill(null, temp, distance, array);

                //Don't allow tiny AreaGroups.
                if (array.Count > 3)
                {
                    //Get a random AreaType
                    AreaGroup group = new AreaGroup((Biome)random.Next(1, numOfTypes));

                    for (int i = 0; i < array.Count; i++)
                    {
                        if (array[i].getBiome() == Biome.NOT_ASSIGNED)
                        {
                            group.addArea(array[i]);
                        }
                    }
                }
                else
                {
                    foreach (Area a in array)
                    {
                        unset.Enqueue(a);
                    }
                }
            }

            x++;

            if (x > bounds.x)
            {
                x = 0;
                y++;
                if (y > bounds.y)
                {
                    isFinished = true;
                }
            }
        }

        while (unset.Count > 0)
        {
            //Set this Area to the nearest AreaGroup.
            Area next = unset.Dequeue();
            if (next.getBiome() == Biome.NOT_ASSIGNED)
            {
                foreach (Area a in next.getNeighbors())
                {
                    if (a.getGroup() != null)
                    {
                        a.getGroup().addArea(next);
                        break;
                    }
                }
                if (next.getBiome() == Biome.NOT_ASSIGNED)
                {
                    unset.Enqueue(next);
                }
            }
        }
    }