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