From 5f16f71b0a9739af288a7e211a30a6cc0f6be800 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 25 Feb 2021 10:15:37 +0900 Subject: [PATCH] fixing random terragen crash? (was it block change event too big?) --- src/net/torvald/terrarum/IngameInstance.kt | 6 +++--- .../torvald/terrarum/gameworld/GameWorld.kt | 14 +++++++------ .../terrarum/modulebasegame/BuildingMaker.kt | 20 +++++++++---------- .../modulebasegame/gameactors/FixtureBase.kt | 8 ++++---- .../gameactors/PlayerBuilderSigrid.kt | 4 +++- .../modulebasegame/gameitems/BlockBase.kt | 6 ++++-- .../gameworld/WorldSimulator.kt | 4 ++-- .../modulebasegame/worldgenerator/Biomegen.kt | 10 +++++----- .../modulebasegame/worldgenerator/Terragen.kt | 10 +++++----- 9 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/net/torvald/terrarum/IngameInstance.kt b/src/net/torvald/terrarum/IngameInstance.kt index 834d65276..e1a19734d 100644 --- a/src/net/torvald/terrarum/IngameInstance.kt +++ b/src/net/torvald/terrarum/IngameInstance.kt @@ -144,7 +144,7 @@ open class IngameInstance(val batch: SpriteBatch) : Screen { */ open fun queueTerrainChangedEvent(old: ItemID, new: ItemID, position: Long) { val (x, y) = LandUtil.resolveBlockAddr(world, position) - terrainChangeQueue.addFirst(BlockChangeQueueItem(old, new, x, y)) + terrainChangeQueue.addLast(BlockChangeQueueItem(old, new, x, y)) } /** @@ -152,7 +152,7 @@ open class IngameInstance(val batch: SpriteBatch) : Screen { */ open fun queueWallChangedEvent(old: ItemID, new: ItemID, position: Long) { val (x, y) = LandUtil.resolveBlockAddr(world, position) - wallChangeQueue.addFirst(BlockChangeQueueItem(old, new, x, y)) + wallChangeQueue.addLast(BlockChangeQueueItem(old, new, x, y)) } /** @@ -163,7 +163,7 @@ open class IngameInstance(val batch: SpriteBatch) : Screen { */ open fun queueWireChangedEvent(old: ItemID, new: ItemID, position: Long) { val (x, y) = LandUtil.resolveBlockAddr(world, position) - wireChangeQueue.addFirst(BlockChangeQueueItem(old, new, x, y)) + wireChangeQueue.addLast(BlockChangeQueueItem(old, new, x, y)) } diff --git a/src/net/torvald/terrarum/gameworld/GameWorld.kt b/src/net/torvald/terrarum/gameworld/GameWorld.kt index fd366b0ec..55fb376c2 100644 --- a/src/net/torvald/terrarum/gameworld/GameWorld.kt +++ b/src/net/torvald/terrarum/gameworld/GameWorld.kt @@ -269,7 +269,7 @@ open class GameWorld : Disposable { * * * @param itemID Tile as in ItemID, with tag removed! */ - fun setTileWall(x: Int, y: Int, itemID: ItemID) { + fun setTileWall(x: Int, y: Int, itemID: ItemID, bypassEvent: Boolean) { val (x, y) = coerceXY(x, y) val tilenum = tileNameToNumberMap[itemID]!! @@ -277,7 +277,8 @@ open class GameWorld : Disposable { layerWall.unsafeSetTile(x, y, tilenum) wallDamages.remove(LandUtil.getBlockAddr(this, x, y)) - Terrarum.ingame?.queueWallChangedEvent(oldWall, itemID, LandUtil.getBlockAddr(this, x, y)) + if (!bypassEvent) + Terrarum.ingame?.queueWallChangedEvent(oldWall, itemID, LandUtil.getBlockAddr(this, x, y)) } /** @@ -291,7 +292,7 @@ open class GameWorld : Disposable { * * * @param itemID Tile as in ItemID, with tag removed! */ - fun setTileTerrain(x: Int, y: Int, itemID: ItemID) { + fun setTileTerrain(x: Int, y: Int, itemID: ItemID, bypassEvent: Boolean) { val (x, y) = coerceXY(x, y) val tilenum = tileNameToNumberMap[itemID]!! @@ -306,7 +307,8 @@ open class GameWorld : Disposable { } // fluid tiles-item should be modified so that they will also place fluid onto their respective map - Terrarum.ingame?.queueTerrainChangedEvent(oldTerrain, itemID, LandUtil.getBlockAddr(this, x, y)) + if (!bypassEvent) + Terrarum.ingame?.queueTerrainChangedEvent(oldTerrain, itemID, LandUtil.getBlockAddr(this, x, y)) } /*fun setTileWire(x: Int, y: Int, tile: Byte) { @@ -431,7 +433,7 @@ open class GameWorld : Disposable { // remove tile from the world if (terrainDamages[addr] ?: 0f >= BlockCodex[getTileFromTerrain(x, y)].strength) { - setTileTerrain(x, y, Block.AIR) + setTileTerrain(x, y, Block.AIR, false) terrainDamages.remove(addr) return true } @@ -460,7 +462,7 @@ open class GameWorld : Disposable { // remove tile from the world if (wallDamages[addr]!! >= BlockCodex[getTileFromWall(x, y)].strength) { - setTileWall(x, y, Block.AIR) + setTileWall(x, y, Block.AIR, false) wallDamages.remove(addr) return true } diff --git a/src/net/torvald/terrarum/modulebasegame/BuildingMaker.kt b/src/net/torvald/terrarum/modulebasegame/BuildingMaker.kt index 8e49ccadc..8fad920b8 100644 --- a/src/net/torvald/terrarum/modulebasegame/BuildingMaker.kt +++ b/src/net/torvald/terrarum/modulebasegame/BuildingMaker.kt @@ -84,19 +84,19 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) { println("[BuildingMaker] Generating builder world...") for (y in 0 until gameWorld.height) { - gameWorld.setTileWall(0, y, Block.ILLUMINATOR_RED) - gameWorld.setTileWall(gameWorld.width - 1, y, Block.ILLUMINATOR_RED) - gameWorld.setTileTerrain(0, y, Block.ILLUMINATOR_RED_OFF) - gameWorld.setTileTerrain(gameWorld.width - 1, y, Block.ILLUMINATOR_RED_OFF) + gameWorld.setTileWall(0, y, Block.ILLUMINATOR_RED, true) + gameWorld.setTileWall(gameWorld.width - 1, y, Block.ILLUMINATOR_RED, true) + gameWorld.setTileTerrain(0, y, Block.ILLUMINATOR_RED_OFF, true) + gameWorld.setTileTerrain(gameWorld.width - 1, y, Block.ILLUMINATOR_RED_OFF, true) } for (y in 150 until gameWorld.height) { for (x in 1 until gameWorld.width - 1) { // wall layer - gameWorld.setTileWall(x, y, Block.DIRT) + gameWorld.setTileWall(x, y, Block.DIRT, true) // terrain layer - gameWorld.setTileTerrain(x, y, if (y == 150) Block.GRASS else Block.DIRT) + gameWorld.setTileTerrain(x, y, if (y == 150) Block.GRASS else Block.DIRT, true) } } @@ -425,15 +425,15 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) { // test paint terrain layer PENMODE_PENCIL -> { if (palSelection.startsWith("wall@")) - world.setTileWall(x, y, palSelection.substring(5)) + world.setTileWall(x, y, palSelection.substring(5), true) else - world.setTileTerrain(x, y, palSelection) + world.setTileTerrain(x, y, palSelection, true) } PENMODE_PENCIL_ERASE -> { if (currentPenTarget and PENTARGET_WALL != 0) - world.setTileWall(x, y, Block.AIR) + world.setTileWall(x, y, Block.AIR, true) else - world.setTileTerrain(x, y, Block.AIR) + world.setTileTerrain(x, y, Block.AIR, true) } PENMODE_EYEDROPPER -> { uiPaletteSelector.fore = if (world.getTileFromTerrain(x, y) == Block.AIR) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt index ca6cbbe73..46ccbcddf 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt @@ -78,10 +78,10 @@ open class FixtureBase( // if the collision type is allow_move_down, only the top surface tile should be "the platform" // lower part must not have such property (think of the table!) // TODO does this ACTUALLY work ?! - world.setTileTerrain(x, y, if (y == posY) BlockBox.ALLOW_MOVE_DOWN else BlockBox.NO_COLLISION) + world.setTileTerrain(x, y, if (y == posY) BlockBox.ALLOW_MOVE_DOWN else BlockBox.NO_COLLISION, false) } else - world.setTileTerrain(x, y, blockBox.collisionType) + world.setTileTerrain(x, y, blockBox.collisionType, false) } } @@ -116,7 +116,7 @@ open class FixtureBase( // remove filler block for (x in posX until posX + blockBox.width) { for (y in posY until posY + blockBox.height) { - world.setTileTerrain(x, y, Block.AIR) + world.setTileTerrain(x, y, Block.AIR, false) } } @@ -157,7 +157,7 @@ open class FixtureBase( for (x in posX until posX + blockBox.width) { for (y in posY until posY + blockBox.height) { if (world.getTileFromTerrain(x, y) == blockBox.collisionType) { - world.setTileTerrain(x, y, Block.AIR) + world.setTileTerrain(x, y, Block.AIR, false) } } } diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt index d10e1cb7a..29bfe98fb 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt @@ -80,7 +80,9 @@ object PlayerBuilderSigrid { try { inventory.add("wall@"+t, 9995) } - catch (e: Throwable) {} + catch (e: Throwable) { + System.err.println("[PlayerBuilder] $e") + } } // item ids are defined in /items/itemid.csv diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt index 21544b353..dd9c8db98 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt @@ -56,14 +56,16 @@ object BlockBase { ingame.world.setTileWall( mouseTile.x, mouseTile.y, - itemID.substring(5) + itemID.substring(5), + false ) } else { ingame.world.setTileTerrain( mouseTile.x, mouseTile.y, - itemID + itemID, + false ) } diff --git a/src/net/torvald/terrarum/modulebasegame/gameworld/WorldSimulator.kt b/src/net/torvald/terrarum/modulebasegame/gameworld/WorldSimulator.kt index 323ea38d6..6dc6585d1 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameworld/WorldSimulator.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameworld/WorldSimulator.kt @@ -208,8 +208,8 @@ object WorldSimulator { // process the gradual falling of the selected "stack" if (!fallableStackProcessed && fallDownCounter != 0 && isFallable) { // replace blocks - world.setTileTerrain(x, y, Block.AIR) - world.setTileTerrain(x, y + fallDownCounter, currentTile) + world.setTileTerrain(x, y, Block.AIR, true) + world.setTileTerrain(x, y + fallDownCounter, currentTile, true) fallableStackProcessed = true } diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt index f9e4643fd..43ce0d24f 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Biomegen.kt @@ -86,17 +86,17 @@ class Biomegen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par when (control) { 0 -> { // woodlands if (tileThis == Block.DIRT && nearbyTerr.any { it == Block.AIR } && nearbyWall.any { it == Block.AIR }) { - world.setTileTerrain(x, y, Block.GRASS) + world.setTileTerrain(x, y, Block.GRASS, true) } } 1 -> { // shrublands if (tileThis == Block.DIRT && nearbyTerr.any { it == Block.AIR } && nearbyWall.any { it == Block.AIR }) { - world.setTileTerrain(x, y, Block.GRASS) + world.setTileTerrain(x, y, Block.GRASS, true) } } 2, 3 -> { // plains if (tileThis == Block.DIRT && nearbyTerr.any { it == Block.AIR } && nearbyWall.any { it == Block.AIR }) { - world.setTileTerrain(x, y, Block.GRASS) + world.setTileTerrain(x, y, Block.GRASS, true) } } /*3 -> { // sands @@ -109,8 +109,8 @@ class Biomegen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par }*/ 4 -> { // rockylands if (tileThis == Block.DIRT) { - world.setTileTerrain(x, y, Block.STONE) - world.setTileWall(x, y, Block.STONE) + world.setTileTerrain(x, y, Block.STONE, true) + world.setTileWall(x, y, Block.STONE, true) } } } diff --git a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt index ef711faaa..8d32538b7 100644 --- a/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt +++ b/src/net/torvald/terrarum/modulebasegame/worldgenerator/Terragen.kt @@ -28,8 +28,8 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par (0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs -> ThreadExecutor.submit { val localJoise = getGenerator(seed, params as TerragenParams) - val localLock = java.lang.Object() // in an attempt to fix the "premature exit" issue of a thread run - synchronized(localLock) { // also see: https://stackoverflow.com/questions/28818494/threads-stopping-prematurely-for-certain-values + //val localLock = java.lang.Object() // in an attempt to fix the "premature exit" issue of a thread run + //synchronized(localLock) { // also see: https://stackoverflow.com/questions/28818494/threads-stopping-prematurely-for-certain-values for (x in xs) { for (y in 0 until world.height) { val sampleTheta = (x.toDouble() / world.width) * TWO_PI @@ -43,7 +43,7 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par draw(x, y, noise, world) } } - } + //} } } @@ -71,8 +71,8 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par val wallBlock = groundDepthBlock[terr] val terrBlock = if (cave == 0) Block.AIR else wallBlock //wallBlock * cave // AIR is always zero, this is the standard - world.setTileTerrain(x, y, terrBlock) - world.setTileWall(x, y, wallBlock) + world.setTileTerrain(x, y, terrBlock, true) + world.setTileWall(x, y, wallBlock, true) }