]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mapgen.cpp
Set fallback content if resolving content vector requires everything
[dragonfireclient.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 "util/serialize.h"
26 #include "server.h"
27 #include "environment.h"
28 #include "emerge.h"
29 #include "mg_biome.h"
30 #include "mg_ore.h"
31 #include "mg_decoration.h"
32 #include "mg_schematic.h"
33 #include "mapgen_v5.h"
34 #include "mapgen_v7.h"
35 #include "settings.h"
36 #include "main.h"
37 #include "log.h"
38
39
40 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
41 {
42         {BIOME_TYPE_NORMAL, "normal"},
43         {BIOME_TYPE_LIQUID, "liquid"},
44         {BIOME_TYPE_NETHER, "nether"},
45         {BIOME_TYPE_AETHER, "aether"},
46         {BIOME_TYPE_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_CLAYLIKE, "claylike"},
74         {0, NULL},
75 };
76
77 struct EnumString ModApiMapgen::es_Rotation[] =
78 {
79         {ROTATE_0,    "0"},
80         {ROTATE_90,   "90"},
81         {ROTATE_180,  "180"},
82         {ROTATE_270,  "270"},
83         {ROTATE_RAND, "random"},
84         {0, NULL},
85 };
86
87
88 ///////////////////////////////////////////////////////////////////////////////
89
90
91 bool read_schematic(lua_State *L, int index, Schematic *schem,
92         INodeDefManager *ndef, std::map<std::string, std::string> &replace_names)
93 {
94         //// Get schematic size
95         lua_getfield(L, index, "size");
96         v3s16 size = read_v3s16(L, -1);
97         lua_pop(L, 1);
98
99         //// Get schematic data
100         lua_getfield(L, index, "data");
101         luaL_checktype(L, -1, LUA_TTABLE);
102
103         int numnodes = size.X * size.Y * size.Z;
104         MapNode *schemdata = new MapNode[numnodes];
105         int i = 0;
106
107         lua_pushnil(L);
108         while (lua_next(L, -2)) {
109                 if (i >= numnodes) {
110                         i++;
111                         lua_pop(L, 1);
112                         continue;
113                 }
114
115                 // same as readnode, except param1 default is MTSCHEM_PROB_CONST
116                 lua_getfield(L, -1, "name");
117                 std::string name = luaL_checkstring(L, -1);
118                 lua_pop(L, 1);
119
120                 u8 param1;
121                 lua_getfield(L, -1, "param1");
122                 param1 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : MTSCHEM_PROB_ALWAYS;
123                 lua_pop(L, 1);
124
125                 u8 param2;
126                 lua_getfield(L, -1, "param2");
127                 param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0;
128                 lua_pop(L, 1);
129
130                 std::map<std::string, std::string>::iterator it;
131                 it = replace_names.find(name);
132                 if (it != replace_names.end())
133                         name = it->second;
134
135                 schemdata[i] = MapNode(ndef, name, param1, param2);
136
137                 i++;
138                 lua_pop(L, 1);
139         }
140
141         if (i != numnodes) {
142                 errorstream << "read_schematic: incorrect number of "
143                         "nodes provided in raw schematic data (got " << i <<
144                         ", expected " << numnodes << ")." << std::endl;
145                 delete schemdata;
146                 return false;
147         }
148
149         //// Get Y-slice probability values (if present)
150         u8 *slice_probs = new u8[size.Y];
151         for (i = 0; i != size.Y; i++)
152                 slice_probs[i] = MTSCHEM_PROB_ALWAYS;
153
154         lua_getfield(L, index, "yslice_prob");
155         if (lua_istable(L, -1)) {
156                 lua_pushnil(L);
157                 while (lua_next(L, -2)) {
158                         if (getintfield(L, -1, "ypos", i) && i >= 0 && i < size.Y) {
159                                 slice_probs[i] = getintfield_default(L, -1,
160                                         "prob", MTSCHEM_PROB_ALWAYS);
161                         }
162                         lua_pop(L, 1);
163                 }
164         }
165
166         // Here, we read the nodes directly from the INodeDefManager - there is no
167         // need for pending node resolutions so we'll mark this schematic as updated
168         schem->flags       = SCHEM_CIDS_UPDATED;
169
170         schem->size        = size;
171         schem->schemdata   = schemdata;
172         schem->slice_probs = slice_probs;
173         return true;
174 }
175
176
177 bool get_schematic(lua_State *L, int index, Schematic *schem,
178         INodeDefManager *ndef, std::map<std::string, std::string> &replace_names)
179 {
180         if (index < 0)
181                 index = lua_gettop(L) + 1 + index;
182
183         if (lua_istable(L, index)) {
184                 return read_schematic(L, index, schem, ndef, replace_names);
185         } else if (lua_isstring(L, index)) {
186                 const char *filename = lua_tostring(L, index);
187                 return schem->loadSchematicFromFile(filename, ndef, replace_names);
188         } else {
189                 return false;
190         }
191 }
192
193
194 void read_schematic_replacements(lua_State *L,
195         std::map<std::string, std::string> &replace_names, int index)
196 {
197         lua_pushnil(L);
198         while (lua_next(L, index)) {
199                 std::string replace_from;
200                 std::string replace_to;
201
202                 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
203                         lua_rawgeti(L, -1, 1);
204                         replace_from = lua_tostring(L, -1);
205                         lua_pop(L, 1);
206
207                         lua_rawgeti(L, -1, 2);
208                         replace_to = lua_tostring(L, -1);
209                         lua_pop(L, 1);
210                 } else { // New {x = "y", ...} format
211                         replace_from = lua_tostring(L, -2);
212                         replace_to = lua_tostring(L, -1);
213                 }
214
215                 replace_names[replace_from] = replace_to;
216                 lua_pop(L, 1);
217         }
218 }
219
220
221 // get_mapgen_object(objectname)
222 // returns the requested object used during map generation
223 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
224 {
225         const char *mgobjstr = lua_tostring(L, 1);
226
227         int mgobjint;
228         if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
229                 return 0;
230
231         enum MapgenObject mgobj = (MapgenObject)mgobjint;
232
233         EmergeManager *emerge = getServer(L)->getEmergeManager();
234         Mapgen *mg = emerge->getCurrentMapgen();
235         if (!mg)
236                 return 0;
237
238         size_t maplen = mg->csize.X * mg->csize.Z;
239
240         switch (mgobj) {
241                 case MGOBJ_VMANIP: {
242                         ManualMapVoxelManipulator *vm = mg->vm;
243
244                         // VoxelManip object
245                         LuaVoxelManip *o = new LuaVoxelManip(vm, true);
246                         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
247                         luaL_getmetatable(L, "VoxelManip");
248                         lua_setmetatable(L, -2);
249
250                         // emerged min pos
251                         push_v3s16(L, vm->m_area.MinEdge);
252
253                         // emerged max pos
254                         push_v3s16(L, vm->m_area.MaxEdge);
255
256                         return 3;
257                 }
258                 case MGOBJ_HEIGHTMAP: {
259                         if (!mg->heightmap)
260                                 return 0;
261
262                         lua_newtable(L);
263                         for (size_t i = 0; i != maplen; i++) {
264                                 lua_pushinteger(L, mg->heightmap[i]);
265                                 lua_rawseti(L, -2, i + 1);
266                         }
267
268                         return 1;
269                 }
270                 case MGOBJ_BIOMEMAP: {
271                         if (!mg->biomemap)
272                                 return 0;
273
274                         lua_newtable(L);
275                         for (size_t i = 0; i != maplen; i++) {
276                                 lua_pushinteger(L, mg->biomemap[i]);
277                                 lua_rawseti(L, -2, i + 1);
278                         }
279
280                         return 1;
281                 }
282                 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
283                 case MGOBJ_HUMIDMAP:
284                         if (strcmp(emerge->params.mg_name.c_str(), "v7"))
285                                 return 0;
286
287                         MapgenV7 *mgv7 = (MapgenV7 *)mg;
288
289                         float *arr = (mgobj == MGOBJ_HEATMAP) ?
290                                 mgv7->noise_heat->result : mgv7->noise_humidity->result;
291                         if (!arr)
292                                 return 0;
293
294                         lua_newtable(L);
295                         for (size_t i = 0; i != maplen; i++) {
296                                 lua_pushnumber(L, arr[i]);
297                                 lua_rawseti(L, -2, i + 1);
298                         }
299
300                         return 1;
301                 }
302                 case MGOBJ_GENNOTIFY: {
303                         std::map<std::string, std::vector<v3s16> >event_map;
304                         std::map<std::string, std::vector<v3s16> >::iterator it;
305
306                         mg->gennotify.getEvents(event_map);
307
308                         lua_newtable(L);
309                         for (it = event_map.begin(); it != event_map.end(); ++it) {
310                                 lua_newtable(L);
311
312                                 for (size_t j = 0; j != it->second.size(); j++) {
313                                         push_v3s16(L, it->second[j]);
314                                         lua_rawseti(L, -2, j + 1);
315                                 }
316
317                                 lua_setfield(L, -2, it->first.c_str());
318                         }
319
320                         return 1;
321                 }
322         }
323
324         return 0;
325 }
326
327 // set_mapgen_params(params)
328 // set mapgen parameters
329 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
330 {
331         if (!lua_istable(L, 1))
332                 return 0;
333
334         EmergeManager *emerge = getServer(L)->getEmergeManager();
335         assert(emerge);
336
337         std::string flagstr;
338         u32 flags = 0, flagmask = 0;
339
340         lua_getfield(L, 1, "mgname");
341         if (lua_isstring(L, -1)) {
342                 emerge->params.mg_name = std::string(lua_tostring(L, -1));
343                 delete emerge->params.sparams;
344                 emerge->params.sparams = NULL;
345         }
346
347         lua_getfield(L, 1, "seed");
348         if (lua_isnumber(L, -1))
349                 emerge->params.seed = lua_tointeger(L, -1);
350
351         lua_getfield(L, 1, "water_level");
352         if (lua_isnumber(L, -1))
353                 emerge->params.water_level = lua_tointeger(L, -1);
354
355         lua_getfield(L, 1, "flagmask");
356         if (lua_isstring(L, -1)) {
357                 flagstr = lua_tostring(L, -1);
358                 emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL);
359                 errorstream << "set_mapgen_params(): flagmask field is deprecated, "
360                         "see lua_api.txt" << std::endl;
361         }
362
363         if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
364                 emerge->params.flags &= ~flagmask;
365                 emerge->params.flags |= flags;
366         }
367
368         return 0;
369 }
370
371 // set_noiseparams(name, noiseparams, set_default)
372 // set global config values for noise parameters
373 int ModApiMapgen::l_set_noiseparams(lua_State *L)
374 {
375         const char *name = luaL_checkstring(L, 1);
376
377         NoiseParams np;
378         if (!read_noiseparams(L, 2, &np))
379                 return 0;
380
381         bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
382
383         g_settings->setNoiseParams(name, np, set_default);
384
385         return 0;
386 }
387
388 // set_gen_notify(flags, {deco_id_table})
389 int ModApiMapgen::l_set_gen_notify(lua_State *L)
390 {
391         u32 flags = 0, flagmask = 0;
392         EmergeManager *emerge = getServer(L)->getEmergeManager();
393
394         if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
395                 emerge->gen_notify_on &= ~flagmask;
396                 emerge->gen_notify_on |= flags;
397         }
398
399         if (lua_istable(L, 2)) {
400                 lua_pushnil(L);
401                 while (lua_next(L, 2)) {
402                         if (lua_isnumber(L, -1))
403                                 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
404                         lua_pop(L, 1);
405                 }
406         }
407
408         return 0;
409 }
410
411 // register_biome({lots of stuff})
412 int ModApiMapgen::l_register_biome(lua_State *L)
413 {
414         int index = 1;
415         luaL_checktype(L, index, LUA_TTABLE);
416
417         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
418         BiomeManager *bmgr    = getServer(L)->getEmergeManager()->biomemgr;
419
420         enum BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
421                 es_BiomeTerrainType, BIOME_TYPE_NORMAL);
422         Biome *b = bmgr->create(biometype);
423
424         b->name           = getstringfield_default(L, index, "name", "");
425         b->depth_top      = getintfield_default(L, index, "depth_top",    1);
426         b->depth_filler   = getintfield_default(L, index, "depth_filler", 3);
427         b->height_min     = getintfield_default(L, index, "height_min",   0);
428         b->height_max     = getintfield_default(L, index, "height_max",   0);
429         b->heat_point     = getfloatfield_default(L, index, "heat_point",     0.);
430         b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
431         b->flags          = 0; //reserved
432
433         u32 id = bmgr->add(b);
434         if (id == (u32)-1) {
435                 delete b;
436                 return 0;
437         }
438
439         NodeResolveInfo *nri = new NodeResolveInfo(b);
440         std::list<std::string> &nnames = nri->nodenames;
441         nnames.push_back(getstringfield_default(L, index, "node_top",        ""));
442         nnames.push_back(getstringfield_default(L, index, "node_filler",     ""));
443         nnames.push_back(getstringfield_default(L, index, "node_stone",      ""));
444         nnames.push_back(getstringfield_default(L, index, "node_water",      ""));
445         nnames.push_back(getstringfield_default(L, index, "node_dust",       ""));
446         nnames.push_back(getstringfield_default(L, index, "node_dust_water", ""));
447         ndef->pendNodeResolve(nri);
448
449         verbosestream << "register_biome: " << b->name << std::endl;
450
451         lua_pushinteger(L, id);
452         return 1;
453 }
454
455 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
456 {
457         BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
458         bmgr->clear();
459         return 0;
460 }
461
462 int ModApiMapgen::l_clear_registered_decorations(lua_State *L)
463 {
464         DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr;
465         dmgr->clear();
466         return 0;
467 }
468
469 int ModApiMapgen::l_clear_registered_ores(lua_State *L)
470 {
471         OreManager *omgr = getServer(L)->getEmergeManager()->oremgr;
472         omgr->clear();
473         return 0;
474 }
475
476 // register_decoration({lots of stuff})
477 int ModApiMapgen::l_register_decoration(lua_State *L)
478 {
479         int index = 1;
480         luaL_checktype(L, index, LUA_TTABLE);
481
482         INodeDefManager *ndef      = getServer(L)->getNodeDefManager();
483         DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
484         BiomeManager *biomemgr     = getServer(L)->getEmergeManager()->biomemgr;
485
486         enum DecorationType decotype = (DecorationType)getenumfield(L, index,
487                                 "deco_type", es_DecorationType, -1);
488
489         Decoration *deco = decomgr->create(decotype);
490         if (!deco) {
491                 errorstream << "register_decoration: decoration placement type "
492                         << decotype << " not implemented";
493                 return 0;
494         }
495
496         deco->name       = getstringfield_default(L, index, "name", "");
497         deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
498         deco->sidelen    = getintfield_default(L, index, "sidelen", 8);
499         if (deco->sidelen <= 0) {
500                 errorstream << "register_decoration: sidelen must be "
501                         "greater than 0" << std::endl;
502                 delete deco;
503                 return 0;
504         }
505
506         NodeResolveInfo *nri = new NodeResolveInfo(deco);
507
508         //// Get node name(s) to place decoration on
509         std::vector<const char *> place_on_names;
510         getstringlistfield(L, index, "place_on", place_on_names);
511         nri->nodelistinfo.push_back(NodeListInfo(place_on_names.size()));
512         for (size_t i = 0; i != place_on_names.size(); i++)
513                 nri->nodenames.push_back(place_on_names[i]);
514
515         getflagsfield(L, index, "flags", flagdesc_deco, &deco->flags, NULL);
516
517         //// Get NoiseParams to define how decoration is placed
518         lua_getfield(L, index, "noise_params");
519         if (read_noiseparams(L, -1, &deco->np))
520                 deco->flags |= DECO_USE_NOISE;
521         lua_pop(L, 1);
522
523         //// Get biomes associated with this decoration (if any)
524         std::vector<const char *> biome_list;
525         getstringlistfield(L, index, "biomes", biome_list);
526         for (size_t i = 0; i != biome_list.size(); i++) {
527                 Biome *b = (Biome *)biomemgr->getByName(biome_list[i]);
528                 if (!b)
529                         continue;
530
531                 deco->biomes.insert(b->id);
532         }
533
534         //// Handle decoration type-specific parameters
535         bool success = false;
536         switch (decotype) {
537                 case DECO_SIMPLE:
538                         success = regDecoSimple(L, nri, (DecoSimple *)deco);
539                         break;
540                 case DECO_SCHEMATIC:
541                         success = regDecoSchematic(L, ndef, (DecoSchematic *)deco);
542                         break;
543                 case DECO_LSYSTEM:
544                         break;
545         }
546
547         ndef->pendNodeResolve(nri);
548
549         if (!success) {
550                 delete deco;
551                 return 0;
552         }
553
554         u32 id = decomgr->add(deco);
555         if (id == (u32)-1) {
556                 delete deco;
557                 return 0;
558         }
559
560         verbosestream << "register_decoration: " << deco->name << std::endl;
561
562         lua_pushinteger(L, id);
563         return 1;
564 }
565
566 bool ModApiMapgen::regDecoSimple(lua_State *L,
567                 NodeResolveInfo *nri, DecoSimple *deco)
568 {
569         int index = 1;
570
571         deco->deco_height     = getintfield_default(L, index, "height", 1);
572         deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
573         deco->nspawnby        = getintfield_default(L, index, "num_spawn_by", -1);
574
575         if (deco->deco_height <= 0) {
576                 errorstream << "register_decoration: simple decoration height"
577                         " must be greater than 0" << std::endl;
578                 return false;
579         }
580
581         std::vector<const char *> deco_names;
582         getstringlistfield(L, index, "decoration", deco_names);
583         if (deco_names.size() == 0) {
584                 errorstream << "register_decoration: no decoration nodes "
585                         "defined" << std::endl;
586                 return false;
587         }
588         nri->nodelistinfo.push_back(NodeListInfo(deco_names.size()));
589         for (size_t i = 0; i != deco_names.size(); i++)
590                 nri->nodenames.push_back(deco_names[i]);
591
592         std::vector<const char *> spawnby_names;
593         getstringlistfield(L, index, "spawn_by", spawnby_names);
594         if (deco->nspawnby != -1 && spawnby_names.size() == 0) {
595                 errorstream << "register_decoration: no spawn_by nodes defined,"
596                         " but num_spawn_by specified" << std::endl;
597                 return false;
598         }
599         nri->nodelistinfo.push_back(NodeListInfo(spawnby_names.size()));
600         for (size_t i = 0; i != spawnby_names.size(); i++)
601                 nri->nodenames.push_back(spawnby_names[i]);
602
603         return true;
604 }
605
606 bool ModApiMapgen::regDecoSchematic(lua_State *L, INodeDefManager *ndef,
607         DecoSchematic *deco)
608 {
609         int index = 1;
610
611         deco->rotation = (Rotation)getenumfield(L, index, "rotation",
612                 es_Rotation, ROTATE_0);
613
614         std::map<std::string, std::string> replace_names;
615         lua_getfield(L, index, "replacements");
616         if (lua_istable(L, -1))
617                 read_schematic_replacements(L, replace_names, lua_gettop(L));
618         lua_pop(L, 1);
619
620         // TODO(hmmmm): get a ref from registered schematics
621         Schematic *schem = new Schematic;
622         lua_getfield(L, index, "schematic");
623         if (!get_schematic(L, -1, schem, ndef, replace_names)) {
624                 lua_pop(L, 1);
625                 delete schem;
626                 return false;
627         }
628         lua_pop(L, 1);
629
630         deco->schematic = schem;
631
632         return true;
633 }
634
635 // register_ore({lots of stuff})
636 int ModApiMapgen::l_register_ore(lua_State *L)
637 {
638         int index = 1;
639         luaL_checktype(L, index, LUA_TTABLE);
640
641         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
642         OreManager *oremgr    = getServer(L)->getEmergeManager()->oremgr;
643
644         enum OreType oretype = (OreType)getenumfield(L, index,
645                                 "ore_type", es_OreType, ORE_SCATTER);
646         Ore *ore = oremgr->create(oretype);
647         if (!ore) {
648                 errorstream << "register_ore: ore_type " << oretype << " not implemented";
649                 return 0;
650         }
651
652         ore->name           = getstringfield_default(L, index, "name", "");
653         ore->ore_param2     = (u8)getintfield_default(L, index, "ore_param2", 0);
654         ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
655         ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
656         ore->clust_size     = getintfield_default(L, index, "clust_size", 0);
657         ore->height_min     = getintfield_default(L, index, "height_min", 0);
658         ore->height_max     = getintfield_default(L, index, "height_max", 0);
659         ore->nthresh        = getfloatfield_default(L, index, "noise_threshhold", 0);
660         ore->noise          = NULL;
661         ore->flags          = 0;
662
663         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
664                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
665                         "must be greater than 0" << std::endl;
666                 delete ore;
667                 return 0;
668         }
669
670         getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
671
672         lua_getfield(L, index, "noise_params");
673         if (read_noiseparams(L, -1, &ore->np)) {
674                 ore->flags |= OREFLAG_USE_NOISE;
675         } else if (ore->NEEDS_NOISE) {
676                 errorstream << "register_ore: specified ore type requires valid "
677                         "noise parameters" << std::endl;
678                 delete ore;
679                 return 0;
680         }
681         lua_pop(L, 1);
682
683         u32 id = oremgr->add(ore);
684         if (id == (u32)-1) {
685                 delete ore;
686                 return 0;
687         }
688
689         NodeResolveInfo *nri = new NodeResolveInfo(ore);
690         nri->nodenames.push_back(getstringfield_default(L, index, "ore", ""));
691
692         std::vector<const char *> wherein_names;
693         getstringlistfield(L, index, "wherein", wherein_names);
694         nri->nodelistinfo.push_back(NodeListInfo(wherein_names.size()));
695         for (size_t i = 0; i != wherein_names.size(); i++)
696                 nri->nodenames.push_back(wherein_names[i]);
697
698         ndef->pendNodeResolve(nri);
699
700         verbosestream << "register_ore: " << ore->name << std::endl;
701
702         lua_pushinteger(L, id);
703         return 1;
704 }
705
706 // create_schematic(p1, p2, probability_list, filename)
707 int ModApiMapgen::l_create_schematic(lua_State *L)
708 {
709         Schematic schem;
710
711         Map *map = &(getEnv(L)->getMap());
712         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
713
714         v3s16 p1 = read_v3s16(L, 1);
715         v3s16 p2 = read_v3s16(L, 2);
716         sortBoxVerticies(p1, p2);
717
718         std::vector<std::pair<v3s16, u8> > prob_list;
719         if (lua_istable(L, 3)) {
720                 lua_pushnil(L);
721                 while (lua_next(L, 3)) {
722                         if (lua_istable(L, -1)) {
723                                 lua_getfield(L, -1, "pos");
724                                 v3s16 pos = read_v3s16(L, -1);
725                                 lua_pop(L, 1);
726
727                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
728                                 prob_list.push_back(std::make_pair(pos, prob));
729                         }
730
731                         lua_pop(L, 1);
732                 }
733         }
734
735         std::vector<std::pair<s16, u8> > slice_prob_list;
736         if (lua_istable(L, 5)) {
737                 lua_pushnil(L);
738                 while (lua_next(L, 5)) {
739                         if (lua_istable(L, -1)) {
740                                 s16 ypos = getintfield_default(L, -1, "ypos", 0);
741                                 u8 prob  = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
742                                 slice_prob_list.push_back(std::make_pair(ypos, prob));
743                         }
744
745                         lua_pop(L, 1);
746                 }
747         }
748
749         const char *filename = luaL_checkstring(L, 4);
750
751         if (!schem.getSchematicFromMap(map, p1, p2)) {
752                 errorstream << "create_schematic: failed to get schematic "
753                         "from map" << std::endl;
754                 return 0;
755         }
756
757         schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
758
759         schem.saveSchematicToFile(filename, ndef);
760         actionstream << "create_schematic: saved schematic file '"
761                 << filename << "'." << std::endl;
762
763         return 1;
764 }
765
766 // place_schematic(p, schematic, rotation, replacement)
767 int ModApiMapgen::l_place_schematic(lua_State *L)
768 {
769         Schematic schem;
770
771         Map *map = &(getEnv(L)->getMap());
772         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
773
774         //// Read position
775         v3s16 p = read_v3s16(L, 1);
776
777         //// Read rotation
778         int rot = ROTATE_0;
779         if (lua_isstring(L, 3))
780                 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
781
782         //// Read force placement
783         bool force_placement = true;
784         if (lua_isboolean(L, 5))
785                 force_placement = lua_toboolean(L, 5);
786
787         //// Read node replacements
788         std::map<std::string, std::string> replace_names;
789         if (lua_istable(L, 4))
790                 read_schematic_replacements(L, replace_names, 4);
791
792         //// Read schematic
793         if (!get_schematic(L, 2, &schem, ndef, replace_names)) {
794                 errorstream << "place_schematic: failed to get schematic" << std::endl;
795                 return 0;
796         }
797
798         schem.placeStructure(map, p, 0, (Rotation)rot, force_placement, ndef);
799
800         return 1;
801 }
802
803 void ModApiMapgen::Initialize(lua_State *L, int top)
804 {
805         API_FCT(get_mapgen_object);
806
807         API_FCT(set_mapgen_params);
808         API_FCT(set_noiseparams);
809         API_FCT(set_gen_notify);
810
811         API_FCT(register_biome);
812         API_FCT(register_decoration);
813         API_FCT(register_ore);
814
815         API_FCT(clear_registered_biomes);
816         API_FCT(clear_registered_decorations);
817         API_FCT(clear_registered_ores);
818
819         API_FCT(create_schematic);
820         API_FCT(place_schematic);
821 }