2024-09-03 09:18:35 -04:00
|
|
|
struct StandardWorldGenerator: WorldGenerator {
|
2024-09-05 22:32:30 +10:00
|
|
|
private var heightNoise: LayeredNoiseAlt<SimplexNoise<Float>>!
|
|
|
|
private var terrainNoise: LayeredNoise<SimplexNoise<Float>>!
|
|
|
|
private var colorNoise: LayeredNoise<ImprovedPerlin<Float>>!
|
2024-08-30 21:56:39 +10:00
|
|
|
|
|
|
|
public mutating func reset(seed: UInt64) {
|
|
|
|
var random: any RandomProvider
|
2024-09-01 02:09:49 +10:00
|
|
|
let initialState = SplitMix64.createState(seed: seed)
|
2024-08-30 21:56:39 +10:00
|
|
|
#if true
|
2024-09-01 02:09:49 +10:00
|
|
|
random = Xoroshiro128PlusPlus(state: initialState)
|
2024-08-30 21:56:39 +10:00
|
|
|
#else
|
2024-09-01 02:09:49 +10:00
|
|
|
random = PCG32Random(seed: initialState)
|
2024-08-30 21:56:39 +10:00
|
|
|
#endif
|
|
|
|
|
2024-09-05 22:32:30 +10:00
|
|
|
self.heightNoise = .init(random: &random, octaves: 200, frequency: 0.0002, amplitude: 2)
|
|
|
|
self.terrainNoise = .init(random: &random, octaves: 10, frequency: 0.01, amplitude: 0.337)
|
|
|
|
self.colorNoise = .init(random: &random, octaves: 150, frequency: 0.00366, amplitude: 2)
|
2024-08-30 21:56:39 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
public func makeChunk(id chunkID: SIMD3<Int>) -> Chunk {
|
|
|
|
let chunkOrigin = chunkID &<< Chunk.shift
|
|
|
|
var chunk = Chunk(position: chunkOrigin)
|
2024-09-05 22:32:30 +10:00
|
|
|
for z in 0..<Chunk.size {
|
|
|
|
for x in 0..<Chunk.size {
|
|
|
|
let height = self.heightNoise.get(SIMD2<Float>(chunkOrigin.xz &+ SIMD2<Int>(x, z)))
|
|
|
|
for y in 0..<Chunk.size {
|
|
|
|
let ipos = SIMD3(x, y, z)
|
|
|
|
let fpos = SIMD3<Float>(chunkOrigin &+ ipos)
|
|
|
|
let height = fpos.y / 64.0 + height
|
|
|
|
let value = height + self.terrainNoise.get(fpos)
|
|
|
|
let block: BlockType = if value < 0 {
|
|
|
|
.solid(.init(
|
|
|
|
hue: Float(180 + self.colorNoise.get(fpos) * 180),
|
|
|
|
saturation: 0.7, value: 0.9).linear)
|
|
|
|
} else {
|
|
|
|
.air
|
|
|
|
}
|
|
|
|
chunk.setBlock(internal: ipos, type: block)
|
|
|
|
}
|
2024-08-30 21:56:39 +10:00
|
|
|
}
|
2024-09-05 22:32:30 +10:00
|
|
|
}
|
2024-08-30 21:56:39 +10:00
|
|
|
return chunk
|
|
|
|
}
|
|
|
|
}
|