From 4eccfda92a3b6a9e7b6f0c26e6a795723c52f7e0 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Tue, 24 Sep 2024 11:44:34 -0400 Subject: [PATCH] v2.5.3: fixed RNG in falling loading affecting levelgen --- .../main/java/com/watabou/utils/Random.java | 35 ++++++++++++++++--- .../scenes/InterlevelScene.java | 7 ++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/SPD-classes/src/main/java/com/watabou/utils/Random.java b/SPD-classes/src/main/java/com/watabou/utils/Random.java index e5c7a88cd..27d7227fa 100644 --- a/SPD-classes/src/main/java/com/watabou/utils/Random.java +++ b/SPD-classes/src/main/java/com/watabou/utils/Random.java @@ -75,7 +75,12 @@ public class Random { //returns a uniformly distributed float in the range [0, 1) public static synchronized float Float() { - return generators.peek().nextFloat(); + return Float(true); + } + + public static synchronized float Float( boolean useGeneratorStack ) { + if (useGeneratorStack) return generators.peekFirst().nextFloat(); + else return generators.peekLast().nextFloat(); } //returns a uniformly distributed float in the range [0, max) @@ -95,12 +100,27 @@ public class Random { //returns a uniformly distributed int in the range [-2^31, 2^31) public static synchronized int Int() { - return generators.peek().nextInt(); + return Int(true); + } + + //returns a uniformly distributed int in the range [-2^31, 2^31) + //can either use the current generator in the stack, or force the first generator (pure random) + public static synchronized int Int( boolean useGeneratorStack ) { + if (useGeneratorStack) return generators.peekFirst().nextInt(); + else return generators.peekLast().nextInt(); } //returns a uniformly distributed int in the range [0, max) public static synchronized int Int( int max ) { - return max > 0 ? generators.peek().nextInt(max) : 0; + return Int(max, true); + } + + //returns a uniformly distributed int in the range [0, max) + //can either use the current generator in the stack, or force the first generator (pure random) + public static synchronized int Int( int max, boolean useGeneratorStack ) { + if (max <= 0) return 0; + else if (useGeneratorStack) return generators.peekFirst().nextInt(max); + else return generators.peekLast().nextInt(max); } //returns a uniformly distributed int in the range [min, max) @@ -132,7 +152,14 @@ public class Random { //returns a uniformly distributed long in the range [-2^63, 2^63) public static synchronized long Long() { - return generators.peek().nextLong(); + return Long(true); + } + + //returns a uniformly distributed long in the range [-2^63, 2^63) + //can either use the current generator in the stack, or force the first generator (pure random) + public static synchronized long Long( boolean useGeneratorStack ) { + if (useGeneratorStack) return generators.peekFirst().nextLong(); + else return generators.peekLast().nextLong(); } //returns a mostly uniformly distributed long in the range [0, max) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java index ac3789a83..179d7a8cd 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java @@ -577,8 +577,11 @@ public class InterlevelScene extends PixelScene { if (mode == Mode.FALL) { loadingText.setPos( - (Camera.main.width - loadingText.width() - 4) + Random.NormalFloat(-2, 2), - (Camera.main.height - loadingText.height() - 6) + Random.NormalFloat(-2, 2) + //the randomization is effectively -2 to +2 + // we don't use the generator stack as levelgen may be occurring + // and we don't want to accidentally use a seeded generator + (Camera.main.width - loadingText.width() - 4) + 4*(Random.Float(false)-0.5f), + (Camera.main.height - loadingText.height() - 6) + 4*(Random.Float(false)-0.5f) ); align(loadingText); }