-
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
10 changed files
with
276 additions
and
16 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
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
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
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
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
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
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/WorldBuilder.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,44 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.planet.gen.WorldGenerator | ||
import io.github.addoncommunity.galactifun.scripting.PlanetDsl | ||
import io.github.addoncommunity.galactifun.scripting.RequiredProperty | ||
import io.github.addoncommunity.galactifun.scripting.dsl.gen.GeneratorBuilder | ||
import io.github.addoncommunity.galactifun.scripting.dsl.gen.GeneratorBuilderProvider | ||
import org.bukkit.Material | ||
import org.bukkit.inventory.ItemStack | ||
import java.util.* | ||
|
||
@PlanetDsl | ||
class WorldBuilder { | ||
|
||
var world: String? = null | ||
|
||
var generator: WorldGenerator by RequiredProperty() | ||
|
||
var spawnVanillaMobs = false | ||
|
||
internal val blockMappings = EnumMap<Material, ItemStack>(Material::class.java) | ||
|
||
@PlanetDsl | ||
inner class MappingBuilder { | ||
infix fun Material.mapTo(item: ItemStack) { | ||
this@WorldBuilder.blockMappings[this] = item | ||
} | ||
|
||
infix fun Material.mapTo(item: Material) { | ||
this mapTo ItemStack(item) | ||
} | ||
} | ||
} | ||
|
||
inline fun WorldBuilder.blockMappings(block: WorldBuilder.MappingBuilder.() -> Unit) { | ||
MappingBuilder().apply(block) | ||
} | ||
|
||
inline fun <T : GeneratorBuilder> WorldBuilder.generator( | ||
provider: GeneratorBuilderProvider<T>, | ||
block: T.() -> Unit | ||
) { | ||
generator = provider.provide().apply(block).build() | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/gen/AbstractPerlin.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,30 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl.gen | ||
|
||
import io.github.addoncommunity.galactifun.scripting.PlanetDsl | ||
import io.github.addoncommunity.galactifun.scripting.RequiredProperty | ||
|
||
@PlanetDsl | ||
abstract class AbstractPerlin : GeneratorBuilder() { | ||
|
||
var config: PerlinConfig by RequiredProperty() | ||
|
||
var generateBedrock = true | ||
|
||
var averageHeight: Int by RequiredProperty() | ||
var minHeight: Int by RequiredProperty() | ||
var surfaceHeight = 10 | ||
|
||
class PerlinConfig { | ||
var octaves: Int = 8 | ||
var scale: Double by RequiredProperty() | ||
var amplitude: Double by RequiredProperty() | ||
var frequency: Double by RequiredProperty() | ||
|
||
var flattenFactor: Double = 1.0 | ||
var smoothen: Boolean = false | ||
} | ||
} | ||
|
||
inline fun AbstractPerlin.noiseConfig(block: AbstractPerlin.PerlinConfig.() -> Unit) { | ||
config = AbstractPerlin.PerlinConfig().apply(block) | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/gen/GeneratorBuilder.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,25 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl.gen | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.planet.gen.WorldGenerator | ||
import io.github.addoncommunity.galactifun.scripting.RequiredProperty | ||
import org.bukkit.block.Biome | ||
import org.bukkit.generator.BiomeProvider | ||
import org.bukkit.generator.WorldInfo | ||
|
||
abstract class GeneratorBuilder { | ||
|
||
var biomeProvider: BiomeProvider by RequiredProperty() | ||
|
||
abstract fun build(): WorldGenerator | ||
} | ||
|
||
fun GeneratorBuilder.singleBiome(biome: Biome) { | ||
biomeProvider = object : BiomeProvider() { | ||
override fun getBiome(worldInfo: WorldInfo, x: Int, y: Int, z: Int) = biome | ||
override fun getBiomes(worldInfo: WorldInfo) = mutableListOf(biome) | ||
} | ||
} | ||
|
||
interface GeneratorBuilderProvider<T : GeneratorBuilder> { | ||
fun provide(): T | ||
} |
122 changes: 122 additions & 0 deletions
122
src/main/kotlin/io/github/addoncommunity/galactifun/scripting/dsl/gen/PerlinBuilder.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,122 @@ | ||
package io.github.addoncommunity.galactifun.scripting.dsl.gen | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.planet.gen.WorldGenerator | ||
import org.bukkit.Material | ||
import org.bukkit.generator.WorldInfo | ||
import org.bukkit.util.noise.SimplexOctaveGenerator | ||
import java.util.* | ||
import kotlin.math.pow | ||
|
||
class PerlinBuilder : AbstractPerlin() { | ||
|
||
var noiseGenerator: (WorldInfo, Random, Int, Int, Int, Int) -> Material = | ||
{ _, _, _, _, _, _ -> Material.AIR } | ||
|
||
var surfaceGenerator: (WorldInfo, Random, Int, Int, Int, Int) -> Material = | ||
{ _, _, _, _, _, _ -> Material.AIR } | ||
|
||
|
||
override fun build(): WorldGenerator { | ||
// Prevent performance issues by unboxing before building the generator | ||
val octaves = config.octaves | ||
val scale = config.scale | ||
val amplitude = config.amplitude | ||
val frequency = config.frequency | ||
val flattenFactor = config.flattenFactor | ||
val smoothen = config.smoothen | ||
|
||
val averageHeight = averageHeight | ||
val minHeight = minHeight + if (generateBedrock) 1 else 0 | ||
|
||
return object : WorldGenerator() { | ||
override val biomeProvider = this@PerlinBuilder.biomeProvider | ||
|
||
@Volatile | ||
private lateinit var baseNoise: SimplexOctaveGenerator | ||
|
||
fun getMinHeight(worldInfo: WorldInfo): Int = minHeight.coerceAtLeast(worldInfo.minHeight) | ||
|
||
override fun generateNoise( | ||
worldInfo: WorldInfo, | ||
random: Random, | ||
chunkX: Int, | ||
chunkZ: Int, | ||
chunkData: ChunkData | ||
) { | ||
val cx = chunkX * 16 | ||
val cz = chunkZ * 16 | ||
val min = getMinHeight(worldInfo) | ||
for (x in 0..15) { | ||
for (z in 0..15) { | ||
val height = getHeight(worldInfo, cx + x, cz + z) | ||
for (y in min until height - surfaceHeight) { | ||
chunkData.setBlock(x, y, z, noiseGenerator(worldInfo, random, cx + x, y, cz + z, height)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun generateSurface( | ||
worldInfo: WorldInfo, | ||
random: Random, | ||
chunkX: Int, | ||
chunkZ: Int, | ||
chunkData: ChunkData | ||
) { | ||
val cx = chunkX * 16 | ||
val cz = chunkZ * 16 | ||
for (x in 0..15) { | ||
for (z in 0..15) { | ||
val height = getHeight(worldInfo, cx + x, cz + z) | ||
for (y in height - surfaceHeight until height) { | ||
chunkData.setBlock(x, y, z, surfaceGenerator(worldInfo, random, cx + x, y, cz + z, height)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun generateBedrock( | ||
worldInfo: WorldInfo, | ||
random: Random, | ||
chunkX: Int, | ||
chunkZ: Int, | ||
chunkData: ChunkData | ||
) { | ||
if (!generateBedrock) return | ||
val y = getMinHeight(worldInfo) | ||
for (x in 0..15) { | ||
for (z in 0..15) { | ||
chunkData.setBlock(x, y, z, Material.BEDROCK) | ||
} | ||
} | ||
} | ||
|
||
private fun getHeight(worldInfo: WorldInfo, x: Int, z: Int): Int { | ||
if (!::baseNoise.isInitialized) { | ||
baseNoise = SimplexOctaveGenerator(worldInfo.seed, octaves) | ||
baseNoise.setScale(scale) | ||
} | ||
var height = baseNoise.noise(x.toDouble(), z.toDouble(), frequency, amplitude, true) | ||
height = height.pow(flattenFactor) | ||
height = (height + 1) / 2 | ||
return (height * (averageHeight - minHeight)).toInt() | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun PerlinBuilder.generateNoiseBlock( | ||
block: (WorldInfo, Random, Int, Int, Int, Int) -> Material | ||
) { | ||
noiseGenerator = block | ||
} | ||
|
||
fun PerlinBuilder.generateSurfaceBlock( | ||
block: (WorldInfo, Random, Int, Int, Int, Int) -> Material | ||
) { | ||
surfaceGenerator = block | ||
} | ||
|
||
object Perlin : GeneratorBuilderProvider<PerlinBuilder> { | ||
override fun provide() = PerlinBuilder() | ||
} |