Skip to content

Commit cbb9301

Browse files
committed
Biomes: Add 'min_pos'/'max_pos' xyz biome limits
'y_min' and 'y_max' are still accepted for compatibility.
1 parent c7c03ad commit cbb9301

File tree

4 files changed

+47
-34
lines changed

4 files changed

+47
-34
lines changed

src/mapgen/mapgen.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ void MapgenBasic::generateBiomes(MgStoneType *mgstone_type,
693693

694694
if (is_stone_surface || is_water_surface) {
695695
// (Re)calculate biome
696-
biome = biomegen->getBiomeAtIndex(index, y);
696+
biome = biomegen->getBiomeAtIndex(index, v3s16(x, y, z));
697697

698698
if (biomemap[index] == BIOME_NONE && is_stone_surface)
699699
biomemap[index] = biome->index;
@@ -704,7 +704,7 @@ void MapgenBasic::generateBiomes(MgStoneType *mgstone_type,
704704
noise_filler_depth->result[index], 0.0f);
705705
depth_water_top = biome->depth_water_top;
706706
depth_riverbed = biome->depth_riverbed;
707-
biome_y_min = biome->y_min;
707+
biome_y_min = biome->min_pos.Y;
708708

709709
// Detect stone type for dungeons during every biome calculation.
710710
// If none detected the last selected biome stone is chosen.

src/mapgen/mg_biome.cpp

+29-19
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ BiomeManager::BiomeManager(Server *server) :
4646
b->depth_filler = -MAX_MAP_GENERATION_LIMIT;
4747
b->depth_water_top = 0;
4848
b->depth_riverbed = 0;
49-
b->y_min = -MAX_MAP_GENERATION_LIMIT;
50-
b->y_max = MAX_MAP_GENERATION_LIMIT;
49+
b->min_pos = v3s16(-MAX_MAP_GENERATION_LIMIT,
50+
-MAX_MAP_GENERATION_LIMIT, -MAX_MAP_GENERATION_LIMIT);
51+
b->max_pos = v3s16(MAX_MAP_GENERATION_LIMIT,
52+
MAX_MAP_GENERATION_LIMIT, MAX_MAP_GENERATION_LIMIT);
5153
b->heat_point = 0.0;
5254
b->humidity_point = 0.0;
5355
b->vertical_blend = 0;
@@ -106,7 +108,7 @@ float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity
106108

107109

108110
// For BiomeGen type 'BiomeGenOriginal'
109-
Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y)
111+
Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, v3s16 pos)
110112
{
111113
Biome *biome_closest = nullptr;
112114
Biome *biome_closest_blend = nullptr;
@@ -115,14 +117,17 @@ Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y
115117

116118
for (size_t i = 1; i < getNumObjects(); i++) {
117119
Biome *b = (Biome *)getRaw(i);
118-
if (!b || y > b->y_max + b->vertical_blend || y < b->y_min)
120+
if (!b ||
121+
pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
122+
pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
123+
pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
119124
continue;
120125

121126
float d_heat = heat - b->heat_point;
122127
float d_humidity = humidity - b->humidity_point;
123128
float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
124129

125-
if (y <= b->y_max) { // Within y limits of biome b
130+
if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
126131
if (dist < dist_min) {
127132
dist_min = dist;
128133
biome_closest = b;
@@ -133,10 +138,10 @@ Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y
133138
}
134139
}
135140

136-
mysrand(y + (heat + humidity) / 2);
141+
mysrand(pos.Y + (heat + humidity) / 2);
137142
if (biome_closest_blend &&
138143
myrand_range(0, biome_closest_blend->vertical_blend) >=
139-
y - biome_closest_blend->y_max)
144+
pos.Y - biome_closest_blend->max_pos.Y)
140145
return biome_closest_blend;
141146

142147
return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
@@ -206,7 +211,7 @@ Biome *BiomeGenOriginal::calcBiomeAtPoint(v3s16 pos) const
206211
NoisePerlin2D(&m_params->np_humidity, pos.X, pos.Z, m_params->seed) +
207212
NoisePerlin2D(&m_params->np_humidity_blend, pos.X, pos.Z, m_params->seed);
208213

209-
return calcBiomeFromNoise(heat, humidity, pos.Y);
214+
return calcBiomeFromNoise(heat, humidity, pos);
210215
}
211216

212217

@@ -226,13 +231,15 @@ void BiomeGenOriginal::calcBiomeNoise(v3s16 pmin)
226231
}
227232

228233

229-
biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap)
234+
biome_t *BiomeGenOriginal::getBiomes(s16 *heightmap, v3s16 pmin)
230235
{
231-
for (s32 i = 0; i != m_csize.X * m_csize.Z; i++) {
236+
for (s16 zr = 0; zr < m_csize.Z; zr++)
237+
for (s16 xr = 0; xr < m_csize.X; xr++) {
238+
s32 i = zr * m_csize.X + xr;
232239
Biome *biome = calcBiomeFromNoise(
233240
noise_heat->result[i],
234241
noise_humidity->result[i],
235-
heightmap[i]);
242+
v3s16(pmin.X + xr, heightmap[i], pmin.Z + zr));
236243

237244
biomemap[i] = biome->index;
238245
}
@@ -245,20 +252,20 @@ Biome *BiomeGenOriginal::getBiomeAtPoint(v3s16 pos) const
245252
{
246253
return getBiomeAtIndex(
247254
(pos.Z - m_pmin.Z) * m_csize.X + (pos.X - m_pmin.X),
248-
pos.Y);
255+
pos);
249256
}
250257

251258

252-
Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, s16 y) const
259+
Biome *BiomeGenOriginal::getBiomeAtIndex(size_t index, v3s16 pos) const
253260
{
254261
return calcBiomeFromNoise(
255262
noise_heat->result[index],
256263
noise_humidity->result[index],
257-
y);
264+
pos);
258265
}
259266

260267

261-
Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) const
268+
Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, v3s16 pos) const
262269
{
263270
Biome *biome_closest = nullptr;
264271
Biome *biome_closest_blend = nullptr;
@@ -267,14 +274,17 @@ Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) c
267274

268275
for (size_t i = 1; i < m_bmgr->getNumObjects(); i++) {
269276
Biome *b = (Biome *)m_bmgr->getRaw(i);
270-
if (!b || y > b->y_max + b->vertical_blend || y < b->y_min)
277+
if (!b ||
278+
pos.Y < b->min_pos.Y || pos.Y > b->max_pos.Y + b->vertical_blend ||
279+
pos.X < b->min_pos.X || pos.X > b->max_pos.X ||
280+
pos.Z < b->min_pos.Z || pos.Z > b->max_pos.Z)
271281
continue;
272282

273283
float d_heat = heat - b->heat_point;
274284
float d_humidity = humidity - b->humidity_point;
275285
float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
276286

277-
if (y <= b->y_max) { // Within y limits of biome b
287+
if (pos.Y <= b->max_pos.Y) { // Within y limits of biome b
278288
if (dist < dist_min) {
279289
dist_min = dist;
280290
biome_closest = b;
@@ -288,11 +298,11 @@ Biome *BiomeGenOriginal::calcBiomeFromNoise(float heat, float humidity, s16 y) c
288298
// Carefully tune pseudorandom seed variation to avoid single node dither
289299
// and create larger scale blending patterns similar to horizontal biome
290300
// blend.
291-
mysrand(y + (heat + humidity) / 2);
301+
mysrand(pos.Y + (heat + humidity) / 2);
292302

293303
if (biome_closest_blend &&
294304
myrand_range(0, biome_closest_blend->vertical_blend) >=
295-
y - biome_closest_blend->y_max)
305+
pos.Y - biome_closest_blend->max_pos.Y)
296306
return biome_closest_blend;
297307

298308
return (biome_closest) ? biome_closest : (Biome *)m_bmgr->getRaw(BIOME_NONE);

src/mapgen/mg_biome.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class Biome : public ObjDef, public NodeResolver {
6363
s16 depth_water_top;
6464
s16 depth_riverbed;
6565

66-
s16 y_min;
67-
s16 y_max;
66+
v3s16 min_pos;
67+
v3s16 max_pos;
6868
float heat_point;
6969
float humidity_point;
7070
s16 vertical_blend;
@@ -108,14 +108,14 @@ class BiomeGen {
108108
// Gets all biomes in current chunk using each corresponding element of
109109
// heightmap as the y position, then stores the results by biome index in
110110
// biomemap (also returned)
111-
virtual biome_t *getBiomes(s16 *heightmap) = 0;
111+
virtual biome_t *getBiomes(s16 *heightmap, v3s16 pmin) = 0;
112112

113113
// Gets a single biome at the specified position, which must be contained
114114
// in the region formed by m_pmin and (m_pmin + m_csize - 1).
115115
virtual Biome *getBiomeAtPoint(v3s16 pos) const = 0;
116116

117117
// Same as above, but uses a raw numeric index correlating to the (x,z) position.
118-
virtual Biome *getBiomeAtIndex(size_t index, s16 y) const = 0;
118+
virtual Biome *getBiomeAtIndex(size_t index, v3s16 pos) const = 0;
119119

120120
// Result of calcBiomes bulk computation.
121121
biome_t *biomemap = nullptr;
@@ -164,11 +164,11 @@ class BiomeGenOriginal : public BiomeGen {
164164
Biome *calcBiomeAtPoint(v3s16 pos) const;
165165
void calcBiomeNoise(v3s16 pmin);
166166

167-
biome_t *getBiomes(s16 *heightmap);
167+
biome_t *getBiomes(s16 *heightmap, v3s16 pmin);
168168
Biome *getBiomeAtPoint(v3s16 pos) const;
169-
Biome *getBiomeAtIndex(size_t index, s16 y) const;
169+
Biome *getBiomeAtIndex(size_t index, v3s16 pos) const;
170170

171-
Biome *calcBiomeFromNoise(float heat, float humidity, s16 y) const;
171+
Biome *calcBiomeFromNoise(float heat, float humidity, v3s16 pos) const;
172172

173173
float *heatmap;
174174
float *humidmap;
@@ -230,7 +230,7 @@ class BiomeManager : public ObjDefManager {
230230
NoiseParams &np_heat_blend, u64 seed);
231231
float getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
232232
NoiseParams &np_humidity_blend, u64 seed);
233-
Biome *getBiomeFromNoiseOriginal(float heat, float humidity, s16 y);
233+
Biome *getBiomeFromNoiseOriginal(float heat, float humidity, v3s16 pos);
234234

235235
private:
236236
Server *m_server;

src/script/lua_api/l_mapgen.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,15 @@ Biome *read_biome_def(lua_State *L, int index, const NodeDefManager *ndef)
388388
b->depth_filler = getintfield_default(L, index, "depth_filler", -31000);
389389
b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
390390
b->depth_riverbed = getintfield_default(L, index, "depth_riverbed", 0);
391-
b->y_min = getintfield_default(L, index, "y_min", -31000);
392-
b->y_max = getintfield_default(L, index, "y_max", 31000);
393391
b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
394392
b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
395393
b->vertical_blend = getintfield_default(L, index, "vertical_blend", 0);
396-
b->flags = 0; //reserved
394+
b->flags = 0; // reserved
395+
396+
b->min_pos = getv3s16field_default(L, index, "min_pos", v3s16(-31000, -31000, -31000));
397+
getintfield(L, index, "y_min", b->min_pos.Y);
398+
b->max_pos = getv3s16field_default(L, index, "max_pos", v3s16(31000, 31000, 31000));
399+
getintfield(L, index, "y_max", b->max_pos.Y);
397400

398401
std::vector<std::string> &nn = b->m_nodenames;
399402
nn.push_back(getstringfield_default(L, index, "node_top", ""));
@@ -617,7 +620,7 @@ int ModApiMapgen::l_get_biome_data(lua_State *L)
617620
if (!humidity)
618621
return 0;
619622

620-
Biome *biome = (Biome *)bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos.Y);
623+
Biome *biome = (Biome *)bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos);
621624
if (!biome || biome->index == OBJDEF_INVALID_INDEX)
622625
return 0;
623626

@@ -1041,7 +1044,7 @@ int ModApiMapgen::l_register_biome(lua_State *L)
10411044
luaL_checktype(L, index, LUA_TTABLE);
10421045

10431046
const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
1044-
BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
1047+
BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
10451048

10461049
Biome *biome = read_biome_def(L, index, ndef);
10471050
if (!biome)

0 commit comments

Comments
 (0)