]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_env.cpp
Weather support
[dragonfireclient.git] / src / script / lua_api / l_env.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 "cpp_api/scriptapi.h"
21 #include "lua_api/l_base.h"
22 #include "lua_api/l_env.h"
23 #include "lua_api/l_vmanip.h"
24 #include "environment.h"
25 #include "server.h"
26 #include "daynightratio.h"
27 #include "util/pointedthing.h"
28 #include "content_sao.h"
29
30 #include "common/c_converter.h"
31 #include "common/c_content.h"
32 #include "common/c_internal.h"
33 #include "lua_api/l_nodemeta.h"
34 #include "lua_api/l_nodetimer.h"
35 #include "lua_api/l_noise.h"
36 #include "treegen.h"
37 #include "pathfinder.h"
38 #include "emerge.h"
39 #include "mapgen_v7.h"
40
41
42 #define GET_ENV_PTR ServerEnvironment* env =                                   \
43                                 dynamic_cast<ServerEnvironment*>(getEnv(L));                   \
44                                 if( env == NULL) return 0
45                                 
46 struct EnumString ModApiEnvMod::es_MapgenObject[] =
47 {
48         {MGOBJ_VMANIP,    "voxelmanip"},
49         {MGOBJ_HEIGHTMAP, "heightmap"},
50         {MGOBJ_BIOMEMAP,  "biomemap"},
51         {MGOBJ_HEATMAP,   "heatmap"},
52         {MGOBJ_HUMIDMAP,  "humiditymap"},
53         {0, NULL},
54 };
55
56
57 ///////////////////////////////////////////////////////////////////////////////
58
59
60 void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
61                 u32 active_object_count, u32 active_object_count_wider)
62 {
63         ScriptApi* scriptIface = SERVER_TO_SA(env);
64         scriptIface->realityCheck();
65
66         lua_State* L = scriptIface->getStack();
67         assert(lua_checkstack(L, 20));
68         StackUnroller stack_unroller(L);
69
70         // Get minetest.registered_abms
71         lua_getglobal(L, "minetest");
72         lua_getfield(L, -1, "registered_abms");
73         luaL_checktype(L, -1, LUA_TTABLE);
74         int registered_abms = lua_gettop(L);
75
76         // Get minetest.registered_abms[m_id]
77         lua_pushnumber(L, m_id);
78         lua_gettable(L, registered_abms);
79         if(lua_isnil(L, -1))
80                 assert(0);
81
82         // Call action
83         luaL_checktype(L, -1, LUA_TTABLE);
84         lua_getfield(L, -1, "action");
85         luaL_checktype(L, -1, LUA_TFUNCTION);
86         push_v3s16(L, p);
87         pushnode(L, n, env->getGameDef()->ndef());
88         lua_pushnumber(L, active_object_count);
89         lua_pushnumber(L, active_object_count_wider);
90         if(lua_pcall(L, 4, 0, 0))
91                 script_error(L, "error: %s", lua_tostring(L, -1));
92 }
93
94 // Exported functions
95
96 // minetest.set_node(pos, node)
97 // pos = {x=num, y=num, z=num}
98 int ModApiEnvMod::l_set_node(lua_State *L)
99 {
100         GET_ENV_PTR;
101
102         INodeDefManager *ndef = env->getGameDef()->ndef();
103         // parameters
104         v3s16 pos = read_v3s16(L, 1);
105         MapNode n = readnode(L, 2, ndef);
106         // Do it
107         bool succeeded = env->setNode(pos, n);
108         lua_pushboolean(L, succeeded);
109         return 1;
110 }
111
112 int ModApiEnvMod::l_add_node(lua_State *L)
113 {
114         return l_set_node(L);
115 }
116
117 // minetest.remove_node(pos)
118 // pos = {x=num, y=num, z=num}
119 int ModApiEnvMod::l_remove_node(lua_State *L)
120 {
121         GET_ENV_PTR;
122
123         // parameters
124         v3s16 pos = read_v3s16(L, 1);
125         // Do it
126         bool succeeded = env->removeNode(pos);
127         lua_pushboolean(L, succeeded);
128         return 1;
129 }
130
131 // minetest.get_node(pos)
132 // pos = {x=num, y=num, z=num}
133 int ModApiEnvMod::l_get_node(lua_State *L)
134 {
135         GET_ENV_PTR;
136
137         // pos
138         v3s16 pos = read_v3s16(L, 1);
139         // Do it
140         MapNode n = env->getMap().getNodeNoEx(pos);
141         // Return node
142         pushnode(L, n, env->getGameDef()->ndef());
143         return 1;
144 }
145
146 // minetest.get_node_or_nil(pos)
147 // pos = {x=num, y=num, z=num}
148 int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
149 {
150         GET_ENV_PTR;
151
152         // pos
153         v3s16 pos = read_v3s16(L, 1);
154         // Do it
155         try{
156                 MapNode n = env->getMap().getNode(pos);
157                 // Return node
158                 pushnode(L, n, env->getGameDef()->ndef());
159                 return 1;
160         } catch(InvalidPositionException &e)
161         {
162                 lua_pushnil(L);
163                 return 1;
164         }
165 }
166
167 // minetest.get_node_light(pos, timeofday)
168 // pos = {x=num, y=num, z=num}
169 // timeofday: nil = current time, 0 = night, 0.5 = day
170 int ModApiEnvMod::l_get_node_light(lua_State *L)
171 {
172         GET_ENV_PTR;
173
174         // Do it
175         v3s16 pos = read_v3s16(L, 1);
176         u32 time_of_day = env->getTimeOfDay();
177         if(lua_isnumber(L, 2))
178                 time_of_day = 24000.0 * lua_tonumber(L, 2);
179         time_of_day %= 24000;
180         u32 dnr = time_to_daynight_ratio(time_of_day, true);
181         try{
182                 MapNode n = env->getMap().getNode(pos);
183                 INodeDefManager *ndef = env->getGameDef()->ndef();
184                 lua_pushinteger(L, n.getLightBlend(dnr, ndef));
185                 return 1;
186         } catch(InvalidPositionException &e)
187         {
188                 lua_pushnil(L);
189                 return 1;
190         }
191 }
192
193 // minetest.place_node(pos, node)
194 // pos = {x=num, y=num, z=num}
195 int ModApiEnvMod::l_place_node(lua_State *L)
196 {
197         GET_ENV_PTR;
198
199         v3s16 pos = read_v3s16(L, 1);
200         MapNode n = readnode(L, 2, env->getGameDef()->ndef());
201
202         // Don't attempt to load non-loaded area as of now
203         MapNode n_old = env->getMap().getNodeNoEx(pos);
204         if(n_old.getContent() == CONTENT_IGNORE){
205                 lua_pushboolean(L, false);
206                 return 1;
207         }
208         // Create item to place
209         INodeDefManager *ndef = getServer(L)->ndef();
210         IItemDefManager *idef = getServer(L)->idef();
211         ItemStack item(ndef->get(n).name, 1, 0, "", idef);
212         // Make pointed position
213         PointedThing pointed;
214         pointed.type = POINTEDTHING_NODE;
215         pointed.node_abovesurface = pos;
216         pointed.node_undersurface = pos + v3s16(0,-1,0);
217         // Place it with a NULL placer (appears in Lua as a non-functional
218         // ObjectRef)
219         bool success = get_scriptapi(L)->item_OnPlace(item, NULL, pointed);
220         lua_pushboolean(L, success);
221         return 1;
222 }
223
224 // minetest.dig_node(pos)
225 // pos = {x=num, y=num, z=num}
226 int ModApiEnvMod::l_dig_node(lua_State *L)
227 {
228         GET_ENV_PTR;
229
230         v3s16 pos = read_v3s16(L, 1);
231
232         // Don't attempt to load non-loaded area as of now
233         MapNode n = env->getMap().getNodeNoEx(pos);
234         if(n.getContent() == CONTENT_IGNORE){
235                 lua_pushboolean(L, false);
236                 return 1;
237         }
238         // Dig it out with a NULL digger (appears in Lua as a
239         // non-functional ObjectRef)
240         bool success = get_scriptapi(L)->node_on_dig(pos, n, NULL);
241         lua_pushboolean(L, success);
242         return 1;
243 }
244
245 // minetest.punch_node(pos)
246 // pos = {x=num, y=num, z=num}
247 int ModApiEnvMod::l_punch_node(lua_State *L)
248 {
249         GET_ENV_PTR;
250
251         v3s16 pos = read_v3s16(L, 1);
252
253         // Don't attempt to load non-loaded area as of now
254         MapNode n = env->getMap().getNodeNoEx(pos);
255         if(n.getContent() == CONTENT_IGNORE){
256                 lua_pushboolean(L, false);
257                 return 1;
258         }
259         // Punch it with a NULL puncher (appears in Lua as a non-functional
260         // ObjectRef)
261         bool success = get_scriptapi(L)->node_on_punch(pos, n, NULL);
262         lua_pushboolean(L, success);
263         return 1;
264 }
265
266 // minetest.get_node_max_level(pos)
267 // pos = {x=num, y=num, z=num}
268 int ModApiEnvMod::l_get_node_max_level(lua_State *L)
269 {
270         GET_ENV_PTR;
271
272         v3s16 pos = read_v3s16(L, 1);
273         MapNode n = env->getMap().getNodeNoEx(pos);
274         lua_pushnumber(L, n.getMaxLevel(env->getGameDef()->ndef()));
275         return 1;
276 }
277
278 // minetest.get_node_level(pos)
279 // pos = {x=num, y=num, z=num}
280 int ModApiEnvMod::l_get_node_level(lua_State *L)
281 {
282         GET_ENV_PTR;
283
284         v3s16 pos = read_v3s16(L, 1);
285         MapNode n = env->getMap().getNodeNoEx(pos);
286         lua_pushnumber(L, n.getLevel(env->getGameDef()->ndef()));
287         return 1;
288 }
289
290 // minetest.add_node_level(pos, level)
291 // pos = {x=num, y=num, z=num}
292 // level: 0..8
293 int ModApiEnvMod::l_add_node_level(lua_State *L)
294 {
295         GET_ENV_PTR;
296
297         v3s16 pos = read_v3s16(L, 1);
298         u8 level = 1;
299         if(lua_isnumber(L, 2))
300                 level = lua_tonumber(L, 2);
301         MapNode n = env->getMap().getNodeNoEx(pos);
302         lua_pushnumber(L, n.addLevel(env->getGameDef()->ndef(), level));
303         env->setNode(pos, n);
304         return 1;
305 }
306
307
308 // minetest.get_meta(pos)
309 int ModApiEnvMod::l_get_meta(lua_State *L)
310 {
311         GET_ENV_PTR;
312
313         // Do it
314         v3s16 p = read_v3s16(L, 1);
315         NodeMetaRef::create(L, p, env);
316         return 1;
317 }
318
319 // minetest.get_node_timer(pos)
320 int ModApiEnvMod::l_get_node_timer(lua_State *L)
321 {
322         GET_ENV_PTR;
323
324         // Do it
325         v3s16 p = read_v3s16(L, 1);
326         NodeTimerRef::create(L, p, env);
327         return 1;
328 }
329
330 // minetest.add_entity(pos, entityname) -> ObjectRef or nil
331 // pos = {x=num, y=num, z=num}
332 int ModApiEnvMod::l_add_entity(lua_State *L)
333 {
334         GET_ENV_PTR;
335
336         // pos
337         v3f pos = checkFloatPos(L, 1);
338         // content
339         const char *name = luaL_checkstring(L, 2);
340         // Do it
341         ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
342         int objectid = env->addActiveObject(obj);
343         // If failed to add, return nothing (reads as nil)
344         if(objectid == 0)
345                 return 0;
346         // Return ObjectRef
347         get_scriptapi(L)->objectrefGetOrCreate(obj);
348         return 1;
349 }
350
351 // minetest.add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
352 // pos = {x=num, y=num, z=num}
353 int ModApiEnvMod::l_add_item(lua_State *L)
354 {
355         GET_ENV_PTR;
356
357         // pos
358         //v3f pos = checkFloatPos(L, 1);
359         // item
360         ItemStack item = read_item(L, 2,getServer(L));
361         if(item.empty() || !item.isKnown(getServer(L)->idef()))
362                 return 0;
363         // Use minetest.spawn_item to spawn a __builtin:item
364         lua_getglobal(L, "minetest");
365         lua_getfield(L, -1, "spawn_item");
366         if(lua_isnil(L, -1))
367                 return 0;
368         lua_pushvalue(L, 1);
369         lua_pushstring(L, item.getItemString().c_str());
370         if(lua_pcall(L, 2, 1, 0))
371                 script_error(L, "error: %s", lua_tostring(L, -1));
372         return 1;
373         /*lua_pushvalue(L, 1);
374         lua_pushstring(L, "__builtin:item");
375         lua_pushstring(L, item.getItemString().c_str());
376         return l_add_entity(L);*/
377         /*// Do it
378         ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
379         int objectid = env->addActiveObject(obj);
380         // If failed to add, return nothing (reads as nil)
381         if(objectid == 0)
382                 return 0;
383         // Return ObjectRef
384         objectrefGetOrCreate(L, obj);
385         return 1;*/
386 }
387
388 // minetest.get_player_by_name(name)
389 int ModApiEnvMod::l_get_player_by_name(lua_State *L)
390 {
391         GET_ENV_PTR;
392
393         // Do it
394         const char *name = luaL_checkstring(L, 1);
395         Player *player = env->getPlayer(name);
396         if(player == NULL){
397                 lua_pushnil(L);
398                 return 1;
399         }
400         PlayerSAO *sao = player->getPlayerSAO();
401         if(sao == NULL){
402                 lua_pushnil(L);
403                 return 1;
404         }
405         // Put player on stack
406         get_scriptapi(L)->objectrefGetOrCreate(sao);
407         return 1;
408 }
409
410 // minetest.get_objects_inside_radius(pos, radius)
411 int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
412 {
413         // Get the table insert function
414         lua_getglobal(L, "table");
415         lua_getfield(L, -1, "insert");
416         int table_insert = lua_gettop(L);
417
418         GET_ENV_PTR;
419
420         // Do it
421         v3f pos = checkFloatPos(L, 1);
422         float radius = luaL_checknumber(L, 2) * BS;
423         std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
424         lua_newtable(L);
425         int table = lua_gettop(L);
426         for(std::set<u16>::const_iterator
427                         i = ids.begin(); i != ids.end(); i++){
428                 ServerActiveObject *obj = env->getActiveObject(*i);
429                 // Insert object reference into table
430                 lua_pushvalue(L, table_insert);
431                 lua_pushvalue(L, table);
432                 get_scriptapi(L)->objectrefGetOrCreate(obj);
433                 if(lua_pcall(L, 2, 0, 0))
434                         script_error(L, "error: %s", lua_tostring(L, -1));
435         }
436         return 1;
437 }
438
439 // minetest.set_timeofday(val)
440 // val = 0...1
441 int ModApiEnvMod::l_set_timeofday(lua_State *L)
442 {
443         GET_ENV_PTR;
444
445         // Do it
446         float timeofday_f = luaL_checknumber(L, 1);
447         assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
448         int timeofday_mh = (int)(timeofday_f * 24000.0);
449         // This should be set directly in the environment but currently
450         // such changes aren't immediately sent to the clients, so call
451         // the server instead.
452         //env->setTimeOfDay(timeofday_mh);
453         getServer(L)->setTimeOfDay(timeofday_mh);
454         return 0;
455 }
456
457 // minetest.get_timeofday() -> 0...1
458 int ModApiEnvMod::l_get_timeofday(lua_State *L)
459 {
460         GET_ENV_PTR;
461
462         // Do it
463         int timeofday_mh = env->getTimeOfDay();
464         float timeofday_f = (float)timeofday_mh / 24000.0;
465         lua_pushnumber(L, timeofday_f);
466         return 1;
467 }
468
469
470 // minetest.find_node_near(pos, radius, nodenames) -> pos or nil
471 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
472 int ModApiEnvMod::l_find_node_near(lua_State *L)
473 {
474         GET_ENV_PTR;
475
476         INodeDefManager *ndef = getServer(L)->ndef();
477         v3s16 pos = read_v3s16(L, 1);
478         int radius = luaL_checkinteger(L, 2);
479         std::set<content_t> filter;
480         if(lua_istable(L, 3)){
481                 int table = 3;
482                 lua_pushnil(L);
483                 while(lua_next(L, table) != 0){
484                         // key at index -2 and value at index -1
485                         luaL_checktype(L, -1, LUA_TSTRING);
486                         ndef->getIds(lua_tostring(L, -1), filter);
487                         // removes value, keeps key for next iteration
488                         lua_pop(L, 1);
489                 }
490         } else if(lua_isstring(L, 3)){
491                 ndef->getIds(lua_tostring(L, 3), filter);
492         }
493
494         for(int d=1; d<=radius; d++){
495                 std::list<v3s16> list;
496                 getFacePositions(list, d);
497                 for(std::list<v3s16>::iterator i = list.begin();
498                                 i != list.end(); ++i){
499                         v3s16 p = pos + (*i);
500                         content_t c = env->getMap().getNodeNoEx(p).getContent();
501                         if(filter.count(c) != 0){
502                                 push_v3s16(L, p);
503                                 return 1;
504                         }
505                 }
506         }
507         return 0;
508 }
509
510 // minetest.find_nodes_in_area(minp, maxp, nodenames) -> list of positions
511 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
512 int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
513 {
514         GET_ENV_PTR;
515
516         INodeDefManager *ndef = getServer(L)->ndef();
517         v3s16 minp = read_v3s16(L, 1);
518         v3s16 maxp = read_v3s16(L, 2);
519         std::set<content_t> filter;
520         if(lua_istable(L, 3)){
521                 int table = 3;
522                 lua_pushnil(L);
523                 while(lua_next(L, table) != 0){
524                         // key at index -2 and value at index -1
525                         luaL_checktype(L, -1, LUA_TSTRING);
526                         ndef->getIds(lua_tostring(L, -1), filter);
527                         // removes value, keeps key for next iteration
528                         lua_pop(L, 1);
529                 }
530         } else if(lua_isstring(L, 3)){
531                 ndef->getIds(lua_tostring(L, 3), filter);
532         }
533
534         // Get the table insert function
535         lua_getglobal(L, "table");
536         lua_getfield(L, -1, "insert");
537         int table_insert = lua_gettop(L);
538
539         lua_newtable(L);
540         int table = lua_gettop(L);
541         for(s16 x=minp.X; x<=maxp.X; x++)
542         for(s16 y=minp.Y; y<=maxp.Y; y++)
543         for(s16 z=minp.Z; z<=maxp.Z; z++)
544         {
545                 v3s16 p(x,y,z);
546                 content_t c = env->getMap().getNodeNoEx(p).getContent();
547                 if(filter.count(c) != 0){
548                         lua_pushvalue(L, table_insert);
549                         lua_pushvalue(L, table);
550                         push_v3s16(L, p);
551                         if(lua_pcall(L, 2, 0, 0))
552                                 script_error(L, "error: %s", lua_tostring(L, -1));
553                 }
554         }
555         return 1;
556 }
557
558 // minetest.get_perlin(seeddiff, octaves, persistence, scale)
559 // returns world-specific PerlinNoise
560 int ModApiEnvMod::l_get_perlin(lua_State *L)
561 {
562         GET_ENV_PTR;
563
564         int seeddiff = luaL_checkint(L, 1);
565         int octaves = luaL_checkint(L, 2);
566         float persistence = luaL_checknumber(L, 3);
567         float scale = luaL_checknumber(L, 4);
568
569         LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
570         *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
571         luaL_getmetatable(L, "PerlinNoise");
572         lua_setmetatable(L, -2);
573         return 1;
574 }
575
576 // minetest.get_perlin_map(noiseparams, size)
577 // returns world-specific PerlinNoiseMap
578 int ModApiEnvMod::l_get_perlin_map(lua_State *L)
579 {
580         GET_ENV_PTR;
581
582         NoiseParams *np = read_noiseparams(L, 1);
583         if (!np)
584                 return 0;
585         v3s16 size = read_v3s16(L, 2);
586
587         int seed = (int)(env->getServerMap().getSeed());
588         LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(np, seed, size);
589         *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
590         luaL_getmetatable(L, "PerlinNoiseMap");
591         lua_setmetatable(L, -2);
592         return 1;
593 }
594
595 // minetest.get_voxel_manip()
596 // returns voxel manipulator
597 int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
598 {
599         GET_ENV_PTR;
600
601         Map *map = &(env->getMap());
602         LuaVoxelManip *o = new LuaVoxelManip(map);
603         
604         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
605         luaL_getmetatable(L, "VoxelManip");
606         lua_setmetatable(L, -2);
607         return 1;
608 }
609
610 // minetest.get_mapgen_object(objectname)
611 // returns the requested object used during map generation
612 int ModApiEnvMod::l_get_mapgen_object(lua_State *L)
613 {
614         const char *mgobjstr = lua_tostring(L, 1);
615         
616         int mgobjint;
617         if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : ""))
618                 return 0;
619                 
620         enum MapgenObject mgobj = (MapgenObject)mgobjint;
621
622         EmergeManager *emerge = getServer(L)->getEmergeManager();
623         Mapgen *mg = emerge->getCurrentMapgen();
624         if (!mg)
625                 return 0;
626         
627         size_t maplen = mg->csize.X * mg->csize.Z;
628         
629         int nargs = 1;
630         
631         switch (mgobj) {
632                 case MGOBJ_VMANIP: {
633                         ManualMapVoxelManipulator *vm = mg->vm;
634                         
635                         // VoxelManip object
636                         LuaVoxelManip *o = new LuaVoxelManip(vm, true);
637                         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
638                         luaL_getmetatable(L, "VoxelManip");
639                         lua_setmetatable(L, -2);
640                         
641                         // emerged min pos
642                         push_v3s16(L, vm->m_area.MinEdge);
643
644                         // emerged max pos
645                         push_v3s16(L, vm->m_area.MaxEdge);
646                         
647                         nargs = 3;
648                         
649                         break; }
650                 case MGOBJ_HEIGHTMAP: {
651                         if (!mg->heightmap)
652                                 return 0;
653                         
654                         lua_newtable(L);
655                         for (size_t i = 0; i != maplen; i++) {
656                                 lua_pushinteger(L, mg->heightmap[i]);
657                                 lua_rawseti(L, -2, i + 1);
658                         }
659                         break; }
660                 case MGOBJ_BIOMEMAP: {
661                         if (!mg->biomemap)
662                                 return 0;
663                         
664                         lua_newtable(L);
665                         for (size_t i = 0; i != maplen; i++) {
666                                 lua_pushinteger(L, mg->biomemap[i]);
667                                 lua_rawseti(L, -2, i + 1);
668                         }
669                         break; }
670                 case MGOBJ_HEATMAP: { // Mapgen V7 specific objects
671                 case MGOBJ_HUMIDMAP:
672                         if (strcmp(emerge->params->mg_name.c_str(), "v7"))
673                                 return 0;
674                         
675                         MapgenV7 *mgv7 = (MapgenV7 *)mg;
676
677                         float *arr = (mgobj == MGOBJ_HEATMAP) ? 
678                                 mgv7->noise_heat->result : mgv7->noise_humidity->result;
679                         if (!arr)
680                                 return 0;
681                         
682                         lua_newtable(L);
683                         for (size_t i = 0; i != maplen; i++) {
684                                 lua_pushnumber(L, arr[i]);
685                                 lua_rawseti(L, -2, i + 1);
686                         }
687                         break; }
688         }
689         
690         return nargs;
691 }
692
693 // minetest.set_mapgen_params(params)
694 // set mapgen parameters
695 int ModApiEnvMod::l_set_mapgen_params(lua_State *L)
696 {
697         if (!lua_istable(L, 1))
698                 return 0;
699
700         EmergeManager *emerge = getServer(L)->getEmergeManager();
701         if (emerge->mapgen.size())
702                 return 0;
703         
704         MapgenParams *oparams = new MapgenParams;
705         u32 paramsmodified = 0;
706         u32 flagmask = 0;
707         
708         lua_getfield(L, 1, "mgname");
709         if (lua_isstring(L, -1)) {
710                 oparams->mg_name = std::string(lua_tostring(L, -1));
711                 paramsmodified |= MGPARAMS_SET_MGNAME;
712         }
713         
714         lua_getfield(L, 1, "seed");
715         if (lua_isnumber(L, -1)) {
716                 oparams->seed = lua_tointeger(L, -1);
717                 paramsmodified |= MGPARAMS_SET_SEED;
718         }
719         
720         lua_getfield(L, 1, "water_level");
721         if (lua_isnumber(L, -1)) {
722                 oparams->water_level = lua_tointeger(L, -1);
723                 paramsmodified |= MGPARAMS_SET_WATER_LEVEL;
724         }
725
726         lua_getfield(L, 1, "flags");
727         if (lua_isstring(L, -1)) {
728                 std::string flagstr = std::string(lua_tostring(L, -1));
729                 oparams->flags = readFlagString(flagstr, flagdesc_mapgen);
730                 paramsmodified |= MGPARAMS_SET_FLAGS;
731         
732                 lua_getfield(L, 1, "flagmask");
733                 if (lua_isstring(L, -1)) {
734                         flagstr = std::string(lua_tostring(L, -1));
735                         flagmask = readFlagString(flagstr, flagdesc_mapgen);
736                 }
737         }
738         
739         emerge->luaoverride_params          = oparams;
740         emerge->luaoverride_params_modified = paramsmodified;
741         emerge->luaoverride_flagmask        = flagmask;
742         
743         return 0;
744 }
745
746 // minetest.clear_objects()
747 // clear all objects in the environment
748 int ModApiEnvMod::l_clear_objects(lua_State *L)
749 {
750         GET_ENV_PTR;
751
752         env->clearAllObjects();
753         return 0;
754 }
755
756 // minetest.line_of_sight(pos1, pos2, stepsize) -> true/false
757 int ModApiEnvMod::l_line_of_sight(lua_State *L) {
758         float stepsize = 1.0;
759
760         GET_ENV_PTR;
761
762         // read position 1 from lua
763         v3f pos1 = checkFloatPos(L, 1);
764         // read position 2 from lua
765         v3f pos2 = checkFloatPos(L, 2);
766         //read step size from lua
767         if (lua_isnumber(L, 3))
768                 stepsize = lua_tonumber(L, 3);
769
770         return (env->line_of_sight(pos1,pos2,stepsize));
771 }
772
773 // minetest.find_path(pos1, pos2, searchdistance,
774 //     max_jump, max_drop, algorithm) -> table containing path
775 int ModApiEnvMod::l_find_path(lua_State *L)
776 {
777         GET_ENV_PTR;
778
779         v3s16 pos1                  = read_v3s16(L, 1);
780         v3s16 pos2                  = read_v3s16(L, 2);
781         unsigned int searchdistance = luaL_checkint(L, 3);
782         unsigned int max_jump       = luaL_checkint(L, 4);
783         unsigned int max_drop       = luaL_checkint(L, 5);
784         algorithm algo              = A_PLAIN_NP;
785         if (!lua_isnil(L, 6)) {
786                 std::string algorithm = luaL_checkstring(L,6);
787
788                 if (algorithm == "A*")
789                         algo = A_PLAIN;
790
791                 if (algorithm == "Dijkstra")
792                         algo = DIJKSTRA;
793         }
794
795         std::vector<v3s16> path =
796                         get_Path(env,pos1,pos2,searchdistance,max_jump,max_drop,algo);
797
798         if (path.size() > 0)
799         {
800                 lua_newtable(L);
801                 int top = lua_gettop(L);
802                 unsigned int index = 1;
803                 for (std::vector<v3s16>::iterator i = path.begin(); i != path.end();i++)
804                 {
805                         lua_pushnumber(L,index);
806                         push_v3s16(L, *i);
807                         lua_settable(L, top);
808                         index++;
809                 }
810                 return 1;
811         }
812
813         return 0;
814 }
815
816 // minetest.spawn_tree(pos, treedef)
817 int ModApiEnvMod::l_spawn_tree(lua_State *L)
818 {
819         GET_ENV_PTR;
820
821         v3s16 p0 = read_v3s16(L, 1);
822
823         treegen::TreeDef tree_def;
824         std::string trunk,leaves,fruit;
825         INodeDefManager *ndef = env->getGameDef()->ndef();
826
827         if(lua_istable(L, 2))
828         {
829                 getstringfield(L, 2, "axiom", tree_def.initial_axiom);
830                 getstringfield(L, 2, "rules_a", tree_def.rules_a);
831                 getstringfield(L, 2, "rules_b", tree_def.rules_b);
832                 getstringfield(L, 2, "rules_c", tree_def.rules_c);
833                 getstringfield(L, 2, "rules_d", tree_def.rules_d);
834                 getstringfield(L, 2, "trunk", trunk);
835                 tree_def.trunknode=ndef->getId(trunk);
836                 getstringfield(L, 2, "leaves", leaves);
837                 tree_def.leavesnode=ndef->getId(leaves);
838                 tree_def.leaves2_chance=0;
839                 getstringfield(L, 2, "leaves2", leaves);
840                 if (leaves !="")
841                 {
842                         tree_def.leaves2node=ndef->getId(leaves);
843                         getintfield(L, 2, "leaves2_chance", tree_def.leaves2_chance);
844                 }
845                 getintfield(L, 2, "angle", tree_def.angle);
846                 getintfield(L, 2, "iterations", tree_def.iterations);
847                 getintfield(L, 2, "random_level", tree_def.iterations_random_level);
848                 getstringfield(L, 2, "trunk_type", tree_def.trunk_type);
849                 getboolfield(L, 2, "thin_branches", tree_def.thin_branches);
850                 tree_def.fruit_chance=0;
851                 getstringfield(L, 2, "fruit", fruit);
852                 if (fruit != "")
853                 {
854                         tree_def.fruitnode=ndef->getId(fruit);
855                         getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
856                 }
857                 getintfield(L, 2, "seed", tree_def.seed);
858         }
859         else
860                 return 0;
861         treegen::spawn_ltree (env, p0, ndef, tree_def);
862         return 1;
863 }
864
865
866 // minetest.transforming_liquid_add(pos)
867 int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
868 {
869         GET_ENV_PTR;
870
871         v3s16 p0 = read_v3s16(L, 1);
872         env->getMap().transforming_liquid_add(p0);
873         return 1;
874 }
875
876 // minetest.get_heat(pos)
877 // pos = {x=num, y=num, z=num}
878 int ModApiEnvMod::l_get_heat(lua_State *L)
879 {
880         GET_ENV_PTR;
881
882         v3s16 pos = read_v3s16(L, 1);
883         lua_pushnumber(L, env->getServerMap().getHeat(env, pos));
884         return 1;
885 }
886
887 // minetest.get_humidity(pos)
888 // pos = {x=num, y=num, z=num}
889 int ModApiEnvMod::l_get_humidity(lua_State *L)
890 {
891         GET_ENV_PTR;
892
893         v3s16 pos = read_v3s16(L, 1);
894         lua_pushnumber(L, env->getServerMap().getHumidity(env, pos));
895         return 1;
896 }
897
898
899 bool ModApiEnvMod::Initialize(lua_State *L,int top)
900 {
901
902         bool retval = true;
903
904         retval &= API_FCT(set_node);
905         retval &= API_FCT(add_node);
906         retval &= API_FCT(add_item);
907         retval &= API_FCT(remove_node);
908         retval &= API_FCT(get_node);
909         retval &= API_FCT(get_node_or_nil);
910         retval &= API_FCT(get_node_light);
911         retval &= API_FCT(place_node);
912         retval &= API_FCT(dig_node);
913         retval &= API_FCT(punch_node);
914         retval &= API_FCT(get_node_max_level);
915         retval &= API_FCT(get_node_level);
916         retval &= API_FCT(add_node_level);
917         retval &= API_FCT(add_entity);
918         retval &= API_FCT(get_meta);
919         retval &= API_FCT(get_node_timer);
920         retval &= API_FCT(get_player_by_name);
921         retval &= API_FCT(get_objects_inside_radius);
922         retval &= API_FCT(set_timeofday);
923         retval &= API_FCT(get_timeofday);
924         retval &= API_FCT(find_node_near);
925         retval &= API_FCT(find_nodes_in_area);
926         retval &= API_FCT(get_perlin);
927         retval &= API_FCT(get_perlin_map);
928         retval &= API_FCT(get_voxel_manip);
929         retval &= API_FCT(get_mapgen_object);
930         retval &= API_FCT(set_mapgen_params);
931         retval &= API_FCT(clear_objects);
932         retval &= API_FCT(spawn_tree);
933         retval &= API_FCT(find_path);
934         retval &= API_FCT(line_of_sight);
935         retval &= API_FCT(transforming_liquid_add);
936         retval &= API_FCT(get_heat);
937         retval &= API_FCT(get_humidity);
938
939         return retval;
940 }
941
942 ModApiEnvMod modapienv_prototype;