Example #1
0
        private static void GeneratePlants(MapTile map, Random rng, List <string> plantTypes, int numOfSeedPlants, int growthGenerations)
        {
            // place seed plants
            List <Point> availableSpots = map.GetAllDirtTiles();
            Point        nextSpot       = null;
            int          placedPlants   = 0;
            int          maxTries       = availableSpots.Count();
            int          tries          = 0;

            // placement algorithm
            while (placedPlants < numOfSeedPlants || tries > maxTries)
            {
                ++tries;
                Plant p;
                nextSpot = availableSpots[rng.Next(0, availableSpots.Count)];
                if (nextSpot != null)
                {
                    placedPlants++;
                    p = DataReader.GetPlant(plantTypes[rng.Next(0, plantTypes.Count)]);
                    if (p.RequirementsMet(map, nextSpot))
                    {
                        map.Blocks[nextSpot.X * map.Width + nextSpot.Y] = p;
                    }
                }
            }
            // growth algorithm
            for (int g = 0; g < growthGenerations; g++)
            {
                for (int i = 0; i < map.Width; i++)
                {
                    for (int j = 0; j < map.Height; j++)
                    {
                        if (map[i, j] is Plant p && g % p.GrowthInterval == 0)
                        {
                            p.Grow(map, new Point(i, j), rng);
                        }
                    }
                }
            }
        }
Example #2
0
        private static void GenerateTrees(MapTile map, Random rng, int numOfSeedTrees, int growthGenerations)
        {
            // place seed trees
            List <Point> availableSpots = map.GetAllDirtTiles();
            List <Point> treeSpots      = new List <Point>();
            Point        nextSpot       = null;
            int          placedTrees    = 0;

            // placement algorithm
            while (placedTrees < numOfSeedTrees)
            {
                Point potentialSpot;
                int   count = 0;
                while (nextSpot == null && count < availableSpots.Count)
                {
                    count++;
                    potentialSpot = availableSpots[rng.Next(0, availableSpots.Count)];
                    if (potentialSpot.DistFrom(map.GetClosestOfTileTypeToPos(potentialSpot, new Water())) <= 16)
                    {
                        nextSpot = potentialSpot;
                    }
                }

                if (nextSpot != null)
                {
                    treeSpots.Add(nextSpot);
                    map.Blocks[nextSpot.X * map.Width + nextSpot.Y] = new Tree(Material.Wood);
                }
                placedTrees++;
            }

            // run growth algorithm
            for (int i = 0; i < growthGenerations; i++)
            {
                int treesThisGen = treeSpots.Count;
                for (int j = 0; j < treesThisGen; j++)
                {
                    Point currentTree = treeSpots[j];
                    int   count       = 0;
                    do
                    {
                        count++;
                        nextSpot = new Point(rng.Next(Math.Max(currentTree.X - 8, 0), Math.Min(currentTree.X + 8, map.Width - 1)), rng.Next(Math.Max(currentTree.Y - 8, 0), Math.Min(currentTree.Y + 8, map.Height - 1)));
                        for (int x = Math.Max(nextSpot.X - 3, 0); x <= Math.Min(nextSpot.X + 3, map.Width - 1); x++)
                        {
                            for (int y = Math.Max(nextSpot.Y - 3, 0); y <= Math.Min(nextSpot.Y + 3, map.Height - 1); y++)
                            {
                                if (map[x, y] is Tree)
                                {
                                    availableSpots.Remove(nextSpot);
                                }
                            }
                        }
                    } while ((map.Blocks.GetEmptyAdjacentBlocks(new Point(map.Width, map.Height), nextSpot).Count != 8 || availableSpots.Contains(nextSpot) == false) && count <= 7);
                    if (count <= 7)
                    {
                        treeSpots.Add(nextSpot);
                        map.Blocks[nextSpot.X * map.Width + nextSpot.Y] = new Tree(Material.Wood);
                    }
                }
            }
        }