game load wip

This commit is contained in:
minjaesong
2021-09-03 17:54:10 +09:00
parent 42ea79bcc2
commit 57e4b83649
27 changed files with 542 additions and 462 deletions

View File

@@ -9,6 +9,7 @@ import net.torvald.terrarum.*
import net.torvald.terrarum.AppLoader.*
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED
import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.blockproperties.BlockPropUtil
import net.torvald.terrarum.blockproperties.WireCodex
import net.torvald.terrarum.blockstats.BlockStats
@@ -23,17 +24,24 @@ import net.torvald.terrarum.gameitem.GameItem
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.console.AVTracker
import net.torvald.terrarum.console.ActorsList
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameparticles.ParticleBase
import net.torvald.terrarum.gameactors.WireActor
import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.modulebasegame.gameactors.*
import net.torvald.terrarum.modulebasegame.gameactors.physicssolver.CollisionSolver
import net.torvald.terrarum.gameworld.WorldSimulator
import net.torvald.terrarum.itemproperties.MaterialCodex
import net.torvald.terrarum.modulebasegame.gameworld.GameEconomy
import net.torvald.terrarum.modulebasegame.ui.*
import net.torvald.terrarum.modulebasegame.worldgenerator.RoguelikeRandomiser
import net.torvald.terrarum.weather.WeatherMixer
import net.torvald.terrarum.modulebasegame.worldgenerator.Worldgen
import net.torvald.terrarum.modulebasegame.worldgenerator.WorldgenParams
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.VDUtil
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.VirtualDisk
import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.serialise.WriteMeta
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.worlddrawer.BlocksDrawer
import net.torvald.terrarum.worlddrawer.FeaturesDrawer
@@ -221,7 +229,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
when (gameLoadMode) {
GameLoadMode.CREATE_NEW -> enterCreateNewWorld(gameLoadInfoPayload as NewWorldParameters)
GameLoadMode.LOAD_FROM -> enterLoadFromSave(gameLoadInfoPayload as GameSaveData)
GameLoadMode.LOAD_FROM -> enterLoadFromSave(gameLoadInfoPayload as WriteMeta.WorldMeta)
}
IngameRenderer.setRenderedWorld(world)
@@ -230,16 +238,6 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
super.show() // gameInitialised = true
}
data class GameSaveData(
val world: GameWorld,
val historicalFigureIDBucket: ArrayList<Int>,
val realGamePlayer: IngamePlayer,
val rogueS0: Long,
val rogueS1: Long,
val weatherS0: Long,
val weatherS1: Long
)
data class NewWorldParameters(
val width: Int,
val height: Int,
@@ -259,12 +257,30 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
/**
* Init instance by loading saved world
*/
private fun enterLoadFromSave(gameSaveData: GameSaveData) {
private fun enterLoadFromSave(meta: WriteMeta.WorldMeta) {
if (gameInitialised) {
printdbg(this, "loaded successfully.")
}
else {
TODO()
RoguelikeRandomiser.loadFromSave(meta.randseed0, meta.randseed1)
WeatherMixer.loadFromSave(meta.weatseed0, meta.weatseed1)
// Load BlockCodex //
BlockCodex.clear()
meta.blocks.forEach { module, csv -> BlockCodex.fromCSV(module, csv.doc) }
// Load WireCodex //
WireCodex.clear()
meta.wires.forEach { module, csv -> WireCodex.fromCSV(module, ModMgr.getPath(module, "wires/"), csv.doc) }
// Load ItemCodex //
ItemCodex.clear()
meta.items.forEach { module, csv -> ModMgr.GameItemLoader.fromCSV(module, csv.doc) }
// TODO registerNewDynamicItem
// Load MaterialCodex //
MaterialCodex.clear()
meta.materials.forEach { module, csv -> MaterialCodex.fromCSV(module, csv.doc) }
}
}
@@ -284,7 +300,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
// init map as chosen size
val timeNow = System.currentTimeMillis() / 1000
val timeNow = AppLoader.getTIME_T()
world = GameWorld(1, worldParams.width, worldParams.height, timeNow, timeNow, 0) // new game, so the creation time is right now
gameworldIndices.add(world.worldIndex)
world.extraFields["basegame.economy"] = GameEconomy()
@@ -307,10 +323,17 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
/** Load rest of the game with GL context */
fun postInit() {
//setTheRealGamerFirstTime(PlayerBuilderSigrid())
setTheRealGamerFirstTime(PlayerBuilderTestSubject1())
if (actorNowPlaying == null) {
//setTheRealGamerFirstTime(PlayerBuilderSigrid())
setTheRealGamerFirstTime(PlayerBuilderTestSubject1())
// setTheRealGamerFirstTime(PlayerBuilderWerebeastTest())
savegameArchive = VDUtil.createNewDisk(
1L shl 60,
actorNowPlaying!!.actorValue.getAsString(AVKey.NAME) ?: "Player ${AppLoader.getTIME_T()}",
Common.CHARSET
)
}
MegaRainGovernor // invoke MegaRain Governor

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.modulebasegame.console
import com.badlogic.gdx.utils.Json
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.Terrarum.ingame
import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.modulebasegame.TerrarumIngame
@@ -19,7 +20,8 @@ import java.io.IOException
object ExportMeta : ConsoleCommand {
override fun execute(args: Array<String>) {
try {
val str = WriteMeta(Terrarum.ingame!! as TerrarumIngame).invoke()
val currentPlayTime_t = AppLoader.getTIME_T() - ingame!!.loadedTime_t
val str = WriteMeta(ingame!! as TerrarumIngame, currentPlayTime_t)
val writer = java.io.FileWriter(AppLoader.defaultDir + "/Exports/savegame.json", false)
writer.write(str)
writer.close()
@@ -40,7 +42,7 @@ object ExportWorld : ConsoleCommand {
override fun execute(args: Array<String>) {
if (args.size == 2) {
try {
val str = WriteWorld(Terrarum.ingame!! as TerrarumIngame).invoke()
val str = WriteWorld(ingame!! as TerrarumIngame)
val writer = java.io.FileWriter(AppLoader.defaultDir + "/Exports/${args[1]}.json", false)
writer.write(str)
writer.close()

View File

@@ -18,7 +18,7 @@ object ImportWorld : ConsoleCommand {
if (args.size == 2) {
try {
val reader = java.io.FileReader(AppLoader.defaultDir + "/Exports/${args[1]}.json")
ReadWorld(Terrarum.ingame!! as TerrarumIngame).invoke(reader)
ReadWorld(Terrarum.ingame!! as TerrarumIngame, reader)
Echo("Importworld: imported a world from ${args[1]}.json")
}
catch (e: IOException) {
@@ -41,7 +41,7 @@ object ImportActor : ConsoleCommand {
if (args.size == 2) {
try {
val reader = java.io.FileReader(AppLoader.defaultDir + "/Exports/${args[1]}.json")
ReadActor(Terrarum.ingame!! as TerrarumIngame).invoke(reader)
ReadActor(Terrarum.ingame!! as TerrarumIngame, reader)
Echo("Importactor: imported an actor from ${args[1]}.json")
}
catch (e: IOException) {

View File

@@ -23,17 +23,9 @@ object Load : ConsoleCommand {
val charset = Common.CHARSET
val file = File(AppLoader.defaultDir + "/Exports/${args[1]}")
val disk = VDUtil.readDiskArchive(file, charset = charset)
val meta = ReadMeta(disk)
val metaFile = disk.entries[-1]!!
val metaReader = ByteArray64Reader((metaFile.contents as EntryFile).getContent(), Common.CHARSET)
val meta = Common.jsoner.fromJson(WriteMeta.WorldMeta::class.java, metaReader)
WriteMeta.WorldMeta::class.declaredMemberProperties.forEach {
println("${it.name} = ${it.get(meta)}")
}
println(WriteMeta.unasciiAndUnzipStr(meta.blocks))
meta.blocks.forEach { s, str -> println("Module $s\n"); println(str.doc) }
println(meta.loadorder.joinToString())
}
catch (e: IOException) {

View File

@@ -11,10 +11,7 @@ import net.torvald.terrarum.gameactors.Actor
import net.torvald.terrarum.gameactors.BlockMarkerActor
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.*
import net.torvald.terrarum.serialise.Common
import net.torvald.terrarum.serialise.WriteActor
import net.torvald.terrarum.serialise.WriteMeta
import net.torvald.terrarum.serialise.WriteWorld
import net.torvald.terrarum.serialise.*
import java.io.File
import java.io.IOException
@@ -40,33 +37,11 @@ object Save : ConsoleCommand {
try {
val ingame = Terrarum.ingame!! as TerrarumIngame
val savename = args[1].trim()
val creation_t = VDUtil.currentUnixtime
val time_t = VDUtil.currentUnixtime
val disk = VDUtil.createNewDisk(1L shl 60, savename, Common.CHARSET)
val file = File(AppLoader.defaultDir + "/Exports/${args[1]}")
// NOTE: don't bother with the entryID of DiskEntries; it will be overwritten anyway
val metaContent = EntryFile(WriteMeta(ingame).encodeToByteArray64())
val meta = DiskEntry(-1, 0, "savegame".toByteArray(), creation_t, time_t, metaContent)
addFile(disk, meta)
val worldContent = EntryFile(WriteWorld(ingame).encodeToByteArray64())
val world = DiskEntry(ingame.world.worldIndex, 0, "world${ingame.world.worldIndex}".toByteArray(), creation_t, time_t, worldContent)
addFile(disk, world)
listOf(ingame.actorContainerActive, ingame.actorContainerInactive).forEach { actors ->
actors.forEach {
if (acceptable(it)) {
val actorContent = EntryFile(WriteActor.encodeToByteArray64(it))
val actor = DiskEntry(it.referenceID, 0, "actor${it.referenceID}".toByteArray(), creation_t, time_t, actorContent)
addFile(disk, actor)
}
}
}
disk.capacity = 0
VDUtil.dumpToRealMachine(disk, File(AppLoader.defaultDir + "/Exports/${args[1]}"))
WriteSavegame(disk, file, ingame)
}
catch (e: IOException) {
Echo("Save: IOException raised.")

View File

@@ -1,23 +0,0 @@
package net.torvald.terrarum.modulebasegame.console
import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.console.Echo
import net.torvald.terrarum.console.EchoError
import net.torvald.terrarum.serialise.SavegameWriter
/**
* Created by minjaesong on 2019-02-22.
*/
internal object SavegameWriterTest: ConsoleCommand {
override fun execute(args: Array<String>) {
val r = SavegameWriter.invoke(args.getOrNull(1))
if (!r) {
EchoError("Saving failed")
}
}
override fun printUsage() {
Echo("savetest [optional out name}")
}
}

View File

@@ -15,6 +15,7 @@ class IngamePlayer : ActorHumanoid, HasAssembledSprite {
override var animDescPath = "invalid"
override var animDescPathGlow: String? = null
internal var worldCurrentlyPlaying = 0 // only filled up on save and load; DO NOT USE THIS
private constructor()
@@ -33,6 +34,7 @@ class IngamePlayer : ActorHumanoid, HasAssembledSprite {
referenceID = Terrarum.PLAYER_REF_ID // forcibly set ID
density = BASE_DENSITY
collisionType = COLLISION_KINEMATIC
worldCurrentlyPlaying = Terrarum.ingame?.world?.worldIndex ?: 0
}
}