3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_mapgen.h"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_vmanip.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
25 #include "cpp_api/s_security.h"
26 #include "util/serialize.h"
28 #include "environment.h"
30 #include "mapgen/mg_biome.h"
31 #include "mapgen/mg_ore.h"
32 #include "mapgen/mg_decoration.h"
33 #include "mapgen/mg_schematic.h"
34 #include "mapgen/mapgen_v5.h"
35 #include "mapgen/mapgen_v7.h"
40 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
42 {BIOMETYPE_NORMAL, "normal"},
46 struct EnumString ModApiMapgen::es_DecorationType[] =
48 {DECO_SIMPLE, "simple"},
49 {DECO_SCHEMATIC, "schematic"},
50 {DECO_LSYSTEM, "lsystem"},
54 struct EnumString ModApiMapgen::es_MapgenObject[] =
56 {MGOBJ_VMANIP, "voxelmanip"},
57 {MGOBJ_HEIGHTMAP, "heightmap"},
58 {MGOBJ_BIOMEMAP, "biomemap"},
59 {MGOBJ_HEATMAP, "heatmap"},
60 {MGOBJ_HUMIDMAP, "humiditymap"},
61 {MGOBJ_GENNOTIFY, "gennotify"},
65 struct EnumString ModApiMapgen::es_OreType[] =
67 {ORE_SCATTER, "scatter"},
72 {ORE_STRATUM, "stratum"},
76 struct EnumString ModApiMapgen::es_Rotation[] =
82 {ROTATE_RAND, "random"},
86 struct EnumString ModApiMapgen::es_SchematicFormatType[] =
88 {SCHEM_FMT_HANDLE, "handle"},
89 {SCHEM_FMT_MTS, "mts"},
90 {SCHEM_FMT_LUA, "lua"},
94 ObjDef *get_objdef(lua_State *L, int index, const ObjDefManager *objmgr);
96 Biome *get_or_load_biome(lua_State *L, int index,
97 BiomeManager *biomemgr);
98 Biome *read_biome_def(lua_State *L, int index, const NodeDefManager *ndef);
99 size_t get_biome_list(lua_State *L, int index,
100 BiomeManager *biomemgr, std::unordered_set<biome_t> *biome_id_list);
102 Schematic *get_or_load_schematic(lua_State *L, int index,
103 SchematicManager *schemmgr, StringMap *replace_names);
104 Schematic *load_schematic(lua_State *L, int index, const NodeDefManager *ndef,
105 StringMap *replace_names);
106 Schematic *load_schematic_from_def(lua_State *L, int index,
107 const NodeDefManager *ndef, StringMap *replace_names);
108 bool read_schematic_def(lua_State *L, int index,
109 Schematic *schem, std::vector<std::string> *names);
111 bool read_deco_simple(lua_State *L, DecoSimple *deco);
112 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
115 ///////////////////////////////////////////////////////////////////////////////
117 ObjDef *get_objdef(lua_State *L, int index, const ObjDefManager *objmgr)
120 index = lua_gettop(L) + 1 + index;
122 // If a number, assume this is a handle to an object def
123 if (lua_isnumber(L, index))
124 return objmgr->get(lua_tointeger(L, index));
126 // If a string, assume a name is given instead
127 if (lua_isstring(L, index))
128 return objmgr->getByName(lua_tostring(L, index));
133 ///////////////////////////////////////////////////////////////////////////////
135 Schematic *get_or_load_schematic(lua_State *L, int index,
136 SchematicManager *schemmgr, StringMap *replace_names)
139 index = lua_gettop(L) + 1 + index;
141 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
145 schem = load_schematic(L, index, schemmgr->getNodeDef(),
150 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
159 Schematic *load_schematic(lua_State *L, int index, const NodeDefManager *ndef,
160 StringMap *replace_names)
163 index = lua_gettop(L) + 1 + index;
165 Schematic *schem = NULL;
167 if (lua_istable(L, index)) {
168 schem = load_schematic_from_def(L, index, ndef,
174 } else if (lua_isnumber(L, index)) {
176 } else if (lua_isstring(L, index)) {
177 schem = SchematicManager::create(SCHEMATIC_NORMAL);
179 std::string filepath = lua_tostring(L, index);
180 if (!fs::IsPathAbsolute(filepath))
181 filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
183 if (!schem->loadSchematicFromFile(filepath, ndef,
194 Schematic *load_schematic_from_def(lua_State *L, int index,
195 const NodeDefManager *ndef, StringMap *replace_names)
197 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
199 if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
204 size_t num_nodes = schem->m_nodenames.size();
206 schem->m_nnlistsizes.push_back(num_nodes);
209 for (size_t i = 0; i != num_nodes; i++) {
210 StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
211 if (it != replace_names->end())
212 schem->m_nodenames[i] = it->second;
217 ndef->pendNodeResolve(schem);
223 bool read_schematic_def(lua_State *L, int index,
224 Schematic *schem, std::vector<std::string> *names)
226 if (!lua_istable(L, index))
229 //// Get schematic size
230 lua_getfield(L, index, "size");
231 v3s16 size = check_v3s16(L, -1);
236 //// Get schematic data
237 lua_getfield(L, index, "data");
238 luaL_checktype(L, -1, LUA_TTABLE);
240 u32 numnodes = size.X * size.Y * size.Z;
241 schem->schemdata = new MapNode[numnodes];
243 size_t names_base = names->size();
244 std::unordered_map<std::string, content_t> name_id_map;
247 for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
253 if (!getstringfield(L, -1, "name", name))
254 throw LuaError("Schematic data definition with missing name field");
256 //// Read param1/prob
258 if (!getintfield(L, -1, "param1", param1) &&
259 !getintfield(L, -1, "prob", param1))
260 param1 = MTSCHEM_PROB_ALWAYS_OLD;
263 u8 param2 = getintfield_default(L, -1, "param2", 0);
265 //// Find or add new nodename-to-ID mapping
266 std::unordered_map<std::string, content_t>::iterator it = name_id_map.find(name);
267 content_t name_index;
268 if (it != name_id_map.end()) {
269 name_index = it->second;
271 name_index = names->size() - names_base;
272 name_id_map[name] = name_index;
273 names->push_back(name);
276 //// Perform probability/force_place fixup on param1
278 if (getboolfield_default(L, -1, "force_place", false))
279 param1 |= MTSCHEM_FORCE_PLACE;
281 //// Actually set the node in the schematic
282 schem->schemdata[i] = MapNode(name_index, param1, param2);
286 errorstream << "read_schematic_def: incorrect number of "
287 "nodes provided in raw schematic data (got " << i <<
288 ", expected " << numnodes << ")." << std::endl;
292 //// Get Y-slice probability values (if present)
293 schem->slice_probs = new u8[size.Y];
294 for (i = 0; i != (u32) size.Y; i++)
295 schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
297 lua_getfield(L, index, "yslice_prob");
298 if (lua_istable(L, -1)) {
299 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
301 if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
302 !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
305 schem->slice_probs[ypos] >>= 1;
313 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
316 index = lua_gettop(L) + 1 + index;
319 while (lua_next(L, index)) {
320 std::string replace_from;
321 std::string replace_to;
323 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
324 lua_rawgeti(L, -1, 1);
325 if (!lua_isstring(L, -1))
326 throw LuaError("schematics: replace_from field is not a string");
327 replace_from = lua_tostring(L, -1);
330 lua_rawgeti(L, -1, 2);
331 if (!lua_isstring(L, -1))
332 throw LuaError("schematics: replace_to field is not a string");
333 replace_to = lua_tostring(L, -1);
335 } else { // New {x = "y", ...} format
336 if (!lua_isstring(L, -2))
337 throw LuaError("schematics: replace_from field is not a string");
338 replace_from = lua_tostring(L, -2);
339 if (!lua_isstring(L, -1))
340 throw LuaError("schematics: replace_to field is not a string");
341 replace_to = lua_tostring(L, -1);
344 replace_names->insert(std::make_pair(replace_from, replace_to));
349 ///////////////////////////////////////////////////////////////////////////////
351 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
354 index = lua_gettop(L) + 1 + index;
356 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
360 biome = read_biome_def(L, index, biomemgr->getNodeDef());
364 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
373 Biome *read_biome_def(lua_State *L, int index, const NodeDefManager *ndef)
375 if (!lua_istable(L, index))
378 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
379 ModApiMapgen::es_BiomeTerrainType, BIOMETYPE_NORMAL);
380 Biome *b = BiomeManager::create(biometype);
382 b->name = getstringfield_default(L, index, "name", "");
383 b->depth_top = getintfield_default(L, index, "depth_top", 0);
384 b->depth_filler = getintfield_default(L, index, "depth_filler", -31000);
385 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
386 b->depth_riverbed = getintfield_default(L, index, "depth_riverbed", 0);
387 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
388 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
389 b->vertical_blend = getintfield_default(L, index, "vertical_blend", 0);
390 b->flags = 0; // reserved
392 b->min_pos = getv3s16field_default(
393 L, index, "min_pos", v3s16(-31000, -31000, -31000));
394 getintfield(L, index, "y_min", b->min_pos.Y);
395 b->max_pos = getv3s16field_default(
396 L, index, "max_pos", v3s16(31000, 31000, 31000));
397 getintfield(L, index, "y_max", b->max_pos.Y);
399 std::vector<std::string> &nn = b->m_nodenames;
400 nn.push_back(getstringfield_default(L, index, "node_top", ""));
401 nn.push_back(getstringfield_default(L, index, "node_filler", ""));
402 nn.push_back(getstringfield_default(L, index, "node_stone", ""));
403 nn.push_back(getstringfield_default(L, index, "node_water_top", ""));
404 nn.push_back(getstringfield_default(L, index, "node_water", ""));
405 nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
406 nn.push_back(getstringfield_default(L, index, "node_riverbed", ""));
407 nn.push_back(getstringfield_default(L, index, "node_dust", ""));
409 size_t nnames = getstringlistfield(L, index, "node_cave_liquid", &nn);
410 // If no cave liquids defined, set list to "ignore" to trigger old hardcoded
411 // cave liquid behaviour.
413 nn.emplace_back("ignore");
416 b->m_nnlistsizes.push_back(nnames);
418 nn.push_back(getstringfield_default(L, index, "node_dungeon", ""));
419 nn.push_back(getstringfield_default(L, index, "node_dungeon_alt", ""));
420 nn.push_back(getstringfield_default(L, index, "node_dungeon_stair", ""));
421 ndef->pendNodeResolve(b);
427 size_t get_biome_list(lua_State *L, int index,
428 BiomeManager *biomemgr, std::unordered_set<biome_t> *biome_id_list)
431 index = lua_gettop(L) + 1 + index;
433 if (lua_isnil(L, index))
436 bool is_single = true;
437 if (lua_istable(L, index)) {
438 lua_getfield(L, index, "name");
439 is_single = !lua_isnil(L, -1);
444 Biome *biome = get_or_load_biome(L, index, biomemgr);
446 infostream << "get_biome_list: failed to get biome '"
447 << (lua_isstring(L, index) ? lua_tostring(L, index) : "")
448 << "'." << std::endl;
452 biome_id_list->insert(biome->index);
456 // returns number of failed resolutions
457 size_t fail_count = 0;
460 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
462 Biome *biome = get_or_load_biome(L, -1, biomemgr);
465 infostream << "get_biome_list: failed to get biome '"
466 << (lua_isstring(L, -1) ? lua_tostring(L, -1) : "")
471 biome_id_list->insert(biome->index);
477 ///////////////////////////////////////////////////////////////////////////////
479 // get_biome_id(biomename)
480 // returns the biome id as used in biomemap and returned by 'get_biome_data()'
481 int ModApiMapgen::l_get_biome_id(lua_State *L)
483 NO_MAP_LOCK_REQUIRED;
485 const char *biome_str = lua_tostring(L, 1);
489 const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
493 const Biome *biome = (Biome *)bmgr->getByName(biome_str);
494 if (!biome || biome->index == OBJDEF_INVALID_INDEX)
497 lua_pushinteger(L, biome->index);
503 // get_biome_name(biome_id)
504 // returns the biome name string
505 int ModApiMapgen::l_get_biome_name(lua_State *L)
507 NO_MAP_LOCK_REQUIRED;
509 int biome_id = luaL_checkinteger(L, 1);
511 const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
515 const Biome *b = (Biome *)bmgr->getRaw(biome_id);
516 lua_pushstring(L, b->name.c_str());
523 // returns the heat at the position
524 int ModApiMapgen::l_get_heat(lua_State *L)
526 NO_MAP_LOCK_REQUIRED;
528 v3s16 pos = read_v3s16(L, 1);
531 NoiseParams np_heat_blend;
533 MapSettingsManager *settingsmgr =
534 getServer(L)->getEmergeManager()->map_settings_mgr;
536 if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
538 !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
543 if (!settingsmgr->getMapSetting("seed", &value))
545 std::istringstream ss(value);
549 const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
553 float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
555 lua_pushnumber(L, heat);
562 // returns the humidity at the position
563 int ModApiMapgen::l_get_humidity(lua_State *L)
565 NO_MAP_LOCK_REQUIRED;
567 v3s16 pos = read_v3s16(L, 1);
569 NoiseParams np_humidity;
570 NoiseParams np_humidity_blend;
572 MapSettingsManager *settingsmgr =
573 getServer(L)->getEmergeManager()->map_settings_mgr;
575 if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
577 !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
582 if (!settingsmgr->getMapSetting("seed", &value))
584 std::istringstream ss(value);
588 const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
592 float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
593 np_humidity_blend, seed);
595 lua_pushnumber(L, humidity);
601 // get_biome_data(pos)
602 // returns a table containing the biome id, heat and humidity at the position
603 int ModApiMapgen::l_get_biome_data(lua_State *L)
605 NO_MAP_LOCK_REQUIRED;
607 v3s16 pos = read_v3s16(L, 1);
610 NoiseParams np_heat_blend;
611 NoiseParams np_humidity;
612 NoiseParams np_humidity_blend;
614 MapSettingsManager *settingsmgr =
615 getServer(L)->getEmergeManager()->map_settings_mgr;
617 if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat",
619 !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend",
621 !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity",
623 !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend",
628 if (!settingsmgr->getMapSetting("seed", &value))
630 std::istringstream ss(value);
634 const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager();
638 float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed);
642 float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity,
643 np_humidity_blend, seed);
647 const Biome *biome = bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos);
648 if (!biome || biome->index == OBJDEF_INVALID_INDEX)
653 lua_pushinteger(L, biome->index);
654 lua_setfield(L, -2, "biome");
656 lua_pushnumber(L, heat);
657 lua_setfield(L, -2, "heat");
659 lua_pushnumber(L, humidity);
660 lua_setfield(L, -2, "humidity");
666 // get_mapgen_object(objectname)
667 // returns the requested object used during map generation
668 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
670 NO_MAP_LOCK_REQUIRED;
672 const char *mgobjstr = lua_tostring(L, 1);
675 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
678 enum MapgenObject mgobj = (MapgenObject)mgobjint;
680 EmergeManager *emerge = getServer(L)->getEmergeManager();
681 Mapgen *mg = emerge->getCurrentMapgen();
683 throw LuaError("Must only be called in a mapgen thread!");
685 size_t maplen = mg->csize.X * mg->csize.Z;
689 MMVManip *vm = mg->vm;
692 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
693 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
694 luaL_getmetatable(L, "VoxelManip");
695 lua_setmetatable(L, -2);
698 push_v3s16(L, vm->m_area.MinEdge);
701 push_v3s16(L, vm->m_area.MaxEdge);
705 case MGOBJ_HEIGHTMAP: {
709 lua_createtable(L, maplen, 0);
710 for (size_t i = 0; i != maplen; i++) {
711 lua_pushinteger(L, mg->heightmap[i]);
712 lua_rawseti(L, -2, i + 1);
717 case MGOBJ_BIOMEMAP: {
721 lua_createtable(L, maplen, 0);
722 for (size_t i = 0; i != maplen; i++) {
723 lua_pushinteger(L, mg->biomegen->biomemap[i]);
724 lua_rawseti(L, -2, i + 1);
729 case MGOBJ_HEATMAP: {
730 if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
733 BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
735 lua_createtable(L, maplen, 0);
736 for (size_t i = 0; i != maplen; i++) {
737 lua_pushnumber(L, bg->heatmap[i]);
738 lua_rawseti(L, -2, i + 1);
744 case MGOBJ_HUMIDMAP: {
745 if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
748 BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
750 lua_createtable(L, maplen, 0);
751 for (size_t i = 0; i != maplen; i++) {
752 lua_pushnumber(L, bg->humidmap[i]);
753 lua_rawseti(L, -2, i + 1);
758 case MGOBJ_GENNOTIFY: {
759 std::map<std::string, std::vector<v3s16> >event_map;
761 mg->gennotify.getEvents(event_map);
763 lua_createtable(L, 0, event_map.size());
764 for (auto it = event_map.begin(); it != event_map.end(); ++it) {
765 lua_createtable(L, it->second.size(), 0);
767 for (size_t j = 0; j != it->second.size(); j++) {
768 push_v3s16(L, it->second[j]);
769 lua_rawseti(L, -2, j + 1);
772 lua_setfield(L, -2, it->first.c_str());
783 // get_spawn_level(x = num, z = num)
784 int ModApiMapgen::l_get_spawn_level(lua_State *L)
786 NO_MAP_LOCK_REQUIRED;
788 s16 x = luaL_checkinteger(L, 1);
789 s16 z = luaL_checkinteger(L, 2);
791 EmergeManager *emerge = getServer(L)->getEmergeManager();
792 int spawn_level = emerge->getSpawnLevelAtPoint(v2s16(x, z));
793 // Unsuitable spawn point
794 if (spawn_level == MAX_MAP_GENERATION_LIMIT)
797 // 'findSpawnPos()' in server.cpp adds at least 1
798 lua_pushinteger(L, spawn_level + 1);
804 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
806 NO_MAP_LOCK_REQUIRED;
808 log_deprecated(L, "get_mapgen_params is deprecated; "
809 "use get_mapgen_setting instead");
813 MapSettingsManager *settingsmgr =
814 getServer(L)->getEmergeManager()->map_settings_mgr;
818 settingsmgr->getMapSetting("mg_name", &value);
819 lua_pushstring(L, value.c_str());
820 lua_setfield(L, -2, "mgname");
822 settingsmgr->getMapSetting("seed", &value);
823 std::istringstream ss(value);
826 lua_pushinteger(L, seed);
827 lua_setfield(L, -2, "seed");
829 settingsmgr->getMapSetting("water_level", &value);
830 lua_pushinteger(L, stoi(value, -32768, 32767));
831 lua_setfield(L, -2, "water_level");
833 settingsmgr->getMapSetting("chunksize", &value);
834 lua_pushinteger(L, stoi(value, -32768, 32767));
835 lua_setfield(L, -2, "chunksize");
837 settingsmgr->getMapSetting("mg_flags", &value);
838 lua_pushstring(L, value.c_str());
839 lua_setfield(L, -2, "flags");
845 // set_mapgen_params(params)
846 // set mapgen parameters
847 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
849 NO_MAP_LOCK_REQUIRED;
851 log_deprecated(L, "set_mapgen_params is deprecated; "
852 "use set_mapgen_setting instead");
854 if (!lua_istable(L, 1))
857 MapSettingsManager *settingsmgr =
858 getServer(L)->getEmergeManager()->map_settings_mgr;
860 lua_getfield(L, 1, "mgname");
861 if (lua_isstring(L, -1))
862 settingsmgr->setMapSetting("mg_name", readParam<std::string>(L, -1), true);
864 lua_getfield(L, 1, "seed");
865 if (lua_isnumber(L, -1))
866 settingsmgr->setMapSetting("seed", readParam<std::string>(L, -1), true);
868 lua_getfield(L, 1, "water_level");
869 if (lua_isnumber(L, -1))
870 settingsmgr->setMapSetting("water_level", readParam<std::string>(L, -1), true);
872 lua_getfield(L, 1, "chunksize");
873 if (lua_isnumber(L, -1))
874 settingsmgr->setMapSetting("chunksize", readParam<std::string>(L, -1), true);
876 lua_getfield(L, 1, "flags");
877 if (lua_isstring(L, -1))
878 settingsmgr->setMapSetting("mg_flags", readParam<std::string>(L, -1), true);
883 // get_mapgen_setting(name)
884 int ModApiMapgen::l_get_mapgen_setting(lua_State *L)
886 NO_MAP_LOCK_REQUIRED;
889 MapSettingsManager *settingsmgr =
890 getServer(L)->getEmergeManager()->map_settings_mgr;
892 const char *name = luaL_checkstring(L, 1);
893 if (!settingsmgr->getMapSetting(name, &value))
896 lua_pushstring(L, value.c_str());
900 // get_mapgen_setting_noiseparams(name)
901 int ModApiMapgen::l_get_mapgen_setting_noiseparams(lua_State *L)
903 NO_MAP_LOCK_REQUIRED;
906 MapSettingsManager *settingsmgr =
907 getServer(L)->getEmergeManager()->map_settings_mgr;
909 const char *name = luaL_checkstring(L, 1);
910 if (!settingsmgr->getMapSettingNoiseParams(name, &np))
913 push_noiseparams(L, &np);
917 // set_mapgen_setting(name, value, override_meta)
918 // set mapgen config values
919 int ModApiMapgen::l_set_mapgen_setting(lua_State *L)
921 NO_MAP_LOCK_REQUIRED;
923 MapSettingsManager *settingsmgr =
924 getServer(L)->getEmergeManager()->map_settings_mgr;
926 const char *name = luaL_checkstring(L, 1);
927 const char *value = luaL_checkstring(L, 2);
928 bool override_meta = readParam<bool>(L, 3, false);
930 if (!settingsmgr->setMapSetting(name, value, override_meta)) {
931 errorstream << "set_mapgen_setting: cannot set '"
932 << name << "' after initialization" << std::endl;
939 // set_mapgen_setting_noiseparams(name, noiseparams, set_default)
940 // set mapgen config values for noise parameters
941 int ModApiMapgen::l_set_mapgen_setting_noiseparams(lua_State *L)
943 NO_MAP_LOCK_REQUIRED;
945 MapSettingsManager *settingsmgr =
946 getServer(L)->getEmergeManager()->map_settings_mgr;
948 const char *name = luaL_checkstring(L, 1);
951 if (!read_noiseparams(L, 2, &np)) {
952 errorstream << "set_mapgen_setting_noiseparams: cannot set '" << name
953 << "'; invalid noiseparams table" << std::endl;
957 bool override_meta = readParam<bool>(L, 3, false);
959 if (!settingsmgr->setMapSettingNoiseParams(name, &np, override_meta)) {
960 errorstream << "set_mapgen_setting_noiseparams: cannot set '"
961 << name << "' after initialization" << std::endl;
968 // set_noiseparams(name, noiseparams, set_default)
969 // set global config values for noise parameters
970 int ModApiMapgen::l_set_noiseparams(lua_State *L)
972 NO_MAP_LOCK_REQUIRED;
974 const char *name = luaL_checkstring(L, 1);
977 if (!read_noiseparams(L, 2, &np)) {
978 errorstream << "set_noiseparams: cannot set '" << name
979 << "'; invalid noiseparams table" << std::endl;
983 bool set_default = !lua_isboolean(L, 3) || readParam<bool>(L, 3);
985 Settings::getLayer(set_default ? SL_DEFAULTS : SL_GLOBAL)->setNoiseParams(name, np);
991 // get_noiseparams(name)
992 int ModApiMapgen::l_get_noiseparams(lua_State *L)
994 NO_MAP_LOCK_REQUIRED;
996 std::string name = luaL_checkstring(L, 1);
999 if (!g_settings->getNoiseParams(name, np))
1002 push_noiseparams(L, &np);
1007 // set_gen_notify(flags, {deco_id_table})
1008 int ModApiMapgen::l_set_gen_notify(lua_State *L)
1010 NO_MAP_LOCK_REQUIRED;
1012 u32 flags = 0, flagmask = 0;
1013 EmergeManager *emerge = getServer(L)->getEmergeManager();
1015 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
1016 emerge->gen_notify_on &= ~flagmask;
1017 emerge->gen_notify_on |= flags;
1020 if (lua_istable(L, 2)) {
1022 while (lua_next(L, 2)) {
1023 if (lua_isnumber(L, -1))
1024 emerge->gen_notify_on_deco_ids.insert((u32)lua_tonumber(L, -1));
1034 int ModApiMapgen::l_get_gen_notify(lua_State *L)
1036 NO_MAP_LOCK_REQUIRED;
1038 EmergeManager *emerge = getServer(L)->getEmergeManager();
1039 push_flags_string(L, flagdesc_gennotify, emerge->gen_notify_on,
1040 emerge->gen_notify_on);
1044 for (u32 gen_notify_on_deco_id : emerge->gen_notify_on_deco_ids) {
1045 lua_pushnumber(L, gen_notify_on_deco_id);
1046 lua_rawseti(L, -2, i++);
1052 // get_decoration_id(decoration_name)
1053 // returns the decoration ID as used in gennotify
1054 int ModApiMapgen::l_get_decoration_id(lua_State *L)
1056 NO_MAP_LOCK_REQUIRED;
1058 const char *deco_str = luaL_checkstring(L, 1);
1062 const DecorationManager *dmgr =
1063 getServer(L)->getEmergeManager()->getDecorationManager();
1068 Decoration *deco = (Decoration *)dmgr->getByName(deco_str);
1073 lua_pushinteger(L, deco->index);
1079 // register_biome({lots of stuff})
1080 int ModApiMapgen::l_register_biome(lua_State *L)
1082 NO_MAP_LOCK_REQUIRED;
1085 luaL_checktype(L, index, LUA_TTABLE);
1087 const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
1088 BiomeManager *bmgr = getServer(L)->getEmergeManager()->getWritableBiomeManager();
1090 Biome *biome = read_biome_def(L, index, ndef);
1094 ObjDefHandle handle = bmgr->add(biome);
1095 if (handle == OBJDEF_INVALID_HANDLE) {
1100 lua_pushinteger(L, handle);
1105 // register_decoration({lots of stuff})
1106 int ModApiMapgen::l_register_decoration(lua_State *L)
1108 NO_MAP_LOCK_REQUIRED;
1111 luaL_checktype(L, index, LUA_TTABLE);
1113 const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
1114 EmergeManager *emerge = getServer(L)->getEmergeManager();
1115 DecorationManager *decomgr = emerge->getWritableDecorationManager();
1116 BiomeManager *biomemgr = emerge->getWritableBiomeManager();
1117 SchematicManager *schemmgr = emerge->getWritableSchematicManager();
1119 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
1120 "deco_type", es_DecorationType, -1);
1122 Decoration *deco = decomgr->create(decotype);
1124 errorstream << "register_decoration: decoration placement type "
1125 << decotype << " not implemented" << std::endl;
1129 deco->name = getstringfield_default(L, index, "name", "");
1130 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
1131 deco->y_min = getintfield_default(L, index, "y_min", -31000);
1132 deco->y_max = getintfield_default(L, index, "y_max", 31000);
1133 deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
1134 deco->place_offset_y = getintfield_default(L, index, "place_offset_y", 0);
1135 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
1136 if (deco->sidelen <= 0) {
1137 errorstream << "register_decoration: sidelen must be "
1138 "greater than 0" << std::endl;
1143 //// Get node name(s) to place decoration on
1144 size_t nread = getstringlistfield(L, index, "place_on", &deco->m_nodenames);
1145 deco->m_nnlistsizes.push_back(nread);
1147 //// Get decoration flags
1148 getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
1150 //// Get NoiseParams to define how decoration is placed
1151 lua_getfield(L, index, "noise_params");
1152 if (read_noiseparams(L, -1, &deco->np))
1153 deco->flags |= DECO_USE_NOISE;
1156 //// Get biomes associated with this decoration (if any)
1157 lua_getfield(L, index, "biomes");
1158 if (get_biome_list(L, -1, biomemgr, &deco->biomes))
1159 infostream << "register_decoration: couldn't get all biomes " << std::endl;
1162 //// Get node name(s) to 'spawn by'
1163 size_t nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames);
1164 deco->m_nnlistsizes.push_back(nnames);
1165 if (nnames == 0 && deco->nspawnby != -1) {
1166 errorstream << "register_decoration: no spawn_by nodes defined,"
1167 " but num_spawn_by specified" << std::endl;
1170 //// Handle decoration type-specific parameters
1171 bool success = false;
1174 success = read_deco_simple(L, (DecoSimple *)deco);
1176 case DECO_SCHEMATIC:
1177 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
1188 ndef->pendNodeResolve(deco);
1190 ObjDefHandle handle = decomgr->add(deco);
1191 if (handle == OBJDEF_INVALID_HANDLE) {
1196 lua_pushinteger(L, handle);
1201 bool read_deco_simple(lua_State *L, DecoSimple *deco)
1207 deco->deco_height = getintfield_default(L, index, "height", 1);
1208 deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
1210 if (deco->deco_height <= 0) {
1211 errorstream << "register_decoration: simple decoration height"
1212 " must be greater than 0" << std::endl;
1216 size_t nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames);
1217 deco->m_nnlistsizes.push_back(nnames);
1220 errorstream << "register_decoration: no decoration nodes "
1221 "defined" << std::endl;
1225 param2 = getintfield_default(L, index, "param2", 0);
1226 param2_max = getintfield_default(L, index, "param2_max", 0);
1228 if (param2 < 0 || param2 > 255 || param2_max < 0 || param2_max > 255) {
1229 errorstream << "register_decoration: param2 or param2_max out of bounds (0-255)"
1234 deco->deco_param2 = (u8)param2;
1235 deco->deco_param2_max = (u8)param2_max;
1241 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
1245 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
1246 ModApiMapgen::es_Rotation, ROTATE_0);
1248 StringMap replace_names;
1249 lua_getfield(L, index, "replacements");
1250 if (lua_istable(L, -1))
1251 read_schematic_replacements(L, -1, &replace_names);
1254 lua_getfield(L, index, "schematic");
1255 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
1258 deco->schematic = schem;
1259 return schem != NULL;
1263 // register_ore({lots of stuff})
1264 int ModApiMapgen::l_register_ore(lua_State *L)
1266 NO_MAP_LOCK_REQUIRED;
1269 luaL_checktype(L, index, LUA_TTABLE);
1271 const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
1272 EmergeManager *emerge = getServer(L)->getEmergeManager();
1273 BiomeManager *bmgr = emerge->getWritableBiomeManager();
1274 OreManager *oremgr = emerge->getWritableOreManager();
1276 enum OreType oretype = (OreType)getenumfield(L, index,
1277 "ore_type", es_OreType, ORE_SCATTER);
1278 Ore *ore = oremgr->create(oretype);
1280 errorstream << "register_ore: ore_type " << oretype << " not implemented\n";
1284 ore->name = getstringfield_default(L, index, "name", "");
1285 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
1286 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
1287 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
1288 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
1292 //// Get noise_threshold
1293 warn_if_field_exists(L, index, "noise_threshhold",
1294 "Deprecated: new name is \"noise_threshold\".");
1297 if (!getfloatfield(L, index, "noise_threshold", nthresh) &&
1298 !getfloatfield(L, index, "noise_threshhold", nthresh))
1300 ore->nthresh = nthresh;
1302 //// Get y_min/y_max
1303 warn_if_field_exists(L, index, "height_min",
1304 "Deprecated: new name is \"y_min\".");
1305 warn_if_field_exists(L, index, "height_max",
1306 "Deprecated: new name is \"y_max\".");
1309 if (!getintfield(L, index, "y_min", ymin) &&
1310 !getintfield(L, index, "height_min", ymin))
1312 if (!getintfield(L, index, "y_max", ymax) &&
1313 !getintfield(L, index, "height_max", ymax))
1318 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
1319 errorstream << "register_ore: clust_scarcity and clust_num_ores"
1320 "must be greater than 0" << std::endl;
1326 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
1328 //// Get biomes associated with this decoration (if any)
1329 lua_getfield(L, index, "biomes");
1330 if (get_biome_list(L, -1, bmgr, &ore->biomes))
1331 infostream << "register_ore: couldn't get all biomes " << std::endl;
1334 //// Get noise parameters if needed
1335 lua_getfield(L, index, "noise_params");
1336 if (read_noiseparams(L, -1, &ore->np)) {
1337 ore->flags |= OREFLAG_USE_NOISE;
1338 } else if (ore->needs_noise) {
1340 "register_ore: ore type requires 'noise_params' but it is not specified, falling back to defaults");
1344 //// Get type-specific parameters
1347 OreSheet *oresheet = (OreSheet *)ore;
1349 oresheet->column_height_min = getintfield_default(L, index,
1350 "column_height_min", 1);
1351 oresheet->column_height_max = getintfield_default(L, index,
1352 "column_height_max", ore->clust_size);
1353 oresheet->column_midpoint_factor = getfloatfield_default(L, index,
1354 "column_midpoint_factor", 0.5f);
1359 OrePuff *orepuff = (OrePuff *)ore;
1361 lua_getfield(L, index, "np_puff_top");
1362 read_noiseparams(L, -1, &orepuff->np_puff_top);
1365 lua_getfield(L, index, "np_puff_bottom");
1366 read_noiseparams(L, -1, &orepuff->np_puff_bottom);
1372 OreVein *orevein = (OreVein *)ore;
1374 orevein->random_factor = getfloatfield_default(L, index,
1375 "random_factor", 1.f);
1380 OreStratum *orestratum = (OreStratum *)ore;
1382 lua_getfield(L, index, "np_stratum_thickness");
1383 if (read_noiseparams(L, -1, &orestratum->np_stratum_thickness))
1384 ore->flags |= OREFLAG_USE_NOISE2;
1387 orestratum->stratum_thickness = getintfield_default(L, index,
1388 "stratum_thickness", 8);
1396 ObjDefHandle handle = oremgr->add(ore);
1397 if (handle == OBJDEF_INVALID_HANDLE) {
1402 ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
1404 size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
1405 ore->m_nnlistsizes.push_back(nnames);
1407 ndef->pendNodeResolve(ore);
1409 lua_pushinteger(L, handle);
1414 // register_schematic({schematic}, replacements={})
1415 int ModApiMapgen::l_register_schematic(lua_State *L)
1417 NO_MAP_LOCK_REQUIRED;
1419 SchematicManager *schemmgr =
1420 getServer(L)->getEmergeManager()->getWritableSchematicManager();
1422 StringMap replace_names;
1423 if (lua_istable(L, 2))
1424 read_schematic_replacements(L, 2, &replace_names);
1426 Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
1431 ObjDefHandle handle = schemmgr->add(schem);
1432 if (handle == OBJDEF_INVALID_HANDLE) {
1437 lua_pushinteger(L, handle);
1442 // clear_registered_biomes()
1443 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
1445 NO_MAP_LOCK_REQUIRED;
1447 BiomeManager *bmgr =
1448 getServer(L)->getEmergeManager()->getWritableBiomeManager();
1454 // clear_registered_decorations()
1455 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
1457 NO_MAP_LOCK_REQUIRED;
1459 DecorationManager *dmgr =
1460 getServer(L)->getEmergeManager()->getWritableDecorationManager();
1466 // clear_registered_ores()
1467 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
1469 NO_MAP_LOCK_REQUIRED;
1472 getServer(L)->getEmergeManager()->getWritableOreManager();
1478 // clear_registered_schematics()
1479 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
1481 NO_MAP_LOCK_REQUIRED;
1483 SchematicManager *smgr =
1484 getServer(L)->getEmergeManager()->getWritableSchematicManager();
1490 // generate_ores(vm, p1, p2, [ore_id])
1491 int ModApiMapgen::l_generate_ores(lua_State *L)
1493 NO_MAP_LOCK_REQUIRED;
1495 EmergeManager *emerge = getServer(L)->getEmergeManager();
1498 mg.seed = emerge->mgparams->seed;
1499 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1500 mg.ndef = getServer(L)->getNodeDefManager();
1502 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1503 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1504 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1505 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1506 sortBoxVerticies(pmin, pmax);
1508 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1510 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
1516 // generate_decorations(vm, p1, p2, [deco_id])
1517 int ModApiMapgen::l_generate_decorations(lua_State *L)
1519 NO_MAP_LOCK_REQUIRED;
1521 EmergeManager *emerge = getServer(L)->getEmergeManager();
1524 mg.seed = emerge->mgparams->seed;
1525 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1526 mg.ndef = getServer(L)->getNodeDefManager();
1528 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1529 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1530 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1531 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1532 sortBoxVerticies(pmin, pmax);
1534 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1536 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
1542 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
1543 int ModApiMapgen::l_create_schematic(lua_State *L)
1547 const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
1549 const char *filename = luaL_checkstring(L, 4);
1550 CHECK_SECURE_PATH(L, filename, true);
1552 Map *map = &(getEnv(L)->getMap());
1555 v3s16 p1 = check_v3s16(L, 1);
1556 v3s16 p2 = check_v3s16(L, 2);
1557 sortBoxVerticies(p1, p2);
1559 std::vector<std::pair<v3s16, u8> > prob_list;
1560 if (lua_istable(L, 3)) {
1562 while (lua_next(L, 3)) {
1563 if (lua_istable(L, -1)) {
1564 lua_getfield(L, -1, "pos");
1565 v3s16 pos = check_v3s16(L, -1);
1568 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1569 prob_list.emplace_back(pos, prob);
1576 std::vector<std::pair<s16, u8> > slice_prob_list;
1577 if (lua_istable(L, 5)) {
1579 while (lua_next(L, 5)) {
1580 if (lua_istable(L, -1)) {
1581 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1582 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1583 slice_prob_list.emplace_back(ypos, prob);
1590 if (!schem.getSchematicFromMap(map, p1, p2)) {
1591 errorstream << "create_schematic: failed to get schematic "
1592 "from map" << std::endl;
1596 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1598 schem.saveSchematicToFile(filename, ndef);
1599 actionstream << "create_schematic: saved schematic file '"
1600 << filename << "'." << std::endl;
1602 lua_pushboolean(L, true);
1607 // place_schematic(p, schematic, rotation,
1608 // replacements, force_placement, flagstring)
1609 int ModApiMapgen::l_place_schematic(lua_State *L)
1615 ServerMap *map = &(env->getServerMap());
1616 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1619 v3s16 p = check_v3s16(L, 1);
1623 std::string enumstr = readParam<std::string>(L, 3, "");
1624 if (!enumstr.empty())
1625 string_to_enum(es_Rotation, rot, enumstr);
1627 //// Read force placement
1628 bool force_placement = true;
1629 if (lua_isboolean(L, 5))
1630 force_placement = readParam<bool>(L, 5);
1632 //// Read node replacements
1633 StringMap replace_names;
1634 if (lua_istable(L, 4))
1635 read_schematic_replacements(L, 4, &replace_names);
1638 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1640 errorstream << "place_schematic: failed to get schematic" << std::endl;
1646 read_flags(L, 6, flagdesc_deco, &flags, NULL);
1648 schem->placeOnMap(map, p, flags, (Rotation)rot, force_placement);
1650 lua_pushboolean(L, true);
1655 // place_schematic_on_vmanip(vm, p, schematic, rotation,
1656 // replacements, force_placement, flagstring)
1657 int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L)
1659 NO_MAP_LOCK_REQUIRED;
1661 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1663 //// Read VoxelManip object
1664 MMVManip *vm = LuaVoxelManip::checkobject(L, 1)->vm;
1667 v3s16 p = check_v3s16(L, 2);
1671 std::string enumstr = readParam<std::string>(L, 4, "");
1672 if (!enumstr.empty())
1673 string_to_enum(es_Rotation, rot, std::string(enumstr));
1675 //// Read force placement
1676 bool force_placement = true;
1677 if (lua_isboolean(L, 6))
1678 force_placement = readParam<bool>(L, 6);
1680 //// Read node replacements
1681 StringMap replace_names;
1682 if (lua_istable(L, 5))
1683 read_schematic_replacements(L, 5, &replace_names);
1686 Schematic *schem = get_or_load_schematic(L, 3, schemmgr, &replace_names);
1688 errorstream << "place_schematic: failed to get schematic" << std::endl;
1694 read_flags(L, 7, flagdesc_deco, &flags, NULL);
1696 bool schematic_did_fit = schem->placeOnVManip(
1697 vm, p, flags, (Rotation)rot, force_placement);
1699 lua_pushboolean(L, schematic_did_fit);
1704 // serialize_schematic(schematic, format, options={...})
1705 int ModApiMapgen::l_serialize_schematic(lua_State *L)
1707 NO_MAP_LOCK_REQUIRED;
1709 const SchematicManager *schemmgr = getServer(L)->getEmergeManager()->getSchematicManager();
1712 bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
1713 u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
1716 bool was_loaded = false;
1717 const Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1719 schem = load_schematic(L, 1, NULL, NULL);
1723 errorstream << "serialize_schematic: failed to get schematic" << std::endl;
1727 //// Read format of definition to save as
1728 int schem_format = SCHEM_FMT_MTS;
1729 std::string enumstr = readParam<std::string>(L, 2, "");
1730 if (!enumstr.empty())
1731 string_to_enum(es_SchematicFormatType, schem_format, enumstr);
1733 //// Serialize to binary string
1734 std::ostringstream os(std::ios_base::binary);
1735 switch (schem_format) {
1737 schem->serializeToMts(&os, schem->m_nodenames);
1740 schem->serializeToLua(&os, schem->m_nodenames,
1741 use_comments, indent_spaces);
1750 std::string ser = os.str();
1751 lua_pushlstring(L, ser.c_str(), ser.length());
1755 // read_schematic(schematic, options={...})
1756 int ModApiMapgen::l_read_schematic(lua_State *L)
1758 NO_MAP_LOCK_REQUIRED;
1760 const SchematicManager *schemmgr =
1761 getServer(L)->getEmergeManager()->getSchematicManager();
1764 std::string write_yslice = getstringfield_default(L, 2, "write_yslice_prob", "all");
1767 bool was_loaded = false;
1768 Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1770 schem = load_schematic(L, 1, NULL, NULL);
1774 errorstream << "read_schematic: failed to get schematic" << std::endl;
1779 //// Create the Lua table
1780 u32 numnodes = schem->size.X * schem->size.Y * schem->size.Z;
1781 const std::vector<std::string> &names = schem->m_nodenames;
1783 lua_createtable(L, 0, (write_yslice == "none") ? 2 : 3);
1785 // Create the size field
1786 push_v3s16(L, schem->size);
1787 lua_setfield(L, 1, "size");
1789 // Create the yslice_prob field
1790 if (write_yslice != "none") {
1791 lua_createtable(L, schem->size.Y, 0);
1792 for (u16 y = 0; y != schem->size.Y; ++y) {
1793 u8 probability = schem->slice_probs[y] & MTSCHEM_PROB_MASK;
1794 if (probability < MTSCHEM_PROB_ALWAYS || write_yslice != "low") {
1795 lua_createtable(L, 0, 2);
1796 lua_pushinteger(L, y);
1797 lua_setfield(L, 3, "ypos");
1798 lua_pushinteger(L, probability * 2);
1799 lua_setfield(L, 3, "prob");
1800 lua_rawseti(L, 2, y + 1);
1803 lua_setfield(L, 1, "yslice_prob");
1806 // Create the data field
1807 lua_createtable(L, numnodes, 0); // data table
1808 for (u32 i = 0; i < numnodes; ++i) {
1809 MapNode node = schem->schemdata[i];
1810 u8 probability = node.param1 & MTSCHEM_PROB_MASK;
1811 bool force_place = node.param1 & MTSCHEM_FORCE_PLACE;
1812 lua_createtable(L, 0, force_place ? 4 : 3);
1813 lua_pushstring(L, names[schem->schemdata[i].getContent()].c_str());
1814 lua_setfield(L, 3, "name");
1815 lua_pushinteger(L, probability * 2);
1816 lua_setfield(L, 3, "prob");
1817 lua_pushinteger(L, node.param2);
1818 lua_setfield(L, 3, "param2");
1820 lua_pushboolean(L, 1);
1821 lua_setfield(L, 3, "force_place");
1823 lua_rawseti(L, 2, i + 1);
1825 lua_setfield(L, 1, "data");
1834 void ModApiMapgen::Initialize(lua_State *L, int top)
1836 API_FCT(get_biome_id);
1837 API_FCT(get_biome_name);
1839 API_FCT(get_humidity);
1840 API_FCT(get_biome_data);
1841 API_FCT(get_mapgen_object);
1842 API_FCT(get_spawn_level);
1844 API_FCT(get_mapgen_params);
1845 API_FCT(set_mapgen_params);
1846 API_FCT(get_mapgen_setting);
1847 API_FCT(set_mapgen_setting);
1848 API_FCT(get_mapgen_setting_noiseparams);
1849 API_FCT(set_mapgen_setting_noiseparams);
1850 API_FCT(set_noiseparams);
1851 API_FCT(get_noiseparams);
1852 API_FCT(set_gen_notify);
1853 API_FCT(get_gen_notify);
1854 API_FCT(get_decoration_id);
1856 API_FCT(register_biome);
1857 API_FCT(register_decoration);
1858 API_FCT(register_ore);
1859 API_FCT(register_schematic);
1861 API_FCT(clear_registered_biomes);
1862 API_FCT(clear_registered_decorations);
1863 API_FCT(clear_registered_ores);
1864 API_FCT(clear_registered_schematics);
1866 API_FCT(generate_ores);
1867 API_FCT(generate_decorations);
1868 API_FCT(create_schematic);
1869 API_FCT(place_schematic);
1870 API_FCT(place_schematic_on_vmanip);
1871 API_FCT(serialize_schematic);
1872 API_FCT(read_schematic);