v0.8.0: adjusted Random to be more flexible, now uses a generator stack
This commit is contained in:
parent
438ce6f0a9
commit
fd38cfc155
|
@ -21,6 +21,9 @@
|
||||||
|
|
||||||
package com.watabou.utils;
|
package com.watabou.utils;
|
||||||
|
|
||||||
|
import com.watabou.noosa.Game;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -28,19 +31,38 @@ import java.util.List;
|
||||||
|
|
||||||
public class Random {
|
public class Random {
|
||||||
|
|
||||||
private static java.util.Random rand = new java.util.Random();
|
//we store a stack of random number generators, which may be seeded deliberately or randomly.
|
||||||
|
//top of the stack is what is currently being used to generate new numbers.
|
||||||
public static void seed( ){
|
//the base generator is always created with no seed, and cannot be popped.
|
||||||
rand = new java.util.Random();
|
private static ArrayDeque<java.util.Random> generators;
|
||||||
|
static {
|
||||||
|
resetGenerators();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void seed( long seed ){
|
public static void resetGenerators(){
|
||||||
rand.setSeed(seed);
|
generators = new ArrayDeque<>();
|
||||||
|
generators.push(new java.util.Random());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void pushGenerator(){
|
||||||
|
generators.push( new java.util.Random() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void pushGenerator( long seed ){
|
||||||
|
generators.push( new java.util.Random( seed ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void popGenerator(){
|
||||||
|
if (generators.size() == 1){
|
||||||
|
Game.reportException( new RuntimeException("tried to pop the last random number generator!"));
|
||||||
|
} else {
|
||||||
|
generators.pop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns a uniformly distributed float in the range [0, 1)
|
//returns a uniformly distributed float in the range [0, 1)
|
||||||
public static float Float() {
|
public static float Float() {
|
||||||
return rand.nextFloat();
|
return generators.peek().nextFloat();
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns a uniformly distributed float in the range [0, max)
|
//returns a uniformly distributed float in the range [0, max)
|
||||||
|
@ -60,7 +82,7 @@ public class Random {
|
||||||
|
|
||||||
//returns a uniformly distributed int in the range [0, max)
|
//returns a uniformly distributed int in the range [0, max)
|
||||||
public static int Int( int max ) {
|
public static int Int( int max ) {
|
||||||
return max > 0 ? rand.nextInt(max) : 0;
|
return max > 0 ? generators.peek().nextInt(max) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns a uniformly distributed int in the range [min, max)
|
//returns a uniformly distributed int in the range [min, max)
|
||||||
|
@ -80,7 +102,7 @@ public class Random {
|
||||||
|
|
||||||
//returns a uniformly distributed long in the range [-2^63, 2^63)
|
//returns a uniformly distributed long in the range [-2^63, 2^63)
|
||||||
public static long Long() {
|
public static long Long() {
|
||||||
return rand.nextLong();
|
return generators.peek().nextLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
//returns a uniformly distributed long in the range [0, max)
|
//returns a uniformly distributed long in the range [0, max)
|
||||||
|
@ -169,7 +191,7 @@ public class Random {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static<T> void shuffle( List<?extends T> list){
|
public static<T> void shuffle( List<?extends T> list){
|
||||||
Collections.shuffle(list, rand);
|
Collections.shuffle(list, generators.peek());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static<T> void shuffle( T[] array ) {
|
public static<T> void shuffle( T[] array ) {
|
||||||
|
|
|
@ -173,7 +173,7 @@ public class Dungeon {
|
||||||
Actor.clear();
|
Actor.clear();
|
||||||
Actor.resetNextID();
|
Actor.resetNextID();
|
||||||
|
|
||||||
Random.seed( seed );
|
Random.pushGenerator( seed );
|
||||||
|
|
||||||
Scroll.initLabels();
|
Scroll.initLabels();
|
||||||
Potion.initColors();
|
Potion.initColors();
|
||||||
|
@ -182,7 +182,7 @@ public class Dungeon {
|
||||||
SpecialRoom.initForRun();
|
SpecialRoom.initForRun();
|
||||||
SecretRoom.initForRun();
|
SecretRoom.initForRun();
|
||||||
|
|
||||||
Random.seed();
|
Random.resetGenerators();
|
||||||
|
|
||||||
Statistics.reset();
|
Statistics.reset();
|
||||||
Notes.reset();
|
Notes.reset();
|
||||||
|
@ -313,11 +313,14 @@ public class Dungeon {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long seedForDepth(int depth){
|
public static long seedForDepth(int depth){
|
||||||
Random.seed( seed );
|
Random.pushGenerator( seed );
|
||||||
for (int i = 0; i < depth; i ++)
|
|
||||||
|
for (int i = 0; i < depth; i ++) {
|
||||||
Random.Long(); //we don't care about these values, just need to go through them
|
Random.Long(); //we don't care about these values, just need to go through them
|
||||||
|
}
|
||||||
long result = Random.Long();
|
long result = Random.Long();
|
||||||
Random.seed();
|
|
||||||
|
Random.popGenerator();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,7 +169,7 @@ public abstract class Level implements Bundlable {
|
||||||
|
|
||||||
public void create() {
|
public void create() {
|
||||||
|
|
||||||
Random.seed( Dungeon.seedCurDepth() );
|
Random.pushGenerator( Dungeon.seedCurDepth() );
|
||||||
|
|
||||||
if (!(Dungeon.bossLevel() || Dungeon.depth == 21) /*final shop floor*/) {
|
if (!(Dungeon.bossLevel() || Dungeon.depth == 21) /*final shop floor*/) {
|
||||||
|
|
||||||
|
@ -261,7 +261,7 @@ public abstract class Level implements Bundlable {
|
||||||
createMobs();
|
createMobs();
|
||||||
createItems();
|
createItems();
|
||||||
|
|
||||||
Random.seed();
|
Random.popGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSize(int w, int h){
|
public void setSize(int w, int h){
|
||||||
|
|
|
@ -392,11 +392,14 @@ public class DungeonTileSheet {
|
||||||
public static byte[] tileVariance;
|
public static byte[] tileVariance;
|
||||||
|
|
||||||
public static void setupVariance(int size, long seed){
|
public static void setupVariance(int size, long seed){
|
||||||
Random.seed( seed );
|
Random.pushGenerator( seed );
|
||||||
|
|
||||||
tileVariance = new byte[size];
|
tileVariance = new byte[size];
|
||||||
for (int i = 0; i < tileVariance.length; i++)
|
for (int i = 0; i < tileVariance.length; i++) {
|
||||||
tileVariance[i] = (byte) Random.Int(100);
|
tileVariance[i] = (byte) Random.Int(100);
|
||||||
Random.seed();
|
}
|
||||||
|
|
||||||
|
Random.popGenerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
//These alt visuals will trigger 50% of the time (45% of the time if a rare alt is also present)
|
//These alt visuals will trigger 50% of the time (45% of the time if a rare alt is also present)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user