diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Belongings.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Belongings.java index e395edf33..60210fba0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Belongings.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/hero/Belongings.java @@ -41,8 +41,6 @@ import java.util.Iterator; public class Belongings implements Iterable { - public static final int BACKPACK_SIZE = 20; - private Hero owner; public Bag backpack; @@ -55,10 +53,20 @@ public class Belongings implements Iterable { public Belongings( Hero owner ) { this.owner = owner; - backpack = new Bag() {{ - name = Messages.get(Bag.class, "name"); - size = BACKPACK_SIZE; - }}; + backpack = new Bag() { + { + name = Messages.get(Bag.class, "name"); + } + public int capacity(){ + int cap = super.capacity(); + for (Item item : items){ + if (item instanceof Bag){ + cap++; + } + } + return cap; + } + }; backpack.owner = owner; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java index 75e4a63da..7907c8318 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Item.java @@ -170,20 +170,25 @@ public class Item implements Bundlable { } public boolean collect( Bag container ) { - + ArrayList items = container.items; - - if (items.contains( this )) { - return true; - } - + for (Item item:items) { - if (item instanceof Bag && ((Bag)item).grab( this )) { + if (item instanceof Bag && ((Bag)item).canHold( this )) { if (collect( (Bag)item )){ return true; } } } + + if (!container.canHold(this)){ + GLog.n( Messages.get(Item.class, "pack_full", container.name()) ); + return false; + } + + if (items.contains( this )) { + return true; + } if (stackable) { for (Item item:items) { @@ -194,25 +199,17 @@ public class Item implements Bundlable { } } } - - if (items.size() < container.size) { - - if (Dungeon.hero != null && Dungeon.hero.isAlive()) { - Badges.validateItemLevelAquired( this ); - } - - items.add( this ); - Dungeon.quickslot.replacePlaceholder(this); - updateQuickslot(); - Collections.sort( items, itemComparator ); - return true; - - } else { - - GLog.n( Messages.get(Item.class, "pack_full", container.name()) ); - return false; - + + if (Dungeon.hero != null && Dungeon.hero.isAlive()) { + Badges.validateItemLevelAquired( this ); } + + items.add( this ); + Dungeon.quickslot.replacePlaceholder(this); + updateQuickslot(); + Collections.sort( items, itemComparator ); + return true; + } public boolean collect() { diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/KindofMisc.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/KindofMisc.java index 8615c406f..cc9e40a1c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/KindofMisc.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/KindofMisc.java @@ -21,6 +21,7 @@ package com.shatteredpixel.shatteredpixeldungeon.items; +import com.shatteredpixel.shatteredpixeldungeon.Dungeon; import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Hero; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.scenes.GameScene; @@ -50,13 +51,14 @@ public abstract class KindofMisc extends EquipableItem { protected void onSelect(int index) { KindofMisc equipped = (index == 0 ? m1 : m2); - //temporarily give 1 extra backpack spot to support swapping with a full inventory - hero.belongings.backpack.size++; + int slot = Dungeon.quickslot.getSlot(KindofMisc.this); + detach(hero.belongings.backpack); if (equipped.doUnequip(hero, true, false)) { - //fully re-execute rather than just call doEquip as we want to preserve quickslot - execute(hero, AC_EQUIP); + doEquip(hero); + } else { + collect(); } - hero.belongings.backpack.size--; + if (slot != -1) Dungeon.quickslot.setSlot(slot, KindofMisc.this); } }); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java index bef696166..6dd286a1c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/Bag.java @@ -49,8 +49,10 @@ public class Bag extends Item implements Iterable { public Char owner; public ArrayList items = new ArrayList<>(); - - public int size = 1; + + public int capacity(){ + return 20; // default container size + } @Override public void execute( Hero hero, String action ) { @@ -68,7 +70,7 @@ public class Bag extends Item implements Iterable { public boolean collect( Bag container ) { for (Item item : container.items.toArray( new Item[0] )) { - if (grab( item )) { + if (canHold( item )) { int slot = Dungeon.quickslot.getSlot(item); item.detachAll(container); if (!item.collect(this)) { @@ -146,8 +148,17 @@ public class Bag extends Item implements Iterable { } return false; } - - public boolean grab( Item item ) { + + public boolean canHold( Item item ){ + if (items.contains(item) || item instanceof Bag || items.size() < capacity()){ + return true; + } else { + for (Item i : items) { + if (item.isSimilar( i )) { + return true; + } + } + } return false; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/MagicalHolster.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/MagicalHolster.java index f3229d88e..516037d26 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/MagicalHolster.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/MagicalHolster.java @@ -31,16 +31,18 @@ public class MagicalHolster extends Bag { { image = ItemSpriteSheet.HOLSTER; - - size = 20; } public static final float HOLSTER_SCALE_FACTOR = 0.85f; public static final float HOLSTER_DURABILITY_FACTOR = 1.2f; @Override - public boolean grab( Item item ) { - return item instanceof Wand || item instanceof MissileWeapon || item instanceof Bomb; + public boolean canHold( Item item ) { + if (item instanceof Wand || item instanceof MissileWeapon || item instanceof Bomb){ + return super.canHold(item); + } else { + return false; + } } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/PotionBandolier.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/PotionBandolier.java index 6aa4340f2..de05b8a98 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/PotionBandolier.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/PotionBandolier.java @@ -22,20 +22,25 @@ package com.shatteredpixel.shatteredpixeldungeon.items.bags; import com.shatteredpixel.shatteredpixeldungeon.items.Item; +import com.shatteredpixel.shatteredpixeldungeon.items.bombs.Bomb; import com.shatteredpixel.shatteredpixeldungeon.items.potions.Potion; +import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand; +import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.MissileWeapon; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; public class PotionBandolier extends Bag { { image = ItemSpriteSheet.BANDOLIER; - - size = 20; } @Override - public boolean grab( Item item ) { - return item instanceof Potion; + public boolean canHold( Item item ) { + if (item instanceof Potion){ + return super.canHold(item); + } else { + return false; + } } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/ScrollHolder.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/ScrollHolder.java index 3315b9c1a..8dff4122c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/ScrollHolder.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/ScrollHolder.java @@ -22,22 +22,27 @@ package com.shatteredpixel.shatteredpixeldungeon.items.bags; import com.shatteredpixel.shatteredpixeldungeon.items.Item; +import com.shatteredpixel.shatteredpixeldungeon.items.bombs.Bomb; import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll; import com.shatteredpixel.shatteredpixeldungeon.items.spells.BeaconOfReturning; import com.shatteredpixel.shatteredpixeldungeon.items.spells.Spell; +import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand; +import com.shatteredpixel.shatteredpixeldungeon.items.weapon.missiles.MissileWeapon; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; public class ScrollHolder extends Bag { { image = ItemSpriteSheet.HOLDER; - - size = 20; } - + @Override - public boolean grab( Item item ) { - return item instanceof Scroll || item instanceof Spell; + public boolean canHold( Item item ) { + if (item instanceof Scroll || item instanceof Spell){ + return super.canHold(item); + } else { + return false; + } } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/VelvetPouch.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/VelvetPouch.java index c955e8c66..6eeac1333 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/VelvetPouch.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/bags/VelvetPouch.java @@ -24,6 +24,8 @@ package com.shatteredpixel.shatteredpixeldungeon.items.bags; import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.items.quest.GooBlob; import com.shatteredpixel.shatteredpixeldungeon.items.quest.MetalShard; +import com.shatteredpixel.shatteredpixeldungeon.items.scrolls.Scroll; +import com.shatteredpixel.shatteredpixeldungeon.items.spells.Spell; import com.shatteredpixel.shatteredpixeldungeon.items.stones.Runestone; import com.shatteredpixel.shatteredpixeldungeon.plants.Plant; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; @@ -32,14 +34,16 @@ public class VelvetPouch extends Bag { { image = ItemSpriteSheet.POUCH; - - size = 20; } - + @Override - public boolean grab( Item item ) { - return item instanceof Plant.Seed || item instanceof Runestone - || item instanceof GooBlob || item instanceof MetalShard; + public boolean canHold( Item item ) { + if (item instanceof Plant.Seed || item instanceof Runestone + || item instanceof GooBlob || item instanceof MetalShard){ + return super.canHold(item); + } else { + return false; + } } @Override diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/ShopRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/ShopRoom.java index a90b119c9..bbe4e5e93 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/ShopRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/ShopRoom.java @@ -292,7 +292,7 @@ public class ShopRoom extends SpecialRoom { //count up items in the main bag for (Item item : pack.backpack.items) { for (Bag bag : bags.keySet()){ - if (bag.grab(item)){ + if (bag.canHold(item)){ bags.put(bag, bags.get(bag)+1); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java index 2c3ccbb95..2c0c25db7 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndBag.java @@ -109,7 +109,6 @@ public class WndBag extends WndTabbed { private String title; private int nCols; - private int nRows; protected int count; protected int col; @@ -135,15 +134,14 @@ public class WndBag extends WndTabbed { lastBag = bag; nCols = PixelScene.landscape() ? COLS_L : COLS_P; - nRows = (int)Math.ceil((Belongings.BACKPACK_SIZE + 4) / (float)nCols); - int slotsWidth = SLOT_WIDTH * nCols + SLOT_MARGIN * (nCols - 1); - int slotsHeight = SLOT_HEIGHT * nRows + SLOT_MARGIN * (nRows - 1); placeTitle( bag, slotsWidth ); placeItems( bag ); + int slotsHeight = SLOT_HEIGHT * row + SLOT_MARGIN * (row - 1); + resize( slotsWidth, slotsHeight + TITLE_HEIGHT ); Belongings stuff = Dungeon.hero.belongings; @@ -229,12 +227,16 @@ public class WndBag extends WndTabbed { } // Free Space - while ((count - 4) < container.size) { + while ((count - 4) < container.capacity()) { placeItem( null ); } } protected void placeItem( final Item item ) { + + count++; + + if (item instanceof Bag) return; int x = col * (SLOT_WIDTH + SLOT_MARGIN); int y = TITLE_HEIGHT + row * (SLOT_HEIGHT + SLOT_MARGIN); @@ -245,8 +247,7 @@ public class WndBag extends WndTabbed { col = 0; row++; } - - count++; + } @Override