Compare commits

..

No commits in common. "b83c9edf137cb173e61de5c98f0e418fe97f95e0" and "d2f119a112e02339dc8a170578589a44347b85ab" have entirely different histories.

3 changed files with 34 additions and 18 deletions

View File

@ -37,13 +37,6 @@ class FlywheelBatteryBlockEntity(blockPos: BlockPos, blockState: BlockState) : M
private var multiblock: ShapedMultiblock? = null private var multiblock: ShapedMultiblock? = null
private var lastHeight = -1 private var lastHeight = -1
var batteryLevel = Decimal.ZERO var batteryLevel = Decimal.ZERO
set(value) {
if (field != value) {
field = value
markDirtyFast()
}
}
private var cachedChargeEfficiency = Decimal.ONE private var cachedChargeEfficiency = Decimal.ONE
val formed: Boolean val formed: Boolean

View File

@ -188,6 +188,24 @@ fun <T : Enum<T>> T.prev(values: Array<out T>): T {
return values[next] return values[next]
} }
fun IntArray.shuffle(random: RandomGenerator) {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
fun <T> MutableList<T>.shuffle(random: RandomGenerator) {
for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
}
fun IntArray.shuffle(random: RandomSource) { fun IntArray.shuffle(random: RandomSource) {
for (i in lastIndex downTo 1) { for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1) val j = random.nextInt(i + 1)
@ -198,7 +216,12 @@ fun IntArray.shuffle(random: RandomSource) {
} }
fun <T> MutableList<T>.shuffle(random: RandomSource) { fun <T> MutableList<T>.shuffle(random: RandomSource) {
return Util.shuffle(this, random) for (i in lastIndex downTo 1) {
val j = random.nextInt(i + 1)
val copy = this[i]
this[i] = this[j]
this[j] = copy
}
} }
fun <T> List<T>.random(random: RandomGenerator): T { fun <T> List<T>.random(random: RandomGenerator): T {

View File

@ -7,8 +7,8 @@ import net.minecraft.world.level.levelgen.PositionalRandomFactory
import java.lang.StringBuilder import java.lang.StringBuilder
import java.util.random.RandomGenerator import java.util.random.RandomGenerator
class CMWCRandom(seed: Long = System.nanoTime()) : RandomGenerator, RandomSource { class CMWCRandom(seed: Long = System.nanoTime(), private val stateSize: Int = CMWC_DEFAULT_STATE_SIZE) : RandomGenerator, RandomSource {
private val state = IntArray(CMWC_STATE_SIZE) private val state = IntArray(stateSize)
private var carry = 0 private var carry = 0
private var stateIndex = 0 private var stateIndex = 0
private val gaussian = MarsagliaPolarGaussian(this) private val gaussian = MarsagliaPolarGaussian(this)
@ -37,7 +37,7 @@ class CMWCRandom(seed: Long = System.nanoTime()) : RandomGenerator, RandomSource
} }
override fun nextInt(): Int { override fun nextInt(): Int {
stateIndex = (stateIndex + 1).and(CMWC_STATE_SIZE - 1) stateIndex = (stateIndex + 1) % state.size
val t = 18782L * state[stateIndex] + carry val t = 18782L * state[stateIndex] + carry
carry = t.ushr(32).toInt() carry = t.ushr(32).toInt()
@ -83,24 +83,24 @@ class CMWCRandom(seed: Long = System.nanoTime()) : RandomGenerator, RandomSource
} }
override fun fork(): RandomSource { override fun fork(): RandomSource {
return CMWCRandom(nextLong()) return CMWCRandom(nextLong(), stateSize)
} }
override fun forkPositional(): PositionalRandomFactory { override fun forkPositional(): PositionalRandomFactory {
return Positional(nextLong()) return Positional(nextLong(), stateSize)
} }
class Positional(val seed: Long) : PositionalRandomFactory { class Positional(val seed: Long, val stateSize: Int = CMWC_DEFAULT_STATE_SIZE) : PositionalRandomFactory {
override fun at(x: Int, y: Int, z: Int): RandomSource { override fun at(x: Int, y: Int, z: Int): RandomSource {
return CMWCRandom(Mth.getSeed(x, y, z).xor(seed)) return CMWCRandom(Mth.getSeed(x, y, z).xor(seed), stateSize)
} }
override fun fromHashOf(name: String): RandomSource { override fun fromHashOf(name: String): RandomSource {
return CMWCRandom(name.hashCode().toLong().xor(seed)) return CMWCRandom(name.hashCode().toLong().xor(seed), stateSize)
} }
override fun fromSeed(seed: Long): RandomSource { override fun fromSeed(seed: Long): RandomSource {
return CMWCRandom(seed) return CMWCRandom(seed, stateSize)
} }
override fun parityConfigString(builder: StringBuilder) { override fun parityConfigString(builder: StringBuilder) {
@ -109,7 +109,7 @@ class CMWCRandom(seed: Long = System.nanoTime()) : RandomGenerator, RandomSource
} }
companion object { companion object {
const val CMWC_STATE_SIZE = 256 // 4096 const val CMWC_DEFAULT_STATE_SIZE = 256 // 4096
const val CMWC_CARRY_MAX = 809430660 const val CMWC_CARRY_MAX = 809430660
} }
} }