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
);
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
);
31 private static void saveMap(FileHandle file
, int[][] map
) {
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
++) {
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
));
55 private static int[][] loadMap(FileHandle file
) throws Exception
{
57 int ver
, width
, height
;
59 byte[] data
= file
.readBytes();
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
) {
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
;
74 } else throw new Exception("version mismatch");
78 public static int[][] getLoadedForeMap() {
82 public static int[][] getLoadedBackMap() {
86 public static void loadMap() {
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
);
95 public static GameProc
load() {
96 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
97 GameProc gameProc
= null;
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");
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();
116 public static void save(GameProc gameProc
) {
117 FileHandle file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/");
119 file
= Gdx
.files
.absolute(CaveGame
.GAME_FOLDER
+"/saves/game.sav");
121 ObjectOutputStream out
= new ObjectOutputStream(file
.write(false));
122 out
.writeInt(VERSION
);
123 out
.writeObject(gameProc
);
125 } catch (Exception e
) {
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());