From a41037826ca80c2580dee3a93992f0fc26be6767 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 17 Sep 2023 22:28:27 +0700 Subject: [PATCH] Fixes to dataValue and flickering lights --- build.gradle.kts | 2 +- .../kstarbound/client/StarboundClient.kt | 8 ++--- .../dbotthepony/kstarbound/defs/JsonDriven.kt | 32 ++++++++----------- .../kstarbound/math/PeriodicFunction.kt | 16 ++++++++-- .../ru/dbotthepony/kstarbound/world/World.kt | 3 ++ .../kstarbound/world/object/WorldObject.kt | 15 ++++++--- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index bc921dd7..fd3b56a9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -82,7 +82,7 @@ dependencies { implementation("com.github.jnr:jnr-ffi:2.2.13") implementation("ru.dbotthepony:kbox2d:2.4.1.6") - implementation("ru.dbotthepony:kvector:2.6.0") + implementation("ru.dbotthepony:kvector:2.6.1") implementation("com.github.ben-manes.caffeine:caffeine:3.1.5") } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt index 5fdbdac2..67ae6f11 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/client/StarboundClient.kt @@ -788,10 +788,10 @@ class StarboundClient : Closeable { viewportWidth / settings.zoom / PIXELS_IN_STARBOUND_UNIT, viewportHeight / settings.zoom / PIXELS_IN_STARBOUND_UNIT) - viewportCellX = roundTowardsNegativeInfinity(viewportRectangle.mins.x) - 4 - viewportCellY = roundTowardsNegativeInfinity(viewportRectangle.mins.y) - 4 - viewportCellWidth = roundTowardsPositiveInfinity(viewportRectangle.width) + 8 - viewportCellHeight = roundTowardsPositiveInfinity(viewportRectangle.height) + 8 + viewportCellX = roundTowardsNegativeInfinity(viewportRectangle.mins.x) - 16 + viewportCellY = roundTowardsNegativeInfinity(viewportRectangle.mins.y) - 16 + viewportCellWidth = roundTowardsPositiveInfinity(viewportRectangle.width) + 32 + viewportCellHeight = roundTowardsPositiveInfinity(viewportRectangle.height) + 32 if (viewportLighting.width != viewportCellWidth || viewportLighting.height != viewportCellHeight) { viewportLighting = LightCalculator(viewportCells, viewportCellWidth, viewportCellHeight) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt index aeae639e..c46ef10d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/JsonDriven.kt @@ -182,10 +182,19 @@ abstract class JsonDriven(val path: String) { value = properties[name]?.let { if (alwaysCopy) it.deepCopy() else it } } else { val itr = defs.iterator() - value = merge(properties[name], itr.next()[name]) + var isCopy = false + value = properties[name] while ((value == null || value is JsonObject) && itr.hasNext()) { - value = mergeNoCopy(value, itr.next()[name]) + val next = itr.next()[name] + + if (value is JsonObject) { + if (next !is JsonObject) continue + value = mergeNoCopy(if (isCopy) value else value.deepCopy(), next) + isCopy = true + } else { + value = next + } } } @@ -208,27 +217,12 @@ abstract class JsonDriven(val path: String) { if (existing == null) { a[k] = v.deepCopy() - } else { - a[k] = mergeNoCopy(existing, v)!! + } else if (existing is JsonObject && v is JsonObject) { + a[k] = mergeNoCopy(existing, v) } } return a } - - @JvmStatic - protected fun mergeNoCopy(a: JsonElement?, b: JsonElement?): JsonElement? { - if (a is JsonObject && b is JsonObject) return mergeNoCopy(a, b) - return a - } - - @JvmStatic - protected fun merge(a: JsonElement?, b: JsonElement?): JsonElement? { - if (a == null && b == null) return null - if (a == null) return b!!.deepCopy() - if (b == null) return a.deepCopy() - - return mergeNoCopy(a.deepCopy(), b) - } } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/math/PeriodicFunction.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/math/PeriodicFunction.kt index f78453d7..2ebb6d7b 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/math/PeriodicFunction.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/math/PeriodicFunction.kt @@ -2,6 +2,8 @@ package ru.dbotthepony.kstarbound.math import ru.dbotthepony.kvector.util.linearInterpolation import java.util.random.RandomGenerator +import kotlin.math.PI +import kotlin.math.sin data class PeriodicFunction( val period: Double = 1.0, @@ -14,6 +16,12 @@ data class PeriodicFunction( fun interpolate(t: Double, a: Double, b: Double): Double } + object Sin : Interpolator { + override fun interpolate(t: Double, a: Double, b: Double): Double { + return linearInterpolation((sin(t * PI - PI / 2.0) + 1.0) / 2.0, a, b) + } + } + private var timer = 0.0 private var timerMax = 1.0 @@ -31,9 +39,9 @@ data class PeriodicFunction( // we, however, do not, if period time is big enough while (timer <= 0.0) { base = target - target = (if (isDescending) min else max) + random.nextDouble(-magnitudeVariance, magnitudeVariance) + target = (if (isDescending) min else max) + if (magnitudeVariance == 0.0) 0.0 else random.nextDouble(-magnitudeVariance, magnitudeVariance) isDescending = !isDescending - timerMax = (halfPeriod + random.nextDouble(-halfVariance, halfVariance)).coerceAtLeast(0.01) + timerMax = (halfPeriod + if (halfVariance == 0.0) 0.0 else random.nextDouble(-halfVariance, halfVariance)).coerceAtLeast(0.01) timer += timerMax if (timerMax <= 0.05 && timer <= 0.0) { @@ -50,4 +58,8 @@ data class PeriodicFunction( fun value(): Double { return value(::linearInterpolation) } + + fun sinValue(): Double { + return value(Sin) + } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt index d64218fe..7cf86ab9 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt @@ -27,6 +27,7 @@ import ru.dbotthepony.kvector.vector.Vector2d import ru.dbotthepony.kvector.vector.Vector2i import java.lang.ref.ReferenceQueue import java.lang.ref.WeakReference +import java.util.random.RandomGenerator const val CHUNK_SIZE_BITS = 5 const val CHUNK_SIZE_MASK = 1 or 2 or 4 or 8 or 16 @@ -239,6 +240,8 @@ abstract class World, ChunkType : Chunk String?>() private var frame = 0 set(value) { @@ -62,6 +61,7 @@ open class WorldObject( } private var frameTimer = 0.0 + val flickerPeriod = prototype.value.flickerPeriod?.copy() var isRemoved = false private set @@ -138,7 +138,7 @@ open class WorldObject( } open fun thinkShared() { - + flickerPeriod?.update(Starbound.TICK_TIME_ADVANCE, world.random) } open fun thinkClient() { @@ -170,8 +170,15 @@ open class WorldObject( } fun addLights(lightCalculator: LightCalculator, xOffset: Int, yOffset: Int) { - if ("default" in lightColors) { - lightCalculator.addPointLight(pos.x - xOffset, pos.y - yOffset, lightColors["default"]!!) + var color = lightColors[color.lowercase] + + if (color != null) { + if (flickerPeriod != null) { + val sample = flickerPeriod.sinValue().toFloat() + color *= sample + } + + lightCalculator.addPointLight(pos.x - xOffset, pos.y - yOffset, color) } }