v0.8.1: fixed generator class being rearranged by IDE

This commit is contained in:
Evan Debenham 2020-05-16 17:10:57 -04:00
parent 93d95e33ff
commit 8b442d556c

View File

@ -178,218 +178,6 @@ import java.util.LinkedHashMap;
public class Generator {
private static final String CATEGORY_PROBS = "_probs";
private static final float[][] floorSetTierProbs = new float[][] {
{0, 70, 20, 8, 2},
{0, 25, 50, 20, 5},
{0, 0, 40, 50, 10},
{0, 0, 20, 40, 40},
{0, 0, 0, 20, 80}
};
private static HashMap<Category,Float> categoryProbs = new LinkedHashMap<>();
public static void reset() {
for (Category cat : Category.values()) {
categoryProbs.put( cat, cat.prob );
if (cat.defaultProbs != null) cat.probs = cat.defaultProbs.clone();
}
}
public static void reset(Category cat){
cat.probs = cat.defaultProbs.clone();
}
public static Item random() {
Category cat = Random.chances( categoryProbs );
if (cat == null){
reset();
cat = Random.chances( categoryProbs );
}
categoryProbs.put( cat, categoryProbs.get( cat ) - 1);
return random( cat );
}
public static Item random( Category cat ) {
switch (cat) {
case ARMOR:
return randomArmor();
case WEAPON:
return randomWeapon();
case MISSILE:
return randomMissile();
case ARTIFACT:
Item item = randomArtifact();
//if we're out of artifacts, return a ring instead.
return item != null ? item : random(Category.RING);
default:
int i = Random.chances(cat.probs);
if (i == -1) {
reset(cat);
i = Random.chances(cat.probs);
}
if (cat.defaultProbs != null) cat.probs[i]--;
return ((Item) Reflection.newInstance(cat.classes[i])).random();
}
}
//overrides any deck systems and always uses default probs
public static Item randomUsingDefaults( Category cat ){
if (cat.defaultProbs == null) {
return random(cat); //currently covers weapons/armor/missiles
} else {
return ((Item) Reflection.newInstance(cat.classes[Random.chances(cat.defaultProbs)])).random();
}
}
public static Item random( Class<? extends Item> cl ) {
return Reflection.newInstance(cl).random();
}
public static Armor randomArmor(){
return randomArmor(Dungeon.depth / 5);
}
public static Armor randomArmor(int floorSet) {
floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
Armor a = (Armor)Reflection.newInstance(Category.ARMOR.classes[Random.chances(floorSetTierProbs[floorSet])]);
a.random();
return a;
}
public static final Category[] wepTiers = new Category[]{
Category.WEP_T1,
Category.WEP_T2,
Category.WEP_T3,
Category.WEP_T4,
Category.WEP_T5
};
public static MeleeWeapon randomWeapon(){
return randomWeapon(Dungeon.depth / 5);
}
public static MeleeWeapon randomWeapon(int floorSet) {
floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
Category c = wepTiers[Random.chances(floorSetTierProbs[floorSet])];
MeleeWeapon w = (MeleeWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
w.random();
return w;
}
public static final Category[] misTiers = new Category[]{
Category.MIS_T1,
Category.MIS_T2,
Category.MIS_T3,
Category.MIS_T4,
Category.MIS_T5
};
public static MissileWeapon randomMissile(){
return randomMissile(Dungeon.depth / 5);
}
public static MissileWeapon randomMissile(int floorSet) {
floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
Category c = misTiers[Random.chances(floorSetTierProbs[floorSet])];
MissileWeapon w = (MissileWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
w.random();
return w;
}
//enforces uniqueness of artifacts throughout a run.
public static Artifact randomArtifact() {
Category cat = Category.ARTIFACT;
int i = Random.chances( cat.probs );
//if no artifacts are left, return null
if (i == -1){
return null;
}
cat.probs[i]--;
return (Artifact) Reflection.newInstance((Class<? extends Artifact>) cat.classes[i]).random();
}
public static boolean removeArtifact(Class<?extends Artifact> artifact) {
Category cat = Category.ARTIFACT;
for (int i = 0; i < cat.classes.length; i++){
if (cat.classes[i].equals(artifact)) {
cat.probs[i] = 0;
return true;
}
}
return false;
}
private static final String GENERAL_PROBS = "general_probs";
public static void storeInBundle(Bundle bundle) {
Float[] genProbs = categoryProbs.values().toArray(new Float[0]);
float[] storeProbs = new float[genProbs.length];
for (int i = 0; i < storeProbs.length; i++){
storeProbs[i] = genProbs[i];
}
bundle.put( GENERAL_PROBS, storeProbs);
for (Category cat : Category.values()){
if (cat.defaultProbs == null) continue;
boolean needsStore = false;
for (int i = 0; i < cat.probs.length; i++){
if (cat.probs[i] != cat.defaultProbs[i]){
needsStore = true;
break;
}
}
if (needsStore){
bundle.put(cat.name().toLowerCase() + CATEGORY_PROBS, cat.probs);
}
}
}
public static void restoreFromBundle(Bundle bundle) {
reset();
if (bundle.contains(GENERAL_PROBS)){
float[] probs = bundle.getFloatArray(GENERAL_PROBS);
for (int i = 0; i < probs.length; i++){
categoryProbs.put(Category.values()[i], probs[i]);
}
}
for (Category cat : Category.values()){
if (bundle.contains(cat.name().toLowerCase() + CATEGORY_PROBS)){
float[] probs = bundle.getFloatArray(cat.name().toLowerCase() + CATEGORY_PROBS);
if (cat.defaultProbs != null && probs.length == cat.defaultProbs.length){
cat.probs = probs;
}
}
}
//pre-0.8.1
if (bundle.contains("spawned_artifacts")) {
for (Class<? extends Artifact> artifact : bundle.getClassArray("spawned_artifacts")) {
Category cat = Category.ARTIFACT;
for (int i = 0; i < cat.classes.length; i++) {
if (cat.classes[i].equals(artifact)) {
cat.probs[i] = 0;
}
}
}
}
}
public enum Category {
WEAPON ( 6, MeleeWeapon.class),
WEP_T1 ( 0, MeleeWeapon.class),
@ -423,6 +211,31 @@ public class Generator {
public Class<?>[] classes;
//some item types use a deck-based system, where the probs decrement as items are picked
// until they are all 0, and then they reset. Those generator classes should define
// defaultProbs. If defaultProbs is null then a deck system isn't used.
//Artifacts in particular don't reset, no duplicates!
public float[] probs;
public float[] defaultProbs = null;
public float prob;
public Class<? extends Item> superClass;
private Category( float prob, Class<? extends Item> superClass ) {
this.prob = prob;
this.superClass = superClass;
}
public static int order( Item item ) {
for (int i=0; i < values().length; i++) {
if (values()[i].superClass.isInstance( item )) {
return i;
}
}
return item instanceof Bag ? Integer.MAX_VALUE : Integer.MAX_VALUE - 1;
}
static {
GOLD.classes = new Class<?>[]{
Gold.class };
@ -648,30 +461,216 @@ public class Generator {
ARTIFACT.defaultProbs = new float[]{ 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1};
ARTIFACT.probs = ARTIFACT.defaultProbs.clone();
}
}
//some item types use a deck-based system, where the probs decrement as items are picked
// until they are all 0, and then they reset. Those generator classes should define
// defaultProbs. If defaultProbs is null then a deck system isn't used.
//Artifacts in particular don't reset, no duplicates!
public float[] probs;
private static final float[][] floorSetTierProbs = new float[][] {
{0, 70, 20, 8, 2},
{0, 25, 50, 20, 5},
{0, 0, 40, 50, 10},
{0, 0, 20, 40, 40},
{0, 0, 0, 20, 80}
};
public float prob;
public Class<? extends Item> superClass;
private static HashMap<Category,Float> categoryProbs = new LinkedHashMap<>();
private Category( float prob, Class<? extends Item> superClass ) {
this.prob = prob;
this.superClass = superClass;
public static void reset() {
for (Category cat : Category.values()) {
categoryProbs.put( cat, cat.prob );
if (cat.defaultProbs != null) cat.probs = cat.defaultProbs.clone();
}
}
public static void reset(Category cat){
cat.probs = cat.defaultProbs.clone();
}
public static Item random() {
Category cat = Random.chances( categoryProbs );
if (cat == null){
reset();
cat = Random.chances( categoryProbs );
}
categoryProbs.put( cat, categoryProbs.get( cat ) - 1);
return random( cat );
}
public static Item random( Category cat ) {
switch (cat) {
case ARMOR:
return randomArmor();
case WEAPON:
return randomWeapon();
case MISSILE:
return randomMissile();
case ARTIFACT:
Item item = randomArtifact();
//if we're out of artifacts, return a ring instead.
return item != null ? item : random(Category.RING);
default:
int i = Random.chances(cat.probs);
if (i == -1) {
reset(cat);
i = Random.chances(cat.probs);
}
if (cat.defaultProbs != null) cat.probs[i]--;
return ((Item) Reflection.newInstance(cat.classes[i])).random();
}
}
//overrides any deck systems and always uses default probs
public static Item randomUsingDefaults( Category cat ){
if (cat.defaultProbs == null) {
return random(cat); //currently covers weapons/armor/missiles
} else {
return ((Item) Reflection.newInstance(cat.classes[Random.chances(cat.defaultProbs)])).random();
}
}
public static Item random( Class<? extends Item> cl ) {
return Reflection.newInstance(cl).random();
}
public static Armor randomArmor(){
return randomArmor(Dungeon.depth / 5);
}
public static Armor randomArmor(int floorSet) {
floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
Armor a = (Armor)Reflection.newInstance(Category.ARMOR.classes[Random.chances(floorSetTierProbs[floorSet])]);
a.random();
return a;
}
public static final Category[] wepTiers = new Category[]{
Category.WEP_T1,
Category.WEP_T2,
Category.WEP_T3,
Category.WEP_T4,
Category.WEP_T5
};
public static MeleeWeapon randomWeapon(){
return randomWeapon(Dungeon.depth / 5);
}
public static MeleeWeapon randomWeapon(int floorSet) {
floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
Category c = wepTiers[Random.chances(floorSetTierProbs[floorSet])];
MeleeWeapon w = (MeleeWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
w.random();
return w;
}
public static final Category[] misTiers = new Category[]{
Category.MIS_T1,
Category.MIS_T2,
Category.MIS_T3,
Category.MIS_T4,
Category.MIS_T5
};
public static MissileWeapon randomMissile(){
return randomMissile(Dungeon.depth / 5);
}
public static MissileWeapon randomMissile(int floorSet) {
floorSet = (int)GameMath.gate(0, floorSet, floorSetTierProbs.length-1);
Category c = misTiers[Random.chances(floorSetTierProbs[floorSet])];
MissileWeapon w = (MissileWeapon)Reflection.newInstance(c.classes[Random.chances(c.probs)]);
w.random();
return w;
}
//enforces uniqueness of artifacts throughout a run.
public static Artifact randomArtifact() {
Category cat = Category.ARTIFACT;
int i = Random.chances( cat.probs );
//if no artifacts are left, return null
if (i == -1){
return null;
}
public static int order( Item item ) {
for (int i=0; i < values().length; i++) {
if (values()[i].superClass.isInstance( item )) {
return i;
cat.probs[i]--;
return (Artifact) Reflection.newInstance((Class<? extends Artifact>) cat.classes[i]).random();
}
public static boolean removeArtifact(Class<?extends Artifact> artifact) {
Category cat = Category.ARTIFACT;
for (int i = 0; i < cat.classes.length; i++){
if (cat.classes[i].equals(artifact)) {
cat.probs[i] = 0;
return true;
}
}
return false;
}
private static final String GENERAL_PROBS = "general_probs";
private static final String CATEGORY_PROBS = "_probs";
public static void storeInBundle(Bundle bundle) {
Float[] genProbs = categoryProbs.values().toArray(new Float[0]);
float[] storeProbs = new float[genProbs.length];
for (int i = 0; i < storeProbs.length; i++){
storeProbs[i] = genProbs[i];
}
bundle.put( GENERAL_PROBS, storeProbs);
for (Category cat : Category.values()){
if (cat.defaultProbs == null) continue;
boolean needsStore = false;
for (int i = 0; i < cat.probs.length; i++){
if (cat.probs[i] != cat.defaultProbs[i]){
needsStore = true;
break;
}
}
return item instanceof Bag ? Integer.MAX_VALUE : Integer.MAX_VALUE - 1;
if (needsStore){
bundle.put(cat.name().toLowerCase() + CATEGORY_PROBS, cat.probs);
}
}
public float[] defaultProbs = null;
}
public static void restoreFromBundle(Bundle bundle) {
reset();
if (bundle.contains(GENERAL_PROBS)){
float[] probs = bundle.getFloatArray(GENERAL_PROBS);
for (int i = 0; i < probs.length; i++){
categoryProbs.put(Category.values()[i], probs[i]);
}
}
for (Category cat : Category.values()){
if (bundle.contains(cat.name().toLowerCase() + CATEGORY_PROBS)){
float[] probs = bundle.getFloatArray(cat.name().toLowerCase() + CATEGORY_PROBS);
if (cat.defaultProbs != null && probs.length == cat.defaultProbs.length){
cat.probs = probs;
}
}
}
//pre-0.8.1
if (bundle.contains("spawned_artifacts")) {
for (Class<? extends Artifact> artifact : bundle.getClassArray("spawned_artifacts")) {
Category cat = Category.ARTIFACT;
for (int i = 0; i < cat.classes.length; i++) {
if (cat.classes[i].equals(artifact)) {
cat.probs[i] = 0;
}
}
}
}
}
}