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 // minetest.get_mapgen_object(objectname)
82 // returns the requested object used during map generation
83 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
85 const char *mgobjstr = lua_tostring(L, 1);
88 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
91 enum MapgenObject mgobj = (MapgenObject)mgobjint;
93 EmergeManager *emerge = getServer(L)->getEmergeManager();
94 Mapgen *mg = emerge->getCurrentMapgen();
98 size_t maplen = mg->csize.X * mg->csize.Z;
102 ManualMapVoxelManipulator *vm = mg->vm;
105 LuaVoxelManip *o = new LuaVoxelManip(vm, true);
106 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
107 luaL_getmetatable(L, "VoxelManip");
108 lua_setmetatable(L, -2);
111 push_v3s16(L, vm->m_area.MinEdge);
114 push_v3s16(L, vm->m_area.MaxEdge);
117 case MGOBJ_HEIGHTMAP: {
122 for (size_t i = 0; i != maplen; i++) {
123 lua_pushinteger(L, mg->heightmap[i]);
124 lua_rawseti(L, -2, i + 1);
128 case MGOBJ_BIOMEMAP: {
133 for (size_t i = 0; i != maplen; i++) {
134 lua_pushinteger(L, mg->biomemap[i]);
135 lua_rawseti(L, -2, i + 1);
139 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
141 if (strcmp(emerge->params.mg_name.c_str(), "v7"))
144 MapgenV7 *mgv7 = (MapgenV7 *)mg;
146 float *arr = (mgobj == MGOBJ_HEATMAP) ?
147 mgv7->noise_heat->result : mgv7->noise_humidity->result;
152 for (size_t i = 0; i != maplen; i++) {
153 lua_pushnumber(L, arr[i]);
154 lua_rawseti(L, -2, i + 1);
158 case MGOBJ_GENNOTIFY: {
160 for (int i = 0; flagdesc_gennotify[i].name; i++) {
161 if (!(emerge->gennotify & flagdesc_gennotify[i].flag))
164 std::vector<v3s16> *posvec = mg->gen_notifications[i];
169 for (unsigned int j = 0; j != posvec->size(); j++) {
170 push_v3s16(L, (*posvec)[j]);
171 lua_rawseti(L, -2, j + 1);
173 lua_setfield(L, -2, flagdesc_gennotify[i].name);
184 // minetest.set_mapgen_params(params)
185 // set mapgen parameters
186 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
188 if (!lua_istable(L, 1))
191 EmergeManager *emerge = getServer(L)->getEmergeManager();
196 lua_getfield(L, 1, "mgname");
197 if (lua_isstring(L, -1)) {
198 emerge->params.mg_name = std::string(lua_tostring(L, -1));
199 delete emerge->params.sparams;
200 emerge->params.sparams = NULL;
203 lua_getfield(L, 1, "seed");
204 if (lua_isnumber(L, -1))
205 emerge->params.seed = lua_tointeger(L, -1);
207 lua_getfield(L, 1, "water_level");
208 if (lua_isnumber(L, -1))
209 emerge->params.water_level = lua_tointeger(L, -1);
211 lua_getfield(L, 1, "flagmask");
212 if (lua_isstring(L, -1)) {
213 flagstr = lua_tostring(L, -1);
214 emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL);
215 errorstream << "set_mapgen_params(): flagmask field is deprecated, "
216 "see lua_api.txt" << std::endl;
219 lua_getfield(L, 1, "flags");
220 if (lua_isstring(L, -1)) {
223 flagstr = lua_tostring(L, -1);
224 flags = readFlagString(flagstr, flagdesc_mapgen, &flagmask);
226 emerge->params.flags &= ~flagmask;
227 emerge->params.flags |= flags;
233 // minetest.set_noiseparam_defaults({np1={noise params}, ...})
234 // set default values for noise parameters if not present in global settings
235 int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L)
238 std::string val, name;
240 if (!lua_istable(L, 1))
244 while (lua_next(L, 1)) {
245 if (read_noiseparams_nc(L, -1, &np)) {
246 if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
248 if (!lua_isstring(L, -2))
251 name = lua_tostring(L, -2);
252 g_settings->setDefault(name, val);
260 // set_gen_notify(string)
261 int ModApiMapgen::l_set_gen_notify(lua_State *L)
263 if (lua_isstring(L, 1)) {
264 EmergeManager *emerge = getServer(L)->getEmergeManager();
265 emerge->gennotify = readFlagString(lua_tostring(L, 1),
266 flagdesc_gennotify, NULL);
271 // register_biome({lots of stuff})
272 int ModApiMapgen::l_register_biome(lua_State *L)
275 luaL_checktype(L, index, LUA_TTABLE);
277 BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
279 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
283 enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
284 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
285 Biome *b = bmgr->createBiome(terrain);
287 b->name = getstringfield_default(L, index, "name",
289 b->nname_top = getstringfield_default(L, index, "node_top",
290 "mapgen_dirt_with_grass");
291 b->nname_filler = getstringfield_default(L, index, "node_filler",
293 b->nname_water = getstringfield_default(L, index, "node_water",
294 "mapgen_water_source");
295 b->nname_dust = getstringfield_default(L, index, "node_dust",
297 b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
298 "mapgen_water_source");
300 b->depth_top = getintfield_default(L, index, "depth_top", 1);
301 b->depth_filler = getintfield_default(L, index, "depth_filler", 3);
302 b->height_min = getintfield_default(L, index, "height_min", 0);
303 b->height_max = getintfield_default(L, index, "height_max", 0);
304 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.);
305 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
307 b->flags = 0; //reserved
308 b->c_top = CONTENT_IGNORE;
309 b->c_filler = CONTENT_IGNORE;
310 b->c_water = CONTENT_IGNORE;
311 b->c_dust = CONTENT_IGNORE;
312 b->c_dust_water = CONTENT_IGNORE;
314 verbosestream << "register_biome: " << b->name << std::endl;
320 // register_decoration({lots of stuff})
321 int ModApiMapgen::l_register_decoration(lua_State *L)
324 luaL_checktype(L, index, LUA_TTABLE);
326 EmergeManager *emerge = getServer(L)->getEmergeManager();
327 BiomeDefManager *bdef = emerge->biomedef;
329 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
330 "deco_type", es_DecorationType, -1);
331 if (decotype == -1) {
332 errorstream << "register_decoration: unrecognized "
333 "decoration placement type";
337 Decoration *deco = createDecoration(decotype);
339 errorstream << "register_decoration: decoration placement type "
340 << decotype << " not implemented";
344 deco->c_place_on = CONTENT_IGNORE;
345 deco->place_on_name = getstringfield_default(L, index, "place_on", "ignore");
346 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
347 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
348 if (deco->sidelen <= 0) {
349 errorstream << "register_decoration: sidelen must be "
350 "greater than 0" << std::endl;
355 lua_getfield(L, index, "noise_params");
356 deco->np = read_noiseparams(L, -1);
359 lua_getfield(L, index, "biomes");
360 if (lua_istable(L, -1)) {
362 while (lua_next(L, -2)) {
363 const char *s = lua_tostring(L, -1);
364 u8 biomeid = bdef->getBiomeIdByName(s);
366 deco->biomes.insert(biomeid);
375 DecoSimple *dsimple = (DecoSimple *)deco;
376 dsimple->c_deco = CONTENT_IGNORE;
377 dsimple->c_spawnby = CONTENT_IGNORE;
378 dsimple->spawnby_name = getstringfield_default(L, index, "spawn_by", "air");
379 dsimple->deco_height = getintfield_default(L, index, "height", 1);
380 dsimple->deco_height_max = getintfield_default(L, index, "height_max", 0);
381 dsimple->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
383 lua_getfield(L, index, "decoration");
384 if (lua_istable(L, -1)) {
386 while (lua_next(L, -2)) {
387 const char *s = lua_tostring(L, -1);
389 dsimple->decolist_names.push_back(str);
393 } else if (lua_isstring(L, -1)) {
394 dsimple->deco_name = std::string(lua_tostring(L, -1));
396 dsimple->deco_name = std::string("air");
400 if (dsimple->deco_height <= 0) {
401 errorstream << "register_decoration: simple decoration height"
402 " must be greater than 0" << std::endl;
408 case DECO_SCHEMATIC: {
409 DecoSchematic *dschem = (DecoSchematic *)deco;
410 dschem->flags = getflagsfield(L, index, "flags",
411 flagdesc_deco_schematic, NULL);
412 dschem->rotation = (Rotation)getenumfield(L, index,
413 "rotation", es_Rotation, ROTATE_0);
415 lua_getfield(L, index, "replacements");
416 if (lua_istable(L, -1)) {
417 int i = lua_gettop(L);
419 while (lua_next(L, i) != 0) {
420 // key at index -2 and value at index -1
421 lua_rawgeti(L, -1, 1);
422 std::string replace_from = lua_tostring(L, -1);
424 lua_rawgeti(L, -1, 2);
425 std::string replace_to = lua_tostring(L, -1);
427 dschem->replacements[replace_from] = replace_to;
428 // removes value, keeps key for next iteration
434 lua_getfield(L, index, "schematic");
435 if (!read_schematic(L, -1, dschem, getServer(L))) {
441 if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
442 errorstream << "register_decoration: failed to load schematic file '"
443 << dschem->filename << "'" << std::endl;
449 //DecoLSystem *decolsystem = (DecoLSystem *)deco;
454 emerge->decorations.push_back(deco);
456 verbosestream << "register_decoration: decoration '" << deco->getName()
457 << "' registered" << std::endl;
461 // register_ore({lots of stuff})
462 int ModApiMapgen::l_register_ore(lua_State *L)
465 luaL_checktype(L, index, LUA_TTABLE);
467 EmergeManager *emerge = getServer(L)->getEmergeManager();
469 enum OreType oretype = (OreType)getenumfield(L, index,
470 "ore_type", es_OreType, ORE_SCATTER);
471 Ore *ore = createOre(oretype);
473 errorstream << "register_ore: ore_type "
474 << oretype << " not implemented";
478 ore->ore_name = getstringfield_default(L, index, "ore", "");
479 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
480 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
481 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
482 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
483 ore->height_min = getintfield_default(L, index, "height_min", 0);
484 ore->height_max = getintfield_default(L, index, "height_max", 0);
485 ore->flags = getflagsfield(L, index, "flags", flagdesc_ore, NULL);
486 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
487 lua_getfield(L, index, "wherein");
488 if (lua_istable(L, -1)) {
489 int i = lua_gettop(L);
491 while(lua_next(L, i) != 0) {
492 ore->wherein_names.push_back(lua_tostring(L, -1));
495 } else if (lua_isstring(L, -1)) {
496 ore->wherein_names.push_back(lua_tostring(L, -1));
498 ore->wherein_names.push_back("");
502 lua_getfield(L, index, "noise_params");
503 ore->np = read_noiseparams(L, -1);
508 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
509 errorstream << "register_ore: clust_scarcity and clust_num_ores"
510 "must be greater than 0" << std::endl;
515 emerge->ores.push_back(ore);
517 verbosestream << "register_ore: ore '" << ore->ore_name
518 << "' registered" << std::endl;
522 // create_schematic(p1, p2, probability_list, filename)
523 int ModApiMapgen::l_create_schematic(lua_State *L)
525 DecoSchematic dschem;
527 Map *map = &(getEnv(L)->getMap());
528 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
530 v3s16 p1 = read_v3s16(L, 1);
531 v3s16 p2 = read_v3s16(L, 2);
532 sortBoxVerticies(p1, p2);
534 std::vector<std::pair<v3s16, u8> > prob_list;
535 if (lua_istable(L, 3)) {
537 while (lua_next(L, 3)) {
538 if (lua_istable(L, -1)) {
539 lua_getfield(L, -1, "pos");
540 v3s16 pos = read_v3s16(L, -1);
543 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
544 prob_list.push_back(std::make_pair(pos, prob));
551 std::vector<std::pair<s16, u8> > slice_prob_list;
552 if (lua_istable(L, 5)) {
554 while (lua_next(L, 5)) {
555 if (lua_istable(L, -1)) {
556 s16 ypos = getintfield_default(L, -1, "ypos", 0);
557 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
558 slice_prob_list.push_back(std::make_pair(ypos, prob));
565 const char *s = lua_tostring(L, 4);
566 dschem.filename = std::string(s ? s : "");
568 if (!dschem.getSchematicFromMap(map, p1, p2)) {
569 errorstream << "create_schematic: failed to get schematic "
570 "from map" << std::endl;
574 dschem.applyProbabilities(p1, &prob_list, &slice_prob_list);
576 dschem.saveSchematicFile(ndef);
577 actionstream << "create_schematic: saved schematic file '"
578 << dschem.filename << "'." << std::endl;
584 // place_schematic(p, schematic, rotation, replacement)
585 int ModApiMapgen::l_place_schematic(lua_State *L)
587 DecoSchematic dschem;
589 Map *map = &(getEnv(L)->getMap());
590 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
592 v3s16 p = read_v3s16(L, 1);
593 if (!read_schematic(L, 2, &dschem, getServer(L)))
597 if (lua_isstring(L, 3))
598 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
600 dschem.rotation = (Rotation)rot;
602 if (lua_istable(L, 4)) {
604 while (lua_next(L, 4) != 0) {
605 // key at index -2 and value at index -1
606 lua_rawgeti(L, -1, 1);
607 std::string replace_from = lua_tostring(L, -1);
609 lua_rawgeti(L, -1, 2);
610 std::string replace_to = lua_tostring(L, -1);
612 dschem.replacements[replace_from] = replace_to;
613 // removes value, keeps key for next iteration
618 bool force_placement = true;
619 if (lua_isboolean(L, 5))
620 force_placement = lua_toboolean(L, 5);
622 if (!dschem.filename.empty()) {
623 if (!dschem.loadSchematicFile()) {
624 errorstream << "place_schematic: failed to load schematic file '"
625 << dschem.filename << "'" << std::endl;
628 dschem.resolveNodeNames(ndef);
631 dschem.placeStructure(map, p, force_placement);
636 void ModApiMapgen::Initialize(lua_State *L, int top)
638 API_FCT(get_mapgen_object);
640 API_FCT(set_mapgen_params);
641 API_FCT(set_noiseparam_defaults);
642 API_FCT(set_gen_notify);
644 API_FCT(register_biome);
645 API_FCT(register_decoration);
646 API_FCT(register_ore);
648 API_FCT(create_schematic);
649 API_FCT(place_schematic);