/// <summary> /// Generates pre-grown trees for the world voxel grid. /// </summary> private void GenerateTrees() { UnityEngine.Random.seed = Settings.Seed; for (int x = 0; x < WorldVoxels.Instance.Voxels.GetLength(0); ++x) { //Plant a tree in this column with a random chance. if (UnityEngine.Random.value > Settings.TreeChance) { continue; } //Find the surface height. int y = (int)(WorldVoxels.Instance.Voxels.GetLength(1) * (1.0f - Settings.Biome_Surface)); UnityEngine.Assertions.Assert.IsTrue(y >= 0 && y < WorldVoxels.Instance.Voxels.GetLength(1)); //If the first estimate is empty space, move downward. if (WorldVoxels.Instance.Voxels[x, y] == VoxelTypes.Empty) { while (y > 0 && WorldVoxels.Instance.Voxels[x, y - 1] == VoxelTypes.Empty) { y -= 1; } } //Otherwise, move upward. else { while (y < WorldVoxels.Instance.Voxels.GetLength(1) - 1 && WorldVoxels.Instance.Voxels[x, y] != VoxelTypes.Empty) { y += 1; } } //Now that we have the surface position, grow a tree there. Vector2i pos = new Vector2i(x, y); Tree tree = new Tree(new GrowPattern_Oak(), new GrowData_Oak(pos, VoxelTypes.Tree_Wood)); tree.GrowDat = tree.GrowPattern.Sprout(WorldVoxels.Instance.Voxels, VoxelTypes.Tree_Wood, pos, new List<Vector2i>()); WorldTrees.Instance.Trees.Add(tree); int nGrowths = (int)Mathf.Lerp(Settings.TreeMinGrowth, Settings.TreeMaxGrowth, Mathf.Pow(UnityEngine.Random.value, Settings.TreeGrowthExponent)); for (int i = 0; i < nGrowths; ++i) tree.GrowBareBones(); } }