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 {BIOME_NORMAL, "normal"},
43 {BIOME_LIQUID, "liquid"},
44 {BIOME_NETHER, "nether"},
45 {BIOME_AETHER, "aether"},
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"},
78 struct EnumString ModApiMapgen::es_Rotation[] =
84 {ROTATE_RAND, "random"},
88 struct EnumString ModApiMapgen::es_SchematicFormatType[] =
90 {SCHEM_FMT_HANDLE, "handle"},
91 {SCHEM_FMT_MTS, "mts"},
92 {SCHEM_FMT_LUA, "lua"},
96 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
98 Biome *get_or_load_biome(lua_State *L, int index,
99 BiomeManager *biomemgr);
100 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
101 size_t get_biome_list(lua_State *L, int index,
102 BiomeManager *biomemgr, std::set<u8> *biome_id_list);
104 Schematic *get_or_load_schematic(lua_State *L, int index,
105 SchematicManager *schemmgr, StringMap *replace_names);
106 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
107 StringMap *replace_names);
108 Schematic *load_schematic_from_def(lua_State *L, int index,
109 INodeDefManager *ndef, StringMap *replace_names);
110 bool read_schematic_def(lua_State *L, int index,
111 Schematic *schem, std::vector<std::string> *names);
113 bool read_deco_simple(lua_State *L, DecoSimple *deco);
114 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
117 ///////////////////////////////////////////////////////////////////////////////
119 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
122 index = lua_gettop(L) + 1 + index;
124 // If a number, assume this is a handle to an object def
125 if (lua_isnumber(L, index))
126 return objmgr->get(lua_tointeger(L, index));
128 // If a string, assume a name is given instead
129 if (lua_isstring(L, index))
130 return objmgr->getByName(lua_tostring(L, index));
135 ///////////////////////////////////////////////////////////////////////////////
137 Schematic *get_or_load_schematic(lua_State *L, int index,
138 SchematicManager *schemmgr, StringMap *replace_names)
141 index = lua_gettop(L) + 1 + index;
143 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
147 schem = load_schematic(L, index, schemmgr->getNodeDef(),
152 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
161 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
162 StringMap *replace_names)
165 index = lua_gettop(L) + 1 + index;
167 Schematic *schem = NULL;
169 if (lua_istable(L, index)) {
170 schem = load_schematic_from_def(L, index, ndef,
176 } else if (lua_isnumber(L, index)) {
178 } else if (lua_isstring(L, index)) {
179 schem = SchematicManager::create(SCHEMATIC_NORMAL);
181 std::string filepath = lua_tostring(L, index);
182 if (!fs::IsPathAbsolute(filepath))
183 filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
185 if (!schem->loadSchematicFromFile(filepath, ndef,
196 Schematic *load_schematic_from_def(lua_State *L, int index,
197 INodeDefManager *ndef, StringMap *replace_names)
199 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
201 if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
206 size_t num_nodes = schem->m_nodenames.size();
208 schem->m_nnlistsizes.push_back(num_nodes);
211 for (size_t i = 0; i != num_nodes; i++) {
212 StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
213 if (it != replace_names->end())
214 schem->m_nodenames[i] = it->second;
219 ndef->pendNodeResolve(schem);
225 bool read_schematic_def(lua_State *L, int index,
226 Schematic *schem, std::vector<std::string> *names)
228 if (!lua_istable(L, index))
231 //// Get schematic size
232 lua_getfield(L, index, "size");
233 v3s16 size = check_v3s16(L, -1);
238 //// Get schematic data
239 lua_getfield(L, index, "data");
240 luaL_checktype(L, -1, LUA_TTABLE);
242 u32 numnodes = size.X * size.Y * size.Z;
243 schem->schemdata = new MapNode[numnodes];
245 size_t names_base = names->size();
246 std::map<std::string, content_t> name_id_map;
249 for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
255 if (!getstringfield(L, -1, "name", name))
256 throw LuaError("Schematic data definition with missing name field");
258 //// Read param1/prob
260 if (!getintfield(L, -1, "param1", param1) &&
261 !getintfield(L, -1, "prob", param1))
262 param1 = MTSCHEM_PROB_ALWAYS_OLD;
265 u8 param2 = getintfield_default(L, -1, "param2", 0);
267 //// Find or add new nodename-to-ID mapping
268 std::map<std::string, content_t>::iterator it = name_id_map.find(name);
269 content_t name_index;
270 if (it != name_id_map.end()) {
271 name_index = it->second;
273 name_index = names->size() - names_base;
274 name_id_map[name] = name_index;
275 names->push_back(name);
278 //// Perform probability/force_place fixup on param1
280 if (getboolfield_default(L, -1, "force_place", false))
281 param1 |= MTSCHEM_FORCE_PLACE;
283 //// Actually set the node in the schematic
284 schem->schemdata[i] = MapNode(name_index, param1, param2);
288 errorstream << "read_schematic_def: incorrect number of "
289 "nodes provided in raw schematic data (got " << i <<
290 ", expected " << numnodes << ")." << std::endl;
294 //// Get Y-slice probability values (if present)
295 schem->slice_probs = new u8[size.Y];
296 for (i = 0; i != (u32) size.Y; i++)
297 schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
299 lua_getfield(L, index, "yslice_prob");
300 if (lua_istable(L, -1)) {
301 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
303 if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
304 !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
307 schem->slice_probs[ypos] >>= 1;
315 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
318 index = lua_gettop(L) + 1 + index;
321 while (lua_next(L, index)) {
322 std::string replace_from;
323 std::string replace_to;
325 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
326 lua_rawgeti(L, -1, 1);
327 replace_from = lua_tostring(L, -1);
330 lua_rawgeti(L, -1, 2);
331 replace_to = lua_tostring(L, -1);
333 } else { // New {x = "y", ...} format
334 replace_from = lua_tostring(L, -2);
335 replace_to = lua_tostring(L, -1);
338 replace_names->insert(std::make_pair(replace_from, replace_to));
343 ///////////////////////////////////////////////////////////////////////////////
345 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
348 index = lua_gettop(L) + 1 + index;
350 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
354 biome = read_biome_def(L, index, biomemgr->getNodeDef());
358 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
367 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
369 if (!lua_istable(L, index))
372 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
373 ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL);
374 Biome *b = BiomeManager::create(biometype);
376 b->name = getstringfield_default(L, index, "name", "");
377 b->depth_top = getintfield_default(L, index, "depth_top", 0);
378 b->depth_filler = getintfield_default(L, index, "depth_filler", -31000);
379 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
380 b->y_min = getintfield_default(L, index, "y_min", -31000);
381 b->y_max = getintfield_default(L, index, "y_max", 31000);
382 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
383 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
384 b->flags = 0; //reserved
386 std::vector<std::string> &nn = b->m_nodenames;
387 nn.push_back(getstringfield_default(L, index, "node_top", ""));
388 nn.push_back(getstringfield_default(L, index, "node_filler", ""));
389 nn.push_back(getstringfield_default(L, index, "node_stone", ""));
390 nn.push_back(getstringfield_default(L, index, "node_water_top", ""));
391 nn.push_back(getstringfield_default(L, index, "node_water", ""));
392 nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
393 nn.push_back(getstringfield_default(L, index, "node_dust", ""));
394 ndef->pendNodeResolve(b);
400 size_t get_biome_list(lua_State *L, int index,
401 BiomeManager *biomemgr, std::set<u8> *biome_id_list)
404 index = lua_gettop(L) + 1 + index;
406 if (lua_isnil(L, index))
409 bool is_single = true;
410 if (lua_istable(L, index)) {
411 lua_getfield(L, index, "name");
412 is_single = !lua_isnil(L, -1);
417 Biome *biome = get_or_load_biome(L, index, biomemgr);
419 errorstream << "get_biome_list: failed to get biome '"
420 << (lua_isstring(L, index) ? lua_tostring(L, index) : "")
421 << "'." << std::endl;
425 biome_id_list->insert(biome->index);
429 // returns number of failed resolutions
430 size_t fail_count = 0;
433 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
435 Biome *biome = get_or_load_biome(L, -1, biomemgr);
438 errorstream << "get_biome_list: failed to get biome '"
439 << (lua_isstring(L, -1) ? lua_tostring(L, -1) : "")
444 biome_id_list->insert(biome->index);
450 ///////////////////////////////////////////////////////////////////////////////
452 // get_mapgen_object(objectname)
453 // returns the requested object used during map generation
454 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
456 const char *mgobjstr = lua_tostring(L, 1);
459 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
462 enum MapgenObject mgobj = (MapgenObject)mgobjint;
464 EmergeManager *emerge = getServer(L)->getEmergeManager();
465 Mapgen *mg = emerge->getCurrentMapgen();
469 size_t maplen = mg->csize.X * mg->csize.Z;
473 MMVManip *vm = mg->vm;
476 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
477 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
478 luaL_getmetatable(L, "VoxelManip");
479 lua_setmetatable(L, -2);
482 push_v3s16(L, vm->m_area.MinEdge);
485 push_v3s16(L, vm->m_area.MaxEdge);
489 case MGOBJ_HEIGHTMAP: {
494 for (size_t i = 0; i != maplen; i++) {
495 lua_pushinteger(L, mg->heightmap[i]);
496 lua_rawseti(L, -2, i + 1);
501 case MGOBJ_BIOMEMAP: {
506 for (size_t i = 0; i != maplen; i++) {
507 lua_pushinteger(L, mg->biomemap[i]);
508 lua_rawseti(L, -2, i + 1);
513 case MGOBJ_HEATMAP: {
518 for (size_t i = 0; i != maplen; i++) {
519 lua_pushnumber(L, mg->heatmap[i]);
520 lua_rawseti(L, -2, i + 1);
526 case MGOBJ_HUMIDMAP: {
531 for (size_t i = 0; i != maplen; i++) {
532 lua_pushnumber(L, mg->humidmap[i]);
533 lua_rawseti(L, -2, i + 1);
538 case MGOBJ_GENNOTIFY: {
539 std::map<std::string, std::vector<v3s16> >event_map;
540 std::map<std::string, std::vector<v3s16> >::iterator it;
542 mg->gennotify.getEvents(event_map);
545 for (it = event_map.begin(); it != event_map.end(); ++it) {
548 for (size_t j = 0; j != it->second.size(); j++) {
549 push_v3s16(L, it->second[j]);
550 lua_rawseti(L, -2, j + 1);
553 lua_setfield(L, -2, it->first.c_str());
564 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
566 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
570 lua_pushstring(L, params->mg_name.c_str());
571 lua_setfield(L, -2, "mgname");
573 lua_pushinteger(L, params->seed);
574 lua_setfield(L, -2, "seed");
576 lua_pushinteger(L, params->water_level);
577 lua_setfield(L, -2, "water_level");
579 lua_pushinteger(L, params->chunksize);
580 lua_setfield(L, -2, "chunksize");
582 std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, (u32)-1);
583 lua_pushstring(L, flagstr.c_str());
584 lua_setfield(L, -2, "flags");
590 // set_mapgen_params(params)
591 // set mapgen parameters
592 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
594 if (!lua_istable(L, 1))
597 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
598 u32 flags = 0, flagmask = 0;
600 lua_getfield(L, 1, "mgname");
601 if (lua_isstring(L, -1)) {
602 params->mg_name = lua_tostring(L, -1);
603 delete params->sparams;
604 params->sparams = NULL;
607 lua_getfield(L, 1, "seed");
608 if (lua_isnumber(L, -1))
609 params->seed = lua_tointeger(L, -1);
611 lua_getfield(L, 1, "water_level");
612 if (lua_isnumber(L, -1))
613 params->water_level = lua_tointeger(L, -1);
615 warn_if_field_exists(L, 1, "flagmask",
616 "Deprecated: flags field now includes unset flags.");
617 lua_getfield(L, 1, "flagmask");
618 if (lua_isstring(L, -1))
619 params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL);
621 if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
622 params->flags &= ~flagmask;
623 params->flags |= flags;
630 // set_noiseparams(name, noiseparams, set_default)
631 // set global config values for noise parameters
632 int ModApiMapgen::l_set_noiseparams(lua_State *L)
634 const char *name = luaL_checkstring(L, 1);
637 if (!read_noiseparams(L, 2, &np))
640 bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
642 g_settings->setNoiseParams(name, np, set_default);
648 // get_noiseparams(name)
649 int ModApiMapgen::l_get_noiseparams(lua_State *L)
651 std::string name = luaL_checkstring(L, 1);
654 if (!g_settings->getNoiseParams(name, np))
657 push_noiseparams(L, &np);
662 // set_gen_notify(flags, {deco_id_table})
663 int ModApiMapgen::l_set_gen_notify(lua_State *L)
665 u32 flags = 0, flagmask = 0;
666 EmergeManager *emerge = getServer(L)->getEmergeManager();
668 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
669 emerge->gen_notify_on &= ~flagmask;
670 emerge->gen_notify_on |= flags;
673 if (lua_istable(L, 2)) {
675 while (lua_next(L, 2)) {
676 if (lua_isnumber(L, -1))
677 emerge->gen_notify_on_deco_ids.insert((u32)lua_tonumber(L, -1));
687 int ModApiMapgen::l_get_gen_notify(lua_State *L)
689 EmergeManager *emerge = getServer(L)->getEmergeManager();
690 push_flags_string(L, flagdesc_gennotify, emerge->gen_notify_on,
691 emerge->gen_notify_on);
695 for (std::set<u32>::iterator it = emerge->gen_notify_on_deco_ids.begin();
696 it != emerge->gen_notify_on_deco_ids.end(); ++it) {
697 lua_pushnumber(L, *it);
698 lua_rawseti(L, -2, i);
705 // register_biome({lots of stuff})
706 int ModApiMapgen::l_register_biome(lua_State *L)
709 luaL_checktype(L, index, LUA_TTABLE);
711 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
712 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
714 Biome *biome = read_biome_def(L, index, ndef);
718 ObjDefHandle handle = bmgr->add(biome);
719 if (handle == OBJDEF_INVALID_HANDLE) {
724 lua_pushinteger(L, handle);
729 // register_decoration({lots of stuff})
730 int ModApiMapgen::l_register_decoration(lua_State *L)
733 luaL_checktype(L, index, LUA_TTABLE);
735 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
736 DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
737 BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr;
738 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
740 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
741 "deco_type", es_DecorationType, -1);
743 Decoration *deco = decomgr->create(decotype);
745 errorstream << "register_decoration: decoration placement type "
746 << decotype << " not implemented" << std::endl;
750 deco->name = getstringfield_default(L, index, "name", "");
751 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
752 deco->y_min = getintfield_default(L, index, "y_min", -31000);
753 deco->y_max = getintfield_default(L, index, "y_max", 31000);
754 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
755 if (deco->sidelen <= 0) {
756 errorstream << "register_decoration: sidelen must be "
757 "greater than 0" << std::endl;
762 //// Get node name(s) to place decoration on
763 size_t nread = getstringlistfield(L, index, "place_on", &deco->m_nodenames);
764 deco->m_nnlistsizes.push_back(nread);
766 //// Get decoration flags
767 getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
769 //// Get NoiseParams to define how decoration is placed
770 lua_getfield(L, index, "noise_params");
771 if (read_noiseparams(L, -1, &deco->np))
772 deco->flags |= DECO_USE_NOISE;
775 //// Get biomes associated with this decoration (if any)
776 lua_getfield(L, index, "biomes");
777 if (get_biome_list(L, -1, biomemgr, &deco->biomes))
778 errorstream << "register_decoration: couldn't get all biomes " << std::endl;
781 //// Handle decoration type-specific parameters
782 bool success = false;
785 success = read_deco_simple(L, (DecoSimple *)deco);
788 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
799 ndef->pendNodeResolve(deco);
801 ObjDefHandle handle = decomgr->add(deco);
802 if (handle == OBJDEF_INVALID_HANDLE) {
807 lua_pushinteger(L, handle);
812 bool read_deco_simple(lua_State *L, DecoSimple *deco)
817 deco->deco_height = getintfield_default(L, index, "height", 1);
818 deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
819 deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
821 if (deco->deco_height <= 0) {
822 errorstream << "register_decoration: simple decoration height"
823 " must be greater than 0" << std::endl;
827 nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames);
828 deco->m_nnlistsizes.push_back(nnames);
830 errorstream << "register_decoration: no decoration nodes "
831 "defined" << std::endl;
835 nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames);
836 deco->m_nnlistsizes.push_back(nnames);
837 if (nnames == 0 && deco->nspawnby != -1) {
838 errorstream << "register_decoration: no spawn_by nodes defined,"
839 " but num_spawn_by specified" << std::endl;
847 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
851 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
852 ModApiMapgen::es_Rotation, ROTATE_0);
854 StringMap replace_names;
855 lua_getfield(L, index, "replacements");
856 if (lua_istable(L, -1))
857 read_schematic_replacements(L, -1, &replace_names);
860 lua_getfield(L, index, "schematic");
861 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
864 deco->schematic = schem;
865 return schem != NULL;
869 // register_ore({lots of stuff})
870 int ModApiMapgen::l_register_ore(lua_State *L)
873 luaL_checktype(L, index, LUA_TTABLE);
875 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
876 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
877 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
879 enum OreType oretype = (OreType)getenumfield(L, index,
880 "ore_type", es_OreType, ORE_SCATTER);
881 Ore *ore = oremgr->create(oretype);
883 errorstream << "register_ore: ore_type " << oretype << " not implemented";
887 ore->name = getstringfield_default(L, index, "name", "");
888 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
889 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
890 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
891 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
892 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0);
897 warn_if_field_exists(L, index, "height_min",
898 "Deprecated: new name is \"y_min\".");
899 warn_if_field_exists(L, index, "height_max",
900 "Deprecated: new name is \"y_max\".");
903 if (!getintfield(L, index, "y_min", ymin) &&
904 !getintfield(L, index, "height_min", ymin))
906 if (!getintfield(L, index, "y_max", ymax) &&
907 !getintfield(L, index, "height_max", ymax))
912 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
913 errorstream << "register_ore: clust_scarcity and clust_num_ores"
914 "must be greater than 0" << std::endl;
920 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
922 //// Get biomes associated with this decoration (if any)
923 lua_getfield(L, index, "biomes");
924 if (get_biome_list(L, -1, bmgr, &ore->biomes))
925 errorstream << "register_ore: couldn't get all biomes " << std::endl;
928 //// Get noise parameters if needed
929 lua_getfield(L, index, "noise_params");
930 if (read_noiseparams(L, -1, &ore->np)) {
931 ore->flags |= OREFLAG_USE_NOISE;
932 } else if (ore->NEEDS_NOISE) {
933 errorstream << "register_ore: specified ore type requires valid "
934 "noise parameters" << std::endl;
940 if (oretype == ORE_VEIN) {
941 OreVein *orevein = (OreVein *)ore;
942 orevein->random_factor = getfloatfield_default(L, index,
943 "random_factor", 1.f);
946 ObjDefHandle handle = oremgr->add(ore);
947 if (handle == OBJDEF_INVALID_HANDLE) {
952 ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
954 size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
955 ore->m_nnlistsizes.push_back(nnames);
957 ndef->pendNodeResolve(ore);
959 lua_pushinteger(L, handle);
964 // register_schematic({schematic}, replacements={})
965 int ModApiMapgen::l_register_schematic(lua_State *L)
967 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
969 StringMap replace_names;
970 if (lua_istable(L, 2))
971 read_schematic_replacements(L, 2, &replace_names);
973 Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
978 ObjDefHandle handle = schemmgr->add(schem);
979 if (handle == OBJDEF_INVALID_HANDLE) {
984 lua_pushinteger(L, handle);
989 // clear_registered_biomes()
990 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
992 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
998 // clear_registered_decorations()
999 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
1001 DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
1007 // clear_registered_ores()
1008 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
1010 OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
1016 // clear_registered_schematics()
1017 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
1019 SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
1025 // generate_ores(vm, p1, p2, [ore_id])
1026 int ModApiMapgen::l_generate_ores(lua_State *L)
1028 EmergeManager *emerge = getServer(L)->getEmergeManager();
1031 mg.seed = emerge->params.seed;
1032 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1033 mg.ndef = getServer(L)->getNodeDefManager();
1035 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1036 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1037 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1038 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1039 sortBoxVerticies(pmin, pmax);
1041 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1043 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
1049 // generate_decorations(vm, p1, p2, [deco_id])
1050 int ModApiMapgen::l_generate_decorations(lua_State *L)
1052 EmergeManager *emerge = getServer(L)->getEmergeManager();
1055 mg.seed = emerge->params.seed;
1056 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1057 mg.ndef = getServer(L)->getNodeDefManager();
1059 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1060 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1061 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1062 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1063 sortBoxVerticies(pmin, pmax);
1065 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1067 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
1073 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
1074 int ModApiMapgen::l_create_schematic(lua_State *L)
1076 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1078 const char *filename = luaL_checkstring(L, 4);
1079 CHECK_SECURE_PATH_OPTIONAL(L, filename);
1081 Map *map = &(getEnv(L)->getMap());
1084 v3s16 p1 = check_v3s16(L, 1);
1085 v3s16 p2 = check_v3s16(L, 2);
1086 sortBoxVerticies(p1, p2);
1088 std::vector<std::pair<v3s16, u8> > prob_list;
1089 if (lua_istable(L, 3)) {
1091 while (lua_next(L, 3)) {
1092 if (lua_istable(L, -1)) {
1093 lua_getfield(L, -1, "pos");
1094 v3s16 pos = check_v3s16(L, -1);
1097 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1098 prob_list.push_back(std::make_pair(pos, prob));
1105 std::vector<std::pair<s16, u8> > slice_prob_list;
1106 if (lua_istable(L, 5)) {
1108 while (lua_next(L, 5)) {
1109 if (lua_istable(L, -1)) {
1110 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1111 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1112 slice_prob_list.push_back(std::make_pair(ypos, prob));
1119 if (!schem.getSchematicFromMap(map, p1, p2)) {
1120 errorstream << "create_schematic: failed to get schematic "
1121 "from map" << std::endl;
1125 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1127 schem.saveSchematicToFile(filename, ndef);
1128 actionstream << "create_schematic: saved schematic file '"
1129 << filename << "'." << std::endl;
1131 lua_pushboolean(L, true);
1136 // place_schematic(p, schematic, rotation, replacement)
1137 int ModApiMapgen::l_place_schematic(lua_State *L)
1139 Map *map = &(getEnv(L)->getMap());
1140 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1143 v3s16 p = check_v3s16(L, 1);
1147 const char *enumstr = lua_tostring(L, 3);
1149 string_to_enum(es_Rotation, rot, std::string(enumstr));
1151 //// Read force placement
1152 bool force_placement = true;
1153 if (lua_isboolean(L, 5))
1154 force_placement = lua_toboolean(L, 5);
1156 //// Read node replacements
1157 StringMap replace_names;
1158 if (lua_istable(L, 4))
1159 read_schematic_replacements(L, 4, &replace_names);
1162 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1164 errorstream << "place_schematic: failed to get schematic" << std::endl;
1168 schem->placeStructure(map, p, 0, (Rotation)rot, force_placement);
1170 lua_pushboolean(L, true);
1174 // serialize_schematic(schematic, format, options={...})
1175 int ModApiMapgen::l_serialize_schematic(lua_State *L)
1177 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1180 bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
1181 u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
1184 bool was_loaded = false;
1185 Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1187 schem = load_schematic(L, 1, NULL, NULL);
1191 errorstream << "serialize_schematic: failed to get schematic" << std::endl;
1195 //// Read format of definition to save as
1196 int schem_format = SCHEM_FMT_MTS;
1197 const char *enumstr = lua_tostring(L, 2);
1199 string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr));
1201 //// Serialize to binary string
1202 std::ostringstream os(std::ios_base::binary);
1203 switch (schem_format) {
1205 schem->serializeToMts(&os, schem->m_nodenames);
1208 schem->serializeToLua(&os, schem->m_nodenames,
1209 use_comments, indent_spaces);
1218 std::string ser = os.str();
1219 lua_pushlstring(L, ser.c_str(), ser.length());
1224 void ModApiMapgen::Initialize(lua_State *L, int top)
1226 API_FCT(get_mapgen_object);
1228 API_FCT(get_mapgen_params);
1229 API_FCT(set_mapgen_params);
1230 API_FCT(set_noiseparams);
1231 API_FCT(get_noiseparams);
1232 API_FCT(set_gen_notify);
1233 API_FCT(get_gen_notify);
1235 API_FCT(register_biome);
1236 API_FCT(register_decoration);
1237 API_FCT(register_ore);
1238 API_FCT(register_schematic);
1240 API_FCT(clear_registered_biomes);
1241 API_FCT(clear_registered_decorations);
1242 API_FCT(clear_registered_ores);
1243 API_FCT(clear_registered_schematics);
1245 API_FCT(generate_ores);
1246 API_FCT(generate_decorations);
1247 API_FCT(create_schematic);
1248 API_FCT(place_schematic);
1249 API_FCT(serialize_schematic);