diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java index 609adb1ea..363645306 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Hero.java @@ -1545,33 +1545,46 @@ public class Hero extends Char { int doorCell = ((HeroAction.Unlock)curAction).dst; int door = Dungeon.level.map[doorCell]; - - if (door == Terrain.LOCKED_DOOR){ - Notes.remove(new IronKey(Dungeon.depth)); - Level.set( doorCell, Terrain.DOOR ); - } else { - Notes.remove(new SkeletonKey(Dungeon.depth)); - Level.set( doorCell, Terrain.UNLOCKED_EXIT ); - } - GameScene.updateKeyDisplay(); - Level.set( doorCell, door == Terrain.LOCKED_DOOR ? Terrain.DOOR : Terrain.UNLOCKED_EXIT ); - GameScene.updateMap( doorCell ); - spend( Key.TIME_TO_UNLOCK ); + if (Dungeon.level.adjacent(pos, doorCell)) { + boolean hasKey = true; + if (door == Terrain.LOCKED_DOOR) { + hasKey = Notes.remove(new IronKey(Dungeon.depth)); + if (hasKey) Level.set(doorCell, Terrain.DOOR); + } else { + hasKey = Notes.remove(new SkeletonKey(Dungeon.depth)); + if (hasKey) Level.set(doorCell, Terrain.UNLOCKED_EXIT); + } + + if (hasKey) { + GameScene.updateKeyDisplay(); + Level.set(doorCell, door == Terrain.LOCKED_DOOR ? Terrain.DOOR : Terrain.UNLOCKED_EXIT); + GameScene.updateMap(doorCell); + spend(Key.TIME_TO_UNLOCK); + } + } } else if (curAction instanceof HeroAction.OpenChest) { - + Heap heap = Dungeon.level.heaps.get( ((HeroAction.OpenChest)curAction).dst ); - if (heap.type == Type.SKELETON || heap.type == Type.REMAINS) { - Sample.INSTANCE.play( Assets.SND_BONES ); - } else if (heap.type == Type.LOCKED_CHEST){ - Notes.remove(new GoldenKey(Dungeon.depth)); - } else if (heap.type == Type.CRYSTAL_CHEST){ - Notes.remove(new CrystalKey(Dungeon.depth)); + + if (Dungeon.level.adjacent(pos, heap.pos)){ + boolean hasKey = true; + if (heap.type == Type.SKELETON || heap.type == Type.REMAINS) { + Sample.INSTANCE.play( Assets.SND_BONES ); + } else if (heap.type == Type.LOCKED_CHEST){ + hasKey = Notes.remove(new GoldenKey(Dungeon.depth)); + } else if (heap.type == Type.CRYSTAL_CHEST){ + hasKey = Notes.remove(new CrystalKey(Dungeon.depth)); + } + + if (hasKey) { + GameScene.updateKeyDisplay(); + heap.open(this); + spend(Key.TIME_TO_UNLOCK); + } } - GameScene.updateKeyDisplay(); - heap.open( this ); - spend( Key.TIME_TO_UNLOCK ); + } curAction = null; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Notes.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Notes.java index 0efd2e8d6..76d56550f 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Notes.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Notes.java @@ -191,38 +191,43 @@ public class Notes { } } - public static void add( Landmark landmark ) { + public static boolean add( Landmark landmark ) { LandmarkRecord l = new LandmarkRecord( landmark, Dungeon.depth ); if (!records.contains(l)) { - records.add(new LandmarkRecord(landmark, Dungeon.depth)); + boolean result = records.add(new LandmarkRecord(landmark, Dungeon.depth)); Collections.sort(records); + return result; } + return false; } - public static void remove( Landmark landmark ) { - records.remove( new LandmarkRecord(landmark, Dungeon.depth) ); + public static boolean remove( Landmark landmark ) { + return records.remove( new LandmarkRecord(landmark, Dungeon.depth) ); } - public static void add( Key key ){ + public static boolean add( Key key ){ KeyRecord k = new KeyRecord(key); if (!records.contains(k)){ - records.add(k); + boolean result = records.add(k); Collections.sort(records); + return result; } else { k = (KeyRecord) records.get(records.indexOf(k)); k.quantity(k.quantity() + key.quantity()); + return true; } } - public static void remove( Key key ){ + public static boolean remove( Key key ){ KeyRecord k = new KeyRecord( key ); if (records.contains(k)){ k = (KeyRecord) records.get(records.indexOf(k)); k.quantity(k.quantity() - key.quantity()); if (k.quantity() <= 0){ - records.remove(k); + return records.remove(k); } } + return false; } public static int keyCount( Key key ){