able to load chunked world save

This commit is contained in:
minjaesong
2021-09-11 18:44:05 +09:00
parent 9fa0e7debe
commit b4813a1ebe
10 changed files with 77 additions and 30 deletions

View File

@@ -75,9 +75,6 @@ object Common {
}
override fun read(json: Json, jsonData: JsonValue, type: Class<*>): BlockLayer {
// full auto
//return strToBlockLayer(json.fromJson(type, jsonData.toJson(JsonWriter.OutputType.minimal)) as LayerInfo)
// full manual
try {
return strToBlockLayer(LayerInfo(

View File

@@ -21,7 +21,6 @@ object WriteMeta {
val meta = WorldMeta(
genver = Common.GENVER,
savename = world.worldName,
terrseed = world.generatorSeed,
randseed0 = RoguelikeRandomiser.RNG.state0,
randseed1 = RoguelikeRandomiser.RNG.state1,
weatseed0 = WeatherMixer.RNG.state0,
@@ -46,7 +45,6 @@ object WriteMeta {
data class WorldMeta(
val genver: Int = -1,
val savename: String = "",
val terrseed: Long = 0,
val randseed0: Long = 0,
val randseed1: Long = 0,
val weatseed0: Long = 0,

View File

@@ -5,6 +5,7 @@ import net.torvald.gdx.graphics.PixmapIO2
import net.torvald.terrarum.*
import net.torvald.terrarum.App.printdbg
import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.gameworld.BlockLayer
import net.torvald.terrarum.modulebasegame.IngameRenderer
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.IngamePlayer
@@ -97,11 +98,11 @@ object WriteSavegame {
// Write World //
val worldNum = ingame.world.worldIndex
/*val worldContent = EntryFile(WriteWorld.encodeToByteArray64(ingame))
val world = DiskEntry(worldNum.toLong(), 0, "world${worldNum}".toByteArray(Common.CHARSET), creation_t, time_t, worldContent)
addFile(disk, world)*/
val worldMeta = EntryFile(WriteWorld.encodeToByteArray64(ingame))
val world = DiskEntry(worldNum.toLong(), 0, creation_t, time_t, worldMeta)
addFile(disk, world)
val layers = arrayOf(ingame.world.layerTerrain, ingame.world.layerWall)
val layers = intArrayOf(0,1).map { ingame.world.getLayer(it) }
val cw = ingame.world.width / LandUtil.CHUNK_W
val ch = ingame.world.height / LandUtil.CHUNK_H
for (layer in layers.indices) {
@@ -119,8 +120,6 @@ object WriteSavegame {
}
}
// TODO save worldinfo
// Write Actors //
listOf(ingame.actorContainerActive, ingame.actorContainerInactive).forEach { actors ->
@@ -174,12 +173,31 @@ object LoadSavegame {
// val faction = Common.jsoner.fromJson(FactionCodex.javaClass, getUnzipInputStream(getFileBytes(disk, -20)))
val apocryphas = Common.jsoner.fromJson(Apocryphas.javaClass, getUnzipInputStream(getFileBytes(disk, -1024)))
// set lateinit vars on the gameworld
world.layerTerrain = BlockLayer(world.width, world.height)
world.layerWall = BlockLayer(world.width, world.height)
val worldParam = TerrarumIngame.Codices(meta, item, apocryphas)
newIngame.world = world
newIngame.gameLoadInfoPayload = worldParam
newIngame.gameLoadMode = TerrarumIngame.GameLoadMode.LOAD_FROM
newIngame.savegameArchive = disk
// load all the world blocklayer chunks
val worldnum = world.worldIndex.toLong()
val cw = LandUtil.CHUNK_W; val ch = LandUtil.CHUNK_H
val chunksX = world.width / cw
for (layer in 0L..1L) {
val worldLayer = world.getLayer(layer.toInt())
for (chunk in 0L until (world.width * world.height) / (cw * ch)) {
val chunkFile = VDUtil.getAsNormalFile(disk, worldnum.shl(32) or layer.shl(24) or chunk)
val cx = chunk % chunksX
val cy = chunk / chunksX
ReadWorld.decodeChunkToLayer(chunkFile.getContent(), worldLayer, cx.toInt(), cy.toInt())
}
}
actors.forEach { newIngame.addNewActor(it) }
// by doing this, whatever the "possession" the player had will be broken by the game load

View File

@@ -7,10 +7,11 @@ import net.torvald.terrarum.gameactors.ActorID
import net.torvald.terrarum.gameactors.BlockMarkerActor
import net.torvald.terrarum.gameworld.BlockLayer
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.gameworld.GameWorldTitleScreen
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.realestate.LandUtil
import net.torvald.terrarum.tvda.ByteArray64
import net.torvald.terrarum.tvda.ByteArray64Writer
import net.torvald.terrarum.realestate.LandUtil
import java.io.Reader
/**
@@ -75,6 +76,9 @@ object WriteWorld {
*/
object ReadWorld {
fun readLayerFormat(worldDataStream: Reader): GameWorld =
fillInDetails(Common.jsoner.fromJson(GameWorldTitleScreen::class.java, worldDataStream))
operator fun invoke(worldDataStream: Reader): GameWorld =
fillInDetails(Common.jsoner.fromJson(GameWorld::class.java, worldDataStream))
@@ -87,7 +91,7 @@ object ReadWorld {
}
fun readWorldAndSetNewWorld(ingame: TerrarumIngame, worldDataStream: Reader): GameWorld {
val world = invoke(worldDataStream)
val world = readLayerFormat(worldDataStream)
ingame.world = world
return world
}
@@ -95,7 +99,7 @@ object ReadWorld {
private val cw = LandUtil.CHUNK_W
private val ch = LandUtil.CHUNK_H
fun decodeToLayer(chunk: ByteArray64, targetLayer: BlockLayer, cx: Int, cy: Int) {
fun decodeChunkToLayer(chunk: ByteArray64, targetLayer: BlockLayer, cx: Int, cy: Int) {
val bytes = Common.unzip(chunk)
if (bytes.size != cw * ch * 2L)
throw UnsupportedOperationException("Chunk size mismatch: decoded chunk size is ${bytes.size} bytes " +
@@ -105,7 +109,14 @@ object ReadWorld {
val tilenum = bytes[2L*k].toUint().shl(8) or bytes[2L*k + 1].toUint()
val offx = k % cw
val offy = k / cw
targetLayer.unsafeSetTile(cx * cw + offx, cy * ch * offy, tilenum)
// try {
targetLayer.unsafeSetTile(cx * cw + offx, cy * ch + offy, tilenum)
// }
// catch (e: IndexOutOfBoundsException) {
// printdbgerr(this, "IndexOutOfBoundsException, cx = $cx, cy = $cy, k = $k, offx = $offx, offy = $offy")
// throw e
// }
}
}