From b2aece0176403196833c6c3e967ac360b7d50ae0 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 25 Feb 2022 11:42:22 +0900 Subject: [PATCH] changed lightboxes so that (de)serialiser won't complain; world/actor json will will write game version it saved --- src/net/torvald/terrarum/gameactors/Luminous.kt | 17 ++++++++++++++++- src/net/torvald/terrarum/gameworld/GameWorld.kt | 2 +- .../modulebasegame/gameactors/ActorHumanoid.kt | 4 ++-- .../gameactors/FixtureTikiTorch.kt | 8 ++++---- .../{WeaponSwung.kt => ItemCarrying.kt} | 2 +- .../gameactors/ProjectileSimple.kt | 6 +++--- src/net/torvald/terrarum/serialise/Common.kt | 2 +- .../torvald/terrarum/serialise/WriteActor.kt | 10 ++++++---- .../torvald/terrarum/serialise/WriteWorld.kt | 17 ++++++++++++++--- .../terrarum/worlddrawer/LightmapRenderer.kt | 4 ++-- 10 files changed, 50 insertions(+), 22 deletions(-) rename src/net/torvald/terrarum/modulebasegame/gameactors/{WeaponSwung.kt => ItemCarrying.kt} (96%) diff --git a/src/net/torvald/terrarum/gameactors/Luminous.kt b/src/net/torvald/terrarum/gameactors/Luminous.kt index 72f533c2e..d04b338c2 100644 --- a/src/net/torvald/terrarum/gameactors/Luminous.kt +++ b/src/net/torvald/terrarum/gameactors/Luminous.kt @@ -2,7 +2,18 @@ package net.torvald.terrarum.gameactors import net.torvald.gdx.graphics.Cvec -data class Lightbox(val hitbox: Hitbox, val getLight: () -> Cvec) +class Lightbox() { + var hitbox: Hitbox = Hitbox(0.0,0.0,0.0,0.0) + var light: Cvec = Cvec() + + constructor(hitbox: Hitbox, light: Cvec) : this() { + this.hitbox = hitbox + this.light = light + } + + operator fun component1() = hitbox + operator fun component2() = light +} /** * For actors that either emits or blocks lights @@ -16,6 +27,8 @@ interface Luminous { * * Hitbox(x-offset, y-offset, width, height) * (Use ArrayList for normal circumstances) + * + * NOTE: MUST NOT SERIALISE (use `@Transient`) */ val lightBoxList: List @@ -24,6 +37,8 @@ interface Luminous { * * Hitbox(x-offset, y-offset, width, height) * (Use ArrayList for normal circumstances) + * + * NOTE: MUST NOT SERIALISE (use `@Transient`) */ val shadeBoxList: List } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index 60a4a9ca8..6c41181b3 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -127,7 +127,7 @@ open class GameWorld() : Disposable { val extraFields = HashMap() - internal var genver = -1 // only gets used when the game saves and loads +// internal var genver = -1 // only gets used when the game saves and loads internal var comp = -1 // only gets used when the game saves and loads internal val dynamicItemInventory = ItemTable() diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt index ff8256212..ac5b147c6 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorHumanoid.kt @@ -99,11 +99,11 @@ open class ActorHumanoid : ActorWithBody, Controllable, Pocketed, Factionable, L * (Use ArrayList for normal circumstances) */ override val lightBoxList: List - get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3)) { actorValueColour }).toList() // things are asymmetric!! + get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3), actorValueColour)).toList() // things are asymmetric!! // use getter; dimension of the player may change by time. override val shadeBoxList: List - get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3)) { actorValueShade }).toList() // things are asymmetric!! + get() = arrayOf(Lightbox(Hitbox(2.0, 2.0, hitbox.width - 3, hitbox.height - 3), actorValueShade)).toList() // things are asymmetric!! // use getter; dimension of the player may change by time. @Transient val BASE_DENSITY = 980.0 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureTikiTorch.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureTikiTorch.kt index 7bb78060e..f91681570 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureTikiTorch.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureTikiTorch.kt @@ -27,13 +27,13 @@ internal class FixtureTikiTorch : FixtureBase, Luminous { private val rndHash2 = rng.nextInt() private var color: Cvec - get() = BlockCodex[Block.TORCH].getLumCol(rndHash1, rndHash2) + get() = try { BlockCodex[Block.TORCH].getLumCol(rndHash1, rndHash2) } catch (e: NullPointerException) { Cvec() } set(value) { throw UnsupportedOperationException() } - override val lightBoxList: ArrayList = ArrayList(1) - override val shadeBoxList: ArrayList = ArrayList(1) + @Transient override val lightBoxList: ArrayList = ArrayList(1) + @Transient override val shadeBoxList: ArrayList = ArrayList(1) constructor() : super( BlockBox(BlockBox.NO_COLLISION, 1, 2), @@ -53,7 +53,7 @@ internal class FixtureTikiTorch : FixtureBase, Luminous { setHitboxDimension(16, 32, 0, 0) - lightBoxList.add(Lightbox(Hitbox(6.0, 5.0, 4.0, 3.0)) { color }) + lightBoxList.add(Lightbox(Hitbox(6.0, 5.0, 4.0, 3.0), color)) makeNewSprite(CommonResourcePool.getAsTextureRegionPack("sprites-fixtures-tiki_torch.tga")) sprite!!.setRowsAndFrames(1, 2) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/WeaponSwung.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ItemCarrying.kt similarity index 96% rename from src/net/torvald/terrarum/modulebasegame/gameactors/WeaponSwung.kt rename to src/net/torvald/terrarum/modulebasegame/gameactors/ItemCarrying.kt index bde9efccf..4afefccf4 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/WeaponSwung.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ItemCarrying.kt @@ -7,7 +7,7 @@ import net.torvald.terrarum.gameitems.ItemID /** * Created by minjaesong on 2016-04-26. */ -class WeaponSwung : ActorWithBody, Luminous { +class ItemCarrying : ActorWithBody, Luminous { var itemID: ItemID = ""; private set diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ProjectileSimple.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ProjectileSimple.kt index a3c83519b..8c6c200aa 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ProjectileSimple.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ProjectileSimple.kt @@ -38,8 +38,8 @@ open class ProjectileSimple : ActorWithBody, Luminous, Projectile { * Hitbox(x-offset, y-offset, width, height) * (Use ArrayList for normal circumstances) */ - override val lightBoxList = ArrayList() - override val shadeBoxList = ArrayList() + @Transient override val lightBoxList = ArrayList() + @Transient override val shadeBoxList = ArrayList() private val lifetimeMax = 2500 private var lifetimeCounter = 0f @@ -58,7 +58,7 @@ open class ProjectileSimple : ActorWithBody, Luminous, Projectile { setPosition(fromPoint.x, fromPoint.y) posPre = Point2d(fromPoint.x, fromPoint.y) // lightbox sized 8x8 centered to the bullet - lightBoxList.add(Lightbox(Hitbox(-4.0, -4.0, 8.0, 8.0)) { color }) + lightBoxList.add(Lightbox(Hitbox(-4.0, -4.0, 8.0, 8.0), color)) //this.externalV.set(velocity) damage = bulletDatabase[type][OFFSET_DAMAGE] as Int diff --git a/src/net/torvald/terrarum/serialise/Common.kt b/src/net/torvald/terrarum/serialise/Common.kt index 7bb0c388d..3c3053691 100644 --- a/src/net/torvald/terrarum/serialise/Common.kt +++ b/src/net/torvald/terrarum/serialise/Common.kt @@ -46,7 +46,7 @@ object Common { // install custom (de)serialiser init { -// jsoner.ignoreUnknownFields = true + jsoner.ignoreUnknownFields = true jsoner.setUsePrototypes(false) jsoner.setIgnoreDeprecated(false) diff --git a/src/net/torvald/terrarum/serialise/WriteActor.kt b/src/net/torvald/terrarum/serialise/WriteActor.kt index d4742ab58..6c4e84013 100644 --- a/src/net/torvald/terrarum/serialise/WriteActor.kt +++ b/src/net/torvald/terrarum/serialise/WriteActor.kt @@ -21,16 +21,17 @@ import java.util.* */ object WriteActor { + // genver must be found on fixed location of the JSON string operator fun invoke(actor: Actor): String { val s = Common.jsoner.toJson(actor, actor.javaClass) - return """{"class":"${actor.javaClass.canonicalName}",${s.substring(1)}""" + return """{"genver":${Common.GENVER},"class":"${actor.javaClass.canonicalName}",${s.substring(1)}""" } fun encodeToByteArray64(actor: Actor): ByteArray64 { val baw = ByteArray64Writer(Common.CHARSET) - val classDef = """{"class":"${actor.javaClass.canonicalName}"""" - baw.write(classDef) + val header = """{"genver":${Common.GENVER},"class":"${actor.javaClass.canonicalName}"""" + baw.write(header) Common.jsoner.toJson(actor, actor.javaClass, baw) baw.flush(); baw.close() // by this moment, contents of the baw will be: @@ -39,7 +40,7 @@ object WriteActor { // and we want to turn it into this: // {"class":"some.class.Name","actorValue":{},......} val ba = baw.toByteArray64() - ba[classDef.toByteArray(Common.CHARSET).size.toLong()] = ','.code.toByte() + ba[header.toByteArray(Common.CHARSET).size.toLong()] = ','.code.toByte() return ba } @@ -90,6 +91,7 @@ object WritePlayer { val actorJson = WriteActor.encodeToByteArray64(player) + val adl = player.animDesc!!.getRawADL() val adlGlow = player.animDescGlow?.getRawADL() // NULLABLE! diff --git a/src/net/torvald/terrarum/serialise/WriteWorld.kt b/src/net/torvald/terrarum/serialise/WriteWorld.kt index 1fbc48f04..6e8420834 100644 --- a/src/net/torvald/terrarum/serialise/WriteWorld.kt +++ b/src/net/torvald/terrarum/serialise/WriteWorld.kt @@ -35,7 +35,7 @@ object WriteWorld { val world = ingame.world val currentPlayTime_t = time_t - ingame.loadedTime_t - world.genver = Common.GENVER +// world.genver = Common.GENVER world.comp = Common.COMP_GZIP world.lastPlayTime = time_t world.totalPlayTime += currentPlayTime_t @@ -56,17 +56,28 @@ object WriteWorld { return world } + // genver must be found on fixed location of the JSON string operator fun invoke(ingame: TerrarumIngame, time_t: Long, actorsList: List, playersList: List): String { - return Common.jsoner.toJson(preWrite(ingame, time_t, actorsList, playersList)) + val s = Common.jsoner.toJson(preWrite(ingame, time_t, actorsList, playersList)) + return """{"genver":${Common.GENVER},${s.substring(1)}""" } fun encodeToByteArray64(ingame: TerrarumIngame, time_t: Long, actorsList: List, playersList: List): ByteArray64 { val baw = ByteArray64Writer(Common.CHARSET) + val header = """{"genver":${Common.GENVER}""" + baw.write(header) Common.jsoner.toJson(preWrite(ingame, time_t, actorsList, playersList), baw) baw.flush(); baw.close() + // by this moment, contents of the baw will be: + // {"genver":123456{"actorValue":{},......} + // (note that first bracket is not closed, and another open bracket after "genver" property) + // and we want to turn it into this: + // {"genver":123456,"actorValue":{},......} + val ba = baw.toByteArray64() + ba[header.toByteArray(Common.CHARSET).size.toLong()] = ','.code.toByte() - return baw.toByteArray64() + return ba } /** diff --git a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt index aded501b4..b4483a9c6 100644 --- a/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt +++ b/src/net/torvald/terrarum/worlddrawer/LightmapRenderer.kt @@ -358,7 +358,7 @@ object LightmapRenderer { ..lightBoxX.plus(lightBoxW).div(TILE_SIZE).floorInt()) { val oldLight = lanternMap[LandUtil.getBlockAddr(world, x, y)] ?: Cvec(0) // if two or more luminous actors share the same block, mix the light - val actorLight = colour() + val actorLight = colour lanternMap[LandUtil.getBlockAddr(world, x, y)] = oldLight.maxAndAssign(actorLight) } @@ -377,7 +377,7 @@ object LightmapRenderer { ..lightBoxX.plus(lightBoxW).div(TILE_SIZE).floorInt()) { val oldLight = shadowMap[LandUtil.getBlockAddr(world, x, y)] ?: Cvec(0) // if two or more luminous actors share the same block, mix the light - val actorLight = colour() + val actorLight = colour shadowMap[LandUtil.getBlockAddr(world, x, y)] = oldLight.maxAndAssign(actorLight) }