3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_mapgen.h"
21 #include "lua_api/l_internal.h"
22 #include "lua_api/l_vmanip.h"
23 #include "common/c_converter.h"
24 #include "common/c_content.h"
25 #include "util/serialize.h"
27 #include "environment.h"
31 #include "mg_decoration.h"
32 #include "mg_schematic.h"
33 #include "mapgen_v5.h"
34 #include "mapgen_v7.h"
40 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
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"},
50 struct EnumString ModApiMapgen::es_DecorationType[] =
52 {DECO_SIMPLE, "simple"},
53 {DECO_SCHEMATIC, "schematic"},
54 {DECO_LSYSTEM, "lsystem"},
58 struct EnumString ModApiMapgen::es_MapgenObject[] =
60 {MGOBJ_VMANIP, "voxelmanip"},
61 {MGOBJ_HEIGHTMAP, "heightmap"},
62 {MGOBJ_BIOMEMAP, "biomemap"},
63 {MGOBJ_HEATMAP, "heatmap"},
64 {MGOBJ_HUMIDMAP, "humiditymap"},
65 {MGOBJ_GENNOTIFY, "gennotify"},
69 struct EnumString ModApiMapgen::es_OreType[] =
71 {ORE_SCATTER, "scatter"},
73 {ORE_CLAYLIKE, "claylike"},
77 struct EnumString ModApiMapgen::es_Rotation[] =
83 {ROTATE_RAND, "random"},
88 static void read_schematic_replacements(lua_State *L,
89 std::map<std::string, std::string> &replace_names, int index)
92 while (lua_next(L, index)) {
93 std::string replace_from;
94 std::string replace_to;
96 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
97 lua_rawgeti(L, -1, 1);
98 replace_from = lua_tostring(L, -1);
101 lua_rawgeti(L, -1, 2);
102 replace_to = lua_tostring(L, -1);
104 } else { // New {x = "y", ...} format
105 replace_from = lua_tostring(L, -2);
106 replace_to = lua_tostring(L, -1);
109 replace_names[replace_from] = replace_to;
115 // get_mapgen_object(objectname)
116 // returns the requested object used during map generation
117 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
119 const char *mgobjstr = lua_tostring(L, 1);
122 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
125 enum MapgenObject mgobj = (MapgenObject)mgobjint;
127 EmergeManager *emerge = getServer(L)->getEmergeManager();
128 Mapgen *mg = emerge->getCurrentMapgen();
132 size_t maplen = mg->csize.X * mg->csize.Z;
136 ManualMapVoxelManipulator *vm = mg->vm;
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);
145 push_v3s16(L, vm->m_area.MinEdge);
148 push_v3s16(L, vm->m_area.MaxEdge);
152 case MGOBJ_HEIGHTMAP: {
157 for (size_t i = 0; i != maplen; i++) {
158 lua_pushinteger(L, mg->heightmap[i]);
159 lua_rawseti(L, -2, i + 1);
164 case MGOBJ_BIOMEMAP: {
169 for (size_t i = 0; i != maplen; i++) {
170 lua_pushinteger(L, mg->biomemap[i]);
171 lua_rawseti(L, -2, i + 1);
176 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
178 if (strcmp(emerge->params.mg_name.c_str(), "v7"))
181 MapgenV7 *mgv7 = (MapgenV7 *)mg;
183 float *arr = (mgobj == MGOBJ_HEATMAP) ?
184 mgv7->noise_heat->result : mgv7->noise_humidity->result;
189 for (size_t i = 0; i != maplen; i++) {
190 lua_pushnumber(L, arr[i]);
191 lua_rawseti(L, -2, i + 1);
196 case MGOBJ_GENNOTIFY: {
197 std::map<std::string, std::vector<v3s16> >event_map;
198 std::map<std::string, std::vector<v3s16> >::iterator it;
200 mg->gennotify.getEvents(event_map);
203 for (it = event_map.begin(); it != event_map.end(); ++it) {
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);
211 lua_setfield(L, -2, it->first.c_str());
221 // set_mapgen_params(params)
222 // set mapgen parameters
223 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
225 if (!lua_istable(L, 1))
228 EmergeManager *emerge = getServer(L)->getEmergeManager();
232 u32 flags = 0, flagmask = 0;
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;
241 lua_getfield(L, 1, "seed");
242 if (lua_isnumber(L, -1))
243 emerge->params.seed = lua_tointeger(L, -1);
245 lua_getfield(L, 1, "water_level");
246 if (lua_isnumber(L, -1))
247 emerge->params.water_level = lua_tointeger(L, -1);
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;
257 if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
258 emerge->params.flags &= ~flagmask;
259 emerge->params.flags |= flags;
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)
270 std::string val, name;
272 if (!lua_istable(L, 1))
276 while (lua_next(L, 1)) {
277 if (read_noiseparams_nc(L, -1, &np)) {
278 if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
280 if (!lua_isstring(L, -2))
283 name = lua_tostring(L, -2);
284 g_settings->setDefault(name, val);
292 // set_gen_notify(flags, {deco_id_table})
293 int ModApiMapgen::l_set_gen_notify(lua_State *L)
295 u32 flags = 0, flagmask = 0;
296 EmergeManager *emerge = getServer(L)->getEmergeManager();
298 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
299 emerge->gen_notify_on &= ~flagmask;
300 emerge->gen_notify_on |= flags;
303 if (lua_istable(L, 2)) {
305 while (lua_next(L, 2)) {
306 if (lua_isnumber(L, -1))
307 emerge->gen_notify_on_deco_ids.insert(lua_tonumber(L, -1));
315 // register_biome({lots of stuff})
316 int ModApiMapgen::l_register_biome(lua_State *L)
319 luaL_checktype(L, index, LUA_TTABLE);
321 NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
322 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
324 enum BiomeType biometype = (BiomeType)getenumfield(L, index, "type",
325 es_BiomeTerrainType, BIOME_TYPE_NORMAL);
326 Biome *b = bmgr->create(biometype);
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
337 u32 id = bmgr->add(b);
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);
357 verbosestream << "register_biome: " << b->name << std::endl;
359 lua_pushinteger(L, id);
363 int ModApiMapgen::l_clear_registered_biomes(lua_State *L)
365 BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr;
370 // register_decoration({lots of stuff})
371 int ModApiMapgen::l_register_decoration(lua_State *L)
374 luaL_checktype(L, index, LUA_TTABLE);
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;
381 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
382 "deco_type", es_DecorationType, -1);
384 Decoration *deco = decomgr->create(decotype);
386 errorstream << "register_decoration: decoration placement type "
387 << decotype << " not implemented";
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;
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);
407 //// Get NoiseParams to define how decoration is placed
408 lua_getfield(L, index, "noise_params");
409 deco->np = read_noiseparams(L, -1);
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]);
420 deco->biomes.insert(b->id);
423 //// Handle decoration type-specific parameters
424 bool success = false;
427 success = regDecoSimple(L, resolver, (DecoSimple *)deco);
430 success = regDecoSchematic(L, ndef, (DecoSchematic *)deco);
441 u32 id = decomgr->add(deco);
447 lua_pushinteger(L, id);
451 bool ModApiMapgen::regDecoSimple(lua_State *L,
452 NodeResolver *resolver, DecoSimple *deco)
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);
460 if (deco->deco_height <= 0) {
461 errorstream << "register_decoration: simple decoration height"
462 " must be greater than 0" << std::endl;
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;
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;
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);
490 bool ModApiMapgen::regDecoSchematic(lua_State *L, INodeDefManager *ndef,
496 getflagsfield(L, index, "flags", flagdesc_deco_schematic, &deco->flags, NULL);
498 deco->rotation = (Rotation)getenumfield(L, index, "rotation",
499 es_Rotation, ROTATE_0);
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));
507 Schematic *schem = new Schematic;
508 lua_getfield(L, index, "schematic");
509 if (!get_schematic(L, -1, schem, ndef, replace_names)) {
516 deco->schematic = schem;
521 // register_ore({lots of stuff})
522 int ModApiMapgen::l_register_ore(lua_State *L)
525 luaL_checktype(L, index, LUA_TTABLE);
527 NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver();
528 OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr;
530 enum OreType oretype = (OreType)getenumfield(L, index,
531 "ore_type", es_OreType, ORE_SCATTER);
532 Ore *ore = oremgr->create(oretype);
534 errorstream << "register_ore: ore_type " << oretype << " not implemented";
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);
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;
556 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
558 lua_getfield(L, index, "noise_params");
559 ore->np = read_noiseparams(L, -1);
562 u32 id = oremgr->add(ore);
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);
573 resolver->addNode(getstringfield_default(L, index, "ore", ""),
574 "", CONTENT_AIR, &ore->c_ore);
576 lua_pushinteger(L, id);
580 // create_schematic(p1, p2, probability_list, filename)
581 int ModApiMapgen::l_create_schematic(lua_State *L)
585 Map *map = &(getEnv(L)->getMap());
586 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
588 v3s16 p1 = read_v3s16(L, 1);
589 v3s16 p2 = read_v3s16(L, 2);
590 sortBoxVerticies(p1, p2);
592 std::vector<std::pair<v3s16, u8> > prob_list;
593 if (lua_istable(L, 3)) {
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);
601 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
602 prob_list.push_back(std::make_pair(pos, prob));
609 std::vector<std::pair<s16, u8> > slice_prob_list;
610 if (lua_istable(L, 5)) {
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));
623 const char *filename = luaL_checkstring(L, 4);
625 if (!schem.getSchematicFromMap(map, p1, p2)) {
626 errorstream << "create_schematic: failed to get schematic "
627 "from map" << std::endl;
631 schem.applyProbabilities(p1, &prob_list, &slice_prob_list);
633 schem.saveSchematicToFile(filename, ndef);
634 actionstream << "create_schematic: saved schematic file '"
635 << filename << "'." << std::endl;
640 // place_schematic(p, schematic, rotation, replacement)
641 int ModApiMapgen::l_place_schematic(lua_State *L)
645 Map *map = &(getEnv(L)->getMap());
646 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
649 v3s16 p = read_v3s16(L, 1);
653 if (lua_isstring(L, 3))
654 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
656 //// Read force placement
657 bool force_placement = true;
658 if (lua_isboolean(L, 5))
659 force_placement = lua_toboolean(L, 5);
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);
667 if (!get_schematic(L, 2, &schem, ndef, replace_names)) {
668 errorstream << "place_schematic: failed to get schematic" << std::endl;
672 schem.placeStructure(map, p, 0, (Rotation)rot, force_placement, ndef);
677 void ModApiMapgen::Initialize(lua_State *L, int top)
679 API_FCT(get_mapgen_object);
681 API_FCT(set_mapgen_params);
682 API_FCT(set_noiseparam_defaults);
683 API_FCT(set_gen_notify);
685 API_FCT(register_biome);
686 API_FCT(register_decoration);
687 API_FCT(register_ore);
688 API_FCT(clear_registered_biomes);
690 API_FCT(create_schematic);
691 API_FCT(place_schematic);