Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undo support #53

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.watabou.pixeldungeon"
package="org.ayal.pixeldungeonundone"
android:versionCode="70"
android:versionName="1.7.5a"
android:installLocation="auto">
Expand Down Expand Up @@ -28,7 +28,7 @@

<activity
android:label="@string/app_name"
android:name=".PixelDungeon"
android:name=".Main"
android:screenOrientation="portrait">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
Expand Down
Binary file modified assets/banners.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Pixel Dungeon</string>
<string name="app_name">Pixel Dungeon Undone</string>

</resources>
204 changes: 134 additions & 70 deletions src/com/watabou/pixeldungeon/Dungeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.watabou.pixeldungeon;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -61,6 +63,7 @@
import com.watabou.pixeldungeon.scenes.StartScene;
import com.watabou.pixeldungeon.ui.QuickSlot;
import com.watabou.pixeldungeon.utils.BArray;
import com.watabou.pixeldungeon.utils.FIFO;
import com.watabou.pixeldungeon.utils.Utils;
import com.watabou.pixeldungeon.windows.WndResurrect;
import com.watabou.utils.Bundlable;
Expand Down Expand Up @@ -95,14 +98,18 @@ public class Dungeon {
public static boolean nightMode;

public static SparseArray<ArrayList<Item>> droppedItems;


final static int undoLength = 10;
public static FIFO<byte[]> undoGameBuffer;
public static FIFO<byte[]> undoLevelBuffer;

public static void init() {

challenges = PixelDungeon.challenges();

Actor.clear();

PathFinder.setMapSize( Level.WIDTH, Level.HEIGHT );
PathFinder.setMapSize(Level.WIDTH, Level.HEIGHT);

Scroll.initLabels();
Potion.initColors();
Expand All @@ -116,7 +123,10 @@ public static void init() {
gold = 0;

droppedItems = new SparseArray<ArrayList<Item>>();


undoGameBuffer = new FIFO<>(undoLength);
undoLevelBuffer = new FIFO<>(undoLength);

potionOfStrength = 0;
scrollsOfUpgrade = 0;
scrollsOfEnchantment = 0;
Expand Down Expand Up @@ -361,64 +371,95 @@ private static String depthFile( HeroClass cl ) {
return RG_DEPTH_FILE;
}
}


public static Bundle saveGameBundle () {
Bundle bundle = new Bundle();

bundle.put( VERSION, Game.version );
bundle.put( CHALLENGES, challenges );
bundle.put( HERO, hero );
bundle.put( GOLD, gold );
bundle.put( DEPTH, depth );

for (int d : droppedItems.keyArray()) {
bundle.put( String.format( DROPPED, d ), droppedItems.get( d ) );
}

bundle.put( POS, potionOfStrength );
bundle.put( SOU, scrollsOfUpgrade );
bundle.put( SOE, scrollsOfEnchantment );
bundle.put( DV, dewVial );
bundle.put( WT, transmutation );

int count = 0;
int ids[] = new int[chapters.size()];
for (Integer id : chapters) {
ids[count++] = id;
}
bundle.put( CHAPTERS, ids );

Bundle quests = new Bundle();
Ghost .Quest.storeInBundle( quests );
Wandmaker .Quest.storeInBundle( quests );
Blacksmith .Quest.storeInBundle( quests );
Imp .Quest.storeInBundle( quests );
bundle.put( QUESTS, quests );

Room.storeRoomsInBundle( bundle );

Statistics.storeInBundle( bundle );
Journal.storeInBundle( bundle );

QuickSlot.save( bundle );

Scroll.save( bundle );
Potion.save( bundle );
Wand.save( bundle );
Ring.save( bundle );

Bundle badges = new Bundle();
Badges.saveLocal( badges );
bundle.put( BADGES, badges );

return bundle;

}

public static void saveUndo () {

Actor.fixTime();
ByteArrayOutputStream bos;
try {
Bundle gameBundle = saveGameBundle();

bos = new ByteArrayOutputStream();
Bundle.write(gameBundle, bos);
undoGameBuffer.add(bos.toByteArray());
bos.close();

Bundle levelBundle = new Bundle();
levelBundle.put(LEVEL, level);

bos = new ByteArrayOutputStream();
Bundle.write(levelBundle, bos);
undoLevelBuffer.add(bos.toByteArray());
bos.close();

} catch (IOException e) {
e.printStackTrace();
}
}

public static void saveGame( String fileName ) throws IOException {
try {
Bundle bundle = new Bundle();

bundle.put( VERSION, Game.version );
bundle.put( CHALLENGES, challenges );
bundle.put( HERO, hero );
bundle.put( GOLD, gold );
bundle.put( DEPTH, depth );

for (int d : droppedItems.keyArray()) {
bundle.put( String.format( DROPPED, d ), droppedItems.get( d ) );
}

bundle.put( POS, potionOfStrength );
bundle.put( SOU, scrollsOfUpgrade );
bundle.put( SOE, scrollsOfEnchantment );
bundle.put( DV, dewVial );
bundle.put( WT, transmutation );

int count = 0;
int ids[] = new int[chapters.size()];
for (Integer id : chapters) {
ids[count++] = id;
}
bundle.put( CHAPTERS, ids );

Bundle quests = new Bundle();
Ghost .Quest.storeInBundle( quests );
Wandmaker .Quest.storeInBundle( quests );
Blacksmith .Quest.storeInBundle( quests );
Imp .Quest.storeInBundle( quests );
bundle.put( QUESTS, quests );

Room.storeRoomsInBundle( bundle );

Statistics.storeInBundle( bundle );
Journal.storeInBundle( bundle );

QuickSlot.save( bundle );

Scroll.save( bundle );
Potion.save( bundle );
Wand.save( bundle );
Ring.save( bundle );

Bundle badges = new Bundle();
Badges.saveLocal( badges );
bundle.put( BADGES, badges );

Bundle bundle = saveGameBundle();
OutputStream output = Game.instance.openFileOutput( fileName, Game.MODE_PRIVATE );
Bundle.write( bundle, output );
Bundle.write(bundle, output);
output.close();

} catch (Exception e) {

GamesInProgress.setUnknown( hero.heroClass );
GamesInProgress.setUnknown(hero.heroClass);
}
}

Expand Down Expand Up @@ -455,10 +496,22 @@ public static void loadGame( HeroClass cl ) throws IOException {
public static void loadGame( String fileName ) throws IOException {
loadGame( fileName, false );
}


public static void loadGame( int undoLocation) {

try {
loadGame(gameBundle(undoLocation), true);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void loadGame( String fileName, boolean fullLoad ) throws IOException {

Bundle bundle = gameBundle( fileName );
loadGame(gameBundle(fileName), fullLoad);

}

public static void loadGame( Bundle bundle, boolean fullLoad ) throws IOException {

Dungeon.challenges = bundle.getInt( CHALLENGES );

Expand Down Expand Up @@ -539,19 +592,25 @@ public static void loadGame( String fileName, boolean fullLoad ) throws IOExcept
}
}
}

public static Level loadLevel( HeroClass cl ) throws IOException {


public static Level loadLevel( InputStream input) throws IOException {
Dungeon.level = null;
Actor.clear();

InputStream input = Game.instance.openFileInput( Utils.format( depthFile( cl ), depth ) ) ;

Bundle bundle = Bundle.read( input );
input.close();

return (Level)bundle.get( "level" );
}

public static Level loadLevel( HeroClass cl ) throws IOException {
return loadLevel(Game.instance.openFileInput( Utils.format( depthFile( cl ), depth ) )) ;
}

public static Level loadLevel( int undoLocation) throws IOException {
return loadLevel(new ByteArrayInputStream( undoLevelBuffer.get(undoLocation)));
}

public static void deleteGame( HeroClass cl, boolean deleteLevels ) {

Game.instance.deleteFile( gameFile( cl ) );
Expand All @@ -563,17 +622,22 @@ public static void deleteGame( HeroClass cl, boolean deleteLevels ) {
}
}

GamesInProgress.delete( cl );
GamesInProgress.delete(cl);
}

public static Bundle gameBundle( String fileName ) throws IOException {

InputStream input = Game.instance.openFileInput( fileName );
Bundle bundle = Bundle.read( input );

public static Bundle gameBundle( InputStream input ) throws IOException {
Bundle bundle = Bundle.read(input);
input.close();

return bundle;
}

public static Bundle gameBundle( int undoLocation ) throws IOException {
return gameBundle(new ByteArrayInputStream( undoGameBuffer.get(undoLocation) ));
}

public static Bundle gameBundle( String fileName ) throws IOException {
return gameBundle(Game.instance.openFileInput( fileName ));
}

public static void preview( GamesInProgress.Info info, Bundle bundle ) {
info.depth = bundle.getInt( DEPTH );
Expand Down
5 changes: 3 additions & 2 deletions src/com/watabou/pixeldungeon/actors/hero/Hero.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public boolean act() {
return false;

} else {

restoreHealth = false;

ready = false;
Expand Down Expand Up @@ -466,7 +466,8 @@ private void ready() {
sprite.idle();
curAction = null;
ready = true;


Dungeon.saveUndo();
GameScene.ready();
}

Expand Down
6 changes: 5 additions & 1 deletion src/com/watabou/pixeldungeon/actors/mobs/Mimic.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.watabou.pixeldungeon.levels.Level;
import com.watabou.pixeldungeon.scenes.GameScene;
import com.watabou.pixeldungeon.sprites.MimicSprite;
import com.watabou.utils.Bundlable;
import com.watabou.utils.Bundle;
import com.watabou.utils.Random;

Expand Down Expand Up @@ -64,7 +65,10 @@ public void storeInBundle( Bundle bundle ) {
@Override
public void restoreFromBundle( Bundle bundle ) {
super.restoreFromBundle( bundle );
items = new ArrayList<Item>( (Collection<? extends Item>) bundle.getCollection( ITEMS ) );
items = new ArrayList<Item>();
for (Bundlable bund: bundle.getCollection( ITEMS )) {
items.add((Item) bund);
}
adjustStats( bundle.getInt( LEVEL ) );
}

Expand Down
6 changes: 5 additions & 1 deletion src/com/watabou/pixeldungeon/items/Heap.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ public void destroy() {
public void restoreFromBundle( Bundle bundle ) {
pos = bundle.getInt( POS );
type = Type.valueOf( bundle.getString( TYPE ) );
items = new LinkedList<Item>( (Collection<? extends Item>) bundle.getCollection( ITEMS ) );

items = new LinkedList<Item>();
for (Bundlable bun : bundle.getCollection( ITEMS )) {
items.add( (Item) bun);
}
}

@Override
Expand Down
8 changes: 6 additions & 2 deletions src/com/watabou/pixeldungeon/levels/RegularLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.watabou.pixeldungeon.items.scrolls.ScrollOfUpgrade;
import com.watabou.pixeldungeon.levels.Room.Type;
import com.watabou.pixeldungeon.levels.painters.*;
import com.watabou.utils.Bundlable;
import com.watabou.utils.Bundle;
import com.watabou.utils.Graph;
import com.watabou.utils.Random;
Expand Down Expand Up @@ -681,8 +682,11 @@ public void storeInBundle( Bundle bundle ) {
@Override
public void restoreFromBundle( Bundle bundle ) {
super.restoreFromBundle( bundle );

rooms = new HashSet<Room>( (Collection<? extends Room>) bundle.getCollection( "rooms" ) );
rooms = new HashSet<Room>();
for (Bundlable bund: bundle.getCollection( "rooms" ) ) {
rooms.add((Room) bund);
}

for (Room r : rooms) {
if (r.type == Type.WEAK_FLOOR) {
weakFloorCreated = true;
Expand Down
Loading