forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockidentifier.cpp
275 lines (234 loc) · 7.71 KB
/
blockidentifier.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/** Copyright (c) 2013, Sean Kasun */
#include <QDebug>
#include <QtWidgets/QMessageBox>
#include <assert.h>
#include <cmath>
#include "./blockidentifier.h"
#include "./json.h"
static BlockInfo unknownBlock;
BlockInfo::BlockInfo()
: variants(false)
, transparent(false)
, liquid(false)
, rendernormal(true)
, providepower(false)
, spawninside(false)
, grass(false)
, foliage(false) {}
bool BlockInfo::hasVariants() const {
return this->variants;
}
bool BlockInfo::isOpaque() const {
return !(this->transparent);
}
bool BlockInfo::isLiquid() const {
return this->liquid;
}
bool BlockInfo::doesBlockHaveSolidTopSurface() const {
if (this->isOpaque() && this->renderAsNormalBlock()) return true;
if (this->hopper) return true;
return false;
}
bool BlockInfo::isBlockNormalCube() const {
return this->isOpaque() && this->renderAsNormalBlock() &&
!this->canProvidePower();
}
bool BlockInfo::renderAsNormalBlock() const {
return this->rendernormal;
}
bool BlockInfo::canProvidePower() const {
return this->providepower;
}
void BlockInfo::setName(const QString & newname) {
// set name
name = newname;
// precompute mob spawning conditions
bedrock = this->name.contains("Bedrock", Qt::CaseInsensitive);
hopper = this->name.contains("Hopper", Qt::CaseInsensitive);
snow = this->name.contains("Snow", Qt::CaseInsensitive);
// precompute biome based watercolormodifier
water = this->name.contains("Water", Qt::CaseInsensitive);
}
const QString & BlockInfo::getName() { return name; }
bool BlockInfo::isBedrock() { return bedrock; }
bool BlockInfo::isHopper() { return hopper; }
bool BlockInfo::isSnow() { return snow; }
bool BlockInfo::biomeWater() { return water; }
bool BlockInfo::biomeGrass() { return grass; }
bool BlockInfo::biomeFoliage() { return foliage; }
void BlockInfo::setBiomeGrass(bool value) { grass = value; }
void BlockInfo::setBiomeFoliage(bool value) { foliage = value; }
// --------- --------- --------- ---------
// BlockIdentifier
// --------- --------- --------- ---------
BlockIdentifier::BlockIdentifier() {
for (int i = 0; i < 16; i++)
unknownBlock.colors[i] = 0xff00ff;
unknownBlock.alpha = 1.0;
unknownBlock.setName("Unknown Block");
}
BlockIdentifier::~BlockIdentifier() {
for (int i = 0; i < packs.length(); i++) {
for (int j = 0; j < packs[i].length(); j++)
delete packs[i][j];
}
}
BlockIdentifier& BlockIdentifier::Instance() {
static BlockIdentifier singleton;
return singleton;
}
BlockInfo &BlockIdentifier::getBlockInfo(uint hid) {
if (blocks.contains(hid)) {
return *blocks[hid];
}
// no blocks at all found.. dammit
return unknownBlock;
}
bool BlockIdentifier::hasBlockInfo(uint hid) {
return blocks.contains(hid);
}
void BlockIdentifier::enableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = true;
}
void BlockIdentifier::disableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = false;
}
int BlockIdentifier::addDefinitions(JSONArray *defs, int pack) {
if (pack == -1) {
pack = packs.length();
packs.append(QList<BlockInfo*>());
}
int len = defs->length();
for (int i = 0; i < len; i++)
parseDefinition(dynamic_cast<JSONObject *>(defs->at(i)), NULL, pack);
return pack;
}
void BlockIdentifier::parseDefinition(JSONObject *b, BlockInfo *parent,
int pack) {
BlockInfo *block = new BlockInfo();
// int id;
// if (parent == NULL) {
// id = b->at("id")->asNumber();
// } else {
// id = parent->id;
// int data = b->at("data")->asNumber();
// id |= data << 12;
// }
// block->id = id;
// name of Block
QString name;
if (b->has("name"))
name = b->at("name")->asString();
else if (parent != NULL)
name = parent->getName();
else
name = "Unknown";
block->setName(name);
block->enabled = true;
// optional Block State
if (b->has("blockstate"))
block->blockstate = b->at("blockstate")->asString();
if (b->has("transparent")) {
block->transparent = b->at("transparent")->asBool();
block->rendernormal = false; // for most cases except the following
if (b->has("rendercube"))
block->rendernormal = b->at("rendercube")->asBool();
block->spawninside = false; // for most cases except the following
if (b->has("spawninside"))
block->spawninside = b->at("spawninside")->asBool();
} else if (parent != NULL) {
block->transparent = parent->transparent;
block->rendernormal = parent->rendernormal;
block->spawninside = parent->spawninside;
} else {
block->transparent = false;
block->rendernormal = true;
block->spawninside = false;
}
// generic attributes
if (b->has("liquid"))
block->liquid = b->at("liquid")->asBool();
else if (parent != NULL)
block->liquid = parent->liquid;
else
block->liquid = false;
if (b->has("canProvidePower"))
block->providepower = b->at("canProvidePower")->asBool();
else if (parent != NULL)
block->providepower = parent->providepower;
else
block->providepower = false;
if (b->has("alpha"))
block->alpha = b->at("alpha")->asNumber();
else if (parent != NULL)
block->alpha = parent->alpha;
else
block->alpha = 1.0;
QColor blockcolor;
if (b->has("color")) {
QString colorname = b->at("color")->asString();
if (colorname.length() == 6) {
// check if this is an old color definition with missing '#'
bool ok;
colorname.toInt(&ok,16);
if (ok)
colorname.push_front('#');
}
blockcolor.setNamedColor(colorname);
assert(blockcolor.isValid());
} else if (parent != NULL) {
// copy brightest color from parent
blockcolor = parent->colors[15];
} else {
// use hashed by name instead
quint32 hue = qHash(block->getName());
blockcolor.setHsv(hue % 360, 255, 255);
}
// pre-calculate light spectrum
for (int i = 0; i < 16; i++) {
// calculate light attenuation similar to Minecraft
// except base 90% here, were Minecraft is using 80% per level
double light_factor = pow(0.90,15-i);
block->colors[i].setRgb(light_factor*blockcolor.red(),
light_factor*blockcolor.green(),
light_factor*blockcolor.blue(),
255*block->alpha );
}
// biome dependant color
if (b->has("biomeGrass"))
block->setBiomeGrass( b->at("biomeGrass")->asBool() );
if (b->has("biomeFoliage"))
block->setBiomeFoliage( b->at("biomeFoliage")->asBool() );
// variants due to block_state difference
if (b->has("variants")) {
block->variants = true;
JSONArray *variants = dynamic_cast<JSONArray *>(b->at("variants"));
int vlen = variants->length();
for (int j = 0; j < vlen; j++)
parseDefinition(dynamic_cast<JSONObject *>(variants->at(j)), block, pack);
}
uint hid = qHash(name);
if (!block->blockstate.isEmpty())
hid = qHash(name + ":" + block->blockstate);
if (blocks.contains(hid)) {
// this will only trigger during development of vanilla_blocks.json
// and prevents generating a wrong definition file
QMessageBox::warning((QWidget*)(NULL),
"Error loading Block definition: " + name,
"Failed to add Block from definition file, as it might be a duplicate\nor generates the same hash as an already existing Block." ,
QMessageBox::Cancel, QMessageBox::Cancel);
}
blocks.insert(hid, block);
packs[pack].append(block);
// we need this ugly code to allow mob spawn detection
// todo: rework mob spawn highlight
if (block->getName() == "minecraft:air") {
blocks.insert(0, block);
}
}