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 "util/serialize.h"
27 #include "environment.h"
31 #include "mg_decoration.h"
32 #include "mg_schematic.h"
33 #include "mapgen_v5.h"
34 #include "mapgen_v7.h"
39 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
41 {BIOME_NORMAL, "normal"},
42 {BIOME_LIQUID, "liquid"},
43 {BIOME_NETHER, "nether"},
44 {BIOME_AETHER, "aether"},
49 struct EnumString ModApiMapgen::es_DecorationType[] =
51 {DECO_SIMPLE, "simple"},
52 {DECO_SCHEMATIC, "schematic"},
53 {DECO_LSYSTEM, "lsystem"},
57 struct EnumString ModApiMapgen::es_MapgenObject[] =
59 {MGOBJ_VMANIP, "voxelmanip"},
60 {MGOBJ_HEIGHTMAP, "heightmap"},
61 {MGOBJ_BIOMEMAP, "biomemap"},
62 {MGOBJ_HEATMAP, "heatmap"},
63 {MGOBJ_HUMIDMAP, "humiditymap"},
64 {MGOBJ_GENNOTIFY, "gennotify"},
68 struct EnumString ModApiMapgen::es_OreType[] =
70 {ORE_SCATTER, "scatter"},
77 struct EnumString ModApiMapgen::es_Rotation[] =
83 {ROTATE_RAND, "random"},
87 struct EnumString ModApiMapgen::es_SchematicFormatType[] =
89 {SCHEM_FMT_HANDLE, "handle"},
90 {SCHEM_FMT_MTS, "mts"},
91 {SCHEM_FMT_LUA, "lua"},
95 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
97 Biome *get_or_load_biome(lua_State *L, int index,
98 BiomeManager *biomemgr);
99 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
100 size_t get_biome_list(lua_State *L, int index,
101 BiomeManager *biomemgr, std::set<u8> *biome_id_list);
103 Schematic *get_or_load_schematic(lua_State *L, int index,
104 SchematicManager *schemmgr, StringMap *replace_names);
105 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
106 StringMap *replace_names);
107 Schematic *load_schematic_from_def(lua_State *L, int index,
108 INodeDefManager *ndef, StringMap *replace_names);
109 bool read_schematic_def(lua_State *L, int index,
110 Schematic *schem, std::vector<std::string> *names);
112 bool read_deco_simple(lua_State *L, DecoSimple *deco);
113 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
116 ///////////////////////////////////////////////////////////////////////////////
118 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
121 index = lua_gettop(L) + 1 + index;
123 // If a number, assume this is a handle to an object def
124 if (lua_isnumber(L, index))
125 return objmgr->get(lua_tointeger(L, index));
127 // If a string, assume a name is given instead
128 if (lua_isstring(L, index))
129 return objmgr->getByName(lua_tostring(L, index));
134 ///////////////////////////////////////////////////////////////////////////////
136 Schematic *get_or_load_schematic(lua_State *L, int index,
137 SchematicManager *schemmgr, StringMap *replace_names)
140 index = lua_gettop(L) + 1 + index;
142 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
146 schem = load_schematic(L, index, schemmgr->getNodeDef(),
151 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
160 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
161 StringMap *replace_names)
164 index = lua_gettop(L) + 1 + index;
166 Schematic *schem = NULL;
168 if (lua_istable(L, index)) {
169 schem = load_schematic_from_def(L, index, ndef,
175 } else if (lua_isnumber(L, index)) {
177 } else if (lua_isstring(L, index)) {
178 schem = SchematicManager::create(SCHEMATIC_NORMAL);
180 std::string filepath = lua_tostring(L, index);
181 if (!fs::IsPathAbsolute(filepath))
182 filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
184 if (!schem->loadSchematicFromFile(filepath, ndef,
195 Schematic *load_schematic_from_def(lua_State *L, int index,
196 INodeDefManager *ndef, StringMap *replace_names)
198 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
200 if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
205 size_t num_nodes = schem->m_nodenames.size();
207 schem->m_nnlistsizes.push_back(num_nodes);
210 for (size_t i = 0; i != num_nodes; i++) {
211 StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
212 if (it != replace_names->end())
213 schem->m_nodenames[i] = it->second;
217 ndef->pendNodeResolve(schem);
223 bool read_schematic_def(lua_State *L, int index,
224 Schematic *schem, std::vector<std::string> *names)
226 if (!lua_istable(L, index))
229 //// Get schematic size
230 lua_getfield(L, index, "size");
231 v3s16 size = check_v3s16(L, -1);
236 //// Get schematic data
237 lua_getfield(L, index, "data");
238 luaL_checktype(L, -1, LUA_TTABLE);
240 u32 numnodes = size.X * size.Y * size.Z;
241 schem->schemdata = new MapNode[numnodes];
243 size_t names_base = names->size();
244 std::map<std::string, content_t> name_id_map;
247 for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
253 if (!getstringfield(L, -1, "name", name))
254 throw LuaError("Schematic data definition with missing name field");
256 //// Read param1/prob
258 if (!getintfield(L, -1, "param1", param1) &&
259 !getintfield(L, -1, "prob", param1))
260 param1 = MTSCHEM_PROB_ALWAYS_OLD;
263 u8 param2 = getintfield_default(L, -1, "param2", 0);
265 //// Find or add new nodename-to-ID mapping
266 std::map<std::string, content_t>::iterator it = name_id_map.find(name);
267 content_t name_index;
268 if (it != name_id_map.end()) {
269 name_index = it->second;
271 name_index = names->size() - names_base;
272 name_id_map[name] = name_index;
273 names->push_back(name);
276 //// Perform probability/force_place fixup on param1
278 if (getboolfield_default(L, -1, "force_place", false))
279 param1 |= MTSCHEM_FORCE_PLACE;
281 //// Actually set the node in the schematic
282 schem->schemdata[i] = MapNode(name_index, param1, param2);
286 errorstream << "read_schematic_def: incorrect number of "
287 "nodes provided in raw schematic data (got " << i <<
288 ", expected " << numnodes << ")." << std::endl;
292 //// Get Y-slice probability values (if present)
293 schem->slice_probs = new u8[size.Y];
294 for (i = 0; i != (u32) size.Y; i++)
295 schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
297 lua_getfield(L, index, "yslice_prob");
298 if (lua_istable(L, -1)) {
299 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
301 if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
302 !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
305 schem->slice_probs[ypos] >>= 1;
313 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
316 index = lua_gettop(L) + 1 + index;
319 while (lua_next(L, index)) {
320 std::string replace_from;
321 std::string replace_to;
323 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
324 lua_rawgeti(L, -1, 1);
325 replace_from = lua_tostring(L, -1);
328 lua_rawgeti(L, -1, 2);
329 replace_to = lua_tostring(L, -1);
331 } else { // New {x = "y", ...} format
332 replace_from = lua_tostring(L, -2);
333 replace_to = lua_tostring(L, -1);
336 replace_names->insert(std::make_pair(replace_from, replace_to));
341 ///////////////////////////////////////////////////////////////////////////////
343 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
346 index = lua_gettop(L) + 1 + index;
348 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
352 biome = read_biome_def(L, index, biomemgr->getNodeDef());
356 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
365 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
367 if (!lua_istable(L, index))
370 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
371 ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL);
372 Biome *b = BiomeManager::create(biometype);
374 b->name = getstringfield_default(L, index, "name", "");
375 b->depth_top = getintfield_default(L, index, "depth_top", 1);
376 b->depth_filler = getintfield_default(L, index, "depth_filler", 2);
377 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
378 b->y_min = getintfield_default(L, index, "y_min", -31000);
379 b->y_max = getintfield_default(L, index, "y_max", 31000);
380 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
381 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
382 b->flags = 0; //reserved
384 std::vector<std::string> &nn = b->m_nodenames;
385 nn.push_back(getstringfield_default(L, index, "node_top", ""));
386 nn.push_back(getstringfield_default(L, index, "node_filler", ""));
387 nn.push_back(getstringfield_default(L, index, "node_stone", ""));
388 nn.push_back(getstringfield_default(L, index, "node_water_top", ""));
389 nn.push_back(getstringfield_default(L, index, "node_water", ""));
390 nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
391 nn.push_back(getstringfield_default(L, index, "node_dust", ""));
392 ndef->pendNodeResolve(b);
398 size_t get_biome_list(lua_State *L, int index,
399 BiomeManager *biomemgr, std::set<u8> *biome_id_list)
402 index = lua_gettop(L) + 1 + index;
404 if (lua_isnil(L, index))
407 bool is_single = true;
408 if (lua_istable(L, index)) {
409 lua_getfield(L, index, "name");
410 is_single = !lua_isnil(L, -1);
415 Biome *biome = get_or_load_biome(L, index, biomemgr);
417 errorstream << "get_biome_list: failed to get biome" << std::endl;
421 biome_id_list->insert(biome->index);
425 // returns number of failed resolutions
426 size_t fail_count = 0;
429 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
431 Biome *biome = get_or_load_biome(L, -1, biomemgr);
434 errorstream << "get_biome_list: failed to load biome (index "
435 << count << ")" << std::endl;
439 biome_id_list->insert(biome->index);
445 ///////////////////////////////////////////////////////////////////////////////
447 // get_mapgen_object(objectname)
448 // returns the requested object used during map generation
449 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
451 const char *mgobjstr = lua_tostring(L, 1);
454 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
457 enum MapgenObject mgobj = (MapgenObject)mgobjint;
459 EmergeManager *emerge = getServer(L)->getEmergeManager();
460 Mapgen *mg = emerge->getCurrentMapgen();
464 size_t maplen = mg->csize.X * mg->csize.Z;
468 MMVManip *vm = mg->vm;
471 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
472 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
473 luaL_getmetatable(L, "VoxelManip");
474 lua_setmetatable(L, -2);
477 push_v3s16(L, vm->m_area.MinEdge);
480 push_v3s16(L, vm->m_area.MaxEdge);
484 case MGOBJ_HEIGHTMAP: {
489 for (size_t i = 0; i != maplen; i++) {
490 lua_pushinteger(L, mg->heightmap[i]);
491 lua_rawseti(L, -2, i + 1);
496 case MGOBJ_BIOMEMAP: {
501 for (size_t i = 0; i != maplen; i++) {
502 lua_pushinteger(L, mg->biomemap[i]);
503 lua_rawseti(L, -2, i + 1);
508 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
510 if (strcmp(emerge->params.mg_name.c_str(), "v7"))
513 MapgenV7 *mgv7 = (MapgenV7 *)mg;
515 float *arr = (mgobj == MGOBJ_HEATMAP) ?
516 mgv7->noise_heat->result : mgv7->noise_humidity->result;
521 for (size_t i = 0; i != maplen; i++) {
522 lua_pushnumber(L, arr[i]);
523 lua_rawseti(L, -2, i + 1);
528 case MGOBJ_GENNOTIFY: {
529 std::map<std::string, std::vector<v3s16> >event_map;
530 std::map<std::string, std::vector<v3s16> >::iterator it;
532 mg->gennotify.getEvents(event_map);
535 for (it = event_map.begin(); it != event_map.end(); ++it) {
538 for (size_t j = 0; j != it->second.size(); j++) {
539 push_v3s16(L, it->second[j]);
540 lua_rawseti(L, -2, j + 1);
543 lua_setfield(L, -2, it->first.c_str());
554 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
556 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
560 lua_pushstring(L, params->mg_name.c_str());
561 lua_setfield(L, -2, "mgname");
563 lua_pushinteger(L, params->seed);
564 lua_setfield(L, -2, "seed");
566 lua_pushinteger(L, params->water_level);
567 lua_setfield(L, -2, "water_level");
569 lua_pushinteger(L, params->chunksize);
570 lua_setfield(L, -2, "chunksize");
572 std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, (u32)-1);
573 lua_pushstring(L, flagstr.c_str());
574 lua_setfield(L, -2, "flags");
580 // set_mapgen_params(params)
581 // set mapgen parameters
582 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
584 if (!lua_istable(L, 1))
587 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
588 u32 flags = 0, flagmask = 0;
590 lua_getfield(L, 1, "mgname");
591 if (lua_isstring(L, -1)) {
592 params->mg_name = lua_tostring(L, -1);
593 delete params->sparams;
594 params->sparams = NULL;
597 lua_getfield(L, 1, "seed");
598 if (lua_isnumber(L, -1))
599 params->seed = lua_tointeger(L, -1);
601 lua_getfield(L, 1, "water_level");
602 if (lua_isnumber(L, -1))
603 params->water_level = lua_tointeger(L, -1);
605 warn_if_field_exists(L, 1, "flagmask",
606 "Deprecated: flags field now includes unset flags.");
607 lua_getfield(L, 1, "flagmask");
608 if (lua_isstring(L, -1))
609 params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL);
611 if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
612 params->flags &= ~flagmask;
613 params->flags |= flags;
620 // set_noiseparams(name, noiseparams, set_default)
621 // set global config values for noise parameters
622 int ModApiMapgen::l_set_noiseparams(lua_State *L)
624 const char *name = luaL_checkstring(L, 1);
627 if (!read_noiseparams(L, 2, &np))
630 bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
632 g_settings->setNoiseParams(name, np, set_default);
638 // set_gen_notify(flags, {deco_id_table})
639 int ModApiMapgen::l_set_gen_notify(lua_State *L)
641 u32 flags = 0, flagmask = 0;
642 EmergeManager *emerge = getServer(L)->getEmergeManager();
644 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
645 emerge->gen_notify_on &= ~flagmask;
646 emerge->gen_notify_on |= flags;
649 if (lua_istable(L, 2)) {
651 while (lua_next(L, 2)) {
652 if (lua_isnumber(L, -1))
653 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
662 // register_biome({lots of stuff})
663 int ModApiMapgen::l_register_biome(lua_State *L)
666 luaL_checktype(L, index, LUA_TTABLE);
668 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
669 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
671 Biome *biome = read_biome_def(L, index, ndef);
675 ObjDefHandle handle = bmgr->add(biome);
676 if (handle == OBJDEF_INVALID_HANDLE) {
681 lua_pushinteger(L, handle);
686 // register_decoration({lots of stuff})
687 int ModApiMapgen::l_register_decoration(lua_State *L)
690 luaL_checktype(L, index, LUA_TTABLE);
692 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
693 DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
694 BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr;
695 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
697 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
698 "deco_type", es_DecorationType, -1);
700 Decoration *deco = decomgr->create(decotype);
702 errorstream << "register_decoration: decoration placement type "
703 << decotype << " not implemented" << std::endl;
707 deco->name = getstringfield_default(L, index, "name", "");
708 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
709 deco->y_min = getintfield_default(L, index, "y_min", -31000);
710 deco->y_max = getintfield_default(L, index, "y_max", 31000);
711 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
712 if (deco->sidelen <= 0) {
713 errorstream << "register_decoration: sidelen must be "
714 "greater than 0" << std::endl;
719 //// Get node name(s) to place decoration on
720 size_t nread = getstringlistfield(L, index, "place_on", &deco->m_nodenames);
721 deco->m_nnlistsizes.push_back(nread);
723 //// Get decoration flags
724 getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
726 //// Get NoiseParams to define how decoration is placed
727 lua_getfield(L, index, "noise_params");
728 if (read_noiseparams(L, -1, &deco->np))
729 deco->flags |= DECO_USE_NOISE;
732 //// Get biomes associated with this decoration (if any)
733 lua_getfield(L, index, "biomes");
734 if (get_biome_list(L, -1, biomemgr, &deco->biomes))
735 errorstream << "register_decoration: couldn't get all biomes " << std::endl;
738 //// Handle decoration type-specific parameters
739 bool success = false;
742 success = read_deco_simple(L, (DecoSimple *)deco);
745 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
756 ndef->pendNodeResolve(deco);
758 ObjDefHandle handle = decomgr->add(deco);
759 if (handle == OBJDEF_INVALID_HANDLE) {
764 lua_pushinteger(L, handle);
769 bool read_deco_simple(lua_State *L, DecoSimple *deco)
774 deco->deco_height = getintfield_default(L, index, "height", 1);
775 deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
776 deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
778 if (deco->deco_height <= 0) {
779 errorstream << "register_decoration: simple decoration height"
780 " must be greater than 0" << std::endl;
784 nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames);
785 deco->m_nnlistsizes.push_back(nnames);
787 errorstream << "register_decoration: no decoration nodes "
788 "defined" << std::endl;
792 nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames);
793 deco->m_nnlistsizes.push_back(nnames);
794 if (nnames == 0 && deco->nspawnby != -1) {
795 errorstream << "register_decoration: no spawn_by nodes defined,"
796 " but num_spawn_by specified" << std::endl;
804 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
808 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
809 ModApiMapgen::es_Rotation, ROTATE_0);
811 StringMap replace_names;
812 lua_getfield(L, index, "replacements");
813 if (lua_istable(L, -1))
814 read_schematic_replacements(L, -1, &replace_names);
817 lua_getfield(L, index, "schematic");
818 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
821 deco->schematic = schem;
822 return schem != NULL;
826 // register_ore({lots of stuff})
827 int ModApiMapgen::l_register_ore(lua_State *L)
830 luaL_checktype(L, index, LUA_TTABLE);
832 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
833 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
834 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
836 enum OreType oretype = (OreType)getenumfield(L, index,
837 "ore_type", es_OreType, ORE_SCATTER);
838 Ore *ore = oremgr->create(oretype);
840 errorstream << "register_ore: ore_type " << oretype << " not implemented";
844 ore->name = getstringfield_default(L, index, "name", "");
845 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
846 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
847 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
848 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
849 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0);
854 warn_if_field_exists(L, index, "height_min",
855 "Deprecated: new name is \"y_min\".");
856 warn_if_field_exists(L, index, "height_max",
857 "Deprecated: new name is \"y_max\".");
860 if (!getintfield(L, index, "y_min", ymin) &&
861 !getintfield(L, index, "height_min", ymin))
863 if (!getintfield(L, index, "y_max", ymax) &&
864 !getintfield(L, index, "height_max", ymax))
869 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
870 errorstream << "register_ore: clust_scarcity and clust_num_ores"
871 "must be greater than 0" << std::endl;
877 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
879 //// Get biomes associated with this decoration (if any)
880 lua_getfield(L, index, "biomes");
881 if (get_biome_list(L, -1, bmgr, &ore->biomes))
882 errorstream << "register_ore: couldn't get all biomes " << std::endl;
885 //// Get noise parameters if needed
886 lua_getfield(L, index, "noise_params");
887 if (read_noiseparams(L, -1, &ore->np)) {
888 ore->flags |= OREFLAG_USE_NOISE;
889 } else if (ore->NEEDS_NOISE) {
890 errorstream << "register_ore: specified ore type requires valid "
891 "noise parameters" << std::endl;
897 if (oretype == ORE_VEIN) {
898 OreVein *orevein = (OreVein *)ore;
899 orevein->random_factor = getfloatfield_default(L, index,
900 "random_factor", 1.f);
903 ObjDefHandle handle = oremgr->add(ore);
904 if (handle == OBJDEF_INVALID_HANDLE) {
909 ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
911 size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
912 ore->m_nnlistsizes.push_back(nnames);
914 ndef->pendNodeResolve(ore);
916 lua_pushinteger(L, handle);
921 // register_schematic({schematic}, replacements={})
922 int ModApiMapgen::l_register_schematic(lua_State *L)
924 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
926 StringMap replace_names;
927 if (lua_istable(L, 2))
928 read_schematic_replacements(L, 2, &replace_names);
930 Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
935 ObjDefHandle handle = schemmgr->add(schem);
936 if (handle == OBJDEF_INVALID_HANDLE) {
941 lua_pushinteger(L, handle);
946 // clear_registered_biomes()
947 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
949 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
955 // clear_registered_decorations()
956 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
958 DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
964 // clear_registered_ores()
965 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
967 OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
973 // clear_registered_schematics()
974 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
976 SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
982 // generate_ores(vm, p1, p2, [ore_id])
983 int ModApiMapgen::l_generate_ores(lua_State *L)
985 EmergeManager *emerge = getServer(L)->getEmergeManager();
988 mg.seed = emerge->params.seed;
989 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
990 mg.ndef = getServer(L)->getNodeDefManager();
992 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
993 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
994 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
995 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
996 sortBoxVerticies(pmin, pmax);
998 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1000 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
1006 // generate_decorations(vm, p1, p2, [deco_id])
1007 int ModApiMapgen::l_generate_decorations(lua_State *L)
1009 EmergeManager *emerge = getServer(L)->getEmergeManager();
1012 mg.seed = emerge->params.seed;
1013 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
1014 mg.ndef = getServer(L)->getNodeDefManager();
1016 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1017 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1018 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1019 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1020 sortBoxVerticies(pmin, pmax);
1022 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1024 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
1030 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
1031 int ModApiMapgen::l_create_schematic(lua_State *L)
1033 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1034 Map *map = &(getEnv(L)->getMap());
1037 v3s16 p1 = check_v3s16(L, 1);
1038 v3s16 p2 = check_v3s16(L, 2);
1039 sortBoxVerticies(p1, p2);
1041 std::vector<std::pair<v3s16, u8> > prob_list;
1042 if (lua_istable(L, 3)) {
1044 while (lua_next(L, 3)) {
1045 if (lua_istable(L, -1)) {
1046 lua_getfield(L, -1, "pos");
1047 v3s16 pos = check_v3s16(L, -1);
1050 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1051 prob_list.push_back(std::make_pair(pos, prob));
1058 std::vector<std::pair<s16, u8> > slice_prob_list;
1059 if (lua_istable(L, 5)) {
1061 while (lua_next(L, 5)) {
1062 if (lua_istable(L, -1)) {
1063 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1064 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1065 slice_prob_list.push_back(std::make_pair(ypos, prob));
1072 const char *filename = luaL_checkstring(L, 4);
1074 if (!schem.getSchematicFromMap(map, p1, p2)) {
1075 errorstream << "create_schematic: failed to get schematic "
1076 "from map" << std::endl;
1080 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1082 schem.saveSchematicToFile(filename, ndef);
1083 actionstream << "create_schematic: saved schematic file '"
1084 << filename << "'." << std::endl;
1086 lua_pushboolean(L, true);
1091 // place_schematic(p, schematic, rotation, replacement)
1092 int ModApiMapgen::l_place_schematic(lua_State *L)
1094 Map *map = &(getEnv(L)->getMap());
1095 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1098 v3s16 p = check_v3s16(L, 1);
1102 const char *enumstr = lua_tostring(L, 3);
1104 string_to_enum(es_Rotation, rot, std::string(enumstr));
1106 //// Read force placement
1107 bool force_placement = true;
1108 if (lua_isboolean(L, 5))
1109 force_placement = lua_toboolean(L, 5);
1111 //// Read node replacements
1112 StringMap replace_names;
1113 if (lua_istable(L, 4))
1114 read_schematic_replacements(L, 4, &replace_names);
1117 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1119 errorstream << "place_schematic: failed to get schematic" << std::endl;
1123 schem->placeStructure(map, p, 0, (Rotation)rot, force_placement);
1125 lua_pushboolean(L, true);
1129 // serialize_schematic(schematic, format, options={...})
1130 int ModApiMapgen::l_serialize_schematic(lua_State *L)
1132 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1135 bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
1136 u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
1139 bool was_loaded = false;
1140 Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1142 schem = load_schematic(L, 1, NULL, NULL);
1146 errorstream << "serialize_schematic: failed to get schematic" << std::endl;
1150 //// Read format of definition to save as
1151 int schem_format = SCHEM_FMT_MTS;
1152 const char *enumstr = lua_tostring(L, 2);
1154 string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr));
1156 //// Serialize to binary string
1157 std::ostringstream os(std::ios_base::binary);
1158 switch (schem_format) {
1160 schem->serializeToMts(&os, schem->m_nodenames);
1163 schem->serializeToLua(&os, schem->m_nodenames,
1164 use_comments, indent_spaces);
1173 std::string ser = os.str();
1174 lua_pushlstring(L, ser.c_str(), ser.length());
1179 void ModApiMapgen::Initialize(lua_State *L, int top)
1181 API_FCT(get_mapgen_object);
1183 API_FCT(get_mapgen_params);
1184 API_FCT(set_mapgen_params);
1185 API_FCT(set_noiseparams);
1186 API_FCT(set_gen_notify);
1188 API_FCT(register_biome);
1189 API_FCT(register_decoration);
1190 API_FCT(register_ore);
1191 API_FCT(register_schematic);
1193 API_FCT(clear_registered_biomes);
1194 API_FCT(clear_registered_decorations);
1195 API_FCT(clear_registered_ores);
1196 API_FCT(clear_registered_schematics);
1198 API_FCT(generate_ores);
1199 API_FCT(generate_decorations);
1200 API_FCT(create_schematic);
1201 API_FCT(place_schematic);
1202 API_FCT(serialize_schematic);