Added configurable selectionbox width. Min width = 1, Max = 5
[oweals/minetest.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 "server.h"
26 #include "environment.h"
27 #include "biome.h"
28 #include "emerge.h"
29 #include "mapgen_v7.h"
30 #include "main.h"
31
32
33 struct EnumString ModApiMapgen::es_BiomeTerrainType[] =
34 {
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"},
40         {0, NULL},
41 };
42
43 struct EnumString ModApiMapgen::es_DecorationType[] =
44 {
45         {DECO_SIMPLE,    "simple"},
46         {DECO_SCHEMATIC, "schematic"},
47         {DECO_LSYSTEM,   "lsystem"},
48         {0, NULL},
49 };
50
51 struct EnumString ModApiMapgen::es_MapgenObject[] =
52 {
53         {MGOBJ_VMANIP,    "voxelmanip"},
54         {MGOBJ_HEIGHTMAP, "heightmap"},
55         {MGOBJ_BIOMEMAP,  "biomemap"},
56         {MGOBJ_HEATMAP,   "heatmap"},
57         {MGOBJ_HUMIDMAP,  "humiditymap"},
58         {MGOBJ_GENNOTIFY, "gennotify"},
59         {0, NULL},
60 };
61
62 struct EnumString ModApiMapgen::es_OreType[] =
63 {
64         {ORE_SCATTER,  "scatter"},
65         {ORE_SHEET,    "sheet"},
66         {ORE_CLAYLIKE, "claylike"},
67         {0, NULL},
68 };
69
70 struct EnumString ModApiMapgen::es_Rotation[] =
71 {
72         {ROTATE_0,    "0"},
73         {ROTATE_90,   "90"},
74         {ROTATE_180,  "180"},
75         {ROTATE_270,  "270"},
76         {ROTATE_RAND, "random"},
77         {0, NULL},
78 };
79
80
81 static void read_schematic_replacements(lua_State *L, DecoSchematic *dschem, int index)
82 {
83         lua_pushnil(L);
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);
91                         lua_pop(L, 1);
92                         lua_rawgeti(L, -1, 2);
93                         replace_to = lua_tostring(L, -1);
94                         lua_pop(L, 1);
95                 } else {  // New {x = "y", ...} format
96                         replace_from = lua_tostring(L, -2);
97                         replace_to = lua_tostring(L, -1);
98                 }
99                 dschem->replacements[replace_from] = replace_to;
100                 // removes value, keeps key for next iteration
101                 lua_pop(L, 1);
102         }
103 }
104
105
106 // get_mapgen_object(objectname)
107 // returns the requested object used during map generation
108 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
109 {
110         const char *mgobjstr = lua_tostring(L, 1);
111
112         int mgobjint;
113         if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
114                 return 0;
115
116         enum MapgenObject mgobj = (MapgenObject)mgobjint;
117
118         EmergeManager *emerge = getServer(L)->getEmergeManager();
119         Mapgen *mg = emerge->getCurrentMapgen();
120         if (!mg)
121                 return 0;
122
123         size_t maplen = mg->csize.X * mg->csize.Z;
124
125         switch (mgobj) {
126                 case MGOBJ_VMANIP: {
127                         ManualMapVoxelManipulator *vm = mg->vm;
128
129                         // VoxelManip object
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);
134
135                         // emerged min pos
136                         push_v3s16(L, vm->m_area.MinEdge);
137
138                         // emerged max pos
139                         push_v3s16(L, vm->m_area.MaxEdge);
140
141                         return 3; }
142                 case MGOBJ_HEIGHTMAP: {
143                         if (!mg->heightmap)
144                                 return 0;
145
146                         lua_newtable(L);
147                         for (size_t i = 0; i != maplen; i++) {
148                                 lua_pushinteger(L, mg->heightmap[i]);
149                                 lua_rawseti(L, -2, i + 1);
150                         }
151
152                         return 1; }
153                 case MGOBJ_BIOMEMAP: {
154                         if (!mg->biomemap)
155                                 return 0;
156
157                         lua_newtable(L);
158                         for (size_t i = 0; i != maplen; i++) {
159                                 lua_pushinteger(L, mg->biomemap[i]);
160                                 lua_rawseti(L, -2, i + 1);
161                         }
162
163                         return 1; }
164                 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
165                 case MGOBJ_HUMIDMAP:
166                         if (strcmp(emerge->params.mg_name.c_str(), "v7"))
167                                 return 0;
168
169                         MapgenV7 *mgv7 = (MapgenV7 *)mg;
170
171                         float *arr = (mgobj == MGOBJ_HEATMAP) ?
172                                 mgv7->noise_heat->result : mgv7->noise_humidity->result;
173                         if (!arr)
174                                 return 0;
175
176                         lua_newtable(L);
177                         for (size_t i = 0; i != maplen; i++) {
178                                 lua_pushnumber(L, arr[i]);
179                                 lua_rawseti(L, -2, i + 1);
180                         }
181
182                         return 1; }
183                 case MGOBJ_GENNOTIFY: {
184                         lua_newtable(L);
185                         for (int i = 0; flagdesc_gennotify[i].name; i++) {
186                                 if (!(emerge->gennotify & flagdesc_gennotify[i].flag))
187                                         continue;
188
189                                 std::vector<v3s16> *posvec = mg->gen_notifications[i];
190                                 if (!posvec)
191                                         return 0;
192
193                                 lua_newtable(L);
194                                 for (unsigned int j = 0; j != posvec->size(); j++) {
195                                         push_v3s16(L, (*posvec)[j]);
196                                         lua_rawseti(L, -2, j + 1);
197                                 }
198                                 lua_setfield(L, -2, flagdesc_gennotify[i].name);
199
200                                 posvec->clear();
201                         }
202
203                         return 1; }
204         }
205
206         return 0;
207 }
208
209 // set_mapgen_params(params)
210 // set mapgen parameters
211 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
212 {
213         if (!lua_istable(L, 1))
214                 return 0;
215
216         EmergeManager *emerge = getServer(L)->getEmergeManager();
217         assert(emerge);
218
219         std::string flagstr;
220         u32 flags = 0, flagmask = 0;
221
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;
227         }
228
229         lua_getfield(L, 1, "seed");
230         if (lua_isnumber(L, -1))
231                 emerge->params.seed = lua_tointeger(L, -1);
232
233         lua_getfield(L, 1, "water_level");
234         if (lua_isnumber(L, -1))
235                 emerge->params.water_level = lua_tointeger(L, -1);
236
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;
243         }
244
245         if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
246                 emerge->params.flags &= ~flagmask;
247                 emerge->params.flags |= flags;
248         }
249
250         return 0;
251 }
252
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)
256 {
257         NoiseParams np;
258         std::string val, name;
259
260         if (!lua_istable(L, 1))
261                 return 0;
262
263         lua_pushnil(L);
264         while (lua_next(L, 1)) {
265                 if (read_noiseparams_nc(L, -1, &np)) {
266                         if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
267                                 continue;
268                         if (!lua_isstring(L, -2))
269                                 continue;
270
271                         name = lua_tostring(L, -2);
272                         g_settings->setDefault(name, val);
273                 }
274                 lua_pop(L, 1);
275         }
276
277         return 0;
278 }
279
280 // set_gen_notify(string)
281 int ModApiMapgen::l_set_gen_notify(lua_State *L)
282 {
283         u32 flags = 0, flagmask = 0;
284
285         if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
286                 EmergeManager *emerge = getServer(L)->getEmergeManager();
287                 emerge->gennotify = flags;
288         }
289
290         return 0;
291 }
292
293 // register_biome({lots of stuff})
294 int ModApiMapgen::l_register_biome(lua_State *L)
295 {
296         int index = 1;
297         luaL_checktype(L, index, LUA_TTABLE);
298
299         BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
300         if (!bmgr) {
301                 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
302                 return 0;
303         }
304
305         enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
306                                 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
307         Biome *b = bmgr->createBiome(terrain);
308
309         b->name         = getstringfield_default(L, index, "name",
310                                                                                                 "<no 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",
314                                                                                                 "mapgen_dirt");
315         b->nname_water  = getstringfield_default(L, index, "node_water",
316                                                                                                 "mapgen_water_source");
317         b->nname_dust   = getstringfield_default(L, index, "node_dust",
318                                                                                                 "air");
319         b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
320                                                                                                 "mapgen_water_source");
321
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.);
328
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;
335
336         verbosestream << "register_biome: " << b->name << std::endl;
337         bmgr->addBiome(b);
338
339         return 0;
340 }
341
342 // register_decoration({lots of stuff})
343 int ModApiMapgen::l_register_decoration(lua_State *L)
344 {
345         int index = 1;
346         luaL_checktype(L, index, LUA_TTABLE);
347
348         EmergeManager *emerge = getServer(L)->getEmergeManager();
349         BiomeDefManager *bdef = emerge->biomedef;
350
351         enum DecorationType decotype = (DecorationType)getenumfield(L, index,
352                                 "deco_type", es_DecorationType, 0);
353         if (decotype == 0) {
354                 errorstream << "register_decoration: unrecognized "
355                         "decoration placement type";
356                 return 0;
357         }
358
359         Decoration *deco = createDecoration(decotype);
360         if (!deco) {
361                 errorstream << "register_decoration: decoration placement type "
362                         << decotype << " not implemented";
363                 return 0;
364         }
365
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;
373                 delete deco;
374                 return 0;
375         }
376
377         lua_getfield(L, index, "noise_params");
378         deco->np = read_noiseparams(L, -1);
379         lua_pop(L, 1);
380
381         lua_getfield(L, index, "biomes");
382         if (lua_istable(L, -1)) {
383                 lua_pushnil(L);
384                 while (lua_next(L, -2)) {
385                         const char *s = lua_tostring(L, -1);
386                         u8 biomeid = bdef->getBiomeIdByName(s);
387                         if (biomeid)
388                                 deco->biomes.insert(biomeid);
389
390                         lua_pop(L, 1);
391                 }
392                 lua_pop(L, 1);
393         }
394
395         switch (decotype) {
396                 case DECO_SIMPLE: {
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);
404
405                         lua_getfield(L, index, "decoration");
406                         if (lua_istable(L, -1)) {
407                                 lua_pushnil(L);
408                                 while (lua_next(L, -2)) {
409                                         const char *s = lua_tostring(L, -1);
410                                         std::string str(s);
411                                         dsimple->decolist_names.push_back(str);
412
413                                         lua_pop(L, 1);
414                                 }
415                         } else if (lua_isstring(L, -1)) {
416                                 dsimple->deco_name = std::string(lua_tostring(L, -1));
417                         } else {
418                                 dsimple->deco_name = std::string("air");
419                         }
420                         lua_pop(L, 1);
421
422                         if (dsimple->deco_height <= 0) {
423                                 errorstream << "register_decoration: simple decoration height"
424                                         " must be greater than 0" << std::endl;
425                                 delete dsimple;
426                                 return 0;
427                         }
428
429                         break; }
430                 case DECO_SCHEMATIC: {
431                         DecoSchematic *dschem = (DecoSchematic *)deco;
432
433                         dschem->flags = 0;
434                         getflagsfield(L, index, "flags", flagdesc_deco_schematic,
435                                 &dschem->flags, NULL);
436
437                         dschem->rotation = (Rotation)getenumfield(L, index,
438                                 "rotation", es_Rotation, ROTATE_0);
439
440                         lua_getfield(L, index, "replacements");
441                         if (lua_istable(L, -1)) {
442                                 read_schematic_replacements(L, dschem, lua_gettop(L));
443                         }
444                         lua_pop(L, 1);
445
446                         lua_getfield(L, index, "schematic");
447                         if (!read_schematic(L, -1, dschem, getServer(L))) {
448                                 delete dschem;
449                                 return 0;
450                         }
451                         lua_pop(L, -1);
452
453                         if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
454                                 errorstream << "register_decoration: failed to load schematic file '"
455                                         << dschem->filename << "'" << std::endl;
456                                 delete dschem;
457                                 return 0;
458                         }
459                         break; }
460                 case DECO_LSYSTEM: {
461                         //DecoLSystem *decolsystem = (DecoLSystem *)deco;
462
463                         break; }
464         }
465
466         emerge->decorations.push_back(deco);
467
468         verbosestream << "register_decoration: decoration '" << deco->getName()
469                 << "' registered" << std::endl;
470         return 0;
471 }
472
473 // register_ore({lots of stuff})
474 int ModApiMapgen::l_register_ore(lua_State *L)
475 {
476         int index = 1;
477         luaL_checktype(L, index, LUA_TTABLE);
478
479         EmergeManager *emerge = getServer(L)->getEmergeManager();
480
481         enum OreType oretype = (OreType)getenumfield(L, index,
482                                 "ore_type", es_OreType, ORE_SCATTER);
483         Ore *ore = createOre(oretype);
484         if (!ore) {
485                 errorstream << "register_ore: ore_type "
486                         << oretype << " not implemented";
487                 return 0;
488         }
489
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.);
498         ore->flags          = 0;
499         getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
500
501         lua_getfield(L, index, "wherein");
502         if (lua_istable(L, -1)) {
503                 int  i = lua_gettop(L);
504                 lua_pushnil(L);
505                 while(lua_next(L, i) != 0) {
506                         ore->wherein_names.push_back(lua_tostring(L, -1));
507                         lua_pop(L, 1);
508                 }
509         } else if (lua_isstring(L, -1)) {
510                 ore->wherein_names.push_back(lua_tostring(L, -1));
511         } else {
512                 ore->wherein_names.push_back("");
513         }
514         lua_pop(L, 1);
515
516         lua_getfield(L, index, "noise_params");
517         ore->np = read_noiseparams(L, -1);
518         lua_pop(L, 1);
519
520         ore->noise = NULL;
521
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;
525                 delete ore;
526                 return 0;
527         }
528
529         emerge->ores.push_back(ore);
530
531         verbosestream << "register_ore: ore '" << ore->ore_name
532                 << "' registered" << std::endl;
533         return 0;
534 }
535
536 // create_schematic(p1, p2, probability_list, filename)
537 int ModApiMapgen::l_create_schematic(lua_State *L)
538 {
539         DecoSchematic dschem;
540
541         Map *map = &(getEnv(L)->getMap());
542         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
543
544         v3s16 p1 = read_v3s16(L, 1);
545         v3s16 p2 = read_v3s16(L, 2);
546         sortBoxVerticies(p1, p2);
547
548         std::vector<std::pair<v3s16, u8> > prob_list;
549         if (lua_istable(L, 3)) {
550                 lua_pushnil(L);
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);
555                                 lua_pop(L, 1);
556
557                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
558                                 prob_list.push_back(std::make_pair(pos, prob));
559                         }
560
561                         lua_pop(L, 1);
562                 }
563         }
564
565         std::vector<std::pair<s16, u8> > slice_prob_list;
566         if (lua_istable(L, 5)) {
567                 lua_pushnil(L);
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));
573                         }
574
575                         lua_pop(L, 1);
576                 }
577         }
578
579         const char *s = lua_tostring(L, 4);
580         dschem.filename = std::string(s ? s : "");
581
582         if (!dschem.getSchematicFromMap(map, p1, p2)) {
583                 errorstream << "create_schematic: failed to get schematic "
584                         "from map" << std::endl;
585                 return 0;
586         }
587
588         dschem.applyProbabilities(p1, &prob_list, &slice_prob_list);
589
590         dschem.saveSchematicFile(ndef);
591         actionstream << "create_schematic: saved schematic file '"
592                 << dschem.filename << "'." << std::endl;
593
594         return 1;
595 }
596
597
598 // place_schematic(p, schematic, rotation, replacement)
599 int ModApiMapgen::l_place_schematic(lua_State *L)
600 {
601         DecoSchematic dschem;
602
603         Map *map = &(getEnv(L)->getMap());
604         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
605
606         v3s16 p = read_v3s16(L, 1);
607         if (!read_schematic(L, 2, &dschem, getServer(L)))
608                 return 0;
609
610         int rot = ROTATE_0;
611         if (lua_isstring(L, 3))
612                 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
613
614         dschem.rotation = (Rotation)rot;
615
616         if (lua_istable(L, 4)) {
617                 read_schematic_replacements(L, &dschem, 4);
618         }
619
620         bool force_placement = true;
621         if (lua_isboolean(L, 5))
622                 force_placement = lua_toboolean(L, 5);
623
624         if (!dschem.filename.empty()) {
625                 if (!dschem.loadSchematicFile()) {
626                         errorstream << "place_schematic: failed to load schematic file '"
627                                 << dschem.filename << "'" << std::endl;
628                         return 0;
629                 }
630                 dschem.resolveNodeNames(ndef);
631         }
632
633         dschem.placeStructure(map, p, force_placement);
634
635         return 1;
636 }
637
638 void ModApiMapgen::Initialize(lua_State *L, int top)
639 {
640         API_FCT(get_mapgen_object);
641
642         API_FCT(set_mapgen_params);
643         API_FCT(set_noiseparam_defaults);
644         API_FCT(set_gen_notify);
645
646         API_FCT(register_biome);
647         API_FCT(register_decoration);
648         API_FCT(register_ore);
649
650         API_FCT(create_schematic);
651         API_FCT(place_schematic);
652 }