/// <summary> /// Adds trees to the world. /// If treeProb parameter is set to TreeProbability.None then no trees will be added. /// </summary> public void AddTrees(ref BlockData[,,] blocks, TreeProbability treeProb) { if (treeProb == TreeProbability.None) { return; } float woodbaseProbability = treeProb == TreeProbability.Some ? WoodbaseSomeProbability : WoodbaseHighProbability; for (int x = 1; x < _totalBlockNumberX - 1; x++) { // this 20 is hard coded as for now but generally it would be nice if // this loop could know in advance where the lowest grass is for (int y = 20; y < _totalBlockNumberY - TreeHeight - 1; y++) { for (int z = 1; z < _totalBlockNumberZ - 1; z++) { if (blocks[x, y, z].Type != BlockType.Grass) { continue; } if (IsThereEnoughSpaceForTree(in blocks, x, y, z)) { if (FractalFunc(x, y, z, WoodbaseSmooth, WoodbaseOctaves) < woodbaseProbability) { BuildTree(ref blocks, x, y, z); } } } } } }
// Perlin function value of x is equal to its value of -x. Same for y. // To avoid it we need an offset, quite large one to be sure. public TerrainGenerator(GameSettings options) { _worldSizeX = options.WorldSizeX; _worldSizeZ = options.WorldSizeZ; _totalBlockNumberX = _worldSizeX * World.ChunkSize; _totalBlockNumberY = World.WorldSizeY * World.ChunkSize; _totalBlockNumberZ = _worldSizeZ * World.ChunkSize; TreeProbability = options.TreeProbability; WaterLevel = options.WaterLevel; SeedValue = options.SeedValue; }
/// <summary> /// Adds trees to the world. /// If treeProb parameter is set to TreeProbability.None then no trees will be added. /// In order to avoid potential collisions boundary cubes on x and z axis are ignored /// and therefore will never grow a tree resulting in slightly different although unnoticeable /// for player results. /// </summary> public void AddTreesParallel(TreeProbability treeProb) { if (treeProb == TreeProbability.None) { return; } float woodbaseProbability = treeProb == TreeProbability.Some ? WoodbaseSomeProbability : WoodbaseHighProbability; var queue = new MultiThreadTaskQueue(); // schedule one task per chunk for (int i = 0; i < World.Settings.WorldSizeX; i++) { for (int j = 0; j < World.Settings.WorldSizeZ; j++) { queue.ScheduleTask(AddTreesInChunkParallel, woodbaseProbability, i, j); } } queue.RunAllInParallel(); // this is synchronous }