-
Notifications
You must be signed in to change notification settings - Fork 746
/
GameSave.h
123 lines (104 loc) · 2.91 KB
/
GameSave.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef The_Powder_Toy_GameSave_h
#define The_Powder_Toy_GameSave_h
#include <vector>
#include <string>
#include "Config.h"
#include "Misc.h"
#include "bson/BSON.h"
#include "json/json.h"
#include "simulation/Sign.h"
#include "simulation/Particle.h"
//using namespace std;
struct ParseException: public std::exception {
enum ParseResult { OK = 0, Corrupt, WrongVersion, InvalidDimensions, InternalError, MissingElement };
std::string message;
ParseResult result;
public:
ParseException(ParseResult result, std::string message_): message(message_), result(result) {}
const char * what() const throw()
{
return message.c_str();
}
~ParseException() throw() {}
};
class GameSave
{
public:
int blockWidth, blockHeight;
bool fromNewerVersion;
bool hasAmbientHeat;
//Simulation data
//int ** particleMap;
int particlesCount;
Particle * particles;
unsigned char ** blockMap;
float ** fanVelX;
float ** fanVelY;
float ** pressure;
float ** velocityX;
float ** velocityY;
float ** ambientHeat;
//Simulation Options
bool waterEEnabled;
bool legacyEnable;
bool gravityEnable;
bool aheatEnable;
bool paused;
int gravityMode;
int airMode;
int edgeMode;
//Signs
std::vector<sign> signs;
//Element palette
typedef std::pair<std::string, int> PaletteItem;
std::vector<PaletteItem> palette;
// author information
Json::Value authors;
GameSave();
GameSave(GameSave & save);
GameSave(int width, int height);
GameSave(char * data, int dataSize);
GameSave(std::vector<char> data);
GameSave(std::vector<unsigned char> data);
~GameSave();
void setSize(int width, int height);
char * Serialise(unsigned int & dataSize);
std::vector<char> Serialise();
void Transform(matrix2d transform, vector2d translate);
void Expand();
void Collapse();
bool Collapsed();
inline GameSave& operator << (Particle v)
{
if(particlesCount<NPART && v.type)
{
particles[particlesCount++] = v;
}
return *this;
}
inline GameSave& operator << (sign v)
{
if(signs.size()<MAXSIGNS && v.text.length())
signs.push_back(v);
return *this;
}
private:
bool expanded;
bool hasOriginalData;
std::vector<char> originalData;
void InitData();
void InitVars();
void CheckBsonFieldUser(bson_iterator iter, const char *field, unsigned char **data, unsigned int *fieldLen);
void CheckBsonFieldBool(bson_iterator iter, const char *field, bool *flag);
void CheckBsonFieldInt(bson_iterator iter, const char *field, int *setting);
template <typename T> T ** Allocate2DArray(int blockWidth, int blockHeight, T defaultVal);
template <typename T> void Deallocate2DArray(T ***array, int blockHeight);
void dealloc();
void read(char * data, int dataSize);
void readOPS(char * data, int dataLength);
void readPSv(char * data, int dataLength);
char * serialiseOPS(unsigned int & dataSize);
void ConvertJsonToBson(bson *b, Json::Value j, int depth = 0);
void ConvertBsonToJson(bson_iterator *b, Json::Value *j, int depth = 0);
};
#endif