]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mapgen.cpp
Add minetest.clear_registered_biomes() api
[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_noiseparam_defaults({np1={noise params}, ...})
266 // set default values for noise parameters if not present in global settings
267 int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L)
268 {
269         NoiseParams np;
270         std::string val, name;
271
272         if (!lua_istable(L, 1))
273                 return 0;
274
275         lua_pushnil(L);
276         while (lua_next(L, 1)) {
277                 if (read_noiseparams_nc(L, -1, &np)) {
278                         if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
279                                 continue;
280                         if (!lua_isstring(L, -2))
281                                 continue;
282
283                         name = lua_tostring(L, -2);
284                         g_settings->setDefault(name, val);
285                 }
286                 lua_pop(L, 1);
287         }
288
289         return 0;
290 }
291
292 // set_gen_notify(flags, {deco_id_table})
293 int ModApiMapgen::l_set_gen_notify(lua_State *L)
294 {
295         u32 flags = 0, flagmask = 0;
296         EmergeManager *emerge = getServer(L)->getEmergeManager();
297
298         if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
299                 emerge->gen_notify_on &= ~flagmask;
300                 emerge->gen_notify_on |= flags;
301         }
302
303         if (lua_istable(L, 2)) {
304                 lua_pushnil(L);
305                 while (lua_next(L, 2)) {
306                         if (lua_isnumber(L, -1))
307                                 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
308                         lua_pop(L, 1);
309                 }
310         }
311
312         return 0;
313 }
314
315 // register_biome({lots of stuff})
316 int ModApiMapgen::l_register_biome(lua_State *L)
317 {
318         int index = 1;
319         luaL_checktype(L, index, LUA_TTABLE);
320
321         NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
322         BiomeManager *bmgr     = getServer(L)->getEmergeManager()->biomemgr;
323
324         enum BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
325                 es_BiomeTerrainType, BIOME_TYPE_NORMAL);
326         Biome *b = bmgr->create(biometype);
327
328         b->name           = getstringfield_default(L, index, "name", "");
329         b->depth_top      = getintfield_default(L, index, "depth_top",    1);
330         b->depth_filler   = getintfield_default(L, index, "depth_filler", 3);
331         b->height_min     = getintfield_default(L, index, "height_min",   0);
332         b->height_max     = getintfield_default(L, index, "height_max",   0);
333         b->heat_point     = getfloatfield_default(L, index, "heat_point",     0.);
334         b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
335         b->flags          = 0; //reserved
336
337         u32 id = bmgr->add(b);
338         if (id == (u32)-1) {
339                 delete b;
340                 return 0;
341         }
342
343         // Pend node resolutions only if insertion succeeded
344         resolver->addNode(getstringfield_default(L, index, "node_top", ""),
345                  "mapgen_dirt_with_grass", CONTENT_AIR, &b->c_top);
346         resolver->addNode(getstringfield_default(L, index, "node_filler", ""),
347                 "mapgen_dirt", CONTENT_AIR, &b->c_filler);
348         resolver->addNode(getstringfield_default(L, index, "node_stone", ""),
349                 "mapgen_stone", CONTENT_AIR, &b->c_stone);
350         resolver->addNode(getstringfield_default(L, index, "node_water", ""),
351                 "mapgen_water_source", CONTENT_AIR, &b->c_water);
352         resolver->addNode(getstringfield_default(L, index, "node_dust", ""),
353                 "air", CONTENT_IGNORE, &b->c_dust);
354         resolver->addNode(getstringfield_default(L, index, "node_dust_water", ""),
355                 "mapgen_water_source", CONTENT_IGNORE, &b->c_dust_water);
356
357         verbosestream << "register_biome: " << b->name << std::endl;
358
359         lua_pushinteger(L, id);
360         return 1;
361 }
362
363 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
364 {
365         BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
366         bmgr->clear();
367         return 0;
368 }
369
370 // register_decoration({lots of stuff})
371 int ModApiMapgen::l_register_decoration(lua_State *L)
372 {
373         int index = 1;
374         luaL_checktype(L, index, LUA_TTABLE);
375
376         INodeDefManager *ndef      = getServer(L)->getNodeDefManager();
377         NodeResolver *resolver     = getServer(L)->getNodeDefManager()->getResolver();
378         DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr;
379         BiomeManager *biomemgr     = getServer(L)->getEmergeManager()->biomemgr;
380
381         enum DecorationType decotype = (DecorationType)getenumfield(L, index,
382                                 "deco_type", es_DecorationType, -1);
383
384         Decoration *deco = decomgr->create(decotype);
385         if (!deco) {
386                 errorstream << "register_decoration: decoration placement type "
387                         << decotype << " not implemented";
388                 return 0;
389         }
390
391         deco->name       = getstringfield_default(L, index, "name", "");
392         deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
393         deco->sidelen    = getintfield_default(L, index, "sidelen", 8);
394         if (deco->sidelen <= 0) {
395                 errorstream << "register_decoration: sidelen must be "
396                         "greater than 0" << std::endl;
397                 delete deco;
398                 return 0;
399         }
400
401         //// Get node name(s) to place decoration on
402         std::vector<const char *> place_on_names;
403         getstringlistfield(L, index, "place_on", place_on_names);
404         for (size_t i = 0; i != place_on_names.size(); i++)
405                 resolver->addNodeList(place_on_names[i], &deco->c_place_on);
406
407         //// Get NoiseParams to define how decoration is placed
408         lua_getfield(L, index, "noise_params");
409         deco->np = read_noiseparams(L, -1);
410         lua_pop(L, 1);
411
412         //// Get biomes associated with this decoration (if any)
413         std::vector<const char *> biome_list;
414         getstringlistfield(L, index, "biomes", biome_list);
415         for (size_t i = 0; i != biome_list.size(); i++) {
416                 Biome *b = (Biome *)biomemgr->getByName(biome_list[i]);
417                 if (!b)
418                         continue;
419
420                 deco->biomes.insert(b->id);
421         }
422
423         //// Handle decoration type-specific parameters
424         bool success = false;
425         switch (decotype) {
426                 case DECO_SIMPLE:
427                         success = regDecoSimple(L, resolver, (DecoSimple *)deco);
428                         break;
429                 case DECO_SCHEMATIC:
430                         success = regDecoSchematic(L, ndef, (DecoSchematic *)deco);
431                         break;
432                 case DECO_LSYSTEM:
433                         break;
434         }
435
436         if (!success) {
437                 delete deco;
438                 return 0;
439         }
440
441         u32 id = decomgr->add(deco);
442         if (id == (u32)-1) {
443                 delete deco;
444                 return 0;
445         }
446
447         lua_pushinteger(L, id);
448         return 1;
449 }
450
451 bool ModApiMapgen::regDecoSimple(lua_State *L,
452                 NodeResolver *resolver, DecoSimple *deco)
453 {
454         int index = 1;
455
456         deco->deco_height     = getintfield_default(L, index, "height", 1);
457         deco->deco_height_max = getintfield_default(L, index, "height_max", 0);
458         deco->nspawnby        = getintfield_default(L, index, "num_spawn_by", -1);
459
460         if (deco->deco_height <= 0) {
461                 errorstream << "register_decoration: simple decoration height"
462                         " must be greater than 0" << std::endl;
463                 return false;
464         }
465
466         std::vector<const char *> deco_names;
467         getstringlistfield(L, index, "decoration", deco_names);
468         if (deco_names.size() == 0) {
469                 errorstream << "register_decoration: no decoration nodes "
470                         "defined" << std::endl;
471                 return false;
472         }
473
474         std::vector<const char *> spawnby_names;
475         getstringlistfield(L, index, "spawn_by", spawnby_names);
476         if (deco->nspawnby != -1 && spawnby_names.size() == 0) {
477                 errorstream << "register_decoration: no spawn_by nodes defined,"
478                         " but num_spawn_by specified" << std::endl;
479                 return false;
480         }
481
482         for (size_t i = 0; i != deco_names.size(); i++)
483                 resolver->addNodeList(deco_names[i], &deco->c_decos);
484         for (size_t i = 0; i != spawnby_names.size(); i++)
485                 resolver->addNodeList(spawnby_names[i], &deco->c_spawnby);
486
487         return true;
488 }
489
490 bool ModApiMapgen::regDecoSchematic(lua_State *L, INodeDefManager *ndef,
491         DecoSchematic *deco)
492 {
493         int index = 1;
494
495         deco->flags = 0;
496         getflagsfield(L, index, "flags", flagdesc_deco_schematic, &deco->flags, NULL);
497
498         deco->rotation = (Rotation)getenumfield(L, index, "rotation",
499                 es_Rotation, ROTATE_0);
500
501         std::map<std::string, std::string> replace_names;
502         lua_getfield(L, index, "replacements");
503         if (lua_istable(L, -1))
504                 read_schematic_replacements(L, replace_names, lua_gettop(L));
505         lua_pop(L, 1);
506
507         Schematic *schem = new Schematic;
508         lua_getfield(L, index, "schematic");
509         if (!get_schematic(L, -1, schem, ndef, replace_names)) {
510                 lua_pop(L, 1);
511                 delete schem;
512                 return false;
513         }
514         lua_pop(L, 1);
515
516         deco->schematic = schem;
517
518         return true;
519 }
520
521 // register_ore({lots of stuff})
522 int ModApiMapgen::l_register_ore(lua_State *L)
523 {
524         int index = 1;
525         luaL_checktype(L, index, LUA_TTABLE);
526
527         NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
528         OreManager *oremgr     = getServer(L)->getEmergeManager()->oremgr;
529
530         enum OreType oretype = (OreType)getenumfield(L, index,
531                                 "ore_type", es_OreType, ORE_SCATTER);
532         Ore *ore = oremgr->create(oretype);
533         if (!ore) {
534                 errorstream << "register_ore: ore_type " << oretype << " not implemented";
535                 return 0;
536         }
537
538         ore->name           = getstringfield_default(L, index, "name", "");
539         ore->ore_param2     = (u8)getintfield_default(L, index, "ore_param2", 0);
540         ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
541         ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
542         ore->clust_size     = getintfield_default(L, index, "clust_size", 0);
543         ore->height_min     = getintfield_default(L, index, "height_min", 0);
544         ore->height_max     = getintfield_default(L, index, "height_max", 0);
545         ore->nthresh        = getfloatfield_default(L, index, "noise_threshhold", 0);
546         ore->noise          = NULL;
547         ore->flags          = 0;
548
549         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
550                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
551                         "must be greater than 0" << std::endl;
552                 delete ore;
553                 return 0;
554         }
555
556         getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
557
558         lua_getfield(L, index, "noise_params");
559         ore->np = read_noiseparams(L, -1);
560         lua_pop(L, 1);
561
562         u32 id = oremgr->add(ore);
563         if (id == (u32)-1) {
564                 delete ore;
565                 return 0;
566         }
567
568         std::vector<const char *> wherein_names;
569         getstringlistfield(L, index, "wherein", wherein_names);
570         for (size_t i = 0; i != wherein_names.size(); i++)
571                 resolver->addNodeList(wherein_names[i], &ore->c_wherein);
572
573         resolver->addNode(getstringfield_default(L, index, "ore", ""),
574                 "", CONTENT_AIR, &ore->c_ore);
575
576         lua_pushinteger(L, id);
577         return 1;
578 }
579
580 // create_schematic(p1, p2, probability_list, filename)
581 int ModApiMapgen::l_create_schematic(lua_State *L)
582 {
583         Schematic schem;
584
585         Map *map = &(getEnv(L)->getMap());
586         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
587
588         v3s16 p1 = read_v3s16(L, 1);
589         v3s16 p2 = read_v3s16(L, 2);
590         sortBoxVerticies(p1, p2);
591
592         std::vector<std::pair<v3s16, u8> > prob_list;
593         if (lua_istable(L, 3)) {
594                 lua_pushnil(L);
595                 while (lua_next(L, 3)) {
596                         if (lua_istable(L, -1)) {
597                                 lua_getfield(L, -1, "pos");
598                                 v3s16 pos = read_v3s16(L, -1);
599                                 lua_pop(L, 1);
600
601                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
602                                 prob_list.push_back(std::make_pair(pos, prob));
603                         }
604
605                         lua_pop(L, 1);
606                 }
607         }
608
609         std::vector<std::pair<s16, u8> > slice_prob_list;
610         if (lua_istable(L, 5)) {
611                 lua_pushnil(L);
612                 while (lua_next(L, 5)) {
613                         if (lua_istable(L, -1)) {
614                                 s16 ypos = getintfield_default(L, -1, "ypos", 0);
615                                 u8 prob  = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
616                                 slice_prob_list.push_back(std::make_pair(ypos, prob));
617                         }
618
619                         lua_pop(L, 1);
620                 }
621         }
622
623         const char *filename = luaL_checkstring(L, 4);
624
625         if (!schem.getSchematicFromMap(map, p1, p2)) {
626                 errorstream << "create_schematic: failed to get schematic "
627                         "from map" << std::endl;
628                 return 0;
629         }
630
631         schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
632
633         schem.saveSchematicToFile(filename, ndef);
634         actionstream << "create_schematic: saved schematic file '"
635                 << filename << "'." << std::endl;
636
637         return 1;
638 }
639
640 // place_schematic(p, schematic, rotation, replacement)
641 int ModApiMapgen::l_place_schematic(lua_State *L)
642 {
643         Schematic schem;
644
645         Map *map = &(getEnv(L)->getMap());
646         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
647
648         //// Read position
649         v3s16 p = read_v3s16(L, 1);
650
651         //// Read rotation
652         int rot = ROTATE_0;
653         if (lua_isstring(L, 3))
654                 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
655
656         //// Read force placement
657         bool force_placement = true;
658         if (lua_isboolean(L, 5))
659                 force_placement = lua_toboolean(L, 5);
660
661         //// Read node replacements
662         std::map<std::string, std::string> replace_names;
663         if (lua_istable(L, 4))
664                 read_schematic_replacements(L, replace_names, 4);
665
666         //// Read schematic
667         if (!get_schematic(L, 2, &schem, ndef, replace_names)) {
668                 errorstream << "place_schematic: failed to get schematic" << std::endl;
669                 return 0;
670         }
671
672         schem.placeStructure(map, p, 0, (Rotation)rot, force_placement, ndef);
673
674         return 1;
675 }
676
677 void ModApiMapgen::Initialize(lua_State *L, int top)
678 {
679         API_FCT(get_mapgen_object);
680
681         API_FCT(set_mapgen_params);
682         API_FCT(set_noiseparam_defaults);
683         API_FCT(set_gen_notify);
684
685         API_FCT(register_biome);
686         API_FCT(register_decoration);
687         API_FCT(register_ore);
688         API_FCT(clear_registered_biomes);
689
690         API_FCT(create_schematic);
691         API_FCT(place_schematic);
692 }