-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/gen/NoiseMap.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl.gen | ||
|
||
import io.github.addoncommunity.galactifun.util.gen.DoubleChunkGrid | ||
import org.bukkit.util.noise.OctaveGenerator | ||
import org.bukkit.util.noise.SimplexOctaveGenerator | ||
import kotlin.math.pow | ||
|
||
class NoiseMap(noises: Map<String, AbstractPerlin.PerlinConfig>) { | ||
|
||
private val noises = noises.mapValues { Noise(it.value) } | ||
|
||
@Volatile | ||
private var initted = false | ||
|
||
internal fun init(seed: Long) { | ||
if (initted) return | ||
synchronized(this) { | ||
if (initted) return | ||
noises.values.forEach { it.init(seed) } | ||
initted = true | ||
} | ||
} | ||
|
||
class Noise internal constructor(config: AbstractPerlin.PerlinConfig) { | ||
|
||
private val octaves = config.octaves | ||
private val scale = config.scale | ||
private val amplitude = config.amplitude | ||
private val frequency = config.frequency | ||
private val flattenFactor = config.flattenFactor | ||
|
||
@Volatile | ||
private lateinit var noise: OctaveGenerator | ||
|
||
private val grid = DoubleChunkGrid() | ||
|
||
internal fun init(seed: Long) { | ||
noise = SimplexOctaveGenerator(seed, octaves) | ||
noise.setScale(scale) | ||
} | ||
|
||
operator fun invoke(x: Int, z: Int): Double { | ||
return grid.getOrSet(x, z) { | ||
noise.noise(x.toDouble(), z.toDouble(), frequency, amplitude, true).pow(flattenFactor) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters