From 27834cc5951f5133fe51705aeeef1a255bd7ad4e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 12:23:08 +0700 Subject: [PATCH 1/6] Fix wrong layout final iteration value --- .../mc/otm/client/screen/panels/util/GridPanel.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt index 4e7dd5800..3b655f3de 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/util/GridPanel.kt @@ -68,12 +68,12 @@ open class GridPanel( } override fun columns(last: Int): IntIterator { - return (last - 1 downTo 1).iterator() + return (last - 1 downTo 0).iterator() } }, BOTTOM_LEFT { override fun rows(last: Int): IntIterator { - return (last - 1 downTo 1).iterator() + return (last - 1 downTo 0).iterator() } override fun columns(last: Int): IntIterator { @@ -83,11 +83,11 @@ open class GridPanel( BOTTOM_RIGHT { override fun rows(last: Int): IntIterator { - return (last - 1 downTo 1).iterator() + return (last - 1 downTo 0).iterator() } override fun columns(last: Int): IntIterator { - return (last - 1 downTo 1).iterator() + return (last - 1 downTo 0).iterator() } }; From 269227f6cf7ed3f9a0c0a879010bc42bb89d1c00 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 12:58:13 +0700 Subject: [PATCH 2/6] Add helper methods for fast DockProperty creation --- .../otm/client/screen/panels/EditablePanel.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index 530c92222..501315db1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -83,9 +83,54 @@ data class DockProperty(val left: Float = 0f, val top: Float = 0f, val right: Fl companion object { val EMPTY = DockProperty() + @JvmStatic fun all(value: Float): DockProperty { return DockProperty(value, value, value, value) } + + @JvmStatic + @JvmOverloads + fun topLeft(top: Float, left: Float = top): DockProperty { + return DockProperty(left = left, top = top) + } + + @JvmStatic + fun left(value: Float): DockProperty { + return DockProperty(left = value) + } + + @JvmStatic + @JvmOverloads + fun topRight(top: Float, right: Float = top): DockProperty { + return DockProperty(right = right, top = top) + } + + @JvmStatic + fun right(value: Float): DockProperty { + return DockProperty(right = value) + } + + @JvmStatic + fun top(value: Float): DockProperty { + return DockProperty(top = value) + } + + @JvmStatic + @JvmOverloads + fun bottomLeft(bottom: Float, left: Float = bottom): DockProperty { + return DockProperty(left = left, bottom = bottom) + } + + @JvmStatic + @JvmOverloads + fun bottomRight(bottom: Float, right: Float = bottom): DockProperty { + return DockProperty(right = right, bottom = bottom) + } + + @JvmStatic + fun bottom(value: Float): DockProperty { + return DockProperty(bottom = value) + } } } From 9b27ed7fb6a156f32967000c38efea881ea75759 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 13:33:53 +0700 Subject: [PATCH 3/6] Add netFloat and nextDouble helper methods to RandomSource --- .../kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index 6203217b1..47b8a1c39 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -662,3 +662,21 @@ fun RandomSource.nextNormalDoubles(stddev: Double, mean: Double): DoublePair { fun RandomSource.nextNormalDouble(stddev: Double, mean: Double): Double { return nextGaussian() * stddev + mean } + +fun RandomSource.nextFloat(min: Float, max: Float): Float { + if (this is RandomGenerator) + return nextFloat(min, max) + + require(max >= min) { "Min is bigger than max: $min vs $max" } + if (min == max) return min + return min + nextFloat() * (max - min) +} + +fun RandomSource.nextDouble(min: Double, max: Double): Double { + if (this is RandomGenerator) + return nextDouble(min, max) + + require(max >= min) { "Min is bigger than max: $min vs $max" } + if (min == max) return min + return min + nextDouble() * (max - min) +} From b921658eb2f7118b59c0f292766b17f5b5ee6ee3 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 13:34:53 +0700 Subject: [PATCH 4/6] Remove `randomGenerator` from editable panel --- .../client/screen/panels/DecimalHistoryChartPanel.kt | 2 +- .../mc/otm/client/screen/panels/EditablePanel.kt | 4 ---- .../otm/client/screen/tech/AndroidStationScreen.kt | 12 +++++++----- .../ru/dbotthepony/mc/otm/core/math/Clustering.kt | 5 +++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DecimalHistoryChartPanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DecimalHistoryChartPanel.kt index 1b262ce32..dfc9287ab 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DecimalHistoryChartPanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/DecimalHistoryChartPanel.kt @@ -52,7 +52,7 @@ open class DecimalHistoryChartPanel>( map[1f] = formatText(maximum) - for (cluster in chart.asIterable().clusterize(randomGenerator)) { + for (cluster in chart.asIterable().clusterize(random)) { val perc = (cluster.center / maximum).toFloat() if (map.keys.none { (it - perc).absoluteValue < 0.08f }) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index 501315db1..f828af807 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -232,10 +232,6 @@ open class EditablePanel( } } - val randomGenerator: RandomGenerator by lazy { - RandomSource2Generator(random) - } - /** * Bigger values means lesser priority while docking, rendering and processing inputs. */ diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt index f6edd683a..0b52df472 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt @@ -7,6 +7,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap import net.minecraft.ChatFormatting import net.minecraft.client.Minecraft import net.minecraft.network.chat.Component +import net.minecraft.util.RandomSource import net.minecraft.world.entity.player.Inventory import net.minecraft.world.item.ItemStack import net.neoforged.neoforge.network.PacketDistributor @@ -34,6 +35,7 @@ import ru.dbotthepony.mc.otm.config.MachinesConfig import ru.dbotthepony.kommons.math.RGBAColor import ru.dbotthepony.mc.otm.player.matteryPlayer import ru.dbotthepony.mc.otm.client.screen.panels.button.BooleanButtonPanel +import ru.dbotthepony.mc.otm.core.nextFloat import ru.dbotthepony.mc.otm.menu.tech.AndroidStationMenu import ru.dbotthepony.mc.otm.network.AndroidResearchRequestPacket import java.util.* @@ -443,8 +445,8 @@ private class AndroidResearchButton( } private enum class PreviewScrollers( - val init: (EditablePanel<*>, RandomGenerator) -> Unit, - val scroll: (EditablePanel<*>, RandomGenerator) -> Boolean + val init: (EditablePanel<*>, RandomSource) -> Unit, + val scroll: (EditablePanel<*>, RandomSource) -> Boolean ) { LEFT_TO_RIGHT( init = { it, random -> @@ -540,14 +542,14 @@ class AndroidStationScreen(p_97741_: AndroidStationMenu, p_97742_: Inventory, p_ if (isPreview && !layoutInvalidated) { if (firstTick) { - scroller.init.invoke(this, randomGenerator) + scroller.init.invoke(this, random) } - val status = scroller.scroll.invoke(this, randomGenerator) + val status = scroller.scroll.invoke(this, random) if (!status) { scroller = PreviewScrollers.entries.let { it[random.nextInt(it.size)] } - scroller.init.invoke(this, randomGenerator) + scroller.init.invoke(this, random) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Clustering.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Clustering.kt index a13d846c0..267d40356 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Clustering.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Clustering.kt @@ -1,6 +1,7 @@ package ru.dbotthepony.mc.otm.core.math import it.unimi.dsi.fastutil.objects.ObjectArrayList +import net.minecraft.util.RandomSource import ru.dbotthepony.mc.otm.core.random import java.util.random.RandomGenerator import kotlin.math.min @@ -88,7 +89,7 @@ private class MutableCluster>(var center: V) { } private fun > Iterable.clusterize( - random: RandomGenerator, + random: RandomSource, initialClusters: Int = 1, zeroBound: Boolean = false, identity: V, @@ -202,7 +203,7 @@ private fun > Iterable.clusterize( // TODO: could use some tweaking private val DECIMAL_ERROR_TOLERANCE = Decimal("0.02") -fun Iterable.clusterize(random: RandomGenerator, clusters: Int? = null, zeroBound: Boolean = false): List> { +fun Iterable.clusterize(random: RandomSource, clusters: Int? = null, zeroBound: Boolean = false): List> { return clusterize(random, clusters ?: 1, zeroBound, Decimal.ZERO, Decimal::plus, Decimal::minus, Decimal::div, Decimal::absoluteValue) { min, max, error -> if (clusters != null) false From f9821aa5520407d11390d58a18dc5bad9d904a56 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 13:36:46 +0700 Subject: [PATCH 5/6] Use GJRAND64 in menus --- .../dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt | 5 +++-- .../mc/otm/client/screen/tech/AndroidStationScreen.kt | 4 ++-- src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index f828af807..1b3c0da7f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -29,6 +29,7 @@ import ru.dbotthepony.mc.otm.client.screen.panels.input.QueryUserPanel import ru.dbotthepony.mc.otm.core.RandomSource2Generator import ru.dbotthepony.mc.otm.core.collect.concatIterators import ru.dbotthepony.mc.otm.core.collect.flatMap +import ru.dbotthepony.mc.otm.core.util.GJRAND64RandomSource import java.util.* import java.util.concurrent.CopyOnWriteArrayList import java.util.function.Predicate @@ -224,11 +225,11 @@ open class EditablePanel( } } - val random: RandomSource by lazy { + val random: GJRAND64RandomSource by lazy { if (screen is MatteryScreen<*>) { screen.menu.random } else { - RandomSource.create() + GJRAND64RandomSource() } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt index 0b52df472..09ef63314 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/AndroidStationScreen.kt @@ -445,8 +445,8 @@ private class AndroidResearchButton( } private enum class PreviewScrollers( - val init: (EditablePanel<*>, RandomSource) -> Unit, - val scroll: (EditablePanel<*>, RandomSource) -> Boolean + val init: (EditablePanel<*>, RandomGenerator) -> Unit, + val scroll: (EditablePanel<*>, RandomGenerator) -> Boolean ) { LEFT_TO_RIGHT( init = { it, random -> diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt index 8a3711faa..3568f9ecf 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/MatteryMenu.kt @@ -44,6 +44,7 @@ import ru.dbotthepony.mc.otm.container.sortWithIndices import ru.dbotthepony.mc.otm.core.ResourceLocation import ru.dbotthepony.mc.otm.core.collect.ConditionalSet import ru.dbotthepony.mc.otm.core.math.Decimal +import ru.dbotthepony.mc.otm.core.util.GJRAND64RandomSource import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback import ru.dbotthepony.mc.otm.menu.widget.ProfiledLevelGaugeWidget import ru.dbotthepony.mc.otm.network.MatteryStreamCodec @@ -81,7 +82,7 @@ abstract class MatteryMenu( val mSynchronizer = SynchableGroup() val synchronizerRemote = mSynchronizer.Remote() val player: Player get() = inventory.player - val random: RandomSource = RandomSource.create() + val random = GJRAND64RandomSource() private val _playerInventorySlots = ArrayList() private val _playerHotbarSlots = ArrayList() From 3e92c5272dd85f0826bfe29a90a1f900beb51316 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 21 Mar 2025 13:52:00 +0700 Subject: [PATCH 6/6] Move panels additional types to separate file --- .../otm/client/screen/panels/EditablePanel.kt | 115 ------------------ .../mc/otm/client/screen/panels/Types.kt | 115 ++++++++++++++++++ 2 files changed, 115 insertions(+), 115 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/Types.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt index 1b3c0da7f..e2be32ce9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/EditablePanel.kt @@ -11,9 +11,7 @@ import net.minecraft.client.gui.components.events.GuiEventListener import net.minecraft.client.gui.navigation.FocusNavigationEvent import net.minecraft.client.gui.navigation.ScreenRectangle import net.minecraft.client.gui.screens.Screen -import net.minecraft.client.renderer.Rect2i import net.minecraft.network.chat.Component -import net.minecraft.util.RandomSource import org.apache.logging.log4j.LogManager import ru.dbotthepony.mc.otm.SystemTime import ru.dbotthepony.mc.otm.client.CursorType @@ -26,129 +24,16 @@ import ru.dbotthepony.mc.otm.client.render.popScissorRect import ru.dbotthepony.mc.otm.client.render.pushScissorRect import ru.dbotthepony.mc.otm.client.screen.MatteryScreen import ru.dbotthepony.mc.otm.client.screen.panels.input.QueryUserPanel -import ru.dbotthepony.mc.otm.core.RandomSource2Generator import ru.dbotthepony.mc.otm.core.collect.concatIterators import ru.dbotthepony.mc.otm.core.collect.flatMap import ru.dbotthepony.mc.otm.core.util.GJRAND64RandomSource import java.util.* import java.util.concurrent.CopyOnWriteArrayList import java.util.function.Predicate -import java.util.random.RandomGenerator import kotlin.collections.ArrayList import kotlin.math.max import kotlin.math.roundToInt -data class ScreenPos(val x: Float, val y: Float) - -data class DockProperty(val left: Float = 0f, val top: Float = 0f, val right: Float = 0f, val bottom: Float = 0f) { - val isEmpty get() = left == 0f && right == 0f && top == 0f && bottom == 0f - val horizontal get() = left + right - val vertical get() = top + bottom - - operator fun plus(other: DockProperty): DockProperty { - return DockProperty( - left + other.left, - top + other.top, - right + other.right, - bottom + other.bottom - ) - } - - operator fun minus(other: DockProperty): DockProperty { - return DockProperty( - left - other.left, - top - other.top, - right - other.right, - bottom - other.bottom - ) - } - - operator fun plus(other: Float): DockProperty { - return DockProperty( - left + other, - top + other, - right + other, - bottom + other - ) - } - - operator fun minus(other: Float): DockProperty { - return DockProperty( - left - other, - top - other, - right - other, - bottom - other - ) - } - - companion object { - val EMPTY = DockProperty() - - @JvmStatic - fun all(value: Float): DockProperty { - return DockProperty(value, value, value, value) - } - - @JvmStatic - @JvmOverloads - fun topLeft(top: Float, left: Float = top): DockProperty { - return DockProperty(left = left, top = top) - } - - @JvmStatic - fun left(value: Float): DockProperty { - return DockProperty(left = value) - } - - @JvmStatic - @JvmOverloads - fun topRight(top: Float, right: Float = top): DockProperty { - return DockProperty(right = right, top = top) - } - - @JvmStatic - fun right(value: Float): DockProperty { - return DockProperty(right = value) - } - - @JvmStatic - fun top(value: Float): DockProperty { - return DockProperty(top = value) - } - - @JvmStatic - @JvmOverloads - fun bottomLeft(bottom: Float, left: Float = bottom): DockProperty { - return DockProperty(left = left, bottom = bottom) - } - - @JvmStatic - @JvmOverloads - fun bottomRight(bottom: Float, right: Float = bottom): DockProperty { - return DockProperty(right = right, bottom = bottom) - } - - @JvmStatic - fun bottom(value: Float): DockProperty { - return DockProperty(bottom = value) - } - } -} - -enum class Dock { - NONE, LEFT, RIGHT, TOP, BOTTOM, FILL -} - -enum class DockResizeMode(val changeWidth: Boolean, val changeHeight: Boolean) { - ALL(true, true), NONE(false, false), WIDTH(true, false), HEIGHT(false, true) -} - -data class Rect2f(val x: Float, val y: Float, val width: Float, val height: Float) { - fun toIntRect(): Rect2i { - return Rect2i(x.roundToInt(), y.roundToInt(), width.roundToInt(), height.roundToInt()) - } -} - open class EditablePanel( val screen: S, parent: EditablePanel<*>?, diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/Types.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/Types.kt new file mode 100644 index 000000000..7d498d8f6 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/panels/Types.kt @@ -0,0 +1,115 @@ +package ru.dbotthepony.mc.otm.client.screen.panels + +import net.minecraft.client.renderer.Rect2i +import kotlin.math.roundToInt + +data class ScreenPos(val x: Float, val y: Float) + +data class DockProperty(val left: Float = 0f, val top: Float = 0f, val right: Float = 0f, val bottom: Float = 0f) { + val isEmpty get() = left == 0f && right == 0f && top == 0f && bottom == 0f + val horizontal get() = left + right + val vertical get() = top + bottom + + operator fun plus(other: DockProperty): DockProperty { + return DockProperty( + left + other.left, + top + other.top, + right + other.right, + bottom + other.bottom + ) + } + + operator fun minus(other: DockProperty): DockProperty { + return DockProperty( + left - other.left, + top - other.top, + right - other.right, + bottom - other.bottom + ) + } + + operator fun plus(other: Float): DockProperty { + return DockProperty( + left + other, + top + other, + right + other, + bottom + other + ) + } + + operator fun minus(other: Float): DockProperty { + return DockProperty( + left - other, + top - other, + right - other, + bottom - other + ) + } + + companion object { + val EMPTY = DockProperty() + + @JvmStatic + fun all(value: Float): DockProperty { + return DockProperty(value, value, value, value) + } + + @JvmStatic + @JvmOverloads + fun topLeft(top: Float, left: Float = top): DockProperty { + return DockProperty(left = left, top = top) + } + + @JvmStatic + fun left(value: Float): DockProperty { + return DockProperty(left = value) + } + + @JvmStatic + @JvmOverloads + fun topRight(top: Float, right: Float = top): DockProperty { + return DockProperty(right = right, top = top) + } + + @JvmStatic + fun right(value: Float): DockProperty { + return DockProperty(right = value) + } + + @JvmStatic + fun top(value: Float): DockProperty { + return DockProperty(top = value) + } + + @JvmStatic + @JvmOverloads + fun bottomLeft(bottom: Float, left: Float = bottom): DockProperty { + return DockProperty(left = left, bottom = bottom) + } + + @JvmStatic + @JvmOverloads + fun bottomRight(bottom: Float, right: Float = bottom): DockProperty { + return DockProperty(right = right, bottom = bottom) + } + + @JvmStatic + fun bottom(value: Float): DockProperty { + return DockProperty(bottom = value) + } + } +} + +enum class Dock { + NONE, LEFT, RIGHT, TOP, BOTTOM, FILL +} + +enum class DockResizeMode(val changeWidth: Boolean, val changeHeight: Boolean) { + ALL(true, true), NONE(false, false), WIDTH(true, false), HEIGHT(false, true) +} + +data class Rect2f(val x: Float, val y: Float, val width: Float, val height: Float) { + fun toIntRect(): Rect2i { + return Rect2i(x.roundToInt(), y.roundToInt(), width.roundToInt(), height.roundToInt()) + } +}