DEADSOFTWARE

Complete game save
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameSaver.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.files.FileHandle;
5 import ru.deadsoftware.cavecraft.CaveGame;
6 import ru.deadsoftware.cavecraft.Items;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectOutputStream;
10 import java.nio.ByteBuffer;
12 public class GameSaver {
14 private static final int VERSION = 0;
16 private static int[][] fMap, bMap;
17 private static int readIndex;
19 private static int bytesInt(byte[] bytes) {
20 ByteBuffer wrapped = ByteBuffer.wrap(bytes);
21 int res = wrapped.getInt(readIndex);
22 readIndex+=4;
23 return res;
24 }
26 private static void writeInt(FileHandle file, int i, boolean append) {
27 byte[] bytes = ByteBuffer.allocate(4).putInt(i).array();
28 file.writeBytes(bytes, append);
29 }
31 private static void saveMap(FileHandle file, int[][] map) {
32 int rl,bl;
33 int width = map.length;
34 int height = map[0].length;
35 writeInt(file, VERSION, false);
36 writeInt(file, width, true);
37 writeInt(file, height, true);
38 for (int y=0; y<map[0].length; y++) {
39 bl = map[0][y];
40 rl=0;
41 for (int x=0; x<map.length; x++) {
42 if (map[x][y]!=bl || x==map.length-1) {
43 if (x==map.length-1) rl++;
44 writeInt(file, rl, true);
45 writeInt(file, bl, true);
46 System.out.printf("%d. Run:%d; Block:%s\n",y, rl, Items.BLOCKS.getKeyAt(bl));
47 rl=0;
48 bl=map[x][y];
49 }
50 rl++;
51 }
52 }
53 }
55 private static int[][] loadMap(FileHandle file) throws Exception {
56 int[][] map = null;
57 int ver, width, height;
58 int rl,bl;
59 byte[] data = file.readBytes();
60 readIndex = 0;
61 ver = bytesInt(data);
62 if (VERSION == ver) {
63 width = bytesInt(data);
64 height = bytesInt(data);
65 map = new int[width][height];
66 for (int y=0; y<height; y++) {
67 for (int x=0; x<width; x+=rl) {
68 rl = bytesInt(data);
69 bl = bytesInt(data);
70 System.out.printf("%d. Run:%d; Block:%s\n",y, rl, Items.BLOCKS.getKeyAt(bl));
71 for (int i=x; i<x+rl; i++) map[i][y] = bl;
72 }
73 }
74 } else throw new Exception("version mismatch");
75 return map;
76 }
78 public static int[][] getLoadedForeMap() {
79 return fMap;
80 }
82 public static int[][] getLoadedBackMap() {
83 return bMap;
84 }
86 public static void loadMap() {
87 try {
88 fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"));
89 bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"));
90 } catch (Exception e) {
91 Gdx.app.error("GameSaver",e.getMessage(),e);
92 }
93 }
95 public static GameProc load() {
96 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
97 GameProc gameProc = null;
98 try {
99 ObjectInputStream in = new ObjectInputStream(file.read());
100 int ver = in.readInt();
101 if (VERSION == ver) gameProc = (GameProc)in.readObject();
102 else throw new Exception("version mismatch");
103 in.close();
104 } catch (Exception e) {
105 Gdx.app.error("GameSaver",e.getMessage(),e);
107 gameProc.world = new GameWorld();
108 gameProc.world.load();
109 gameProc.physics = new GamePhysics(gameProc);
110 gameProc.resetRenderer();
111 fMap = null;
112 bMap = null;
113 return gameProc;
116 public static void save(GameProc gameProc) {
117 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/");
118 file.mkdirs();
119 file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
120 try {
121 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
122 out.writeInt(VERSION);
123 out.writeObject(gameProc);
124 out.close();
125 } catch (Exception e) {
126 e.printStackTrace();
128 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap());
129 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap());
132 public static boolean exists() {
133 return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav").exists() &&
134 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
135 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());