diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mapgen/mg_biome.cpp | 60 | ||||
-rw-r--r-- | src/mapgen/mg_biome.h | 7 | ||||
-rw-r--r-- | src/script/lua_api/l_mapgen.cpp | 153 | ||||
-rw-r--r-- | src/script/lua_api/l_mapgen.h | 14 |
4 files changed, 231 insertions, 3 deletions
diff --git a/src/mapgen/mg_biome.cpp b/src/mapgen/mg_biome.cpp index b562084e3..2273be280 100644 --- a/src/mapgen/mg_biome.cpp +++ b/src/mapgen/mg_biome.cpp @@ -84,8 +84,66 @@ void BiomeManager::clear() m_objects.resize(1); } -//////////////////////////////////////////////////////////////////////////////// +// For BiomeGen type 'BiomeGenOriginal' +float BiomeManager::getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat, + NoiseParams &np_heat_blend, u64 seed) +{ + return + NoisePerlin2D(&np_heat, pos.X, pos.Z, seed) + + NoisePerlin2D(&np_heat_blend, pos.X, pos.Z, seed); +} + + +// For BiomeGen type 'BiomeGenOriginal' +float BiomeManager::getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity, + NoiseParams &np_humidity_blend, u64 seed) +{ + return + NoisePerlin2D(&np_humidity, pos.X, pos.Z, seed) + + NoisePerlin2D(&np_humidity_blend, pos.X, pos.Z, seed); +} + + +// For BiomeGen type 'BiomeGenOriginal' +Biome *BiomeManager::getBiomeFromNoiseOriginal(float heat, float humidity, s16 y) +{ + Biome *biome_closest = nullptr; + Biome *biome_closest_blend = nullptr; + float dist_min = FLT_MAX; + float dist_min_blend = FLT_MAX; + + for (size_t i = 1; i < getNumObjects(); i++) { + Biome *b = (Biome *)getRaw(i); + if (!b || y > b->y_max + b->vertical_blend || y < b->y_min) + continue; + + float d_heat = heat - b->heat_point; + float d_humidity = humidity - b->humidity_point; + float dist = (d_heat * d_heat) + (d_humidity * d_humidity); + + if (y <= b->y_max) { // Within y limits of biome b + if (dist < dist_min) { + dist_min = dist; + biome_closest = b; + } + } else if (dist < dist_min_blend) { // Blend area above biome b + dist_min_blend = dist; + biome_closest_blend = b; + } + } + + mysrand(y + (heat - humidity) * 2); + if (biome_closest_blend && + myrand_range(0, biome_closest_blend->vertical_blend) >= + y - biome_closest_blend->y_max) + return biome_closest_blend; + + return (biome_closest) ? biome_closest : (Biome *)getRaw(BIOME_NONE); +} + + +//////////////////////////////////////////////////////////////////////////////// void BiomeParamsOriginal::readParams(const Settings *settings) { diff --git a/src/mapgen/mg_biome.h b/src/mapgen/mg_biome.h index ac606e935..a2bfaf5b9 100644 --- a/src/mapgen/mg_biome.h +++ b/src/mapgen/mg_biome.h @@ -225,6 +225,13 @@ public: virtual void clear(); + // For BiomeGen type 'BiomeGenOriginal' + float getHeatAtPosOriginal(v3s16 pos, NoiseParams &np_heat, + NoiseParams &np_heat_blend, u64 seed); + float getHumidityAtPosOriginal(v3s16 pos, NoiseParams &np_humidity, + NoiseParams &np_humidity_blend, u64 seed); + Biome *getBiomeFromNoiseOriginal(float heat, float humidity, s16 y); + private: Server *m_server; diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index b526111d4..db517b942 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -463,7 +463,7 @@ size_t get_biome_list(lua_State *L, int index, /////////////////////////////////////////////////////////////////////////////// // get_biome_id(biomename) -// returns the biome id used in biomemap +// returns the biome id as used in biomemap and returned by 'get_biome_data()' int ModApiMapgen::l_get_biome_id(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -488,6 +488,154 @@ int ModApiMapgen::l_get_biome_id(lua_State *L) } +// get_heat(pos) +// returns the heat at the position +int ModApiMapgen::l_get_heat(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + v3s16 pos = read_v3s16(L, 1); + + NoiseParams np_heat; + NoiseParams np_heat_blend; + + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat", + &np_heat) || + !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend", + &np_heat_blend)) + return 0; + + std::string value; + if (!settingsmgr->getMapSetting("seed", &value)) + return 0; + std::istringstream ss(value); + u64 seed; + ss >> seed; + + BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr; + if (!bmgr) + return 0; + + float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed); + if (!heat) + return 0; + + lua_pushnumber(L, heat); + + return 1; +} + + +// get_humidity(pos) +// returns the humidity at the position +int ModApiMapgen::l_get_humidity(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + v3s16 pos = read_v3s16(L, 1); + + NoiseParams np_humidity; + NoiseParams np_humidity_blend; + + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity", + &np_humidity) || + !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend", + &np_humidity_blend)) + return 0; + + std::string value; + if (!settingsmgr->getMapSetting("seed", &value)) + return 0; + std::istringstream ss(value); + u64 seed; + ss >> seed; + + BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr; + if (!bmgr) + return 0; + + float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity, + np_humidity_blend, seed); + if (!humidity) + return 0; + + lua_pushnumber(L, humidity); + + return 1; +} + + +// get_biome_data(pos) +// returns a table containing the biome id, heat and humidity at the position +int ModApiMapgen::l_get_biome_data(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + v3s16 pos = read_v3s16(L, 1); + + NoiseParams np_heat; + NoiseParams np_heat_blend; + NoiseParams np_humidity; + NoiseParams np_humidity_blend; + + MapSettingsManager *settingsmgr = + getServer(L)->getEmergeManager()->map_settings_mgr; + + if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat", + &np_heat) || + !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend", + &np_heat_blend) || + !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity", + &np_humidity) || + !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend", + &np_humidity_blend)) + return 0; + + std::string value; + if (!settingsmgr->getMapSetting("seed", &value)) + return 0; + std::istringstream ss(value); + u64 seed; + ss >> seed; + + BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr; + if (!bmgr) + return 0; + + float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed); + if (!heat) + return 0; + + float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity, + np_humidity_blend, seed); + if (!humidity) + return 0; + + Biome *biome = (Biome *)bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos.Y); + if (!biome || biome->index == OBJDEF_INVALID_INDEX) + return 0; + + lua_newtable(L); + + lua_pushinteger(L, biome->index); + lua_setfield(L, -2, "biome"); + + lua_pushnumber(L, heat); + lua_setfield(L, -2, "heat"); + + lua_pushnumber(L, humidity); + lua_setfield(L, -2, "humidity"); + + return 1; +} + + // get_mapgen_object(objectname) // returns the requested object used during map generation int ModApiMapgen::l_get_mapgen_object(lua_State *L) @@ -1520,6 +1668,9 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L) void ModApiMapgen::Initialize(lua_State *L, int top) { API_FCT(get_biome_id); + API_FCT(get_heat); + API_FCT(get_humidity); + API_FCT(get_biome_data); API_FCT(get_mapgen_object); API_FCT(get_mapgen_params); diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index 89b825be2..f6a821b4e 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -25,9 +25,21 @@ class ModApiMapgen : public ModApiBase { private: // get_biome_id(biomename) - // returns the biome id used in biomemap + // returns the biome id as used in biomemap and returned by 'get_biome_data()' static int l_get_biome_id(lua_State *L); + // get_heat(pos) + // returns the heat at the position + static int l_get_heat(lua_State *L); + + // get_humidity(pos) + // returns the humidity at the position + static int l_get_humidity(lua_State *L); + + // get_biome_data(pos) + // returns a table containing the biome id, heat and humidity at the position + static int l_get_biome_data(lua_State *L); + // get_mapgen_object(objectname) // returns the requested object used during map generation static int l_get_mapgen_object(lua_State *L); |