diff --git a/src/net/torvald/terrarum/IngameInstance.kt b/src/net/torvald/terrarum/IngameInstance.kt index 526a0bb9b..e3c11e7be 100644 --- a/src/net/torvald/terrarum/IngameInstance.kt +++ b/src/net/torvald/terrarum/IngameInstance.kt @@ -373,7 +373,7 @@ open class IngameInstance(val batch: SpriteBatch) : Screen { val flags2 = FileInputStream(file2).let { it.skip(49L); val r = it.read(); it.close(); r } val flags1 = FileInputStream(file1).let { it.skip(49L); val r = it.read(); it.close(); r } - if (!(flags2 == 0 && flags1 != 0)) { + if (!(flags2 == 0 && flags1 != 0) || !file2.exists()) { file1.copyTo(file2, true) } } catch (e: NoSuchFileException) { diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index 693ce6b27..0fa5f170c 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -21,9 +21,9 @@ import net.torvald.terrarum.blockproperties.WireCodex import net.torvald.terrarum.gameactors.Actor import net.torvald.terrarum.gameactors.ActorID import net.torvald.terrarum.gameactors.faction.FactionCodex +import net.torvald.terrarum.gameworld.fmod import net.torvald.terrarum.itemproperties.ItemCodex import net.torvald.terrarum.itemproperties.MaterialCodex -import net.torvald.terrarum.serialise.Common import net.torvald.terrarum.tvda.VDUtil import net.torvald.terrarum.tvda.VirtualDisk import net.torvald.terrarum.ui.UICanvas @@ -251,13 +251,13 @@ object Terrarum : Disposable { /** Position of the cursor in the world */ val mouseX: Double - get() = WorldCamera.zoomedX + Gdx.input.x / (ingame?.screenZoom ?: 1f).toDouble() + get() = (WorldCamera.zoomedX + Gdx.input.x / (ingame?.screenZoom ?: 1f).toDouble()).fmod(WorldCamera.worldWidth.toDouble()) /** Position of the cursor in the world */ val mouseY: Double - get() = WorldCamera.zoomedY + Gdx.input.y / (ingame?.screenZoom ?: 1f).toDouble() + get() = (WorldCamera.zoomedY + Gdx.input.y / (ingame?.screenZoom ?: 1f).toDouble()) /** Position of the cursor in the world */ val oldMouseX: Double - get() = WorldCamera.zoomedX + (Gdx.input.x - Gdx.input.deltaX) / (ingame?.screenZoom ?: 1f).toDouble() + get() = (WorldCamera.zoomedX + (Gdx.input.x - Gdx.input.deltaX) / (ingame?.screenZoom ?: 1f).toDouble()).fmod(WorldCamera.worldWidth.toDouble()) /** Position of the cursor in the world */ val oldMouseY: Double get() = WorldCamera.zoomedY + (Gdx.input.y - Gdx.input.deltaY) / (ingame?.screenZoom ?: 1f).toDouble() @@ -539,8 +539,7 @@ fun Float.sqr() = this * this fun Double.sqrt() = Math.sqrt(this) fun Float.sqrt() = FastMath.sqrt(this) fun Int.abs() = this.absoluteValue -fun Double.bipolarClamp(limit: Double) = - this.coerceIn(-limit, limit) +fun Double.bipolarClamp(limit: Double) = this.coerceIn(-limit, limit) fun Boolean.toInt() = if (this) 1 else 0 fun Int.bitCount() = java.lang.Integer.bitCount(this) fun Long.bitCount() = java.lang.Long.bitCount(this) diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index c5f49e58b..197ebff58 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -199,7 +199,7 @@ open class GameWorld() : Disposable { fun getLayer(index: Int) = when(index) { 0 -> layerTerrain 1 -> layerWall - else -> throw IllegalArgumentException("Unknown layer index: $index") + else -> null//throw IllegalArgumentException("Unknown layer index: $index") } fun coerceXY(x: Int, y: Int) = (x fmod width) to (y.coerceIn(0, height - 1)) @@ -701,6 +701,7 @@ open class GameWorld() : Disposable { infix fun Int.fmod(other: Int) = Math.floorMod(this, other) infix fun Long.fmod(other: Long) = Math.floorMod(this, other) infix fun Float.fmod(other: Float) = if (this >= 0f) this % other else (this % other) + other +infix fun Double.fmod(other: Double) = if (this >= 0.0) this % other else (this % other) + other inline class FluidType(val value: Int) { infix fun sameAs(other: FluidType) = this.value.absoluteValue == other.value.absoluteValue diff --git a/src/net/torvald/terrarum/serialise/GameSavingThread.kt b/src/net/torvald/terrarum/serialise/GameSavingThread.kt index 10b4af166..581249f47 100644 --- a/src/net/torvald/terrarum/serialise/GameSavingThread.kt +++ b/src/net/torvald/terrarum/serialise/GameSavingThread.kt @@ -124,7 +124,7 @@ class GameSavingThread(val disk: VirtualDisk, val outFile: File, val ingame: Ter Echo("Writing chunks... ${(cw*ch*layer) + chunkNumber + 1}/${cw*ch*layers.size}") - val chunkBytes = WriteWorld.encodeChunk(layers[layer], cx, cy) + val chunkBytes = WriteWorld.encodeChunk(layers[layer]!!, cx, cy) val entryID = worldNum.toLong().shl(32) or layer.toLong().shl(24) or chunkNumber val entryContent = EntryFile(chunkBytes) diff --git a/src/net/torvald/terrarum/serialise/QuickSaveThread.kt b/src/net/torvald/terrarum/serialise/QuickSaveThread.kt index 207fd76d4..791f02982 100644 --- a/src/net/torvald/terrarum/serialise/QuickSaveThread.kt +++ b/src/net/torvald/terrarum/serialise/QuickSaveThread.kt @@ -91,27 +91,27 @@ class QuickSaveThread(val disk: VirtualDisk, val file: File, val ingame: Terraru chunks.forEachIndexed { layerNum, chunks -> if (chunks.size != 0) { - val layer = ingame.world.getLayer(layerNum) + ingame.world.getLayer(layerNum)?.let { layer -> + chunks.forEach { chunkNumber -> - chunks.forEach { chunkNumber -> + Echo("Writing chunks... $chunksWrote/$chunkCount") - Echo("Writing chunks... $chunksWrote/$chunkCount") - - val chunkXY = LandUtil.chunkNumToChunkXY(ingame.world, chunkNumber) + val chunkXY = LandUtil.chunkNumToChunkXY(ingame.world, chunkNumber) // println("Chunk xy from number $chunkNumber -> (${chunkXY.x}, ${chunkXY.y})") - val chunkBytes = WriteWorld.encodeChunk(layer, chunkXY.x, chunkXY.y) - val entryID = worldNum.toLong().shl(32) or layerNum.toLong().shl(24) or chunkNumber.toLong() + val chunkBytes = WriteWorld.encodeChunk(layer, chunkXY.x, chunkXY.y) + val entryID = worldNum.toLong().shl(32) or layerNum.toLong().shl(24) or chunkNumber.toLong() - val entryContent = EntryFile(chunkBytes) - val entry = DiskEntry(entryID, 0, creation_t, time_t, entryContent) - // "W1L0-92,15" - addFile(disk, entry); skimmer.appendEntryOnly(entry) + val entryContent = EntryFile(chunkBytes) + val entry = DiskEntry(entryID, 0, creation_t, time_t, entryContent) + // "W1L0-92,15" + addFile(disk, entry); skimmer.appendEntryOnly(entry) - WriteSavegame.saveProgress += chunkProgressMultiplier - chunksWrote += 1 + WriteSavegame.saveProgress += chunkProgressMultiplier + chunksWrote += 1 + } } } } diff --git a/src/net/torvald/terrarum/serialise/WriteSavegame.kt b/src/net/torvald/terrarum/serialise/WriteSavegame.kt index d28cc7dbd..9a43c6c4f 100644 --- a/src/net/torvald/terrarum/serialise/WriteSavegame.kt +++ b/src/net/torvald/terrarum/serialise/WriteSavegame.kt @@ -166,7 +166,7 @@ object LoadSavegame { val chunkFile = VDUtil.getAsNormalFile(disk, worldnum.shl(32) or layer.toLong().shl(24) or chunk) val chunkXY = LandUtil.chunkNumToChunkXY(world, chunk.toInt()) - ReadWorld.decodeChunkToLayer(chunkFile.getContent(), worldLayer[layer], chunkXY.x, chunkXY.y) + ReadWorld.decodeChunkToLayer(chunkFile.getContent(), worldLayer[layer]!!, chunkXY.x, chunkXY.y) } } diff --git a/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt b/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt index 73a6f0972..f293a8228 100644 --- a/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt +++ b/src/net/torvald/terrarum/worlddrawer/WorldCamera.kt @@ -62,8 +62,12 @@ object WorldCamera { private val nullVec = Vector2(0.0, 0.0) - private var worldWidth = 0 - private var worldHeight = 0 + /** World width in pixels */ + var worldWidth = 0 + private set + /** World height in pixels */ + var worldHeight = 0 + private set fun update(world: GameWorld, player: ActorWithBody?) { if (player == null) return