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