Botcraft 1.21.4
Loading...
Searching...
No Matches
Biome.cpp
Go to the documentation of this file.
1#include <array>
2#include <algorithm>
3
5
6namespace Botcraft
7{
8 namespace
9 {
10 // The corners of the triangle defining grass and leaves colors in the following order:
11 // upper left, lower left, lower right
12 using uint3 = std::array<unsigned int, 3>;
13 static constexpr std::array<uint3, 3> grass_color_triangle = { uint3{ 71, 205, 51 }, uint3{ 191, 183, 85 }, uint3{ 128, 180, 151 } };
14 static constexpr std::array<uint3, 3> leaves_color_triangle = { uint3{ 26, 191, 0 }, uint3{ 174, 164, 42 }, uint3{ 96, 161, 123 } };
15
16 static constexpr int sea_level = 62;
17 }
18
19 Biome::Biome(const std::string& name_, const float temperature_,
20 const float rainfall_, const BiomeType biome_type_)
21 {
22 name = name_;
23 temperature = temperature_;
24 rainfall = rainfall_;
25 biome_type = biome_type_;
26
29 }
30
32 {
33 }
34
35 const std::string& Biome::GetName() const
36 {
37 return name;
38 }
39
40 unsigned int Biome::GetColorMultiplier(const int height, const bool is_grass) const
41 {
42 if (height <= sea_level)
43 {
44 if (is_grass)
45 {
46 return default_grass;
47 }
48 else
49 {
50 return default_leaves;
51 }
52 }
53
54
55 switch (biome_type)
56 {
58 {
59 const int relativ_height = std::max(0, height - sea_level);
60 return ComputeColorTriangle(relativ_height, is_grass);
61 }
63 {
64 // We do not use the Perlin Noise as specified in the wiki, but rather
65 // we choose to select the color of temperature > -0.1
66 // Maybe one day I'll take the time to implement a Perlin Noise function
67 // somewhere
68 return 0xFF6A7039;
69 }
71 {
72 const int relativ_height = std::max(0, height - sea_level);
73 const unsigned int base_color = ComputeColorTriangle(relativ_height, is_grass);
74 // Average the base color with the hardcoded value
75 // Average formula from https://www.compuphase.com/graphic/scale3.htm
76 return ((((base_color ^ 0xFF28340A) & 0xFFFEFEFE) >> 1) + (base_color & 0xFF28340A));
77
78 }
80 return is_grass ? 0xFF90814D : 0xFF9E814D;
81#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
83 return is_grass ? 0xFF778373 : 0xFF878D76;
84#endif
85 default:
86 return 0xFFFFFFFF;
87 }
88 return 0xFFFFFFFF;
89 }
90
91 unsigned int Biome::GetWaterColorMultiplier() const
92 {
93#if PROTOCOL_VERSION < 393 /* < 1.13 */
94 switch (biome_type)
95 {
97 // The hardcoded value for the water in swamps
98 return 0xFFE0FFAE;
99 default:
100 return 0xFFFFFFFF;
101 }
102#else
103 switch (biome_type)
104 {
105 // I couldn't find these values anywhere so
106 // I tried to find something which works
107 case BiomeType::Swamp:
108 return 0xFF4F4F2F; // dark green grayish
110 return 0xFFD0E040; // turquoise
111 case BiomeType::LukewarmOcean: // light tealish
112 return 0xFFb2b964;
114 return 0xFF7B3C24; // dark indigoish
116 return 0xFF341930; // dark purple
117#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
119 return 0xFF76889D;
120#endif
121 default:
122 return 0xFFD48717; // blue
123 }
124#endif
125 }
126
127 unsigned int Biome::ComputeColorTriangle(const int height, const bool is_grass) const
128 {
129 const float local_temperature = std::max(0.0f, std::min(1.0f, temperature - height * 0.0016667f));
130 const float local_rainfall = std::max(0.0f, std::min(1.0f, rainfall)) * local_temperature;
131
132 std::array<float, 3> triangle_coordinates;
133 triangle_coordinates[0] = local_rainfall;
134 triangle_coordinates[1] = local_temperature - local_rainfall;
135 triangle_coordinates[2] = 1.0f - local_temperature;
136
137 std::array<float, 3> color;
138 for (int i = 0; i < 3; ++i)
139 {
140 for (int j = 0; j < 3; ++j)
141 {
142 color[j] += triangle_coordinates[i] * (is_grass ? grass_color_triangle[i][j] : leaves_color_triangle[i][j]);
143 }
144 }
145
146 const unsigned char r = static_cast<unsigned char>(std::max(0.0f, std::min(255.0f, color[0])));
147 const unsigned char g = static_cast<unsigned char>(std::max(0.0f, std::min(255.0f, color[1])));
148 const unsigned char b = static_cast<unsigned char>(std::max(0.0f, std::min(255.0f, color[2])));
149 const unsigned char a = 255;
150
151 return ((a << 24) | (b << 16) | (g << 8) | r);
152 }
153} //Botcraft
unsigned int GetWaterColorMultiplier() const
Definition Biome.cpp:91
unsigned int default_grass
Definition Biome.hpp:47
BiomeType biome_type
Definition Biome.hpp:49
unsigned int GetColorMultiplier(const int height, const bool is_grass) const
Definition Biome.cpp:40
float rainfall
Definition Biome.hpp:46
unsigned int ComputeColorTriangle(const int height, const bool is_grass) const
Definition Biome.cpp:127
const std::string & GetName() const
Definition Biome.cpp:35
Biome(const std::string &name_, const float temperature_, const float rainfall_, const BiomeType biome_type_)
Definition Biome.cpp:19
float temperature
Definition Biome.hpp:45
std::string name
Definition Biome.hpp:44
unsigned int default_leaves
Definition Biome.hpp:48
BiomeType
Enum for biomes with special color processing.
Definition Biome.hpp:9