From cbbe5d3e34aef3c3c106936e91356fcc642aba88 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 31 Oct 2023 15:18:51 +0900 Subject: [PATCH] worldgen estimation change; common worldgen variables moved to Worldgen.kt --- .../terrarum/modulebasegame/ui/UINewWorld.kt | 6 ++++-- .../modulebasegame/ui/UIWorldPortalSearch.kt | 6 ++++-- .../modulebasegame/worldgenerator/Biomegen.kt | 12 ++++-------- .../modulebasegame/worldgenerator/Oregen.kt | 12 ++++-------- .../worldgenerator/OregenAutotiling.kt | 11 ++++------- .../modulebasegame/worldgenerator/Terragen.kt | 13 ++++--------- .../modulebasegame/worldgenerator/Worldgen.kt | 9 ++++++++- 7 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UINewWorld.kt b/src/net/torvald/terrarum/modulebasegame/ui/UINewWorld.kt index 3ab0b73de..5bf877c6e 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UINewWorld.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UINewWorld.kt @@ -300,10 +300,12 @@ class UINewWorld(val remoCon: UIRemoCon) : UICanvas() { App.fontGame.draw(batch, Lang["CONTEXT_GENERATOR_SEED"], drawX - 4, drawY + sizeSelY + inputLineY2) val (wx, wy) = TerrarumIngame.NEW_WORLD_SIZE[sizeSelector.selection] - val etaMin = Worldgen.getEstimationSec(wx, wy).div(60f).roundToInt().coerceAtLeast(1) + val etaSec = Worldgen.getEstimationSec(wx, wy) + val etaMin = etaSec.div(60f).roundToInt().coerceAtLeast(1) val etaText = Lang.getAndUseTemplate("CONTEXT_ESTIMATED_MINUTES_PLURAL", true, etaMin) + val etaTextPrint = etaText + (if (App.IS_DEVELOPMENT_BUILD) " ($etaSec s)" else "") - Toolkit.drawTextCentered(batch, App.fontGame, etaText, width, drawX, drawY + sizeSelY + inputLineY3) + Toolkit.drawTextCentered(batch, App.fontGame, etaTextPrint, width, drawX, drawY + sizeSelY + inputLineY3) } else if (mode == 1) { // code input labels diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt index 4d437bed8..2a1f449cd 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIWorldPortalSearch.kt @@ -195,10 +195,12 @@ class UIWorldPortalSearch(val full: UIWorldPortal) : UICanvas() { App.fontGame.draw(batch, Lang["CONTEXT_PLACE_COORDINATE"], drawX - 4, drawY + sizeSelY + inputLineY2) val (wx, wy) = TerrarumIngame.NEW_WORLD_SIZE[sizeSelector.selection] - val etaMin = Worldgen.getEstimationSec(wx, wy).div(60f).roundToInt().coerceAtLeast(1) + val etaSec = Worldgen.getEstimationSec(wx, wy) + val etaMin = etaSec.div(60f).roundToInt().coerceAtLeast(1) val etaText = Lang.getAndUseTemplate("CONTEXT_ESTIMATED_MINUTES_PLURAL", true, etaMin) + val etaTextPrint = etaText + (if (App.IS_DEVELOPMENT_BUILD) " ($etaSec s)" else "") - Toolkit.drawTextCentered(batch, App.fontGame, etaText, width, drawX, drawY + sizeSelY + inputLineY3) + Toolkit.drawTextCentered(batch, App.fontGame, etaTextPrint, width, drawX, drawY + sizeSelY + inputLineY3) // memory gauge val chunksUsed = full.chunksUsed diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt index 334864ef8..20adab1bc 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt @@ -19,19 +19,15 @@ import kotlin.math.sin */ class Biomegen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, params) { - private val threadExecutor = TerrarumIngame.worldgenThreadExecutor - - private val genSlices = max(threadExecutor.threadCount, world.width / 9) - private val YHEIGHT_MAGIC = 2800.0 / 3.0 private val YHEIGHT_DIVISOR = 2.0 / 7.0 override fun getDone(loadscreen: LoadScreenBase) { // loadscreen.progress.set((loadscreen.progress.get() + 0x1_000000_000000L) and 0x7FFF_000000_000000L) - threadExecutor.renew() - (0 until world.width).sliceEvenly(genSlices).map { xs -> - threadExecutor.submit { + Worldgen.threadExecutor.renew() + (0 until world.width).sliceEvenly(Worldgen.genSlices).map { xs -> + Worldgen.threadExecutor.submit { val localJoise = getGenerator(seed, params as BiomegenParams) for (x in xs) { for (y in 0 until world.height) { @@ -49,7 +45,7 @@ class Biomegen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par } } - threadExecutor.join() + Worldgen.threadExecutor.join() App.printdbg(this, "Waking up Worldgen") } diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Oregen.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Oregen.kt index 24a201d46..b634bd1b6 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Oregen.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Oregen.kt @@ -18,17 +18,13 @@ import kotlin.math.sqrt * Created by minjaesong on 2023-10-25. */ class Oregen(world: GameWorld, private val caveAttenuateBiasScaled: ModuleScaleDomain, seed: Long, private val ores: List) : Gen(world, seed) { - - private val threadExecutor = TerrarumIngame.worldgenThreadExecutor - private val genSlices = max(threadExecutor.threadCount, world.width / 9) - override fun getDone(loadscreen: LoadScreenBase) { loadscreen.stageValue += 1 loadscreen.progress.set(0L) - threadExecutor.renew() - (0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs -> - threadExecutor.submit { + Worldgen.threadExecutor.renew() + (0 until world.width).sliceEvenly(Worldgen.genSlices).mapIndexed { i, xs -> + Worldgen.threadExecutor.submit { val localJoise = getGenerator(seed) for (x in xs) { val sampleTheta = (x.toDouble() / world.width) * TWO_PI @@ -39,7 +35,7 @@ class Oregen(world: GameWorld, private val caveAttenuateBiasScaled: ModuleScaleD } } - threadExecutor.join() + Worldgen.threadExecutor.join() } /** diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/OregenAutotiling.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/OregenAutotiling.kt index a75ad6172..f62b7433b 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/OregenAutotiling.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/OregenAutotiling.kt @@ -20,20 +20,17 @@ import kotlin.math.max */ class OregenAutotiling(world: GameWorld, seed: Long, val tilingModes: HashMap) : Gen(world, seed) { - private val threadExecutor = TerrarumIngame.worldgenThreadExecutor - private val genSlices = max(threadExecutor.threadCount, world.width / 9) - override fun getDone(loadscreen: LoadScreenBase) { - threadExecutor.renew() - (0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs -> - threadExecutor.submit { + Worldgen.threadExecutor.renew() + (0 until world.width).sliceEvenly(Worldgen.genSlices).mapIndexed { i, xs -> + Worldgen.threadExecutor.submit { for (x in xs) { draw(x) } } } - threadExecutor.join() + Worldgen.threadExecutor.join() } private fun getHashCoord(x: Int, y: Int, mod: Int) = diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt index d8fc8a7a8..bbbfacd0a 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt @@ -24,11 +24,6 @@ class Terragen(world: GameWorld, val highlandLowlandSelectCache: ModuleCache, se const val YHEIGHT_DIVISOR = 2.0 / 7.0 } - private val threadExecutor = TerrarumIngame.worldgenThreadExecutor - - private val genSlices = max(threadExecutor.threadCount, world.width / 9) - - private val dirtStoneDitherSize = 3 // actual dither size will be double of this value private val stoneSlateDitherSize = 4 @@ -36,9 +31,9 @@ class Terragen(world: GameWorld, val highlandLowlandSelectCache: ModuleCache, se loadscreen.stageValue += 1 loadscreen.progress.set(0L) - threadExecutor.renew() - (0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs -> - threadExecutor.submit { + Worldgen.threadExecutor.renew() + (0 until world.width).sliceEvenly(Worldgen.genSlices).mapIndexed { i, xs -> + Worldgen.threadExecutor.submit { val localJoise = getGenerator(seed, params as TerragenParams) for (x in xs) { val sampleTheta = (x.toDouble() / world.width) * TWO_PI @@ -49,7 +44,7 @@ class Terragen(world: GameWorld, val highlandLowlandSelectCache: ModuleCache, se } } - threadExecutor.join() + Worldgen.threadExecutor.join() printdbg(this, "Waking up Worldgen") } diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Worldgen.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Worldgen.kt index 0c11d63a5..436574a75 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Worldgen.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Worldgen.kt @@ -8,6 +8,8 @@ import net.torvald.terrarum.BlockCodex import net.torvald.terrarum.LoadScreenBase import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.gameworld.GameWorld +import net.torvald.terrarum.modulebasegame.TerrarumIngame +import kotlin.math.max import kotlin.math.roundToLong /** @@ -21,6 +23,9 @@ object Worldgen { lateinit var params: WorldgenParams private set + val threadExecutor = TerrarumIngame.worldgenThreadExecutor + var genSlices = -1 + private val threadLock = java.lang.Object() fun attachMap(world: GameWorld, genParams: WorldgenParams) { @@ -62,6 +67,8 @@ object Worldgen { fun generateMap(loadscreen: LoadScreenBase) { highlandLowlandSelectCache = getHighlandLowlandSelectCache(params.terragenParams, params.seed) caveAttenuateBiasScaled = getCaveAttenuateBiasScaled(highlandLowlandSelectCache, params.terragenParams) + genSlices = max(threadExecutor.threadCount, world.width / 9) + val jobs = getJobs() @@ -99,7 +106,7 @@ object Worldgen { data class Work(val loadingScreenName: String, val theWork: Gen, val tags: List) fun getEstimationSec(width: Int, height: Int): Long { - return (23.15 * 1.25 * (48600000.0 / bogoflops) * ((width * height) / 40095000.0) * (32.0 / THREAD_COUNT)).roundToLong() + return (23.15 * 1.25 * (48600000.0 / bogoflops) * ((width * height) / 20095000.0) * (32.0 / THREAD_COUNT)).roundToLong() } private fun getHighlandLowlandSelectCache(params: TerragenParams, seed: Long): ModuleCache {