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"
32 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
34 {BIOME_TERRAIN_NORMAL, "normal"},
35 {BIOME_TERRAIN_LIQUID, "liquid"},
36 {BIOME_TERRAIN_NETHER, "nether"},
37 {BIOME_TERRAIN_AETHER, "aether"},
38 {BIOME_TERRAIN_FLAT, "flat"},
42 struct EnumString ModApiMapgen::es_DecorationType[] =
44 {DECO_SIMPLE, "simple"},
45 {DECO_SCHEMATIC, "schematic"},
46 {DECO_LSYSTEM, "lsystem"},
50 struct EnumString ModApiMapgen::es_MapgenObject[] =
52 {MGOBJ_VMANIP, "voxelmanip"},
53 {MGOBJ_HEIGHTMAP, "heightmap"},
54 {MGOBJ_BIOMEMAP, "biomemap"},
55 {MGOBJ_HEATMAP, "heatmap"},
56 {MGOBJ_HUMIDMAP, "humiditymap"},
60 struct EnumString ModApiMapgen::es_OreType[] =
62 {ORE_SCATTER, "scatter"},
64 {ORE_CLAYLIKE, "claylike"},
68 struct EnumString ModApiMapgen::es_Rotation[] =
74 {ROTATE_RAND, "random"},
79 // minetest.get_mapgen_object(objectname)
80 // returns the requested object used during map generation
81 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
83 const char *mgobjstr = lua_tostring(L, 1);
86 if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
89 enum MapgenObject mgobj = (MapgenObject)mgobjint;
91 EmergeManager *emerge = getServer(L)->getEmergeManager();
92 Mapgen *mg = emerge->getCurrentMapgen();
96 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);
119 case MGOBJ_HEIGHTMAP: {
124 for (size_t i = 0; i != maplen; i++) {
125 lua_pushinteger(L, mg->heightmap[i]);
126 lua_rawseti(L, -2, i + 1);
129 case MGOBJ_BIOMEMAP: {
134 for (size_t i = 0; i != maplen; i++) {
135 lua_pushinteger(L, mg->biomemap[i]);
136 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);
162 // minetest.set_mapgen_params(params)
163 // set mapgen parameters
164 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
166 if (!lua_istable(L, 1))
169 EmergeManager *emerge = getServer(L)->getEmergeManager();
170 if (emerge->mapgen.size())
173 MapgenParams *oparams = new MapgenParams;
174 u32 paramsmodified = 0;
177 lua_getfield(L, 1, "mgname");
178 if (lua_isstring(L, -1)) {
179 oparams->mg_name = std::string(lua_tostring(L, -1));
180 paramsmodified |= MGPARAMS_SET_MGNAME;
183 lua_getfield(L, 1, "seed");
184 if (lua_isnumber(L, -1)) {
185 oparams->seed = lua_tointeger(L, -1);
186 paramsmodified |= MGPARAMS_SET_SEED;
189 lua_getfield(L, 1, "water_level");
190 if (lua_isnumber(L, -1)) {
191 oparams->water_level = lua_tointeger(L, -1);
192 paramsmodified |= MGPARAMS_SET_WATER_LEVEL;
195 lua_getfield(L, 1, "flags");
196 if (lua_isstring(L, -1)) {
197 std::string flagstr = std::string(lua_tostring(L, -1));
198 oparams->flags = readFlagString(flagstr, flagdesc_mapgen);
199 paramsmodified |= MGPARAMS_SET_FLAGS;
201 lua_getfield(L, 1, "flagmask");
202 if (lua_isstring(L, -1)) {
203 flagstr = std::string(lua_tostring(L, -1));
204 flagmask = readFlagString(flagstr, flagdesc_mapgen);
208 emerge->luaoverride_params = oparams;
209 emerge->luaoverride_params_modified = paramsmodified;
210 emerge->luaoverride_flagmask = flagmask;
215 // register_biome({lots of stuff})
216 int ModApiMapgen::l_register_biome(lua_State *L)
219 luaL_checktype(L, index, LUA_TTABLE);
221 BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
223 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
227 enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
228 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
229 Biome *b = bmgr->createBiome(terrain);
231 b->name = getstringfield_default(L, index, "name",
233 b->nname_top = getstringfield_default(L, index, "node_top",
234 "mapgen_dirt_with_grass");
235 b->nname_filler = getstringfield_default(L, index, "node_filler",
237 b->nname_water = getstringfield_default(L, index, "node_water",
238 "mapgen_water_source");
239 b->nname_dust = getstringfield_default(L, index, "node_dust",
241 b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
242 "mapgen_water_source");
244 b->depth_top = getintfield_default(L, index, "depth_top", 1);
245 b->depth_filler = getintfield_default(L, index, "depth_filler", 3);
246 b->height_min = getintfield_default(L, index, "height_min", 0);
247 b->height_max = getintfield_default(L, index, "height_max", 0);
248 b->heat_point = getfloatfield_default(L, index, "heat_point", 0.);
249 b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
251 b->flags = 0; //reserved
252 b->c_top = CONTENT_IGNORE;
253 b->c_filler = CONTENT_IGNORE;
254 b->c_water = CONTENT_IGNORE;
255 b->c_dust = CONTENT_IGNORE;
256 b->c_dust_water = CONTENT_IGNORE;
258 verbosestream << "register_biome: " << b->name << std::endl;
264 // register_decoration({lots of stuff})
265 int ModApiMapgen::l_register_decoration(lua_State *L)
268 luaL_checktype(L, index, LUA_TTABLE);
270 EmergeManager *emerge = getServer(L)->getEmergeManager();
271 BiomeDefManager *bdef = emerge->biomedef;
273 enum DecorationType decotype = (DecorationType)getenumfield(L, index,
274 "deco_type", es_DecorationType, -1);
275 if (decotype == -1) {
276 errorstream << "register_decoration: unrecognized "
277 "decoration placement type";
281 Decoration *deco = createDecoration(decotype);
283 errorstream << "register_decoration: decoration placement type "
284 << decotype << " not implemented";
288 deco->c_place_on = CONTENT_IGNORE;
289 deco->place_on_name = getstringfield_default(L, index, "place_on", "ignore");
290 deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02);
291 deco->sidelen = getintfield_default(L, index, "sidelen", 8);
292 if (deco->sidelen <= 0) {
293 errorstream << "register_decoration: sidelen must be "
294 "greater than 0" << std::endl;
299 lua_getfield(L, index, "noise_params");
300 deco->np = read_noiseparams(L, -1);
303 lua_getfield(L, index, "biomes");
304 if (lua_istable(L, -1)) {
306 while (lua_next(L, -2)) {
307 const char *s = lua_tostring(L, -1);
308 u8 biomeid = bdef->getBiomeIdByName(s);
310 deco->biomes.insert(biomeid);
319 DecoSimple *dsimple = (DecoSimple *)deco;
320 dsimple->c_deco = CONTENT_IGNORE;
321 dsimple->c_spawnby = CONTENT_IGNORE;
322 dsimple->spawnby_name = getstringfield_default(L, index, "spawn_by", "air");
323 dsimple->deco_height = getintfield_default(L, index, "height", 1);
324 dsimple->deco_height_max = getintfield_default(L, index, "height_max", 0);
325 dsimple->nspawnby = getintfield_default(L, index, "num_spawn_by", -1);
327 lua_getfield(L, index, "decoration");
328 if (lua_istable(L, -1)) {
330 while (lua_next(L, -2)) {
331 const char *s = lua_tostring(L, -1);
333 dsimple->decolist_names.push_back(str);
337 } else if (lua_isstring(L, -1)) {
338 dsimple->deco_name = std::string(lua_tostring(L, -1));
340 dsimple->deco_name = std::string("air");
344 if (dsimple->deco_height <= 0) {
345 errorstream << "register_decoration: simple decoration height"
346 " must be greater than 0" << std::endl;
352 case DECO_SCHEMATIC: {
353 DecoSchematic *dschem = (DecoSchematic *)deco;
354 dschem->flags = getflagsfield(L, index, "flags", flagdesc_deco_schematic);
355 dschem->rotation = (Rotation)getenumfield(L, index,
356 "rotation", es_Rotation, ROTATE_0);
358 lua_getfield(L, index, "replacements");
359 if (lua_istable(L, -1)) {
360 int i = lua_gettop(L);
362 while (lua_next(L, i) != 0) {
363 // key at index -2 and value at index -1
364 lua_rawgeti(L, -1, 1);
365 std::string replace_from = lua_tostring(L, -1);
367 lua_rawgeti(L, -1, 2);
368 std::string replace_to = lua_tostring(L, -1);
370 dschem->replacements[replace_from] = replace_to;
371 // removes value, keeps key for next iteration
377 lua_getfield(L, index, "schematic");
378 if (!read_schematic(L, -1, dschem, getServer(L))) {
384 if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
385 errorstream << "register_decoration: failed to load schematic file '"
386 << dschem->filename << "'" << std::endl;
392 //DecoLSystem *decolsystem = (DecoLSystem *)deco;
397 emerge->decorations.push_back(deco);
399 verbosestream << "register_decoration: decoration '" << deco->getName()
400 << "' registered" << std::endl;
404 // register_ore({lots of stuff})
405 int ModApiMapgen::l_register_ore(lua_State *L)
408 luaL_checktype(L, index, LUA_TTABLE);
410 EmergeManager *emerge = getServer(L)->getEmergeManager();
412 enum OreType oretype = (OreType)getenumfield(L, index,
413 "ore_type", es_OreType, ORE_SCATTER);
414 Ore *ore = createOre(oretype);
416 errorstream << "register_ore: ore_type "
417 << oretype << " not implemented";
421 ore->ore_name = getstringfield_default(L, index, "ore", "");
422 ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
423 ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
424 ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
425 ore->clust_size = getintfield_default(L, index, "clust_size", 0);
426 ore->height_min = getintfield_default(L, index, "height_min", 0);
427 ore->height_max = getintfield_default(L, index, "height_max", 0);
428 ore->flags = getflagsfield(L, index, "flags", flagdesc_ore);
429 ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.);
431 lua_getfield(L, index, "wherein");
432 if (lua_istable(L, -1)) {
433 int i = lua_gettop(L);
435 while(lua_next(L, i) != 0) {
436 ore->wherein_names.push_back(lua_tostring(L, -1));
439 } else if (lua_isstring(L, -1)) {
440 ore->wherein_names.push_back(lua_tostring(L, -1));
442 ore->wherein_names.push_back("");
446 lua_getfield(L, index, "noise_params");
447 ore->np = read_noiseparams(L, -1);
452 if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
453 errorstream << "register_ore: clust_scarcity and clust_num_ores"
454 "must be greater than 0" << std::endl;
459 emerge->ores.push_back(ore);
461 verbosestream << "register_ore: ore '" << ore->ore_name
462 << "' registered" << std::endl;
466 // create_schematic(p1, p2, probability_list, filename)
467 int ModApiMapgen::l_create_schematic(lua_State *L)
469 DecoSchematic dschem;
471 Map *map = &(getEnv(L)->getMap());
472 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
474 v3s16 p1 = read_v3s16(L, 1);
475 v3s16 p2 = read_v3s16(L, 2);
476 sortBoxVerticies(p1, p2);
478 std::vector<std::pair<v3s16, u8> > probability_list;
479 if (lua_istable(L, 3)) {
481 while (lua_next(L, 3)) {
482 if (lua_istable(L, -1)) {
483 lua_getfield(L, -1, "pos");
484 v3s16 pos = read_v3s16(L, -1);
487 u8 prob = getintfield_default(L, -1, "prob", 0xFF);
488 probability_list.push_back(std::make_pair(pos, prob));
495 dschem.filename = std::string(lua_tostring(L, 4));
497 if (!dschem.getSchematicFromMap(map, p1, p2)) {
498 errorstream << "create_schematic: failed to get schematic "
499 "from map" << std::endl;
503 dschem.applyProbabilities(&probability_list, p1);
505 dschem.saveSchematicFile(ndef);
506 actionstream << "create_schematic: saved schematic file '"
507 << dschem.filename << "'." << std::endl;
513 // place_schematic(p, schematic, rotation, replacement)
514 int ModApiMapgen::l_place_schematic(lua_State *L)
516 DecoSchematic dschem;
518 Map *map = &(getEnv(L)->getMap());
519 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
521 v3s16 p = read_v3s16(L, 1);
522 if (!read_schematic(L, 2, &dschem, getServer(L)))
525 Rotation rot = ROTATE_0;
526 if (lua_isstring(L, 3))
527 string_to_enum(es_Rotation, (int &)rot, std::string(lua_tostring(L, 3)));
529 dschem.rotation = rot;
531 if (lua_istable(L, 4)) {
534 while (lua_next(L, index) != 0) {
535 // key at index -2 and value at index -1
536 lua_rawgeti(L, -1, 1);
537 std::string replace_from = lua_tostring(L, -1);
539 lua_rawgeti(L, -1, 2);
540 std::string replace_to = lua_tostring(L, -1);
542 dschem.replacements[replace_from] = replace_to;
543 // removes value, keeps key for next iteration
548 if (!dschem.filename.empty()) {
549 if (!dschem.loadSchematicFile()) {
550 errorstream << "place_schematic: failed to load schematic file '"
551 << dschem.filename << "'" << std::endl;
554 dschem.resolveNodeNames(ndef);
557 dschem.placeStructure(map, p);
562 void ModApiMapgen::Initialize(lua_State *L, int top)
564 API_FCT(get_mapgen_object);
566 API_FCT(set_mapgen_params);
568 API_FCT(register_biome);
569 API_FCT(register_decoration);
570 API_FCT(register_ore);
572 API_FCT(create_schematic);
573 API_FCT(place_schematic);