terragen now works column-wise (wip soil-rock layer dithering)

This commit is contained in:
minjaesong
2021-02-28 09:44:40 +09:00
parent 7928dd1573
commit 6cf8553ac2

View File

@@ -28,23 +28,12 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
(0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs -> (0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs ->
ThreadExecutor.submit { ThreadExecutor.submit {
val localJoise = getGenerator(seed, params as TerragenParams) val localJoise = getGenerator(seed, params as TerragenParams)
//val localLock = java.lang.Object() // in an attempt to fix the "premature exit" issue of a thread run
//synchronized(localLock) { // also see: https://stackoverflow.com/questions/28818494/threads-stopping-prematurely-for-certain-values
for (x in xs) { for (x in xs) {
for (y in 0 until world.height) {
val sampleTheta = (x.toDouble() / world.width) * TWO_PI val sampleTheta = (x.toDouble() / world.width) * TWO_PI
val sampleOffset = world.width / 8.0 val sampleOffset = world.width / 8.0
val sampleX = sin(sampleTheta) * sampleOffset + sampleOffset // plus sampleOffset to make only draw(x, localJoise, sampleTheta, sampleOffset)
val sampleZ = cos(sampleTheta) * sampleOffset + sampleOffset // positive points are to be sampled
val sampleY = y - (world.height - YHEIGHT_MAGIC) * YHEIGHT_DIVISOR // Q&D offsetting to make ratio of sky:ground to be constant
// DEBUG NOTE: it is the OFFSET FROM THE IDEAL VALUE (observed land height - (HEIGHT * DIVISOR)) that must be constant
val noise = localJoise.map { it.get(sampleX, sampleY, sampleZ) }
draw(x, y, noise, world)
} }
} }
//}
}
} }
ThreadExecutor.join() ThreadExecutor.join()
@@ -57,14 +46,22 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
Block.AIR, Block.DIRT, Block.STONE Block.AIR, Block.DIRT, Block.STONE
) )
private fun draw(x: Int, y: Int, noiseValue: List<Double>, world: GameWorld) { private fun Double.tiered(vararg tiers: Double): Int {
fun Double.tiered(vararg tiers: Double): Int {
tiers.reversed().forEachIndexed { index, it -> tiers.reversed().forEachIndexed { index, it ->
if (this >= it) return (tiers.lastIndex - index) // why?? if (this >= it) return (tiers.lastIndex - index) // why??
} }
return tiers.lastIndex return tiers.lastIndex
} }
//private fun draw(x: Int, y: Int, width: Int, height: Int, noiseValue: List<Double>, world: GameWorld) {
private fun draw(x: Int, noises: List<Joise>, st: Double, soff: Double) {
for (y in 0 until world.height) {
val sx = sin(st) * soff + soff // plus sampleOffset to make only
val sz = cos(st) * soff + soff // positive points are to be sampled
val sy = y - (world.height - YHEIGHT_MAGIC) * YHEIGHT_DIVISOR // Q&D offsetting to make ratio of sky:ground to be constant
// DEBUG NOTE: it is the OFFSET FROM THE IDEAL VALUE (observed land height - (HEIGHT * DIVISOR)) that must be constant
val noiseValue = noises.map { it.get(sx, sy, sz) }
val terr = noiseValue[0].tiered(.0, .5, .88) val terr = noiseValue[0].tiered(.0, .5, .88)
val cave = if (noiseValue[1] < 0.5) 0 else 1 val cave = if (noiseValue[1] < 0.5) 0 else 1
@@ -74,6 +71,7 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
world.setTileTerrain(x, y, terrBlock, true) world.setTileTerrain(x, y, terrBlock, true)
world.setTileWall(x, y, wallBlock, true) world.setTileWall(x, y, wallBlock, true)
} }
}
private fun getGenerator(seed: Long, params: TerragenParams): List<Joise> { private fun getGenerator(seed: Long, params: TerragenParams): List<Joise> {