3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
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"
32 #include "daynightratio.h"
33 #include "util/pointedthing.h"
34 #include "content_sao.h"
36 #include "pathfinder.h"
38 #define GET_ENV_PTR ServerEnvironment* env = \
39 dynamic_cast<ServerEnvironment*>(getEnv(L)); \
40 if (env == NULL) return 0
42 ///////////////////////////////////////////////////////////////////////////////
45 void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
46 u32 active_object_count, u32 active_object_count_wider)
48 GameScripting *scriptIface = env->getScriptIface();
49 scriptIface->realityCheck();
51 lua_State *L = scriptIface->getStack();
52 sanity_check(lua_checkstack(L, 20));
53 StackUnroller stack_unroller(L);
55 lua_pushcfunction(L, script_error_handler);
56 int errorhandler = lua_gettop(L);
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
64 // Get registered_abms[m_id]
65 lua_pushnumber(L, m_id);
69 lua_remove(L, -2); // Remove registered_abms
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]
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))
82 lua_pop(L, 1); // Pop error handler
87 // set_node(pos, node)
88 // pos = {x=num, y=num, z=num}
89 int ModApiEnvMod::l_set_node(lua_State *L)
93 INodeDefManager *ndef = env->getGameDef()->ndef();
95 v3s16 pos = read_v3s16(L, 1);
96 MapNode n = readnode(L, 2, ndef);
98 bool succeeded = env->setNode(pos, n);
99 lua_pushboolean(L, succeeded);
103 int ModApiEnvMod::l_add_node(lua_State *L)
105 return l_set_node(L);
109 // pos = {x=num, y=num, z=num}
110 int ModApiEnvMod::l_remove_node(lua_State *L)
115 v3s16 pos = read_v3s16(L, 1);
117 bool succeeded = env->removeNode(pos);
118 lua_pushboolean(L, succeeded);
122 // swap_node(pos, node)
123 // pos = {x=num, y=num, z=num}
124 int ModApiEnvMod::l_swap_node(lua_State *L)
128 INodeDefManager *ndef = env->getGameDef()->ndef();
130 v3s16 pos = read_v3s16(L, 1);
131 MapNode n = readnode(L, 2, ndef);
133 bool succeeded = env->swapNode(pos, n);
134 lua_pushboolean(L, succeeded);
139 // pos = {x=num, y=num, z=num}
140 int ModApiEnvMod::l_get_node(lua_State *L)
145 v3s16 pos = read_v3s16(L, 1);
147 MapNode n = env->getMap().getNodeNoEx(pos);
149 pushnode(L, n, env->getGameDef()->ndef());
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)
160 v3s16 pos = read_v3s16(L, 1);
163 MapNode n = env->getMap().getNodeNoEx(pos, &pos_ok);
166 pushnode(L, n, env->getGameDef()->ndef());
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)
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);
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));
199 // place_node(pos, node)
200 // pos = {x=num, y=num, z=num}
201 int ModApiEnvMod::l_place_node(lua_State *L)
205 ScriptApiItem *scriptIfaceItem = getScriptApi<ScriptApiItem>(L);
206 Server *server = getServer(L);
207 INodeDefManager *ndef = server->ndef();
208 IItemDefManager *idef = server->idef();
210 v3s16 pos = read_v3s16(L, 1);
211 MapNode n = readnode(L, 2, ndef);
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);
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
228 bool success = scriptIfaceItem->item_OnPlace(item, NULL, pointed);
229 lua_pushboolean(L, success);
234 // pos = {x=num, y=num, z=num}
235 int ModApiEnvMod::l_dig_node(lua_State *L)
239 ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
241 v3s16 pos = read_v3s16(L, 1);
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);
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);
257 // pos = {x=num, y=num, z=num}
258 int ModApiEnvMod::l_punch_node(lua_State *L)
262 ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
264 v3s16 pos = read_v3s16(L, 1);
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);
272 // Punch it with a NULL puncher (appears in Lua as a non-functional
274 bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing());
275 lua_pushboolean(L, success);
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)
285 v3s16 pos = read_v3s16(L, 1);
286 MapNode n = env->getMap().getNodeNoEx(pos);
287 lua_pushnumber(L, n.getMaxLevel(env->getGameDef()->ndef()));
291 // get_node_level(pos)
292 // pos = {x=num, y=num, z=num}
293 int ModApiEnvMod::l_get_node_level(lua_State *L)
297 v3s16 pos = read_v3s16(L, 1);
298 MapNode n = env->getMap().getNodeNoEx(pos);
299 lua_pushnumber(L, n.getLevel(env->getGameDef()->ndef()));
303 // set_node_level(pos, level)
304 // pos = {x=num, y=num, z=num}
306 int ModApiEnvMod::l_set_node_level(lua_State *L)
310 v3s16 pos = read_v3s16(L, 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);
320 // add_node_level(pos, level)
321 // pos = {x=num, y=num, z=num}
323 int ModApiEnvMod::l_add_node_level(lua_State *L)
327 v3s16 pos = read_v3s16(L, 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);
337 // find_nodes_with_meta(pos1, pos2)
338 int ModApiEnvMod::l_find_nodes_with_meta(lua_State *L)
342 std::vector<v3s16> positions = env->getMap().findNodesWithMetadata(
343 check_v3s16(L, 1), check_v3s16(L, 2));
346 for (size_t i = 0; i != positions.size(); i++) {
347 push_v3s16(L, positions[i]);
348 lua_rawseti(L, -2, i + 1);
355 int ModApiEnvMod::l_get_meta(lua_State *L)
360 v3s16 p = read_v3s16(L, 1);
361 NodeMetaRef::create(L, p, env);
365 // get_node_timer(pos)
366 int ModApiEnvMod::l_get_node_timer(lua_State *L)
371 v3s16 p = read_v3s16(L, 1);
372 NodeTimerRef::create(L, p, env);
376 // add_entity(pos, entityname) -> ObjectRef or nil
377 // pos = {x=num, y=num, z=num}
378 int ModApiEnvMod::l_add_entity(lua_State *L)
383 v3f pos = checkFloatPos(L, 1);
385 const char *name = luaL_checkstring(L, 2);
387 ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
388 int objectid = env->addActiveObject(obj);
389 // If failed to add, return nothing (reads as nil)
393 getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
397 // add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
398 // pos = {x=num, y=num, z=num}
399 int ModApiEnvMod::l_add_item(lua_State *L)
404 //v3f pos = checkFloatPos(L, 1);
406 ItemStack item = read_item(L, 2,getServer(L));
407 if(item.empty() || !item.isKnown(getServer(L)->idef()))
410 lua_pushcfunction(L, script_error_handler);
411 int errorhandler = lua_gettop(L);
413 // Use spawn_item to spawn a __builtin:item
414 lua_getglobal(L, "core");
415 lua_getfield(L, -1, "spawn_item");
416 lua_remove(L, -2); // Remove core
420 lua_pushstring(L, item.getItemString().c_str());
421 if(lua_pcall(L, 2, 1, errorhandler))
423 lua_remove(L, errorhandler); // Remove error handler
427 // get_player_by_name(name)
428 int ModApiEnvMod::l_get_player_by_name(lua_State *L)
433 const char *name = luaL_checkstring(L, 1);
434 Player *player = env->getPlayer(name);
439 PlayerSAO *sao = player->getPlayerSAO();
444 // Put player on stack
445 getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
449 // get_objects_inside_radius(pos, radius)
450 int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
455 v3f pos = checkFloatPos(L, 1);
456 float radius = luaL_checknumber(L, 2) * BS;
457 std::vector<u16> ids;
458 env->getObjectsInsideRadius(ids, pos, radius);
459 ScriptApiBase *script = getScriptApiBase(L);
460 lua_createtable(L, ids.size(), 0);
461 std::vector<u16>::const_iterator iter = ids.begin();
462 for(u32 i = 0; iter != ids.end(); iter++) {
463 ServerActiveObject *obj = env->getActiveObject(*iter);
464 // Insert object reference into table
465 script->objectrefGetOrCreate(L, obj);
466 lua_rawseti(L, -2, ++i);
471 // set_timeofday(val)
473 int ModApiEnvMod::l_set_timeofday(lua_State *L)
478 float timeofday_f = luaL_checknumber(L, 1);
479 sanity_check(timeofday_f >= 0.0 && timeofday_f <= 1.0);
480 int timeofday_mh = (int)(timeofday_f * 24000.0);
481 // This should be set directly in the environment but currently
482 // such changes aren't immediately sent to the clients, so call
483 // the server instead.
484 //env->setTimeOfDay(timeofday_mh);
485 getServer(L)->setTimeOfDay(timeofday_mh);
489 // get_timeofday() -> 0...1
490 int ModApiEnvMod::l_get_timeofday(lua_State *L)
495 int timeofday_mh = env->getTimeOfDay();
496 float timeofday_f = (float)timeofday_mh / 24000.0;
497 lua_pushnumber(L, timeofday_f);
502 int ModApiEnvMod::l_get_gametime(lua_State *L)
506 int game_time = env->getGameTime();
507 lua_pushnumber(L, game_time);
512 // find_node_near(pos, radius, nodenames) -> pos or nil
513 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
514 int ModApiEnvMod::l_find_node_near(lua_State *L)
518 INodeDefManager *ndef = getServer(L)->ndef();
519 v3s16 pos = read_v3s16(L, 1);
520 int radius = luaL_checkinteger(L, 2);
521 std::set<content_t> filter;
522 if(lua_istable(L, 3)){
525 while(lua_next(L, table) != 0){
526 // key at index -2 and value at index -1
527 luaL_checktype(L, -1, LUA_TSTRING);
528 ndef->getIds(lua_tostring(L, -1), filter);
529 // removes value, keeps key for next iteration
532 } else if(lua_isstring(L, 3)){
533 ndef->getIds(lua_tostring(L, 3), filter);
536 for(int d=1; d<=radius; d++){
537 std::vector<v3s16> list = FacePositionCache::getFacePositions(d);
538 for(std::vector<v3s16>::iterator i = list.begin();
539 i != list.end(); ++i){
540 v3s16 p = pos + (*i);
541 content_t c = env->getMap().getNodeNoEx(p).getContent();
542 if(filter.count(c) != 0){
551 // find_nodes_in_area(minp, maxp, nodenames) -> list of positions
552 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
553 int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
557 INodeDefManager *ndef = getServer(L)->ndef();
558 v3s16 minp = read_v3s16(L, 1);
559 v3s16 maxp = read_v3s16(L, 2);
560 std::set<content_t> filter;
561 if(lua_istable(L, 3)) {
564 while(lua_next(L, table) != 0) {
565 // key at index -2 and value at index -1
566 luaL_checktype(L, -1, LUA_TSTRING);
567 ndef->getIds(lua_tostring(L, -1), filter);
568 // removes value, keeps key for next iteration
571 } else if(lua_isstring(L, 3)) {
572 ndef->getIds(lua_tostring(L, 3), filter);
575 std::map<content_t, u16> individual_count;
579 for (s16 x = minp.X; x <= maxp.X; x++)
580 for (s16 y = minp.Y; y <= maxp.Y; y++)
581 for (s16 z = minp.Z; z <= maxp.Z; z++) {
583 content_t c = env->getMap().getNodeNoEx(p).getContent();
584 if (filter.count(c) != 0) {
586 lua_rawseti(L, -2, ++i);
587 individual_count[c]++;
591 for (std::set<content_t>::iterator it = filter.begin();
592 it != filter.end(); ++it) {
593 lua_pushnumber(L, individual_count[*it]);
594 lua_setfield(L, -2, ndef->get(*it).name.c_str());
599 // find_nodes_in_area_under_air(minp, maxp, nodenames) -> list of positions
600 // nodenames: e.g. {"ignore", "group:tree"} or "default:dirt"
601 int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
603 /* Note: A similar but generalized (and therefore slower) version of this
604 * function could be created -- e.g. find_nodes_in_area_under -- which
605 * would accept a node name (or ID?) or list of names that the "above node"
612 INodeDefManager *ndef = getServer(L)->ndef();
613 v3s16 minp = read_v3s16(L, 1);
614 v3s16 maxp = read_v3s16(L, 2);
615 std::set<content_t> filter;
617 if (lua_istable(L, 3)) {
620 while(lua_next(L, table) != 0) {
621 // key at index -2 and value at index -1
622 luaL_checktype(L, -1, LUA_TSTRING);
623 ndef->getIds(lua_tostring(L, -1), filter);
624 // removes value, keeps key for next iteration
627 } else if (lua_isstring(L, 3)) {
628 ndef->getIds(lua_tostring(L, 3), filter);
633 for (s16 x = minp.X; x <= maxp.X; x++)
634 for (s16 z = minp.Z; z <= maxp.Z; z++) {
637 content_t c = env->getMap().getNodeNoEx(p).getContent();
638 for (; y <= maxp.Y; y++) {
639 v3s16 psurf(x, y + 1, z);
640 content_t csurf = env->getMap().getNodeNoEx(psurf).getContent();
641 if(c != CONTENT_AIR && csurf == CONTENT_AIR &&
642 filter.count(c) != 0) {
643 push_v3s16(L, v3s16(x, y, z));
644 lua_rawseti(L, -2, ++i);
652 // get_perlin(seeddiff, octaves, persistence, scale)
653 // returns world-specific PerlinNoise
654 int ModApiEnvMod::l_get_perlin(lua_State *L)
660 if (lua_istable(L, 1)) {
661 read_noiseparams(L, 1, ¶ms);
663 params.seed = luaL_checkint(L, 1);
664 params.octaves = luaL_checkint(L, 2);
665 params.persist = luaL_checknumber(L, 3);
666 params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4);
669 params.seed += (int)env->getServerMap().getSeed();
671 LuaPerlinNoise *n = new LuaPerlinNoise(¶ms);
672 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
673 luaL_getmetatable(L, "PerlinNoise");
674 lua_setmetatable(L, -2);
678 // get_perlin_map(noiseparams, size)
679 // returns world-specific PerlinNoiseMap
680 int ModApiEnvMod::l_get_perlin_map(lua_State *L)
685 if (!read_noiseparams(L, 1, &np))
687 v3s16 size = read_v3s16(L, 2);
689 int seed = (int)(env->getServerMap().getSeed());
690 LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size);
691 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
692 luaL_getmetatable(L, "PerlinNoiseMap");
693 lua_setmetatable(L, -2);
698 // returns voxel manipulator
699 int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
703 Map *map = &(env->getMap());
704 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
705 new LuaVoxelManip(map, read_v3s16(L, 1), read_v3s16(L, 2)) :
706 new LuaVoxelManip(map);
708 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
709 luaL_getmetatable(L, "VoxelManip");
710 lua_setmetatable(L, -2);
715 // clear all objects in the environment
716 int ModApiEnvMod::l_clear_objects(lua_State *L)
720 env->clearAllObjects();
724 // line_of_sight(pos1, pos2, stepsize) -> true/false, pos
725 int ModApiEnvMod::l_line_of_sight(lua_State *L)
727 float stepsize = 1.0;
731 // read position 1 from lua
732 v3f pos1 = checkFloatPos(L, 1);
733 // read position 2 from lua
734 v3f pos2 = checkFloatPos(L, 2);
735 //read step size from lua
736 if (lua_isnumber(L, 3)) {
737 stepsize = lua_tonumber(L, 3);
741 bool success = env->line_of_sight(pos1, pos2, stepsize, &p);
742 lua_pushboolean(L, success);
750 // delete_area(p1, p2)
751 // delete mapblocks in area p1..p2
752 int ModApiEnvMod::l_delete_area(lua_State *L)
756 v3s16 bpmin = getNodeBlockPos(read_v3s16(L, 1));
757 v3s16 bpmax = getNodeBlockPos(read_v3s16(L, 2));
758 sortBoxVerticies(bpmin, bpmax);
760 ServerMap &map = env->getServerMap();
763 event.type = MEET_OTHER;
766 for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
767 for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
768 for (s16 x = bpmin.X; x <= bpmax.X; x++) {
770 if (map.deleteBlock(bp))
771 event.modified_blocks.insert(bp);
776 map.dispatchEvent(&event);
777 lua_pushboolean(L, success);
781 // find_path(pos1, pos2, searchdistance,
782 // max_jump, max_drop, algorithm) -> table containing path
783 int ModApiEnvMod::l_find_path(lua_State *L)
787 v3s16 pos1 = read_v3s16(L, 1);
788 v3s16 pos2 = read_v3s16(L, 2);
789 unsigned int searchdistance = luaL_checkint(L, 3);
790 unsigned int max_jump = luaL_checkint(L, 4);
791 unsigned int max_drop = luaL_checkint(L, 5);
792 algorithm algo = A_PLAIN_NP;
793 if (!lua_isnil(L, 6)) {
794 std::string algorithm = luaL_checkstring(L,6);
796 if (algorithm == "A*")
799 if (algorithm == "Dijkstra")
803 std::vector<v3s16> path =
804 get_Path(env,pos1,pos2,searchdistance,max_jump,max_drop,algo);
809 int top = lua_gettop(L);
810 unsigned int index = 1;
811 for (std::vector<v3s16>::iterator i = path.begin(); i != path.end();i++)
813 lua_pushnumber(L,index);
815 lua_settable(L, top);
824 // spawn_tree(pos, treedef)
825 int ModApiEnvMod::l_spawn_tree(lua_State *L)
829 v3s16 p0 = read_v3s16(L, 1);
831 treegen::TreeDef tree_def;
832 std::string trunk,leaves,fruit;
833 INodeDefManager *ndef = env->getGameDef()->ndef();
835 if(lua_istable(L, 2))
837 getstringfield(L, 2, "axiom", tree_def.initial_axiom);
838 getstringfield(L, 2, "rules_a", tree_def.rules_a);
839 getstringfield(L, 2, "rules_b", tree_def.rules_b);
840 getstringfield(L, 2, "rules_c", tree_def.rules_c);
841 getstringfield(L, 2, "rules_d", tree_def.rules_d);
842 getstringfield(L, 2, "trunk", trunk);
843 tree_def.trunknode=ndef->getId(trunk);
844 getstringfield(L, 2, "leaves", leaves);
845 tree_def.leavesnode=ndef->getId(leaves);
846 tree_def.leaves2_chance=0;
847 getstringfield(L, 2, "leaves2", leaves);
850 tree_def.leaves2node=ndef->getId(leaves);
851 getintfield(L, 2, "leaves2_chance", tree_def.leaves2_chance);
853 getintfield(L, 2, "angle", tree_def.angle);
854 getintfield(L, 2, "iterations", tree_def.iterations);
855 if (!getintfield(L, 2, "random_level", tree_def.iterations_random_level))
856 tree_def.iterations_random_level = 0;
857 getstringfield(L, 2, "trunk_type", tree_def.trunk_type);
858 getboolfield(L, 2, "thin_branches", tree_def.thin_branches);
859 tree_def.fruit_chance=0;
860 getstringfield(L, 2, "fruit", fruit);
863 tree_def.fruitnode=ndef->getId(fruit);
864 getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
866 tree_def.explicit_seed = getintfield(L, 2, "seed", tree_def.seed);
872 if ((e = treegen::spawn_ltree (env, p0, ndef, tree_def)) != treegen::SUCCESS) {
873 if (e == treegen::UNBALANCED_BRACKETS) {
874 luaL_error(L, "spawn_tree(): closing ']' has no matching opening bracket");
876 luaL_error(L, "spawn_tree(): unknown error");
883 // transforming_liquid_add(pos)
884 int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
888 v3s16 p0 = read_v3s16(L, 1);
889 env->getMap().transforming_liquid_add(p0);
893 // forceload_block(blockpos)
894 // blockpos = {x=num, y=num, z=num}
895 int ModApiEnvMod::l_forceload_block(lua_State *L)
899 v3s16 blockpos = read_v3s16(L, 1);
900 env->getForceloadedBlocks()->insert(blockpos);
904 // forceload_free_block(blockpos)
905 // blockpos = {x=num, y=num, z=num}
906 int ModApiEnvMod::l_forceload_free_block(lua_State *L)
910 v3s16 blockpos = read_v3s16(L, 1);
911 env->getForceloadedBlocks()->erase(blockpos);
916 int ModApiEnvMod::l_get_us_time(lua_State *L)
918 lua_pushnumber(L, porting::getTimeUs());
922 void ModApiEnvMod::Initialize(lua_State *L, int top)
928 API_FCT(remove_node);
930 API_FCT(get_node_or_nil);
931 API_FCT(get_node_light);
935 API_FCT(get_node_max_level);
936 API_FCT(get_node_level);
937 API_FCT(set_node_level);
938 API_FCT(add_node_level);
940 API_FCT(find_nodes_with_meta);
942 API_FCT(get_node_timer);
943 API_FCT(get_player_by_name);
944 API_FCT(get_objects_inside_radius);
945 API_FCT(set_timeofday);
946 API_FCT(get_timeofday);
947 API_FCT(get_gametime);
948 API_FCT(find_node_near);
949 API_FCT(find_nodes_in_area);
950 API_FCT(find_nodes_in_area_under_air);
951 API_FCT(delete_area);
953 API_FCT(get_perlin_map);
954 API_FCT(get_voxel_manip);
955 API_FCT(clear_objects);
958 API_FCT(line_of_sight);
959 API_FCT(transforming_liquid_add);
960 API_FCT(forceload_block);
961 API_FCT(forceload_free_block);
962 API_FCT(get_us_time);