diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/random/RandomUtils.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/random/RandomUtils.kt index dd3b6f44..2a6a4215 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/random/RandomUtils.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/random/RandomUtils.kt @@ -203,11 +203,31 @@ fun RandomGenerator.nextBytes(length: Int): ByteArray { return data } -fun Collection.random(random: RandomGenerator): T { +// for randomgen compat only +@Deprecated("Delicate API, use only when required for compatibility with original protocol") +fun RandomGenerator.randu64(): ULong { + val a = nextInt().toLong() and 0xFFFFFFFFL + val b = nextInt().toLong() and 0xFFFFFFFFL + return a.toULong().shl(32) or b.toULong() +} + +// for randomgen compat only +@Deprecated("Delicate API, use only when required for compatibility with original protocol") +fun RandomGenerator.nextUInt(max: ULong): ULong { + val denom = ULong.MAX_VALUE / (max + 1UL) + return randu64() / denom +} + +fun Collection.random(random: RandomGenerator, legacyCompatibleSelection: Boolean = false): T { if (isEmpty()) throw NoSuchElementException("List is empty") - return elementAt(random.nextInt(size)) + if (legacyCompatibleSelection) { + // marvelous + return elementAt(random.nextUInt(size.toULong()).toInt()) + } else { + return elementAt(random.nextInt(size)) + } } fun JsonArray.random(random: RandomGenerator): JsonElement {