fix: some NaNs that can be caused by scroll bars

This commit is contained in:
minjaesong
2025-02-01 20:51:40 +09:00
parent a2006b0354
commit 69ebdbc542
13 changed files with 19 additions and 18 deletions

View File

@@ -3,6 +3,7 @@ package com.sudoplay.joise.module
import com.sudoplay.joise.ModuleInstanceMap
import com.sudoplay.joise.ModuleMap
import com.sudoplay.joise.ModulePropertyMap
import net.torvald.terrarum.ifNaN
/**
* Created by minjaesong on 2024-09-07.
@@ -12,7 +13,7 @@ class TerrarumModuleCaveLayerClosureGrad : Module() {
protected val l = ScalarParameter(3.0)
private fun Double.op() = Math.sqrt((-this + h.value - l.value) / l.value).let { if (it.isNaN()) 0.0 else it.coerceIn(0.0, 1.0) }
private fun Double.op() = Math.sqrt((-this + h.value - l.value) / l.value).ifNaN(0.0).coerceIn(0.0, 1.0)
override fun get(x: Double, y: Double) = y.op()
override fun get(x: Double, y: Double, z: Double) = y.op()/*.also {

View File

@@ -3,6 +3,7 @@ package com.sudoplay.joise.module
import com.sudoplay.joise.ModuleInstanceMap
import com.sudoplay.joise.ModuleMap
import com.sudoplay.joise.ModulePropertyMap
import net.torvald.terrarum.ifNaN
import net.torvald.terrarum.modulebasegame.worldgenerator.Worldgen.YHEIGHT_DIVISOR
import net.torvald.terrarum.modulebasegame.worldgenerator.Worldgen.YHEIGHT_MAGIC
@@ -19,7 +20,7 @@ class TerrarumModuleLavaFloorGrad : Module() {
private fun undoYtransform(yp: Double): Double = yp + (3200 - YHEIGHT_MAGIC) * YHEIGHT_DIVISOR
private fun Double.op() = Math.sqrt((this - h.value + l.value) / l.value).let { if (it.isNaN()) 0.0 else it }
private fun Double.op() = Math.sqrt((this - h.value + l.value) / l.value).ifNaN(0.0)
override fun get(x: Double, y: Double) = undoYtransform(y).op()
override fun get(x: Double, y: Double, z: Double) = undoYtransform(y).op()/*.also {

View File

@@ -648,6 +648,8 @@ inline fun Boolean.toLong(shift: Int = 0) = if (this) 1L.shl(shift) else 0L
inline fun Int.bitCount() = java.lang.Integer.bitCount(this)
inline fun Long.bitCount() = java.lang.Long.bitCount(this)
inline fun Double.signedSqrt() = this.abs().sqrt() * this.sign
inline fun Float.ifNaN(new: Float) = if (this.isNaN()) new else this
inline fun Double.ifNaN(new: Double) = if (this.isNaN()) new else this
fun absMax(left: Double, right: Double): Double {

View File

@@ -136,9 +136,5 @@ class Comp(
this.ratio = other.ratio
}
}
private fun Double.unNaN(d: Double): Double {
return if (this.isNaN()) d else this
}
}

View File

@@ -2298,7 +2298,7 @@ open class ActorWithBody : Actor {
@Transient internal val HITBOX_COLOURS1 = Color(0xFFFF0088.toInt())
fun isCloseEnough(a: Double, b: Double) = ((a / b).let { if (it.isNaN()) 0.0 else it } - 1).absoluteValue < PHYS_EPSILON_DIST
fun isCloseEnough(a: Double, b: Double) = ((a / b).ifNaN(0.0) - 1).absoluteValue < PHYS_EPSILON_DIST
}

View File

@@ -891,7 +891,7 @@ open class GameWorld(
if (fluidID == Block.NULL || fluidID == Block.NOT_GENERATED)
fluidID = Fluid.NULL
return FluidInfo(fluidID, if (fill.isNaN()) 0f else fill) // hex FFFFFFFF (magic number for ungenerated tiles) is interpreted as Float.NaN
return FluidInfo(fluidID, fill.ifNaN(0f)) // hex FFFFFFFF (magic number for ungenerated tiles) is interpreted as Float.NaN
}
/*private fun fluidTypeToBlock(type: FluidType) = when (type.abs()) {

View File

@@ -88,7 +88,7 @@ internal object ExportMap2 : ConsoleCommand {
}
private fun Iterable<Float>.normaliseNaN(default: Float = 0f): FloatArray {
return this.map { if (it.isNaN()) default else it }.toFloatArray()
return this.map { it.ifNaN(default) }.toFloatArray()
}
override fun execute(args: Array<String>) {

View File

@@ -340,11 +340,11 @@ open class FixtureSwingingDoorBase : FixtureBase {
private fun ActorWithBody.movingTowardsRight(): Boolean {
// return ((this.controllerV ?: Vector2()) + this.externalV).x >= PHYS_EPSILON_VELO
return (((this.controllerV?.x ?: 0.0) / this.externalV.x).let { if (it.isNaN()) 0.0 else it } - 1) >= PHYS_EPSILON_DIST
return (((this.controllerV?.x ?: 0.0) / this.externalV.x).ifNaN(0.0) - 1) >= PHYS_EPSILON_DIST
}
private fun ActorWithBody.movingTowardsLeft(): Boolean {
// return ((this.controllerV ?: Vector2()) + this.externalV).x <= -PHYS_EPSILON_VELO
return (((this.controllerV?.x ?: 0.0) / this.externalV.x).let { if (it.isNaN()) 0.0 else it } - 1) <= PHYS_EPSILON_DIST
return (((this.controllerV?.x ?: 0.0) / this.externalV.x).ifNaN(0.0) - 1) <= PHYS_EPSILON_DIST
}
private fun ActorWithBody.notMoving(): Boolean {
// return ((this.controllerV ?: Vector2()) + this.externalV).x.absoluteValue < PHYS_EPSILON_VELO

View File

@@ -249,7 +249,7 @@ class UIDebugInventron : UICanvas(
}
private fun drawAnalysis(batch: SpriteBatch) {
val scroll = (analyserScroll.value * analysisTextBuffer.size.times(TEXT_LINE_HEIGHT).minus(analyserHeight - 3)).roundToInt().coerceAtLeast(0)
val scroll = (analyserScroll.value * analysisTextBuffer.size.times(TEXT_LINE_HEIGHT).minus(analyserHeight - 3)).ifNaN(0.0).roundToInt().coerceAtLeast(0)
analysisTextBuffer.forEachIndexed { index, s ->
App.fontGame.draw(batch, s, analyserPosX + 6, analyserPosY2 + 3 + index * TEXT_LINE_HEIGHT - scroll)
}

View File

@@ -23,6 +23,7 @@ import net.torvald.terrarum.FlippingSpriteBatch
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.concurrent.ThreadExecutor
import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.ifNaN
import net.torvald.terrarum.inUse
import net.torvald.terrarum.modulebasegame.worldgenerator.BiomegenParams
import net.torvald.terrarum.modulebasegame.worldgenerator.TerragenParams
@@ -516,7 +517,7 @@ internal class TerragenTest(val seed: Long, val params: TerragenParams) : NoiseM
}
private fun Double.toColour(): Int {
val d = if (this.isNaN()) 0.0 else this.absoluteValue
val d = this.ifNaN(0.0).absoluteValue
val b = d.toFloat()
val c = if (b >= 2f)

View File

@@ -42,9 +42,9 @@ class UIItemHorzSlider(
// reset the scroll status
handlePos = 0.0
this.value = 0.0
field = value
field = value.coerceIn(12, width)
handleTravelDist = width - value
handleTravelDist = width - field
}
override val height = 24

View File

@@ -35,9 +35,9 @@ class UIItemVertSlider(
// reset the scroll status
handlePos = 0.0
this.value = 0.0
field = value
field = value.coerceIn(12, height)
handleTravelDist = height - value
handleTravelDist = height - field
}
companion object {

View File

@@ -517,7 +517,7 @@ internal object BlocksDrawer {
.filter { it.startsWith("fluid@") }.sorted().firstOrNull()
val fillThis =
world.layerFluids.unsafeGetTile1(wx, wy).second.let { if (it.isNaN()) 0f else it.coerceAtMost(1f) }
world.layerFluids.unsafeGetTile1(wx, wy).second.ifNaN(0f).coerceAtMost(1f)
val tile = world.getTileFromTerrain(wx, wy)