forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flatteningconverter.cpp
115 lines (97 loc) · 2.95 KB
/
flatteningconverter.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
/** Copyright (c) 2018, EtlamGit */
#include <QDebug>
#include <assert.h>
#include <cmath>
#include "./flatteningconverter.h"
#include "./json.h"
FlatteningConverter::FlatteningConverter() {}
FlatteningConverter::~FlatteningConverter() {}
FlatteningConverter& FlatteningConverter::Instance() {
static FlatteningConverter singleton;
return singleton;
}
//const BlockData * FlatteningConverter::getPalette() {
PaletteEntry * FlatteningConverter::getPalette() {
return palette;
}
void FlatteningConverter::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 FlatteningConverter::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 FlatteningConverter::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 FlatteningConverter::parseDefinition(
JSONObject *b,
int *parentID,
int pack) {
// get the ancient block ID
int bid, data(0);
if (parentID == NULL) {
bid = b->at("id")->asNumber();
} else {
bid = *parentID;
data = b->at("data")->asNumber();
bid |= data << 8;
}
// try to translate old block name into new flatname
QString flatname;
if (b->has("name")) {
flatname = "minecraft:" + b->at("name")->asString().toLower().replace(" ", "_");
} else if (parentID != NULL) {
flatname = palette[*parentID].name;
} else {
flatname = "Unknown";
}
// or use provided flatname instead
if (b->has("flatname"))
flatname = b->at("flatname")->asString();
palette[bid].name = flatname;
palette[bid].hid = qHash(palette[bid].name);
if ((parentID == NULL) && (data == 0)) {
// spread main block type for data == 0
for (int d=1; d<16; d++) {
int sid = bid | (d<<8);
palette[sid].name = flatname;
palette[sid].hid = palette[bid].hid;
}
}
// packs[pack].append(block);
// get optional mask value (or guess default)
int mask = 0;
if (b->has("mask")) {
mask = b->at("mask")->asNumber();
} else if (b->has("variants")) {
mask = 0x0f;
}
// recursive parsing of variants (with data)
if (b->has("variants")) {
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)), &bid, pack);
}
// spread variants in masked bid
for (int j = vlen; j < 16; j++) {
int id = bid | (j << 8);
int mid = bid | ((j & mask) << 8);
palette[id].name = palette[mid].name;
palette[id].hid = palette[mid].hid;
}
}
}