Skip to content

Commit

Permalink
SimplePerlin
Browse files Browse the repository at this point in the history
  • Loading branch information
Seggan committed Jan 29, 2024
1 parent 036e655 commit fd533a2
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 9 deletions.
70 changes: 70 additions & 0 deletions src/main/kotlin/io/github/addoncommunity/galactifun/Galactifun2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@ package io.github.addoncommunity.galactifun

import co.aikar.commands.PaperCommandManager
import io.github.addoncommunity.galactifun.api.objects.planet.PlanetaryWorld
import io.github.addoncommunity.galactifun.api.objects.properties.Distance.Companion.au
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.Gas
import io.github.addoncommunity.galactifun.api.objects.properties.atmosphere.composition
import io.github.addoncommunity.galactifun.base.BaseUniverse
import io.github.addoncommunity.galactifun.core.Gf2Command
import io.github.addoncommunity.galactifun.core.managers.WorldManager
import io.github.addoncommunity.galactifun.scripting.PlanetScript
import io.github.addoncommunity.galactifun.scripting.dsl.*
import io.github.addoncommunity.galactifun.scripting.dsl.gen.*
import io.github.addoncommunity.galactifun.scripting.evalScript
import io.github.addoncommunity.galactifun.util.years
import io.github.seggan.kfun.AbstractAddon
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun
import io.github.thebusybiscuit.slimefun4.libraries.paperlib.PaperLib
import org.bstats.bukkit.Metrics
import org.bukkit.Bukkit
import org.bukkit.Material
import org.bukkit.block.Biome
import org.bukkit.plugin.java.JavaPlugin
import java.util.logging.Level
import kotlin.script.experimental.api.ResultValue
import kotlin.script.experimental.api.ScriptDiagnostic
import kotlin.script.experimental.api.valueOrThrow
import kotlin.script.experimental.host.toScriptSource
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

class Galactifun2 : AbstractAddon() {

Expand Down Expand Up @@ -118,6 +128,8 @@ class Galactifun2 : AbstractAddon() {
"###################################################"
)
}

doTestingStuff()
}

override fun onDisable() {
Expand All @@ -127,6 +139,64 @@ class Galactifun2 : AbstractAddon() {
override fun getJavaPlugin(): JavaPlugin = this

override fun getBugTrackerURL(): String = "https://github.com/Slimefun-Addon-Community/Galactifun2/issues"

private fun doTestingStuff() {
val script = object : PlanetScript() {}

script.planet {
name = "Mars"
item = Material.RED_CONCRETE
orbiting = BaseUniverse.solarSystem
orbit {
distance = 1.52.au
yearLength = 1.88.years
}
dayCycle = (1.days + 0.65.hours).long

atmosphere {
pressure = 0.006

composition {
95 percent Gas.CARBON_DIOXIDE
2.8 percent Gas.NITROGEN
2 percent Gas.ARGON
0.2 percent Gas.OXYGEN
}
}

world {
generator(SimplePerlin) {
configNoise {
scale = 0.01
frequency = 0.5
amplitude = 0.1
smoothen = true
}

averageHeight = 50
maxDeviation = 20

blocks {
Material.RED_SAND top 2

fillInRestWith(random {
Material.RED_SANDSTONE withWeight 0.8f
Material.IRON_ORE withWeight 0.2f
})
}

singleBiome(Biome.DESERT)
}
}
}

for (planet in script.toRegister) {
if (planet is PlanetaryWorld) {
planet.register()
}
log("Registered planet: ${planet.name}")
}
}
}

private var instance: Galactifun2? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,8 @@ fun PlanetBuilder.eternal(ticks: Int): DayCycle = DayCycle.eternal(ticks)

fun PlanetBuilder.atmosphere(block: AtmosphereBuilder.() -> Unit) {
atmosphere = Atmosphere.buildAtmosphere(block)
}

fun PlanetBuilder.world(block: WorldBuilder.() -> Unit) {
worldConfig = WorldBuilder().apply(block)
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package io.github.addoncommunity.galactifun.scripting.dsl.gen

import io.github.addoncommunity.galactifun.scripting.PlanetDsl
import io.github.addoncommunity.galactifun.scripting.RequiredProperty

abstract class AbstractPerlin : GeneratorBuilder() {
var generateBedrock = true

var averageHeight: Int by RequiredProperty()
var maxDeviation: Int by RequiredProperty()
var minHeight: Int by RequiredProperty()
var minY = Int.MIN_VALUE
var surfaceHeight = 10

@PlanetDsl
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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>) {

Expand All @@ -27,7 +26,7 @@ class NoiseMap(noises: Map<String, AbstractPerlin.PerlinConfig>) {
private val scale = config.scale
private val amplitude = config.amplitude
private val frequency = config.frequency
private val flattenFactor = config.flattenFactor
private val smoothen = config.smoothen

@Volatile
private lateinit var noise: OctaveGenerator
Expand All @@ -41,7 +40,11 @@ class NoiseMap(noises: Map<String, AbstractPerlin.PerlinConfig>) {

operator fun invoke(x: Int, z: Int): Double {
return grid.getOrSet(x, z) {
noise.noise(x.toDouble(), z.toDouble(), frequency, amplitude, true).pow(flattenFactor)
var noise = noise.noise(x.toDouble(), z.toDouble(), frequency, amplitude, true)
if (smoothen) {
noise *= noise
}
noise
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ class PerlinBuilder : AbstractPerlin(), NoiseCombiner {
override fun build(): WorldGenerator {
val averageHeight = averageHeight
val maxDeviation = maxDeviation
val minHeight = minHeight + if (generateBedrock) 1 else 0
val minHeight = minY + if (generateBedrock) 1 else 0

val noises = NoiseMap(noises)

return object : WorldGenerator() {

override val biomeProvider = biomeBuilder?.build(noises) ?: this@PerlinBuilder.biomeProvider

fun getMinHeight(worldInfo: WorldInfo): Int = minHeight.coerceAtLeast(worldInfo.minHeight)

override fun generateNoise(
worldInfo: WorldInfo,
random: Random,
Expand Down Expand Up @@ -107,6 +105,8 @@ class PerlinBuilder : AbstractPerlin(), NoiseCombiner {
val height = noiseCombiner(NoiseCombiner.NoiseInfo(worldInfo, noises, random, x, z))
return (height * maxDeviation + averageHeight).roundToInt()
}

private fun getMinHeight(worldInfo: WorldInfo): Int = minHeight.coerceAtLeast(worldInfo.minHeight)
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ fun PerlinBuilder.generateBlock(block: PerlinBuilder.GenInfo.() -> Material) {
noiseGenerator = block
}

fun PerlinBuilder.noiseConfig(name: String, config: AbstractPerlin.PerlinConfig.() -> Unit) {
fun PerlinBuilder.configNoise(name: String, config: AbstractPerlin.PerlinConfig.() -> Unit) {
noises[name] = AbstractPerlin.PerlinConfig().apply(config)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.addoncommunity.galactifun.scripting.dsl.gen

import io.github.addoncommunity.galactifun.scripting.PlanetDsl
import io.github.thebusybiscuit.slimefun4.libraries.dough.collections.RandomizedSet
import org.bukkit.Material


typealias BlockProvider = SimplePerlinBuilder.GenInfo.() -> Material

@PlanetDsl
class SimpleBlocksBuilder {

var top = mutableListOf<BlockProvider>()
var bottom = mutableListOf<BlockProvider>()
var middle: BlockProvider = { Material.AIR }

private fun addN(n: Int, list: MutableList<BlockProvider>, block: BlockProvider) {
repeat(n) {
list.add(block)
}
}

infix fun BlockProvider.top(n: Int) = addN(n, top, this)

infix fun Material.top(n: Int) = addN(n, top) { this@top }

infix fun BlockProvider.bottom(n: Int) = addN(n, bottom, this)

infix fun Material.bottom(n: Int) = addN(n, bottom) { this@bottom }

@PlanetDsl
class RandomBuilder {
internal val selector = RandomizedSet<Material>()

infix fun Material.withWeight(weight: Float) {
selector.add(this, weight)
}
}
}

fun SimpleBlocksBuilder.fillInRestWith(block: BlockProvider) {
middle = block
}

fun SimpleBlocksBuilder.fillInRestWith(material: Material) {
middle = { material }
}

fun SimpleBlocksBuilder.random(block: SimpleBlocksBuilder.RandomBuilder.() -> Unit): BlockProvider {
val builder = SimpleBlocksBuilder.RandomBuilder()
builder.block()
return {
builder.selector.getRandom(random)
}
}
Loading

0 comments on commit fd533a2

Please sign in to comment.