v0.9.1: improved code for stone of intuition and item handlers, stone now also works on rings!

This commit is contained in:
Evan Debenham 2020-10-20 18:46:22 -04:00
parent 2496dc0d1c
commit 38497c6343
7 changed files with 52 additions and 101 deletions

View File

@ -1118,9 +1118,9 @@ items.stones.stoneofflock.desc=This runestone summons magical sheep for a short
items.stones.stoneofintuition.name=stone of intuition items.stones.stoneofintuition.name=stone of intuition
items.stones.stoneofintuition.inv_title=Select an item items.stones.stoneofintuition.inv_title=Select an item
items.stones.stoneofintuition.desc=This runestone holds a weaker version of the magic found in scrolls of identification. Rather than directly identifying an item, it will work on your intuition, allowing you to attempt to identify a potion or scroll by guessing. items.stones.stoneofintuition.desc=This runestone holds a weaker version of the magic found in scrolls of identification. Rather than directly identifying an item, it will work on your intuition, allowing you to attempt to identify the type of a potion, scroll, or ring by guessing.
items.stones.stoneofintuition$wndguess.text=Guess what the unidentified item is. If you guess correctly, it will be identified! items.stones.stoneofintuition$wndguess.text=Guess what the type of the unidentified item is. If you guess correctly, the type will be identified!
items.stones.stoneofintuition$wndguess.correct=Correct. The item has been identified! items.stones.stoneofintuition$wndguess.correct=Correct. The item's type has been identified!
items.stones.stoneofintuition$wndguess.incorrect=Your guess was incorrect. items.stones.stoneofintuition$wndguess.incorrect=Your guess was incorrect.
items.stones.stoneofshock.name=stone of shock items.stones.stoneofshock.name=stone of shock

View File

@ -28,22 +28,24 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
public class ItemStatusHandler<T extends Item> { public class ItemStatusHandler<T extends Item> {
private Class<? extends T>[] items; private Class<? extends T>[] items;
private HashMap<Class<? extends T>, String> itemLabels; private LinkedHashMap<Class<? extends T>, String> itemLabels;
private HashMap<String, Integer> labelImages; private LinkedHashMap<String, Integer> labelImages;
private HashSet<Class<? extends T>> known; private LinkedHashSet<Class<? extends T>> known;
public ItemStatusHandler( Class<? extends T>[] items, HashMap<String, Integer> labelImages ) { public ItemStatusHandler( Class<? extends T>[] items, HashMap<String, Integer> labelImages ) {
this.items = items; this.items = items;
this.itemLabels = new HashMap<>(); this.itemLabels = new LinkedHashMap<>();
this.labelImages = new HashMap<>(labelImages); this.labelImages = new LinkedHashMap<>(labelImages);
known = new HashSet<>(); known = new LinkedHashSet<>();
ArrayList<String> labelsLeft = new ArrayList<>(labelImages.keySet()); ArrayList<String> labelsLeft = new ArrayList<>(labelImages.keySet());
@ -63,9 +65,9 @@ public class ItemStatusHandler<T extends Item> {
this.items = items; this.items = items;
this.itemLabels = new HashMap<>(); this.itemLabels = new LinkedHashMap<>();
this.labelImages = new HashMap<>(labelImages); this.labelImages = new LinkedHashMap<>(labelImages);
known = new HashSet<>(); known = new LinkedHashSet<>();
ArrayList<String> allLabels = new ArrayList<>(labelImages.keySet()); ArrayList<String> allLabels = new ArrayList<>(labelImages.keySet());
@ -203,7 +205,7 @@ public class ItemStatusHandler<T extends Item> {
} }
public HashSet<Class<? extends T>> unknown() { public HashSet<Class<? extends T>> unknown() {
HashSet<Class<? extends T>> result = new HashSet<>(); LinkedHashSet<Class<? extends T>> result = new LinkedHashSet<>();
for (Class<? extends T> i : items) { for (Class<? extends T> i : items) {
if (!known.contains( i )) { if (!known.contains( i )) {
result.add( i ); result.add( i );

View File

@ -90,21 +90,6 @@ public class Potion extends Item {
private static final float TIME_TO_DRINK = 1f; private static final float TIME_TO_DRINK = 1f;
private static final Class<?>[] potions = {
PotionOfHealing.class,
PotionOfExperience.class,
PotionOfToxicGas.class,
PotionOfLiquidFlame.class,
PotionOfStrength.class,
PotionOfParalyticGas.class,
PotionOfLevitation.class,
PotionOfMindVision.class,
PotionOfPurity.class,
PotionOfInvisibility.class,
PotionOfHaste.class,
PotionOfFrost.class
};
private static final HashMap<String, Integer> colors = new HashMap<String, Integer>() { private static final HashMap<String, Integer> colors = new HashMap<String, Integer>() {
{ {
put("crimson",ItemSpriteSheet.POTION_CRIMSON); put("crimson",ItemSpriteSheet.POTION_CRIMSON);
@ -163,7 +148,7 @@ public class Potion extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void initColors() { public static void initColors() {
handler = new ItemStatusHandler<>( (Class<? extends Potion>[])potions, colors ); handler = new ItemStatusHandler<>( (Class<? extends Potion>[])Generator.Category.POTION.classes, colors );
} }
public static void save( Bundle bundle ) { public static void save( Bundle bundle ) {
@ -188,7 +173,7 @@ public class Potion extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void restore( Bundle bundle ) { public static void restore( Bundle bundle ) {
handler = new ItemStatusHandler<>( (Class<? extends Potion>[])potions, colors, bundle ); handler = new ItemStatusHandler<>( (Class<? extends Potion>[])Generator.Category.POTION.classes, colors, bundle );
} }
public Potion() { public Potion() {
@ -412,7 +397,7 @@ public class Potion extends Item {
} }
public static boolean allKnown() { public static boolean allKnown() {
return handler.known().size() == potions.length; return handler.known().size() == Generator.Category.POTION.classes.length;
} }
protected int splashColor(){ protected int splashColor(){

View File

@ -27,6 +27,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.Buff; import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.Buff;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Talent; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Talent;
import com.shatteredpixel.shatteredpixeldungeon.items.Generator;
import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.items.ItemStatusHandler; import com.shatteredpixel.shatteredpixeldungeon.items.ItemStatusHandler;
import com.shatteredpixel.shatteredpixeldungeon.items.KindofMisc; import com.shatteredpixel.shatteredpixeldungeon.items.KindofMisc;
@ -45,20 +46,6 @@ public class Ring extends KindofMisc {
protected Buff buff; protected Buff buff;
private static final Class<?>[] rings = {
RingOfAccuracy.class,
RingOfEvasion.class,
RingOfElements.class,
RingOfForce.class,
RingOfFuror.class,
RingOfHaste.class,
RingOfEnergy.class,
RingOfMight.class,
RingOfSharpshooting.class,
RingOfTenacity.class,
RingOfWealth.class,
};
private static final HashMap<String, Integer> gems = new HashMap<String, Integer>() { private static final HashMap<String, Integer> gems = new HashMap<String, Integer>() {
{ {
put("garnet",ItemSpriteSheet.RING_GARNET); put("garnet",ItemSpriteSheet.RING_GARNET);
@ -85,7 +72,7 @@ public class Ring extends KindofMisc {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void initGems() { public static void initGems() {
handler = new ItemStatusHandler<>( (Class<? extends Ring>[])rings, gems ); handler = new ItemStatusHandler<>( (Class<? extends Ring>[])Generator.Category.RING.classes, gems );
} }
public static void save( Bundle bundle ) { public static void save( Bundle bundle ) {
@ -98,7 +85,7 @@ public class Ring extends KindofMisc {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void restore( Bundle bundle ) { public static void restore( Bundle bundle ) {
handler = new ItemStatusHandler<>( (Class<? extends Ring>[])rings, gems, bundle ); handler = new ItemStatusHandler<>( (Class<? extends Ring>[])Generator.Category.RING.classes, gems, bundle );
} }
public Ring() { public Ring() {
@ -247,7 +234,7 @@ public class Ring extends KindofMisc {
} }
public static boolean allKnown() { public static boolean allKnown() {
return handler.known().size() == rings.length - 2; return handler.known().size() == Generator.Category.RING.classes.length;
} }
@Override @Override

View File

@ -29,6 +29,7 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.Invisibility;
import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.MagicImmune; import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.MagicImmune;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Talent; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Talent;
import com.shatteredpixel.shatteredpixeldungeon.items.Generator;
import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.items.ItemStatusHandler; import com.shatteredpixel.shatteredpixeldungeon.items.ItemStatusHandler;
import com.shatteredpixel.shatteredpixeldungeon.items.Recipe; import com.shatteredpixel.shatteredpixeldungeon.items.Recipe;
@ -66,21 +67,6 @@ public abstract class Scroll extends Item {
protected static final float TIME_TO_READ = 1f; protected static final float TIME_TO_READ = 1f;
private static final Class<?>[] scrolls = {
ScrollOfIdentify.class,
ScrollOfMagicMapping.class,
ScrollOfRecharging.class,
ScrollOfRemoveCurse.class,
ScrollOfTeleportation.class,
ScrollOfUpgrade.class,
ScrollOfRage.class,
ScrollOfTerror.class,
ScrollOfLullaby.class,
ScrollOfTransmutation.class,
ScrollOfRetribution.class,
ScrollOfMirrorImage.class
};
private static final HashMap<String, Integer> runes = new HashMap<String, Integer>() { private static final HashMap<String, Integer> runes = new HashMap<String, Integer>() {
{ {
put("KAUNAN",ItemSpriteSheet.SCROLL_KAUNAN); put("KAUNAN",ItemSpriteSheet.SCROLL_KAUNAN);
@ -109,7 +95,7 @@ public abstract class Scroll extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void initLabels() { public static void initLabels() {
handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])scrolls, runes ); handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])Generator.Category.SCROLL.classes, runes );
} }
public static void save( Bundle bundle ) { public static void save( Bundle bundle ) {
@ -134,7 +120,7 @@ public abstract class Scroll extends Item {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void restore( Bundle bundle ) { public static void restore( Bundle bundle ) {
handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])scrolls, runes, bundle ); handler = new ItemStatusHandler<>( (Class<? extends Scroll>[])Generator.Category.SCROLL.classes, runes, bundle );
} }
public Scroll() { public Scroll() {
@ -264,7 +250,7 @@ public abstract class Scroll extends Item {
} }
public static boolean allKnown() { public static boolean allKnown() {
return handler.known().size() == scrolls.length; return handler.known().size() == Generator.Category.SCROLL.classes.length;
} }
@Override @Override

View File

@ -23,10 +23,10 @@ package com.shatteredpixel.shatteredpixeldungeon.items.stones;
import com.shatteredpixel.shatteredpixeldungeon.Assets; import com.shatteredpixel.shatteredpixeldungeon.Assets;
import com.shatteredpixel.shatteredpixeldungeon.effects.Identification; import com.shatteredpixel.shatteredpixeldungeon.effects.Identification;
import com.shatteredpixel.shatteredpixeldungeon.items.Generator;
import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion;
import com.shatteredpixel.shatteredpixeldungeon.items.potions.exotic.ExoticPotion; import com.shatteredpixel.shatteredpixeldungeon.items.potions.exotic.ExoticPotion;
import com.shatteredpixel.shatteredpixeldungeon.items.rings.Ring;
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll;
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.exotic.ExoticScroll; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.exotic.ExoticScroll;
import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages;
@ -44,7 +44,7 @@ import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag;
import com.watabou.noosa.Image; import com.watabou.noosa.Image;
import com.watabou.utils.Reflection; import com.watabou.utils.Reflection;
import java.util.HashSet; import java.util.ArrayList;
public class StoneOfIntuition extends InventoryStone { public class StoneOfIntuition extends InventoryStone {
@ -88,7 +88,11 @@ public class StoneOfIntuition extends InventoryStone {
super.onClick(); super.onClick();
useAnimation(); useAnimation();
if (item.getClass() == curGuess){ if (item.getClass() == curGuess){
if (item instanceof Ring){
((Ring) item).setKnown();
} else {
item.identify(); item.identify();
}
GLog.p( Messages.get(WndGuess.class, "correct") ); GLog.p( Messages.get(WndGuess.class, "correct") );
curUser.sprite.parent.add( new Identification( curUser.sprite.center().offset( 0, -16 ) ) ); curUser.sprite.parent.add( new Identification( curUser.sprite.center().offset( 0, -16 ) ) );
} else { } else {
@ -109,44 +113,34 @@ public class StoneOfIntuition extends InventoryStone {
int rows; int rows;
int placed = 0; int placed = 0;
HashSet<Class<?extends Item>> unIDed = new HashSet<>(); final ArrayList<Class<?extends Item>> unIDed = new ArrayList<>();
final Class<?extends Item>[] all;
if (item.isIdentified()){ if (item.isIdentified()){
hide(); hide();
return; return;
} else if (item instanceof Potion){ } else if (item instanceof Potion){
if (item instanceof ExoticPotion) {
for (Class<?extends Item> i : Potion.getUnknown()){
unIDed.add(ExoticPotion.regToExo.get(i));
}
} else {
unIDed.addAll(Potion.getUnknown()); unIDed.addAll(Potion.getUnknown());
all = (Class<? extends Item>[]) Generator.Category.POTION.classes.clone();
if (item instanceof ExoticPotion){
for (int i = 0; i < all.length; i++){
all[i] = ExoticPotion.regToExo.get(all[i]);
}
HashSet<Class<?extends Item>> exoUID = new HashSet<>();
for (Class<?extends Item> i : unIDed){
exoUID.add(ExoticPotion.regToExo.get(i));
}
unIDed = exoUID;
} }
} else if (item instanceof Scroll){ } else if (item instanceof Scroll){
unIDed.addAll(Scroll.getUnknown());
all = (Class<? extends Item>[]) Generator.Category.SCROLL.classes.clone();
if (item instanceof ExoticScroll) { if (item instanceof ExoticScroll) {
for (int i = 0; i < all.length; i++) { for (Class<?extends Item> i : Scroll.getUnknown()){
all[i] = ExoticScroll.regToExo.get(all[i]); unIDed.add(ExoticScroll.regToExo.get(i));
} }
HashSet<Class<? extends Item>> exoUID = new HashSet<>(); } else {
for (Class<? extends Item> i : unIDed) { unIDed.addAll(Scroll.getUnknown());
exoUID.add(ExoticScroll.regToExo.get(i));
}
unIDed = exoUID;
} }
} else if (item instanceof Ring) {
unIDed.addAll(Ring.getUnknown());
} else { } else {
hide(); hide();
return; return;
} }
if (unIDed.size() < 6){ if (unIDed.size() <= 5){
rows = 1; rows = 1;
top += BTN_SIZE/2f; top += BTN_SIZE/2f;
left = (WIDTH - BTN_SIZE*unIDed.size())/2f; left = (WIDTH - BTN_SIZE*unIDed.size())/2f;
@ -155,16 +149,12 @@ public class StoneOfIntuition extends InventoryStone {
left = (WIDTH - BTN_SIZE*((unIDed.size()+1)/2))/2f; left = (WIDTH - BTN_SIZE*((unIDed.size()+1)/2))/2f;
} }
for (int i = 0; i < all.length; i++){ for (final Class<?extends Item> i : unIDed){
if (!unIDed.contains(all[i])) {
continue;
}
final int j = i;
IconButton btn = new IconButton(){ IconButton btn = new IconButton(){
@Override @Override
protected void onClick() { protected void onClick() {
curGuess = all[j]; curGuess = i;
guess.visible = true; guess.visible = true;
guess.text( Messages.titleCase(Messages.get(curGuess, "name")) ); guess.text( Messages.titleCase(Messages.get(curGuess, "name")) );
guess.enable(true); guess.enable(true);
@ -172,7 +162,7 @@ public class StoneOfIntuition extends InventoryStone {
} }
}; };
Image im = new Image(Assets.Sprites.ITEM_ICONS); Image im = new Image(Assets.Sprites.ITEM_ICONS);
im.frame(ItemSpriteSheet.Icons.film.get(Reflection.newInstance(all[i]).icon)); im.frame(ItemSpriteSheet.Icons.film.get(Reflection.newInstance(i).icon));
im.scale.set(2f); im.scale.set(2f);
btn.icon(im); btn.icon(im);
btn.setRect(left + placed*BTN_SIZE, top, BTN_SIZE, BTN_SIZE); btn.setRect(left + placed*BTN_SIZE, top, BTN_SIZE, BTN_SIZE);

View File

@ -40,6 +40,7 @@ import com.shatteredpixel.shatteredpixeldungeon.items.bags.ScrollHolder;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.VelvetPouch; import com.shatteredpixel.shatteredpixeldungeon.items.bags.VelvetPouch;
import com.shatteredpixel.shatteredpixeldungeon.items.food.Food; import com.shatteredpixel.shatteredpixeldungeon.items.food.Food;
import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion;
import com.shatteredpixel.shatteredpixeldungeon.items.rings.Ring;
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll;
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfRemoveCurse; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfRemoveCurse;
import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfTransmutation; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.ScrollOfTransmutation;
@ -443,7 +444,7 @@ public class WndBag extends WndTabbed {
mode == Mode.FOOD && (item instanceof Food) || mode == Mode.FOOD && (item instanceof Food) ||
mode == Mode.POTION && (item instanceof Potion) || mode == Mode.POTION && (item instanceof Potion) ||
mode == Mode.SCROLL && (item instanceof Scroll) || mode == Mode.SCROLL && (item instanceof Scroll) ||
mode == Mode.UNIDED_POTION_OR_SCROLL && (!item.isIdentified() && (item instanceof Scroll || item instanceof Potion)) || mode == Mode.UNIDED_POTION_OR_SCROLL && (!item.isIdentified() && (item instanceof Scroll || item instanceof Potion || item instanceof Ring)) ||
mode == Mode.EQUIPMENT && (item instanceof EquipableItem || item instanceof Wand) || mode == Mode.EQUIPMENT && (item instanceof EquipableItem || item instanceof Wand) ||
mode == Mode.ALCHEMY && Recipe.usableInRecipe(item) || mode == Mode.ALCHEMY && Recipe.usableInRecipe(item) ||
mode == Mode.TRANMSUTABLE && ScrollOfTransmutation.canTransmute(item) || mode == Mode.TRANMSUTABLE && ScrollOfTransmutation.canTransmute(item) ||