Fix threshold type
[oweals/minetest.git] / src / script / lua_api / l_mapgen.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
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"
27 #include "server.h"
28 #include "environment.h"
29 #include "emerge.h"
30 #include "mg_biome.h"
31 #include "mg_ore.h"
32 #include "mg_decoration.h"
33 #include "mg_schematic.h"
34 #include "mapgen_v5.h"
35 #include "mapgen_v7.h"
36 #include "filesys.h"
37 #include "settings.h"
38 #include "log.h"
39
40 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
41 {
42         {BIOME_NORMAL, "normal"},
43         {BIOME_LIQUID, "liquid"},
44         {BIOME_NETHER, "nether"},
45         {BIOME_AETHER, "aether"},
46         {BIOME_FLAT,   "flat"},
47         {0, NULL},
48 };
49
50 struct EnumString ModApiMapgen::es_DecorationType[] =
51 {
52         {DECO_SIMPLE,    "simple"},
53         {DECO_SCHEMATIC, "schematic"},
54         {DECO_LSYSTEM,   "lsystem"},
55         {0, NULL},
56 };
57
58 struct EnumString ModApiMapgen::es_MapgenObject[] =
59 {
60         {MGOBJ_VMANIP,    "voxelmanip"},
61         {MGOBJ_HEIGHTMAP, "heightmap"},
62         {MGOBJ_BIOMEMAP,  "biomemap"},
63         {MGOBJ_HEATMAP,   "heatmap"},
64         {MGOBJ_HUMIDMAP,  "humiditymap"},
65         {MGOBJ_GENNOTIFY, "gennotify"},
66         {0, NULL},
67 };
68
69 struct EnumString ModApiMapgen::es_OreType[] =
70 {
71         {ORE_SCATTER, "scatter"},
72         {ORE_SHEET,   "sheet"},
73         {ORE_PUFF,    "puff"},
74         {ORE_BLOB,    "blob"},
75         {ORE_VEIN,    "vein"},
76         {0, NULL},
77 };
78
79 struct EnumString ModApiMapgen::es_Rotation[] =
80 {
81         {ROTATE_0,    "0"},
82         {ROTATE_90,   "90"},
83         {ROTATE_180,  "180"},
84         {ROTATE_270,  "270"},
85         {ROTATE_RAND, "random"},
86         {0, NULL},
87 };
88
89 struct EnumString ModApiMapgen::es_SchematicFormatType[] =
90 {
91         {SCHEM_FMT_HANDLE, "handle"},
92         {SCHEM_FMT_MTS,    "mts"},
93         {SCHEM_FMT_LUA,    "lua"},
94         {0, NULL},
95 };
96
97 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr);
98
99 Biome *get_or_load_biome(lua_State *L, int index,
100         BiomeManager *biomemgr);
101 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
102 size_t get_biome_list(lua_State *L, int index,
103         BiomeManager *biomemgr, std::set<u8> *biome_id_list);
104
105 Schematic *get_or_load_schematic(lua_State *L, int index,
106         SchematicManager *schemmgr, StringMap *replace_names);
107 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
108         StringMap *replace_names);
109 Schematic *load_schematic_from_def(lua_State *L, int index,
110         INodeDefManager *ndef, StringMap *replace_names);
111 bool read_schematic_def(lua_State *L, int index,
112         Schematic *schem, std::vector<std::string> *names);
113
114 bool read_deco_simple(lua_State *L, DecoSimple *deco);
115 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco);
116
117
118 ///////////////////////////////////////////////////////////////////////////////
119
120 ObjDef *get_objdef(lua_State *L, int index, ObjDefManager *objmgr)
121 {
122         if (index < 0)
123                 index = lua_gettop(L) + 1 + index;
124
125         // If a number, assume this is a handle to an object def
126         if (lua_isnumber(L, index))
127                 return objmgr->get(lua_tointeger(L, index));
128
129         // If a string, assume a name is given instead
130         if (lua_isstring(L, index))
131                 return objmgr->getByName(lua_tostring(L, index));
132
133         return NULL;
134 }
135
136 ///////////////////////////////////////////////////////////////////////////////
137
138 Schematic *get_or_load_schematic(lua_State *L, int index,
139         SchematicManager *schemmgr, StringMap *replace_names)
140 {
141         if (index < 0)
142                 index = lua_gettop(L) + 1 + index;
143
144         Schematic *schem = (Schematic *)get_objdef(L, index, schemmgr);
145         if (schem)
146                 return schem;
147
148         schem = load_schematic(L, index, schemmgr->getNodeDef(),
149                 replace_names);
150         if (!schem)
151                 return NULL;
152
153         if (schemmgr->add(schem) == OBJDEF_INVALID_HANDLE) {
154                 delete schem;
155                 return NULL;
156         }
157
158         return schem;
159 }
160
161
162 Schematic *load_schematic(lua_State *L, int index, INodeDefManager *ndef,
163         StringMap *replace_names)
164 {
165         if (index < 0)
166                 index = lua_gettop(L) + 1 + index;
167
168         Schematic *schem = NULL;
169
170         if (lua_istable(L, index)) {
171                 schem = load_schematic_from_def(L, index, ndef,
172                         replace_names);
173                 if (!schem) {
174                         delete schem;
175                         return NULL;
176                 }
177         } else if (lua_isnumber(L, index)) {
178                 return NULL;
179         } else if (lua_isstring(L, index)) {
180                 schem = SchematicManager::create(SCHEMATIC_NORMAL);
181
182                 std::string filepath = lua_tostring(L, index);
183                 if (!fs::IsPathAbsolute(filepath))
184                         filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath;
185
186                 if (!schem->loadSchematicFromFile(filepath, ndef,
187                                 replace_names)) {
188                         delete schem;
189                         return NULL;
190                 }
191         }
192
193         return schem;
194 }
195
196
197 Schematic *load_schematic_from_def(lua_State *L, int index,
198         INodeDefManager *ndef, StringMap *replace_names)
199 {
200         Schematic *schem = SchematicManager::create(SCHEMATIC_NORMAL);
201
202         if (!read_schematic_def(L, index, schem, &schem->m_nodenames)) {
203                 delete schem;
204                 return NULL;
205         }
206
207         size_t num_nodes = schem->m_nodenames.size();
208
209         schem->m_nnlistsizes.push_back(num_nodes);
210
211         if (replace_names) {
212                 for (size_t i = 0; i != num_nodes; i++) {
213                         StringMap::iterator it = replace_names->find(schem->m_nodenames[i]);
214                         if (it != replace_names->end())
215                                 schem->m_nodenames[i] = it->second;
216                 }
217         }
218
219         if (ndef)
220                 ndef->pendNodeResolve(schem);
221
222         return schem;
223 }
224
225
226 bool read_schematic_def(lua_State *L, int index,
227         Schematic *schem, std::vector<std::string> *names)
228 {
229         if (!lua_istable(L, index))
230                 return false;
231
232         //// Get schematic size
233         lua_getfield(L, index, "size");
234         v3s16 size = check_v3s16(L, -1);
235         lua_pop(L, 1);
236
237         schem->size = size;
238
239         //// Get schematic data
240         lua_getfield(L, index, "data");
241         luaL_checktype(L, -1, LUA_TTABLE);
242
243         u32 numnodes = size.X * size.Y * size.Z;
244         schem->schemdata = new MapNode[numnodes];
245
246         size_t names_base = names->size();
247         std::map<std::string, content_t> name_id_map;
248
249         u32 i = 0;
250         for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) {
251                 if (i >= numnodes)
252                         continue;
253
254                 //// Read name
255                 std::string name;
256                 if (!getstringfield(L, -1, "name", name))
257                         throw LuaError("Schematic data definition with missing name field");
258
259                 //// Read param1/prob
260                 u8 param1;
261                 if (!getintfield(L, -1, "param1", param1) &&
262                         !getintfield(L, -1, "prob", param1))
263                         param1 = MTSCHEM_PROB_ALWAYS_OLD;
264
265                 //// Read param2
266                 u8 param2 = getintfield_default(L, -1, "param2", 0);
267
268                 //// Find or add new nodename-to-ID mapping
269                 std::map<std::string, content_t>::iterator it = name_id_map.find(name);
270                 content_t name_index;
271                 if (it != name_id_map.end()) {
272                         name_index = it->second;
273                 } else {
274                         name_index = names->size() - names_base;
275                         name_id_map[name] = name_index;
276                         names->push_back(name);
277                 }
278
279                 //// Perform probability/force_place fixup on param1
280                 param1 >>= 1;
281                 if (getboolfield_default(L, -1, "force_place", false))
282                         param1 |= MTSCHEM_FORCE_PLACE;
283
284                 //// Actually set the node in the schematic
285                 schem->schemdata[i] = MapNode(name_index, param1, param2);
286         }
287
288         if (i != numnodes) {
289                 errorstream << "read_schematic_def: incorrect number of "
290                         "nodes provided in raw schematic data (got " << i <<
291                         ", expected " << numnodes << ")." << std::endl;
292                 return false;
293         }
294
295         //// Get Y-slice probability values (if present)
296         schem->slice_probs = new u8[size.Y];
297         for (i = 0; i != (u32) size.Y; i++)
298                 schem->slice_probs[i] = MTSCHEM_PROB_ALWAYS;
299
300         lua_getfield(L, index, "yslice_prob");
301         if (lua_istable(L, -1)) {
302                 for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
303                         u16 ypos;
304                         if (!getintfield(L, -1, "ypos", ypos) || (ypos >= size.Y) ||
305                                 !getintfield(L, -1, "prob", schem->slice_probs[ypos]))
306                                 continue;
307
308                         schem->slice_probs[ypos] >>= 1;
309                 }
310         }
311
312         return true;
313 }
314
315
316 void read_schematic_replacements(lua_State *L, int index, StringMap *replace_names)
317 {
318         if (index < 0)
319                 index = lua_gettop(L) + 1 + index;
320
321         lua_pushnil(L);
322         while (lua_next(L, index)) {
323                 std::string replace_from;
324                 std::string replace_to;
325
326                 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
327                         lua_rawgeti(L, -1, 1);
328                         replace_from = lua_tostring(L, -1);
329                         lua_pop(L, 1);
330
331                         lua_rawgeti(L, -1, 2);
332                         replace_to = lua_tostring(L, -1);
333                         lua_pop(L, 1);
334                 } else { // New {x = "y", ...} format
335                         replace_from = lua_tostring(L, -2);
336                         replace_to = lua_tostring(L, -1);
337                 }
338
339                 replace_names->insert(std::make_pair(replace_from, replace_to));
340                 lua_pop(L, 1);
341         }
342 }
343
344 ///////////////////////////////////////////////////////////////////////////////
345
346 Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr)
347 {
348         if (index < 0)
349                 index = lua_gettop(L) + 1 + index;
350
351         Biome *biome = (Biome *)get_objdef(L, index, biomemgr);
352         if (biome)
353                 return biome;
354
355         biome = read_biome_def(L, index, biomemgr->getNodeDef());
356         if (!biome)
357                 return NULL;
358
359         if (biomemgr->add(biome) == OBJDEF_INVALID_HANDLE) {
360                 delete biome;
361                 return NULL;
362         }
363
364         return biome;
365 }
366
367
368 Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
369 {
370         if (!lua_istable(L, index))
371                 return NULL;
372
373         BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
374                 ModApiMapgen::es_BiomeTerrainType, BIOME_NORMAL);
375         Biome *b = BiomeManager::create(biometype);
376
377         b->name            = getstringfield_default(L, index, "name", "");
378         b->depth_top       = getintfield_default(L,    index, "depth_top",       0);
379         b->depth_filler    = getintfield_default(L,    index, "depth_filler",    -31000);
380         b->depth_water_top = getintfield_default(L,    index, "depth_water_top", 0);
381         b->y_min           = getintfield_default(L,    index, "y_min",           -31000);
382         b->y_max           = getintfield_default(L,    index, "y_max",           31000);
383         b->heat_point      = getfloatfield_default(L,  index, "heat_point",      0.f);
384         b->humidity_point  = getfloatfield_default(L,  index, "humidity_point",  0.f);
385         b->flags           = 0; //reserved
386
387         std::vector<std::string> &nn = b->m_nodenames;
388         nn.push_back(getstringfield_default(L, index, "node_top",         ""));
389         nn.push_back(getstringfield_default(L, index, "node_filler",      ""));
390         nn.push_back(getstringfield_default(L, index, "node_stone",       ""));
391         nn.push_back(getstringfield_default(L, index, "node_water_top",   ""));
392         nn.push_back(getstringfield_default(L, index, "node_water",       ""));
393         nn.push_back(getstringfield_default(L, index, "node_river_water", ""));
394         nn.push_back(getstringfield_default(L, index, "node_dust",        ""));
395         ndef->pendNodeResolve(b);
396
397         return b;
398 }
399
400
401 size_t get_biome_list(lua_State *L, int index,
402         BiomeManager *biomemgr, std::set<u8> *biome_id_list)
403 {
404         if (index < 0)
405                 index = lua_gettop(L) + 1 + index;
406
407         if (lua_isnil(L, index))
408                 return 0;
409
410         bool is_single = true;
411         if (lua_istable(L, index)) {
412                 lua_getfield(L, index, "name");
413                 is_single = !lua_isnil(L, -1);
414                 lua_pop(L, 1);
415         }
416
417         if (is_single) {
418                 Biome *biome = get_or_load_biome(L, index, biomemgr);
419                 if (!biome) {
420                         errorstream << "get_biome_list: failed to get biome '"
421                                 << (lua_isstring(L, index) ? lua_tostring(L, index) : "")
422                                 << "'." << std::endl;
423                         return 1;
424                 }
425
426                 biome_id_list->insert(biome->index);
427                 return 0;
428         }
429
430         // returns number of failed resolutions
431         size_t fail_count = 0;
432         size_t count = 0;
433
434         for (lua_pushnil(L); lua_next(L, index); lua_pop(L, 1)) {
435                 count++;
436                 Biome *biome = get_or_load_biome(L, -1, biomemgr);
437                 if (!biome) {
438                         fail_count++;
439                         errorstream << "get_biome_list: failed to get biome '"
440                                 << (lua_isstring(L, -1) ? lua_tostring(L, -1) : "")
441                                 << "'" << std::endl;
442                         continue;
443                 }
444
445                 biome_id_list->insert(biome->index);
446         }
447
448         return fail_count;
449 }
450
451 ///////////////////////////////////////////////////////////////////////////////
452
453 // get_biome_id(biomename)
454 // returns the biome id used in biomemap
455 int ModApiMapgen::l_get_biome_id(lua_State *L)
456 {
457         NO_MAP_LOCK_REQUIRED;
458
459         const char *biome_str = lua_tostring(L, 1);
460         if (!biome_str)
461                 return 0;
462
463         BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
464
465         if (!bmgr)
466                 return 0;
467
468         Biome *biome = (Biome *)bmgr->getByName(biome_str);
469
470         if (!biome || biome->index == OBJDEF_INVALID_INDEX)
471                 return 0;
472
473         lua_pushinteger(L, biome->index);
474
475         return 1;
476 }
477
478
479 // get_mapgen_object(objectname)
480 // returns the requested object used during map generation
481 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
482 {
483         NO_MAP_LOCK_REQUIRED;
484
485         const char *mgobjstr = lua_tostring(L, 1);
486
487         int mgobjint;
488         if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
489                 return 0;
490
491         enum MapgenObject mgobj = (MapgenObject)mgobjint;
492
493         EmergeManager *emerge = getServer(L)->getEmergeManager();
494         Mapgen *mg = emerge->getCurrentMapgen();
495         if (!mg)
496                 throw LuaError("Must only be called in a mapgen thread!");
497
498         size_t maplen = mg->csize.X * mg->csize.Z;
499
500         switch (mgobj) {
501         case MGOBJ_VMANIP: {
502                 MMVManip *vm = mg->vm;
503
504                 // VoxelManip object
505                 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
506                 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
507                 luaL_getmetatable(L, "VoxelManip");
508                 lua_setmetatable(L, -2);
509
510                 // emerged min pos
511                 push_v3s16(L, vm->m_area.MinEdge);
512
513                 // emerged max pos
514                 push_v3s16(L, vm->m_area.MaxEdge);
515
516                 return 3;
517         }
518         case MGOBJ_HEIGHTMAP: {
519                 if (!mg->heightmap)
520                         return 0;
521
522                 lua_newtable(L);
523                 for (size_t i = 0; i != maplen; i++) {
524                         lua_pushinteger(L, mg->heightmap[i]);
525                         lua_rawseti(L, -2, i + 1);
526                 }
527
528                 return 1;
529         }
530         case MGOBJ_BIOMEMAP: {
531                 if (!mg->biomemap)
532                         return 0;
533
534                 lua_newtable(L);
535                 for (size_t i = 0; i != maplen; i++) {
536                         lua_pushinteger(L, mg->biomemap[i]);
537                         lua_rawseti(L, -2, i + 1);
538                 }
539
540                 return 1;
541         }
542         case MGOBJ_HEATMAP: {
543                 if (!mg->heatmap)
544                         return 0;
545
546                 lua_newtable(L);
547                 for (size_t i = 0; i != maplen; i++) {
548                         lua_pushnumber(L, mg->heatmap[i]);
549                         lua_rawseti(L, -2, i + 1);
550                 }
551
552                 return 1;
553         }
554
555         case MGOBJ_HUMIDMAP: {
556                 if (!mg->humidmap)
557                         return 0;
558
559                 lua_newtable(L);
560                 for (size_t i = 0; i != maplen; i++) {
561                         lua_pushnumber(L, mg->humidmap[i]);
562                         lua_rawseti(L, -2, i + 1);
563                 }
564
565                 return 1;
566         }
567         case MGOBJ_GENNOTIFY: {
568                 std::map<std::string, std::vector<v3s16> >event_map;
569                 std::map<std::string, std::vector<v3s16> >::iterator it;
570
571                 mg->gennotify.getEvents(event_map);
572
573                 lua_newtable(L);
574                 for (it = event_map.begin(); it != event_map.end(); ++it) {
575                         lua_newtable(L);
576
577                         for (size_t j = 0; j != it->second.size(); j++) {
578                                 push_v3s16(L, it->second[j]);
579                                 lua_rawseti(L, -2, j + 1);
580                         }
581
582                         lua_setfield(L, -2, it->first.c_str());
583                 }
584
585                 return 1;
586         }
587         }
588
589         return 0;
590 }
591
592
593 int ModApiMapgen::l_get_mapgen_params(lua_State *L)
594 {
595         NO_MAP_LOCK_REQUIRED;
596
597         MapgenParams *params = &getServer(L)->getEmergeManager()->params;
598
599         lua_newtable(L);
600
601         lua_pushstring(L, params->mg_name.c_str());
602         lua_setfield(L, -2, "mgname");
603
604         lua_pushinteger(L, params->seed);
605         lua_setfield(L, -2, "seed");
606
607         lua_pushinteger(L, params->water_level);
608         lua_setfield(L, -2, "water_level");
609
610         lua_pushinteger(L, params->chunksize);
611         lua_setfield(L, -2, "chunksize");
612
613         std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, U32_MAX);
614         lua_pushstring(L, flagstr.c_str());
615         lua_setfield(L, -2, "flags");
616
617         return 1;
618 }
619
620
621 // set_mapgen_params(params)
622 // set mapgen parameters
623 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
624 {
625         NO_MAP_LOCK_REQUIRED;
626
627         if (!lua_istable(L, 1))
628                 return 0;
629
630         EmergeManager *emerge = getServer(L)->getEmergeManager();
631         if (emerge->isRunning())
632                 throw LuaError("Cannot set parameters while mapgen is running");
633
634         MapgenParams *params = &emerge->params;
635         u32 flags = 0, flagmask = 0;
636
637         lua_getfield(L, 1, "mgname");
638         if (lua_isstring(L, -1)) {
639                 params->mg_name = lua_tostring(L, -1);
640                 delete params->sparams;
641                 params->sparams = NULL;
642         }
643
644         lua_getfield(L, 1, "seed");
645         if (lua_isnumber(L, -1))
646                 params->seed = lua_tointeger(L, -1);
647
648         lua_getfield(L, 1, "water_level");
649         if (lua_isnumber(L, -1))
650                 params->water_level = lua_tointeger(L, -1);
651
652         lua_getfield(L, 1, "chunksize");
653         if (lua_isnumber(L, -1))
654                 params->chunksize = lua_tointeger(L, -1);
655
656         warn_if_field_exists(L, 1, "flagmask",
657                 "Deprecated: flags field now includes unset flags.");
658         lua_getfield(L, 1, "flagmask");
659         if (lua_isstring(L, -1))
660                 params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL);
661
662         if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
663                 params->flags &= ~flagmask;
664                 params->flags |= flags;
665         }
666
667         return 0;
668 }
669
670
671 // set_noiseparams(name, noiseparams, set_default)
672 // set global config values for noise parameters
673 int ModApiMapgen::l_set_noiseparams(lua_State *L)
674 {
675         NO_MAP_LOCK_REQUIRED;
676
677         const char *name = luaL_checkstring(L, 1);
678
679         NoiseParams np;
680         if (!read_noiseparams(L, 2, &np))
681                 return 0;
682
683         bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
684
685         g_settings->setNoiseParams(name, np, set_default);
686
687         return 0;
688 }
689
690
691 // get_noiseparams(name)
692 int ModApiMapgen::l_get_noiseparams(lua_State *L)
693 {
694         NO_MAP_LOCK_REQUIRED;
695
696         std::string name = luaL_checkstring(L, 1);
697
698         NoiseParams np;
699         if (!g_settings->getNoiseParams(name, np))
700                 return 0;
701
702         push_noiseparams(L, &np);
703         return 1;
704 }
705
706
707 // set_gen_notify(flags, {deco_id_table})
708 int ModApiMapgen::l_set_gen_notify(lua_State *L)
709 {
710         NO_MAP_LOCK_REQUIRED;
711
712         u32 flags = 0, flagmask = 0;
713         EmergeManager *emerge = getServer(L)->getEmergeManager();
714
715         if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
716                 emerge->gen_notify_on &= ~flagmask;
717                 emerge->gen_notify_on |= flags;
718         }
719
720         if (lua_istable(L, 2)) {
721                 lua_pushnil(L);
722                 while (lua_next(L, 2)) {
723                         if (lua_isnumber(L, -1))
724                                 emerge->gen_notify_on_deco_ids.insert((u32)lua_tonumber(L, -1));
725                         lua_pop(L, 1);
726                 }
727         }
728
729         return 0;
730 }
731
732
733 // get_gen_notify()
734 int ModApiMapgen::l_get_gen_notify(lua_State *L)
735 {
736         NO_MAP_LOCK_REQUIRED;
737
738         EmergeManager *emerge = getServer(L)->getEmergeManager();
739         push_flags_string(L, flagdesc_gennotify, emerge->gen_notify_on,
740                 emerge->gen_notify_on);
741
742         lua_newtable(L);
743         int i = 1;
744         for (std::set<u32>::iterator it = emerge->gen_notify_on_deco_ids.begin();
745                         it != emerge->gen_notify_on_deco_ids.end(); ++it) {
746                 lua_pushnumber(L, *it);
747                 lua_rawseti(L, -2, i);
748                 i++;
749         }
750         return 2;
751 }
752
753
754 // register_biome({lots of stuff})
755 int ModApiMapgen::l_register_biome(lua_State *L)
756 {
757         NO_MAP_LOCK_REQUIRED;
758
759         int index = 1;
760         luaL_checktype(L, index, LUA_TTABLE);
761
762         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
763         BiomeManager *bmgr    = getServer(L)->getEmergeManager()->biomemgr;
764
765         Biome *biome = read_biome_def(L, index, ndef);
766         if (!biome)
767                 return 0;
768
769         ObjDefHandle handle = bmgr->add(biome);
770         if (handle == OBJDEF_INVALID_HANDLE) {
771                 delete biome;
772                 return 0;
773         }
774
775         lua_pushinteger(L, handle);
776         return 1;
777 }
778
779
780 // register_decoration({lots of stuff})
781 int ModApiMapgen::l_register_decoration(lua_State *L)
782 {
783         NO_MAP_LOCK_REQUIRED;
784
785         int index = 1;
786         luaL_checktype(L, index, LUA_TTABLE);
787
788         INodeDefManager *ndef      = getServer(L)->getNodeDefManager();
789         DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
790         BiomeManager *biomemgr     = getServer(L)->getEmergeManager()->biomemgr;
791         SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
792
793         enum DecorationType decotype = (DecorationType)getenumfield(L, index,
794                                 "deco_type", es_DecorationType, -1);
795
796         Decoration *deco = decomgr->create(decotype);
797         if (!deco) {
798                 errorstream << "register_decoration: decoration placement type "
799                         << decotype << " not implemented" << std::endl;
800                 return 0;
801         }
802
803         deco->name       = getstringfield_default(L, index, "name", "");
804         deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
805         deco->y_min      = getintfield_default(L, index, "y_min", -31000);
806         deco->y_max      = getintfield_default(L, index, "y_max", 31000);
807         deco->sidelen    = getintfield_default(L, index, "sidelen", 8);
808         if (deco->sidelen <= 0) {
809                 errorstream << "register_decoration: sidelen must be "
810                         "greater than 0" << std::endl;
811                 delete deco;
812                 return 0;
813         }
814
815         //// Get node name(s) to place decoration on
816         size_t nread = getstringlistfield(L, index, "place_on", &deco->m_nodenames);
817         deco->m_nnlistsizes.push_back(nread);
818
819         //// Get decoration flags
820         getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
821
822         //// Get NoiseParams to define how decoration is placed
823         lua_getfield(L, index, "noise_params");
824         if (read_noiseparams(L, -1, &deco->np))
825                 deco->flags |= DECO_USE_NOISE;
826         lua_pop(L, 1);
827
828         //// Get biomes associated with this decoration (if any)
829         lua_getfield(L, index, "biomes");
830         if (get_biome_list(L, -1, biomemgr, &deco->biomes))
831                 errorstream << "register_decoration: couldn't get all biomes " << std::endl;
832         lua_pop(L, 1);
833
834         //// Handle decoration type-specific parameters
835         bool success = false;
836         switch (decotype) {
837         case DECO_SIMPLE:
838                 success = read_deco_simple(L, (DecoSimple *)deco);
839                 break;
840         case DECO_SCHEMATIC:
841                 success = read_deco_schematic(L, schemmgr, (DecoSchematic *)deco);
842                 break;
843         case DECO_LSYSTEM:
844                 break;
845         }
846
847         if (!success) {
848                 delete deco;
849                 return 0;
850         }
851
852         ndef->pendNodeResolve(deco);
853
854         ObjDefHandle handle = decomgr->add(deco);
855         if (handle == OBJDEF_INVALID_HANDLE) {
856                 delete deco;
857                 return 0;
858         }
859
860         lua_pushinteger(L, handle);
861         return 1;
862 }
863
864
865 bool read_deco_simple(lua_State *L, DecoSimple *deco)
866 {
867         size_t nnames;
868         int index = 1;
869
870         deco->deco_height     = getintfield_default(L, index, "height", 1);
871         deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
872         deco->nspawnby        = getintfield_default(L, index, "num_spawn_by", -1);
873
874         if (deco->deco_height <= 0) {
875                 errorstream << "register_decoration: simple decoration height"
876                         " must be greater than 0" << std::endl;
877                 return false;
878         }
879
880         nnames = getstringlistfield(L, index, "decoration", &deco->m_nodenames);
881         deco->m_nnlistsizes.push_back(nnames);
882         if (nnames == 0) {
883                 errorstream << "register_decoration: no decoration nodes "
884                         "defined" << std::endl;
885                 return false;
886         }
887
888         nnames = getstringlistfield(L, index, "spawn_by", &deco->m_nodenames);
889         deco->m_nnlistsizes.push_back(nnames);
890         if (nnames == 0 && deco->nspawnby != -1) {
891                 errorstream << "register_decoration: no spawn_by nodes defined,"
892                         " but num_spawn_by specified" << std::endl;
893                 return false;
894         }
895
896         return true;
897 }
898
899
900 bool read_deco_schematic(lua_State *L, SchematicManager *schemmgr, DecoSchematic *deco)
901 {
902         int index = 1;
903
904         deco->rotation = (Rotation)getenumfield(L, index, "rotation",
905                 ModApiMapgen::es_Rotation, ROTATE_0);
906
907         StringMap replace_names;
908         lua_getfield(L, index, "replacements");
909         if (lua_istable(L, -1))
910                 read_schematic_replacements(L, -1, &replace_names);
911         lua_pop(L, 1);
912
913         lua_getfield(L, index, "schematic");
914         Schematic *schem = get_or_load_schematic(L, -1, schemmgr, &replace_names);
915         lua_pop(L, 1);
916
917         deco->schematic = schem;
918         return schem != NULL;
919 }
920
921
922 // register_ore({lots of stuff})
923 int ModApiMapgen::l_register_ore(lua_State *L)
924 {
925         NO_MAP_LOCK_REQUIRED;
926
927         int index = 1;
928         luaL_checktype(L, index, LUA_TTABLE);
929
930         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
931         BiomeManager *bmgr    = getServer(L)->getEmergeManager()->biomemgr;
932         OreManager *oremgr    = getServer(L)->getEmergeManager()->oremgr;
933
934         enum OreType oretype = (OreType)getenumfield(L, index,
935                                 "ore_type", es_OreType, ORE_SCATTER);
936         Ore *ore = oremgr->create(oretype);
937         if (!ore) {
938                 errorstream << "register_ore: ore_type " << oretype << " not implemented\n";
939                 return 0;
940         }
941
942         ore->name           = getstringfield_default(L, index, "name", "");
943         ore->ore_param2     = (u8)getintfield_default(L, index, "ore_param2", 0);
944         ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
945         ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
946         ore->clust_size     = getintfield_default(L, index, "clust_size", 0);
947         ore->noise          = NULL;
948         ore->flags          = 0;
949
950         //// Get noise_threshold
951         warn_if_field_exists(L, index, "noise_threshhold",
952                 "Deprecated: new name is \"noise_threshold\".");
953
954         float nthresh;
955         if (!getfloatfield(L, index, "noise_threshold", nthresh) &&
956                         !getfloatfield(L, index, "noise_threshhold", nthresh))
957                 nthresh = 0;
958         ore->nthresh = nthresh;
959
960         //// Get y_min/y_max
961         warn_if_field_exists(L, index, "height_min",
962                 "Deprecated: new name is \"y_min\".");
963         warn_if_field_exists(L, index, "height_max",
964                 "Deprecated: new name is \"y_max\".");
965
966         int ymin, ymax;
967         if (!getintfield(L, index, "y_min", ymin) &&
968                 !getintfield(L, index, "height_min", ymin))
969                 ymin = -31000;
970         if (!getintfield(L, index, "y_max", ymax) &&
971                 !getintfield(L, index, "height_max", ymax))
972                 ymax = 31000;
973         ore->y_min = ymin;
974         ore->y_max = ymax;
975
976         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
977                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
978                         "must be greater than 0" << std::endl;
979                 delete ore;
980                 return 0;
981         }
982
983         //// Get flags
984         getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
985
986         //// Get biomes associated with this decoration (if any)
987         lua_getfield(L, index, "biomes");
988         if (get_biome_list(L, -1, bmgr, &ore->biomes))
989                 errorstream << "register_ore: couldn't get all biomes " << std::endl;
990         lua_pop(L, 1);
991
992         //// Get noise parameters if needed
993         lua_getfield(L, index, "noise_params");
994         if (read_noiseparams(L, -1, &ore->np)) {
995                 ore->flags |= OREFLAG_USE_NOISE;
996         } else if (ore->NEEDS_NOISE) {
997                 errorstream << "register_ore: specified ore type requires valid "
998                         "noise parameters" << std::endl;
999                 delete ore;
1000                 return 0;
1001         }
1002         lua_pop(L, 1);
1003
1004         //// Get type-specific parameters
1005         switch (oretype) {
1006                 case ORE_SHEET: {
1007                         OreSheet *oresheet = (OreSheet *)ore;
1008
1009                         oresheet->column_height_min = getintfield_default(L, index,
1010                                 "column_height_min", 1);
1011                         oresheet->column_height_max = getintfield_default(L, index,
1012                                 "column_height_max", ore->clust_size);
1013                         oresheet->column_midpoint_factor = getfloatfield_default(L, index,
1014                                 "column_midpoint_factor", 0.5f);
1015
1016                         break;
1017                 }
1018                 case ORE_PUFF: {
1019                         OrePuff *orepuff = (OrePuff *)ore;
1020
1021                         lua_getfield(L, index, "np_puff_top");
1022                         read_noiseparams(L, -1, &orepuff->np_puff_top);
1023                         lua_pop(L, 1);
1024
1025                         lua_getfield(L, index, "np_puff_bottom");
1026                         read_noiseparams(L, -1, &orepuff->np_puff_bottom);
1027                         lua_pop(L, 1);
1028
1029                         break;
1030                 }
1031                 case ORE_VEIN: {
1032                         OreVein *orevein = (OreVein *)ore;
1033
1034                         orevein->random_factor = getfloatfield_default(L, index,
1035                                 "random_factor", 1.f);
1036
1037                         break;
1038                 }
1039                 default:
1040                         break;
1041         }
1042
1043         ObjDefHandle handle = oremgr->add(ore);
1044         if (handle == OBJDEF_INVALID_HANDLE) {
1045                 delete ore;
1046                 return 0;
1047         }
1048
1049         ore->m_nodenames.push_back(getstringfield_default(L, index, "ore", ""));
1050
1051         size_t nnames = getstringlistfield(L, index, "wherein", &ore->m_nodenames);
1052         ore->m_nnlistsizes.push_back(nnames);
1053
1054         ndef->pendNodeResolve(ore);
1055
1056         lua_pushinteger(L, handle);
1057         return 1;
1058 }
1059
1060
1061 // register_schematic({schematic}, replacements={})
1062 int ModApiMapgen::l_register_schematic(lua_State *L)
1063 {
1064         NO_MAP_LOCK_REQUIRED;
1065
1066         SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1067
1068         StringMap replace_names;
1069         if (lua_istable(L, 2))
1070                 read_schematic_replacements(L, 2, &replace_names);
1071
1072         Schematic *schem = load_schematic(L, 1, schemmgr->getNodeDef(),
1073                 &replace_names);
1074         if (!schem)
1075                 return 0;
1076
1077         ObjDefHandle handle = schemmgr->add(schem);
1078         if (handle == OBJDEF_INVALID_HANDLE) {
1079                 delete schem;
1080                 return 0;
1081         }
1082
1083         lua_pushinteger(L, handle);
1084         return 1;
1085 }
1086
1087
1088 // clear_registered_biomes()
1089 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
1090 {
1091         NO_MAP_LOCK_REQUIRED;
1092
1093         BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
1094         bmgr->clear();
1095         return 0;
1096 }
1097
1098
1099 // clear_registered_decorations()
1100 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
1101 {
1102         NO_MAP_LOCK_REQUIRED;
1103
1104         DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
1105         dmgr->clear();
1106         return 0;
1107 }
1108
1109
1110 // clear_registered_ores()
1111 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
1112 {
1113         NO_MAP_LOCK_REQUIRED;
1114
1115         OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
1116         omgr->clear();
1117         return 0;
1118 }
1119
1120
1121 // clear_registered_schematics()
1122 int ModApiMapgen::l_clear_registered_schematics(lua_State *L)
1123 {
1124         NO_MAP_LOCK_REQUIRED;
1125
1126         SchematicManager *smgr = getServer(L)->getEmergeManager()->schemmgr;
1127         smgr->clear();
1128         return 0;
1129 }
1130
1131
1132 // generate_ores(vm, p1, p2, [ore_id])
1133 int ModApiMapgen::l_generate_ores(lua_State *L)
1134 {
1135         NO_MAP_LOCK_REQUIRED;
1136
1137         EmergeManager *emerge = getServer(L)->getEmergeManager();
1138
1139         Mapgen mg;
1140         mg.seed = emerge->params.seed;
1141         mg.vm   = LuaVoxelManip::checkobject(L, 1)->vm;
1142         mg.ndef = getServer(L)->getNodeDefManager();
1143
1144         v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1145                         mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1146         v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1147                         mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1148         sortBoxVerticies(pmin, pmax);
1149
1150         u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1151
1152         emerge->oremgr->placeAllOres(&mg, blockseed, pmin, pmax);
1153
1154         return 0;
1155 }
1156
1157
1158 // generate_decorations(vm, p1, p2, [deco_id])
1159 int ModApiMapgen::l_generate_decorations(lua_State *L)
1160 {
1161         NO_MAP_LOCK_REQUIRED;
1162
1163         EmergeManager *emerge = getServer(L)->getEmergeManager();
1164
1165         Mapgen mg;
1166         mg.seed = emerge->params.seed;
1167         mg.vm   = LuaVoxelManip::checkobject(L, 1)->vm;
1168         mg.ndef = getServer(L)->getNodeDefManager();
1169
1170         v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) :
1171                         mg.vm->m_area.MinEdge + v3s16(1,1,1) * MAP_BLOCKSIZE;
1172         v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) :
1173                         mg.vm->m_area.MaxEdge - v3s16(1,1,1) * MAP_BLOCKSIZE;
1174         sortBoxVerticies(pmin, pmax);
1175
1176         u32 blockseed = Mapgen::getBlockSeed(pmin, mg.seed);
1177
1178         emerge->decomgr->placeAllDecos(&mg, blockseed, pmin, pmax);
1179
1180         return 0;
1181 }
1182
1183
1184 // create_schematic(p1, p2, probability_list, filename, y_slice_prob_list)
1185 int ModApiMapgen::l_create_schematic(lua_State *L)
1186 {
1187         MAP_LOCK_REQUIRED;
1188
1189         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
1190
1191         const char *filename = luaL_checkstring(L, 4);
1192         CHECK_SECURE_PATH_OPTIONAL(L, filename);
1193
1194         Map *map = &(getEnv(L)->getMap());
1195         Schematic schem;
1196
1197         v3s16 p1 = check_v3s16(L, 1);
1198         v3s16 p2 = check_v3s16(L, 2);
1199         sortBoxVerticies(p1, p2);
1200
1201         std::vector<std::pair<v3s16, u8> > prob_list;
1202         if (lua_istable(L, 3)) {
1203                 lua_pushnil(L);
1204                 while (lua_next(L, 3)) {
1205                         if (lua_istable(L, -1)) {
1206                                 lua_getfield(L, -1, "pos");
1207                                 v3s16 pos = check_v3s16(L, -1);
1208                                 lua_pop(L, 1);
1209
1210                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1211                                 prob_list.push_back(std::make_pair(pos, prob));
1212                         }
1213
1214                         lua_pop(L, 1);
1215                 }
1216         }
1217
1218         std::vector<std::pair<s16, u8> > slice_prob_list;
1219         if (lua_istable(L, 5)) {
1220                 lua_pushnil(L);
1221                 while (lua_next(L, 5)) {
1222                         if (lua_istable(L, -1)) {
1223                                 s16 ypos = getintfield_default(L, -1, "ypos", 0);
1224                                 u8 prob  = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
1225                                 slice_prob_list.push_back(std::make_pair(ypos, prob));
1226                         }
1227
1228                         lua_pop(L, 1);
1229                 }
1230         }
1231
1232         if (!schem.getSchematicFromMap(map, p1, p2)) {
1233                 errorstream << "create_schematic: failed to get schematic "
1234                         "from map" << std::endl;
1235                 return 0;
1236         }
1237
1238         schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
1239
1240         schem.saveSchematicToFile(filename, ndef);
1241         actionstream << "create_schematic: saved schematic file '"
1242                 << filename << "'." << std::endl;
1243
1244         lua_pushboolean(L, true);
1245         return 1;
1246 }
1247
1248
1249 // place_schematic(p, schematic, rotation, replacement)
1250 int ModApiMapgen::l_place_schematic(lua_State *L)
1251 {
1252         MAP_LOCK_REQUIRED;
1253
1254         Map *map = &(getEnv(L)->getMap());
1255         SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1256
1257         //// Read position
1258         v3s16 p = check_v3s16(L, 1);
1259
1260         //// Read rotation
1261         int rot = ROTATE_0;
1262         const char *enumstr = lua_tostring(L, 3);
1263         if (enumstr)
1264                 string_to_enum(es_Rotation, rot, std::string(enumstr));
1265
1266         //// Read force placement
1267         bool force_placement = true;
1268         if (lua_isboolean(L, 5))
1269                 force_placement = lua_toboolean(L, 5);
1270
1271         //// Read node replacements
1272         StringMap replace_names;
1273         if (lua_istable(L, 4))
1274                 read_schematic_replacements(L, 4, &replace_names);
1275
1276         //// Read schematic
1277         Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names);
1278         if (!schem) {
1279                 errorstream << "place_schematic: failed to get schematic" << std::endl;
1280                 return 0;
1281         }
1282
1283         schem->placeOnMap(map, p, 0, (Rotation)rot, force_placement);
1284
1285         lua_pushboolean(L, true);
1286         return 1;
1287 }
1288
1289 int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L)
1290 {
1291         NO_MAP_LOCK_REQUIRED;
1292
1293         SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1294
1295         //// Read VoxelManip object
1296         MMVManip *vm = LuaVoxelManip::checkobject(L, 1)->vm;
1297
1298         //// Read position
1299         v3s16 p = check_v3s16(L, 2);
1300
1301         //// Read rotation
1302         int rot = ROTATE_0;
1303         const char *enumstr = lua_tostring(L, 4);
1304         if (enumstr)
1305                 string_to_enum(es_Rotation, rot, std::string(enumstr));
1306
1307         //// Read force placement
1308         bool force_placement = true;
1309         if (lua_isboolean(L, 6))
1310                 force_placement = lua_toboolean(L, 6);
1311
1312         //// Read node replacements
1313         StringMap replace_names;
1314         if (lua_istable(L, 5))
1315                 read_schematic_replacements(L, 5, &replace_names);
1316
1317         //// Read schematic
1318         Schematic *schem = get_or_load_schematic(L, 3, schemmgr, &replace_names);
1319         if (!schem) {
1320                 errorstream << "place_schematic: failed to get schematic" << std::endl;
1321                 return 0;
1322         }
1323
1324         bool schematic_did_fit = schem->placeOnVManip(
1325                 vm, p, 0, (Rotation)rot, force_placement);
1326
1327         lua_pushboolean(L, schematic_did_fit);
1328         return 1;
1329 }
1330
1331 // serialize_schematic(schematic, format, options={...})
1332 int ModApiMapgen::l_serialize_schematic(lua_State *L)
1333 {
1334         NO_MAP_LOCK_REQUIRED;
1335
1336         SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr;
1337
1338         //// Read options
1339         bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false);
1340         u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0);
1341
1342         //// Get schematic
1343         bool was_loaded = false;
1344         Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr);
1345         if (!schem) {
1346                 schem = load_schematic(L, 1, NULL, NULL);
1347                 was_loaded = true;
1348         }
1349         if (!schem) {
1350                 errorstream << "serialize_schematic: failed to get schematic" << std::endl;
1351                 return 0;
1352         }
1353
1354         //// Read format of definition to save as
1355         int schem_format = SCHEM_FMT_MTS;
1356         const char *enumstr = lua_tostring(L, 2);
1357         if (enumstr)
1358                 string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr));
1359
1360         //// Serialize to binary string
1361         std::ostringstream os(std::ios_base::binary);
1362         switch (schem_format) {
1363         case SCHEM_FMT_MTS:
1364                 schem->serializeToMts(&os, schem->m_nodenames);
1365                 break;
1366         case SCHEM_FMT_LUA:
1367                 schem->serializeToLua(&os, schem->m_nodenames,
1368                         use_comments, indent_spaces);
1369                 break;
1370         default:
1371                 return 0;
1372         }
1373
1374         if (was_loaded)
1375                 delete schem;
1376
1377         std::string ser = os.str();
1378         lua_pushlstring(L, ser.c_str(), ser.length());
1379         return 1;
1380 }
1381
1382
1383 void ModApiMapgen::Initialize(lua_State *L, int top)
1384 {
1385         API_FCT(get_biome_id);
1386         API_FCT(get_mapgen_object);
1387
1388         API_FCT(get_mapgen_params);
1389         API_FCT(set_mapgen_params);
1390         API_FCT(set_noiseparams);
1391         API_FCT(get_noiseparams);
1392         API_FCT(set_gen_notify);
1393         API_FCT(get_gen_notify);
1394
1395         API_FCT(register_biome);
1396         API_FCT(register_decoration);
1397         API_FCT(register_ore);
1398         API_FCT(register_schematic);
1399
1400         API_FCT(clear_registered_biomes);
1401         API_FCT(clear_registered_decorations);
1402         API_FCT(clear_registered_ores);
1403         API_FCT(clear_registered_schematics);
1404
1405         API_FCT(generate_ores);
1406         API_FCT(generate_decorations);
1407         API_FCT(create_schematic);
1408         API_FCT(place_schematic);
1409         API_FCT(place_schematic_on_vmanip);
1410         API_FCT(serialize_schematic);
1411 }