]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mapgen.cpp
Update Mapgen VoxelManipulator on buffer invalidation
[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 // 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 // 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         u32 flags = 0, flagmask = 0;
196
197         lua_getfield(L, 1, "mgname");
198         if (lua_isstring(L, -1)) {
199                 emerge->params.mg_name = std::string(lua_tostring(L, -1));
200                 delete emerge->params.sparams;
201                 emerge->params.sparams = NULL;
202         }
203
204         lua_getfield(L, 1, "seed");
205         if (lua_isnumber(L, -1))
206                 emerge->params.seed = lua_tointeger(L, -1);
207
208         lua_getfield(L, 1, "water_level");
209         if (lua_isnumber(L, -1))
210                 emerge->params.water_level = lua_tointeger(L, -1);
211
212         lua_getfield(L, 1, "flagmask");
213         if (lua_isstring(L, -1)) {
214                 flagstr = lua_tostring(L, -1);
215                 emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL);
216                 errorstream << "set_mapgen_params(): flagmask field is deprecated, "
217                         "see lua_api.txt" << std::endl;
218         }
219
220         if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
221                 emerge->params.flags &= ~flagmask;
222                 emerge->params.flags |= flags;
223         }
224
225         return 0;
226 }
227
228 // set_noiseparam_defaults({np1={noise params}, ...})
229 // set default values for noise parameters if not present in global settings
230 int ModApiMapgen::l_set_noiseparam_defaults(lua_State *L)
231 {
232         NoiseParams np;
233         std::string val, name;
234
235         if (!lua_istable(L, 1))
236                 return 0;
237
238         lua_pushnil(L);
239         while (lua_next(L, 1)) {
240                 if (read_noiseparams_nc(L, -1, &np)) {
241                         if (!serializeStructToString(&val, NOISEPARAMS_FMT_STR, &np))
242                                 continue;
243                         if (!lua_isstring(L, -2))
244                                 continue;
245
246                         name = lua_tostring(L, -2);
247                         g_settings->setDefault(name, val);
248                 }
249                 lua_pop(L, 1);
250         }
251
252         return 0;
253 }
254
255 // set_gen_notify(string)
256 int ModApiMapgen::l_set_gen_notify(lua_State *L)
257 {
258         u32 flags = 0, flagmask = 0;
259
260         if (read_flags(L, 1, flagdesc_gennotify, &flags, &flagmask)) {
261                 EmergeManager *emerge = getServer(L)->getEmergeManager();
262                 emerge->gennotify = flags;
263         }
264
265         return 0;
266 }
267
268 // register_biome({lots of stuff})
269 int ModApiMapgen::l_register_biome(lua_State *L)
270 {
271         int index = 1;
272         luaL_checktype(L, index, LUA_TTABLE);
273
274         BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef;
275         if (!bmgr) {
276                 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
277                 return 0;
278         }
279
280         enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
281                                 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
282         Biome *b = bmgr->createBiome(terrain);
283
284         b->name         = getstringfield_default(L, index, "name",
285                                                                                                 "<no name>");
286         b->nname_top    = getstringfield_default(L, index, "node_top",
287                                                                                                 "mapgen_dirt_with_grass");
288         b->nname_filler = getstringfield_default(L, index, "node_filler",
289                                                                                                 "mapgen_dirt");
290         b->nname_water  = getstringfield_default(L, index, "node_water",
291                                                                                                 "mapgen_water_source");
292         b->nname_dust   = getstringfield_default(L, index, "node_dust",
293                                                                                                 "air");
294         b->nname_dust_water = getstringfield_default(L, index, "node_dust_water",
295                                                                                                 "mapgen_water_source");
296
297         b->depth_top      = getintfield_default(L, index, "depth_top",    1);
298         b->depth_filler   = getintfield_default(L, index, "depth_filler", 3);
299         b->height_min     = getintfield_default(L, index, "height_min",   0);
300         b->height_max     = getintfield_default(L, index, "height_max",   0);
301         b->heat_point     = getfloatfield_default(L, index, "heat_point",     0.);
302         b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.);
303
304         b->flags        = 0; //reserved
305         b->c_top        = CONTENT_IGNORE;
306         b->c_filler     = CONTENT_IGNORE;
307         b->c_water      = CONTENT_IGNORE;
308         b->c_dust       = CONTENT_IGNORE;
309         b->c_dust_water = CONTENT_IGNORE;
310
311         verbosestream << "register_biome: " << b->name << std::endl;
312         bmgr->addBiome(b);
313
314         return 0;
315 }
316
317 // register_decoration({lots of stuff})
318 int ModApiMapgen::l_register_decoration(lua_State *L)
319 {
320         int index = 1;
321         luaL_checktype(L, index, LUA_TTABLE);
322
323         EmergeManager *emerge = getServer(L)->getEmergeManager();
324         BiomeDefManager *bdef = emerge->biomedef;
325
326         enum DecorationType decotype = (DecorationType)getenumfield(L, index,
327                                 "deco_type", es_DecorationType, 0);
328         if (decotype == 0) {
329                 errorstream << "register_decoration: unrecognized "
330                         "decoration placement type";
331                 return 0;
332         }
333
334         Decoration *deco = createDecoration(decotype);
335         if (!deco) {
336                 errorstream << "register_decoration: decoration placement type "
337                         << decotype << " not implemented";
338                 return 0;
339         }
340
341         deco->c_place_on    = CONTENT_IGNORE;
342         deco->place_on_name = getstringfield_default(L, index, "place_on", "ignore");
343         deco->fill_ratio    = getfloatfield_default(L, index, "fill_ratio", 0.02);
344         deco->sidelen       = getintfield_default(L, index, "sidelen", 8);
345         if (deco->sidelen <= 0) {
346                 errorstream << "register_decoration: sidelen must be "
347                         "greater than 0" << std::endl;
348                 delete deco;
349                 return 0;
350         }
351
352         lua_getfield(L, index, "noise_params");
353         deco->np = read_noiseparams(L, -1);
354         lua_pop(L, 1);
355
356         lua_getfield(L, index, "biomes");
357         if (lua_istable(L, -1)) {
358                 lua_pushnil(L);
359                 while (lua_next(L, -2)) {
360                         const char *s = lua_tostring(L, -1);
361                         u8 biomeid = bdef->getBiomeIdByName(s);
362                         if (biomeid)
363                                 deco->biomes.insert(biomeid);
364
365                         lua_pop(L, 1);
366                 }
367                 lua_pop(L, 1);
368         }
369
370         switch (decotype) {
371                 case DECO_SIMPLE: {
372                         DecoSimple *dsimple = (DecoSimple *)deco;
373                         dsimple->c_deco     = CONTENT_IGNORE;
374                         dsimple->c_spawnby  = CONTENT_IGNORE;
375                         dsimple->spawnby_name    = getstringfield_default(L, index, "spawn_by", "air");
376                         dsimple->deco_height     = getintfield_default(L, index, "height", 1);
377                         dsimple->deco_height_max = getintfield_default(L, index, "height_max", 0);
378                         dsimple->nspawnby        = getintfield_default(L, index, "num_spawn_by", -1);
379
380                         lua_getfield(L, index, "decoration");
381                         if (lua_istable(L, -1)) {
382                                 lua_pushnil(L);
383                                 while (lua_next(L, -2)) {
384                                         const char *s = lua_tostring(L, -1);
385                                         std::string str(s);
386                                         dsimple->decolist_names.push_back(str);
387
388                                         lua_pop(L, 1);
389                                 }
390                         } else if (lua_isstring(L, -1)) {
391                                 dsimple->deco_name = std::string(lua_tostring(L, -1));
392                         } else {
393                                 dsimple->deco_name = std::string("air");
394                         }
395                         lua_pop(L, 1);
396
397                         if (dsimple->deco_height <= 0) {
398                                 errorstream << "register_decoration: simple decoration height"
399                                         " must be greater than 0" << std::endl;
400                                 delete dsimple;
401                                 return 0;
402                         }
403
404                         break; }
405                 case DECO_SCHEMATIC: {
406                         DecoSchematic *dschem = (DecoSchematic *)deco;
407
408                         dschem->flags = 0;
409                         getflagsfield(L, index, "flags", flagdesc_deco_schematic,
410                                 &dschem->flags, NULL);
411
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->nthresh        = getfloatfield_default(L, index, "noise_threshhold", 0.);
486         ore->flags          = 0;
487         getflagsfield(L, index, "flags", flagdesc_ore, &ore->flags, NULL);
488
489         lua_getfield(L, index, "wherein");
490         if (lua_istable(L, -1)) {
491                 int  i = lua_gettop(L);
492                 lua_pushnil(L);
493                 while(lua_next(L, i) != 0) {
494                         ore->wherein_names.push_back(lua_tostring(L, -1));
495                         lua_pop(L, 1);
496                 }
497         } else if (lua_isstring(L, -1)) {
498                 ore->wherein_names.push_back(lua_tostring(L, -1));
499         } else {
500                 ore->wherein_names.push_back("");
501         }
502         lua_pop(L, 1);
503
504         lua_getfield(L, index, "noise_params");
505         ore->np = read_noiseparams(L, -1);
506         lua_pop(L, 1);
507
508         ore->noise = NULL;
509
510         if (ore->clust_scarcity <= 0 || ore->clust_num_ores <= 0) {
511                 errorstream << "register_ore: clust_scarcity and clust_num_ores"
512                         "must be greater than 0" << std::endl;
513                 delete ore;
514                 return 0;
515         }
516
517         emerge->ores.push_back(ore);
518
519         verbosestream << "register_ore: ore '" << ore->ore_name
520                 << "' registered" << std::endl;
521         return 0;
522 }
523
524 // create_schematic(p1, p2, probability_list, filename)
525 int ModApiMapgen::l_create_schematic(lua_State *L)
526 {
527         DecoSchematic dschem;
528
529         Map *map = &(getEnv(L)->getMap());
530         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
531
532         v3s16 p1 = read_v3s16(L, 1);
533         v3s16 p2 = read_v3s16(L, 2);
534         sortBoxVerticies(p1, p2);
535
536         std::vector<std::pair<v3s16, u8> > prob_list;
537         if (lua_istable(L, 3)) {
538                 lua_pushnil(L);
539                 while (lua_next(L, 3)) {
540                         if (lua_istable(L, -1)) {
541                                 lua_getfield(L, -1, "pos");
542                                 v3s16 pos = read_v3s16(L, -1);
543                                 lua_pop(L, 1);
544
545                                 u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
546                                 prob_list.push_back(std::make_pair(pos, prob));
547                         }
548
549                         lua_pop(L, 1);
550                 }
551         }
552
553         std::vector<std::pair<s16, u8> > slice_prob_list;
554         if (lua_istable(L, 5)) {
555                 lua_pushnil(L);
556                 while (lua_next(L, 5)) {
557                         if (lua_istable(L, -1)) {
558                                 s16 ypos = getintfield_default(L, -1, "ypos", 0);
559                                 u8 prob  = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS);
560                                 slice_prob_list.push_back(std::make_pair(ypos, prob));
561                         }
562
563                         lua_pop(L, 1);
564                 }
565         }
566
567         const char *s = lua_tostring(L, 4);
568         dschem.filename = std::string(s ? s : "");
569
570         if (!dschem.getSchematicFromMap(map, p1, p2)) {
571                 errorstream << "create_schematic: failed to get schematic "
572                         "from map" << std::endl;
573                 return 0;
574         }
575
576         dschem.applyProbabilities(p1, &prob_list, &slice_prob_list);
577
578         dschem.saveSchematicFile(ndef);
579         actionstream << "create_schematic: saved schematic file '"
580                 << dschem.filename << "'." << std::endl;
581
582         return 1;
583 }
584
585
586 // place_schematic(p, schematic, rotation, replacement)
587 int ModApiMapgen::l_place_schematic(lua_State *L)
588 {
589         DecoSchematic dschem;
590
591         Map *map = &(getEnv(L)->getMap());
592         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
593
594         v3s16 p = read_v3s16(L, 1);
595         if (!read_schematic(L, 2, &dschem, getServer(L)))
596                 return 0;
597
598         int rot = ROTATE_0;
599         if (lua_isstring(L, 3))
600                 string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3)));
601
602         dschem.rotation = (Rotation)rot;
603
604         if (lua_istable(L, 4)) {
605                 lua_pushnil(L);
606                 while (lua_next(L, 4) != 0) {
607                         // key at index -2 and value at index -1
608                         lua_rawgeti(L, -1, 1);
609                         std::string replace_from = lua_tostring(L, -1);
610                         lua_pop(L, 1);
611                         lua_rawgeti(L, -1, 2);
612                         std::string replace_to = lua_tostring(L, -1);
613                         lua_pop(L, 1);
614                         dschem.replacements[replace_from] = replace_to;
615                         // removes value, keeps key for next iteration
616                         lua_pop(L, 1);
617                 }
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 }