From 1060d963645b9edc8513e59700ff1f141970d634 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 13 Jan 2022 09:55:51 +0900 Subject: [PATCH] IS_DEVELOPMENT_BUILD key can now be toggled with vm option '-ea' --- src/net/torvald/UnsafePtr.kt | 4 +- src/net/torvald/terrarum/App.java | 49 +++++++++++-------- src/net/torvald/terrarum/ModMgr.kt | 8 +-- .../terrarum/blockproperties/BlockCodex.kt | 7 ++- .../terrarum/blockproperties/WireCodex.kt | 9 ++-- .../torvald/terrarum/gameitems/GameItem.kt | 14 ++++++ .../terrarum/itemproperties/ItemCodex.kt | 4 +- .../terrarum/itemproperties/Material.kt | 7 ++- .../terrarum/modulebasegame/TerrarumIngame.kt | 14 +++--- .../terrarum/modulebasegame/TitleScreen.kt | 2 +- .../gameactors/FixtureStorageChest.kt | 1 - .../gameitems/PickaxeGeneric.kt | 3 +- .../modulebasegame/ui/UILoadDemoSavefiles.kt | 2 +- .../terrarum/modulebasegame/ui/UIRemoCon.kt | 13 ++--- 14 files changed, 78 insertions(+), 59 deletions(-) diff --git a/src/net/torvald/UnsafePtr.kt b/src/net/torvald/UnsafePtr.kt index c83f0d1f2..a26c5083b 100644 --- a/src/net/torvald/UnsafePtr.kt +++ b/src/net/torvald/UnsafePtr.kt @@ -90,7 +90,7 @@ internal class UnsafePtr(pointer: Long, allocSize: Long) { fun destroy() { if (!destroyed) { - println("[UnsafePtr] Destroying pointer $this; called from:") + printdbg(this, "Destroying pointer $this; called from:") printStackTrace(this) UnsafeHelper.unsafe.freeMemory(ptr) @@ -100,7 +100,7 @@ internal class UnsafePtr(pointer: Long, allocSize: Long) { UnsafeHelper.unsafeAllocatedSize -= size } else { - println("[UnsafePtr] Destroy() is called but the pointer $this is already been destroyed; called from:") + printdbg(this, "Destroy() is called but the pointer $this is already been destroyed; called from:") printStackTrace(this) } } diff --git a/src/net/torvald/terrarum/App.java b/src/net/torvald/terrarum/App.java index 1ffc8f54e..b7050d0c2 100644 --- a/src/net/torvald/terrarum/App.java +++ b/src/net/torvald/terrarum/App.java @@ -72,8 +72,13 @@ public class App implements ApplicationListener { /** * when FALSE, some assertion and print code will not execute */ - public static boolean IS_DEVELOPMENT_BUILD = true; + public static boolean IS_DEVELOPMENT_BUILD = false; + { + // if -ea flag is set, turn on all the debug prints + try { assert (false); } + catch (AssertionError e) { IS_DEVELOPMENT_BUILD = true; } + } /** * Singleton instance @@ -378,8 +383,6 @@ public class App implements ApplicationListener { Object[] iconPathsTemp = appIconPaths.toArray(); appConfig.setWindowIcon(Arrays.copyOf(iconPathsTemp, iconPathsTemp.length, String[].class)); - IS_DEVELOPMENT_BUILD = true; - // set some more configuration vars MULTITHREAD = THREAD_COUNT >= 3 && getConfigBoolean("multithread"); @@ -920,10 +923,12 @@ public class App implements ApplicationListener { // test print - System.out.println("[App] Test printing every registered item"); - Terrarum.INSTANCE.getItemCodex().getItemCodex().values().stream().map(GameItem::getOriginalID).forEach( - (String s) -> System.out.print(s + " ")); - System.out.println(); + if (IS_DEVELOPMENT_BUILD) { + System.out.println("[App] Test printing every registered item"); + Terrarum.INSTANCE.getItemCodex().getItemCodex().values().stream().map(GameItem::getOriginalID).forEach( + (String s) -> System.out.print(s + " ")); + System.out.println(); + } try { @@ -1348,6 +1353,14 @@ public class App implements ApplicationListener { System.out.println("[" + out + "] " + message); } + public static void printmsgerr(Object obj, Object message) { + String out = (obj instanceof String) ? (String) obj : obj.getClass().getSimpleName(); + if (message == null) + System.out.println(csiR + "[" + out + "] null" + csi0); + else + System.out.println(csiR + "[" + out + "] " + message + csi0); + } + public static ShaderProgram loadShaderFromFile(String vert, String frag) { ShaderProgram s = new ShaderProgram(Gdx.files.internal(vert), Gdx.files.internal(frag)); @@ -1369,29 +1382,23 @@ public class App implements ApplicationListener { } public static void measureDebugTime(String name, kotlin.jvm.functions.Function0 block) { - if (IS_DEVELOPMENT_BUILD) { //debugTimers.put(name, kotlin.system.TimingKt.measureNanoTime(block)); - long start = System.nanoTime(); - block.invoke(); - debugTimers.put(name, System.nanoTime() - start); - } + long start = System.nanoTime(); + block.invoke(); + debugTimers.put(name, System.nanoTime() - start); } public static void setDebugTime(String name, long value) { - if (IS_DEVELOPMENT_BUILD) { - debugTimers.put(name, value); - } + debugTimers.put(name, value); } public static void addDebugTime(String target, String... targets) { - if (IS_DEVELOPMENT_BUILD) { - long l = 0L; - for (String s : targets) { - l += ((long) debugTimers.get(s)); - } - debugTimers.put(target, l); + long l = 0L; + for (String s : targets) { + l += ((long) debugTimers.get(s)); } + debugTimers.put(target, l); } public static long getTIME_T() { diff --git a/src/net/torvald/terrarum/ModMgr.kt b/src/net/torvald/terrarum/ModMgr.kt index bdb9a0ec6..cdf779f46 100644 --- a/src/net/torvald/terrarum/ModMgr.kt +++ b/src/net/torvald/terrarum/ModMgr.kt @@ -219,10 +219,10 @@ object ModMgr { } - printdbg(this, "Module $moduleName processed") + printmsg(this, "Module $moduleName processed") } catch (noSuchModule: FileNotFoundException) { - printdbgerr(this, "No such module: $moduleName, skipping...") + printmsgerr(this, "No such module: $moduleName, skipping...") logError(LoadErrorType.NOT_EVEN_THERE, moduleName) @@ -232,8 +232,8 @@ object ModMgr { // TODO: Instead of skipping module with error, just display the error message onto the face? - printdbgerr(this, "There was an error while loading module $moduleName") - printdbgerr(this, "\t$e") + printmsgerr(this, "There was an error while loading module $moduleName") + printmsgerr(this, "\t$e") print(App.csiR); e.printStackTrace(System.out); print(App.csi0) logError(LoadErrorType.YOUR_FAULT, moduleName, e) diff --git a/src/net/torvald/terrarum/blockproperties/BlockCodex.kt b/src/net/torvald/terrarum/blockproperties/BlockCodex.kt index d480d1977..d7b79244e 100644 --- a/src/net/torvald/terrarum/blockproperties/BlockCodex.kt +++ b/src/net/torvald/terrarum/blockproperties/BlockCodex.kt @@ -3,7 +3,6 @@ package net.torvald.terrarum.blockproperties import net.torvald.gdx.graphics.Cvec import net.torvald.terrarum.App import net.torvald.terrarum.App.printdbg -import net.torvald.terrarum.App.printmsg import net.torvald.terrarum.ReferencingRanges.PREFIX_VIRTUALTILE import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.gameworld.FluidType @@ -59,7 +58,7 @@ class BlockCodex { * Later entry (possible from other modules) will replace older ones */ fun fromModule(module: String, path: String) { - App.printmsg(this, "Building block properties table") + printdbg(this, "Building block properties table") try { register(module, CSVFetcher.readFromModule(module, path)) } @@ -67,7 +66,7 @@ class BlockCodex { } fun fromCSV(module: String, csvString: String) { - App.printmsg(this, "Building wire properties table for module $module") + printdbg(this, "Building wire properties table for module $module") val csvParser = org.apache.commons.csv.CSVParser.parse( csvString, @@ -211,7 +210,7 @@ class BlockCodex { blockProps[prop.id] = prop - printmsg(this, "Setting prop ${prop.id} ->>\t${prop.nameKey}\tsolid:${prop.isSolid}") + printdbg(this, "Setting prop ${prop.id} ->>\t${prop.nameKey}\tsolid:${prop.isSolid}") } } diff --git a/src/net/torvald/terrarum/blockproperties/WireCodex.kt b/src/net/torvald/terrarum/blockproperties/WireCodex.kt index 7a96f7ff5..5a0c07668 100644 --- a/src/net/torvald/terrarum/blockproperties/WireCodex.kt +++ b/src/net/torvald/terrarum/blockproperties/WireCodex.kt @@ -1,6 +1,7 @@ package net.torvald.terrarum.blockproperties import net.torvald.terrarum.* +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID @@ -39,7 +40,7 @@ class WireCodex { * @param path to the "wires" directory, not path to the CSV; must end with a slash! */ fun fromModule(module: String, path: String) { - App.printmsg(this, "Building wire properties table for module $module") + printdbg(this, "Building wire properties table for module $module") try { register(module, path, CSVFetcher.readFromModule(module, path + "wires.csv")) } @@ -47,7 +48,7 @@ class WireCodex { } fun fromCSV(module: String, path: String, csvString: String) { - App.printmsg(this, "Building wire properties table for module $module") + printdbg(this, "Building wire properties table for module $module") val csvParser = org.apache.commons.csv.CSVParser.parse( csvString, @@ -64,7 +65,7 @@ class WireCodex { setProp(module, it.intVal("id"), it) } - App.printmsg(this, "Registering wire textures into the resource pool") + printdbg(this, "Registering wire textures into the resource pool") wireProps.keys.forEach { id -> val wireid = id.split(':').last().toInt() @@ -148,6 +149,6 @@ class WireCodex { val loadedClassInstance = loadedClassConstructor.newInstance(prop.id, invImgSheet, invImgX, invImgY) ItemCodex[prop.id] = loadedClassInstance as GameItem - App.printmsg(this, "Setting prop ${prop.id} ->>\t${prop.nameKey}") + printdbg(this, "Setting prop ${prop.id} ->>\t${prop.nameKey}") } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gameitems/GameItem.kt b/src/net/torvald/terrarum/gameitems/GameItem.kt index 94c05ca69..01762c62e 100644 --- a/src/net/torvald/terrarum/gameitems/GameItem.kt +++ b/src/net/torvald/terrarum/gameitems/GameItem.kt @@ -371,6 +371,20 @@ fun mouseInInteractableRange(actor: ActorWithBody, action: () -> Boolean): Boole val distMax = actor.actorValue.getAsDouble(AVKey.REACH)!! * (actor.actorValue.getAsDouble(AVKey.REACHBUFF) ?: 1.0) * actor.scale // perform some error checking here if (dist <= distMax.sqr()) return action() else return false } +fun mouseInInteractableRangeTools(actor: ActorWithBody, item: GameItem?, reachMultiplierInTiles: (Int) -> Double = { it.toDouble() }, action: () -> Boolean): Boolean { + if (item == null) return false + val mousePos1 = Vector2(Terrarum.mouseX, Terrarum.mouseY) + val mousePos2 = Vector2(Terrarum.mouseX + INGAME.world.width * TILE_SIZED, Terrarum.mouseY) + val mousePos3 = Vector2(Terrarum.mouseX - INGAME.world.width * TILE_SIZED, Terrarum.mouseY) + val actorPos = actor.centrePosVector + val dist = minOf(actorPos.distanceSquared(mousePos1), actorPos.distanceSquared(mousePos2), actorPos.distanceSquared(mousePos3)) + + val reachBonus = (actor.actorValue.getAsDouble(AVKey.REACHBUFF) ?: 1.0) * actor.scale + val distMax = actor.actorValue.getAsDouble(AVKey.REACH)!! * reachBonus // perform some error checking here + val toolDistMax = (TILE_SIZED * reachMultiplierInTiles(item.material.toolReach)) * reachBonus + + if (dist <= minOf(toolDistMax, distMax).sqr()) return action() else return false +} fun IntRange.pickRandom() = HQRNG().nextInt(this.endInclusive - this.start + 1) + this.start // count() on 200 million entries? Se on vitun hyvää idea fun IntArray.pickRandom(): Int = this[HQRNG().nextInt(this.size)] fun DoubleArray.pickRandom(): Double = this[HQRNG().nextInt(this.size)] diff --git a/src/net/torvald/terrarum/itemproperties/ItemCodex.kt b/src/net/torvald/terrarum/itemproperties/ItemCodex.kt index a15db0bc4..fd4c0fc50 100644 --- a/src/net/torvald/terrarum/itemproperties/ItemCodex.kt +++ b/src/net/torvald/terrarum/itemproperties/ItemCodex.kt @@ -58,9 +58,7 @@ class ItemCodex { * @param: dynamicID string of "dyn:" */ fun registerNewDynamicItem(dynamicID: ItemID, item: GameItem) { - if (App.IS_DEVELOPMENT_BUILD) { - printdbg(this, "Registering new dynamic item $dynamicID (from ${item.originalID})") - } + printdbg(this, "Registering new dynamic item $dynamicID (from ${item.originalID})") dynamicItemDescription[dynamicID] = item dynamicToStaticTable[dynamicID] = item.originalID } diff --git a/src/net/torvald/terrarum/itemproperties/Material.kt b/src/net/torvald/terrarum/itemproperties/Material.kt index a1679d879..43dd1c1a4 100644 --- a/src/net/torvald/terrarum/itemproperties/Material.kt +++ b/src/net/torvald/terrarum/itemproperties/Material.kt @@ -1,7 +1,6 @@ package net.torvald.terrarum.itemproperties -import net.torvald.terrarum.App -import net.torvald.terrarum.App.printmsg +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.Codex import net.torvald.terrarum.blockproperties.floatVal import net.torvald.terrarum.blockproperties.intVal @@ -44,7 +43,7 @@ class MaterialCodex { internal constructor() fun fromModule(module: String, path: String) { - App.printmsg(this, "Building material properties table") + printdbg(this, "Building material properties table") try { register(CSVFetcher.readFromModule(module, path)) } @@ -74,7 +73,7 @@ class MaterialCodex { materialProps[prop.identifier] = prop - printmsg(this, "${prop.identifier}\t${prop.strength}\t${prop.density}\t${prop.forceMod}\t${prop.enduranceMod}") + printdbg(this, "${prop.identifier}\t${prop.strength}\t${prop.density}\t${prop.forceMod}\t${prop.enduranceMod}") } } diff --git a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt index 0a76b108c..1b9475d95 100644 --- a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt +++ b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt @@ -1137,8 +1137,8 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) { arrayOf(actorContainerActive, actorContainerInactive).forEach { actorContainer -> val indexToDelete = actorContainer.searchForIndex(actor.referenceID) { it.referenceID } if (indexToDelete != null) { - printdbg(this, "Removing actor $actor") - printStackTrace(this) +// printdbg(this, "Removing actor $actor") +// printStackTrace(this) actor.dispose() actorContainer.removeAt(indexToDelete) @@ -1180,13 +1180,13 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) { override fun addNewActor(actor: Actor?) { if (actor == null) return - if (App.IS_DEVELOPMENT_BUILD && theGameHasActor(actor.referenceID)) { + if (theGameHasActor(actor.referenceID)) { throw ReferencedActorAlreadyExistsException(actor) } else { if (actor.referenceID !in ReferencingRanges.ACTORS_WIRES && actor.referenceID !in ReferencingRanges.ACTORS_WIRES_HELPER) { - printdbg(this, "Adding actor $actor") - printStackTrace(this) +// printdbg(this, "Adding actor $actor") +// printStackTrace(this) } actorContainerActive.add(actor) @@ -1199,7 +1199,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) { } fun activateDormantActor(actor: Actor) { - if (App.IS_DEVELOPMENT_BUILD && !isInactive(actor.referenceID)) { + if (!isInactive(actor.referenceID)) { /*if (isActive(actor.referenceID)) throw Error("The actor $actor is already activated") else @@ -1306,7 +1306,7 @@ open class TerrarumIngame(batch: SpriteBatch) : IngameInstance(batch) { } - println("[Ingame] Resize event") + printdbg(this, "Resize event") } override fun dispose() { diff --git a/src/net/torvald/terrarum/modulebasegame/TitleScreen.kt b/src/net/torvald/terrarum/modulebasegame/TitleScreen.kt index c60f313fa..55040ddc8 100644 --- a/src/net/torvald/terrarum/modulebasegame/TitleScreen.kt +++ b/src/net/torvald/terrarum/modulebasegame/TitleScreen.kt @@ -231,7 +231,7 @@ class TitleScreen(batch: SpriteBatch) : IngameInstance(batch) { worldFBO = FloatFrameBuffer(App.scr.width, App.scr.height, false) // load list of savegames - println("[TitleScreen] update list of savegames") + printdbg(this, "update list of savegames") // to show "Continue" and "Load" on the titlescreen, uncomment this line App.updateListOfSavegames() diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt index 2b1d7d8d1..925a88a21 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureStorageChest.kt @@ -46,7 +46,6 @@ internal class FixtureStorageChest : FixtureBase { actorValue[AVKey.BASEMASS] = MASS - println("FixtureStorageChest constructor call") printStackTrace(this) } diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/PickaxeGeneric.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/PickaxeGeneric.kt index b6a41927e..9c2a56520 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/PickaxeGeneric.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/PickaxeGeneric.kt @@ -10,6 +10,7 @@ import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.gameitems.mouseInInteractableRange +import net.torvald.terrarum.gameitems.mouseInInteractableRangeTools import net.torvald.terrarum.itemproperties.Calculate import net.torvald.terrarum.modulebasegame.gameactors.DroppedItem import net.torvald.terrarum.modulebasegame.gameitems.PickaxeCore.BASE_MASS_AND_SIZE @@ -29,7 +30,7 @@ object PickaxeCore { fun startPrimaryUse( actor: ActorWithBody, delta: Float, item: GameItem?, mx: Int, my: Int, dropProbability: Double = 1.0, mw: Int = 1, mh: Int = 1, attackActorBlocks: Boolean = true - ) = mouseInInteractableRange(actor) { + ) = mouseInInteractableRangeTools(actor, item) { // un-round the mx val ww = INGAME.world.width val apos = actor.centrePosPoint diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt b/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt index 37d13b08e..35ab71854 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UILoadDemoSavefiles.kt @@ -157,7 +157,7 @@ class UILoadDemoSavefiles(val remoCon: UIRemoCon) : UICanvas() { scrollAnimCounter = 0f loadFired = 0 - println("savelist mode: $mode") + printdbg(this, "savelist mode: $mode") } override fun show() { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt index e4470d0f3..4576697d5 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIRemoCon.kt @@ -5,8 +5,7 @@ import com.badlogic.gdx.graphics.Camera import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.App -import net.torvald.terrarum.App.printdbg -import net.torvald.terrarum.App.printdbgerr +import net.torvald.terrarum.App.* import net.torvald.terrarum.QNDTreeNode import net.torvald.terrarum.Terrarum import net.torvald.terrarum.Yaml @@ -109,7 +108,7 @@ open class UIRemoCon(val parent: TitleScreen, val treeRoot: QNDTreeNode) val tag = it.tags if (tag.contains("WRITETOCONFIG")) WriteConfig() - print("[UIRemoCon] Returning from ${currentRemoConContents.data}") + if (IS_DEVELOPMENT_BUILD) print("[UIRemoCon] Returning from ${currentRemoConContents.data}") if (currentRemoConContents.parent != null) { remoConTray.consume() @@ -120,7 +119,7 @@ open class UIRemoCon(val parent: TitleScreen, val treeRoot: QNDTreeNode) parent.uiFakeBlurOverlay.setAsClose() - println(" to ${currentlySelectedRemoConItem}") + if (IS_DEVELOPMENT_BUILD) println(" to ${currentlySelectedRemoConItem}") } else { throw NullPointerException("No parent node to return") @@ -128,8 +127,10 @@ open class UIRemoCon(val parent: TitleScreen, val treeRoot: QNDTreeNode) } else { // check if target exists - //println("current node: ${currentRemoConContents.data}") - //currentRemoConContents.children.forEach { println("- ${it.data}") } + if (IS_DEVELOPMENT_BUILD) { + //println("current node: ${currentRemoConContents.data}") + //currentRemoConContents.children.forEach { println("- ${it.data}") } + } if (currentRemoConContents.children.size > selectedIndex ?: 0x7FFFFFFF) { setNewRemoConContents(currentRemoConContents.children[selectedIndex!!])