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