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"},
88 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
90 Biome *get_or_load_biome(lua_State *L, int index,
91 BiomeManager *biomemgr);
92 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
93 size_t get_biome_list(lua_State *L, int index,
94 BiomeManager *biomemgr, std::set<u8> *biome_id_list);
96 Schematic *get_or_load_schematic(lua_State *L, int index,
97 SchematicManager *schemmgr, StringMap *replace_names);
98 Schematic *read_schematic_def(lua_State *L, int index,
99 INodeDefManager *ndef, StringMap *replace_names);
100 Schematic *load_schematic(lua_State *L, int index,
101 SchematicManager *schemmgr, StringMap *replace_names);
103 bool read_deco_simple(lua_State *L, NodeResolveInfo *nri, DecoSimple *deco);
104 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
107 ///////////////////////////////////////////////////////////////////////////////
110 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
113 index = lua_gettop(L) + 1 + index;
115 // If a number, assume this is a handle to an object def
116 if (lua_isnumber(L, index))
117 return objmgr->get(lua_tointeger(L, index));
119 // If a string, assume a name is given instead
120 if (lua_isstring(L, index))
121 return objmgr->getByName(lua_tostring(L, index));
127 Schematic *load_schematic(lua_State *L, int index,
128 SchematicManager *schemmgr, StringMap *replace_names)
131 index = lua_gettop(L) + 1 + index;
135 if (lua_istable(L, index)) {
136 schem = read_schematic_def(L, index, schemmgr->getNodeDef(), replace_names);
141 } else if (lua_isnumber(L, index)) {
143 } else if (lua_isstring(L, index)) {
144 schem = SchematicManager::create(SCHEMATIC_NORMAL);
145 if (!schem->loadSchematicFromFile(lua_tostring(L, index),
146 schemmgr->getNodeDef(), replace_names)) {
158 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
161 index = lua_gettop(L) + 1 + index;
163 Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
167 biome = read_biome_def(L, index, biomemgr->getNodeDef());
171 if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
180 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
182 if (!lua_istable(L, index))
185 BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
186 ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL);
187 Biome *b = BiomeManager::create(biometype);
189 b->name = getstringfield_default(L, index, "name", "");
190 b->depth_top = getintfield_default(L, index, "depth_top", 1);
191 b->depth_filler = getintfield_default(L, index, "depth_filler", 2);
192 b->depth_water_top = getintfield_default(L, index, "depth_water_top", 0);
193 b->y_min = getintfield_default(L, index, "y_min", -31000);
194 b->y_max = getintfield_default(L, index, "y_max", 31000);
195 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.f);
196 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.f);
197 b->flags = 0; //reserved
199 NodeResolveInfo *nri = new NodeResolveInfo(b);
200 std::list<std::string> &nnames = nri->nodenames;
201 nnames.push_back(getstringfield_default(L, index, "node_top", ""));
202 nnames.push_back(getstringfield_default(L, index, "node_filler", ""));
203 nnames.push_back(getstringfield_default(L, index, "node_stone", ""));
204 nnames.push_back(getstringfield_default(L, index, "node_water_top", ""));
205 nnames.push_back(getstringfield_default(L, index, "node_water", ""));
206 nnames.push_back(getstringfield_default(L, index, "node_dust", ""));
207 ndef->pendNodeResolve(nri);
213 size_t get_biome_list(lua_State *L, int index,
214 BiomeManager *biomemgr, std::set<u8> *biome_id_list)
217 index = lua_gettop(L) + 1 + index;
219 if (lua_isnil(L, index))
222 bool is_single = true;
223 if (lua_istable(L, index)) {
224 lua_getfield(L, index, "name");
225 is_single = !lua_isnil(L, -1);
230 Biome *biome = get_or_load_biome(L, index, biomemgr);
232 errorstream << "get_biome_list: failed to get biome" << std::endl;
236 biome_id_list->insert(biome->index);
240 // returns number of failed resolutions
241 size_t fail_count = 0;
244 for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
246 Biome *biome = get_or_load_biome(L, -1, biomemgr);
249 errorstream << "get_biome_list: failed to load biome (index "
250 << count << ")" << std::endl;
254 biome_id_list->insert(biome->index);
261 Schematic *get_or_load_schematic(lua_State *L, int index,
262 SchematicManager *schemmgr, StringMap *replace_names)
265 index = lua_gettop(L) + 1 + index;
267 Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
271 schem = load_schematic(L, index, schemmgr, replace_names);
275 if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
284 Schematic *read_schematic_def(lua_State *L, int index,
285 INodeDefManager *ndef, StringMap *replace_names)
287 if (!lua_istable(L, index))
290 //// Get schematic size
291 lua_getfield(L, index, "size");
292 v3s16 size = read_v3s16(L, -1);
295 //// Get schematic data
296 lua_getfield(L, index, "data");
297 luaL_checktype(L, -1, LUA_TTABLE);
299 int numnodes = size.X * size.Y * size.Z;
300 MapNode *schemdata = new MapNode[numnodes];
304 while (lua_next(L, -2)) {
311 // same as readnode, except param1 default is MTSCHEM_PROB_CONST
312 lua_getfield(L, -1, "name");
313 std::string name = luaL_checkstring(L, -1);
317 lua_getfield(L, -1, "param1");
318 param1 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : MTSCHEM_PROB_ALWAYS;
322 lua_getfield(L, -1, "param2");
323 param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0;
326 StringMap::iterator it = replace_names->find(name);
327 if (it != replace_names->end())
330 schemdata[i] = MapNode(ndef, name, param1, param2);
337 errorstream << "read_schematic: incorrect number of "
338 "nodes provided in raw schematic data (got " << i <<
339 ", expected " << numnodes << ")." << std::endl;
344 //// Get Y-slice probability values (if present)
345 u8 *slice_probs = new u8[size.Y];
346 for (i = 0; i != size.Y; i++)
347 slice_probs[i] = MTSCHEM_PROB_ALWAYS;
349 lua_getfield(L, index, "yslice_prob");
350 if (lua_istable(L, -1)) {
352 while (lua_next(L, -2)) {
353 if (getintfield(L, -1, "ypos", i) && i >= 0 && i < size.Y) {
354 slice_probs[i] = getintfield_default(L, -1,
355 "prob", MTSCHEM_PROB_ALWAYS);
361 Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
363 // Here, we read the nodes directly from the INodeDefManager - there is no
364 // need for pending node resolutions so we'll mark this schematic as updated
365 schem->flags = SCHEM_CIDS_UPDATED;
368 schem->schemdata = schemdata;
369 schem->slice_probs = slice_probs;
374 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
377 index = lua_gettop(L) + 1 + index;
380 while (lua_next(L, index)) {
381 std::string replace_from;
382 std::string replace_to;
384 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
385 lua_rawgeti(L, -1, 1);
386 replace_from = lua_tostring(L, -1);
389 lua_rawgeti(L, -1, 2);
390 replace_to = lua_tostring(L, -1);
392 } else { // New {x = "y", ...} format
393 replace_from = lua_tostring(L, -2);
394 replace_to = lua_tostring(L, -1);
397 replace_names->insert(std::make_pair(replace_from, replace_to));
403 ///////////////////////////////////////////////////////////////////////////////
406 // get_mapgen_object(objectname)
407 // returns the requested object used during map generation
408 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
410 const char *mgobjstr = lua_tostring(L, 1);
413 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
416 enum MapgenObject mgobj = (MapgenObject)mgobjint;
418 EmergeManager *emerge = getServer(L)->getEmergeManager();
419 Mapgen *mg = emerge->getCurrentMapgen();
423 size_t maplen = mg->csize.X * mg->csize.Z;
427 MMVManip *vm = mg->vm;
430 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
431 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
432 luaL_getmetatable(L, "VoxelManip");
433 lua_setmetatable(L, -2);
436 push_v3s16(L, vm->m_area.MinEdge);
439 push_v3s16(L, vm->m_area.MaxEdge);
443 case MGOBJ_HEIGHTMAP: {
448 for (size_t i = 0; i != maplen; i++) {
449 lua_pushinteger(L, mg->heightmap[i]);
450 lua_rawseti(L, -2, i + 1);
455 case MGOBJ_BIOMEMAP: {
460 for (size_t i = 0; i != maplen; i++) {
461 lua_pushinteger(L, mg->biomemap[i]);
462 lua_rawseti(L, -2, i + 1);
467 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
469 if (strcmp(emerge->params.mg_name.c_str(), "v7"))
472 MapgenV7 *mgv7 = (MapgenV7 *)mg;
474 float *arr = (mgobj == MGOBJ_HEATMAP) ?
475 mgv7->noise_heat->result : mgv7->noise_humidity->result;
480 for (size_t i = 0; i != maplen; i++) {
481 lua_pushnumber(L, arr[i]);
482 lua_rawseti(L, -2, i + 1);
487 case MGOBJ_GENNOTIFY: {
488 std::map<std::string, std::vector<v3s16> >event_map;
489 std::map<std::string, std::vector<v3s16> >::iterator it;
491 mg->gennotify.getEvents(event_map);
494 for (it = event_map.begin(); it != event_map.end(); ++it) {
497 for (size_t j = 0; j != it->second.size(); j++) {
498 push_v3s16(L, it->second[j]);
499 lua_rawseti(L, -2, j + 1);
502 lua_setfield(L, -2, it->first.c_str());
513 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
515 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
519 lua_pushstring(L, params->mg_name.c_str());
520 lua_setfield(L, -2, "mgname");
522 lua_pushinteger(L, params->seed);
523 lua_setfield(L, -2, "seed");
525 lua_pushinteger(L, params->water_level);
526 lua_setfield(L, -2, "water_level");
528 lua_pushinteger(L, params->chunksize);
529 lua_setfield(L, -2, "chunksize");
531 std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, (u32)-1);
532 lua_pushstring(L, flagstr.c_str());
533 lua_setfield(L, -2, "flags");
539 // set_mapgen_params(params)
540 // set mapgen parameters
541 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
543 if (!lua_istable(L, 1))
546 MapgenParams *params = &getServer(L)->getEmergeManager()->params;
547 u32 flags = 0, flagmask = 0;
549 lua_getfield(L, 1, "mgname");
550 if (lua_isstring(L, -1)) {
551 params->mg_name = lua_tostring(L, -1);
552 delete params->sparams;
553 params->sparams = NULL;
556 lua_getfield(L, 1, "seed");
557 if (lua_isnumber(L, -1))
558 params->seed = lua_tointeger(L, -1);
560 lua_getfield(L, 1, "water_level");
561 if (lua_isnumber(L, -1))
562 params->water_level = lua_tointeger(L, -1);
564 warn_if_field_exists(L, 1, "flagmask",
565 "Deprecated: flags field now includes unset flags.");
566 lua_getfield(L, 1, "flagmask");
567 if (lua_isstring(L, -1))
568 params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL);
570 if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
571 params->flags &= ~flagmask;
572 params->flags |= flags;
579 // set_noiseparams(name, noiseparams, set_default)
580 // set global config values for noise parameters
581 int ModApiMapgen::l_set_noiseparams(lua_State *L)
583 const char *name = luaL_checkstring(L, 1);
586 if (!read_noiseparams(L, 2, &np))
589 bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
591 g_settings->setNoiseParams(name, np, set_default);
597 // set_gen_notify(flags, {deco_id_table})
598 int ModApiMapgen::l_set_gen_notify(lua_State *L)
600 u32 flags = 0, flagmask = 0;
601 EmergeManager *emerge = getServer(L)->getEmergeManager();
603 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
604 emerge->gen_notify_on &= ~flagmask;
605 emerge->gen_notify_on |= flags;
608 if (lua_istable(L, 2)) {
610 while (lua_next(L, 2)) {
611 if (lua_isnumber(L, -1))
612 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
621 // register_biome({lots of stuff})
622 int ModApiMapgen::l_register_biome(lua_State *L)
625 luaL_checktype(L, index, LUA_TTABLE);
627 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
628 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
630 Biome *biome = read_biome_def(L, index, ndef);
634 ObjDefHandle handle = bmgr->add(biome);
635 if (handle == OBJDEF_INVALID_HANDLE) {
640 lua_pushinteger(L, handle);
645 // register_decoration({lots of stuff})
646 int ModApiMapgen::l_register_decoration(lua_State *L)
649 luaL_checktype(L, index, LUA_TTABLE);
651 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
652 DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
653 BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr;
654 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
656 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
657 "deco_type", es_DecorationType, -1);
659 Decoration *deco = decomgr->create(decotype);
661 errorstream << "register_decoration: decoration placement type "
662 << decotype << " not implemented" << std::endl;
666 deco->name = getstringfield_default(L, index, "name", "");
667 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
668 deco->y_min = getintfield_default(L, index, "y_min", -31000);
669 deco->y_max = getintfield_default(L, index, "y_max", 31000);
670 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
671 if (deco->sidelen <= 0) {
672 errorstream << "register_decoration: sidelen must be "
673 "greater than 0" << std::endl;
678 NodeResolveInfo *nri = new NodeResolveInfo(deco);
680 //// Get node name(s) to place decoration on
681 std::vector<const char *> place_on_names;
682 getstringlistfield(L, index, "place_on", place_on_names);
683 nri->nodelistinfo.push_back(NodeListInfo(place_on_names.size()));
684 for (size_t i = 0; i != place_on_names.size(); i++)
685 nri->nodenames.push_back(place_on_names[i]);
687 getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
689 //// Get NoiseParams to define how decoration is placed
690 lua_getfield(L, index, "noise_params");
691 if (read_noiseparams(L, -1, &deco->np))
692 deco->flags |= DECO_USE_NOISE;
695 //// Get biomes associated with this decoration (if any)
696 lua_getfield(L, index, "biomes");
697 if (get_biome_list(L, -1, biomemgr, &deco->biomes))
698 errorstream << "register_decoration: couldn't get all biomes " << std::endl;
701 //// Handle decoration type-specific parameters
702 bool success = false;
705 success = read_deco_simple(L, nri, (DecoSimple *)deco);
708 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
714 ndef->pendNodeResolve(nri);
721 ObjDefHandle handle = decomgr->add(deco);
722 if (handle == OBJDEF_INVALID_HANDLE) {
727 lua_pushinteger(L, handle);
732 bool read_deco_simple(lua_State *L, NodeResolveInfo *nri, DecoSimple *deco)
736 deco->deco_height = getintfield_default(L, index, "height", 1);
737 deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
738 deco->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
740 if (deco->deco_height <= 0) {
741 errorstream << "register_decoration: simple decoration height"
742 " must be greater than 0" << std::endl;
746 std::vector<const char *> deco_names;
747 getstringlistfield(L, index, "decoration", deco_names);
748 if (deco_names.size() == 0) {
749 errorstream << "register_decoration: no decoration nodes "
750 "defined" << std::endl;
753 nri->nodelistinfo.push_back(NodeListInfo(deco_names.size()));
754 for (size_t i = 0; i != deco_names.size(); i++)
755 nri->nodenames.push_back(deco_names[i]);
757 std::vector<const char *> spawnby_names;
758 getstringlistfield(L, index, "spawn_by", spawnby_names);
759 if (deco->nspawnby != -1 && spawnby_names.size() == 0) {
760 errorstream << "register_decoration: no spawn_by nodes defined,"
761 " but num_spawn_by specified" << std::endl;
764 nri->nodelistinfo.push_back(NodeListInfo(spawnby_names.size()));
765 for (size_t i = 0; i != spawnby_names.size(); i++)
766 nri->nodenames.push_back(spawnby_names[i]);
772 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
776 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
777 ModApiMapgen::es_Rotation, ROTATE_0);
779 StringMap replace_names;
780 lua_getfield(L, index, "replacements");
781 if (lua_istable(L, -1))
782 read_schematic_replacements(L, -1, &replace_names);
785 lua_getfield(L, index, "schematic");
786 Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
789 deco->schematic = schem;
790 return schem != NULL;
794 // register_ore({lots of stuff})
795 int ModApiMapgen::l_register_ore(lua_State *L)
798 luaL_checktype(L, index, LUA_TTABLE);
800 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
801 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
803 enum OreType oretype = (OreType)getenumfield(L, index,
804 "ore_type", es_OreType, ORE_SCATTER);
805 Ore *ore = oremgr->create(oretype);
807 errorstream << "register_ore: ore_type " << oretype << " not implemented";
811 ore->name = getstringfield_default(L, index, "name", "");
812 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
813 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
814 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
815 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
816 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0);
820 warn_if_field_exists(L, index, "height_min",
821 "Deprecated: new name is \"y_min\".");
822 warn_if_field_exists(L, index, "height_max",
823 "Deprecated: new name is \"y_max\".");
826 if (!getintfield(L, index, "y_min", ymin) &&
827 !getintfield(L, index, "height_min", ymin))
829 if (!getintfield(L, index, "y_max", ymax) &&
830 !getintfield(L, index, "height_max", ymax))
835 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
836 errorstream << "register_ore: clust_scarcity and clust_num_ores"
837 "must be greater than 0" << std::endl;
842 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
844 lua_getfield(L, index, "noise_params");
845 if (read_noiseparams(L, -1, &ore->np)) {
846 ore->flags |= OREFLAG_USE_NOISE;
847 } else if (ore->NEEDS_NOISE) {
848 errorstream << "register_ore: specified ore type requires valid "
849 "noise parameters" << std::endl;
855 if (oretype == ORE_VEIN) {
856 OreVein *orevein = (OreVein *)ore;
857 orevein->random_factor = getfloatfield_default(L, index,
858 "random_factor", 1.f);
861 ObjDefHandle handle = oremgr->add(ore);
862 if (handle == OBJDEF_INVALID_HANDLE) {
867 NodeResolveInfo *nri = new NodeResolveInfo(ore);
868 nri->nodenames.push_back(getstringfield_default(L, index, "ore", ""));
870 std::vector<const char *> wherein_names;
871 getstringlistfield(L, index, "wherein", wherein_names);
872 nri->nodelistinfo.push_back(NodeListInfo(wherein_names.size()));
873 for (size_t i = 0; i != wherein_names.size(); i++)
874 nri->nodenames.push_back(wherein_names[i]);
876 ndef->pendNodeResolve(nri);
878 lua_pushinteger(L, handle);
883 // register_schematic({schematic}, replacements={})
884 int ModApiMapgen::l_register_schematic(lua_State *L)
886 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
888 StringMap replace_names;
889 if (lua_istable(L, 2))
890 read_schematic_replacements(L, 2, &replace_names);
892 Schematic *schem = load_schematic(L, 1, schemmgr, &replace_names);
896 ObjDefHandle handle = schemmgr->add(schem);
897 if (handle == OBJDEF_INVALID_HANDLE) {
902 lua_pushinteger(L, handle);
907 // clear_registered_biomes()
908 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
910 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
916 // clear_registered_decorations()
917 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
919 DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
925 // clear_registered_ores()
926 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
928 OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
934 // clear_registered_schematics()
935 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
937 SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
943 // generate_ores(vm, p1, p2, [ore_id])
944 int ModApiMapgen::l_generate_ores(lua_State *L)
946 EmergeManager *emerge = getServer(L)->getEmergeManager();
949 mg.seed = emerge->params.seed;
950 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
951 mg.ndef = getServer(L)->getNodeDefManager();
953 v3s16 pmin = lua_istable(L, 2) ? read_v3s16(L, 2) :
954 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
955 v3s16 pmax = lua_istable(L, 3) ? read_v3s16(L, 3) :
956 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
957 sortBoxVerticies(pmin, pmax);
959 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
961 emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
967 // generate_decorations(vm, p1, p2, [deco_id])
968 int ModApiMapgen::l_generate_decorations(lua_State *L)
970 EmergeManager *emerge = getServer(L)->getEmergeManager();
973 mg.seed = emerge->params.seed;
974 mg.vm = LuaVoxelManip::checkobject(L, 1)->vm;
975 mg.ndef = getServer(L)->getNodeDefManager();
977 v3s16 pmin = lua_istable(L, 2) ? read_v3s16(L, 2) :
978 mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
979 v3s16 pmax = lua_istable(L, 3) ? read_v3s16(L, 3) :
980 mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
981 sortBoxVerticies(pmin, pmax);
983 u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
985 emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
991 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
992 int ModApiMapgen::l_create_schematic(lua_State *L)
996 Map *map = &(getEnv(L)->getMap());
997 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
999 v3s16 p1 = read_v3s16(L, 1);
1000 v3s16 p2 = read_v3s16(L, 2);
1001 sortBoxVerticies(p1, p2);
1003 std::vector<std::pair<v3s16, u8> > prob_list;
1004 if (lua_istable(L, 3)) {
1006 while (lua_next(L, 3)) {
1007 if (lua_istable(L, -1)) {
1008 lua_getfield(L, -1, "pos");
1009 v3s16 pos = read_v3s16(L, -1);
1012 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1013 prob_list.push_back(std::make_pair(pos, prob));
1020 std::vector<std::pair<s16, u8> > slice_prob_list;
1021 if (lua_istable(L, 5)) {
1023 while (lua_next(L, 5)) {
1024 if (lua_istable(L, -1)) {
1025 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1026 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1027 slice_prob_list.push_back(std::make_pair(ypos, prob));
1034 const char *filename = luaL_checkstring(L, 4);
1036 if (!schem.getSchematicFromMap(map, p1, p2)) {
1037 errorstream << "create_schematic: failed to get schematic "
1038 "from map" << std::endl;
1042 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1044 schem.saveSchematicToFile(filename, ndef);
1045 actionstream << "create_schematic: saved schematic file '"
1046 << filename << "'." << std::endl;
1048 lua_pushboolean(L, true);
1053 // place_schematic(p, schematic, rotation, replacement)
1054 int ModApiMapgen::l_place_schematic(lua_State *L)
1056 Map *map = &(getEnv(L)->getMap());
1057 SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1060 v3s16 p = read_v3s16(L, 1);
1064 if (lua_isstring(L, 3))
1065 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
1067 //// Read force placement
1068 bool force_placement = true;
1069 if (lua_isboolean(L, 5))
1070 force_placement = lua_toboolean(L, 5);
1072 //// Read node replacements
1073 StringMap replace_names;
1074 if (lua_istable(L, 4))
1075 read_schematic_replacements(L, 4, &replace_names);
1078 Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1080 errorstream << "place_schematic: failed to get schematic" << std::endl;
1084 schem->placeStructure(map, p, 0, (Rotation)rot, force_placement,
1085 schemmgr->getNodeDef());
1087 lua_pushboolean(L, true);
1092 void ModApiMapgen::Initialize(lua_State *L, int top)
1094 API_FCT(get_mapgen_object);
1096 API_FCT(get_mapgen_params);
1097 API_FCT(set_mapgen_params);
1098 API_FCT(set_noiseparams);
1099 API_FCT(set_gen_notify);
1101 API_FCT(register_biome);
1102 API_FCT(register_decoration);
1103 API_FCT(register_ore);
1104 API_FCT(register_schematic);
1106 API_FCT(clear_registered_biomes);
1107 API_FCT(clear_registered_decorations);
1108 API_FCT(clear_registered_ores);
1109 API_FCT(clear_registered_schematics);
1111 API_FCT(generate_ores);
1112 API_FCT(generate_decorations);
1113 API_FCT(create_schematic);
1114 API_FCT(place_schematic);