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