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"
26 #include "environment.h"
29 #include "mapgen_v7.h"
33 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
35 {BIOME_TERRAIN_NORMAL, "normal"},
36 {BIOME_TERRAIN_LIQUID, "liquid"},
37 {BIOME_TERRAIN_NETHER, "nether"},
38 {BIOME_TERRAIN_AETHER, "aether"},
39 {BIOME_TERRAIN_FLAT, "flat"},
43 struct EnumString ModApiMapgen::es_DecorationType[] =
45 {DECO_SIMPLE, "simple"},
46 {DECO_SCHEMATIC, "schematic"},
47 {DECO_LSYSTEM, "lsystem"},
51 struct EnumString ModApiMapgen::es_MapgenObject[] =
53 {MGOBJ_VMANIP, "voxelmanip"},
54 {MGOBJ_HEIGHTMAP, "heightmap"},
55 {MGOBJ_BIOMEMAP, "biomemap"},
56 {MGOBJ_HEATMAP, "heatmap"},
57 {MGOBJ_HUMIDMAP, "humiditymap"},
58 {MGOBJ_GENNOTIFY, "gennotify"},
62 struct EnumString ModApiMapgen::es_OreType[] =
64 {ORE_SCATTER, "scatter"},
66 {ORE_CLAYLIKE, "claylike"},
70 struct EnumString ModApiMapgen::es_Rotation[] =
76 {ROTATE_RAND, "random"},
81 static void read_schematic_replacements(lua_State *L, DecoSchematic *dschem, int index)
84 while (lua_next(L, index)) {
85 // key at index -2 and value at index -1
86 std::string replace_from;
87 std::string replace_to;
88 if (lua_istable(L, -1)) { // Old {{"x", "y"}, ...} format
89 lua_rawgeti(L, -1, 1);
90 replace_from = lua_tostring(L, -1);
92 lua_rawgeti(L, -1, 2);
93 replace_to = lua_tostring(L, -1);
95 } else { // New {x = "y", ...} format
96 replace_from = lua_tostring(L, -2);
97 replace_to = lua_tostring(L, -1);
99 dschem->replacements[replace_from] = replace_to;
100 // removes value, keeps key for next iteration
106 // get_mapgen_object(objectname)
107 // returns the requested object used during map generation
108 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
110 const char *mgobjstr = lua_tostring(L, 1);
113 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
116 enum MapgenObject mgobj = (MapgenObject)mgobjint;
118 EmergeManager *emerge = getServer(L)->getEmergeManager();
119 Mapgen *mg = emerge->getCurrentMapgen();
123 size_t maplen = mg->csize.X * mg->csize.Z;
127 ManualMapVoxelManipulator *vm = mg->vm;
130 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
131 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
132 luaL_getmetatable(L, "VoxelManip");
133 lua_setmetatable(L, -2);
136 push_v3s16(L, vm->m_area.MinEdge);
139 push_v3s16(L, vm->m_area.MaxEdge);
142 case MGOBJ_HEIGHTMAP: {
147 for (size_t i = 0; i != maplen; i++) {
148 lua_pushinteger(L, mg->heightmap[i]);
149 lua_rawseti(L, -2, i + 1);
153 case MGOBJ_BIOMEMAP: {
158 for (size_t i = 0; i != maplen; i++) {
159 lua_pushinteger(L, mg->biomemap[i]);
160 lua_rawseti(L, -2, i + 1);
164 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
166 if (strcmp(emerge->params.mg_name.c_str(), "v7"))
169 MapgenV7 *mgv7 = (MapgenV7 *)mg;
171 float *arr = (mgobj == MGOBJ_HEATMAP) ?
172 mgv7->noise_heat->result : mgv7->noise_humidity->result;
177 for (size_t i = 0; i != maplen; i++) {
178 lua_pushnumber(L, arr[i]);
179 lua_rawseti(L, -2, i + 1);
183 case MGOBJ_GENNOTIFY: {
185 for (int i = 0; flagdesc_gennotify[i].name; i++) {
186 if (!(emerge->gennotify & flagdesc_gennotify[i].flag))
189 std::vector<v3s16> *posvec = mg->gen_notifications[i];
194 for (unsigned int j = 0; j != posvec->size(); j++) {
195 push_v3s16(L, (*posvec)[j]);
196 lua_rawseti(L, -2, j + 1);
198 lua_setfield(L, -2, flagdesc_gennotify[i].name);
209 // set_mapgen_params(params)
210 // set mapgen parameters
211 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
213 if (!lua_istable(L, 1))
216 EmergeManager *emerge = getServer(L)->getEmergeManager();
220 u32 flags = 0, flagmask = 0;
222 lua_getfield(L, 1, "mgname");
223 if (lua_isstring(L, -1)) {
224 emerge->params.mg_name = std::string(lua_tostring(L, -1));
225 delete emerge->params.sparams;
226 emerge->params.sparams = NULL;
229 lua_getfield(L, 1, "seed");
230 if (lua_isnumber(L, -1))
231 emerge->params.seed = lua_tointeger(L, -1);
233 lua_getfield(L, 1, "water_level");
234 if (lua_isnumber(L, -1))
235 emerge->params.water_level = lua_tointeger(L, -1);
237 lua_getfield(L, 1, "flagmask");
238 if (lua_isstring(L, -1)) {
239 flagstr = lua_tostring(L, -1);
240 emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL);
241 errorstream << "set_mapgen_params(): flagmask field is deprecated, "
242 "see lua_api.txt" << std::endl;
245 if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
246 emerge->params.flags &= ~flagmask;
247 emerge->params.flags |= flags;
253 // set_noiseparam_defaults({np1={noise params}, ...})
254 // set default values for noise parameters if not present in global settings
255 int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L)
258 std::string val, name;
260 if (!lua_istable(L, 1))
264 while (lua_next(L, 1)) {
265 if (read_noiseparams_nc(L, -1, &np)) {
266 if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
268 if (!lua_isstring(L, -2))
271 name = lua_tostring(L, -2);
272 g_settings->setDefault(name, val);
280 // set_gen_notify(string)
281 int ModApiMapgen::l_set_gen_notify(lua_State *L)
283 u32 flags = 0, flagmask = 0;
285 if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
286 EmergeManager *emerge = getServer(L)->getEmergeManager();
287 emerge->gennotify = flags;
293 // register_biome({lots of stuff})
294 int ModApiMapgen::l_register_biome(lua_State *L)
297 luaL_checktype(L, index, LUA_TTABLE);
299 BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
301 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
305 enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
306 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
307 Biome *b = bmgr->createBiome(terrain);
309 b->name = getstringfield_default(L, index, "name",
311 b->nname_top = getstringfield_default(L, index, "node_top",
312 "mapgen_dirt_with_grass");
313 b->nname_filler = getstringfield_default(L, index, "node_filler",
315 b->nname_water = getstringfield_default(L, index, "node_water",
316 "mapgen_water_source");
317 b->nname_dust = getstringfield_default(L, index, "node_dust",
319 b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
320 "mapgen_water_source");
322 b->depth_top = getintfield_default(L, index, "depth_top", 1);
323 b->depth_filler = getintfield_default(L, index, "depth_filler", 3);
324 b->height_min = getintfield_default(L, index, "height_min", 0);
325 b->height_max = getintfield_default(L, index, "height_max", 0);
326 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.);
327 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
329 b->flags = 0; //reserved
330 b->c_top = CONTENT_IGNORE;
331 b->c_filler = CONTENT_IGNORE;
332 b->c_water = CONTENT_IGNORE;
333 b->c_dust = CONTENT_IGNORE;
334 b->c_dust_water = CONTENT_IGNORE;
336 verbosestream << "register_biome: " << b->name << std::endl;
342 // register_decoration({lots of stuff})
343 int ModApiMapgen::l_register_decoration(lua_State *L)
346 luaL_checktype(L, index, LUA_TTABLE);
348 EmergeManager *emerge = getServer(L)->getEmergeManager();
349 BiomeDefManager *bdef = emerge->biomedef;
351 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
352 "deco_type", es_DecorationType, 0);
354 errorstream << "register_decoration: unrecognized "
355 "decoration placement type";
359 Decoration *deco = createDecoration(decotype);
361 errorstream << "register_decoration: decoration placement type "
362 << decotype << " not implemented";
366 deco->c_place_on = CONTENT_IGNORE;
367 deco->place_on_name = getstringfield_default(L, index, "place_on", "ignore");
368 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
369 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
370 if (deco->sidelen <= 0) {
371 errorstream << "register_decoration: sidelen must be "
372 "greater than 0" << std::endl;
377 lua_getfield(L, index, "noise_params");
378 deco->np = read_noiseparams(L, -1);
381 lua_getfield(L, index, "biomes");
382 if (lua_istable(L, -1)) {
384 while (lua_next(L, -2)) {
385 const char *s = lua_tostring(L, -1);
386 u8 biomeid = bdef->getBiomeIdByName(s);
388 deco->biomes.insert(biomeid);
397 DecoSimple *dsimple = (DecoSimple *)deco;
398 dsimple->c_deco = CONTENT_IGNORE;
399 dsimple->c_spawnby = CONTENT_IGNORE;
400 dsimple->spawnby_name = getstringfield_default(L, index, "spawn_by", "air");
401 dsimple->deco_height = getintfield_default(L, index, "height", 1);
402 dsimple->deco_height_max = getintfield_default(L, index, "height_max", 0);
403 dsimple->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
405 lua_getfield(L, index, "decoration");
406 if (lua_istable(L, -1)) {
408 while (lua_next(L, -2)) {
409 const char *s = lua_tostring(L, -1);
411 dsimple->decolist_names.push_back(str);
415 } else if (lua_isstring(L, -1)) {
416 dsimple->deco_name = std::string(lua_tostring(L, -1));
418 dsimple->deco_name = std::string("air");
422 if (dsimple->deco_height <= 0) {
423 errorstream << "register_decoration: simple decoration height"
424 " must be greater than 0" << std::endl;
430 case DECO_SCHEMATIC: {
431 DecoSchematic *dschem = (DecoSchematic *)deco;
434 getflagsfield(L, index, "flags", flagdesc_deco_schematic,
435 &dschem->flags, NULL);
437 dschem->rotation = (Rotation)getenumfield(L, index,
438 "rotation", es_Rotation, ROTATE_0);
440 lua_getfield(L, index, "replacements");
441 if (lua_istable(L, -1)) {
442 read_schematic_replacements(L, dschem, lua_gettop(L));
446 lua_getfield(L, index, "schematic");
447 if (!read_schematic(L, -1, dschem, getServer(L))) {
453 if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
454 errorstream << "register_decoration: failed to load schematic file '"
455 << dschem->filename << "'" << std::endl;
461 //DecoLSystem *decolsystem = (DecoLSystem *)deco;
466 emerge->decorations.push_back(deco);
468 verbosestream << "register_decoration: decoration '" << deco->getName()
469 << "' registered" << std::endl;
473 // register_ore({lots of stuff})
474 int ModApiMapgen::l_register_ore(lua_State *L)
477 luaL_checktype(L, index, LUA_TTABLE);
479 EmergeManager *emerge = getServer(L)->getEmergeManager();
481 enum OreType oretype = (OreType)getenumfield(L, index,
482 "ore_type", es_OreType, ORE_SCATTER);
483 Ore *ore = createOre(oretype);
485 errorstream << "register_ore: ore_type "
486 << oretype << " not implemented";
490 ore->ore_name = getstringfield_default(L, index, "ore", "");
491 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
492 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
493 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
494 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
495 ore->height_min = getintfield_default(L, index, "height_min", 0);
496 ore->height_max = getintfield_default(L, index, "height_max", 0);
497 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
499 getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
501 lua_getfield(L, index, "wherein");
502 if (lua_istable(L, -1)) {
503 int i = lua_gettop(L);
505 while(lua_next(L, i) != 0) {
506 ore->wherein_names.push_back(lua_tostring(L, -1));
509 } else if (lua_isstring(L, -1)) {
510 ore->wherein_names.push_back(lua_tostring(L, -1));
512 ore->wherein_names.push_back("");
516 lua_getfield(L, index, "noise_params");
517 ore->np = read_noiseparams(L, -1);
522 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
523 errorstream << "register_ore: clust_scarcity and clust_num_ores"
524 "must be greater than 0" << std::endl;
529 emerge->ores.push_back(ore);
531 verbosestream << "register_ore: ore '" << ore->ore_name
532 << "' registered" << std::endl;
536 // create_schematic(p1, p2, probability_list, filename)
537 int ModApiMapgen::l_create_schematic(lua_State *L)
539 DecoSchematic dschem;
541 Map *map = &(getEnv(L)->getMap());
542 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
544 v3s16 p1 = read_v3s16(L, 1);
545 v3s16 p2 = read_v3s16(L, 2);
546 sortBoxVerticies(p1, p2);
548 std::vector<std::pair<v3s16, u8> > prob_list;
549 if (lua_istable(L, 3)) {
551 while (lua_next(L, 3)) {
552 if (lua_istable(L, -1)) {
553 lua_getfield(L, -1, "pos");
554 v3s16 pos = read_v3s16(L, -1);
557 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
558 prob_list.push_back(std::make_pair(pos, prob));
565 std::vector<std::pair<s16, u8> > slice_prob_list;
566 if (lua_istable(L, 5)) {
568 while (lua_next(L, 5)) {
569 if (lua_istable(L, -1)) {
570 s16 ypos = getintfield_default(L, -1, "ypos", 0);
571 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
572 slice_prob_list.push_back(std::make_pair(ypos, prob));
579 const char *s = lua_tostring(L, 4);
580 dschem.filename = std::string(s ? s : "");
582 if (!dschem.getSchematicFromMap(map, p1, p2)) {
583 errorstream << "create_schematic: failed to get schematic "
584 "from map" << std::endl;
588 dschem.applyProbabilities(p1, &prob_list, &slice_prob_list);
590 dschem.saveSchematicFile(ndef);
591 actionstream << "create_schematic: saved schematic file '"
592 << dschem.filename << "'." << std::endl;
598 // place_schematic(p, schematic, rotation, replacement)
599 int ModApiMapgen::l_place_schematic(lua_State *L)
601 DecoSchematic dschem;
603 Map *map = &(getEnv(L)->getMap());
604 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
606 v3s16 p = read_v3s16(L, 1);
607 if (!read_schematic(L, 2, &dschem, getServer(L)))
611 if (lua_isstring(L, 3))
612 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
614 dschem.rotation = (Rotation)rot;
616 if (lua_istable(L, 4)) {
617 read_schematic_replacements(L, &dschem, 4);
620 bool force_placement = true;
621 if (lua_isboolean(L, 5))
622 force_placement = lua_toboolean(L, 5);
624 if (!dschem.filename.empty()) {
625 if (!dschem.loadSchematicFile()) {
626 errorstream << "place_schematic: failed to load schematic file '"
627 << dschem.filename << "'" << std::endl;
630 dschem.resolveNodeNames(ndef);
633 dschem.placeStructure(map, p, force_placement);
638 void ModApiMapgen::Initialize(lua_State *L, int top)
640 API_FCT(get_mapgen_object);
642 API_FCT(set_mapgen_params);
643 API_FCT(set_noiseparam_defaults);
644 API_FCT(set_gen_notify);
646 API_FCT(register_biome);
647 API_FCT(register_decoration);
648 API_FCT(register_ore);
650 API_FCT(create_schematic);
651 API_FCT(place_schematic);