-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.cpp
220 lines (208 loc) · 6.35 KB
/
deck.cpp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "deck.h"
Deck::Deck(QString *base) : baseDir(base) {}
Deck::~Deck() {
for (int i = tiles.size() - 1; i >= 0; --i) {
delete tiles.at(i);
tiles.at(i) = NULL;
}
for (map<QString, QSvgRenderer*>::iterator it = baseImages.begin(); it != baseImages.end(); ++it) {
delete it->second;
}
}
void Deck::loadTiles(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
cerr << "File not found: " << filename.toStdString() << endl;
return;
}
Tile *tile;
TileImage *image;
QXmlStreamReader reader;
bool subs[16];
fill_n(subs, 16, false);
bool ok;
reader.setDevice(&file);
reader.readNext();
while (!reader.isEndDocument()) {
QString name = reader.name().toString();
QXmlStreamAttributes attrs = reader.attributes();
ImageLayer *layer;
if (name == "tile") {
// We've reached a <tile> element, and create a new tile.
if (reader.isStartElement()) {
tile = new Tile(); // Deleted in destructor or upon failed sanity check
image = tile->getImageContainer();
}
// We've reached a </tile> element, and can push the tile into the deck (after sanity check).
else if (reader.isEndElement()) {
bool good = true;
for (int i = 0; i < 16; ++i) {
if (!subs[i]) {
cerr << "A tile must contain 16 subtiles. Failed on " << i << " Check your XML file." << endl;
good = false;
break;
}
}
if (good) {
tiles.push_back(tile);
}
else {
delete tile;
}
fill_n(subs, 16, false);
}
}
else if (name == "graphics" && reader.isStartElement()) {
for (int i = 0; i < attrs.size(); ++i) {
QString attrName = attrs.at(i).name().toString();
QString attrVal = attrs.at(i).value().toString();
if (attrName == "img") {
QSvgRenderer *rend;
// Search for the image - we may already have loaded it.
map<QString, QSvgRenderer*>::const_iterator it = baseImages.find(attrVal);
if (it == baseImages.end()) {
// If not, load it now.
QString tempStr(*baseDir);
tempStr.append("/");
tempStr.append(attrVal);
rend = new QSvgRenderer(tempStr); // Deleted in destructor
baseImages[attrVal] = rend;
}
else {
rend = it->second;
}
layer = new ImageLayer(); // Will be handed to tileimage, and deleted there
layer->renderer = rend;
image->addImageLayer(layer);
}
else if (attrName == "position") {
QStringList strList = attrVal.split(",");
int x = strList.value(0).toInt(&ok);
if (!ok) {
cerr << "Casting position X " << attrs.at(i).value().toString().toStdString() << " to integer failed. Check your XML file." << endl;
}
int y = strList.value(1).toInt(&ok);
if (!ok) {
cerr << "Casting position Y " << attrs.at(i).value().toString().toStdString() << " to integer failed. Check your XML file." << endl;
}
layer->x = x;
layer->y = y;
}
else if (attrName == "rotation") {
int rot = attrVal.toInt(&ok);
if (!ok) {
cerr << "Casting rotation " << attrs.at(i).value().toString().toStdString() << " to integer failed. Check your XML file." << endl;
}
layer->rotation = rot;
}
else if (attrName == "scale") {
QStringList strList = attrVal.split("%");
double scale = strList.value(0).toDouble(&ok);
if (!ok) {
cerr << "Casting scale " << attrs.at(i).value().toString().toStdString() << " to double failed. Check your XML file." << endl;
}
scale /= 100;
layer->scale = scale;
}
else if (attrName == "stationary") {
layer->stationary = (attrVal.toLower() == "true");
}
}
}
else if (name == "subtile" && reader.isStartElement()) {
int pos = -1;
Terrain t = undefined;
TerrainAddon ta = none;
for (int i = 0; i < attrs.size(); ++i) {
QString attrName = attrs.at(i).name().toString();
QString attrVal = attrs.at(i).value().toString();
if (attrName == "position") {
pos = attrs.at(i).value().toInt(&ok);
if (!ok) {
cerr << "Casting position " << attrs.at(i).value().toString().toStdString() << " to integer failed. Check your XML file." << endl;
}
}
else if (attrName == "terrain") {
t = (terrainMap()[attrs.at(i).value().toString().toStdString()]);
}
else if (attrName == "addon") {
ta = (terrainAddonMap()[attrs.at(i).value().toString().toStdString()]);
}
}
if (pos < 0 || pos > 15 || t == undefined) {
cerr << "Position must be in range 0-15, and terrain must not be undefined. Check your XML file." << endl;
}
else {
tile->addSubtile(new Subtile(t, ta, tile, pos), pos);
subs[pos] = true;
}
}
else if (name == "connection" && reader.isStartElement()) {
int to = -1;
int from = -1;
for (int i = 0; i < attrs.size(); ++i) {
QString attrName = attrs.at(i).name().toString();
if (attrName == "from") {
from = attrs.at(i).value().toInt(&ok);
if (!ok) {
cerr << "Casting " << attrs.at(i).value().toString().toStdString() << " to integer failed. Check your XML file." << endl;
}
}
else if (attrName == "to") {
to = attrs.at(i).value().toInt(&ok);
if (!ok) {
cerr << "Casting " << attrs.at(i).value().toString().toStdString() << " to integer failed. Check your XML file." << endl;
}
}
}
if (from < 0 || from > 15 || to < 0 || to > 15) {
cerr << "Values for 'from' and 'to' must be in range 0-15. Check your XML file." << endl;
}
else {
tile->connectSubtiles(from, to);
}
}
reader.readNext();
}
}
void Deck::shuffleDeck() {
shuffleDeck(0, tiles.size() - 1);
}
void Deck::shuffleDeck(int from) {
shuffleDeck(from, tiles.size() - 1);
}
void Deck::shuffleDeck(int from, int to) {
if (from >= to || to >= tiles.size()) {
return;
}
srand(time(NULL));
int random;
for (int i = to; i >= from; --i) {
random = rand() % (to - from);
swapTiles(i, random + from);
}
}
void Deck::eraseTile(int num) {
if (num < 0 || num >= tiles.size()) {
return;
}
delete tiles.at(num);
tiles.erase(tiles.begin() + num);
}
void Deck::swapTiles(int num1, int num2) {
Tile *temp = tiles.at(num1);
tiles.at(num1) = tiles.at(num2);
tiles.at(num2) = temp;
}
void Deck::mixIntoDeck(int num) {
srand(time(NULL));
int diff = tiles.size() - num - 1;
int random = (rand() % diff) + 1;
swapTiles(num, random);
}
int Deck::getSize() {
return tiles.size();
}
Tile* Deck::getTile(int position) {
return tiles.at(position);
}