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"
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"},
89 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
91 Biome *get_or_load_biome(lua_State *L, int index,
92 BiomeManager *biomemgr);
93 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
94 size_t get_biome_list(lua_State *L, int index,
95 BiomeManager *biomemgr, std::set<u8> *biome_id_list);
97 Schematic *get_or_load_schematic(lua_State *L, int index,
98 SchematicManager *schemmgr, StringMap *replace_names);
99 Schematic *read_schematic_def(lua_State *L, int index,
100 INodeDefManager *ndef, StringMap *replace_names);
101 Schematic *load_schematic(lua_State *L, int index,
102 SchematicManager *schemmgr, StringMap *replace_names);
104 bool read_deco_simple(lua_State *L, NodeResolveInfo *nri, DecoSimple *deco);
105 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
108 ///////////////////////////////////////////////////////////////////////////////
111 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
114 index = lua_gettop(L) + 1 + index;
116 // If a number, assume this is a handle to an object def
117 if (lua_isnumber(L, index))
118 return objmgr->get(lua_tointeger(L, index));
120 // If a string, assume a name is given instead
121 if (lua_isstring(L, index))
122 return objmgr->getByName(lua_tostring(L, index));
128 Schematic *load_schematic(lua_State *L, int index,
129 SchematicManager *schemmgr, StringMap *replace_names)
132 index = lua_gettop(L) + 1 + index;
136 if (lua_istable(L, index)) {
137 schem = read_schematic_def(L, index, schemmgr->getNodeDef(), replace_names);
142 } else if (lua_isnumber(L, index)) {
144 } else if (lua_isstring(L, index)) {
145 schem = SchematicManager::create(SCHEMATIC_NORMAL);
147 std::string filepath = lua_tostring(L, index);
148 if (!fs::IsPathAbsolute(filepath))
149 filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
151 if (!schem->loadSchematicFromFile(filepath.c_str(),
152 schemmgr->getNodeDef(), replace_names)) {
164 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
167 index = lua_gettop(L) + 1 + index;
169 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
173 biome = read_biome_def(L, index, biomemgr->getNodeDef());
177 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
186 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
188 if (!lua_istable(L, index))
191 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
192 ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL);
193 Biome *b = BiomeManager::create(biometype);
195 b->name = getstringfield_default(L, index, "name", "");
196 b->depth_top = getintfield_default(L, index, "depth_top", 1);
197 b->depth_filler = getintfield_default(L, index, "depth_filler", 2);
198 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
199 b->y_min = getintfield_default(L, index, "y_min", -31000);
200 b->y_max = getintfield_default(L, index, "y_max", 31000);
201 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
202 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
203 b->flags = 0; //reserved
205 NodeResolveInfo *nri = new NodeResolveInfo(b);
206 std::list<std::string> &nnames = nri->nodenames;
207 nnames.push_back(getstringfield_default(L, index, "node_top", ""));
208 nnames.push_back(getstringfield_default(L, index, "node_filler", ""));
209 nnames.push_back(getstringfield_default(L, index, "node_stone", ""));
210 nnames.push_back(getstringfield_default(L, index, "node_water_top", ""));
211 nnames.push_back(getstringfield_default(L, index, "node_water", ""));
212 nnames.push_back(getstringfield_default(L, index, "node_dust", ""));
213 ndef->pendNodeResolve(nri);
219 size_t get_biome_list(lua_State *L, int index,
220 BiomeManager *biomemgr, std::set<u8> *biome_id_list)
223 index = lua_gettop(L) + 1 + index;
225 if (lua_isnil(L, index))
228 bool is_single = true;
229 if (lua_istable(L, index)) {
230 lua_getfield(L, index, "name");
231 is_single = !lua_isnil(L, -1);
236 Biome *biome = get_or_load_biome(L, index, biomemgr);
238 errorstream << "get_biome_list: failed to get biome" << std::endl;
242 biome_id_list->insert(biome->index);
246 // returns number of failed resolutions
247 size_t fail_count = 0;
250 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
252 Biome *biome = get_or_load_biome(L, -1, biomemgr);
255 errorstream << "get_biome_list: failed to load biome (index "
256 << count << ")" << std::endl;
260 biome_id_list->insert(biome->index);
267 Schematic *get_or_load_schematic(lua_State *L, int index,
268 SchematicManager *schemmgr, StringMap *replace_names)
271 index = lua_gettop(L) + 1 + index;
273 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
277 schem = load_schematic(L, index, schemmgr, replace_names);
281 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
290 Schematic *read_schematic_def(lua_State *L, int index,
291 INodeDefManager *ndef, StringMap *replace_names)
293 if (!lua_istable(L, index))
296 //// Get schematic size
297 lua_getfield(L, index, "size");
298 v3s16 size = read_v3s16(L, -1);
301 //// Get schematic data
302 lua_getfield(L, index, "data");
303 luaL_checktype(L, -1, LUA_TTABLE);
305 int numnodes = size.X * size.Y * size.Z;
306 MapNode *schemdata = new MapNode[numnodes];
310 while (lua_next(L, -2)) {
317 // same as readnode, except param1 default is MTSCHEM_PROB_CONST
318 lua_getfield(L, -1, "name");
319 std::string name = luaL_checkstring(L, -1);
323 lua_getfield(L, -1, "param1");
324 param1 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : MTSCHEM_PROB_ALWAYS;
328 lua_getfield(L, -1, "param2");
329 param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0;
332 StringMap::iterator it = replace_names->find(name);
333 if (it != replace_names->end())
336 schemdata[i] = MapNode(ndef, name, param1, param2);
343 errorstream << "read_schematic: incorrect number of "
344 "nodes provided in raw schematic data (got " << i <<
345 ", expected " << numnodes << ")." << std::endl;
350 //// Get Y-slice probability values (if present)
351 u8 *slice_probs = new u8[size.Y];
352 for (i = 0; i != size.Y; i++)
353 slice_probs[i] = MTSCHEM_PROB_ALWAYS;
355 lua_getfield(L, index, "yslice_prob");
356 if (lua_istable(L, -1)) {
358 while (lua_next(L, -2)) {
359 if (getintfield(L, -1, "ypos", i) && i >= 0 && i < size.Y) {
360 slice_probs[i] = getintfield_default(L, -1,
361 "prob", MTSCHEM_PROB_ALWAYS);
367 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
369 // Here, we read the nodes directly from the INodeDefManager - there is no
370 // need for pending node resolutions so we'll mark this schematic as updated
371 schem->flags = SCHEM_CIDS_UPDATED;
374 schem->schemdata = schemdata;
375 schem->slice_probs = slice_probs;
380 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
383 index = lua_gettop(L) + 1 + index;
386 while (lua_next(L, index)) {
387 std::string replace_from;
388 std::string replace_to;
390 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
391 lua_rawgeti(L, -1, 1);
392 replace_from = lua_tostring(L, -1);
395 lua_rawgeti(L, -1, 2);
396 replace_to = lua_tostring(L, -1);
398 } else { // New {x = "y", ...} format
399 replace_from = lua_tostring(L, -2);
400 replace_to = lua_tostring(L, -1);
403 replace_names->insert(std::make_pair(replace_from, replace_to));
409 ///////////////////////////////////////////////////////////////////////////////
412 // get_mapgen_object(objectname)
413 // returns the requested object used during map generation
414 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
416 const char *mgobjstr = lua_tostring(L, 1);
419 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
422 enum MapgenObject mgobj = (MapgenObject)mgobjint;
424 EmergeManager *emerge = getServer(L)->getEmergeManager();
425 Mapgen *mg = emerge->getCurrentMapgen();
429 size_t maplen = mg->csize.X * mg->csize.Z;
433 MMVManip *vm = mg->vm;
436 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
437 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
438 luaL_getmetatable(L, "VoxelManip");
439 lua_setmetatable(L, -2);
442 push_v3s16(L, vm->m_area.MinEdge);
445 push_v3s16(L, vm->m_area.MaxEdge);
449 case MGOBJ_HEIGHTMAP: {
454 for (size_t i = 0; i != maplen; i++) {
455 lua_pushinteger(L, mg->heightmap[i]);
456 lua_rawseti(L, -2, i + 1);
461 case MGOBJ_BIOMEMAP: {
466 for (size_t i = 0; i != maplen; i++) {
467 lua_pushinteger(L, mg->biomemap[i]);
468 lua_rawseti(L, -2, i + 1);
473 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
475 if (strcmp(emerge->params.mg_name.c_str(), "v7"))
478 MapgenV7 *mgv7 = (MapgenV7 *)mg;
480 float *arr = (mgobj == MGOBJ_HEATMAP) ?
481 mgv7->noise_heat->result : mgv7->noise_humidity->result;
486 for (size_t i = 0; i != maplen; i++) {
487 lua_pushnumber(L, arr[i]);
488 lua_rawseti(L, -2, i + 1);
493 case MGOBJ_GENNOTIFY: {
494 std::map<std::string, std::vector<v3s16> >event_map;
495 std::map<std::string, std::vector<v3s16> >::iterator it;
497 mg->gennotify.getEvents(event_map);
500 for (it = event_map.begin(); it != event_map.end(); ++it) {
503 for (size_t j = 0; j != it->second.size(); j++) {
504 push_v3s16(L, it->second[j]);
505 lua_rawseti(L, -2, j + 1);
508 lua_setfield(L, -2, it->first.c_str());
519 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
521 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
525 lua_pushstring(L, params->mg_name.c_str());
526 lua_setfield(L, -2, "mgname");
528 lua_pushinteger(L, params->seed);
529 lua_setfield(L, -2, "seed");
531 lua_pushinteger(L, params->water_level);
532 lua_setfield(L, -2, "water_level");
534 lua_pushinteger(L, params->chunksize);
535 lua_setfield(L, -2, "chunksize");
537 std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, (u32)-1);
538 lua_pushstring(L, flagstr.c_str());
539 lua_setfield(L, -2, "flags");
545 // set_mapgen_params(params)
546 // set mapgen parameters
547 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
549 if (!lua_istable(L, 1))
552 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
553 u32 flags = 0, flagmask = 0;
555 lua_getfield(L, 1, "mgname");
556 if (lua_isstring(L, -1)) {
557 params->mg_name = lua_tostring(L, -1);
558 delete params->sparams;
559 params->sparams = NULL;
562 lua_getfield(L, 1, "seed");
563 if (lua_isnumber(L, -1))
564 params->seed = lua_tointeger(L, -1);
566 lua_getfield(L, 1, "water_level");
567 if (lua_isnumber(L, -1))
568 params->water_level = lua_tointeger(L, -1);
570 warn_if_field_exists(L, 1, "flagmask",
571 "Deprecated: flags field now includes unset flags.");
572 lua_getfield(L, 1, "flagmask");
573 if (lua_isstring(L, -1))
574 params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL);
576 if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
577 params->flags &= ~flagmask;
578 params->flags |= flags;
585 // set_noiseparams(name, noiseparams, set_default)
586 // set global config values for noise parameters
587 int ModApiMapgen::l_set_noiseparams(lua_State *L)
589 const char *name = luaL_checkstring(L, 1);
592 if (!read_noiseparams(L, 2, &np))
595 bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
597 g_settings->setNoiseParams(name, np, set_default);
603 // set_gen_notify(flags, {deco_id_table})
604 int ModApiMapgen::l_set_gen_notify(lua_State *L)
606 u32 flags = 0, flagmask = 0;
607 EmergeManager *emerge = getServer(L)->getEmergeManager();
609 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
610 emerge->gen_notify_on &= ~flagmask;
611 emerge->gen_notify_on |= flags;
614 if (lua_istable(L, 2)) {
616 while (lua_next(L, 2)) {
617 if (lua_isnumber(L, -1))
618 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
627 // register_biome({lots of stuff})
628 int ModApiMapgen::l_register_biome(lua_State *L)
631 luaL_checktype(L, index, LUA_TTABLE);
633 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
634 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
636 Biome *biome = read_biome_def(L, index, ndef);
640 ObjDefHandle handle = bmgr->add(biome);
641 if (handle == OBJDEF_INVALID_HANDLE) {
646 lua_pushinteger(L, handle);
651 // register_decoration({lots of stuff})
652 int ModApiMapgen::l_register_decoration(lua_State *L)
655 luaL_checktype(L, index, LUA_TTABLE);
657 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
658 DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
659 BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr;
660 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
662 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
663 "deco_type", es_DecorationType, -1);
665 Decoration *deco = decomgr->create(decotype);
667 errorstream << "register_decoration: decoration placement type "
668 << decotype << " not implemented" << std::endl;
672 deco->name = getstringfield_default(L, index, "name", "");
673 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
674 deco->y_min = getintfield_default(L, index, "y_min", -31000);
675 deco->y_max = getintfield_default(L, index, "y_max", 31000);
676 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
677 if (deco->sidelen <= 0) {
678 errorstream << "register_decoration: sidelen must be "
679 "greater than 0" << std::endl;
684 NodeResolveInfo *nri = new NodeResolveInfo(deco);
686 //// Get node name(s) to place decoration on
687 std::vector<const char *> place_on_names;
688 getstringlistfield(L, index, "place_on", place_on_names);
689 nri->nodelistinfo.push_back(NodeListInfo(place_on_names.size()));
690 for (size_t i = 0; i != place_on_names.size(); i++)
691 nri->nodenames.push_back(place_on_names[i]);
693 getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
695 //// Get NoiseParams to define how decoration is placed
696 lua_getfield(L, index, "noise_params");
697 if (read_noiseparams(L, -1, &deco->np))
698 deco->flags |= DECO_USE_NOISE;
701 //// Get biomes associated with this decoration (if any)
702 lua_getfield(L, index, "biomes");
703 if (get_biome_list(L, -1, biomemgr, &deco->biomes))
704 errorstream << "register_decoration: couldn't get all biomes " << std::endl;
707 //// Handle decoration type-specific parameters
708 bool success = false;
711 success = read_deco_simple(L, nri, (DecoSimple *)deco);
714 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
720 ndef->pendNodeResolve(nri);
727 ObjDefHandle handle = decomgr->add(deco);
728 if (handle == OBJDEF_INVALID_HANDLE) {
733 lua_pushinteger(L, handle);
738 bool read_deco_simple(lua_State *L, NodeResolveInfo *nri, DecoSimple *deco)
742 deco->deco_height = getintfield_default(L, index, "height", 1);
743 deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
744 deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
746 if (deco->deco_height <= 0) {
747 errorstream << "register_decoration: simple decoration height"
748 " must be greater than 0" << std::endl;
752 std::vector<const char *> deco_names;
753 getstringlistfield(L, index, "decoration", deco_names);
754 if (deco_names.size() == 0) {
755 errorstream << "register_decoration: no decoration nodes "
756 "defined" << std::endl;
759 nri->nodelistinfo.push_back(NodeListInfo(deco_names.size()));
760 for (size_t i = 0; i != deco_names.size(); i++)
761 nri->nodenames.push_back(deco_names[i]);
763 std::vector<const char *> spawnby_names;
764 getstringlistfield(L, index, "spawn_by", spawnby_names);
765 if (deco->nspawnby != -1 && spawnby_names.size() == 0) {
766 errorstream << "register_decoration: no spawn_by nodes defined,"
767 " but num_spawn_by specified" << std::endl;
770 nri->nodelistinfo.push_back(NodeListInfo(spawnby_names.size()));
771 for (size_t i = 0; i != spawnby_names.size(); i++)
772 nri->nodenames.push_back(spawnby_names[i]);
778 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
782 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
783 ModApiMapgen::es_Rotation, ROTATE_0);
785 StringMap replace_names;
786 lua_getfield(L, index, "replacements");
787 if (lua_istable(L, -1))
788 read_schematic_replacements(L, -1, &replace_names);
791 lua_getfield(L, index, "schematic");
792 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
795 deco->schematic = schem;
796 return schem != NULL;
800 // register_ore({lots of stuff})
801 int ModApiMapgen::l_register_ore(lua_State *L)
804 luaL_checktype(L, index, LUA_TTABLE);
806 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
807 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
809 enum OreType oretype = (OreType)getenumfield(L, index,
810 "ore_type", es_OreType, ORE_SCATTER);
811 Ore *ore = oremgr->create(oretype);
813 errorstream << "register_ore: ore_type " << oretype << " not implemented";
817 ore->name = getstringfield_default(L, index, "name", "");
818 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
819 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
820 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
821 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
822 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0);
826 warn_if_field_exists(L, index, "height_min",
827 "Deprecated: new name is \"y_min\".");
828 warn_if_field_exists(L, index, "height_max",
829 "Deprecated: new name is \"y_max\".");
832 if (!getintfield(L, index, "y_min", ymin) &&
833 !getintfield(L, index, "height_min", ymin))
835 if (!getintfield(L, index, "y_max", ymax) &&
836 !getintfield(L, index, "height_max", ymax))
841 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
842 errorstream << "register_ore: clust_scarcity and clust_num_ores"
843 "must be greater than 0" << std::endl;
848 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
850 lua_getfield(L, index, "noise_params");
851 if (read_noiseparams(L, -1, &ore->np)) {
852 ore->flags |= OREFLAG_USE_NOISE;
853 } else if (ore->NEEDS_NOISE) {
854 errorstream << "register_ore: specified ore type requires valid "
855 "noise parameters" << std::endl;
861 if (oretype == ORE_VEIN) {
862 OreVein *orevein = (OreVein *)ore;
863 orevein->random_factor = getfloatfield_default(L, index,
864 "random_factor", 1.f);
867 ObjDefHandle handle = oremgr->add(ore);
868 if (handle == OBJDEF_INVALID_HANDLE) {
873 NodeResolveInfo *nri = new NodeResolveInfo(ore);
874 nri->nodenames.push_back(getstringfield_default(L, index, "ore", ""));
876 std::vector<const char *> wherein_names;
877 getstringlistfield(L, index, "wherein", wherein_names);
878 nri->nodelistinfo.push_back(NodeListInfo(wherein_names.size()));
879 for (size_t i = 0; i != wherein_names.size(); i++)
880 nri->nodenames.push_back(wherein_names[i]);
882 ndef->pendNodeResolve(nri);
884 lua_pushinteger(L, handle);
889 // register_schematic({schematic}, replacements={})
890 int ModApiMapgen::l_register_schematic(lua_State *L)
892 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
894 StringMap replace_names;
895 if (lua_istable(L, 2))
896 read_schematic_replacements(L, 2, &replace_names);
898 Schematic *schem = load_schematic(L, 1, schemmgr, &replace_names);
902 ObjDefHandle handle = schemmgr->add(schem);
903 if (handle == OBJDEF_INVALID_HANDLE) {
908 lua_pushinteger(L, handle);
913 // clear_registered_biomes()
914 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
916 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
922 // clear_registered_decorations()
923 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
925 DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
931 // clear_registered_ores()
932 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
934 OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
940 // clear_registered_schematics()
941 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
943 SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
949 // generate_ores(vm, p1, p2, [ore_id])
950 int ModApiMapgen::l_generate_ores(lua_State *L)
952 EmergeManager *emerge = getServer(L)->getEmergeManager();
955 mg.seed = emerge->params.seed;
956 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
957 mg.ndef = getServer(L)->getNodeDefManager();
959 v3s16 pmin = lua_istable(L, 2) ? read_v3s16(L, 2) :
960 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
961 v3s16 pmax = lua_istable(L, 3) ? read_v3s16(L, 3) :
962 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
963 sortBoxVerticies(pmin, pmax);
965 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
967 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
973 // generate_decorations(vm, p1, p2, [deco_id])
974 int ModApiMapgen::l_generate_decorations(lua_State *L)
976 EmergeManager *emerge = getServer(L)->getEmergeManager();
979 mg.seed = emerge->params.seed;
980 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
981 mg.ndef = getServer(L)->getNodeDefManager();
983 v3s16 pmin = lua_istable(L, 2) ? read_v3s16(L, 2) :
984 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
985 v3s16 pmax = lua_istable(L, 3) ? read_v3s16(L, 3) :
986 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
987 sortBoxVerticies(pmin, pmax);
989 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
991 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
997 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
998 int ModApiMapgen::l_create_schematic(lua_State *L)
1002 Map *map = &(getEnv(L)->getMap());
1003 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1005 v3s16 p1 = read_v3s16(L, 1);
1006 v3s16 p2 = read_v3s16(L, 2);
1007 sortBoxVerticies(p1, p2);
1009 std::vector<std::pair<v3s16, u8> > prob_list;
1010 if (lua_istable(L, 3)) {
1012 while (lua_next(L, 3)) {
1013 if (lua_istable(L, -1)) {
1014 lua_getfield(L, -1, "pos");
1015 v3s16 pos = read_v3s16(L, -1);
1018 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1019 prob_list.push_back(std::make_pair(pos, prob));
1026 std::vector<std::pair<s16, u8> > slice_prob_list;
1027 if (lua_istable(L, 5)) {
1029 while (lua_next(L, 5)) {
1030 if (lua_istable(L, -1)) {
1031 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1032 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1033 slice_prob_list.push_back(std::make_pair(ypos, prob));
1040 const char *filename = luaL_checkstring(L, 4);
1042 if (!schem.getSchematicFromMap(map, p1, p2)) {
1043 errorstream << "create_schematic: failed to get schematic "
1044 "from map" << std::endl;
1048 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1050 schem.saveSchematicToFile(filename, ndef);
1051 actionstream << "create_schematic: saved schematic file '"
1052 << filename << "'." << std::endl;
1054 lua_pushboolean(L, true);
1059 // place_schematic(p, schematic, rotation, replacement)
1060 int ModApiMapgen::l_place_schematic(lua_State *L)
1062 Map *map = &(getEnv(L)->getMap());
1063 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1066 v3s16 p = read_v3s16(L, 1);
1070 if (lua_isstring(L, 3))
1071 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
1073 //// Read force placement
1074 bool force_placement = true;
1075 if (lua_isboolean(L, 5))
1076 force_placement = lua_toboolean(L, 5);
1078 //// Read node replacements
1079 StringMap replace_names;
1080 if (lua_istable(L, 4))
1081 read_schematic_replacements(L, 4, &replace_names);
1084 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1086 errorstream << "place_schematic: failed to get schematic" << std::endl;
1090 schem->placeStructure(map, p, 0, (Rotation)rot, force_placement,
1091 schemmgr->getNodeDef());
1093 lua_pushboolean(L, true);
1098 void ModApiMapgen::Initialize(lua_State *L, int top)
1100 API_FCT(get_mapgen_object);
1102 API_FCT(get_mapgen_params);
1103 API_FCT(set_mapgen_params);
1104 API_FCT(set_noiseparams);
1105 API_FCT(set_gen_notify);
1107 API_FCT(register_biome);
1108 API_FCT(register_decoration);
1109 API_FCT(register_ore);
1110 API_FCT(register_schematic);
1112 API_FCT(clear_registered_biomes);
1113 API_FCT(clear_registered_decorations);
1114 API_FCT(clear_registered_ores);
1115 API_FCT(clear_registered_schematics);
1117 API_FCT(generate_ores);
1118 API_FCT(generate_decorations);
1119 API_FCT(create_schematic);
1120 API_FCT(place_schematic);