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
;
7 import java
.io
.BufferedOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.ObjectInputStream
;
10 import java
.io
.ObjectOutputStream
;
11 import java
.nio
.ByteBuffer
;
13 public class GameSaver
{
15 private static final int VERSION
= 0;
17 private static int[][] fMap
, bMap
;
18 private static int readIndex
;
20 private static int bytesToInt(byte[] bytes
) {
21 ByteBuffer wrapped
= ByteBuffer
.wrap(bytes
);
22 int res
= wrapped
.getInt(readIndex
);
27 private static byte[] intToBytes(int i
) {
28 return ByteBuffer
.allocate(4).putInt(i
).array();
31 private static void saveMap(FileHandle file
, int[][] map
) throws IOException
{
33 int width
= map
.length
;
34 int height
= map
[0].length
;
35 BufferedOutputStream out
= new BufferedOutputStream(file
.write(false));
36 out
.write(intToBytes(VERSION
));
37 out
.write(intToBytes(width
));
38 out
.write(intToBytes(height
));
39 for (int y
=0; y
<map
[0].length
; y
++) {
42 for (int x
=0; x
<map
.length
; x
++) {
44 out
.write(intToBytes(rl
));
45 out
.write(intToBytes(bl
));
51 out
.write(intToBytes(rl
));
52 out
.write(intToBytes(bl
));
58 private static int[][] loadMap(FileHandle file
) throws Exception
{
60 int ver
, width
, height
;
62 byte[] data
= file
.readBytes();
64 ver
= bytesToInt(data
);
66 width
= bytesToInt(data
);
67 height
= bytesToInt(data
);
68 map
= new int[width
][height
];
69 for (int y
=0; y
<height
; y
++) {
70 for (int x
=0; x
<width
; x
+=rl
) {
71 rl
= bytesToInt(data
);
72 bl
= bytesToInt(data
);
73 for (int i
=x
; i
<x
+rl
; i
++) map
[i
][y
] = bl
;
76 } else throw new Exception("version mismatch");
80 public static int[][] getLoadedForeMap() {
84 public static int[][] getLoadedBackMap() {
88 public static void loadWorld() {
90 fMap
= loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/foremap.sav"));
91 bMap
= loadMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+ "/saves/backmap.sav"));
92 } catch (Exception e
) {
93 Gdx
.app
.error("GameSaver",e
.getMessage(),e
);
97 public static GameProc
load() {
98 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
99 GameProc gameProc
= null;
101 ObjectInputStream in
= new ObjectInputStream(file
.read());
102 int ver
= in
.readInt();
103 if (VERSION
== ver
) gameProc
= (GameProc
)in
.readObject();
104 else throw new Exception("version mismatch");
106 } catch (Exception e
) {
107 Gdx
.app
.error("GameSaver",e
.getMessage(),e
);
109 gameProc
.world
= new GameWorld();
110 gameProc
.world
.load();
111 gameProc
.physics
= new GamePhysics(gameProc
);
112 gameProc
.resetRenderer();
118 public static void save(GameProc gameProc
) {
119 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/");
121 file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
123 ObjectOutputStream out
= new ObjectOutputStream(file
.write(false));
124 out
.writeInt(VERSION
);
125 out
.writeObject(gameProc
);
127 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav"), gameProc
.world
.getFullForeMap());
128 saveMap(Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav"), gameProc
.world
.getFullBackMap());
129 } catch (Exception e
) {
134 public static boolean exists() {
135 return (Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav").exists() &&
136 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/foremap.sav").exists() &&
137 Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/backmap.sav").exists());