From 652dfe39eb644d962c2b43a3de27ee432a0f05af Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 4 Oct 2023 17:34:16 +0900 Subject: [PATCH] in preparation of new demoworld creation for future version --- assets/mods/basegame/blocks/blocks.csv | 1 + assets/mods/basegame/commands.csv | 1 + .../torvald/terrarum/blockproperties/Block.kt | 1 + .../terrarum/blockproperties/BlockProp.kt | 2 +- .../torvald/terrarum/gameworld/GameWorld.kt | 29 ++++++++---- .../modulebasegame/console/ExportWorld.kt | 45 +++++++++++++++++++ .../modulebasegame/console/ImportWorld.kt | 2 + .../terrarum/serialise/ReadSimpleWorld.kt | 3 +- .../terrarum/worlddrawer/LightmapRenderer.kt | 4 +- 9 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/net/torvald/terrarum/modulebasegame/console/ExportWorld.kt diff --git a/assets/mods/basegame/blocks/blocks.csv b/assets/mods/basegame/blocks/blocks.csv index 959b9726a..22ce66122 100644 --- a/assets/mods/basegame/blocks/blocks.csv +++ b/assets/mods/basegame/blocks/blocks.csv @@ -1,5 +1,6 @@ "id";"drop";"spawn";"name";"shdr";"shdg";"shdb";"shduv";"str";"dsty";"mate";"solid";"wall";"grav";"dlfn";"fv";"fr";"lumr";"lumg";"lumb";"lumuv";"colour";"vscs";"refl";"tags" "0";"0";"0";"BLOCK_AIR";"0.0312";"0.0312";"0.0312";"0.0312";"1";"1";"NULL";"0";"1";"N/A";"0";"0";"4";"0.0000";"0.0000";"0.0000";"0.0000";"N/A";"N/A";"0.0";"INCONSEQUENTIAL,AIR" +"1";"0";"0";"BLOCK_UPDATE";"0.0312";"0.0312";"0.0312";"0.0312";"1";"1";"NULL";"0";"1";"N/A";"0";"0";"4";"0.0000";"0.0000";"0.0000";"0.0000";"N/A";"N/A";"0.0";"INTERNAL" "16";"17";"17";"BLOCK_STONE";"0.1252";"0.1252";"0.1252";"0.1252";"48";"2400";"ROCK";"1";"1";"N/A";"0";"4";"16";"0.0000";"0.0000";"0.0000";"0.0000";"N/A";"N/A";"0.0";"ROCK,NATURAL" "17";"17";"17";"BLOCK_STONE_QUARRIED";"0.1252";"0.1252";"0.1252";"0.1252";"48";"2400";"ROCK";"1";"1";"N/A";"0";"4";"16";"0.0000";"0.0000";"0.0000";"0.0000";"N/A";"N/A";"0.0";"ROCK" "18";"18";"18";"BLOCK_STONE_TILE_WHITE";"0.1252";"0.1252";"0.1252";"0.1252";"48";"2400";"ROCK";"1";"1";"N/A";"0";"4";"16";"0.0000";"0.0000";"0.0000";"0.0000";"N/A";"N/A";"0.18";"STONE" diff --git a/assets/mods/basegame/commands.csv b/assets/mods/basegame/commands.csv index 80b92454a..60c4d185c 100644 --- a/assets/mods/basegame/commands.csv +++ b/assets/mods/basegame/commands.csv @@ -3,6 +3,7 @@ CheatWarnTest CodexEdictis ExportCodices ExportMap +ExportWorld ForceGC GetAV GetFaction diff --git a/src/net/torvald/terrarum/blockproperties/Block.kt b/src/net/torvald/terrarum/blockproperties/Block.kt index 7270d00f7..bbe346c96 100644 --- a/src/net/torvald/terrarum/blockproperties/Block.kt +++ b/src/net/torvald/terrarum/blockproperties/Block.kt @@ -6,6 +6,7 @@ package net.torvald.terrarum.blockproperties object Block { const val AIR = "basegame:0" // hard coded; this is the standard + const val UPDATE = "basegame:1" // hard coded const val STONE = "basegame:16" const val STONE_QUARRIED = "basegame:17" diff --git a/src/net/torvald/terrarum/blockproperties/BlockProp.kt b/src/net/torvald/terrarum/blockproperties/BlockProp.kt index ac75be5d0..dba818ef5 100644 --- a/src/net/torvald/terrarum/blockproperties/BlockProp.kt +++ b/src/net/torvald/terrarum/blockproperties/BlockProp.kt @@ -22,7 +22,7 @@ class BlockProp { var shadeColB = 0f var shadeColA = 0f - lateinit var opacity: Cvec + var opacity = Cvec() fun getOpacity(channel: Int) = when (channel) { 0 -> shadeColR diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index 0e709dc9e..0cd909fcf 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -38,9 +38,10 @@ class PhysicalStatus() { /** * Special version of GameWorld where everything, including layer data, are saved in a single JSON file (i.e. not chunked) */ -class SimpleGameWorld : GameWorld() { +class SimpleGameWorld(width: Int, height: Int) : GameWorld(width, height) { override lateinit var layerWall: BlockLayer override lateinit var layerTerrain: BlockLayer + constructor() : this(0, 0) } open class GameWorld( @@ -48,10 +49,14 @@ open class GameWorld( ) : Disposable { constructor() : this(UUID.randomUUID()) + constructor(width: Int, height: Int) : this(UUID.randomUUID()) { + this.width = width + this.height = height + } var worldCreator: UUID = UUID(0L,0L) // TODO record a value to this - var width: Int = 999; private set - var height: Int = 999; private set + var width: Int = 0; private set + var height: Int = 0; private set var playersLastStatus = PlayersLastStatus() // only gets used when the game saves and loads @@ -208,17 +213,24 @@ open class GameWorld( if (App.tileMaker != null) { App.tileMaker.tags.forEach { - printdbg(this, "tileNumber ${it.value.tileNumber} <-> tileName ${it.key}") + if (!forcedTileNumberToNames.contains(it.key)) { + printdbg(this, "tileNumber ${it.value.tileNumber} <-> tileName ${it.key}") - tileNumberToNameMap[it.value.tileNumber.toLong()] = it.key - tileNameToNumberMap[it.key] = it.value.tileNumber + tileNumberToNameMap[it.value.tileNumber.toLong()] = it.key + tileNameToNumberMap[it.key] = it.value.tileNumber + } } // AN EXCEPTIONAL TERM: tilenum 0 is always redirected to Air tile, even if the tilenum for actual Air tile is not zero tileNumberToNameMap[0] = Block.AIR + tileNumberToNameMap[2] = Block.UPDATE } } + private val forcedTileNumberToNames = hashSetOf( + Block.AIR, Block.UPDATE + ) + fun coordInWorld(x: Int, y: Int) = y in 0 until height // ROUNDWORLD implementation fun coordInWorldStrict(x: Int, y: Int) = x in 0 until width && y in 0 until height // ROUNDWORLD implementation @@ -242,6 +254,7 @@ open class GameWorld( // AN EXCEPTIONAL TERM: tilenum 0 is always redirected to Air tile, even if the tilenum for actual Air tile is not zero tileNumberToNameMap[0] = Block.AIR + tileNumberToNameMap[2] = Block.UPDATE } /** @@ -265,7 +278,7 @@ open class GameWorld( */ fun getTileFromWall(rawX: Int, rawY: Int): ItemID { val (x, y) = coerceXY(rawX, rawY) - return tileNumberToNameMap[layerWall.unsafeGetTile(x, y).toLong()] ?: throw NoSuchElementException("No tile name mapping for wall ${layerWall.unsafeGetTile(x, y)} in ($x, $y) from $layerWall") + return tileNumberToNameMap[layerWall.unsafeGetTile(x, y).toLong()] ?: Block.UPDATE//throw NoSuchElementException("No tile name mapping for wall ${layerWall.unsafeGetTile(x, y)} in ($x, $y) from $layerWall") } /** @@ -273,7 +286,7 @@ open class GameWorld( */ fun getTileFromTerrain(rawX: Int, rawY: Int): ItemID { val (x, y) = coerceXY(rawX, rawY) - return tileNumberToNameMap[layerTerrain.unsafeGetTile(x, y).toLong()] ?: throw NoSuchElementException("No tile name mapping for terrain ${layerTerrain.unsafeGetTile(x, y)} in ($x, $y) from $layerTerrain") + return tileNumberToNameMap[layerTerrain.unsafeGetTile(x, y).toLong()] ?: Block.UPDATE//throw NoSuchElementException("No tile name mapping for terrain ${layerTerrain.unsafeGetTile(x, y)} in ($x, $y) from $layerTerrain") } /** diff --git a/src/net/torvald/terrarum/modulebasegame/console/ExportWorld.kt b/src/net/torvald/terrarum/modulebasegame/console/ExportWorld.kt new file mode 100644 index 000000000..3f6e3ea83 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/console/ExportWorld.kt @@ -0,0 +1,45 @@ +package net.torvald.terrarum.modulebasegame.console + +import net.torvald.terrarum.App +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.console.ConsoleCommand +import net.torvald.terrarum.console.Echo +import net.torvald.terrarum.gameworld.SimpleGameWorld +import net.torvald.terrarum.modulebasegame.TerrarumIngame +import net.torvald.terrarum.serialise.WriteSimpleWorld +import java.io.File +import java.io.IOException + +/** + * Used to create the titlescreen world + * + * Created by minjaesong on 2023-10-04. + */ +object ExportWorld : ConsoleCommand { + override fun execute(args: Array) { + if (args.size == 2) { + try { + val ingame = Terrarum.ingame!! as TerrarumIngame + val file = File(App.defaultDir + "/Exports/${args[1]}.json") + val simpleworld = SimpleGameWorld(ingame.world.width, ingame.world.height).also { + it.layerTerrain = ingame.world.layerTerrain + it.layerWall = ingame.world.layerWall + it.tileNumberToNameMap.putAll(ingame.world.tileNumberToNameMap) + } + file.writeText(WriteSimpleWorld(ingame, simpleworld, listOf())) + Echo("Exportworld: exported the world as ${args[1]}.json") + } + catch (e: IOException) { + Echo("Exportworld: IOException raised.") + e.printStackTrace() + } + } + else { + printUsage() + } + } + + override fun printUsage() { + Echo("Usage: exportworld filename-without-extension") + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/console/ImportWorld.kt b/src/net/torvald/terrarum/modulebasegame/console/ImportWorld.kt index 421767934..21c8691c9 100644 --- a/src/net/torvald/terrarum/modulebasegame/console/ImportWorld.kt +++ b/src/net/torvald/terrarum/modulebasegame/console/ImportWorld.kt @@ -10,6 +10,8 @@ import java.io.File import java.io.IOException /** + * Used to debug the titlescreen world + * * Created by minjaesong on 2021-08-25. */ object ImportWorld : ConsoleCommand { diff --git a/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt b/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt index 9ed647279..2a6ee3e86 100644 --- a/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt +++ b/src/net/torvald/terrarum/serialise/ReadSimpleWorld.kt @@ -51,7 +51,8 @@ object WriteSimpleWorld { operator fun invoke(ingame: IngameInstance, world: SimpleGameWorld, actorsList: List): String { val time_t = App.getTIME_T() - val s = Common.jsoner.toJson(preWrite(ingame, time_t, world, actorsList)) + preWrite(ingame, time_t, world, actorsList) + val s = Common.jsoner.toJson(world) return """{"genver":${Common.GENVER},${s.substring(1)}""" } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt index 8d6120e32..10494213e 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt @@ -551,8 +551,8 @@ object LightmapRenderer { _thisTerrain = world.getTileFromTerrainRaw(worldX, worldY) _thisTerrainProp = BlockCodex[world.tileNumberToNameMap[_thisTerrain.toLong()]] - _reflectanceAccumulator.set(App.tileMaker.terrainTileColourMap[_thisTerrainProp.id]!!) - _reflectanceAccumulator.a = 0f // temporarily disabled + _reflectanceAccumulator.set(App.tileMaker.terrainTileColourMap[_thisTerrainProp.id] ?: Cvec()) + _reflectanceAccumulator.a = 0f // TODO temporarily disabled _reflectanceAccumulator.mul(_thisTerrainProp.reflectance).mul(giScale) _mapLightLevelThis.max(lx, ly, _reflectanceAccumulator)