Skip to content

Commit d45e5da

Browse files
committedJan 16, 2018
Biomes: Add 'get heat', 'get humidity', 'get biome data' APIs
'get biome data' returns biome id, heat and humidity. Clean up nearby lines in lua_api.txt.
1 parent 4c0d4e4 commit d45e5da

File tree

5 files changed

+302
-39
lines changed

5 files changed

+302
-39
lines changed
 

Diff for: ‎doc/lua_api.txt

+71-36
Original file line numberDiff line numberDiff line change
@@ -2816,58 +2816,93 @@ and `minetest.auth_reload` call the authentication handler.
28162816
* Return voxel manipulator object.
28172817
* Loads the manipulator from the map if positions are passed.
28182818
* `minetest.set_gen_notify(flags, {deco_ids})`
2819-
* Set the types of on-generate notifications that should be collected
2820-
* `flags` is a flag field with the available flags: `dungeon`, `temple`, `cave_begin`,
2821-
`cave_end`, `large_cave_begin`, `large_cave_end`, `decoration`
2822-
* The second parameter is a list of IDS of decorations which notification is requested for
2823-
* `get_gen_notify()`: returns a flagstring and a table with the `deco_id`s
2819+
* Set the types of on-generate notifications that should be collected.
2820+
* `flags` is a flag field with the available flags:
2821+
* dungeon
2822+
* temple
2823+
* cave_begin
2824+
* cave_end
2825+
* large_cave_begin
2826+
* large_cave_end
2827+
* decoration
2828+
* The second parameter is a list of IDS of decorations which notification
2829+
is requested for.
2830+
* `get_gen_notify()`
2831+
* Returns a flagstring and a table with the `deco_id`s.
28242832
* `minetest.get_mapgen_object(objectname)`
28252833
* Return requested mapgen object if available (see "Mapgen objects")
2834+
* `minetest.get_heat(pos)`
2835+
* Returns the heat at the position, or `nil` on failure.
2836+
* `minetest.get_humidity(pos)`
2837+
* Returns the humidity at the position, or `nil` on failure.
2838+
* `minetest.get_biome_data(pos)`
2839+
* Returns a table containing:
2840+
* `biome` the biome id of the biome at that position
2841+
* `heat` the heat at the position
2842+
* `humidity` the humidity at the position
2843+
* Or returns `nil` on failure.
28262844
* `minetest.get_biome_id(biome_name)`
2827-
* Returns the biome id, as used in the biomemap Mapgen object, for a
2828-
given biome_name string.
2829-
* `minetest.get_mapgen_params()` Returns mapgen parameters, a table containing
2830-
`mgname`, `seed`, `chunksize`, `water_level`, and `flags`.
2831-
* Deprecated: use `minetest.get_mapgen_setting(name)` instead
2845+
* Returns the biome id, as used in the biomemap Mapgen object and returned
2846+
by `minetest.get_biome_data(pos)`, for a given biome_name string.
2847+
* `minetest.get_mapgen_params()`
2848+
* Deprecated: use `minetest.get_mapgen_setting(name)` instead.
2849+
* Returns a table containing:
2850+
* `mgname`
2851+
* `seed`
2852+
* `chunksize`
2853+
* `water_level`
2854+
* `flags`
28322855
* `minetest.set_mapgen_params(MapgenParams)`
2833-
* Deprecated: use `minetest.set_mapgen_setting(name, value, override)` instead
2834-
* Set map generation parameters
2835-
* Function cannot be called after the registration period; only initialization
2836-
and `on_mapgen_init`
2837-
* Takes a table as an argument with the fields `mgname`, `seed`, `water_level`,
2838-
and `flags`.
2839-
* Leave field unset to leave that parameter unchanged
2840-
* `flags` contains a comma-delimited string of flags to set,
2841-
or if the prefix `"no"` is attached, clears instead.
2842-
* `flags` is in the same format and has the same options as `mg_flags` in `minetest.conf`
2856+
* Deprecated: use `minetest.set_mapgen_setting(name, value, override)`
2857+
instead.
2858+
* Set map generation parameters.
2859+
* Function cannot be called after the registration period; only
2860+
initialization and `on_mapgen_init`.
2861+
* Takes a table as an argument with the fields:
2862+
* `mgname`
2863+
* `seed`
2864+
* `chunksize`
2865+
* `water_level`
2866+
* `flags`
2867+
* Leave field unset to leave that parameter unchanged.
2868+
* `flags` contains a comma-delimited string of flags to set, or if the
2869+
prefix `"no"` is attached, clears instead.
2870+
* `flags` is in the same format and has the same options as `mg_flags` in
2871+
`minetest.conf`.
28432872
* `minetest.get_mapgen_setting(name)`
2844-
* Gets the *active* mapgen setting (or nil if none exists) in string format with the following
2845-
order of precedence:
2873+
* Gets the *active* mapgen setting (or nil if none exists) in string
2874+
format with the following order of precedence:
28462875
1) Settings loaded from map_meta.txt or overrides set during mod execution
28472876
2) Settings set by mods without a metafile override
28482877
3) Settings explicitly set in the user config file, minetest.conf
28492878
4) Settings set as the user config default
28502879
* `minetest.get_mapgen_setting_noiseparams(name)`
2851-
* Same as above, but returns the value as a NoiseParams table if the setting `name` exists
2852-
and is a valid NoiseParams
2880+
* Same as above, but returns the value as a NoiseParams table if the
2881+
setting `name` exists and is a valid NoiseParams.
28532882
* `minetest.set_mapgen_setting(name, value, [override_meta])`
2854-
* Sets a mapgen param to `value`, and will take effect if the corresponding mapgen setting
2855-
is not already present in map_meta.txt.
2856-
* `override_meta` is an optional boolean (default: `false`). If this is set to true,
2857-
the setting will become the active setting regardless of the map metafile contents.
2858-
* Note: to set the seed, use `"seed"`, not `"fixed_map_seed"`
2883+
* Sets a mapgen param to `value`, and will take effect if the corresponding
2884+
mapgen setting is not already present in map_meta.txt.
2885+
* `override_meta` is an optional boolean (default: `false`). If this is set
2886+
to true, the setting will become the active setting regardless of the map
2887+
metafile contents.
2888+
* Note: to set the seed, use `"seed"`, not `"fixed_map_seed"`.
28592889
* `minetest.set_mapgen_setting_noiseparams(name, value, [override_meta])`
2860-
* Same as above, except value is a NoiseParams table.
2890+
* Same as above, except value is a NoiseParams table.
28612891
* `minetest.set_noiseparams(name, noiseparams, set_default)`
2862-
* Sets the noiseparams setting of `name` to the noiseparams table specified in `noiseparams`.
2863-
* `set_default` is an optional boolean (default: `true`) that specifies whether the setting
2864-
should be applied to the default config or current active config
2865-
* `minetest.get_noiseparams(name)`: returns a table of the noiseparams for name
2892+
* Sets the noiseparams setting of `name` to the noiseparams table specified
2893+
in `noiseparams`.
2894+
* `set_default` is an optional boolean (default: `true`) that specifies
2895+
whether the setting should be applied to the default config or current
2896+
active config.
2897+
* `minetest.get_noiseparams(name)`
2898+
* Returns a table of the noiseparams for name.
28662899
* `minetest.generate_ores(vm, pos1, pos2)`
2867-
* Generate all registered ores within the VoxelManip `vm` and in the area from `pos1` to `pos2`.
2900+
* Generate all registered ores within the VoxelManip `vm` and in the area
2901+
from `pos1` to `pos2`.
28682902
* `pos1` and `pos2` are optional and default to mapchunk minp and maxp.
28692903
* `minetest.generate_decorations(vm, pos1, pos2)`
2870-
* Generate all registered decorations within the VoxelManip `vm` and in the area from `pos1` to `pos2`.
2904+
* Generate all registered decorations within the VoxelManip `vm` and in the
2905+
area from `pos1` to `pos2`.
28712906
* `pos1` and `pos2` are optional and default to mapchunk minp and maxp.
28722907
* `minetest.clear_objects([options])`
28732908
* Clear all objects in the environment

Diff for: ‎src/mapgen/mg_biome.cpp

+59-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,66 @@ void BiomeManager::clear()
8484
m_objects.resize(1);
8585
}
8686

87-
////////////////////////////////////////////////////////////////////////////////
8887

88+
// For BiomeGen type 'BiomeGenOriginal'
89+
float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
90+
NoiseParams &np_heat_blend, u64 seed)
91+
{
92+
return
93+
NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) +
94+
NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed);
95+
}
96+
97+
98+
// For BiomeGen type 'BiomeGenOriginal'
99+
float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
100+
NoiseParams &np_humidity_blend, u64 seed)
101+
{
102+
return
103+
NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) +
104+
NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed);
105+
}
106+
107+
108+
// For BiomeGen type 'BiomeGenOriginal'
109+
Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y)
110+
{
111+
Biome *biome_closest = nullptr;
112+
Biome *biome_closest_blend = nullptr;
113+
float dist_min = FLT_MAX;
114+
float dist_min_blend = FLT_MAX;
115+
116+
for (size_t i = 1; i < getNumObjects(); i++) {
117+
Biome *b = (Biome *)getRaw(i);
118+
if (!b || y > b->y_max + b->vertical_blend || y < b->y_min)
119+
continue;
120+
121+
float d_heat = heat - b->heat_point;
122+
float d_humidity = humidity - b->humidity_point;
123+
float dist = (d_heat * d_heat) + (d_humidity * d_humidity);
124+
125+
if (y <= b->y_max) { // Within y limits of biome b
126+
if (dist < dist_min) {
127+
dist_min = dist;
128+
biome_closest = b;
129+
}
130+
} else if (dist < dist_min_blend) { // Blend area above biome b
131+
dist_min_blend = dist;
132+
biome_closest_blend = b;
133+
}
134+
}
135+
136+
mysrand(y + (heat - humidity) * 2);
137+
if (biome_closest_blend &&
138+
myrand_range(0, biome_closest_blend->vertical_blend) >=
139+
y - biome_closest_blend->y_max)
140+
return biome_closest_blend;
141+
142+
return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE);
143+
}
144+
145+
146+
////////////////////////////////////////////////////////////////////////////////
89147

90148
void BiomeParamsOriginal::readParams(const Settings *settings)
91149
{

Diff for: ‎src/mapgen/mg_biome.h

+7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ class BiomeManager : public ObjDefManager {
225225

226226
virtual void clear();
227227

228+
// For BiomeGen type 'BiomeGenOriginal'
229+
float getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat,
230+
NoiseParams &np_heat_blend, u64 seed);
231+
float getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity,
232+
NoiseParams &np_humidity_blend, u64 seed);
233+
Biome *getBiomeFromNoiseOriginal(float heat, float humidity, s16 y);
234+
228235
private:
229236
Server *m_server;
230237

Diff for: ‎src/script/lua_api/l_mapgen.cpp

+152-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ size_t get_biome_list(lua_State *L, int index,
463463
///////////////////////////////////////////////////////////////////////////////
464464

465465
// get_biome_id(biomename)
466-
// returns the biome id used in biomemap
466+
// returns the biome id as used in biomemap and returned by 'get_biome_data()'
467467
int ModApiMapgen::l_get_biome_id(lua_State *L)
468468
{
469469
NO_MAP_LOCK_REQUIRED;
@@ -488,6 +488,154 @@ int ModApiMapgen::l_get_biome_id(lua_State *L)
488488
}
489489

490490

491+
// get_heat(pos)
492+
// returns the heat at the position
493+
int ModApiMapgen::l_get_heat(lua_State *L)
494+
{
495+
NO_MAP_LOCK_REQUIRED;
496+
497+
v3s16 pos = read_v3s16(L, 1);
498+
499+
NoiseParams np_heat;
500+
NoiseParams np_heat_blend;
501+
502+
MapSettingsManager *settingsmgr =
503+
getServer(L)->getEmergeManager()->map_settings_mgr;
504+
505+
if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
506+
&np_heat) ||
507+
!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
508+
&np_heat_blend))
509+
return 0;
510+
511+
std::string value;
512+
if (!settingsmgr->getMapSetting("seed", &value))
513+
return 0;
514+
std::istringstream ss(value);
515+
u64 seed;
516+
ss >> seed;
517+
518+
BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
519+
if (!bmgr)
520+
return 0;
521+
522+
float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
523+
if (!heat)
524+
return 0;
525+
526+
lua_pushnumber(L, heat);
527+
528+
return 1;
529+
}
530+
531+
532+
// get_humidity(pos)
533+
// returns the humidity at the position
534+
int ModApiMapgen::l_get_humidity(lua_State *L)
535+
{
536+
NO_MAP_LOCK_REQUIRED;
537+
538+
v3s16 pos = read_v3s16(L, 1);
539+
540+
NoiseParams np_humidity;
541+
NoiseParams np_humidity_blend;
542+
543+
MapSettingsManager *settingsmgr =
544+
getServer(L)->getEmergeManager()->map_settings_mgr;
545+
546+
if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
547+
&np_humidity) ||
548+
!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
549+
&np_humidity_blend))
550+
return 0;
551+
552+
std::string value;
553+
if (!settingsmgr->getMapSetting("seed", &value))
554+
return 0;
555+
std::istringstream ss(value);
556+
u64 seed;
557+
ss >> seed;
558+
559+
BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
560+
if (!bmgr)
561+
return 0;
562+
563+
float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
564+
np_humidity_blend, seed);
565+
if (!humidity)
566+
return 0;
567+
568+
lua_pushnumber(L, humidity);
569+
570+
return 1;
571+
}
572+
573+
574+
// get_biome_data(pos)
575+
// returns a table containing the biome id, heat and humidity at the position
576+
int ModApiMapgen::l_get_biome_data(lua_State *L)
577+
{
578+
NO_MAP_LOCK_REQUIRED;
579+
580+
v3s16 pos = read_v3s16(L, 1);
581+
582+
NoiseParams np_heat;
583+
NoiseParams np_heat_blend;
584+
NoiseParams np_humidity;
585+
NoiseParams np_humidity_blend;
586+
587+
MapSettingsManager *settingsmgr =
588+
getServer(L)->getEmergeManager()->map_settings_mgr;
589+
590+
if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
591+
&np_heat) ||
592+
!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
593+
&np_heat_blend) ||
594+
!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
595+
&np_humidity) ||
596+
!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
597+
&np_humidity_blend))
598+
return 0;
599+
600+
std::string value;
601+
if (!settingsmgr->getMapSetting("seed", &value))
602+
return 0;
603+
std::istringstream ss(value);
604+
u64 seed;
605+
ss >> seed;
606+
607+
BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
608+
if (!bmgr)
609+
return 0;
610+
611+
float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
612+
if (!heat)
613+
return 0;
614+
615+
float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
616+
np_humidity_blend, seed);
617+
if (!humidity)
618+
return 0;
619+
620+
Biome *biome = (Biome *)bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos.Y);
621+
if (!biome || biome->index == OBJDEF_INVALID_INDEX)
622+
return 0;
623+
624+
lua_newtable(L);
625+
626+
lua_pushinteger(L, biome->index);
627+
lua_setfield(L, -2, "biome");
628+
629+
lua_pushnumber(L, heat);
630+
lua_setfield(L, -2, "heat");
631+
632+
lua_pushnumber(L, humidity);
633+
lua_setfield(L, -2, "humidity");
634+
635+
return 1;
636+
}
637+
638+
491639
// get_mapgen_object(objectname)
492640
// returns the requested object used during map generation
493641
int ModApiMapgen::l_get_mapgen_object(lua_State *L)
@@ -1520,6 +1668,9 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L)
15201668
void ModApiMapgen::Initialize(lua_State *L, int top)
15211669
{
15221670
API_FCT(get_biome_id);
1671+
API_FCT(get_heat);
1672+
API_FCT(get_humidity);
1673+
API_FCT(get_biome_data);
15231674
API_FCT(get_mapgen_object);
15241675

15251676
API_FCT(get_mapgen_params);

Diff for: ‎src/script/lua_api/l_mapgen.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,21 @@ class ModApiMapgen : public ModApiBase
2525
{
2626
private:
2727
// get_biome_id(biomename)
28-
// returns the biome id used in biomemap
28+
// returns the biome id as used in biomemap and returned by 'get_biome_data()'
2929
static int l_get_biome_id(lua_State *L);
3030

31+
// get_heat(pos)
32+
// returns the heat at the position
33+
static int l_get_heat(lua_State *L);
34+
35+
// get_humidity(pos)
36+
// returns the humidity at the position
37+
static int l_get_humidity(lua_State *L);
38+
39+
// get_biome_data(pos)
40+
// returns a table containing the biome id, heat and humidity at the position
41+
static int l_get_biome_data(lua_State *L);
42+
3143
// get_mapgen_object(objectname)
3244
// returns the requested object used during map generation
3345
static int l_get_mapgen_object(lua_State *L);

0 commit comments

Comments
 (0)
Please sign in to comment.