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"},
79 struct EnumString ModApiMapgen::es_Rotation[] =
85 {ROTATE_RAND, "random"},
89 struct EnumString ModApiMapgen::es_SchematicFormatType[] =
91 {SCHEM_FMT_HANDLE, "handle"},
92 {SCHEM_FMT_MTS, "mts"},
93 {SCHEM_FMT_LUA, "lua"},
97 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
99 Biome *get_or_load_biome(lua_State *L, int index,
100 BiomeManager *biomemgr);
101 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
102 size_t get_biome_list(lua_State *L, int index,
103 BiomeManager *biomemgr, UNORDERED_SET<u8> *biome_id_list);
105 Schematic *get_or_load_schematic(lua_State *L, int index,
106 SchematicManager *schemmgr, StringMap *replace_names);
107 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
108 StringMap *replace_names);
109 Schematic *load_schematic_from_def(lua_State *L, int index,
110 INodeDefManager *ndef, StringMap *replace_names);
111 bool read_schematic_def(lua_State *L, int index,
112 Schematic *schem, std::vector<std::string> *names);
114 bool read_deco_simple(lua_State *L, DecoSimple *deco);
115 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
118 ///////////////////////////////////////////////////////////////////////////////
120 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
123 index = lua_gettop(L) + 1 + index;
125 // If a number, assume this is a handle to an object def
126 if (lua_isnumber(L, index))
127 return objmgr->get(lua_tointeger(L, index));
129 // If a string, assume a name is given instead
130 if (lua_isstring(L, index))
131 return objmgr->getByName(lua_tostring(L, index));
136 ///////////////////////////////////////////////////////////////////////////////
138 Schematic *get_or_load_schematic(lua_State *L, int index,
139 SchematicManager *schemmgr, StringMap *replace_names)
142 index = lua_gettop(L) + 1 + index;
144 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
148 schem = load_schematic(L, index, schemmgr->getNodeDef(),
153 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
162 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
163 StringMap *replace_names)
166 index = lua_gettop(L) + 1 + index;
168 Schematic *schem = NULL;
170 if (lua_istable(L, index)) {
171 schem = load_schematic_from_def(L, index, ndef,
177 } else if (lua_isnumber(L, index)) {
179 } else if (lua_isstring(L, index)) {
180 schem = SchematicManager::create(SCHEMATIC_NORMAL);
182 std::string filepath = lua_tostring(L, index);
183 if (!fs::IsPathAbsolute(filepath))
184 filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
186 if (!schem->loadSchematicFromFile(filepath, ndef,
197 Schematic *load_schematic_from_def(lua_State *L, int index,
198 INodeDefManager *ndef, StringMap *replace_names)
200 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
202 if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
207 size_t num_nodes = schem->m_nodenames.size();
209 schem->m_nnlistsizes.push_back(num_nodes);
212 for (size_t i = 0; i != num_nodes; i++) {
213 StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
214 if (it != replace_names->end())
215 schem->m_nodenames[i] = it->second;
220 ndef->pendNodeResolve(schem);
226 bool read_schematic_def(lua_State *L, int index,
227 Schematic *schem, std::vector<std::string> *names)
229 if (!lua_istable(L, index))
232 //// Get schematic size
233 lua_getfield(L, index, "size");
234 v3s16 size = check_v3s16(L, -1);
239 //// Get schematic data
240 lua_getfield(L, index, "data");
241 luaL_checktype(L, -1, LUA_TTABLE);
243 u32 numnodes = size.X * size.Y * size.Z;
244 schem->schemdata = new MapNode[numnodes];
246 size_t names_base = names->size();
247 UNORDERED_MAP<std::string, content_t> name_id_map;
250 for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
256 if (!getstringfield(L, -1, "name", name))
257 throw LuaError("Schematic data definition with missing name field");
259 //// Read param1/prob
261 if (!getintfield(L, -1, "param1", param1) &&
262 !getintfield(L, -1, "prob", param1))
263 param1 = MTSCHEM_PROB_ALWAYS_OLD;
266 u8 param2 = getintfield_default(L, -1, "param2", 0);
268 //// Find or add new nodename-to-ID mapping
269 UNORDERED_MAP<std::string, content_t>::iterator it = name_id_map.find(name);
270 content_t name_index;
271 if (it != name_id_map.end()) {
272 name_index = it->second;
274 name_index = names->size() - names_base;
275 name_id_map[name] = name_index;
276 names->push_back(name);
279 //// Perform probability/force_place fixup on param1
281 if (getboolfield_default(L, -1, "force_place", false))
282 param1 |= MTSCHEM_FORCE_PLACE;
284 //// Actually set the node in the schematic
285 schem->schemdata[i] = MapNode(name_index, param1, param2);
289 errorstream << "read_schematic_def: incorrect number of "
290 "nodes provided in raw schematic data (got " << i <<
291 ", expected " << numnodes << ")." << std::endl;
295 //// Get Y-slice probability values (if present)
296 schem->slice_probs = new u8[size.Y];
297 for (i = 0; i != (u32) size.Y; i++)
298 schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
300 lua_getfield(L, index, "yslice_prob");
301 if (lua_istable(L, -1)) {
302 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
304 if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
305 !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
308 schem->slice_probs[ypos] >>= 1;
316 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
319 index = lua_gettop(L) + 1 + index;
322 while (lua_next(L, index)) {
323 std::string replace_from;
324 std::string replace_to;
326 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
327 lua_rawgeti(L, -1, 1);
328 if (!lua_isstring(L, -1))
329 throw LuaError("schematics: replace_from field is not a string");
330 replace_from = lua_tostring(L, -1);
333 lua_rawgeti(L, -1, 2);
334 if (!lua_isstring(L, -1))
335 throw LuaError("schematics: replace_to field is not a string");
336 replace_to = lua_tostring(L, -1);
338 } else { // New {x = "y", ...} format
339 if (!lua_isstring(L, -2))
340 throw LuaError("schematics: replace_from field is not a string");
341 replace_from = lua_tostring(L, -2);
342 if (!lua_isstring(L, -1))
343 throw LuaError("schematics: replace_to field is not a string");
344 replace_to = lua_tostring(L, -1);
347 replace_names->insert(std::make_pair(replace_from, replace_to));
352 ///////////////////////////////////////////////////////////////////////////////
354 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
357 index = lua_gettop(L) + 1 + index;
359 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
363 biome = read_biome_def(L, index, biomemgr->getNodeDef());
367 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
376 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
378 if (!lua_istable(L, index))
381 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
382 ModApiMapgen::es_BiomeTerrainType, BIOMETYPE_NORMAL);
383 Biome *b = BiomeManager::create(biometype);
385 b->name = getstringfield_default(L, index, "name", "");
386 b->depth_top = getintfield_default(L, index, "depth_top", 0);
387 b->depth_filler = getintfield_default(L, index, "depth_filler", -31000);
388 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
389 b->depth_riverbed = getintfield_default(L, index, "depth_riverbed", 0);
390 b->y_min = getintfield_default(L, index, "y_min", -31000);
391 b->y_max = getintfield_default(L, index, "y_max", 31000);
392 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
393 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
394 b->flags = 0; //reserved
396 std::vector<std::string> &nn = b->m_nodenames;
397 nn.push_back(getstringfield_default(L, index, "node_top", ""));
398 nn.push_back(getstringfield_default(L, index, "node_filler", ""));
399 nn.push_back(getstringfield_default(L, index, "node_stone", ""));
400 nn.push_back(getstringfield_default(L, index, "node_water_top", ""));
401 nn.push_back(getstringfield_default(L, index, "node_water", ""));
402 nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
403 nn.push_back(getstringfield_default(L, index, "node_riverbed", ""));
404 nn.push_back(getstringfield_default(L, index, "node_dust", ""));
405 ndef->pendNodeResolve(b);
411 size_t get_biome_list(lua_State *L, int index,
412 BiomeManager *biomemgr, UNORDERED_SET<u8> *biome_id_list)
415 index = lua_gettop(L) + 1 + index;
417 if (lua_isnil(L, index))
420 bool is_single = true;
421 if (lua_istable(L, index)) {
422 lua_getfield(L, index, "name");
423 is_single = !lua_isnil(L, -1);
428 Biome *biome = get_or_load_biome(L, index, biomemgr);
430 infostream << "get_biome_list: failed to get biome '"
431 << (lua_isstring(L, index) ? lua_tostring(L, index) : "")
432 << "'." << std::endl;
436 biome_id_list->insert(biome->index);
440 // returns number of failed resolutions
441 size_t fail_count = 0;
444 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
446 Biome *biome = get_or_load_biome(L, -1, biomemgr);
449 infostream << "get_biome_list: failed to get biome '"
450 << (lua_isstring(L, -1) ? lua_tostring(L, -1) : "")
455 biome_id_list->insert(biome->index);
461 ///////////////////////////////////////////////////////////////////////////////
463 // get_biome_id(biomename)
464 // returns the biome id used in biomemap
465 int ModApiMapgen::l_get_biome_id(lua_State *L)
467 NO_MAP_LOCK_REQUIRED;
469 const char *biome_str = lua_tostring(L, 1);
473 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
478 Biome *biome = (Biome *)bmgr->getByName(biome_str);
480 if (!biome || biome->index == OBJDEF_INVALID_INDEX)
483 lua_pushinteger(L, biome->index);
489 // get_mapgen_object(objectname)
490 // returns the requested object used during map generation
491 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
493 NO_MAP_LOCK_REQUIRED;
495 const char *mgobjstr = lua_tostring(L, 1);
498 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
501 enum MapgenObject mgobj = (MapgenObject)mgobjint;
503 EmergeManager *emerge = getServer(L)->getEmergeManager();
504 Mapgen *mg = emerge->getCurrentMapgen();
506 throw LuaError("Must only be called in a mapgen thread!");
508 size_t maplen = mg->csize.X * mg->csize.Z;
512 MMVManip *vm = mg->vm;
515 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
516 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
517 luaL_getmetatable(L, "VoxelManip");
518 lua_setmetatable(L, -2);
521 push_v3s16(L, vm->m_area.MinEdge);
524 push_v3s16(L, vm->m_area.MaxEdge);
528 case MGOBJ_HEIGHTMAP: {
533 for (size_t i = 0; i != maplen; i++) {
534 lua_pushinteger(L, mg->heightmap[i]);
535 lua_rawseti(L, -2, i + 1);
540 case MGOBJ_BIOMEMAP: {
545 for (size_t i = 0; i != maplen; i++) {
546 lua_pushinteger(L, mg->biomegen->biomemap[i]);
547 lua_rawseti(L, -2, i + 1);
552 case MGOBJ_HEATMAP: {
553 if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
556 BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
559 for (size_t i = 0; i != maplen; i++) {
560 lua_pushnumber(L, bg->heatmap[i]);
561 lua_rawseti(L, -2, i + 1);
567 case MGOBJ_HUMIDMAP: {
568 if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL)
571 BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen;
574 for (size_t i = 0; i != maplen; i++) {
575 lua_pushnumber(L, bg->humidmap[i]);
576 lua_rawseti(L, -2, i + 1);
581 case MGOBJ_GENNOTIFY: {
582 std::map<std::string, std::vector<v3s16> >event_map;
583 std::map<std::string, std::vector<v3s16> >::iterator it;
585 mg->gennotify.getEvents(event_map);
588 for (it = event_map.begin(); it != event_map.end(); ++it) {
591 for (size_t j = 0; j != it->second.size(); j++) {
592 push_v3s16(L, it->second[j]);
593 lua_rawseti(L, -2, j + 1);
596 lua_setfield(L, -2, it->first.c_str());
607 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
609 NO_MAP_LOCK_REQUIRED;
611 log_deprecated(L, "get_mapgen_params is deprecated; "
612 "use get_mapgen_setting instead");
616 MapSettingsManager *settingsmgr =
617 getServer(L)->getEmergeManager()->map_settings_mgr;
621 settingsmgr->getMapSetting("mg_name", &value);
622 lua_pushstring(L, value.c_str());
623 lua_setfield(L, -2, "mgname");
625 settingsmgr->getMapSetting("seed", &value);
626 std::istringstream ss(value);
629 lua_pushinteger(L, seed);
630 lua_setfield(L, -2, "seed");
632 settingsmgr->getMapSetting("water_level", &value);
633 lua_pushinteger(L, stoi(value, -32768, 32767));
634 lua_setfield(L, -2, "water_level");
636 settingsmgr->getMapSetting("chunksize", &value);
637 lua_pushinteger(L, stoi(value, -32768, 32767));
638 lua_setfield(L, -2, "chunksize");
640 settingsmgr->getMapSetting("mg_flags", &value);
641 lua_pushstring(L, value.c_str());
642 lua_setfield(L, -2, "flags");
648 // set_mapgen_params(params)
649 // set mapgen parameters
650 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
652 NO_MAP_LOCK_REQUIRED;
654 log_deprecated(L, "set_mapgen_params is deprecated; "
655 "use set_mapgen_setting instead");
657 if (!lua_istable(L, 1))
660 MapSettingsManager *settingsmgr =
661 getServer(L)->getEmergeManager()->map_settings_mgr;
663 lua_getfield(L, 1, "mgname");
664 if (lua_isstring(L, -1))
665 settingsmgr->setMapSetting("mg_name", lua_tostring(L, -1), true);
667 lua_getfield(L, 1, "seed");
668 if (lua_isnumber(L, -1))
669 settingsmgr->setMapSetting("seed", lua_tostring(L, -1), true);
671 lua_getfield(L, 1, "water_level");
672 if (lua_isnumber(L, -1))
673 settingsmgr->setMapSetting("water_level", lua_tostring(L, -1), true);
675 lua_getfield(L, 1, "chunksize");
676 if (lua_isnumber(L, -1))
677 settingsmgr->setMapSetting("chunksize", lua_tostring(L, -1), true);
679 warn_if_field_exists(L, 1, "flagmask",
680 "Deprecated: flags field now includes unset flags.");
682 lua_getfield(L, 1, "flags");
683 if (lua_isstring(L, -1))
684 settingsmgr->setMapSetting("mg_flags", lua_tostring(L, -1), true);
689 // get_mapgen_setting(name)
690 int ModApiMapgen::l_get_mapgen_setting(lua_State *L)
692 NO_MAP_LOCK_REQUIRED;
695 MapSettingsManager *settingsmgr =
696 getServer(L)->getEmergeManager()->map_settings_mgr;
698 const char *name = luaL_checkstring(L, 1);
699 if (!settingsmgr->getMapSetting(name, &value))
702 lua_pushstring(L, value.c_str());
706 // get_mapgen_setting_noiseparams(name)
707 int ModApiMapgen::l_get_mapgen_setting_noiseparams(lua_State *L)
709 NO_MAP_LOCK_REQUIRED;
712 MapSettingsManager *settingsmgr =
713 getServer(L)->getEmergeManager()->map_settings_mgr;
715 const char *name = luaL_checkstring(L, 1);
716 if (!settingsmgr->getMapSettingNoiseParams(name, &np))
719 push_noiseparams(L, &np);
723 // set_mapgen_setting(name, value, override_meta)
724 // set mapgen config values
725 int ModApiMapgen::l_set_mapgen_setting(lua_State *L)
727 NO_MAP_LOCK_REQUIRED;
729 MapSettingsManager *settingsmgr =
730 getServer(L)->getEmergeManager()->map_settings_mgr;
732 const char *name = luaL_checkstring(L, 1);
733 const char *value = luaL_checkstring(L, 2);
734 bool override_meta = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : false;
736 if (!settingsmgr->setMapSetting(name, value, override_meta)) {
737 errorstream << "set_mapgen_setting: cannot set '"
738 << name << "' after initialization" << std::endl;
745 // set_mapgen_setting_noiseparams(name, noiseparams, set_default)
746 // set mapgen config values for noise parameters
747 int ModApiMapgen::l_set_mapgen_setting_noiseparams(lua_State *L)
749 NO_MAP_LOCK_REQUIRED;
751 MapSettingsManager *settingsmgr =
752 getServer(L)->getEmergeManager()->map_settings_mgr;
754 const char *name = luaL_checkstring(L, 1);
757 if (!read_noiseparams(L, 2, &np)) {
758 errorstream << "set_mapgen_setting_noiseparams: cannot set '" << name
759 << "'; invalid noiseparams table" << std::endl;
763 bool override_meta = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : false;
765 if (!settingsmgr->setMapSettingNoiseParams(name, &np, override_meta)) {
766 errorstream << "set_mapgen_setting_noiseparams: cannot set '"
767 << name << "' after initialization" << std::endl;
774 // set_noiseparams(name, noiseparams, set_default)
775 // set global config values for noise parameters
776 int ModApiMapgen::l_set_noiseparams(lua_State *L)
778 NO_MAP_LOCK_REQUIRED;
780 const char *name = luaL_checkstring(L, 1);
783 if (!read_noiseparams(L, 2, &np)) {
784 errorstream << "set_noiseparams: cannot set '" << name
785 << "'; invalid noiseparams table" << std::endl;
789 bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
791 g_settings->setNoiseParams(name, np, set_default);
797 // get_noiseparams(name)
798 int ModApiMapgen::l_get_noiseparams(lua_State *L)
800 NO_MAP_LOCK_REQUIRED;
802 std::string name = luaL_checkstring(L, 1);
805 if (!g_settings->getNoiseParams(name, np))
808 push_noiseparams(L, &np);
813 // set_gen_notify(flags, {deco_id_table})
814 int ModApiMapgen::l_set_gen_notify(lua_State *L)
816 NO_MAP_LOCK_REQUIRED;
818 u32 flags = 0, flagmask = 0;
819 EmergeManager *emerge = getServer(L)->getEmergeManager();
821 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
822 emerge->gen_notify_on &= ~flagmask;
823 emerge->gen_notify_on |= flags;
826 if (lua_istable(L, 2)) {
828 while (lua_next(L, 2)) {
829 if (lua_isnumber(L, -1))
830 emerge->gen_notify_on_deco_ids.insert((u32)lua_tonumber(L, -1));
840 int ModApiMapgen::l_get_gen_notify(lua_State *L)
842 NO_MAP_LOCK_REQUIRED;
844 EmergeManager *emerge = getServer(L)->getEmergeManager();
845 push_flags_string(L, flagdesc_gennotify, emerge->gen_notify_on,
846 emerge->gen_notify_on);
850 for (std::set<u32>::iterator it = emerge->gen_notify_on_deco_ids.begin();
851 it != emerge->gen_notify_on_deco_ids.end(); ++it) {
852 lua_pushnumber(L, *it);
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 StringMap replace_names;
1022 lua_getfield(L, index, "replacements");
1023 if (lua_istable(L, -1))
1024 read_schematic_replacements(L, -1, &replace_names);
1027 lua_getfield(L, index, "schematic");
1028 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
1031 deco->schematic = schem;
1032 return schem != NULL;
1036 // register_ore({lots of stuff})
1037 int ModApiMapgen::l_register_ore(lua_State *L)
1039 NO_MAP_LOCK_REQUIRED;
1042 luaL_checktype(L, index, LUA_TTABLE);
1044 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1045 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
1046 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
1048 enum OreType oretype = (OreType)getenumfield(L, index,
1049 "ore_type", es_OreType, ORE_SCATTER);
1050 Ore *ore = oremgr->create(oretype);
1052 errorstream << "register_ore: ore_type " << oretype << " not implemented\n";
1056 ore->name = getstringfield_default(L, index, "name", "");
1057 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
1058 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
1059 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
1060 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
1064 //// Get noise_threshold
1065 warn_if_field_exists(L, index, "noise_threshhold",
1066 "Deprecated: new name is \"noise_threshold\".");
1069 if (!getfloatfield(L, index, "noise_threshold", nthresh) &&
1070 !getfloatfield(L, index, "noise_threshhold", nthresh))
1072 ore->nthresh = nthresh;
1074 //// Get y_min/y_max
1075 warn_if_field_exists(L, index, "height_min",
1076 "Deprecated: new name is \"y_min\".");
1077 warn_if_field_exists(L, index, "height_max",
1078 "Deprecated: new name is \"y_max\".");
1081 if (!getintfield(L, index, "y_min", ymin) &&
1082 !getintfield(L, index, "height_min", ymin))
1084 if (!getintfield(L, index, "y_max", ymax) &&
1085 !getintfield(L, index, "height_max", ymax))
1090 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
1091 errorstream << "register_ore: clust_scarcity and clust_num_ores"
1092 "must be greater than 0" << std::endl;
1098 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
1100 //// Get biomes associated with this decoration (if any)
1101 lua_getfield(L, index, "biomes");
1102 if (get_biome_list(L, -1, bmgr, &ore->biomes))
1103 infostream << "register_ore: couldn't get all biomes " << std::endl;
1106 //// Get noise parameters if needed
1107 lua_getfield(L, index, "noise_params");
1108 if (read_noiseparams(L, -1, &ore->np)) {
1109 ore->flags |= OREFLAG_USE_NOISE;
1110 } else if (ore->NEEDS_NOISE) {
1111 errorstream << "register_ore: specified ore type requires valid "
1112 "noise parameters" << std::endl;
1118 //// Get type-specific parameters
1121 OreSheet *oresheet = (OreSheet *)ore;
1123 oresheet->column_height_min = getintfield_default(L, index,
1124 "column_height_min", 1);
1125 oresheet->column_height_max = getintfield_default(L, index,
1126 "column_height_max", ore->clust_size);
1127 oresheet->column_midpoint_factor = getfloatfield_default(L, index,
1128 "column_midpoint_factor", 0.5f);
1133 OrePuff *orepuff = (OrePuff *)ore;
1135 lua_getfield(L, index, "np_puff_top");
1136 read_noiseparams(L, -1, &orepuff->np_puff_top);
1139 lua_getfield(L, index, "np_puff_bottom");
1140 read_noiseparams(L, -1, &orepuff->np_puff_bottom);
1146 OreVein *orevein = (OreVein *)ore;
1148 orevein->random_factor = getfloatfield_default(L, index,
1149 "random_factor", 1.f);
1157 ObjDefHandle handle = oremgr->add(ore);
1158 if (handle == OBJDEF_INVALID_HANDLE) {
1163 ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
1165 size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
1166 ore->m_nnlistsizes.push_back(nnames);
1168 ndef->pendNodeResolve(ore);
1170 lua_pushinteger(L, handle);
1175 // register_schematic({schematic}, replacements={})
1176 int ModApiMapgen::l_register_schematic(lua_State *L)
1178 NO_MAP_LOCK_REQUIRED;
1180 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1182 StringMap replace_names;
1183 if (lua_istable(L, 2))
1184 read_schematic_replacements(L, 2, &replace_names);
1186 Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
1191 ObjDefHandle handle = schemmgr->add(schem);
1192 if (handle == OBJDEF_INVALID_HANDLE) {
1197 lua_pushinteger(L, handle);
1202 // clear_registered_biomes()
1203 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
1205 NO_MAP_LOCK_REQUIRED;
1207 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
1213 // clear_registered_decorations()
1214 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
1216 NO_MAP_LOCK_REQUIRED;
1218 DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
1224 // clear_registered_ores()
1225 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
1227 NO_MAP_LOCK_REQUIRED;
1229 OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
1235 // clear_registered_schematics()
1236 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
1238 NO_MAP_LOCK_REQUIRED;
1240 SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
1246 // generate_ores(vm, p1, p2, [ore_id])
1247 int ModApiMapgen::l_generate_ores(lua_State *L)
1249 NO_MAP_LOCK_REQUIRED;
1251 EmergeManager *emerge = getServer(L)->getEmergeManager();
1254 mg.seed = emerge->mgparams->seed;
1255 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1256 mg.ndef = getServer(L)->getNodeDefManager();
1258 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1259 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1260 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1261 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1262 sortBoxVerticies(pmin, pmax);
1264 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1266 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
1272 // generate_decorations(vm, p1, p2, [deco_id])
1273 int ModApiMapgen::l_generate_decorations(lua_State *L)
1275 NO_MAP_LOCK_REQUIRED;
1277 EmergeManager *emerge = getServer(L)->getEmergeManager();
1280 mg.seed = emerge->mgparams->seed;
1281 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1282 mg.ndef = getServer(L)->getNodeDefManager();
1284 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1285 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1286 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1287 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1288 sortBoxVerticies(pmin, pmax);
1290 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1292 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
1298 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
1299 int ModApiMapgen::l_create_schematic(lua_State *L)
1303 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1305 const char *filename = luaL_checkstring(L, 4);
1306 CHECK_SECURE_PATH(L, filename, true);
1308 Map *map = &(getEnv(L)->getMap());
1311 v3s16 p1 = check_v3s16(L, 1);
1312 v3s16 p2 = check_v3s16(L, 2);
1313 sortBoxVerticies(p1, p2);
1315 std::vector<std::pair<v3s16, u8> > prob_list;
1316 if (lua_istable(L, 3)) {
1318 while (lua_next(L, 3)) {
1319 if (lua_istable(L, -1)) {
1320 lua_getfield(L, -1, "pos");
1321 v3s16 pos = check_v3s16(L, -1);
1324 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1325 prob_list.push_back(std::make_pair(pos, prob));
1332 std::vector<std::pair<s16, u8> > slice_prob_list;
1333 if (lua_istable(L, 5)) {
1335 while (lua_next(L, 5)) {
1336 if (lua_istable(L, -1)) {
1337 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1338 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1339 slice_prob_list.push_back(std::make_pair(ypos, prob));
1346 if (!schem.getSchematicFromMap(map, p1, p2)) {
1347 errorstream << "create_schematic: failed to get schematic "
1348 "from map" << std::endl;
1352 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1354 schem.saveSchematicToFile(filename, ndef);
1355 actionstream << "create_schematic: saved schematic file '"
1356 << filename << "'." << std::endl;
1358 lua_pushboolean(L, true);
1363 // place_schematic(p, schematic, rotation, replacement)
1364 int ModApiMapgen::l_place_schematic(lua_State *L)
1370 ServerMap *map = &(env->getServerMap());
1371 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1374 v3s16 p = check_v3s16(L, 1);
1378 const char *enumstr = lua_tostring(L, 3);
1380 string_to_enum(es_Rotation, rot, std::string(enumstr));
1382 //// Read force placement
1383 bool force_placement = true;
1384 if (lua_isboolean(L, 5))
1385 force_placement = lua_toboolean(L, 5);
1387 //// Read node replacements
1388 StringMap replace_names;
1389 if (lua_istable(L, 4))
1390 read_schematic_replacements(L, 4, &replace_names);
1393 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1395 errorstream << "place_schematic: failed to get schematic" << std::endl;
1399 schem->placeOnMap(map, p, 0, (Rotation)rot, force_placement);
1401 lua_pushboolean(L, true);
1405 int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L)
1407 NO_MAP_LOCK_REQUIRED;
1409 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1411 //// Read VoxelManip object
1412 MMVManip *vm = LuaVoxelManip::checkobject(L, 1)->vm;
1415 v3s16 p = check_v3s16(L, 2);
1419 const char *enumstr = lua_tostring(L, 4);
1421 string_to_enum(es_Rotation, rot, std::string(enumstr));
1423 //// Read force placement
1424 bool force_placement = true;
1425 if (lua_isboolean(L, 6))
1426 force_placement = lua_toboolean(L, 6);
1428 //// Read node replacements
1429 StringMap replace_names;
1430 if (lua_istable(L, 5))
1431 read_schematic_replacements(L, 5, &replace_names);
1434 Schematic *schem = get_or_load_schematic(L, 3, schemmgr, &replace_names);
1436 errorstream << "place_schematic: failed to get schematic" << std::endl;
1440 bool schematic_did_fit = schem->placeOnVManip(
1441 vm, p, 0, (Rotation)rot, force_placement);
1443 lua_pushboolean(L, schematic_did_fit);
1447 // serialize_schematic(schematic, format, options={...})
1448 int ModApiMapgen::l_serialize_schematic(lua_State *L)
1450 NO_MAP_LOCK_REQUIRED;
1452 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1455 bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
1456 u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
1459 bool was_loaded = false;
1460 Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1462 schem = load_schematic(L, 1, NULL, NULL);
1466 errorstream << "serialize_schematic: failed to get schematic" << std::endl;
1470 //// Read format of definition to save as
1471 int schem_format = SCHEM_FMT_MTS;
1472 const char *enumstr = lua_tostring(L, 2);
1474 string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr));
1476 //// Serialize to binary string
1477 std::ostringstream os(std::ios_base::binary);
1478 switch (schem_format) {
1480 schem->serializeToMts(&os, schem->m_nodenames);
1483 schem->serializeToLua(&os, schem->m_nodenames,
1484 use_comments, indent_spaces);
1493 std::string ser = os.str();
1494 lua_pushlstring(L, ser.c_str(), ser.length());
1499 void ModApiMapgen::Initialize(lua_State *L, int top)
1501 API_FCT(get_biome_id);
1502 API_FCT(get_mapgen_object);
1504 API_FCT(get_mapgen_params);
1505 API_FCT(set_mapgen_params);
1506 API_FCT(get_mapgen_setting);
1507 API_FCT(set_mapgen_setting);
1508 API_FCT(get_mapgen_setting_noiseparams);
1509 API_FCT(set_mapgen_setting_noiseparams);
1510 API_FCT(set_noiseparams);
1511 API_FCT(get_noiseparams);
1512 API_FCT(set_gen_notify);
1513 API_FCT(get_gen_notify);
1515 API_FCT(register_biome);
1516 API_FCT(register_decoration);
1517 API_FCT(register_ore);
1518 API_FCT(register_schematic);
1520 API_FCT(clear_registered_biomes);
1521 API_FCT(clear_registered_decorations);
1522 API_FCT(clear_registered_ores);
1523 API_FCT(clear_registered_schematics);
1525 API_FCT(generate_ores);
1526 API_FCT(generate_decorations);
1527 API_FCT(create_schematic);
1528 API_FCT(place_schematic);
1529 API_FCT(place_schematic_on_vmanip);
1530 API_FCT(serialize_schematic);