diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Heap.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Heap.java index 83922e958..d0d45d9f6 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Heap.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/Heap.java @@ -414,8 +414,8 @@ public class Heap implements Bundlable { //remove any document pages that either don't exist anymore or that the player already has for (Item item : items.toArray(new Item[0])){ if (item instanceof DocumentPage - && ( !((DocumentPage) item).document().pages().contains(((DocumentPage) item).page()) - || ((DocumentPage) item).document().hasPage(((DocumentPage) item).page()))){ + && ( !((DocumentPage) item).document().pageNames().contains(((DocumentPage) item).page()) + || ((DocumentPage) item).document().pageFound(((DocumentPage) item).page()))){ items.remove(item); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/journal/DocumentPage.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/journal/DocumentPage.java index a38688d77..68d6c5aae 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/journal/DocumentPage.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/journal/DocumentPage.java @@ -58,7 +58,7 @@ public abstract class DocumentPage extends Item { } else { WndJournal.last_index = 0; } - document().addPage(page); + document().findPage(page); Sample.INSTANCE.play( Assets.Sounds.ITEM ); hero.spendAndNext( TIME_TO_PICK_UP ); return true; diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Document.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Document.java index 6483b6d7e..0caf5ef99 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Document.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/journal/Document.java @@ -26,11 +26,8 @@ import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; import com.watabou.utils.Bundle; import com.watabou.utils.DeviceCompat; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.LinkedHashMap; -import java.util.List; public enum Document { @@ -40,37 +37,52 @@ public enum Document { Document( int sprite ){ pageSprite = sprite; } + + private static final int NOT_FOUND = 0; + private static final int FOUND = 1; + private static final int READ = 2; + private LinkedHashMap pagesStates = new LinkedHashMap<>(); - private LinkedHashMap pages = new LinkedHashMap<>(); - - public Collection pages(){ - return pages.keySet(); + public boolean findPage(String page ) { + if (pagesStates.containsKey(page) && pagesStates.get(page) == NOT_FOUND){ + pagesStates.put(page, FOUND); + Journal.saveNeeded = true; + return true; + } + return false; } - - public boolean addPage( String page ) { - if (pages.containsKey(page) && !pages.get(page)){ - pages.put(page, true); + + public boolean readPage( String page ) { + if (pagesStates.containsKey(page) && pagesStates.get(page) == FOUND){ + pagesStates.put(page, READ); Journal.saveNeeded = true; return true; } return false; } - public boolean hasPage( String page ){ - return pages.containsKey(page) && pages.get(page); + public boolean pageFound( String page ){ + return pagesStates.containsKey(page) && pagesStates.get(page) > NOT_FOUND; } - public boolean hasPage( int pageIdx ){ - return hasPage( pages.keySet().toArray(new String[0])[pageIdx] ); + public boolean pageFound( int pageIdx ){ + return pageFound( pagesStates.keySet().toArray(new String[0])[pageIdx] ); } - - public boolean hasAnyPages(){ - for (String p : pages.keySet()){ - if (pages.get(p)) { - return true; - } - } - return false; + + public boolean pageRead( String page ){ + return pagesStates.containsKey(page) && pagesStates.get(page) == READ; + } + + public boolean pageRead( int pageIdx ){ + return pageRead( pagesStates.keySet().toArray(new String[0])[pageIdx] ); + } + + public void setPageState( String page, int state ){ + if (pagesStates.containsKey(page)) pagesStates.put(page, state); + } + + public Collection pageNames(){ + return pagesStates.keySet(); } private int pageSprite; @@ -87,7 +99,7 @@ public enum Document { } public String pageTitle( int pageIdx ){ - return pageTitle( pages.keySet().toArray(new String[0])[pageIdx] ); + return pageTitle( pagesStates.keySet().toArray(new String[0])[pageIdx] ); } public String pageBody( String page ){ @@ -95,58 +107,60 @@ public enum Document { } public String pageBody( int pageIdx ){ - return pageBody( pages.keySet().toArray(new String[0])[pageIdx] ); + return pageBody( pagesStates.keySet().toArray(new String[0])[pageIdx] ); } public static final String GUIDE_INTRO_PAGE = "Intro"; public static final String GUIDE_SEARCH_PAGE = "Examining_and_Searching"; static { - ADVENTURERS_GUIDE.pages.put(GUIDE_INTRO_PAGE, DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Identifying", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put(GUIDE_SEARCH_PAGE, DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Strength", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Food", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Levelling", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Surprise_Attacks", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Dieing", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Looting", DeviceCompat.isDebug()); - ADVENTURERS_GUIDE.pages.put("Magic", DeviceCompat.isDebug()); + ADVENTURERS_GUIDE.pagesStates.put(GUIDE_INTRO_PAGE, DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Identifying", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put(GUIDE_SEARCH_PAGE, DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Strength", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Food", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Levelling", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Surprise_Attacks", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Dieing", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Looting", DeviceCompat.isDebug() ? 2 : 0); + ADVENTURERS_GUIDE.pagesStates.put("Magic", DeviceCompat.isDebug() ? 2 : 0); //sewers - ALCHEMY_GUIDE.pages.put("Potions", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Stones", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Energy_Food", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Bombs", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Weapons", DeviceCompat.isDebug()); + ALCHEMY_GUIDE.pagesStates.put("Potions", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Stones", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Energy_Food", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Bombs", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Weapons", DeviceCompat.isDebug() ? 2 : 0); //prison - ALCHEMY_GUIDE.pages.put("Exotic_Potions", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Exotic_Scrolls", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Catalysts", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Brews_Elixirs", DeviceCompat.isDebug()); - ALCHEMY_GUIDE.pages.put("Spells", DeviceCompat.isDebug()); + ALCHEMY_GUIDE.pagesStates.put("Exotic_Potions", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Exotic_Scrolls", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Catalysts", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Brews_Elixirs", DeviceCompat.isDebug() ? 2 : 0); + ALCHEMY_GUIDE.pagesStates.put("Spells", DeviceCompat.isDebug() ? 2 : 0); } private static final String DOCUMENTS = "documents"; public static void store( Bundle bundle ){ - Bundle docBundle = new Bundle(); + Bundle docsBundle = new Bundle(); for ( Document doc : values()){ - ArrayList pages = new ArrayList<>(); - for (String page : doc.pages()){ - if (doc.pages.get(page)){ - pages.add(page); + Bundle pagesBundle = new Bundle(); + boolean empty = true; + for (String page : doc.pageNames()){ + if (doc.pagesStates.get(page) != NOT_FOUND){ + pagesBundle.put(page, doc.pagesStates.get(page)); + empty = false; } } - if (!pages.isEmpty()) { - docBundle.put(doc.name(), pages.toArray(new String[0])); + if (!empty){ + docsBundle.put(doc.name(), pagesBundle); } } - bundle.put( DOCUMENTS, docBundle ); + bundle.put( DOCUMENTS, docsBundle ); } @@ -156,14 +170,25 @@ public enum Document { return; } - Bundle docBundle = bundle.getBundle( DOCUMENTS ); + Bundle docsBundle = bundle.getBundle( DOCUMENTS ); for ( Document doc : values()){ - if (docBundle.contains(doc.name())){ - List pages = Arrays.asList(docBundle.getStringArray(doc.name())); - for (String page : pages){ - if (doc.pages.containsKey(page)) { - doc.pages.put(page, true); + if (docsBundle.contains(doc.name())){ + Bundle pagesBundle = docsBundle.getBundle(doc.name()); + + //compatibility with pre-0.9.4 saves + if (pagesBundle.isNull()) { + for (String page : docsBundle.getStringArray(doc.name())){ + if (doc.pagesStates.containsKey(page)) { + doc.pagesStates.put(page, READ); + } + } + + } else { + for (String page : doc.pageNames()) { + if (pagesBundle.contains(page)) { + doc.pagesStates.put(page, pagesBundle.getInt(page)); + } } } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java index 3779f7fd8..01a1c6ed6 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/RegularLevel.java @@ -436,10 +436,10 @@ public abstract class RegularLevel extends Level { } //guide pages - Collection allPages = Document.ADVENTURERS_GUIDE.pages(); + Collection allPages = Document.ADVENTURERS_GUIDE.pageNames(); ArrayList missingPages = new ArrayList<>(); for ( String page : allPages){ - if (!Document.ADVENTURERS_GUIDE.hasPage(page)){ + if (!Document.ADVENTURERS_GUIDE.pageFound(page)){ missingPages.add(page); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java index 8d4e09b88..5b6fb5af5 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/painters/RegularPainter.java @@ -216,7 +216,7 @@ public abstract class RegularPainter extends Painter { //entrance doors on floor 2 are hidden if the player hasn't picked up 2nd guidebook page if (Dungeon.depth == 2 - && !Document.ADVENTURERS_GUIDE.hasPage(Document.GUIDE_SEARCH_PAGE) + && !Document.ADVENTURERS_GUIDE.pageFound(Document.GUIDE_SEARCH_PAGE) && r instanceof EntranceRoom){ d.type = Room.Door.Type.HIDDEN; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/LaboratoryRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/LaboratoryRoom.java index 873696c41..c70eff1e0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/LaboratoryRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/special/LaboratoryRoom.java @@ -76,10 +76,10 @@ public class LaboratoryRoom extends SpecialRoom { } //guide pages - Collection allPages = Document.ALCHEMY_GUIDE.pages(); + Collection allPages = Document.ALCHEMY_GUIDE.pageNames(); ArrayList missingPages = new ArrayList<>(); for ( String page : allPages){ - if (!Document.ALCHEMY_GUIDE.hasPage(page)){ + if (!Document.ALCHEMY_GUIDE.pageFound(page)){ missingPages.add(page); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/EntranceRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/EntranceRoom.java index 29f541e0d..0b13e9369 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/EntranceRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/standard/EntranceRoom.java @@ -66,7 +66,7 @@ public class EntranceRoom extends StandardRoom { Random.pushGenerator(); //places the first guidebook page on floor 1 - if (Dungeon.depth == 1 && !Document.ADVENTURERS_GUIDE.hasPage(Document.GUIDE_INTRO_PAGE)){ + if (Dungeon.depth == 1 && !Document.ADVENTURERS_GUIDE.pageFound(Document.GUIDE_INTRO_PAGE)){ int pos; do { //can't be on bottom row of tiles @@ -79,7 +79,7 @@ public class EntranceRoom extends StandardRoom { } //places the third guidebook page on floor 2 - if (Dungeon.depth == 2 && !Document.ADVENTURERS_GUIDE.hasPage(Document.GUIDE_SEARCH_PAGE)){ + if (Dungeon.depth == 2 && !Document.ADVENTURERS_GUIDE.pageFound(Document.GUIDE_SEARCH_PAGE)){ int pos; do { //can't be on bottom row of tiles diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java index a3a8f6049..d5e365b10 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/windows/WndJournal.java @@ -245,7 +245,7 @@ public class WndJournal extends WndTabbed { pos += Math.max(ITEM_HEIGHT, title.height()); - for (String page : Document.ADVENTURERS_GUIDE.pages()){ + for (String page : Document.ADVENTURERS_GUIDE.pageNames()){ GuideItem item = new GuideItem( page ); item.setRect( 0, pos, width(), ITEM_HEIGHT ); @@ -268,7 +268,7 @@ public class WndJournal extends WndTabbed { super( iconForPage(page), Messages.titleCase(Document.ADVENTURERS_GUIDE.pageTitle(page))); this.page = page; - found = Document.ADVENTURERS_GUIDE.hasPage(page); + found = Document.ADVENTURERS_GUIDE.pageFound(page); if (!found) { icon.hardlight( 0.5f, 0.5f, 0.5f); @@ -291,7 +291,7 @@ public class WndJournal extends WndTabbed { //TODO might just want this to be part of the Document class private static Image iconForPage( String page ){ - if (!Document.ADVENTURERS_GUIDE.hasPage(page)){ + if (!Document.ADVENTURERS_GUIDE.pageFound(page)){ return new ItemSprite( ItemSpriteSheet.GUIDE_PAGE ); } switch (page){ @@ -349,7 +349,7 @@ public class WndJournal extends WndTabbed { updateList(); } }; - if (Document.ALCHEMY_GUIDE.hasPage(i)) { + if (Document.ALCHEMY_GUIDE.pageFound(i)) { pageButtons[i].icon(new ItemSprite(ItemSpriteSheet.SOMETHING + spriteIndexes[i], null)); } else { pageButtons[i].icon(new ItemSprite(ItemSpriteSheet.SOMETHING, null));