forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biomeidentifier.cpp
314 lines (271 loc) · 9.23 KB
/
biomeidentifier.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
Copyright (c) 2013, Sean Kasun
Copyright (c) 2016, EtlamGit
*/
#include <assert.h>
#include <cmath>
#include "./biomeidentifier.h"
#include "./json.h"
#include "./clamp.h"
// --------- --------- --------- ---------
// BiomeInfo
// --------- --------- --------- ---------
static BiomeInfo unknownBiome;
BiomeInfo::BiomeInfo()
: id(-1)
, name("Unknown Biome")
, enabled(false)
, watermodifier(255,255,255)
, enabledwatermodifier(false)
, alpha(1.0)
, temperature(0.5)
, humidity(0.5)
{}
// Biome based Grass and Foliage color is based on an interpolation in special
// textures provided in the Minecraft assets. It is also possible to do the
// interpolation based only on the color values at the threee corners of that
// color triangle.
// .\assets\minecraft\textures\colormap\grass.png
// last checked with Minecraft 1.11.2
BiomeInfo::T_BiomeCorner BiomeInfo::grassCorners[3] =
{
{ 191, 183, 85 }, // lower left - temperature = 1.0
{ 128, 180, 151 }, // lower right - temperature = 0.0
{ 71, 205, 51 } // upper left - humidity = 1.0
};
// .\assets\minecraft\textures\colormap\foliage.png
// last checked with Minecraft 1.11.2
BiomeInfo::T_BiomeCorner BiomeInfo::foliageCorners[3] =
{
{ 174, 164, 42 }, // lower left - temperature = 1.0
{ 96, 161, 123 }, // lower right - temperature = 0.0
{ 26, 191, 0 } // upper left - humidity = 1.0
};
/*
This method is inspired from Mineways, Copyright (c) 2014, Eric Haines
https://github.com/erich666/Mineways/blob/master/Win/biomes.h
*/
// do an interpolation inside the color triangle given by the corners
// temperature: is the base value for the current Biome
// humidity: is the base value for the current Biome
// elevation: is number of meters above a height of 64
QColor BiomeInfo::getBiomeColor( float temperature, float humidity, int elevation, T_BiomeCorner *corners )
{
// check elevation
// todo: probably negative values are allowed with latest Minecraft versions
// elevation = (elevation < 0) ? 0 : elevation;
// get local temperature and humidity
// perlin noise generator omitted due to performance reasons
temperature = std::clamp( temperature - (float)elevation*0.00166667f, 0.0f, 1.0f );
humidity = std::clamp( humidity, 0.0f, 1.0f );
humidity *= temperature; // scale by temperature to stay in triangle
// lambda values for barycentric coordinates
float lambda[3];
lambda[0] = temperature - humidity;
lambda[1] = 1.0f - temperature;
lambda[2] = humidity;
float red = 0.0f, green = 0.0f, blue = 0.0f;
for ( int i = 0; i < 3; i++ )
{
red += lambda[i] * corners[i].red;
green += lambda[i] * corners[i].green;
blue += lambda[i] * corners[i].blue;
}
int r = (int)std::clamp( red, 0.0f, 255.0f );
int g = (int)std::clamp( green, 0.0f, 255.0f );
int b = (int)std::clamp( blue, 0.0f, 255.0f );
return QColor::fromRgb(r,g,b);
}
QColor BiomeInfo::mixColor( QColor colorizer, QColor blockcolor )
{
// get hue components
float hueC = colorizer.hslHueF();
float hueB = blockcolor.hslHueF(); // monochrome blocks result in -1
// get saturation components
float satC = colorizer.hslSaturationF();
float satB = blockcolor.hslSaturationF();
// get lightness components
float ligC = colorizer.lightnessF();
float ligB = blockcolor.lightnessF();
// mix final color
float hue = hueC;
float sat = satC;
if ((satB != 0) && (hueB != -1.0f)) {
// when block contains some color component
hue = (hueC + hueB ) / 2.0f;
sat = (satC + satB ) / 2.0f;
}
//float lig = std::clamp( ligC + ligB - 0.5f, 0.0f, 1.0f ); // more brightness contrast
float lig = (ligC + ligB ) / 2.0f;
return QColor::fromHslF( hue, sat, lig );
}
QColor BiomeInfo::getBiomeGrassColor( QColor blockcolor, int elevation ) const
{
QColor colorizer;
// remove variants from ID
int id = this->id & 0x7f;
// swampland
if (id == 6) {
// perlin noise generator omitted due to performance reasons
// otherwise the random temperature distribution selects
// (below -0.1°C) 4C.76.3C or 6A.70.39 (above -0.1°C)
colorizer = QColor::fromRgb(0x6a,0x70,0x39); // hard wired
}
// roofed forest
else if (id == 29) {
colorizer = getBiomeColor( this->temperature, this->humidity, elevation, grassCorners );
// average with 0x28340A
colorizer.setRed ( (colorizer.red() + 0x28)>>1 );
colorizer.setGreen( (colorizer.green() + 0x34)>>1 );
colorizer.setBlue ( (colorizer.blue() + 0x0A)>>1 );
}
// mesa
else if ((id == 37) || (id == 38) || (id == 39)) {
colorizer = QColor::fromRgb(0x90,0x81,0x4d); // hard wired
} else
// standard way
colorizer = getBiomeColor( this->temperature, this->humidity, elevation, grassCorners );
return mixColor( colorizer, blockcolor );
}
QColor BiomeInfo::getBiomeFoliageColor( QColor blockcolor, int elevation ) const
{
QColor colorizer;
// remove variants from ID
int id = this->id & 0x7f;
// swampland
if (id == 6) {
colorizer = QColor::fromRgb(0x6a,0x70,0x39); // hard wired
}
// mesa
else if ((id == 37) || (id == 38) || (id == 39)) {
colorizer = QColor::fromRgb(0x9e,0x81,0x4d); // hard wired
} else
// standard way
colorizer = getBiomeColor( this->temperature, this->humidity, elevation, foliageCorners );
return mixColor( colorizer, blockcolor );
}
QColor BiomeInfo::getBiomeWaterColor( QColor watercolor ) const
{
if (this->enabledwatermodifier) {
// calculate modified color components
int r = (int)( watercolor.red() * watermodifier.redF() );
int g = (int)( watercolor.green() * watermodifier.greenF() );
int b = (int)( watercolor.blue() * watermodifier.blueF() );
// return combined modified color
return QColor::fromRgb(r, g, b);
} else
return watercolor;
}
// --------- --------- --------- ---------
// BiomeIdentifier
// --------- --------- --------- ---------
BiomeIdentifier::BiomeIdentifier() {
unknownBiome.name = "Unknown";
}
BiomeIdentifier::~BiomeIdentifier() {
for (int i = 0; i < packs.length(); i++) {
for (int j = 0; j < packs[i].length(); j++)
delete packs[i][j];
}
}
BiomeIdentifier& BiomeIdentifier::Instance() {
static BiomeIdentifier singleton;
return singleton;
}
const BiomeInfo &BiomeIdentifier::getBiome(int biome) const {
auto itr = biomes.find(biome);
if (itr == biomes.end()) {
return unknownBiome;
} else {
return *(*itr);
}
}
void BiomeIdentifier::enableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = true;
updateBiomeDefinition();
}
void BiomeIdentifier::disableDefinitions(int pack) {
if (pack < 0) return;
int len = packs[pack].length();
for (int i = 0; i < len; i++)
packs[pack][i]->enabled = false;
updateBiomeDefinition();
}
int BiomeIdentifier::addDefinitions(JSONArray *defs, int pack) {
if (pack == -1) {
pack = packs.length();
packs.append(QList<BiomeInfo *>());
}
int len = defs->length();
for (int i = 0; i < len; i++) {
JSONObject *b = dynamic_cast<JSONObject *>(defs->at(i));
int id = b->at("id")->asNumber();
BiomeInfo *biome = new BiomeInfo();
biome->enabled = true;
biome->id = id;
if (b->has("name"))
biome->name = b->at("name")->asString();
// check for "alpha" (0: transparent / 1: saturated)
// probably never used
if (b->has("alpha"))
biome->alpha = b->at("alpha")->asNumber();
// get temperature definition
if (b->has("temperature"))
biome->temperature = b->at("temperature")->asNumber();
// get humidity definition
if (b->has("humidity"))
biome->humidity = b->at("humidity")->asNumber();
// get watermodifier definition
if (b->has("watermodifier")) {
biome->watermodifier.setNamedColor(b->at("watermodifier")->asString());
biome->enabledwatermodifier = true;
assert(biome->watermodifier.isValid());
}
// get color definition
QColor biomecolor;
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('#');
}
biomecolor.setNamedColor(colorname);
assert(biomecolor.isValid());
} else {
// use hashed by name instead
quint32 hue = qHash(biome->name);
biomecolor.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);
biome->colors[i].setRgb(light_factor*biomecolor.red(),
light_factor*biomecolor.green(),
light_factor*biomecolor.blue(),
255*biome->alpha );
}
packs[pack].append(biome);
}
updateBiomeDefinition();
return pack;
}
void BiomeIdentifier::updateBiomeDefinition()
{
// start from scratch
biomes.clear();
for (int pack = 0; pack < packs.length(); pack++)
for (int i = 0; i < packs[pack].length(); i++) {
BiomeInfo *bi = packs[pack][i];
if (bi->enabled) {
biomes[bi->id] = bi;
}
}
}