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 "scriptapi.h"
21 #include "scriptapi_env.h"
25 #include "daynightratio.h"
26 #include "content_sao.h"
29 #include "util/pointedthing.h"
30 #include "scriptapi_types.h"
31 #include "scriptapi_noise.h"
32 #include "scriptapi_nodemeta.h"
33 #include "scriptapi_nodetimer.h"
34 #include "scriptapi_object.h"
35 #include "scriptapi_common.h"
36 #include "scriptapi_item.h"
37 #include "scriptapi_node.h"
41 extern void scriptapi_run_callbacks(lua_State *L, int nargs,
42 RunCallbacksMode mode);
45 class LuaABM : public ActiveBlockModifier
51 std::set<std::string> m_trigger_contents;
52 std::set<std::string> m_required_neighbors;
53 float m_trigger_interval;
56 LuaABM(lua_State *L, int id,
57 const std::set<std::string> &trigger_contents,
58 const std::set<std::string> &required_neighbors,
59 float trigger_interval, u32 trigger_chance):
62 m_trigger_contents(trigger_contents),
63 m_required_neighbors(required_neighbors),
64 m_trigger_interval(trigger_interval),
65 m_trigger_chance(trigger_chance)
68 virtual std::set<std::string> getTriggerContents()
70 return m_trigger_contents;
72 virtual std::set<std::string> getRequiredNeighbors()
74 return m_required_neighbors;
76 virtual float getTriggerInterval()
78 return m_trigger_interval;
80 virtual u32 getTriggerChance()
82 return m_trigger_chance;
84 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
85 u32 active_object_count, u32 active_object_count_wider)
90 assert(lua_checkstack(L, 20));
91 StackUnroller stack_unroller(L);
93 // Get minetest.registered_abms
94 lua_getglobal(L, "minetest");
95 lua_getfield(L, -1, "registered_abms");
96 luaL_checktype(L, -1, LUA_TTABLE);
97 int registered_abms = lua_gettop(L);
99 // Get minetest.registered_abms[m_id]
100 lua_pushnumber(L, m_id);
101 lua_gettable(L, registered_abms);
106 luaL_checktype(L, -1, LUA_TTABLE);
107 lua_getfield(L, -1, "action");
108 luaL_checktype(L, -1, LUA_TFUNCTION);
110 pushnode(L, n, env->getGameDef()->ndef());
111 lua_pushnumber(L, active_object_count);
112 lua_pushnumber(L, active_object_count_wider);
113 if(lua_pcall(L, 4, 0, 0))
114 script_error(L, "error: %s", lua_tostring(L, -1));
122 int EnvRef::gc_object(lua_State *L) {
123 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
128 EnvRef* EnvRef::checkobject(lua_State *L, int narg)
130 luaL_checktype(L, narg, LUA_TUSERDATA);
131 void *ud = luaL_checkudata(L, narg, className);
132 if(!ud) luaL_typerror(L, narg, className);
133 return *(EnvRef**)ud; // unbox pointer
136 // Exported functions
138 // EnvRef:set_node(pos, node)
139 // pos = {x=num, y=num, z=num}
140 int EnvRef::l_set_node(lua_State *L)
142 EnvRef *o = checkobject(L, 1);
143 ServerEnvironment *env = o->m_env;
144 if(env == NULL) return 0;
145 INodeDefManager *ndef = env->getGameDef()->ndef();
147 v3s16 pos = read_v3s16(L, 2);
148 MapNode n = readnode(L, 3, ndef);
150 bool succeeded = env->setNode(pos, n);
151 lua_pushboolean(L, succeeded);
155 int EnvRef::l_add_node(lua_State *L)
157 return l_set_node(L);
160 // EnvRef:remove_node(pos)
161 // pos = {x=num, y=num, z=num}
162 int EnvRef::l_remove_node(lua_State *L)
164 EnvRef *o = checkobject(L, 1);
165 ServerEnvironment *env = o->m_env;
166 if(env == NULL) return 0;
169 v3s16 pos = read_v3s16(L, 2);
171 bool succeeded = env->removeNode(pos);
172 lua_pushboolean(L, succeeded);
176 // EnvRef:get_node(pos)
177 // pos = {x=num, y=num, z=num}
178 int EnvRef::l_get_node(lua_State *L)
180 EnvRef *o = checkobject(L, 1);
181 ServerEnvironment *env = o->m_env;
182 if(env == NULL) return 0;
184 v3s16 pos = read_v3s16(L, 2);
186 MapNode n = env->getMap().getNodeNoEx(pos);
188 pushnode(L, n, env->getGameDef()->ndef());
192 // EnvRef:get_node_or_nil(pos)
193 // pos = {x=num, y=num, z=num}
194 int EnvRef::l_get_node_or_nil(lua_State *L)
196 EnvRef *o = checkobject(L, 1);
197 ServerEnvironment *env = o->m_env;
198 if(env == NULL) return 0;
200 v3s16 pos = read_v3s16(L, 2);
203 MapNode n = env->getMap().getNode(pos);
205 pushnode(L, n, env->getGameDef()->ndef());
207 } catch(InvalidPositionException &e)
214 // EnvRef:get_node_light(pos, timeofday)
215 // pos = {x=num, y=num, z=num}
216 // timeofday: nil = current time, 0 = night, 0.5 = day
217 int EnvRef::l_get_node_light(lua_State *L)
219 EnvRef *o = checkobject(L, 1);
220 ServerEnvironment *env = o->m_env;
221 if(env == NULL) return 0;
223 v3s16 pos = read_v3s16(L, 2);
224 u32 time_of_day = env->getTimeOfDay();
225 if(lua_isnumber(L, 3))
226 time_of_day = 24000.0 * lua_tonumber(L, 3);
227 time_of_day %= 24000;
228 u32 dnr = time_to_daynight_ratio(time_of_day, true);
229 MapNode n = env->getMap().getNodeNoEx(pos);
231 MapNode n = env->getMap().getNode(pos);
232 INodeDefManager *ndef = env->getGameDef()->ndef();
233 lua_pushinteger(L, n.getLightBlend(dnr, ndef));
235 } catch(InvalidPositionException &e)
242 // EnvRef:place_node(pos, node)
243 // pos = {x=num, y=num, z=num}
244 int EnvRef::l_place_node(lua_State *L)
246 EnvRef *o = checkobject(L, 1);
247 ServerEnvironment *env = o->m_env;
248 if(env == NULL) return 0;
249 v3s16 pos = read_v3s16(L, 2);
250 MapNode n = readnode(L, 3, env->getGameDef()->ndef());
252 // Don't attempt to load non-loaded area as of now
253 MapNode n_old = env->getMap().getNodeNoEx(pos);
254 if(n_old.getContent() == CONTENT_IGNORE){
255 lua_pushboolean(L, false);
258 // Create item to place
259 INodeDefManager *ndef = get_server(L)->ndef();
260 IItemDefManager *idef = get_server(L)->idef();
261 ItemStack item(ndef->get(n).name, 1, 0, "", idef);
262 // Make pointed position
263 PointedThing pointed;
264 pointed.type = POINTEDTHING_NODE;
265 pointed.node_abovesurface = pos;
266 pointed.node_undersurface = pos + v3s16(0,-1,0);
267 // Place it with a NULL placer (appears in Lua as a non-functional
269 bool success = scriptapi_item_on_place(L, item, NULL, pointed);
270 lua_pushboolean(L, success);
274 // EnvRef:dig_node(pos)
275 // pos = {x=num, y=num, z=num}
276 int EnvRef::l_dig_node(lua_State *L)
278 EnvRef *o = checkobject(L, 1);
279 ServerEnvironment *env = o->m_env;
280 if(env == NULL) return 0;
281 v3s16 pos = read_v3s16(L, 2);
283 // Don't attempt to load non-loaded area as of now
284 MapNode n = env->getMap().getNodeNoEx(pos);
285 if(n.getContent() == CONTENT_IGNORE){
286 lua_pushboolean(L, false);
289 // Dig it out with a NULL digger (appears in Lua as a
290 // non-functional ObjectRef)
291 bool success = scriptapi_node_on_dig(L, pos, n, NULL);
292 lua_pushboolean(L, success);
296 // EnvRef:punch_node(pos)
297 // pos = {x=num, y=num, z=num}
298 int EnvRef::l_punch_node(lua_State *L)
300 EnvRef *o = checkobject(L, 1);
301 ServerEnvironment *env = o->m_env;
302 if(env == NULL) return 0;
303 v3s16 pos = read_v3s16(L, 2);
305 // Don't attempt to load non-loaded area as of now
306 MapNode n = env->getMap().getNodeNoEx(pos);
307 if(n.getContent() == CONTENT_IGNORE){
308 lua_pushboolean(L, false);
311 // Punch it with a NULL puncher (appears in Lua as a non-functional
313 bool success = scriptapi_node_on_punch(L, pos, n, NULL);
314 lua_pushboolean(L, success);
318 // EnvRef:get_meta(pos)
319 int EnvRef::l_get_meta(lua_State *L)
321 //infostream<<"EnvRef::l_get_meta()"<<std::endl;
322 EnvRef *o = checkobject(L, 1);
323 ServerEnvironment *env = o->m_env;
324 if(env == NULL) return 0;
326 v3s16 p = read_v3s16(L, 2);
327 NodeMetaRef::create(L, p, env);
331 // EnvRef:get_node_timer(pos)
332 int EnvRef::l_get_node_timer(lua_State *L)
334 EnvRef *o = checkobject(L, 1);
335 ServerEnvironment *env = o->m_env;
336 if(env == NULL) return 0;
338 v3s16 p = read_v3s16(L, 2);
339 NodeTimerRef::create(L, p, env);
343 // EnvRef:add_entity(pos, entityname) -> ObjectRef or nil
344 // pos = {x=num, y=num, z=num}
345 int EnvRef::l_add_entity(lua_State *L)
347 //infostream<<"EnvRef::l_add_entity()"<<std::endl;
348 EnvRef *o = checkobject(L, 1);
349 ServerEnvironment *env = o->m_env;
350 if(env == NULL) return 0;
352 v3f pos = checkFloatPos(L, 2);
354 const char *name = luaL_checkstring(L, 3);
356 ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
357 int objectid = env->addActiveObject(obj);
358 // If failed to add, return nothing (reads as nil)
362 objectref_get_or_create(L, obj);
366 // EnvRef:add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
367 // pos = {x=num, y=num, z=num}
368 int EnvRef::l_add_item(lua_State *L)
370 //infostream<<"EnvRef::l_add_item()"<<std::endl;
371 EnvRef *o = checkobject(L, 1);
372 ServerEnvironment *env = o->m_env;
373 if(env == NULL) return 0;
375 v3f pos = checkFloatPos(L, 2);
377 ItemStack item = read_item(L, 3);
378 if(item.empty() || !item.isKnown(get_server(L)->idef()))
380 // Use minetest.spawn_item to spawn a __builtin:item
381 lua_getglobal(L, "minetest");
382 lua_getfield(L, -1, "spawn_item");
386 lua_pushstring(L, item.getItemString().c_str());
387 if(lua_pcall(L, 2, 1, 0))
388 script_error(L, "error: %s", lua_tostring(L, -1));
390 /*lua_pushvalue(L, 1);
391 lua_pushstring(L, "__builtin:item");
392 lua_pushstring(L, item.getItemString().c_str());
393 return l_add_entity(L);*/
395 ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
396 int objectid = env->addActiveObject(obj);
397 // If failed to add, return nothing (reads as nil)
401 objectref_get_or_create(L, obj);
405 // EnvRef:add_rat(pos)
406 // pos = {x=num, y=num, z=num}
407 int EnvRef::l_add_rat(lua_State *L)
409 infostream<<"EnvRef::l_add_rat(): C++ mobs have been removed."
410 <<" Doing nothing."<<std::endl;
414 // EnvRef:add_firefly(pos)
415 // pos = {x=num, y=num, z=num}
416 int EnvRef::l_add_firefly(lua_State *L)
418 infostream<<"EnvRef::l_add_firefly(): C++ mobs have been removed."
419 <<" Doing nothing."<<std::endl;
423 // EnvRef:get_player_by_name(name)
424 int EnvRef::l_get_player_by_name(lua_State *L)
426 EnvRef *o = checkobject(L, 1);
427 ServerEnvironment *env = o->m_env;
428 if(env == NULL) return 0;
430 const char *name = luaL_checkstring(L, 2);
431 Player *player = env->getPlayer(name);
436 PlayerSAO *sao = player->getPlayerSAO();
441 // Put player on stack
442 objectref_get_or_create(L, sao);
446 // EnvRef:get_objects_inside_radius(pos, radius)
447 int EnvRef::l_get_objects_inside_radius(lua_State *L)
449 // Get the table insert function
450 lua_getglobal(L, "table");
451 lua_getfield(L, -1, "insert");
452 int table_insert = lua_gettop(L);
454 EnvRef *o = checkobject(L, 1);
455 ServerEnvironment *env = o->m_env;
456 if(env == NULL) return 0;
458 v3f pos = checkFloatPos(L, 2);
459 float radius = luaL_checknumber(L, 3) * BS;
460 std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
462 int table = lua_gettop(L);
463 for(std::set<u16>::const_iterator
464 i = ids.begin(); i != ids.end(); i++){
465 ServerActiveObject *obj = env->getActiveObject(*i);
466 // Insert object reference into table
467 lua_pushvalue(L, table_insert);
468 lua_pushvalue(L, table);
469 objectref_get_or_create(L, obj);
470 if(lua_pcall(L, 2, 0, 0))
471 script_error(L, "error: %s", lua_tostring(L, -1));
476 // EnvRef:set_timeofday(val)
478 int EnvRef::l_set_timeofday(lua_State *L)
480 EnvRef *o = checkobject(L, 1);
481 ServerEnvironment *env = o->m_env;
482 if(env == NULL) return 0;
484 float timeofday_f = luaL_checknumber(L, 2);
485 assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
486 int timeofday_mh = (int)(timeofday_f * 24000.0);
487 // This should be set directly in the environment but currently
488 // such changes aren't immediately sent to the clients, so call
489 // the server instead.
490 //env->setTimeOfDay(timeofday_mh);
491 get_server(L)->setTimeOfDay(timeofday_mh);
495 // EnvRef:get_timeofday() -> 0...1
496 int EnvRef::l_get_timeofday(lua_State *L)
498 EnvRef *o = checkobject(L, 1);
499 ServerEnvironment *env = o->m_env;
500 if(env == NULL) return 0;
502 int timeofday_mh = env->getTimeOfDay();
503 float timeofday_f = (float)timeofday_mh / 24000.0;
504 lua_pushnumber(L, timeofday_f);
509 // EnvRef:find_node_near(pos, radius, nodenames) -> pos or nil
510 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
511 int EnvRef::l_find_node_near(lua_State *L)
513 EnvRef *o = checkobject(L, 1);
514 ServerEnvironment *env = o->m_env;
515 if(env == NULL) return 0;
516 INodeDefManager *ndef = get_server(L)->ndef();
517 v3s16 pos = read_v3s16(L, 2);
518 int radius = luaL_checkinteger(L, 3);
519 std::set<content_t> filter;
520 if(lua_istable(L, 4)){
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
530 } else if(lua_isstring(L, 4)){
531 ndef->getIds(lua_tostring(L, 4), filter);
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){
550 // EnvRef:find_nodes_in_area(minp, maxp, nodenames) -> list of positions
551 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
552 int EnvRef::l_find_nodes_in_area(lua_State *L)
554 EnvRef *o = checkobject(L, 1);
555 ServerEnvironment *env = o->m_env;
556 if(env == NULL) return 0;
557 INodeDefManager *ndef = get_server(L)->ndef();
558 v3s16 minp = read_v3s16(L, 2);
559 v3s16 maxp = read_v3s16(L, 3);
560 std::set<content_t> filter;
561 if(lua_istable(L, 4)){
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, 4)){
572 ndef->getIds(lua_tostring(L, 4), filter);
575 // Get the table insert function
576 lua_getglobal(L, "table");
577 lua_getfield(L, -1, "insert");
578 int table_insert = lua_gettop(L);
581 int table = lua_gettop(L);
582 for(s16 x=minp.X; x<=maxp.X; x++)
583 for(s16 y=minp.Y; y<=maxp.Y; y++)
584 for(s16 z=minp.Z; z<=maxp.Z; z++)
587 content_t c = env->getMap().getNodeNoEx(p).getContent();
588 if(filter.count(c) != 0){
589 lua_pushvalue(L, table_insert);
590 lua_pushvalue(L, table);
592 if(lua_pcall(L, 2, 0, 0))
593 script_error(L, "error: %s", lua_tostring(L, -1));
599 // EnvRef:get_perlin(seeddiff, octaves, persistence, scale)
600 // returns world-specific PerlinNoise
601 int EnvRef::l_get_perlin(lua_State *L)
603 EnvRef *o = checkobject(L, 1);
604 ServerEnvironment *env = o->m_env;
605 if(env == NULL) return 0;
607 int seeddiff = luaL_checkint(L, 2);
608 int octaves = luaL_checkint(L, 3);
609 float persistence = luaL_checknumber(L, 4);
610 float scale = luaL_checknumber(L, 5);
612 LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
613 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
614 luaL_getmetatable(L, "PerlinNoise");
615 lua_setmetatable(L, -2);
619 // EnvRef:get_perlin_map(noiseparams, size)
620 // returns world-specific PerlinNoiseMap
621 int EnvRef::l_get_perlin_map(lua_State *L)
623 EnvRef *o = checkobject(L, 1);
624 ServerEnvironment *env = o->m_env;
628 NoiseParams *np = read_noiseparams(L, 2);
631 v3s16 size = read_v3s16(L, 3);
633 int seed = (int)(env->getServerMap().getSeed());
634 LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(np, seed, size);
635 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
636 luaL_getmetatable(L, "PerlinNoiseMap");
637 lua_setmetatable(L, -2);
641 // EnvRef:clear_objects()
642 // clear all objects in the environment
643 int EnvRef::l_clear_objects(lua_State *L)
645 EnvRef *o = checkobject(L, 1);
646 o->m_env->clearAllObjects();
650 int EnvRef::l_spawn_tree(lua_State *L)
652 EnvRef *o = checkobject(L, 1);
653 ServerEnvironment *env = o->m_env;
654 if(env == NULL) return 0;
655 v3s16 p0 = read_v3s16(L, 2);
657 treegen::TreeDef tree_def;
658 std::string trunk,leaves,fruit;
659 INodeDefManager *ndef = env->getGameDef()->ndef();
661 if(lua_istable(L, 3))
663 getstringfield(L, 3, "axiom", tree_def.initial_axiom);
664 getstringfield(L, 3, "rules_a", tree_def.rules_a);
665 getstringfield(L, 3, "rules_b", tree_def.rules_b);
666 getstringfield(L, 3, "rules_c", tree_def.rules_c);
667 getstringfield(L, 3, "rules_d", tree_def.rules_d);
668 getstringfield(L, 3, "trunk", trunk);
669 tree_def.trunknode=ndef->getId(trunk);
670 getstringfield(L, 3, "leaves", leaves);
671 tree_def.leavesnode=ndef->getId(leaves);
672 tree_def.leaves2_chance=0;
673 getstringfield(L, 3, "leaves2", leaves);
676 tree_def.leaves2node=ndef->getId(leaves);
677 getintfield(L, 3, "leaves2_chance", tree_def.leaves2_chance);
679 getintfield(L, 3, "angle", tree_def.angle);
680 getintfield(L, 3, "iterations", tree_def.iterations);
681 getintfield(L, 3, "random_level", tree_def.iterations_random_level);
682 getstringfield(L, 3, "trunk_type", tree_def.trunk_type);
683 getboolfield(L, 3, "thin_branches", tree_def.thin_branches);
684 tree_def.fruit_chance=0;
685 getstringfield(L, 3, "fruit", fruit);
688 tree_def.fruitnode=ndef->getId(fruit);
689 getintfield(L, 3, "fruit_chance",tree_def.fruit_chance);
691 getintfield(L, 3, "seed", tree_def.seed);
695 treegen::spawn_ltree (env, p0, ndef, tree_def);
700 EnvRef::EnvRef(ServerEnvironment *env):
703 //infostream<<"EnvRef created"<<std::endl;
708 //infostream<<"EnvRef destructing"<<std::endl;
711 // Creates an EnvRef and leaves it on top of stack
712 // Not callable from Lua; all references are created on the C side.
713 void EnvRef::create(lua_State *L, ServerEnvironment *env)
715 EnvRef *o = new EnvRef(env);
716 //infostream<<"EnvRef::create: o="<<o<<std::endl;
717 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
718 luaL_getmetatable(L, className);
719 lua_setmetatable(L, -2);
722 void EnvRef::set_null(lua_State *L)
724 EnvRef *o = checkobject(L, -1);
728 void EnvRef::Register(lua_State *L)
731 int methodtable = lua_gettop(L);
732 luaL_newmetatable(L, className);
733 int metatable = lua_gettop(L);
735 lua_pushliteral(L, "__metatable");
736 lua_pushvalue(L, methodtable);
737 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
739 lua_pushliteral(L, "__index");
740 lua_pushvalue(L, methodtable);
741 lua_settable(L, metatable);
743 lua_pushliteral(L, "__gc");
744 lua_pushcfunction(L, gc_object);
745 lua_settable(L, metatable);
747 lua_pop(L, 1); // drop metatable
749 luaL_openlib(L, 0, methods, 0); // fill methodtable
750 lua_pop(L, 1); // drop methodtable
752 // Cannot be created from Lua
753 //lua_register(L, className, create_object);
756 const char EnvRef::className[] = "EnvRef";
757 const luaL_reg EnvRef::methods[] = {
758 luamethod(EnvRef, set_node),
759 luamethod(EnvRef, add_node),
760 luamethod(EnvRef, remove_node),
761 luamethod(EnvRef, get_node),
762 luamethod(EnvRef, get_node_or_nil),
763 luamethod(EnvRef, get_node_light),
764 luamethod(EnvRef, place_node),
765 luamethod(EnvRef, dig_node),
766 luamethod(EnvRef, punch_node),
767 luamethod(EnvRef, add_entity),
768 luamethod(EnvRef, add_item),
769 luamethod(EnvRef, add_rat),
770 luamethod(EnvRef, add_firefly),
771 luamethod(EnvRef, get_meta),
772 luamethod(EnvRef, get_node_timer),
773 luamethod(EnvRef, get_player_by_name),
774 luamethod(EnvRef, get_objects_inside_radius),
775 luamethod(EnvRef, set_timeofday),
776 luamethod(EnvRef, get_timeofday),
777 luamethod(EnvRef, find_node_near),
778 luamethod(EnvRef, find_nodes_in_area),
779 luamethod(EnvRef, get_perlin),
780 luamethod(EnvRef, get_perlin_map),
781 luamethod(EnvRef, clear_objects),
782 luamethod(EnvRef, spawn_tree),
786 void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp,
790 assert(lua_checkstack(L, 20));
791 //infostream<<"scriptapi_environment_on_generated"<<std::endl;
792 StackUnroller stack_unroller(L);
794 // Get minetest.registered_on_generateds
795 lua_getglobal(L, "minetest");
796 lua_getfield(L, -1, "registered_on_generateds");
800 lua_pushnumber(L, blockseed);
801 scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_FIRST);
804 void scriptapi_environment_step(lua_State *L, float dtime)
807 assert(lua_checkstack(L, 20));
808 //infostream<<"scriptapi_environment_step"<<std::endl;
809 StackUnroller stack_unroller(L);
811 // Get minetest.registered_globalsteps
812 lua_getglobal(L, "minetest");
813 lua_getfield(L, -1, "registered_globalsteps");
815 lua_pushnumber(L, dtime);
816 scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
819 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
822 assert(lua_checkstack(L, 20));
823 verbosestream<<"scriptapi_add_environment"<<std::endl;
824 StackUnroller stack_unroller(L);
826 // Create EnvRef on stack
827 EnvRef::create(L, env);
828 int envref = lua_gettop(L);
830 // minetest.env = envref
831 lua_getglobal(L, "minetest");
832 luaL_checktype(L, -1, LUA_TTABLE);
833 lua_pushvalue(L, envref);
834 lua_setfield(L, -2, "env");
836 // Store environment as light userdata in registry
837 lua_pushlightuserdata(L, env);
838 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_env");
841 Add ActiveBlockModifiers to environment
844 // Get minetest.registered_abms
845 lua_getglobal(L, "minetest");
846 lua_getfield(L, -1, "registered_abms");
847 luaL_checktype(L, -1, LUA_TTABLE);
848 int registered_abms = lua_gettop(L);
850 if(lua_istable(L, registered_abms)){
851 int table = lua_gettop(L);
853 while(lua_next(L, table) != 0){
854 // key at index -2 and value at index -1
855 int id = lua_tonumber(L, -2);
856 int current_abm = lua_gettop(L);
858 std::set<std::string> trigger_contents;
859 lua_getfield(L, current_abm, "nodenames");
860 if(lua_istable(L, -1)){
861 int table = lua_gettop(L);
863 while(lua_next(L, table) != 0){
864 // key at index -2 and value at index -1
865 luaL_checktype(L, -1, LUA_TSTRING);
866 trigger_contents.insert(lua_tostring(L, -1));
867 // removes value, keeps key for next iteration
870 } else if(lua_isstring(L, -1)){
871 trigger_contents.insert(lua_tostring(L, -1));
875 std::set<std::string> required_neighbors;
876 lua_getfield(L, current_abm, "neighbors");
877 if(lua_istable(L, -1)){
878 int table = lua_gettop(L);
880 while(lua_next(L, table) != 0){
881 // key at index -2 and value at index -1
882 luaL_checktype(L, -1, LUA_TSTRING);
883 required_neighbors.insert(lua_tostring(L, -1));
884 // removes value, keeps key for next iteration
887 } else if(lua_isstring(L, -1)){
888 required_neighbors.insert(lua_tostring(L, -1));
892 float trigger_interval = 10.0;
893 getfloatfield(L, current_abm, "interval", trigger_interval);
895 int trigger_chance = 50;
896 getintfield(L, current_abm, "chance", trigger_chance);
898 LuaABM *abm = new LuaABM(L, id, trigger_contents,
899 required_neighbors, trigger_interval, trigger_chance);
901 env->addActiveBlockModifier(abm);
903 // removes value, keeps key for next iteration