]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mapgen.cpp
Fix g_settings not being included
[dragonfireclient.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 // minetest.get_mapgen_object(objectname)
82 // returns the requested object used during map generation
83 int ModApiMapgen::l_get_mapgen_object(lua_State *L)
84 {
85         const char *mgobjstr = lua_tostring(L, 1);
86
87         int mgobjint;
88         if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
89                 return 0;
90
91         enum MapgenObject mgobj = (MapgenObject)mgobjint;
92
93         EmergeManager *emerge = getServer(L)->getEmergeManager();
94         Mapgen *mg = emerge->getCurrentMapgen();
95         if (!mg)
96                 return 0;
97
98         size_t maplen = mg->csize.X * mg->csize.Z;
99
100         switch (mgobj) {
101                 case MGOBJ_VMANIP: {
102                         ManualMapVoxelManipulator *vm = mg->vm;
103
104                         // VoxelManip object
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);
109
110                         // emerged min pos
111                         push_v3s16(L, vm->m_area.MinEdge);
112
113                         // emerged max pos
114                         push_v3s16(L, vm->m_area.MaxEdge);
115
116                         return 3; }
117                 case MGOBJ_HEIGHTMAP: {
118                         if (!mg->heightmap)
119                                 return 0;
120
121                         lua_newtable(L);
122                         for (size_t i = 0; i != maplen; i++) {
123                                 lua_pushinteger(L, mg->heightmap[i]);
124                                 lua_rawseti(L, -2, i + 1);
125                         }
126
127                         return 1; }
128                 case MGOBJ_BIOMEMAP: {
129                         if (!mg->biomemap)
130                                 return 0;
131
132                         lua_newtable(L);
133                         for (size_t i = 0; i != maplen; i++) {
134                                 lua_pushinteger(L, mg->biomemap[i]);
135                                 lua_rawseti(L, -2, i + 1);
136                         }
137
138                         return 1; }
139                 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
140                 case MGOBJ_HUMIDMAP:
141                         if (strcmp(emerge->params.mg_name.c_str(), "v7"))
142                                 return 0;
143
144                         MapgenV7 *mgv7 = (MapgenV7 *)mg;
145
146                         float *arr = (mgobj == MGOBJ_HEATMAP) ?
147                                 mgv7->noise_heat->result : mgv7->noise_humidity->result;
148                         if (!arr)
149                                 return 0;
150
151                         lua_newtable(L);
152                         for (size_t i = 0; i != maplen; i++) {
153                                 lua_pushnumber(L, arr[i]);
154                                 lua_rawseti(L, -2, i + 1);
155                         }
156
157                         return 1; }
158                 case MGOBJ_GENNOTIFY: {
159                         lua_newtable(L);
160                         for (int i = 0; flagdesc_gennotify[i].name; i++) {
161                                 if (!(emerge->gennotify & flagdesc_gennotify[i].flag))
162                                         continue;
163
164                                 std::vector<v3s16> *posvec = mg->gen_notifications[i];
165                                 if (!posvec)
166                                         return 0;
167
168                                 lua_newtable(L);
169                                 for (unsigned int j = 0; j != posvec->size(); j++) {
170                                         push_v3s16(L, (*posvec)[j]);
171                                         lua_rawseti(L, -2, j + 1);
172                                 }
173                                 lua_setfield(L, -2, flagdesc_gennotify[i].name);
174
175                                 posvec->clear();
176                         }
177
178                         return 1; }
179         }
180
181         return 0;
182 }
183
184 // minetest.set_mapgen_params(params)
185 // set mapgen parameters
186 int ModApiMapgen::l_set_mapgen_params(lua_State *L)
187 {
188         if (!lua_istable(L, 1))
189                 return 0;
190
191         EmergeManager *emerge = getServer(L)->getEmergeManager();
192         ASSERT(emerge);
193
194         std::string flagstr;
195
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;
201         }
202
203         lua_getfield(L, 1, "seed");
204         if (lua_isnumber(L, -1))
205                 emerge->params.seed = lua_tointeger(L, -1);
206
207         lua_getfield(L, 1, "water_level");
208         if (lua_isnumber(L, -1))
209                 emerge->params.water_level = lua_tointeger(L, -1);
210
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;
217         }
218
219         lua_getfield(L, 1, "flags");
220         if (lua_isstring(L, -1)) {
221                 u32 flags, flagmask;
222
223                 flagstr = lua_tostring(L, -1);
224                 flags   = readFlagString(flagstr, flagdesc_mapgen, &flagmask);
225
226                 emerge->params.flags &= ~flagmask;
227                 emerge->params.flags |= flags;
228         }
229
230         return 0;
231 }
232
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)
236 {
237         NoiseParams np;
238         std::string val, name;
239
240         if (!lua_istable(L, 1))
241                 return 0;
242
243         lua_pushnil(L);
244         while (lua_next(L, 1)) {
245                 if (read_noiseparams_nc(L, -1, &np)) {
246                         if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
247                                 continue;
248                         if (!lua_isstring(L, -2))
249                                 continue;
250
251                         name = lua_tostring(L, -2);
252                         g_settings->setDefault(name, val);
253                 }
254                 lua_pop(L, 1);
255         }
256
257         return 0;
258 }
259
260 // set_gen_notify(string)
261 int ModApiMapgen::l_set_gen_notify(lua_State *L)
262 {
263         if (lua_isstring(L, 1)) {
264                 EmergeManager *emerge = getServer(L)->getEmergeManager();
265                 emerge->gennotify = readFlagString(lua_tostring(L, 1),
266                         flagdesc_gennotify, NULL);
267         }
268         return 0;
269 }
270
271 // register_biome({lots of stuff})
272 int ModApiMapgen::l_register_biome(lua_State *L)
273 {
274         int index = 1;
275         luaL_checktype(L, index, LUA_TTABLE);
276
277         BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
278         if (!bmgr) {
279                 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
280                 return 0;
281         }
282
283         enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
284                                 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
285         Biome *b = bmgr->createBiome(terrain);
286
287         b->name         = getstringfield_default(L, index, "name",
288                                                                                                 "<no 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",
292                                                                                                 "mapgen_dirt");
293         b->nname_water  = getstringfield_default(L, index, "node_water",
294                                                                                                 "mapgen_water_source");
295         b->nname_dust   = getstringfield_default(L, index, "node_dust",
296                                                                                                 "air");
297         b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
298                                                                                                 "mapgen_water_source");
299
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.);
306
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;
313
314         verbosestream << "register_biome: " << b->name << std::endl;
315         bmgr->addBiome(b);
316
317         return 0;
318 }
319
320 // register_decoration({lots of stuff})
321 int ModApiMapgen::l_register_decoration(lua_State *L)
322 {
323         int index = 1;
324         luaL_checktype(L, index, LUA_TTABLE);
325
326         EmergeManager *emerge = getServer(L)->getEmergeManager();
327         BiomeDefManager *bdef = emerge->biomedef;
328
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";
334                 return 0;
335         }
336
337         Decoration *deco = createDecoration(decotype);
338         if (!deco) {
339                 errorstream << "register_decoration: decoration placement type "
340                         << decotype << " not implemented";
341                 return 0;
342         }
343
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;
351                 delete deco;
352                 return 0;
353         }
354
355         lua_getfield(L, index, "noise_params");
356         deco->np = read_noiseparams(L, -1);
357         lua_pop(L, 1);
358
359         lua_getfield(L, index, "biomes");
360         if (lua_istable(L, -1)) {
361                 lua_pushnil(L);
362                 while (lua_next(L, -2)) {
363                         const char *s = lua_tostring(L, -1);
364                         u8 biomeid = bdef->getBiomeIdByName(s);
365                         if (biomeid)
366                                 deco->biomes.insert(biomeid);
367
368                         lua_pop(L, 1);
369                 }
370                 lua_pop(L, 1);
371         }
372
373         switch (decotype) {
374                 case DECO_SIMPLE: {
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);
382
383                         lua_getfield(L, index, "decoration");
384                         if (lua_istable(L, -1)) {
385                                 lua_pushnil(L);
386                                 while (lua_next(L, -2)) {
387                                         const char *s = lua_tostring(L, -1);
388                                         std::string str(s);
389                                         dsimple->decolist_names.push_back(str);
390
391                                         lua_pop(L, 1);
392                                 }
393                         } else if (lua_isstring(L, -1)) {
394                                 dsimple->deco_name = std::string(lua_tostring(L, -1));
395                         } else {
396                                 dsimple->deco_name = std::string("air");
397                         }
398                         lua_pop(L, 1);
399
400                         if (dsimple->deco_height <= 0) {
401                                 errorstream << "register_decoration: simple decoration height"
402                                         " must be greater than 0" << std::endl;
403                                 delete dsimple;
404                                 return 0;
405                         }
406
407                         break; }
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);
414
415                         lua_getfield(L, index, "replacements");
416                         if (lua_istable(L, -1)) {
417                                 int i = lua_gettop(L);
418                                 lua_pushnil(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);
423                                         lua_pop(L, 1);
424                                         lua_rawgeti(L, -1, 2);
425                                         std::string replace_to = lua_tostring(L, -1);
426                                         lua_pop(L, 1);
427                                         dschem->replacements[replace_from] = replace_to;
428                                         // removes value, keeps key for next iteration
429                                         lua_pop(L, 1);
430                                 }
431                         }
432                         lua_pop(L, 1);
433
434                         lua_getfield(L, index, "schematic");
435                         if (!read_schematic(L, -1, dschem, getServer(L))) {
436                                 delete dschem;
437                                 return 0;
438                         }
439                         lua_pop(L, -1);
440
441                         if (!dschem->filename.empty() && !dschem->loadSchematicFile()) {
442                                 errorstream << "register_decoration: failed to load schematic file '"
443                                         << dschem->filename << "'" << std::endl;
444                                 delete dschem;
445                                 return 0;
446                         }
447                         break; }
448                 case DECO_LSYSTEM: {
449                         //DecoLSystem *decolsystem = (DecoLSystem *)deco;
450
451                         break; }
452         }
453
454         emerge->decorations.push_back(deco);
455
456         verbosestream << "register_decoration: decoration '" << deco->getName()
457                 << "' registered" << std::endl;
458         return 0;
459 }
460
461 // register_ore({lots of stuff})
462 int ModApiMapgen::l_register_ore(lua_State *L)
463 {
464         int index = 1;
465         luaL_checktype(L, index, LUA_TTABLE);
466
467         EmergeManager *emerge = getServer(L)->getEmergeManager();
468
469         enum OreType oretype = (OreType)getenumfield(L, index,
470                                 "ore_type", es_OreType, ORE_SCATTER);
471         Ore *ore = createOre(oretype);
472         if (!ore) {
473                 errorstream << "register_ore: ore_type "
474                         << oretype << " not implemented";
475                 return 0;
476         }
477
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);
490                 lua_pushnil(L);
491                 while(lua_next(L, i) != 0) {
492                         ore->wherein_names.push_back(lua_tostring(L, -1));
493                         lua_pop(L, 1);
494                 }
495         } else if (lua_isstring(L, -1)) {
496                 ore->wherein_names.push_back(lua_tostring(L, -1));
497         } else {
498                 ore->wherein_names.push_back("");
499         }
500         lua_pop(L, 1);
501
502         lua_getfield(L, index, "noise_params");
503         ore->np = read_noiseparams(L, -1);
504         lua_pop(L, 1);
505
506         ore->noise = NULL;
507
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;
511                 delete ore;
512                 return 0;
513         }
514
515         emerge->ores.push_back(ore);
516
517         verbosestream << "register_ore: ore '" << ore->ore_name
518                 << "' registered" << std::endl;
519         return 0;
520 }
521
522 // create_schematic(p1, p2, probability_list, filename)
523 int ModApiMapgen::l_create_schematic(lua_State *L)
524 {
525         DecoSchematic dschem;
526
527         Map *map = &(getEnv(L)->getMap());
528         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
529
530         v3s16 p1 = read_v3s16(L, 1);
531         v3s16 p2 = read_v3s16(L, 2);
532         sortBoxVerticies(p1, p2);
533
534         std::vector<std::pair<v3s16, u8> > prob_list;
535         if (lua_istable(L, 3)) {
536                 lua_pushnil(L);
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);
541                                 lua_pop(L, 1);
542
543                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
544                                 prob_list.push_back(std::make_pair(pos, prob));
545                         }
546
547                         lua_pop(L, 1);
548                 }
549         }
550
551         std::vector<std::pair<s16, u8> > slice_prob_list;
552         if (lua_istable(L, 5)) {
553                 lua_pushnil(L);
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));
559                         }
560
561                         lua_pop(L, 1);
562                 }
563         }
564
565         const char *s = lua_tostring(L, 4);
566         dschem.filename = std::string(s ? s : "");
567
568         if (!dschem.getSchematicFromMap(map, p1, p2)) {
569                 errorstream << "create_schematic: failed to get schematic "
570                         "from map" << std::endl;
571                 return 0;
572         }
573
574         dschem.applyProbabilities(p1, &prob_list, &slice_prob_list);
575
576         dschem.saveSchematicFile(ndef);
577         actionstream << "create_schematic: saved schematic file '"
578                 << dschem.filename << "'." << std::endl;
579
580         return 1;
581 }
582
583
584 // place_schematic(p, schematic, rotation, replacement)
585 int ModApiMapgen::l_place_schematic(lua_State *L)
586 {
587         DecoSchematic dschem;
588
589         Map *map = &(getEnv(L)->getMap());
590         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
591
592         v3s16 p = read_v3s16(L, 1);
593         if (!read_schematic(L, 2, &dschem, getServer(L)))
594                 return 0;
595
596         int rot = ROTATE_0;
597         if (lua_isstring(L, 3))
598                 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
599
600         dschem.rotation = (Rotation)rot;
601
602         if (lua_istable(L, 4)) {
603                 lua_pushnil(L);
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);
608                         lua_pop(L, 1);
609                         lua_rawgeti(L, -1, 2);
610                         std::string replace_to = lua_tostring(L, -1);
611                         lua_pop(L, 1);
612                         dschem.replacements[replace_from] = replace_to;
613                         // removes value, keeps key for next iteration
614                         lua_pop(L, 1);
615                 }
616         }
617
618         bool force_placement = true;
619         if (lua_isboolean(L, 5))
620                 force_placement = lua_toboolean(L, 5);
621
622         if (!dschem.filename.empty()) {
623                 if (!dschem.loadSchematicFile()) {
624                         errorstream << "place_schematic: failed to load schematic file '"
625                                 << dschem.filename << "'" << std::endl;
626                         return 0;
627                 }
628                 dschem.resolveNodeNames(ndef);
629         }
630
631         dschem.placeStructure(map, p, force_placement);
632
633         return 1;
634 }
635
636 void ModApiMapgen::Initialize(lua_State *L, int top)
637 {
638         API_FCT(get_mapgen_object);
639
640         API_FCT(set_mapgen_params);
641         API_FCT(set_noiseparam_defaults);
642         API_FCT(set_gen_notify);
643
644         API_FCT(register_biome);
645         API_FCT(register_decoration);
646         API_FCT(register_ore);
647
648         API_FCT(create_schematic);
649         API_FCT(place_schematic);
650 }