Esempio n. 1
0
 public bool RequirementsMet(MapTile map, Point position)
 {
     if (requirement.Equals(""))
     {
         return(true);
     }
     if (requirement.Contains("tile_nearby;"))
     {
         if (requirement.Contains("water;"))
         {
             int dist = System.Convert.ToInt32(requirement.Replace("tile_nearby;water;", ""));
             if (map.GetClosestOfTileTypeToPos(position, new Water()).DistFrom(position) < dist)
             {
                 return(true);
             }
         }
     }
     if (requirement.Contains("block_nearby;"))
     {
         if (requirement.Contains("wall;"))
         {
             int dist = System.Convert.ToInt32(requirement.Replace("block_nearby;wall;", ""));
             if (map.GetClosestOfBlockTypeToPos(position, BlockType.Wall).DistFrom(position) < dist)
             {
                 return(true);
             }
         }
         else if (requirement.Contains("tree;"))
         {
             int dist = System.Convert.ToInt32(requirement.Replace("block_nearby;tree;", ""));
             if (map.GetClosestOfBlockTypeToPos(position, BlockType.Tree).DistFrom(position) < dist)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        public static void GenerateTrees(Random rng, MapTile map, int numOfSeedTrees, int growthGenerations)
        {
            // place seed trees
            List <Point> availableSpots = map.GetAllGrassTiles();
            List <Point> treeSpots      = new List <Point>();
            Point        nextSpot       = null;
            int          placedTrees    = 0;

            // placement algorithm
            while (placedTrees < numOfSeedTrees)
            {
                Point potentialSpot = null;
                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())) <= 8)
                    {
                        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];
                    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)));
                    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.GetEmptyAdjacentBlocks(nextSpot).Count != 8 || availableSpots.Contains(nextSpot) == false) && count <= 5);
                    if (count <= 5)
                    {
                        treeSpots.Add(nextSpot);
                        map.Blocks[nextSpot.X * map.Width + nextSpot.Y] = new Tree(Material.Wood);
                    }
                }
            }
        }