From 5fcc1a612a9912d5a12ea62b4b05b00f61bf921a Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 13 Feb 2015 15:27:08 -0500 Subject: [PATCH] v0.2.4: Switched to Watabou's much more elegant logic for items falling between floors --- .../shatteredpixeldungeon/Dungeon.java | 69 ++++++++++--------- .../shatteredpixeldungeon/levels/Level.java | 10 +-- .../scenes/GameScene.java | 22 +++++- .../scenes/InterlevelScene.java | 43 +----------- 4 files changed, 61 insertions(+), 83 deletions(-) diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java b/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java index 0195cd2a2..4f9bd14f2 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/Dungeon.java @@ -19,11 +19,6 @@ package com.shatteredpixel.shatteredpixeldungeon; import com.shatteredpixel.shatteredpixeldungeon.actors.Actor; import com.shatteredpixel.shatteredpixeldungeon.actors.Char; -import com.shatteredpixel.shatteredpixeldungeon.actors.blobs.Blob; -import com.shatteredpixel.shatteredpixeldungeon.actors.blobs.ConfusionGas; -import com.shatteredpixel.shatteredpixeldungeon.actors.blobs.Fire; -import com.shatteredpixel.shatteredpixeldungeon.actors.blobs.ParalyticGas; -import com.shatteredpixel.shatteredpixeldungeon.actors.blobs.ToxicGas; import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.Amok; import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.Light; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; @@ -34,11 +29,8 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.npcs.Imp; import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.npcs.Wandmaker; import com.shatteredpixel.shatteredpixeldungeon.items.Ankh; import com.shatteredpixel.shatteredpixeldungeon.items.Generator; +import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; -import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfLevitation; -import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfLiquidFlame; -import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfParalyticGas; -import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfToxicGas; import com.shatteredpixel.shatteredpixeldungeon.items.rings.Ring; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll; import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand; @@ -64,13 +56,16 @@ import com.shatteredpixel.shatteredpixeldungeon.utils.BArray; import com.shatteredpixel.shatteredpixeldungeon.utils.Utils; import com.shatteredpixel.shatteredpixeldungeon.windows.WndResurrect; import com.watabou.noosa.Game; +import com.watabou.utils.Bundlable; import com.watabou.utils.Bundle; import com.watabou.utils.PathFinder; import com.watabou.utils.Random; +import com.watabou.utils.SparseArray; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -133,6 +128,8 @@ public class Dungeon { // Hero's field of view public static boolean[] visible = new boolean[Level.LENGTH]; + public static SparseArray> droppedItems; + public static int version; public static void init() { @@ -159,7 +156,9 @@ public class Dungeon { depth = 0; gold = 0; - + + droppedItems = new SparseArray>(); + for (limitedDrops a : limitedDrops.values()) a.count = 0; @@ -301,24 +300,6 @@ public class Dungeon { Actor.add( level.respawner() ); } - for (Potion potion : level.fallingPotions){ - - int cell = level.randomRespawnCell(); - while (cell == -1) - cell = level.randomRespawnCell(); - - if (potion instanceof PotionOfLiquidFlame) - GameScene.add( Blob.seed( cell, 2, Fire.class)); - else if (potion instanceof PotionOfToxicGas) - GameScene.add( Blob.seed( cell, 1000, ToxicGas.class ) ); - else if (potion instanceof PotionOfParalyticGas) - GameScene.add( Blob.seed( cell, 1000, ParalyticGas.class ) ); - else if (potion instanceof PotionOfLevitation) - GameScene.add( Blob.seed( cell, 1000, ConfusionGas.class ) ); - - } - level.fallingPotions.clear(); - hero.pos = pos != -1 ? pos : level.exit; Light light = hero.buff( Light.class ); @@ -328,11 +309,20 @@ public class Dungeon { try { saveAll(); } catch (IOException e) { - /*This only catches IO errors. Yes, this means things can do wrong, and they can go wrong catastrophically. + /*This only catches IO errors. Yes, this means things can go wrong, and they can go wrong catastrophically. But when they do the user will get a nice 'report this issue' dialogue, and I can fix the bug.*/ } } - + + public static void dropToChasm( Item item ) { + int depth = Dungeon.depth + 1; + ArrayList dropped = (ArrayList)Dungeon.droppedItems.get( depth ); + if (dropped == null) { + Dungeon.droppedItems.put( depth, dropped = new ArrayList() ); + } + dropped.add( item ); + } + public static boolean posNeeded() { int[] quota = {4, 2, 9, 4, 14, 6, 19, 8, 24, 9}; return chance( quota, limitedDrops.strengthPotions.count ); @@ -377,7 +367,7 @@ public class Dungeon { private static final String HERO = "hero"; private static final String GOLD = "gold"; private static final String DEPTH = "depth"; - private static final String QUICKSLOT = "quickslot"; + private static final String DROPPED = "dropped%d"; private static final String LEVEL = "level"; private static final String LIMDROPS = "limiteddrops"; private static final String DV = "dewVial"; @@ -427,6 +417,10 @@ public class Dungeon { bundle.put( GOLD, gold ); bundle.put( DEPTH, depth ); + for (int d : droppedItems.keyArray()) { + bundle.put(String.format(DROPPED, d), droppedItems.get(d)); + } + quickslot.storePlaceholders( bundle ); bundle.put( WT, transmutation ); @@ -507,7 +501,7 @@ public class Dungeon { public static void loadGame( HeroClass cl ) throws IOException { loadGame( gameFile( cl ), true ); } - + public static void loadGame( String fileName ) throws IOException { loadGame( fileName, false ); } @@ -601,6 +595,17 @@ public class Dungeon { Journal.restoreFromBundle( bundle ); Generator.restoreFromBundle( bundle ); + droppedItems = new SparseArray>(); + for (int i=2; i <= Statistics.deepestFloor + 1; i++) { + ArrayList dropped = new ArrayList(); + for (Bundlable b : bundle.getCollection( String.format( DROPPED, i ) ) ) { + dropped.add( (Item)b ); + } + if (!dropped.isEmpty()) { + droppedItems.put( i, dropped ); + } + } + //logic for pre 0.2.4 bags, remove when no longer supporting those saves. if (version <= 32){ int deepest = Statistics.deepestFloor; diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java b/src/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java index fac306cdb..c22012b33 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/levels/Level.java @@ -51,7 +51,6 @@ import com.shatteredpixel.shatteredpixeldungeon.items.bags.ScrollHolder; import com.shatteredpixel.shatteredpixeldungeon.items.bags.SeedPouch; import com.shatteredpixel.shatteredpixeldungeon.items.food.Blandfruit; import com.shatteredpixel.shatteredpixeldungeon.items.food.Food; -import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfHealing; import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfMight; import com.shatteredpixel.shatteredpixeldungeon.items.potions.PotionOfStrength; @@ -154,9 +153,6 @@ public abstract class Level implements Bundlable { public SparseArray plants; protected ArrayList itemsToSpawn = new ArrayList(); - - public ArrayList fallingItems = new ArrayList(); - public ArrayList fallingPotions = new ArrayList(); public int color1 = 0x004400; public int color2 = 0x88CC44; @@ -174,7 +170,6 @@ public abstract class Level implements Bundlable { private static final String PLANTS = "plants"; private static final String MOBS = "mobs"; private static final String BLOBS = "blobs"; - private static final String FALLING = "falling"; private static final String FEELING = "feeling"; public void create() { @@ -339,8 +334,6 @@ public abstract class Level implements Bundlable { blobs.put( blob.getClass(), blob ); } - fallingItems = (ArrayList)bundle.getCollection( FALLING ); - feeling = bundle.getEnum( FEELING, Feeling.class ); if (feeling == Feeling.DARK) viewDistance = (int)Math.ceil(viewDistance/3f); @@ -361,7 +354,6 @@ public abstract class Level implements Bundlable { bundle.put( PLANTS, plants.values() ); bundle.put( MOBS, mobs ); bundle.put( BLOBS, blobs.values() ); - bundle.put( FALLING, fallingItems); bundle.put( FEELING, feeling ); } @@ -634,8 +626,8 @@ public abstract class Level implements Bundlable { heap = new Heap(); heap.pos = cell; if (map[cell] == Terrain.CHASM || (Dungeon.level != null && pit[cell])) { + Dungeon.dropToChasm( item ); GameScene.discard( heap ); - fallingItems.add(item); } else { heaps.put( cell, heap ); GameScene.add( heap ); diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java b/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java index 569b8af11..3cda76a03 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/scenes/GameScene.java @@ -18,8 +18,11 @@ package com.shatteredpixel.shatteredpixeldungeon.scenes; import java.io.IOException; +import java.util.ArrayList; import com.shatteredpixel.shatteredpixeldungeon.*; +import com.shatteredpixel.shatteredpixeldungeon.items.Honeypot; +import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; import com.shatteredpixel.shatteredpixeldungeon.ui.LootIndicator; import com.shatteredpixel.shatteredpixeldungeon.ui.ResumeIndicator; import com.watabou.noosa.Camera; @@ -306,7 +309,24 @@ public class GameScene extends PixelScene { break; default: } - + + ArrayList dropped = Dungeon.droppedItems.get( Dungeon.depth ); + if (dropped != null) { + for (Item item : dropped) { + int pos = Dungeon.level.randomRespawnCell(); + if (item instanceof Potion) { + ((Potion)item).shatter( pos ); + } else if (item instanceof Plant.Seed) { + Dungeon.level.plant( (Plant.Seed)item, pos ); + } else if (item instanceof Honeypot) { + (Honeypot)item.shatter( pos ); + } else { + Dungeon.level.drop( item, pos ); + } + } + Dungeon.droppedItems.remove( Dungeon.depth ); + } + Camera.main.target = hero; fadeIn(); } diff --git a/src/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java b/src/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java index f5dedcda0..be457d4f8 100644 --- a/src/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java +++ b/src/com/shatteredpixel/shatteredpixeldungeon/scenes/InterlevelScene.java @@ -22,8 +22,6 @@ import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.shatteredpixel.shatteredpixeldungeon.Statistics; import com.shatteredpixel.shatteredpixeldungeon.actors.Actor; import com.shatteredpixel.shatteredpixeldungeon.items.Generator; -import com.shatteredpixel.shatteredpixeldungeon.items.Item; -import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; import com.shatteredpixel.shatteredpixeldungeon.levels.Level; import com.shatteredpixel.shatteredpixeldungeon.windows.WndError; import com.shatteredpixel.shatteredpixeldungeon.windows.WndStory; @@ -35,7 +33,6 @@ import com.watabou.noosa.audio.Sample; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; public class InterlevelScene extends PixelScene { @@ -212,9 +209,6 @@ public class InterlevelScene extends PixelScene { private void descend() throws IOException { - Level level; - ArrayList fallingItems = new ArrayList(); - Actor.fixTime(); if (Dungeon.hero == null) { Dungeon.init(); @@ -223,64 +217,31 @@ public class InterlevelScene extends PixelScene { noStory = false; } } else { - level = Dungeon.level; - - fallingItems = level.fallingItems; - level.fallingItems = new ArrayList(); - Dungeon.saveLevel(); } + Level level; if (Dungeon.depth >= Statistics.deepestFloor) { level = Dungeon.newLevel(); } else { Dungeon.depth++; level = Dungeon.loadLevel( Dungeon.hero.heroClass ); } - - for (Item item : fallingItems){ - int cell = level.randomRespawnCell(); - while (cell == -1) - cell = level.randomRespawnCell(); - - if (!(item instanceof Potion)) - level.drop(item, cell); - else - level.fallingPotions.add((Potion)item); - } - Dungeon.switchLevel( level, level.entrance ); - } private void fall() throws IOException { - Level level = Dungeon.level; - - ArrayList fallingItems = level.fallingItems; - level.fallingItems = new ArrayList(); - Actor.fixTime(); Dungeon.saveLevel(); + Level level; if (Dungeon.depth >= Statistics.deepestFloor) { level = Dungeon.newLevel(); } else { Dungeon.depth++; level = Dungeon.loadLevel( Dungeon.hero.heroClass ); } - - for (Item item : fallingItems){ - int cell = level.randomRespawnCell(); - while (cell == -1) - cell = level.randomRespawnCell(); - - if (!(item instanceof Potion)) - level.drop(item, cell); - else - level.fallingPotions.add((Potion)item); - } - Dungeon.switchLevel( level, fallIntoPit ? level.pitCell() : level.randomRespawnCell() ); }