DEADSOFTWARE

Fix bugs
[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 rl=0;
47 bl=map[x][y];
48 }
49 rl++;
50 }
51 }
52 }
54 private static int[][] loadMap(FileHandle file) throws Exception {
55 int[][] map = null;
56 int ver, width, height;
57 int rl,bl;
58 byte[] data = file.readBytes();
59 readIndex = 0;
60 ver = bytesInt(data);
61 if (VERSION == ver) {
62 width = bytesInt(data);
63 height = bytesInt(data);
64 map = new int[width][height];
65 for (int y=0; y<height; y++) {
66 for (int x=0; x<width; x+=rl) {
67 rl = bytesInt(data);
68 bl = bytesInt(data);
69 for (int i=x; i<x+rl; i++) map[i][y] = bl;
70 }
71 }
72 } else throw new Exception("version mismatch");
73 return map;
74 }
76 public static int[][] getLoadedForeMap() {
77 return fMap;
78 }
80 public static int[][] getLoadedBackMap() {
81 return bMap;
82 }
84 public static void loadMap() {
85 try {
86 fMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"));
87 bMap = loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"));
88 } catch (Exception e) {
89 Gdx.app.error("GameSaver",e.getMessage(),e);
90 }
91 }
93 public static GameProc load() {
94 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
95 GameProc gameProc = null;
96 try {
97 ObjectInputStream in = new ObjectInputStream(file.read());
98 int ver = in.readInt();
99 if (VERSION == ver) gameProc = (GameProc)in.readObject();
100 else throw new Exception("version mismatch");
101 in.close();
102 } catch (Exception e) {
103 Gdx.app.error("GameSaver",e.getMessage(),e);
105 gameProc.world = new GameWorld();
106 gameProc.world.load();
107 gameProc.physics = new GamePhysics(gameProc);
108 gameProc.resetRenderer();
109 fMap = null;
110 bMap = null;
111 return gameProc;
114 public static void save(GameProc gameProc) {
115 FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/");
116 file.mkdirs();
117 file = Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav");
118 try {
119 ObjectOutputStream out = new ObjectOutputStream(file.write(false));
120 out.writeInt(VERSION);
121 out.writeObject(gameProc);
122 out.close();
123 } catch (Exception e) {
124 e.printStackTrace();
126 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav"), gameProc.world.getFullForeMap());
127 saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav"), gameProc.world.getFullBackMap());
130 public static boolean exists() {
131 return (Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/game.sav").exists() &&
132 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/foremap.sav").exists() &&
133 Gdx.files.absolute(CaveGame.GAME_FOLDER+"/saves/backmap.sav").exists());