new player save format writing

This commit is contained in:
minjaesong
2021-10-08 14:25:59 +09:00
parent 3f9b41fd29
commit aec6fea49e
21 changed files with 280 additions and 116 deletions

View File

@@ -46,7 +46,11 @@ import net.torvald.terrarum.worlddrawer.FeaturesDrawer
import net.torvald.terrarum.worlddrawer.WorldCamera
import net.torvald.util.CircularArray
import org.khelekore.prtree.PRTree
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet
/**
@@ -218,7 +222,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
// gameLoadMode and gameLoadInfoPayload must be set beforehand!!
when (gameLoadMode) {
GameLoadMode.CREATE_NEW -> enterCreateNewWorld(gameLoadInfoPayload as NewWorldParameters)
GameLoadMode.CREATE_NEW -> enterCreateNewWorld(gameLoadInfoPayload as NewGameParams)
GameLoadMode.LOAD_FROM -> enterLoadFromSave(gameLoadInfoPayload as Codices)
}
@@ -228,6 +232,11 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
super.show() // this function sets gameInitialised = true
}
data class NewGameParams(
val player: IngamePlayer,
val newWorldParams: NewWorldParameters
)
data class NewWorldParameters(
val width: Int,
val height: Int,
@@ -254,14 +263,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
val actors: List<ActorID>
)
private fun setTheRealGamerFirstTime(actor: IngamePlayer) {
if (actor.referenceID != Terrarum.PLAYER_REF_ID) {
throw Error()
}
actorNowPlaying = actor
addNewActor(actorNowPlaying)
}
/**
* Init instance by loading saved world
@@ -308,33 +310,49 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
// go to spawn position
printdbg(this, "World Spawn position: (${world.spawnX}, ${world.spawnY})")
// setTheRealGamerFirstTime(PlayerBuilderSigrid())
setTheRealGamerFirstTime(PlayerBuilderTestSubject1())
// setTheRealGamerFirstTime(PlayerBuilderWerebeastTest())
val worldSavefileName = savegameNickname
val playerSavefileName = actorGamer.actorValue.getAsString(AVKey.NAME) ?: "Player-${actorGamer.uuid}"
savegameArchive = VDUtil.createNewDisk(
worldDisk = VDUtil.createNewDisk(
1L shl 60,
savegameNickname,
worldSavefileName,
Common.CHARSET
)
actorNowPlaying!!.setPosition(
playerDisk = VDUtil.createNewDisk(
1L shl 60,
playerSavefileName,
Common.CHARSET
)
actorGamer.setPosition(
world.spawnX * TILE_SIZED,
world.spawnY * TILE_SIZED
)
// make initial savefile
// we're not writing multiple files at one go because:
// 1. lighten the IO burden
// 2. cannot sync up the "counter" to determine whether both are finished
uiAutosaveNotifier.setAsOpen()
WriteSavegame.immediate(savegameArchive, getSaveFileMain(), this, true) {
makeSavegameBackupCopy() // don't put it on the postInit() or render(); must be called using callback
uiAutosaveNotifier.setAsClose()
WriteSavegame.immediate(WriteSavegame.SaveMode.PLAYER, playerDisk, getPlayerSaveFiledesc(playerSavefileName), this, false, true) {
makeSavegameBackupCopy(getPlayerSaveFiledesc(playerSavefileName))
WriteSavegame.immediate(WriteSavegame.SaveMode.WORLD, worldDisk, getWorldSaveFiledesc(worldSavefileName), this, false, true) {
makeSavegameBackupCopy(getWorldSaveFiledesc(worldSavefileName)) // don't put it on the postInit() or render(); must be called using callback
uiAutosaveNotifier.setAsClose()
}
}
}
/**
* Init instance by creating new world
*/
private fun enterCreateNewWorld(worldParams: NewWorldParameters) {
private fun enterCreateNewWorld(newGameParams: NewGameParams) {
val player = newGameParams.player
val worldParams = newGameParams.newWorldParams
printdbg(this, "Ingame called")
printStackTrace(this)
@@ -343,6 +361,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
}
else {
App.getLoadScreen().addMessage("${App.GAME_NAME} version ${App.getVERSION_STRING()}")
App.getLoadScreen().addMessage("Creating new world")
@@ -364,6 +383,19 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
historicalFigureIDBucket = ArrayList<Int>()
savegameNickname = worldParams.savegameName
player.worldCurrentlyPlaying = UUID.fromString(world.worldIndex.toString())
world.worldCreator = UUID.fromString(player.uuid.toString())
printdbg(this, "new woridIndex: ${world.worldIndex}")
printdbg(this, "worldCurrentlyPlaying: ${player.worldCurrentlyPlaying}")
actorNowPlaying = player
actorGamer = player
addNewActor(player)
}
KeyToggler.forceSet(Input.Keys.Q, false)
@@ -978,13 +1010,13 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
fun queueAutosave() {
val start = System.nanoTime()
uiAutosaveNotifier.setAsOpen()
/*uiAutosaveNotifier.setAsOpen()
makeSavegameBackupCopy()
WriteSavegame.quick(savegameArchive, getSaveFileMain(), this, true) {
uiAutosaveNotifier.setAsClose()
debugTimers.put("Last Autosave Duration", System.nanoTime() - start)
}
}*/
}
@@ -1022,8 +1054,8 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) {
override fun removeActor(actor: Actor?) {
if (actor == null) return
if (actor.referenceID == actorGamer.referenceID || actor.referenceID == 0x51621D) // do not delete this magic
throw ProtectedActorRemovalException("Player")
// if (actor.referenceID == actorGamer.referenceID || actor.referenceID == 0x51621D) // do not delete this magic
// throw ProtectedActorRemovalException("Player")
forceRemoveActor(actor)
}

View File

@@ -24,7 +24,7 @@ object Save : ConsoleCommand {
val disk = VDUtil.createNewDisk(1L shl 60, savename, Common.CHARSET)
val file = File(App.saveDir + "/${args[1]}")
WriteSavegame(disk, file, ingame, false)
// WriteSavegame(disk, file, ingame, false)
}
catch (e: IOException) {
@@ -47,9 +47,7 @@ object Quicksave : ConsoleCommand {
override fun execute(args: Array<String>) {
val ingame = Terrarum.ingame!! as TerrarumIngame
WriteSavegame.quick(ingame.savegameArchive, ingame.getSaveFileMain(), ingame, false) {
}
// WriteSavegame.quick(ingame.savegameArchive, ingame.getSaveFileMain(), ingame, false) {}
}
override fun printUsage() {

View File

@@ -1,10 +1,15 @@
package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Texture
import net.torvald.spriteanimation.HasAssembledSprite
import net.torvald.spriteanimation.SpriteAnimation
import net.torvald.spriteassembler.ADProperties
import net.torvald.spriteassembler.AssembleSheetPixmap
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.tvda.SimpleFileSystem
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import java.util.*
@@ -14,12 +19,16 @@ import java.util.*
* Created by minjaesong on 2015-12-31.
*/
class IngamePlayer : ActorHumanoid, HasAssembledSprite {
class IngamePlayer : ActorHumanoid {
var uuid = UUID.randomUUID(); private set
var worldCurrentlyPlaying: UUID = UUID(0L,0L) // only filled up on save and load; DO NOT USE THIS
/** ADL for main sprite. Necessary. */
@Transient var animDesc: ADProperties? = null
/** ADL for glow sprite. Optional. */
@Transient var animDescGlow: ADProperties? = null
var UUID = UUID(0L,0L); private set
internal var worldCurrentlyPlaying: UUID = UUID(0L,0L) // only filled up on save and load; DO NOT USE THIS
override var animDesc: ADProperties? = null
override var animDescGlow: ADProperties? = null
private constructor()
@@ -38,7 +47,82 @@ class IngamePlayer : ActorHumanoid, HasAssembledSprite {
referenceID = Terrarum.PLAYER_REF_ID // TODO assign random ID
density = BASE_DENSITY
collisionType = COLLISION_KINEMATIC
worldCurrentlyPlaying = Terrarum.ingame?.world?.worldIndex ?: UUID(0L,0L)
}
/**
* Example usage:
* ```
* this.animDescPath = "..."
* this.animDescPathGlow = "..."
* this.sprite = SpriteAnimation(actor)
* this.spriteGlow = SpriteAnimation(actor)
* reassembleSprite(this.sprite, this.spriteGlow)
* ```
*/
fun reassembleSprite(sprite: SpriteAnimation?, anim: ADProperties?, spriteGlow: SpriteAnimation? = null, animGlow: ADProperties? = null) {
if (anim != null && sprite != null)
_rebuild(anim, sprite)
if (animGlow != null && spriteGlow != null)
_rebuild(animGlow, spriteGlow)
}
fun reassembleSprite(disk: SimpleFileSystem, sprite: SpriteAnimation?, anim: ADProperties?, spriteGlow: SpriteAnimation? = null, animGlow: ADProperties? = null) {
if (anim != null && sprite != null)
_rebuild(disk, anim, sprite)
if (animGlow != null && spriteGlow != null)
_rebuild(disk, animGlow, spriteGlow)
}
private fun _rebuild(ad: ADProperties, sprite: SpriteAnimation) {
// TODO injecting held item/armour pictures? Would it be AssembleSheetPixmap's job?
val pixmap = AssembleSheetPixmap.fromAssetsDir(ad)
val texture = Texture(pixmap)
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
pixmap.dispose()
val regionPack = TextureRegionPack(texture, ad.frameWidth, ad.frameHeight)
val newAnimDelays = FloatArray(ad.animations.size)
val newAnimFrames = IntArray(ad.animations.size)
ad.animations.forEach { t, u ->
val index = u.row - 1
newAnimDelays[index] = u.delay
newAnimFrames[index] = u.frames
}
sprite.setSpriteImage(regionPack)
sprite.delays = newAnimDelays
sprite.nFrames = newAnimFrames
sprite.nRows = newAnimDelays.size
}
private fun _rebuild(disk: SimpleFileSystem, ad: ADProperties, sprite: SpriteAnimation) {
// TODO injecting held item/armour pictures? Would it be AssembleSheetPixmap's job?
val pixmap = AssembleSheetPixmap.fromVirtualDisk(disk, ad)
val texture = Texture(pixmap)
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
pixmap.dispose()
val regionPack = TextureRegionPack(texture, ad.frameWidth, ad.frameHeight)
val newAnimDelays = FloatArray(ad.animations.size)
val newAnimFrames = IntArray(ad.animations.size)
ad.animations.forEach { t, u ->
val index = u.row - 1
newAnimDelays[index] = u.delay
newAnimFrames[index] = u.frames
}
sprite.setSpriteImage(regionPack)
sprite.delays = newAnimDelays
sprite.nFrames = newAnimFrames
sprite.nRows = newAnimDelays.size
}
}

View File

@@ -90,13 +90,13 @@ class UIInventoryEscMenu(val full: UIInventoryFull) : UICanvas() {
INGAME.makeSavegameBackupCopy()
// save the game
WriteSavegame(INGAME.savegameArchive, File(App.saveDir, INGAME.savegameNickname), Terrarum.ingame!! as TerrarumIngame, false) {
/*WriteSavegame(INGAME.savegameArchive, File(App.saveDir, INGAME.savegameNickname), Terrarum.ingame!! as TerrarumIngame, false) {
// callback:
System.gc()
screen = 0
full.handler.unlockToggle()
full.unlockTransition()
}
}*/
}
2 -> {
screen = 4; gameMenuButtons.deselect()

View File

@@ -9,6 +9,7 @@ import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.WorldgenLoadScreen
import net.torvald.terrarum.modulebasegame.gameactors.PlayerBuilderTestSubject1
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarum.utils.RandomWordsName
@@ -36,9 +37,11 @@ class UIProxyNewRandomGame : UICanvas() {
override fun endOpening(delta: Float) {
printdbg(this, "endOpening")
val ingame = TerrarumIngame(App.batch)
val worldParam = TerrarumIngame.NewWorldParameters(2880, 1350, HQRNG().nextLong(), RandomWordsName(4))
val worldParam = TerrarumIngame.NewGameParams(
PlayerBuilderTestSubject1(),
TerrarumIngame.NewWorldParameters(2880, 1350, HQRNG().nextLong(), RandomWordsName(4))
)
// val worldParam = TerrarumIngame.NewWorldParameters(2880, 1350, 0x51621D)
// val worldParam = TerrarumIngame.NewWorldParameters(6030, 1800, HQRNG().nextLong()) // small
@@ -51,7 +54,7 @@ class UIProxyNewRandomGame : UICanvas() {
Terrarum.setCurrentIngameInstance(ingame)
//LoadScreen.screenToLoad = ingame
//AppLoader.setScreen(LoadScreen)
val loadScreen = WorldgenLoadScreen(ingame, worldParam.width, worldParam.height)
val loadScreen = WorldgenLoadScreen(ingame, worldParam.newWorldParams.width, worldParam.newWorldParams.height)
App.setLoadScreen(loadScreen)
}