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"
32 #include "mg_decoration.h"
33 #include "mg_schematic.h"
34 #include "mapgen_v5.h"
35 #include "mapgen_v7.h"
40 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
42 {BIOMETYPE_NORMAL, "normal"},
43 {BIOMETYPE_LIQUID, "liquid"},
44 {BIOMETYPE_NETHER, "nether"},
45 {BIOMETYPE_AETHER, "aether"},
46 {BIOMETYPE_FLAT, "flat"},
50 struct EnumString ModApiMapgen::es_DecorationType[] =
52 {DECO_SIMPLE, "simple"},
53 {DECO_SCHEMATIC, "schematic"},
54 {DECO_LSYSTEM, "lsystem"},
58 struct EnumString ModApiMapgen::es_MapgenObject[] =
60 {MGOBJ_VMANIP, "voxelmanip"},
61 {MGOBJ_HEIGHTMAP, "heightmap"},
62 {MGOBJ_BIOMEMAP, "biomemap"},
63 {MGOBJ_HEATMAP, "heatmap"},
64 {MGOBJ_HUMIDMAP, "humiditymap"},
65 {MGOBJ_GENNOTIFY, "gennotify"},
69 struct EnumString ModApiMapgen::es_OreType[] =
71 {ORE_SCATTER, "scatter"},
76 {ORE_STRATUM, "stratum"},
80 struct EnumString ModApiMapgen::es_Rotation[] =
86 {ROTATE_RAND, "random"},
90 struct EnumString ModApiMapgen::es_SchematicFormatType[] =
92 {SCHEM_FMT_HANDLE, "handle"},
93 {SCHEM_FMT_MTS, "mts"},
94 {SCHEM_FMT_LUA, "lua"},
98 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
100 Biome *get_or_load_biome(lua_State *L, int index,
101 BiomeManager *biomemgr);
102 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
103 size_t get_biome_list(lua_State *L, int index,
104 BiomeManager *biomemgr, std::unordered_set<u8> *biome_id_list);
106 Schematic *get_or_load_schematic(lua_State *L, int index,
107 SchematicManager *schemmgr, StringMap *replace_names);
108 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
109 StringMap *replace_names);
110 Schematic *load_schematic_from_def(lua_State *L, int index,
111 INodeDefManager *ndef, StringMap *replace_names);
112 bool read_schematic_def(lua_State *L, int index,
113 Schematic *schem, std::vector<std::string> *names);
115 bool read_deco_simple(lua_State *L, DecoSimple *deco);
116 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
119 ///////////////////////////////////////////////////////////////////////////////
121 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
124 index = lua_gettop(L) + 1 + index;
126 // If a number, assume this is a handle to an object def
127 if (lua_isnumber(L, index))
128 return objmgr->get(lua_tointeger(L, index));
130 // If a string, assume a name is given instead
131 if (lua_isstring(L, index))
132 return objmgr->getByName(lua_tostring(L, index));
137 ///////////////////////////////////////////////////////////////////////////////
139 Schematic *get_or_load_schematic(lua_State *L, int index,
140 SchematicManager *schemmgr, StringMap *replace_names)
143 index = lua_gettop(L) + 1 + index;
145 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
149 schem = load_schematic(L, index, schemmgr->getNodeDef(),
154 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
163 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
164 StringMap *replace_names)
167 index = lua_gettop(L) + 1 + index;
169 Schematic *schem = NULL;
171 if (lua_istable(L, index)) {
172 schem = load_schematic_from_def(L, index, ndef,
178 } else if (lua_isnumber(L, index)) {
180 } else if (lua_isstring(L, index)) {
181 schem = SchematicManager::create(SCHEMATIC_NORMAL);
183 std::string filepath = lua_tostring(L, index);
184 if (!fs::IsPathAbsolute(filepath))
185 filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
187 if (!schem->loadSchematicFromFile(filepath, ndef,
198 Schematic *load_schematic_from_def(lua_State *L, int index,
199 INodeDefManager *ndef, StringMap *replace_names)
201 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
203 if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
208 size_t num_nodes = schem->m_nodenames.size();
210 schem->m_nnlistsizes.push_back(num_nodes);
213 for (size_t i = 0; i != num_nodes; i++) {
214 StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
215 if (it != replace_names->end())
216 schem->m_nodenames[i] = it->second;
221 ndef->pendNodeResolve(schem);
227 bool read_schematic_def(lua_State *L, int index,
228 Schematic *schem, std::vector<std::string> *names)
230 if (!lua_istable(L, index))
233 //// Get schematic size
234 lua_getfield(L, index, "size");
235 v3s16 size = check_v3s16(L, -1);
240 //// Get schematic data
241 lua_getfield(L, index, "data");
242 luaL_checktype(L, -1, LUA_TTABLE);
244 u32 numnodes = size.X * size.Y * size.Z;
245 schem->schemdata = new MapNode[numnodes];
247 size_t names_base = names->size();
248 std::unordered_map<std::string, content_t> name_id_map;
251 for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
257 if (!getstringfield(L, -1, "name", name))
258 throw LuaError("Schematic data definition with missing name field");
260 //// Read param1/prob
262 if (!getintfield(L, -1, "param1", param1) &&
263 !getintfield(L, -1, "prob", param1))
264 param1 = MTSCHEM_PROB_ALWAYS_OLD;
267 u8 param2 = getintfield_default(L, -1, "param2", 0);
269 //// Find or add new nodename-to-ID mapping
270 std::unordered_map<std::string, content_t>::iterator it = name_id_map.find(name);
271 content_t name_index;
272 if (it != name_id_map.end()) {
273 name_index = it->second;
275 name_index = names->size() - names_base;
276 name_id_map[name] = name_index;
277 names->push_back(name);
280 //// Perform probability/force_place fixup on param1
282 if (getboolfield_default(L, -1, "force_place", false))
283 param1 |= MTSCHEM_FORCE_PLACE;
285 //// Actually set the node in the schematic
286 schem->schemdata[i] = MapNode(name_index, param1, param2);
290 errorstream << "read_schematic_def: incorrect number of "
291 "nodes provided in raw schematic data (got " << i <<
292 ", expected " << numnodes << ")." << std::endl;
296 //// Get Y-slice probability values (if present)
297 schem->slice_probs = new u8[size.Y];
298 for (i = 0; i != (u32) size.Y; i++)
299 schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
301 lua_getfield(L, index, "yslice_prob");
302 if (lua_istable(L, -1)) {
303 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
305 if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
306 !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
309 schem->slice_probs[ypos] >>= 1;
317 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
320 index = lua_gettop(L) + 1 + index;
323 while (lua_next(L, index)) {
324 std::string replace_from;
325 std::string replace_to;
327 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
328 lua_rawgeti(L, -1, 1);
329 if (!lua_isstring(L, -1))
330 throw LuaError("schematics: replace_from field is not a string");
331 replace_from = lua_tostring(L, -1);
334 lua_rawgeti(L, -1, 2);
335 if (!lua_isstring(L, -1))
336 throw LuaError("schematics: replace_to field is not a string");
337 replace_to = lua_tostring(L, -1);
339 } else { // New {x = "y", ...} format
340 if (!lua_isstring(L, -2))
341 throw LuaError("schematics: replace_from field is not a string");
342 replace_from = lua_tostring(L, -2);
343 if (!lua_isstring(L, -1))
344 throw LuaError("schematics: replace_to field is not a string");
345 replace_to = lua_tostring(L, -1);
348 replace_names->insert(std::make_pair(replace_from, replace_to));
353 ///////////////////////////////////////////////////////////////////////////////
355 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
358 index = lua_gettop(L) + 1 + index;
360 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
364 biome = read_biome_def(L, index, biomemgr->getNodeDef());
368 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
377 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
379 if (!lua_istable(L, index))
382 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
383 ModApiMapgen::es_BiomeTerrainType, BIOMETYPE_NORMAL);
384 Biome *b = BiomeManager::create(biometype);
386 b->name = getstringfield_default(L, index, "name", "");
387 b->depth_top = getintfield_default(L, index, "depth_top", 0);
388 b->depth_filler = getintfield_default(L, index, "depth_filler", -31000);
389 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
390 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);
393 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
394 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
395 b->flags = 0; //reserved
397 std::vector<std::string> &nn = b->m_nodenames;
398 nn.push_back(getstringfield_default(L, index, "node_top", ""));
399 nn.push_back(getstringfield_default(L, index, "node_filler", ""));
400 nn.push_back(getstringfield_default(L, index, "node_stone", ""));
401 nn.push_back(getstringfield_default(L, index, "node_water_top", ""));
402 nn.push_back(getstringfield_default(L, index, "node_water", ""));
403 nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
404 nn.push_back(getstringfield_default(L, index, "node_riverbed", ""));
405 nn.push_back(getstringfield_default(L, index, "node_dust", ""));
406 ndef->pendNodeResolve(b);
412 size_t get_biome_list(lua_State *L, int index,
413 BiomeManager *biomemgr, std::unordered_set<u8> *biome_id_list)
416 index = lua_gettop(L) + 1 + index;
418 if (lua_isnil(L, index))
421 bool is_single = true;
422 if (lua_istable(L, index)) {
423 lua_getfield(L, index, "name");
424 is_single = !lua_isnil(L, -1);
429 Biome *biome = get_or_load_biome(L, index, biomemgr);
431 infostream << "get_biome_list: failed to get biome '"
432 << (lua_isstring(L, index) ? lua_tostring(L, index) : "")
433 << "'." << std::endl;
437 biome_id_list->insert(biome->index);
441 // returns number of failed resolutions
442 size_t fail_count = 0;
445 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
447 Biome *biome = get_or_load_biome(L, -1, biomemgr);
450 infostream << "get_biome_list: failed to get biome '"
451 << (lua_isstring(L, -1) ? lua_tostring(L, -1) : "")
456 biome_id_list->insert(biome->index);
462 ///////////////////////////////////////////////////////////////////////////////
464 // get_biome_id(biomename)
465 // returns the biome id used in biomemap
466 int ModApiMapgen::l_get_biome_id(lua_State *L)
468 NO_MAP_LOCK_REQUIRED;
470 const char *biome_str = lua_tostring(L, 1);
474 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
479 Biome *biome = (Biome *)bmgr->getByName(biome_str);
481 if (!biome || biome->index == OBJDEF_INVALID_INDEX)
484 lua_pushinteger(L, biome->index);
490 // get_mapgen_object(objectname)
491 // returns the requested object used during map generation
492 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
494 NO_MAP_LOCK_REQUIRED;
496 const char *mgobjstr = lua_tostring(L, 1);
499 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
502 enum MapgenObject mgobj = (MapgenObject)mgobjint;
504 EmergeManager *emerge = getServer(L)->getEmergeManager();
505 Mapgen *mg = emerge->getCurrentMapgen();
507 throw LuaError("Must only be called in a mapgen thread!");
509 size_t maplen = mg->csize.X * mg->csize.Z;
513 MMVManip *vm = mg->vm;
516 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
517 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
518 luaL_getmetatable(L, "VoxelManip");
519 lua_setmetatable(L, -2);
522 push_v3s16(L, vm->m_area.MinEdge);
525 push_v3s16(L, vm->m_area.MaxEdge);
529 case MGOBJ_HEIGHTMAP: {
534 for (size_t i = 0; i != maplen; i++) {
535 lua_pushinteger(L, mg->heightmap[i]);
536 lua_rawseti(L, -2, i + 1);
541 case MGOBJ_BIOMEMAP: {
546 for (size_t i = 0; i != maplen; i++) {
547 lua_pushinteger(L, mg->biomegen->biomemap[i]);
548 lua_rawseti(L, -2, i + 1);
553 case MGOBJ_HEATMAP: {
554 if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
557 BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
560 for (size_t i = 0; i != maplen; i++) {
561 lua_pushnumber(L, bg->heatmap[i]);
562 lua_rawseti(L, -2, i + 1);
568 case MGOBJ_HUMIDMAP: {
569 if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
572 BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
575 for (size_t i = 0; i != maplen; i++) {
576 lua_pushnumber(L, bg->humidmap[i]);
577 lua_rawseti(L, -2, i + 1);
582 case MGOBJ_GENNOTIFY: {
583 std::map<std::string, std::vector<v3s16> >event_map;
584 std::map<std::string, std::vector<v3s16> >::iterator it;
586 mg->gennotify.getEvents(event_map);
589 for (it = event_map.begin(); it != event_map.end(); ++it) {
592 for (size_t j = 0; j != it->second.size(); j++) {
593 push_v3s16(L, it->second[j]);
594 lua_rawseti(L, -2, j + 1);
597 lua_setfield(L, -2, it->first.c_str());
608 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
610 NO_MAP_LOCK_REQUIRED;
612 log_deprecated(L, "get_mapgen_params is deprecated; "
613 "use get_mapgen_setting instead");
617 MapSettingsManager *settingsmgr =
618 getServer(L)->getEmergeManager()->map_settings_mgr;
622 settingsmgr->getMapSetting("mg_name", &value);
623 lua_pushstring(L, value.c_str());
624 lua_setfield(L, -2, "mgname");
626 settingsmgr->getMapSetting("seed", &value);
627 std::istringstream ss(value);
630 lua_pushinteger(L, seed);
631 lua_setfield(L, -2, "seed");
633 settingsmgr->getMapSetting("water_level", &value);
634 lua_pushinteger(L, stoi(value, -32768, 32767));
635 lua_setfield(L, -2, "water_level");
637 settingsmgr->getMapSetting("chunksize", &value);
638 lua_pushinteger(L, stoi(value, -32768, 32767));
639 lua_setfield(L, -2, "chunksize");
641 settingsmgr->getMapSetting("mg_flags", &value);
642 lua_pushstring(L, value.c_str());
643 lua_setfield(L, -2, "flags");
649 // set_mapgen_params(params)
650 // set mapgen parameters
651 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
653 NO_MAP_LOCK_REQUIRED;
655 log_deprecated(L, "set_mapgen_params is deprecated; "
656 "use set_mapgen_setting instead");
658 if (!lua_istable(L, 1))
661 MapSettingsManager *settingsmgr =
662 getServer(L)->getEmergeManager()->map_settings_mgr;
664 lua_getfield(L, 1, "mgname");
665 if (lua_isstring(L, -1))
666 settingsmgr->setMapSetting("mg_name", lua_tostring(L, -1), true);
668 lua_getfield(L, 1, "seed");
669 if (lua_isnumber(L, -1))
670 settingsmgr->setMapSetting("seed", lua_tostring(L, -1), true);
672 lua_getfield(L, 1, "water_level");
673 if (lua_isnumber(L, -1))
674 settingsmgr->setMapSetting("water_level", lua_tostring(L, -1), true);
676 lua_getfield(L, 1, "chunksize");
677 if (lua_isnumber(L, -1))
678 settingsmgr->setMapSetting("chunksize", lua_tostring(L, -1), true);
680 warn_if_field_exists(L, 1, "flagmask",
681 "Deprecated: flags field now includes unset flags.");
683 lua_getfield(L, 1, "flags");
684 if (lua_isstring(L, -1))
685 settingsmgr->setMapSetting("mg_flags", lua_tostring(L, -1), true);
690 // get_mapgen_setting(name)
691 int ModApiMapgen::l_get_mapgen_setting(lua_State *L)
693 NO_MAP_LOCK_REQUIRED;
696 MapSettingsManager *settingsmgr =
697 getServer(L)->getEmergeManager()->map_settings_mgr;
699 const char *name = luaL_checkstring(L, 1);
700 if (!settingsmgr->getMapSetting(name, &value))
703 lua_pushstring(L, value.c_str());
707 // get_mapgen_setting_noiseparams(name)
708 int ModApiMapgen::l_get_mapgen_setting_noiseparams(lua_State *L)
710 NO_MAP_LOCK_REQUIRED;
713 MapSettingsManager *settingsmgr =
714 getServer(L)->getEmergeManager()->map_settings_mgr;
716 const char *name = luaL_checkstring(L, 1);
717 if (!settingsmgr->getMapSettingNoiseParams(name, &np))
720 push_noiseparams(L, &np);
724 // set_mapgen_setting(name, value, override_meta)
725 // set mapgen config values
726 int ModApiMapgen::l_set_mapgen_setting(lua_State *L)
728 NO_MAP_LOCK_REQUIRED;
730 MapSettingsManager *settingsmgr =
731 getServer(L)->getEmergeManager()->map_settings_mgr;
733 const char *name = luaL_checkstring(L, 1);
734 const char *value = luaL_checkstring(L, 2);
735 bool override_meta = lua_isboolean(L, 3) && lua_toboolean(L, 3);
737 if (!settingsmgr->setMapSetting(name, value, override_meta)) {
738 errorstream << "set_mapgen_setting: cannot set '"
739 << name << "' after initialization" << std::endl;
746 // set_mapgen_setting_noiseparams(name, noiseparams, set_default)
747 // set mapgen config values for noise parameters
748 int ModApiMapgen::l_set_mapgen_setting_noiseparams(lua_State *L)
750 NO_MAP_LOCK_REQUIRED;
752 MapSettingsManager *settingsmgr =
753 getServer(L)->getEmergeManager()->map_settings_mgr;
755 const char *name = luaL_checkstring(L, 1);
758 if (!read_noiseparams(L, 2, &np)) {
759 errorstream << "set_mapgen_setting_noiseparams: cannot set '" << name
760 << "'; invalid noiseparams table" << std::endl;
764 bool override_meta = lua_isboolean(L, 3) && lua_toboolean(L, 3);
766 if (!settingsmgr->setMapSettingNoiseParams(name, &np, override_meta)) {
767 errorstream << "set_mapgen_setting_noiseparams: cannot set '"
768 << name << "' after initialization" << std::endl;
775 // set_noiseparams(name, noiseparams, set_default)
776 // set global config values for noise parameters
777 int ModApiMapgen::l_set_noiseparams(lua_State *L)
779 NO_MAP_LOCK_REQUIRED;
781 const char *name = luaL_checkstring(L, 1);
784 if (!read_noiseparams(L, 2, &np)) {
785 errorstream << "set_noiseparams: cannot set '" << name
786 << "'; invalid noiseparams table" << std::endl;
790 bool set_default = !lua_isboolean(L, 3) || lua_toboolean(L, 3);
792 g_settings->setNoiseParams(name, np, set_default);
798 // get_noiseparams(name)
799 int ModApiMapgen::l_get_noiseparams(lua_State *L)
801 NO_MAP_LOCK_REQUIRED;
803 std::string name = luaL_checkstring(L, 1);
806 if (!g_settings->getNoiseParams(name, np))
809 push_noiseparams(L, &np);
814 // set_gen_notify(flags, {deco_id_table})
815 int ModApiMapgen::l_set_gen_notify(lua_State *L)
817 NO_MAP_LOCK_REQUIRED;
819 u32 flags = 0, flagmask = 0;
820 EmergeManager *emerge = getServer(L)->getEmergeManager();
822 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
823 emerge->gen_notify_on &= ~flagmask;
824 emerge->gen_notify_on |= flags;
827 if (lua_istable(L, 2)) {
829 while (lua_next(L, 2)) {
830 if (lua_isnumber(L, -1))
831 emerge->gen_notify_on_deco_ids.insert((u32)lua_tonumber(L, -1));
841 int ModApiMapgen::l_get_gen_notify(lua_State *L)
843 NO_MAP_LOCK_REQUIRED;
845 EmergeManager *emerge = getServer(L)->getEmergeManager();
846 push_flags_string(L, flagdesc_gennotify, emerge->gen_notify_on,
847 emerge->gen_notify_on);
851 for (u32 gen_notify_on_deco_id : emerge->gen_notify_on_deco_ids) {
852 lua_pushnumber(L, gen_notify_on_deco_id);
853 lua_rawseti(L, -2, i);
860 // register_biome({lots of stuff})
861 int ModApiMapgen::l_register_biome(lua_State *L)
863 NO_MAP_LOCK_REQUIRED;
866 luaL_checktype(L, index, LUA_TTABLE);
868 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
869 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
871 Biome *biome = read_biome_def(L, index, ndef);
875 ObjDefHandle handle = bmgr->add(biome);
876 if (handle == OBJDEF_INVALID_HANDLE) {
881 lua_pushinteger(L, handle);
886 // register_decoration({lots of stuff})
887 int ModApiMapgen::l_register_decoration(lua_State *L)
889 NO_MAP_LOCK_REQUIRED;
892 luaL_checktype(L, index, LUA_TTABLE);
894 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
895 DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
896 BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr;
897 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
899 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
900 "deco_type", es_DecorationType, -1);
902 Decoration *deco = decomgr->create(decotype);
904 errorstream << "register_decoration: decoration placement type "
905 << decotype << " not implemented" << std::endl;
909 deco->name = getstringfield_default(L, index, "name", "");
910 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
911 deco->y_min = getintfield_default(L, index, "y_min", -31000);
912 deco->y_max = getintfield_default(L, index, "y_max", 31000);
913 deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
914 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
915 if (deco->sidelen <= 0) {
916 errorstream << "register_decoration: sidelen must be "
917 "greater than 0" << std::endl;
922 //// Get node name(s) to place decoration on
923 size_t nread = getstringlistfield(L, index, "place_on", &deco->m_nodenames);
924 deco->m_nnlistsizes.push_back(nread);
926 //// Get decoration flags
927 getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
929 //// Get NoiseParams to define how decoration is placed
930 lua_getfield(L, index, "noise_params");
931 if (read_noiseparams(L, -1, &deco->np))
932 deco->flags |= DECO_USE_NOISE;
935 //// Get biomes associated with this decoration (if any)
936 lua_getfield(L, index, "biomes");
937 if (get_biome_list(L, -1, biomemgr, &deco->biomes))
938 infostream << "register_decoration: couldn't get all biomes " << std::endl;
941 //// Get node name(s) to 'spawn by'
942 size_t nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames);
943 deco->m_nnlistsizes.push_back(nnames);
944 if (nnames == 0 && deco->nspawnby != -1) {
945 errorstream << "register_decoration: no spawn_by nodes defined,"
946 " but num_spawn_by specified" << std::endl;
949 //// Handle decoration type-specific parameters
950 bool success = false;
953 success = read_deco_simple(L, (DecoSimple *)deco);
956 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
967 ndef->pendNodeResolve(deco);
969 ObjDefHandle handle = decomgr->add(deco);
970 if (handle == OBJDEF_INVALID_HANDLE) {
975 lua_pushinteger(L, handle);
980 bool read_deco_simple(lua_State *L, DecoSimple *deco)
985 deco->deco_height = getintfield_default(L, index, "height", 1);
986 deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
988 if (deco->deco_height <= 0) {
989 errorstream << "register_decoration: simple decoration height"
990 " must be greater than 0" << std::endl;
994 size_t nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames);
995 deco->m_nnlistsizes.push_back(nnames);
997 errorstream << "register_decoration: no decoration nodes "
998 "defined" << std::endl;
1002 param2 = getintfield_default(L, index, "param2", 0);
1003 if ((param2 < 0) || (param2 > 255)) {
1004 errorstream << "register_decoration: param2 out of bounds (0-255)"
1008 deco->deco_param2 = (u8)param2;
1014 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
1018 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
1019 ModApiMapgen::es_Rotation, ROTATE_0);
1021 deco->place_offset_y = getintfield_default(L, index, "place_offset_y", 0);
1023 StringMap replace_names;
1024 lua_getfield(L, index, "replacements");
1025 if (lua_istable(L, -1))
1026 read_schematic_replacements(L, -1, &replace_names);
1029 lua_getfield(L, index, "schematic");
1030 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
1033 deco->schematic = schem;
1034 return schem != NULL;
1038 // register_ore({lots of stuff})
1039 int ModApiMapgen::l_register_ore(lua_State *L)
1041 NO_MAP_LOCK_REQUIRED;
1044 luaL_checktype(L, index, LUA_TTABLE);
1046 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1047 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
1048 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
1050 enum OreType oretype = (OreType)getenumfield(L, index,
1051 "ore_type", es_OreType, ORE_SCATTER);
1052 Ore *ore = oremgr->create(oretype);
1054 errorstream << "register_ore: ore_type " << oretype << " not implemented\n";
1058 ore->name = getstringfield_default(L, index, "name", "");
1059 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
1060 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
1061 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
1062 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
1066 //// Get noise_threshold
1067 warn_if_field_exists(L, index, "noise_threshhold",
1068 "Deprecated: new name is \"noise_threshold\".");
1071 if (!getfloatfield(L, index, "noise_threshold", nthresh) &&
1072 !getfloatfield(L, index, "noise_threshhold", nthresh))
1074 ore->nthresh = nthresh;
1076 //// Get y_min/y_max
1077 warn_if_field_exists(L, index, "height_min",
1078 "Deprecated: new name is \"y_min\".");
1079 warn_if_field_exists(L, index, "height_max",
1080 "Deprecated: new name is \"y_max\".");
1083 if (!getintfield(L, index, "y_min", ymin) &&
1084 !getintfield(L, index, "height_min", ymin))
1086 if (!getintfield(L, index, "y_max", ymax) &&
1087 !getintfield(L, index, "height_max", ymax))
1092 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
1093 errorstream << "register_ore: clust_scarcity and clust_num_ores"
1094 "must be greater than 0" << std::endl;
1100 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
1102 //// Get biomes associated with this decoration (if any)
1103 lua_getfield(L, index, "biomes");
1104 if (get_biome_list(L, -1, bmgr, &ore->biomes))
1105 infostream << "register_ore: couldn't get all biomes " << std::endl;
1108 //// Get noise parameters if needed
1109 lua_getfield(L, index, "noise_params");
1110 if (read_noiseparams(L, -1, &ore->np)) {
1111 ore->flags |= OREFLAG_USE_NOISE;
1112 } else if (ore->NEEDS_NOISE) {
1113 errorstream << "register_ore: specified ore type requires valid "
1114 "noise parameters" << std::endl;
1120 //// Get type-specific parameters
1123 OreSheet *oresheet = (OreSheet *)ore;
1125 oresheet->column_height_min = getintfield_default(L, index,
1126 "column_height_min", 1);
1127 oresheet->column_height_max = getintfield_default(L, index,
1128 "column_height_max", ore->clust_size);
1129 oresheet->column_midpoint_factor = getfloatfield_default(L, index,
1130 "column_midpoint_factor", 0.5f);
1135 OrePuff *orepuff = (OrePuff *)ore;
1137 lua_getfield(L, index, "np_puff_top");
1138 read_noiseparams(L, -1, &orepuff->np_puff_top);
1141 lua_getfield(L, index, "np_puff_bottom");
1142 read_noiseparams(L, -1, &orepuff->np_puff_bottom);
1148 OreVein *orevein = (OreVein *)ore;
1150 orevein->random_factor = getfloatfield_default(L, index,
1151 "random_factor", 1.f);
1156 OreStratum *orestratum = (OreStratum *)ore;
1158 lua_getfield(L, index, "np_stratum_thickness");
1159 read_noiseparams(L, -1, &orestratum->np_stratum_thickness);
1168 ObjDefHandle handle = oremgr->add(ore);
1169 if (handle == OBJDEF_INVALID_HANDLE) {
1174 ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
1176 size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
1177 ore->m_nnlistsizes.push_back(nnames);
1179 ndef->pendNodeResolve(ore);
1181 lua_pushinteger(L, handle);
1186 // register_schematic({schematic}, replacements={})
1187 int ModApiMapgen::l_register_schematic(lua_State *L)
1189 NO_MAP_LOCK_REQUIRED;
1191 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1193 StringMap replace_names;
1194 if (lua_istable(L, 2))
1195 read_schematic_replacements(L, 2, &replace_names);
1197 Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
1202 ObjDefHandle handle = schemmgr->add(schem);
1203 if (handle == OBJDEF_INVALID_HANDLE) {
1208 lua_pushinteger(L, handle);
1213 // clear_registered_biomes()
1214 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
1216 NO_MAP_LOCK_REQUIRED;
1218 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
1224 // clear_registered_decorations()
1225 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
1227 NO_MAP_LOCK_REQUIRED;
1229 DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
1235 // clear_registered_ores()
1236 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
1238 NO_MAP_LOCK_REQUIRED;
1240 OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
1246 // clear_registered_schematics()
1247 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
1249 NO_MAP_LOCK_REQUIRED;
1251 SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
1257 // generate_ores(vm, p1, p2, [ore_id])
1258 int ModApiMapgen::l_generate_ores(lua_State *L)
1260 NO_MAP_LOCK_REQUIRED;
1262 EmergeManager *emerge = getServer(L)->getEmergeManager();
1265 mg.seed = emerge->mgparams->seed;
1266 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1267 mg.ndef = getServer(L)->getNodeDefManager();
1269 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1270 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1271 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1272 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1273 sortBoxVerticies(pmin, pmax);
1275 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1277 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
1283 // generate_decorations(vm, p1, p2, [deco_id])
1284 int ModApiMapgen::l_generate_decorations(lua_State *L)
1286 NO_MAP_LOCK_REQUIRED;
1288 EmergeManager *emerge = getServer(L)->getEmergeManager();
1291 mg.seed = emerge->mgparams->seed;
1292 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1293 mg.ndef = getServer(L)->getNodeDefManager();
1295 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1296 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1297 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1298 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1299 sortBoxVerticies(pmin, pmax);
1301 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1303 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
1309 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
1310 int ModApiMapgen::l_create_schematic(lua_State *L)
1314 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1316 const char *filename = luaL_checkstring(L, 4);
1317 CHECK_SECURE_PATH(L, filename, true);
1319 Map *map = &(getEnv(L)->getMap());
1322 v3s16 p1 = check_v3s16(L, 1);
1323 v3s16 p2 = check_v3s16(L, 2);
1324 sortBoxVerticies(p1, p2);
1326 std::vector<std::pair<v3s16, u8> > prob_list;
1327 if (lua_istable(L, 3)) {
1329 while (lua_next(L, 3)) {
1330 if (lua_istable(L, -1)) {
1331 lua_getfield(L, -1, "pos");
1332 v3s16 pos = check_v3s16(L, -1);
1335 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1336 prob_list.emplace_back(pos, prob);
1343 std::vector<std::pair<s16, u8> > slice_prob_list;
1344 if (lua_istable(L, 5)) {
1346 while (lua_next(L, 5)) {
1347 if (lua_istable(L, -1)) {
1348 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1349 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1350 slice_prob_list.emplace_back(ypos, prob);
1357 if (!schem.getSchematicFromMap(map, p1, p2)) {
1358 errorstream << "create_schematic: failed to get schematic "
1359 "from map" << std::endl;
1363 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1365 schem.saveSchematicToFile(filename, ndef);
1366 actionstream << "create_schematic: saved schematic file '"
1367 << filename << "'." << std::endl;
1369 lua_pushboolean(L, true);
1374 // place_schematic(p, schematic, rotation, replacement)
1375 int ModApiMapgen::l_place_schematic(lua_State *L)
1381 ServerMap *map = &(env->getServerMap());
1382 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1385 v3s16 p = check_v3s16(L, 1);
1389 const char *enumstr = lua_tostring(L, 3);
1391 string_to_enum(es_Rotation, rot, std::string(enumstr));
1393 //// Read force placement
1394 bool force_placement = true;
1395 if (lua_isboolean(L, 5))
1396 force_placement = lua_toboolean(L, 5);
1398 //// Read node replacements
1399 StringMap replace_names;
1400 if (lua_istable(L, 4))
1401 read_schematic_replacements(L, 4, &replace_names);
1404 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1406 errorstream << "place_schematic: failed to get schematic" << std::endl;
1410 schem->placeOnMap(map, p, 0, (Rotation)rot, force_placement);
1412 lua_pushboolean(L, true);
1416 int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L)
1418 NO_MAP_LOCK_REQUIRED;
1420 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1422 //// Read VoxelManip object
1423 MMVManip *vm = LuaVoxelManip::checkobject(L, 1)->vm;
1426 v3s16 p = check_v3s16(L, 2);
1430 const char *enumstr = lua_tostring(L, 4);
1432 string_to_enum(es_Rotation, rot, std::string(enumstr));
1434 //// Read force placement
1435 bool force_placement = true;
1436 if (lua_isboolean(L, 6))
1437 force_placement = lua_toboolean(L, 6);
1439 //// Read node replacements
1440 StringMap replace_names;
1441 if (lua_istable(L, 5))
1442 read_schematic_replacements(L, 5, &replace_names);
1445 Schematic *schem = get_or_load_schematic(L, 3, schemmgr, &replace_names);
1447 errorstream << "place_schematic: failed to get schematic" << std::endl;
1451 bool schematic_did_fit = schem->placeOnVManip(
1452 vm, p, 0, (Rotation)rot, force_placement);
1454 lua_pushboolean(L, schematic_did_fit);
1458 // serialize_schematic(schematic, format, options={...})
1459 int ModApiMapgen::l_serialize_schematic(lua_State *L)
1461 NO_MAP_LOCK_REQUIRED;
1463 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1466 bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
1467 u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
1470 bool was_loaded = false;
1471 Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1473 schem = load_schematic(L, 1, NULL, NULL);
1477 errorstream << "serialize_schematic: failed to get schematic" << std::endl;
1481 //// Read format of definition to save as
1482 int schem_format = SCHEM_FMT_MTS;
1483 const char *enumstr = lua_tostring(L, 2);
1485 string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr));
1487 //// Serialize to binary string
1488 std::ostringstream os(std::ios_base::binary);
1489 switch (schem_format) {
1491 schem->serializeToMts(&os, schem->m_nodenames);
1494 schem->serializeToLua(&os, schem->m_nodenames,
1495 use_comments, indent_spaces);
1504 std::string ser = os.str();
1505 lua_pushlstring(L, ser.c_str(), ser.length());
1510 void ModApiMapgen::Initialize(lua_State *L, int top)
1512 API_FCT(get_biome_id);
1513 API_FCT(get_mapgen_object);
1515 API_FCT(get_mapgen_params);
1516 API_FCT(set_mapgen_params);
1517 API_FCT(get_mapgen_setting);
1518 API_FCT(set_mapgen_setting);
1519 API_FCT(get_mapgen_setting_noiseparams);
1520 API_FCT(set_mapgen_setting_noiseparams);
1521 API_FCT(set_noiseparams);
1522 API_FCT(get_noiseparams);
1523 API_FCT(set_gen_notify);
1524 API_FCT(get_gen_notify);
1526 API_FCT(register_biome);
1527 API_FCT(register_decoration);
1528 API_FCT(register_ore);
1529 API_FCT(register_schematic);
1531 API_FCT(clear_registered_biomes);
1532 API_FCT(clear_registered_decorations);
1533 API_FCT(clear_registered_ores);
1534 API_FCT(clear_registered_schematics);
1536 API_FCT(generate_ores);
1537 API_FCT(generate_decorations);
1538 API_FCT(create_schematic);
1539 API_FCT(place_schematic);
1540 API_FCT(place_schematic_on_vmanip);
1541 API_FCT(serialize_schematic);