]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mapgen.cpp
Noise: Create a deep copy of NoiseParams
[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 static void read_schematic_replacements(lua_State *L,
89         std::map<std::string, std::string> &replace_names, int index)
90 {
91         lua_pushnil(L);
92         while (lua_next(L, index)) {
93                 std::string replace_from;
94                 std::string replace_to;
95
96                 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
97                         lua_rawgeti(L, -1, 1);
98                         replace_from = lua_tostring(L, -1);
99                         lua_pop(L, 1);
100
101                         lua_rawgeti(L, -1, 2);
102                         replace_to = lua_tostring(L, -1);
103                         lua_pop(L, 1);
104                 } else { // New {x = "y", ...} format
105                         replace_from = lua_tostring(L, -2);
106                         replace_to = lua_tostring(L, -1);
107                 }
108
109                 replace_names[replace_from] = replace_to;
110                 lua_pop(L, 1);
111         }
112 }
113
114
115 // get_mapgen_object(objectname)
116 // returns the requested object used during map generation
117 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
118 {
119         const char *mgobjstr = lua_tostring(L, 1);
120
121         int mgobjint;
122         if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
123                 return 0;
124
125         enum MapgenObject mgobj = (MapgenObject)mgobjint;
126
127         EmergeManager *emerge = getServer(L)->getEmergeManager();
128         Mapgen *mg = emerge->getCurrentMapgen();
129         if (!mg)
130                 return 0;
131
132         size_t maplen = mg->csize.X * mg->csize.Z;
133
134         switch (mgobj) {
135                 case MGOBJ_VMANIP: {
136                         ManualMapVoxelManipulator *vm = mg->vm;
137
138                         // VoxelManip object
139                         LuaVoxelManip *o = new LuaVoxelManip(vm, true);
140                         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
141                         luaL_getmetatable(L, "VoxelManip");
142                         lua_setmetatable(L, -2);
143
144                         // emerged min pos
145                         push_v3s16(L, vm->m_area.MinEdge);
146
147                         // emerged max pos
148                         push_v3s16(L, vm->m_area.MaxEdge);
149
150                         return 3;
151                 }
152                 case MGOBJ_HEIGHTMAP: {
153                         if (!mg->heightmap)
154                                 return 0;
155
156                         lua_newtable(L);
157                         for (size_t i = 0; i != maplen; i++) {
158                                 lua_pushinteger(L, mg->heightmap[i]);
159                                 lua_rawseti(L, -2, i + 1);
160                         }
161
162                         return 1;
163                 }
164                 case MGOBJ_BIOMEMAP: {
165                         if (!mg->biomemap)
166                                 return 0;
167
168                         lua_newtable(L);
169                         for (size_t i = 0; i != maplen; i++) {
170                                 lua_pushinteger(L, mg->biomemap[i]);
171                                 lua_rawseti(L, -2, i + 1);
172                         }
173
174                         return 1;
175                 }
176                 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
177                 case MGOBJ_HUMIDMAP:
178                         if (strcmp(emerge->params.mg_name.c_str(), "v7"))
179                                 return 0;
180
181                         MapgenV7 *mgv7 = (MapgenV7 *)mg;
182
183                         float *arr = (mgobj == MGOBJ_HEATMAP) ?
184                                 mgv7->noise_heat->result : mgv7->noise_humidity->result;
185                         if (!arr)
186                                 return 0;
187
188                         lua_newtable(L);
189                         for (size_t i = 0; i != maplen; i++) {
190                                 lua_pushnumber(L, arr[i]);
191                                 lua_rawseti(L, -2, i + 1);
192                         }
193
194                         return 1;
195                 }
196                 case MGOBJ_GENNOTIFY: {
197                         std::map<std::string, std::vector<v3s16> >event_map;
198                         std::map<std::string, std::vector<v3s16> >::iterator it;
199
200                         mg->gennotify.getEvents(event_map);
201
202                         lua_newtable(L);
203                         for (it = event_map.begin(); it != event_map.end(); ++it) {
204                                 lua_newtable(L);
205
206                                 for (size_t j = 0; j != it->second.size(); j++) {
207                                         push_v3s16(L, it->second[j]);
208                                         lua_rawseti(L, -2, j + 1);
209                                 }
210
211                                 lua_setfield(L, -2, it->first.c_str());
212                         }
213
214                         return 1;
215                 }
216         }
217
218         return 0;
219 }
220
221 // set_mapgen_params(params)
222 // set mapgen parameters
223 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
224 {
225         if (!lua_istable(L, 1))
226                 return 0;
227
228         EmergeManager *emerge = getServer(L)->getEmergeManager();
229         assert(emerge);
230
231         std::string flagstr;
232         u32 flags = 0, flagmask = 0;
233
234         lua_getfield(L, 1, "mgname");
235         if (lua_isstring(L, -1)) {
236                 emerge->params.mg_name = std::string(lua_tostring(L, -1));
237                 delete emerge->params.sparams;
238                 emerge->params.sparams = NULL;
239         }
240
241         lua_getfield(L, 1, "seed");
242         if (lua_isnumber(L, -1))
243                 emerge->params.seed = lua_tointeger(L, -1);
244
245         lua_getfield(L, 1, "water_level");
246         if (lua_isnumber(L, -1))
247                 emerge->params.water_level = lua_tointeger(L, -1);
248
249         lua_getfield(L, 1, "flagmask");
250         if (lua_isstring(L, -1)) {
251                 flagstr = lua_tostring(L, -1);
252                 emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL);
253                 errorstream << "set_mapgen_params(): flagmask field is deprecated, "
254                         "see lua_api.txt" << std::endl;
255         }
256
257         if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
258                 emerge->params.flags &= ~flagmask;
259                 emerge->params.flags |= flags;
260         }
261
262         return 0;
263 }
264
265 // set_noiseparams(name, noiseparams, set_default)
266 // set global config values for noise parameters
267 int ModApiMapgen::l_set_noiseparams(lua_State *L)
268 {
269         const char *name = luaL_checkstring(L, 1);
270
271         NoiseParams np;
272         if (!read_noiseparams(L, 2, &np))
273                 return 0;
274
275         bool set_default = lua_isboolean(L, 3) ? lua_toboolean(L, 3) : true;
276
277         g_settings->setNoiseParams(name, np, set_default);
278
279         return 0;
280 }
281
282 // set_gen_notify(flags, {deco_id_table})
283 int ModApiMapgen::l_set_gen_notify(lua_State *L)
284 {
285         u32 flags = 0, flagmask = 0;
286         EmergeManager *emerge = getServer(L)->getEmergeManager();
287
288         if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
289                 emerge->gen_notify_on &= ~flagmask;
290                 emerge->gen_notify_on |= flags;
291         }
292
293         if (lua_istable(L, 2)) {
294                 lua_pushnil(L);
295                 while (lua_next(L, 2)) {
296                         if (lua_isnumber(L, -1))
297                                 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
298                         lua_pop(L, 1);
299                 }
300         }
301
302         return 0;
303 }
304
305 // register_biome({lots of stuff})
306 int ModApiMapgen::l_register_biome(lua_State *L)
307 {
308         int index = 1;
309         luaL_checktype(L, index, LUA_TTABLE);
310
311         NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
312         BiomeManager *bmgr     = getServer(L)->getEmergeManager()->biomemgr;
313
314         enum BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
315                 es_BiomeTerrainType, BIOME_TYPE_NORMAL);
316         Biome *b = bmgr->create(biometype);
317
318         b->name           = getstringfield_default(L, index, "name", "");
319         b->depth_top      = getintfield_default(L, index, "depth_top",    1);
320         b->depth_filler   = getintfield_default(L, index, "depth_filler", 3);
321         b->height_min     = getintfield_default(L, index, "height_min",   0);
322         b->height_max     = getintfield_default(L, index, "height_max",   0);
323         b->heat_point     = getfloatfield_default(L, index, "heat_point",     0.);
324         b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
325         b->flags          = 0; //reserved
326
327         u32 id = bmgr->add(b);
328         if (id == (u32)-1) {
329                 delete b;
330                 return 0;
331         }
332
333         // Pend node resolutions only if insertion succeeded
334         resolver->addNode(getstringfield_default(L, index, "node_top", ""),
335                  "mapgen_dirt_with_grass", CONTENT_AIR, &b->c_top);
336         resolver->addNode(getstringfield_default(L, index, "node_filler", ""),
337                 "mapgen_dirt", CONTENT_AIR, &b->c_filler);
338         resolver->addNode(getstringfield_default(L, index, "node_stone", ""),
339                 "mapgen_stone", CONTENT_AIR, &b->c_stone);
340         resolver->addNode(getstringfield_default(L, index, "node_water", ""),
341                 "mapgen_water_source", CONTENT_AIR, &b->c_water);
342         resolver->addNode(getstringfield_default(L, index, "node_dust", ""),
343                 "air", CONTENT_IGNORE, &b->c_dust);
344         resolver->addNode(getstringfield_default(L, index, "node_dust_water", ""),
345                 "mapgen_water_source", CONTENT_IGNORE, &b->c_dust_water);
346
347         verbosestream << "register_biome: " << b->name << std::endl;
348
349         lua_pushinteger(L, id);
350         return 1;
351 }
352
353 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
354 {
355         BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
356         bmgr->clear();
357         return 0;
358 }
359
360 // register_decoration({lots of stuff})
361 int ModApiMapgen::l_register_decoration(lua_State *L)
362 {
363         int index = 1;
364         luaL_checktype(L, index, LUA_TTABLE);
365
366         INodeDefManager *ndef      = getServer(L)->getNodeDefManager();
367         NodeResolver *resolver     = getServer(L)->getNodeDefManager()->getResolver();
368         DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
369         BiomeManager *biomemgr     = getServer(L)->getEmergeManager()->biomemgr;
370
371         enum DecorationType decotype = (DecorationType)getenumfield(L, index,
372                                 "deco_type", es_DecorationType, -1);
373
374         Decoration *deco = decomgr->create(decotype);
375         if (!deco) {
376                 errorstream << "register_decoration: decoration placement type "
377                         << decotype << " not implemented";
378                 return 0;
379         }
380
381         deco->name       = getstringfield_default(L, index, "name", "");
382         deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
383         deco->sidelen    = getintfield_default(L, index, "sidelen", 8);
384         if (deco->sidelen <= 0) {
385                 errorstream << "register_decoration: sidelen must be "
386                         "greater than 0" << std::endl;
387                 delete deco;
388                 return 0;
389         }
390
391         //// Get node name(s) to place decoration on
392         std::vector<const char *> place_on_names;
393         getstringlistfield(L, index, "place_on", place_on_names);
394         for (size_t i = 0; i != place_on_names.size(); i++)
395                 resolver->addNodeList(place_on_names[i], &deco->c_place_on);
396
397         //// Get NoiseParams to define how decoration is placed
398         lua_getfield(L, index, "noise_params");
399         deco->np = get_noiseparams(L, -1);
400         lua_pop(L, 1);
401
402         //// Get biomes associated with this decoration (if any)
403         std::vector<const char *> biome_list;
404         getstringlistfield(L, index, "biomes", biome_list);
405         for (size_t i = 0; i != biome_list.size(); i++) {
406                 Biome *b = (Biome *)biomemgr->getByName(biome_list[i]);
407                 if (!b)
408                         continue;
409
410                 deco->biomes.insert(b->id);
411         }
412
413         //// Handle decoration type-specific parameters
414         bool success = false;
415         switch (decotype) {
416                 case DECO_SIMPLE:
417                         success = regDecoSimple(L, resolver, (DecoSimple *)deco);
418                         break;
419                 case DECO_SCHEMATIC:
420                         success = regDecoSchematic(L, ndef, (DecoSchematic *)deco);
421                         break;
422                 case DECO_LSYSTEM:
423                         break;
424         }
425
426         if (!success) {
427                 delete deco;
428                 return 0;
429         }
430
431         u32 id = decomgr->add(deco);
432         if (id == (u32)-1) {
433                 delete deco;
434                 return 0;
435         }
436
437         lua_pushinteger(L, id);
438         return 1;
439 }
440
441 bool ModApiMapgen::regDecoSimple(lua_State *L,
442                 NodeResolver *resolver, DecoSimple *deco)
443 {
444         int index = 1;
445
446         deco->deco_height     = getintfield_default(L, index, "height", 1);
447         deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
448         deco->nspawnby        = getintfield_default(L, index, "num_spawn_by", -1);
449
450         if (deco->deco_height <= 0) {
451                 errorstream << "register_decoration: simple decoration height"
452                         " must be greater than 0" << std::endl;
453                 return false;
454         }
455
456         std::vector<const char *> deco_names;
457         getstringlistfield(L, index, "decoration", deco_names);
458         if (deco_names.size() == 0) {
459                 errorstream << "register_decoration: no decoration nodes "
460                         "defined" << std::endl;
461                 return false;
462         }
463
464         std::vector<const char *> spawnby_names;
465         getstringlistfield(L, index, "spawn_by", spawnby_names);
466         if (deco->nspawnby != -1 && spawnby_names.size() == 0) {
467                 errorstream << "register_decoration: no spawn_by nodes defined,"
468                         " but num_spawn_by specified" << std::endl;
469                 return false;
470         }
471
472         for (size_t i = 0; i != deco_names.size(); i++)
473                 resolver->addNodeList(deco_names[i], &deco->c_decos);
474         for (size_t i = 0; i != spawnby_names.size(); i++)
475                 resolver->addNodeList(spawnby_names[i], &deco->c_spawnby);
476
477         return true;
478 }
479
480 bool ModApiMapgen::regDecoSchematic(lua_State *L, INodeDefManager *ndef,
481         DecoSchematic *deco)
482 {
483         int index = 1;
484
485         deco->flags = 0;
486         getflagsfield(L, index, "flags", flagdesc_deco_schematic, &deco->flags, NULL);
487
488         deco->rotation = (Rotation)getenumfield(L, index, "rotation",
489                 es_Rotation, ROTATE_0);
490
491         std::map<std::string, std::string> replace_names;
492         lua_getfield(L, index, "replacements");
493         if (lua_istable(L, -1))
494                 read_schematic_replacements(L, replace_names, lua_gettop(L));
495         lua_pop(L, 1);
496
497         Schematic *schem = new Schematic;
498         lua_getfield(L, index, "schematic");
499         if (!get_schematic(L, -1, schem, ndef, replace_names)) {
500                 lua_pop(L, 1);
501                 delete schem;
502                 return false;
503         }
504         lua_pop(L, 1);
505
506         deco->schematic = schem;
507
508         return true;
509 }
510
511 // register_ore({lots of stuff})
512 int ModApiMapgen::l_register_ore(lua_State *L)
513 {
514         int index = 1;
515         luaL_checktype(L, index, LUA_TTABLE);
516
517         NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
518         OreManager *oremgr     = getServer(L)->getEmergeManager()->oremgr;
519
520         enum OreType oretype = (OreType)getenumfield(L, index,
521                                 "ore_type", es_OreType, ORE_SCATTER);
522         Ore *ore = oremgr->create(oretype);
523         if (!ore) {
524                 errorstream << "register_ore: ore_type " << oretype << " not implemented";
525                 return 0;
526         }
527
528         ore->name           = getstringfield_default(L, index, "name", "");
529         ore->ore_param2     = (u8)getintfield_default(L, index, "ore_param2", 0);
530         ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
531         ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
532         ore->clust_size     = getintfield_default(L, index, "clust_size", 0);
533         ore->height_min     = getintfield_default(L, index, "height_min", 0);
534         ore->height_max     = getintfield_default(L, index, "height_max", 0);
535         ore->nthresh        = getfloatfield_default(L, index, "noise_threshhold", 0);
536         ore->noise          = NULL;
537         ore->flags          = 0;
538
539         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
540                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
541                         "must be greater than 0" << std::endl;
542                 delete ore;
543                 return 0;
544         }
545
546         getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
547
548         lua_getfield(L, index, "noise_params");
549         if (read_noiseparams(L, -1, &ore->np)) {
550                 ore->flags |= OREFLAG_USE_NOISE;
551         } else if (ore->NEEDS_NOISE) {
552                 errorstream << "register_ore: specified ore type requires valid "
553                         "noise parameters" << std::endl;
554                 delete ore;
555                 return 0;
556         }
557         lua_pop(L, 1);
558
559         u32 id = oremgr->add(ore);
560         if (id == (u32)-1) {
561                 delete ore;
562                 return 0;
563         }
564
565         std::vector<const char *> wherein_names;
566         getstringlistfield(L, index, "wherein", wherein_names);
567         for (size_t i = 0; i != wherein_names.size(); i++)
568                 resolver->addNodeList(wherein_names[i], &ore->c_wherein);
569
570         resolver->addNode(getstringfield_default(L, index, "ore", ""),
571                 "", CONTENT_AIR, &ore->c_ore);
572
573         lua_pushinteger(L, id);
574         return 1;
575 }
576
577 // create_schematic(p1, p2, probability_list, filename)
578 int ModApiMapgen::l_create_schematic(lua_State *L)
579 {
580         Schematic schem;
581
582         Map *map = &(getEnv(L)->getMap());
583         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
584
585         v3s16 p1 = read_v3s16(L, 1);
586         v3s16 p2 = read_v3s16(L, 2);
587         sortBoxVerticies(p1, p2);
588
589         std::vector<std::pair<v3s16, u8> > prob_list;
590         if (lua_istable(L, 3)) {
591                 lua_pushnil(L);
592                 while (lua_next(L, 3)) {
593                         if (lua_istable(L, -1)) {
594                                 lua_getfield(L, -1, "pos");
595                                 v3s16 pos = read_v3s16(L, -1);
596                                 lua_pop(L, 1);
597
598                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
599                                 prob_list.push_back(std::make_pair(pos, prob));
600                         }
601
602                         lua_pop(L, 1);
603                 }
604         }
605
606         std::vector<std::pair<s16, u8> > slice_prob_list;
607         if (lua_istable(L, 5)) {
608                 lua_pushnil(L);
609                 while (lua_next(L, 5)) {
610                         if (lua_istable(L, -1)) {
611                                 s16 ypos = getintfield_default(L, -1, "ypos", 0);
612                                 u8 prob  = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
613                                 slice_prob_list.push_back(std::make_pair(ypos, prob));
614                         }
615
616                         lua_pop(L, 1);
617                 }
618         }
619
620         const char *filename = luaL_checkstring(L, 4);
621
622         if (!schem.getSchematicFromMap(map, p1, p2)) {
623                 errorstream << "create_schematic: failed to get schematic "
624                         "from map" << std::endl;
625                 return 0;
626         }
627
628         schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
629
630         schem.saveSchematicToFile(filename, ndef);
631         actionstream << "create_schematic: saved schematic file '"
632                 << filename << "'." << std::endl;
633
634         return 1;
635 }
636
637 // place_schematic(p, schematic, rotation, replacement)
638 int ModApiMapgen::l_place_schematic(lua_State *L)
639 {
640         Schematic schem;
641
642         Map *map = &(getEnv(L)->getMap());
643         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
644
645         //// Read position
646         v3s16 p = read_v3s16(L, 1);
647
648         //// Read rotation
649         int rot = ROTATE_0;
650         if (lua_isstring(L, 3))
651                 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
652
653         //// Read force placement
654         bool force_placement = true;
655         if (lua_isboolean(L, 5))
656                 force_placement = lua_toboolean(L, 5);
657
658         //// Read node replacements
659         std::map<std::string, std::string> replace_names;
660         if (lua_istable(L, 4))
661                 read_schematic_replacements(L, replace_names, 4);
662
663         //// Read schematic
664         if (!get_schematic(L, 2, &schem, ndef, replace_names)) {
665                 errorstream << "place_schematic: failed to get schematic" << std::endl;
666                 return 0;
667         }
668
669         schem.placeStructure(map, p, 0, (Rotation)rot, force_placement, ndef);
670
671         return 1;
672 }
673
674 void ModApiMapgen::Initialize(lua_State *L, int top)
675 {
676         API_FCT(get_mapgen_object);
677
678         API_FCT(set_mapgen_params);
679         API_FCT(set_noiseparams);
680         API_FCT(set_gen_notify);
681
682         API_FCT(register_biome);
683         API_FCT(register_decoration);
684         API_FCT(register_ore);
685         API_FCT(clear_registered_biomes);
686
687         API_FCT(create_schematic);
688         API_FCT(place_schematic);
689 }