|
| 1 | +-- Do not touch to this 2 variables |
1 | 2 | local perlin = PerlinNoise() |
2 | 3 | local chunksize = 32 |
3 | | -local scale = 0.2 |
| 4 | + |
| 5 | +local minGrenerationFreeHeight = 0 -- Generation height limit used to make generation faster if we want empty chunks to allow players to build tall things |
| 6 | +local baseFreeHeight = 30 -- Should be greater than minFreeHeight, difference between both will define max generation height from baseFreeHeight |
4 | 7 |
|
5 | 8 | return function (chunk, seed) |
6 | 9 | perlin:reseed(seed) |
7 | 10 |
|
8 | 11 | local blockLibrary = chunk:GetBlockLibrary() |
9 | 12 | local blockCount = chunk:GetBlockCount() |
10 | 13 |
|
11 | | - local empty = blockLibrary:GetBlockIndex("empty") |
12 | | - local dirt = blockLibrary:GetBlockIndex("dirt") |
13 | | - local grass = blockLibrary:GetBlockIndex("grass") |
| 14 | + local emptyBlock = blockLibrary:GetBlockIndex("empty") |
| 15 | + local debugBlock = blockLibrary:GetBlockIndex("debug") |
| 16 | + local dirtBlock = blockLibrary:GetBlockIndex("dirt") |
| 17 | + local grassBlock = blockLibrary:GetBlockIndex("grass") |
| 18 | + local hullBlock = blockLibrary:GetBlockIndex("hull") |
| 19 | + local hull2Block = blockLibrary:GetBlockIndex("hull2") |
| 20 | + local snowBlock = blockLibrary:GetBlockIndex("snow") |
| 21 | + local stoneBlock = blockLibrary:GetBlockIndex("stone") |
| 22 | + local stoneMossyBlock = blockLibrary:GetBlockIndex("stone_mossy") |
| 23 | + local forcefieldBlock = blockLibrary:GetBlockIndex("forcefield") |
| 24 | + local planksBlock = blockLibrary:GetBlockIndex("planks") |
| 25 | + local stoneBricksBlock = blockLibrary:GetBlockIndex("stone_bricks") |
| 26 | + local copperBlock = blockLibrary:GetBlockIndex("copper_block") |
| 27 | + local glassBlock = blockLibrary:GetBlockIndex("glass") |
14 | 28 |
|
15 | 29 | local planet = chunk:GetContainer() |
16 | 30 | local chunkIndices = chunk:GetIndices() |
17 | | - |
| 31 | + |
| 32 | + local maxHeight = (chunksize * planet:GetChunkCount()^(1/3))/2; |
| 33 | + local maxGenerationHeight = maxHeight - minGrenerationFreeHeight |
| 34 | + local baseHeight = maxHeight - baseFreeHeight -- Only works for planets with the same number of chunks in all the directions |
| 35 | + |
| 36 | + local terrainVariation1Scale = 0.06 * baseHeight |
| 37 | + local terrainVariation2Scale = 0.16 * baseHeight |
| 38 | + local moutainScale = 0.03 * baseHeight |
| 39 | + local spikeScale = 0.2 * baseHeight |
| 40 | + local caveScale = 0.06 -- Other scale unit |
| 41 | + |
18 | 42 | local content = {} |
19 | 43 |
|
20 | 44 | for z = 0, chunksize - 1 do |
21 | 45 | for y = 0, chunksize - 1 do |
22 | 46 | for x = 0, chunksize - 1 do |
23 | 47 | local blockPos = planet:GetBlockIndices(chunkIndices, Vec3ui(x, y, z)) |
24 | 48 | local blockPosNorm, distToCenter = Vec3f(blockPos.x * 1.0, blockPos.y * 1.0, blockPos.z * 1.0):GetNormal() |
| 49 | + distToCenter = math.max(math.abs(blockPos.x + 0.5), math.abs(blockPos.y + 0.5), math.abs(blockPos.z + 0.5)) |
25 | 50 |
|
26 | | - local height = perlin:normalizedOctave3D_01(blockPosNorm.x * scale, blockPosNorm.y * scale, blockPosNorm.z * scale, 4, 2.0) |
27 | | - height = height * 100 |
28 | | - table.insert(content, distToCenter > height and empty or grass) |
| 51 | + if distToCenter > maxGenerationHeight then |
| 52 | + table.insert(content, emptyBlock) |
| 53 | + goto continue |
| 54 | + end |
| 55 | + |
| 56 | + local blockPresence = perlin:normalizedOctave3D_01(blockPos.x * caveScale, blockPos.y * caveScale, blockPos.z * caveScale, 4, 0.1) |
| 57 | + |
| 58 | + if distToCenter <= baseHeight then |
| 59 | + if blockPresence >= 0.3 and blockPresence <= 0.7 then |
| 60 | + if distToCenter <= baseHeight-5 then |
| 61 | + table.insert(content, stoneBlock) |
| 62 | + elseif distToCenter <= baseHeight then |
| 63 | + table.insert(content, dirtBlock) |
| 64 | + end |
| 65 | + else |
| 66 | + table.insert(content, emptyBlock) |
| 67 | + end |
| 68 | + else |
| 69 | + local baseMountainous = perlin:normalizedOctave3D_01((blockPosNorm.x * moutainScale)+10, blockPosNorm.y * moutainScale, blockPosNorm.z * moutainScale, 4, 0.1) |
| 70 | + if baseMountainous < 0.6 then |
| 71 | + mountainous = 0 |
| 72 | + elseif baseMountainous < 0.8 then |
| 73 | + mountainous = 5*baseMountainous-3 |
| 74 | + else |
| 75 | + mountainous = 1 |
| 76 | + end |
| 77 | + |
| 78 | + local heightVariation1 = 10 * perlin:normalizedOctave3D_01(blockPosNorm * terrainVariation1Scale, blockPosNorm.y * terrainVariation1Scale, blockPosNorm.z * terrainVariation1Scale, 4, 0.1) |
| 79 | + local heightVariation2 = 40 * mountainous * perlin:normalizedOctave3D_01((blockPosNorm.x * terrainVariation2Scale)+20, blockPosNorm.y * terrainVariation2Scale, blockPosNorm.z * terrainVariation2Scale, 4, 0.1) |
| 80 | + |
| 81 | + local baseSpikeHeight = perlin:normalizedOctave3D_01((blockPosNorm.x * spikeScale)+30, blockPosNorm.y * spikeScale, blockPosNorm.z * spikeScale, 4, 0.1) |
| 82 | + if baseSpikeHeight < 0.7 then |
| 83 | + spikeHeight = 0 |
| 84 | + elseif baseSpikeHeight < 0.9 then |
| 85 | + spikeHeight = 5*baseSpikeHeight-3.5 |
| 86 | + else |
| 87 | + spikeHeight = 1 |
| 88 | + end |
| 89 | + spikeHeight = (1-mountainous) * spikeHeight * 20 |
| 90 | + |
| 91 | + local height = baseHeight + heightVariation1 + heightVariation2 + spikeHeight |
| 92 | + |
| 93 | + if distToCenter <= height then |
| 94 | + if distToCenter >= height - spikeHeight then |
| 95 | + table.insert(content, stoneMossyBlock) |
| 96 | + elseif mountainous > 0.5 and heightVariation2 > 0.5 then |
| 97 | + table.insert(content, snowBlock) |
| 98 | + elseif mountainous > 0.1 then |
| 99 | + table.insert(content, stoneBlock) |
| 100 | + elseif baseMountainous < 0.4 then |
| 101 | + table.insert(content, grassBlock) |
| 102 | + else |
| 103 | + table.insert(content, dirtBlock) |
| 104 | + end |
| 105 | + else |
| 106 | + table.insert(content, emptyBlock) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + ::continue:: |
29 | 111 | end |
30 | 112 | end |
31 | 113 | end |
| 114 | + |
32 | 115 | chunk:Reset(content) |
33 | 116 | end |
0 commit comments