Skip to content

Commit

Permalink
Basic perlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Jan 26, 2024
1 parent 766b518 commit 6309bd5
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ abstract class AlienWorld(name: String, baseItem: ItemStack) : PlanetaryWorld(na

return world
}

fun addBlockMapping(material: Material, item: ItemStack) {
blockMappings[material] = item
}

fun getBlockMapping(material: Material): ItemStack? {
return blockMappings[material]
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.addoncommunity.galactifun.api.objects.properties.atmosphere

import io.github.addoncommunity.galactifun.scripting.PlanetDsl
import org.bukkit.World
import java.util.*

@PlanetDsl
@AtmosphereDsl
class AtmosphereBuilder internal constructor() {

Expand All @@ -14,11 +16,14 @@ class AtmosphereBuilder internal constructor() {

internal val composition = EnumMap<Gas, Double>(Gas::class.java)

@PlanetDsl
@AtmosphereDsl
inner class CompositionBuilder internal constructor() {
infix fun Double.percent(gas: Gas) {
this@AtmosphereBuilder.composition[gas] = this
}

infix fun Int.percent(gas: Gas) = this.toDouble() percent gas
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld
import org.bukkit.Location
import org.bukkit.entity.Player

@Suppress("unused")
@CommandAlias("gf2")
object Gf2Command : BaseCommand() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import io.github.addoncommunity.galactifun.api.objects.TheUniverse
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryObject
import io.github.addoncommunity.galactifun.api.objects.properties.DayCycle
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Atmosphere
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
import io.github.addoncommunity.galactifun.base.BaseUniverse
import io.github.addoncommunity.galactifun.pluginInstance
import org.bukkit.Material
import org.bukkit.World
import org.bukkit.block.Biome
import kotlin.reflect.KClass
import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.*
Expand All @@ -19,10 +21,10 @@ import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromT
import kotlin.script.experimental.jvmhost.createJvmEvaluationConfigurationFromTemplate

@KotlinScript(
displayName = "Galactifun2 Planet Definition",
fileExtension = "planet.kts",
compilationConfiguration = PlanetScriptConfig::class,
evaluationConfiguration = PlanetScriptEval::class,
hostConfiguration = PlanetScriptHost::class
evaluationConfiguration = PlanetScriptEval::class
)
abstract class PlanetScript {
val eternalDay = DayCycle.ETERNAL_DAY
Expand All @@ -35,6 +37,7 @@ object PlanetScriptConfig : ScriptCompilationConfiguration({
defaultImports(
"io.github.addoncommunity.galactifun.util.*",
"io.github.addoncommunity.galactifun.scripting.dsl.*",
"io.github.addoncommunity.galactifun.scripting.dsl.gen.*",
"io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.lightYears",
"io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.kilometers",
"io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.au",
Expand All @@ -43,10 +46,13 @@ object PlanetScriptConfig : ScriptCompilationConfiguration({
)
defaultImports(
Material::class,
World.Environment::class,
Biome::class,

BaseUniverse::class,
TheUniverse::class,
Atmosphere::class,
World.Environment::class
Gas::class,
)
compilerOptions.append("-Xadd-modules=ALL-MODULE-PATH")
jvm {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ class OrbitBuilder {

inline fun orbit(block: OrbitBuilder.() -> Unit): Orbit {
return OrbitBuilder().apply(block).build()
}

inline fun PlanetBuilder.orbit(block: OrbitBuilder.() -> Unit) {
orbit = OrbitBuilder().apply(block).build()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.addoncommunity.galactifun.scripting.dsl

import io.github.addoncommunity.galactifun.api.objects.UniversalObject
import io.github.addoncommunity.galactifun.api.objects.planet.AlienWorld
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryObject
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld
import io.github.addoncommunity.galactifun.api.objects.properties.DayCycle
Expand All @@ -26,18 +27,36 @@ class PlanetBuilder {
var dayCycle: DayCycle by RequiredProperty()

var atmosphere = Atmosphere.NONE
var world: String? = null

var worldConfig: WorldBuilder? = null

fun build(): PlanetaryObject {
val world = this.world
if (world != null) {
return object : PlanetaryWorld(name, ItemStack(item)) {
override val dayCycle = this@PlanetBuilder.dayCycle
override val orbiting = this@PlanetBuilder.orbiting
override val orbit = this@PlanetBuilder.orbit
override val atmosphere = this@PlanetBuilder.atmosphere
val config = worldConfig
if (config != null) {
val world = config.world
if (world != null) {
return object : PlanetaryWorld(name, ItemStack(item)) {
override val dayCycle = this@PlanetBuilder.dayCycle
override val orbiting = this@PlanetBuilder.orbiting
override val orbit = this@PlanetBuilder.orbit
override val atmosphere = this@PlanetBuilder.atmosphere

override fun loadWorld(): World = Bukkit.getWorld(world) ?: error("World $world does not exist")
override fun loadWorld(): World = Bukkit.getWorld(world) ?: error("World $world does not exist")
}
} else {
return object : AlienWorld(name, ItemStack(item)) {
override val dayCycle = this@PlanetBuilder.dayCycle
override val orbiting = this@PlanetBuilder.orbiting
override val orbit = this@PlanetBuilder.orbit
override val atmosphere = this@PlanetBuilder.atmosphere
override val generator = config.generator

init {
for ((material, item) in config.blockMappings) {
addBlockMapping(material, item)
}
}
}
}
} else {
return object : PlanetaryObject(name, ItemStack(item)) {
Expand All @@ -58,10 +77,6 @@ inline fun PlanetScript.planet(block: PlanetBuilder.() -> Unit): PlanetaryObject
return planet
}

inline fun PlanetBuilder.orbit(block: OrbitBuilder.() -> Unit) {
orbit = OrbitBuilder().apply(block).build()
}

fun PlanetBuilder.eternal(ticks: Int): DayCycle = DayCycle.eternal(ticks)

fun PlanetBuilder.atmosphere(block: AtmosphereBuilder.() -> Unit) {
Expand Down
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()
}
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)
}
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
}
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()
}

0 comments on commit 6309bd5

Please sign in to comment.