3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
34 #include "serverobject.h"
37 #include "luaentity_common.h"
38 #include "content_sao.h" // For LuaEntitySAO
42 #include "craftitemdef.h"
43 #include "main.h" // For g_settings
44 #include "settings.h" // For accessing g_settings
45 #include "nodemetadata.h"
46 #include "mapblock.h" // For getNodeBlockPos
47 #include "content_nodemeta.h"
50 static void stackDump(lua_State *L, std::ostream &o)
53 int top = lua_gettop(L);
54 for (i = 1; i <= top; i++) { /* repeat for each level */
55 int t = lua_type(L, i);
58 case LUA_TSTRING: /* strings */
59 o<<"\""<<lua_tostring(L, i)<<"\"";
62 case LUA_TBOOLEAN: /* booleans */
63 o<<(lua_toboolean(L, i) ? "true" : "false");
66 case LUA_TNUMBER: /* numbers */ {
68 snprintf(buf, 10, "%g", lua_tonumber(L, i));
72 default: /* other values */
73 o<<lua_typename(L, t);
82 static void realitycheck(lua_State *L)
84 int top = lua_gettop(L);
86 dstream<<"Stack is over 30:"<<std::endl;
87 stackDump(L, dstream);
88 script_error(L, "Stack is over 30 (reality check)");
98 StackUnroller(lua_State *L):
102 m_original_top = lua_gettop(m_lua); // store stack height
106 lua_settop(m_lua, m_original_top); // restore stack height
115 ModNameStorer(lua_State *L_, const std::string modname):
118 // Store current modname in registry
119 lua_pushstring(L, modname.c_str());
120 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
124 // Clear current modname in registry
126 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
130 std::string get_current_modname(lua_State *L)
132 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
133 std::string modname = "";
134 if(lua_type(L, -1) == LUA_TSTRING)
135 modname = lua_tostring(L, -1);
140 void check_modname_prefix(lua_State *L, std::string &name)
143 throw LuaError(L, std::string("Name is empty"));
146 name = name.substr(1);
150 std::string modname = get_current_modname(L);
151 assert(modname != "");
153 // For __builtin, anything goes
154 if(modname == "__builtin")
157 if(name.substr(0, modname.size()+1) != modname + "_")
158 throw LuaError(L, std::string("Name \"")+name
159 +"\" does not follow naming conventions: "
160 +"\"modname_\" or \":\" prefix required)");
163 static v3f readFloatPos(lua_State *L, int index)
166 luaL_checktype(L, index, LUA_TTABLE);
167 lua_getfield(L, index, "x");
168 pos.X = lua_tonumber(L, -1);
170 lua_getfield(L, index, "y");
171 pos.Y = lua_tonumber(L, -1);
173 lua_getfield(L, index, "z");
174 pos.Z = lua_tonumber(L, -1);
176 pos *= BS; // Scale to internal format
180 static void pushFloatPos(lua_State *L, v3f p)
184 lua_pushnumber(L, p.X);
185 lua_setfield(L, -2, "x");
186 lua_pushnumber(L, p.Y);
187 lua_setfield(L, -2, "y");
188 lua_pushnumber(L, p.Z);
189 lua_setfield(L, -2, "z");
192 static void pushpos(lua_State *L, v3s16 p)
195 lua_pushnumber(L, p.X);
196 lua_setfield(L, -2, "x");
197 lua_pushnumber(L, p.Y);
198 lua_setfield(L, -2, "y");
199 lua_pushnumber(L, p.Z);
200 lua_setfield(L, -2, "z");
203 static v3s16 readpos(lua_State *L, int index)
205 // Correct rounding at <0
206 v3f pf = readFloatPos(L, index);
207 return floatToInt(pf, BS);
210 static void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef)
213 lua_pushstring(L, ndef->get(n).name.c_str());
214 lua_setfield(L, -2, "name");
215 lua_pushnumber(L, n.getParam1());
216 lua_setfield(L, -2, "param1");
217 lua_pushnumber(L, n.getParam2());
218 lua_setfield(L, -2, "param2");
221 static MapNode readnode(lua_State *L, int index, INodeDefManager *ndef)
223 lua_getfield(L, index, "name");
224 const char *name = lua_tostring(L, -1);
227 lua_getfield(L, index, "param1");
231 param1 = lua_tonumber(L, -1);
234 lua_getfield(L, index, "param2");
238 param2 = lua_tonumber(L, -1);
240 return MapNode(ndef, name, param1, param2);
243 static video::SColor readARGB8(lua_State *L, int index)
246 luaL_checktype(L, index, LUA_TTABLE);
247 lua_getfield(L, index, "a");
248 if(lua_isnumber(L, -1))
249 color.setAlpha(lua_tonumber(L, -1));
251 lua_getfield(L, index, "r");
252 color.setRed(lua_tonumber(L, -1));
254 lua_getfield(L, index, "g");
255 color.setGreen(lua_tonumber(L, -1));
257 lua_getfield(L, index, "b");
258 color.setBlue(lua_tonumber(L, -1));
263 static core::aabbox3d<f32> read_aabbox3df32(lua_State *L, int index, f32 scale)
265 core::aabbox3d<f32> box;
266 if(lua_istable(L, -1)){
267 lua_rawgeti(L, -1, 1);
268 box.MinEdge.X = lua_tonumber(L, -1) * scale;
270 lua_rawgeti(L, -1, 2);
271 box.MinEdge.Y = lua_tonumber(L, -1) * scale;
273 lua_rawgeti(L, -1, 3);
274 box.MinEdge.Z = lua_tonumber(L, -1) * scale;
276 lua_rawgeti(L, -1, 4);
277 box.MaxEdge.X = lua_tonumber(L, -1) * scale;
279 lua_rawgeti(L, -1, 5);
280 box.MaxEdge.Y = lua_tonumber(L, -1) * scale;
282 lua_rawgeti(L, -1, 6);
283 box.MaxEdge.Z = lua_tonumber(L, -1) * scale;
289 static v2s16 read_v2s16(lua_State *L, int index)
292 luaL_checktype(L, index, LUA_TTABLE);
293 lua_getfield(L, index, "x");
294 p.X = lua_tonumber(L, -1);
296 lua_getfield(L, index, "y");
297 p.Y = lua_tonumber(L, -1);
302 static v2f read_v2f(lua_State *L, int index)
305 luaL_checktype(L, index, LUA_TTABLE);
306 lua_getfield(L, index, "x");
307 p.X = lua_tonumber(L, -1);
309 lua_getfield(L, index, "y");
310 p.Y = lua_tonumber(L, -1);
315 static bool getstringfield(lua_State *L, int table,
316 const char *fieldname, std::string &result)
318 lua_getfield(L, table, fieldname);
320 if(lua_isstring(L, -1)){
321 result = lua_tostring(L, -1);
328 static bool getintfield(lua_State *L, int table,
329 const char *fieldname, int &result)
331 lua_getfield(L, table, fieldname);
333 if(lua_isnumber(L, -1)){
334 result = lua_tonumber(L, -1);
341 static bool getfloatfield(lua_State *L, int table,
342 const char *fieldname, float &result)
344 lua_getfield(L, table, fieldname);
346 if(lua_isnumber(L, -1)){
347 result = lua_tonumber(L, -1);
354 static bool getboolfield(lua_State *L, int table,
355 const char *fieldname, bool &result)
357 lua_getfield(L, table, fieldname);
359 if(lua_isboolean(L, -1)){
360 result = lua_toboolean(L, -1);
367 static std::string getstringfield_default(lua_State *L, int table,
368 const char *fieldname, const std::string &default_)
370 std::string result = default_;
371 getstringfield(L, table, fieldname, result);
375 static int getintfield_default(lua_State *L, int table,
376 const char *fieldname, int default_)
378 int result = default_;
379 getintfield(L, table, fieldname, result);
383 /*static float getfloatfield_default(lua_State *L, int table,
384 const char *fieldname, float default_)
386 float result = default_;
387 getfloatfield(L, table, fieldname, result);
391 static bool getboolfield_default(lua_State *L, int table,
392 const char *fieldname, bool default_)
394 bool result = default_;
395 getboolfield(L, table, fieldname, result);
405 static bool string_to_enum(const EnumString *spec, int &result,
406 const std::string &str)
408 const EnumString *esp = spec;
410 if(str == std::string(esp->str)){
419 /*static bool enum_to_string(const EnumString *spec, std::string &result,
422 const EnumString *esp = spec;
433 static int getenumfield(lua_State *L, int table,
434 const char *fieldname, const EnumString *spec, int default_)
436 int result = default_;
437 string_to_enum(spec, result,
438 getstringfield_default(L, table, fieldname, ""));
442 static void setfloatfield(lua_State *L, int table,
443 const char *fieldname, float value)
445 lua_pushnumber(L, value);
448 lua_setfield(L, table, fieldname);
455 static void inventory_set_list_from_lua(Inventory *inv, const char *name,
456 lua_State *L, int tableindex, IGameDef *gamedef, int forcesize=-1)
459 tableindex = lua_gettop(L) + 1 + tableindex;
460 // If nil, delete list
461 if(lua_isnil(L, tableindex)){
462 inv->deleteList(name);
465 // Otherwise set list
466 std::list<std::string> items;
467 luaL_checktype(L, tableindex, LUA_TTABLE);
468 int table = tableindex;
470 while(lua_next(L, table) != 0){
471 // key at index -2 and value at index -1
472 luaL_checktype(L, -1, LUA_TSTRING);
473 std::string itemstring = lua_tostring(L, -1);
474 items.push_back(itemstring);
475 // removes value, keeps key for next iteration
478 int listsize = (forcesize != -1) ? forcesize : items.size();
479 InventoryList *invlist = inv->addList(name, listsize);
481 for(std::list<std::string>::const_iterator
482 i = items.begin(); i != items.end(); i++){
483 if(forcesize != -1 && index == forcesize)
485 const std::string &itemstring = *i;
486 InventoryItem *newitem = NULL;
488 newitem = InventoryItem::deSerialize(itemstring,
490 InventoryItem *olditem = invlist->changeItem(index, newitem);
494 while(forcesize != -1 && index < forcesize){
495 InventoryItem *olditem = invlist->changeItem(index, NULL);
501 static void inventory_get_list_to_lua(Inventory *inv, const char *name,
504 InventoryList *invlist = inv->getList(name);
509 // Get the table insert function
510 lua_getglobal(L, "table");
511 lua_getfield(L, -1, "insert");
512 int table_insert = lua_gettop(L);
513 // Create and fill table
515 int table = lua_gettop(L);
516 for(u32 i=0; i<invlist->getSize(); i++){
517 InventoryItem *item = invlist->getItem(i);
518 lua_pushvalue(L, table_insert);
519 lua_pushvalue(L, table);
521 lua_pushstring(L, "");
523 lua_pushstring(L, item->getItemString().c_str());
525 if(lua_pcall(L, 2, 0, 0))
526 script_error(L, "error: %s", lua_tostring(L, -1));
531 ToolDiggingProperties
534 static ToolDiggingProperties read_tool_digging_properties(
535 lua_State *L, int table)
537 ToolDiggingProperties prop;
538 getfloatfield(L, table, "full_punch_interval", prop.full_punch_interval);
539 getfloatfield(L, table, "basetime", prop.basetime);
540 getfloatfield(L, table, "dt_weight", prop.dt_weight);
541 getfloatfield(L, table, "dt_crackiness", prop.dt_crackiness);
542 getfloatfield(L, table, "dt_crumbliness", prop.dt_crumbliness);
543 getfloatfield(L, table, "dt_cuttability", prop.dt_cuttability);
544 getfloatfield(L, table, "basedurability", prop.basedurability);
545 getfloatfield(L, table, "dd_weight", prop.dd_weight);
546 getfloatfield(L, table, "dd_crackiness", prop.dd_crackiness);
547 getfloatfield(L, table, "dd_crumbliness", prop.dd_crumbliness);
548 getfloatfield(L, table, "dd_cuttability", prop.dd_cuttability);
552 static void set_tool_digging_properties(lua_State *L, int table,
553 const ToolDiggingProperties &prop)
555 setfloatfield(L, table, "full_punch_interval", prop.full_punch_interval);
556 setfloatfield(L, table, "basetime", prop.basetime);
557 setfloatfield(L, table, "dt_weight", prop.dt_weight);
558 setfloatfield(L, table, "dt_crackiness", prop.dt_crackiness);
559 setfloatfield(L, table, "dt_crumbliness", prop.dt_crumbliness);
560 setfloatfield(L, table, "dt_cuttability", prop.dt_cuttability);
561 setfloatfield(L, table, "basedurability", prop.basedurability);
562 setfloatfield(L, table, "dd_weight", prop.dd_weight);
563 setfloatfield(L, table, "dd_crackiness", prop.dd_crackiness);
564 setfloatfield(L, table, "dd_crumbliness", prop.dd_crumbliness);
565 setfloatfield(L, table, "dd_cuttability", prop.dd_cuttability);
568 static void push_tool_digging_properties(lua_State *L,
569 const ToolDiggingProperties &prop)
572 set_tool_digging_properties(L, -1, prop);
579 static ToolDefinition read_tool_definition(lua_State *L, int table)
582 getstringfield(L, table, "image", def.imagename);
583 def.properties = read_tool_digging_properties(L, table);
587 static void push_tool_definition(lua_State *L, const ToolDefinition &def)
590 lua_pushstring(L, def.imagename.c_str());
591 lua_setfield(L, -2, "image");
592 set_tool_digging_properties(L, -1, def.properties);
596 EnumString definitions
599 struct EnumString es_DrawType[] =
601 {NDT_NORMAL, "normal"},
602 {NDT_AIRLIKE, "airlike"},
603 {NDT_LIQUID, "liquid"},
604 {NDT_FLOWINGLIQUID, "flowingliquid"},
605 {NDT_GLASSLIKE, "glasslike"},
606 {NDT_ALLFACES, "allfaces"},
607 {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
608 {NDT_TORCHLIKE, "torchlike"},
609 {NDT_SIGNLIKE, "signlike"},
610 {NDT_PLANTLIKE, "plantlike"},
611 {NDT_FENCELIKE, "fencelike"},
612 {NDT_RAILLIKE, "raillike"},
616 struct EnumString es_ContentParamType[] =
619 {CPT_LIGHT, "light"},
620 {CPT_MINERAL, "mineral"},
621 {CPT_FACEDIR_SIMPLE, "facedir_simple"},
625 struct EnumString es_LiquidType[] =
627 {LIQUID_NONE, "none"},
628 {LIQUID_FLOWING, "flowing"},
629 {LIQUID_SOURCE, "source"},
633 struct EnumString es_NodeBoxType[] =
635 {NODEBOX_REGULAR, "regular"},
636 {NODEBOX_FIXED, "fixed"},
637 {NODEBOX_WALLMOUNTED, "wallmounted"},
641 struct EnumString es_Diggability[] =
643 {DIGGABLE_NOT, "not"},
644 {DIGGABLE_NORMAL, "normal"},
645 {DIGGABLE_CONSTANT, "constant"},
653 static int l_register_nodedef_defaults(lua_State *L)
655 luaL_checktype(L, 1, LUA_TTABLE);
657 lua_pushvalue(L, 1); // Explicitly put parameter 1 on top of stack
658 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_nodedef_default");
663 // Register new object prototype
664 // register_entity(name, prototype)
665 static int l_register_entity(lua_State *L)
667 std::string name = luaL_checkstring(L, 1);
668 check_modname_prefix(L, name);
669 //infostream<<"register_entity: "<<name<<std::endl;
670 luaL_checktype(L, 2, LUA_TTABLE);
672 // Get minetest.registered_entities
673 lua_getglobal(L, "minetest");
674 lua_getfield(L, -1, "registered_entities");
675 luaL_checktype(L, -1, LUA_TTABLE);
676 int registered_entities = lua_gettop(L);
677 lua_pushvalue(L, 2); // Object = param 2 -> stack top
678 // registered_entities[name] = object
679 lua_setfield(L, registered_entities, name.c_str());
681 // Get registered object to top of stack
684 // Set __index to point to itself
685 lua_pushvalue(L, -1);
686 lua_setfield(L, -2, "__index");
688 // Set metatable.__index = metatable
689 luaL_getmetatable(L, "minetest.entity");
690 lua_pushvalue(L, -1); // duplicate metatable
691 lua_setfield(L, -2, "__index");
692 // Set object metatable
693 lua_setmetatable(L, -2);
695 return 0; /* number of results */
698 class LuaABM : public ActiveBlockModifier
704 std::set<std::string> m_trigger_contents;
705 float m_trigger_interval;
706 u32 m_trigger_chance;
708 LuaABM(lua_State *L, int id,
709 const std::set<std::string> &trigger_contents,
710 float trigger_interval, u32 trigger_chance):
713 m_trigger_contents(trigger_contents),
714 m_trigger_interval(trigger_interval),
715 m_trigger_chance(trigger_chance)
718 virtual std::set<std::string> getTriggerContents()
720 return m_trigger_contents;
722 virtual float getTriggerInterval()
724 return m_trigger_interval;
726 virtual u32 getTriggerChance()
728 return m_trigger_chance;
730 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
731 u32 active_object_count, u32 active_object_count_wider)
733 lua_State *L = m_lua;
736 assert(lua_checkstack(L, 20));
737 StackUnroller stack_unroller(L);
739 // Get minetest.registered_abms
740 lua_getglobal(L, "minetest");
741 lua_getfield(L, -1, "registered_abms");
742 luaL_checktype(L, -1, LUA_TTABLE);
743 int registered_abms = lua_gettop(L);
745 // Get minetest.registered_abms[m_id]
746 lua_pushnumber(L, m_id);
747 lua_gettable(L, registered_abms);
752 luaL_checktype(L, -1, LUA_TTABLE);
753 lua_getfield(L, -1, "action");
754 luaL_checktype(L, -1, LUA_TFUNCTION);
756 pushnode(L, n, env->getGameDef()->ndef());
757 lua_pushnumber(L, active_object_count);
758 lua_pushnumber(L, active_object_count_wider);
759 if(lua_pcall(L, 4, 0, 0))
760 script_error(L, "error: %s", lua_tostring(L, -1));
764 // register_abm({...})
765 static int l_register_abm(lua_State *L)
767 //infostream<<"register_abm"<<std::endl;
768 luaL_checktype(L, 1, LUA_TTABLE);
770 // Get minetest.registered_abms
771 lua_getglobal(L, "minetest");
772 lua_getfield(L, -1, "registered_abms");
773 luaL_checktype(L, -1, LUA_TTABLE);
774 int registered_abms = lua_gettop(L);
779 lua_pushnumber(L, id);
780 lua_gettable(L, registered_abms);
788 infostream<<"register_abm: id="<<id<<std::endl;
790 // registered_abms[id] = spec
791 lua_pushnumber(L, id);
793 lua_settable(L, registered_abms);
795 return 0; /* number of results */
798 // register_tool(name, {lots of stuff})
799 static int l_register_tool(lua_State *L)
801 std::string name = luaL_checkstring(L, 1);
802 check_modname_prefix(L, name);
803 //infostream<<"register_tool: "<<name<<std::endl;
804 luaL_checktype(L, 2, LUA_TTABLE);
807 // Get server from registry
808 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
809 Server *server = (Server*)lua_touserdata(L, -1);
810 // And get the writable tool definition manager from the server
811 IWritableToolDefManager *tooldef =
812 server->getWritableToolDefManager();
814 ToolDefinition def = read_tool_definition(L, table);
816 tooldef->registerTool(name, def);
817 return 0; /* number of results */
820 // register_craftitem(name, {lots of stuff})
821 static int l_register_craftitem(lua_State *L)
823 std::string name = luaL_checkstring(L, 1);
824 check_modname_prefix(L, name);
825 //infostream<<"register_craftitem: "<<name<<std::endl;
826 luaL_checktype(L, 2, LUA_TTABLE);
829 // Get server from registry
830 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
831 Server *server = (Server*)lua_touserdata(L, -1);
832 // And get the writable CraftItem definition manager from the server
833 IWritableCraftItemDefManager *craftitemdef =
834 server->getWritableCraftItemDefManager();
836 // Check if on_drop is defined
837 lua_getfield(L, table, "on_drop");
838 bool got_on_drop = !lua_isnil(L, -1);
841 // Check if on_use is defined
842 lua_getfield(L, table, "on_use");
843 bool got_on_use = !lua_isnil(L, -1);
846 CraftItemDefinition def;
848 getstringfield(L, table, "image", def.imagename);
849 getstringfield(L, table, "cookresult_item", def.cookresult_item);
850 getfloatfield(L, table, "furnace_cooktime", def.furnace_cooktime);
851 getfloatfield(L, table, "furnace_burntime", def.furnace_burntime);
852 def.usable = getboolfield_default(L, table, "usable", got_on_use);
853 getboolfield(L, table, "liquids_pointable", def.liquids_pointable);
854 def.dropcount = getintfield_default(L, table, "dropcount", def.dropcount);
855 def.stack_max = getintfield_default(L, table, "stack_max", def.stack_max);
857 // If an on_drop callback is defined, force dropcount to 1
862 craftitemdef->registerCraftItem(name, def);
864 lua_pushvalue(L, table);
865 scriptapi_add_craftitem(L, name.c_str());
867 return 0; /* number of results */
870 // register_node(name, {lots of stuff})
871 static int l_register_node(lua_State *L)
873 std::string name = luaL_checkstring(L, 1);
874 check_modname_prefix(L, name);
875 //infostream<<"register_node: "<<name<<std::endl;
876 luaL_checktype(L, 2, LUA_TTABLE);
877 int nodedef_table = 2;
879 // Get server from registry
880 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
881 Server *server = (Server*)lua_touserdata(L, -1);
882 // And get the writable node definition manager from the server
883 IWritableNodeDefManager *nodedef =
884 server->getWritableNodeDefManager();
886 // Get default node definition from registry
887 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_nodedef_default");
888 int nodedef_default = lua_gettop(L);
891 Add to minetest.registered_nodes with default as metatable
894 // Get the node definition table given as parameter
895 lua_pushvalue(L, nodedef_table);
897 // Set __index to point to itself
898 lua_pushvalue(L, -1);
899 lua_setfield(L, -2, "__index");
901 // Set nodedef_default as metatable for the definition
902 lua_pushvalue(L, nodedef_default);
903 lua_setmetatable(L, nodedef_table);
905 // minetest.registered_nodes[name] = nodedef
906 lua_getglobal(L, "minetest");
907 lua_getfield(L, -1, "registered_nodes");
908 luaL_checktype(L, -1, LUA_TTABLE);
909 lua_pushstring(L, name.c_str());
910 lua_pushvalue(L, nodedef_table);
919 // Default to getting the corresponding NodeItem when dug
920 f.dug_item = std::string("NodeItem \"")+name+"\" 1";
922 // Default to unknown_block.png as all textures
923 f.setAllTextures("unknown_block.png");
926 Read definiton from Lua
931 /* Visual definition */
933 f.drawtype = (NodeDrawType)getenumfield(L, nodedef_table, "drawtype", es_DrawType,
935 getfloatfield(L, nodedef_table, "visual_scale", f.visual_scale);
937 lua_getfield(L, nodedef_table, "tile_images");
938 if(lua_istable(L, -1)){
939 int table = lua_gettop(L);
942 while(lua_next(L, table) != 0){
943 // key at index -2 and value at index -1
944 if(lua_isstring(L, -1))
945 f.tname_tiles[i] = lua_tostring(L, -1);
947 f.tname_tiles[i] = "";
948 // removes value, keeps key for next iteration
956 // Copy last value to all remaining textures
958 std::string lastname = f.tname_tiles[i-1];
960 f.tname_tiles[i] = lastname;
967 getstringfield(L, nodedef_table, "inventory_image", f.tname_inventory);
969 lua_getfield(L, nodedef_table, "special_materials");
970 if(lua_istable(L, -1)){
971 int table = lua_gettop(L);
974 while(lua_next(L, table) != 0){
975 // key at index -2 and value at index -1
976 int smtable = lua_gettop(L);
977 std::string tname = getstringfield_default(
978 L, smtable, "image", "");
979 bool backface_culling = getboolfield_default(
980 L, smtable, "backface_culling", true);
981 MaterialSpec mspec(tname, backface_culling);
982 f.setSpecialMaterial(i, mspec);
983 // removes value, keeps key for next iteration
994 f.alpha = getintfield_default(L, nodedef_table, "alpha", 255);
998 lua_getfield(L, nodedef_table, "post_effect_color");
999 if(!lua_isnil(L, -1))
1000 f.post_effect_color = readARGB8(L, -1);
1003 f.param_type = (ContentParamType)getenumfield(L, nodedef_table, "paramtype",
1004 es_ContentParamType, CPT_NONE);
1006 // True for all ground-like things like stone and mud, false for eg. trees
1007 getboolfield(L, nodedef_table, "is_ground_content", f.is_ground_content);
1008 getboolfield(L, nodedef_table, "light_propagates", f.light_propagates);
1009 getboolfield(L, nodedef_table, "sunlight_propagates", f.sunlight_propagates);
1010 // This is used for collision detection.
1011 // Also for general solidness queries.
1012 getboolfield(L, nodedef_table, "walkable", f.walkable);
1013 // Player can point to these
1014 getboolfield(L, nodedef_table, "pointable", f.pointable);
1015 // Player can dig these
1016 getboolfield(L, nodedef_table, "diggable", f.diggable);
1017 // Player can climb these
1018 getboolfield(L, nodedef_table, "climbable", f.climbable);
1019 // Player can build on these
1020 getboolfield(L, nodedef_table, "buildable_to", f.buildable_to);
1021 // If true, param2 is set to direction when placed. Used for torches.
1022 // NOTE: the direction format is quite inefficient and should be changed
1023 getboolfield(L, nodedef_table, "wall_mounted", f.wall_mounted);
1024 // Whether this content type often contains mineral.
1025 // Used for texture atlas creation.
1026 // Currently only enabled for CONTENT_STONE.
1027 getboolfield(L, nodedef_table, "often_contains_mineral", f.often_contains_mineral);
1028 // Inventory item string as which the node appears in inventory when dug.
1029 // Mineral overrides this.
1030 getstringfield(L, nodedef_table, "dug_item", f.dug_item);
1031 // Extra dug item and its rarity
1032 getstringfield(L, nodedef_table, "extra_dug_item", f.extra_dug_item);
1033 // Usual get interval for extra dug item
1034 getintfield(L, nodedef_table, "extra_dug_item_rarity", f.extra_dug_item_rarity);
1035 // Metadata name of node (eg. "furnace")
1036 getstringfield(L, nodedef_table, "metadata_name", f.metadata_name);
1037 // Whether the node is non-liquid, source liquid or flowing liquid
1038 f.liquid_type = (LiquidType)getenumfield(L, nodedef_table, "liquidtype",
1039 es_LiquidType, LIQUID_NONE);
1040 // If the content is liquid, this is the flowing version of the liquid.
1041 getstringfield(L, nodedef_table, "liquid_alternative_flowing",
1042 f.liquid_alternative_flowing);
1043 // If the content is liquid, this is the source version of the liquid.
1044 getstringfield(L, nodedef_table, "liquid_alternative_source",
1045 f.liquid_alternative_source);
1046 // Viscosity for fluid flow, ranging from 1 to 7, with
1047 // 1 giving almost instantaneous propagation and 7 being
1048 // the slowest possible
1049 f.liquid_viscosity = getintfield_default(L, nodedef_table,
1050 "liquid_viscosity", f.liquid_viscosity);
1051 // Amount of light the node emits
1052 f.light_source = getintfield_default(L, nodedef_table,
1053 "light_source", f.light_source);
1054 f.damage_per_second = getintfield_default(L, nodedef_table,
1055 "damage_per_second", f.damage_per_second);
1057 lua_getfield(L, nodedef_table, "selection_box");
1058 if(lua_istable(L, -1)){
1059 f.selection_box.type = (NodeBoxType)getenumfield(L, -1, "type",
1060 es_NodeBoxType, NODEBOX_REGULAR);
1062 lua_getfield(L, -1, "fixed");
1063 if(lua_istable(L, -1))
1064 f.selection_box.fixed = read_aabbox3df32(L, -1, BS);
1067 lua_getfield(L, -1, "wall_top");
1068 if(lua_istable(L, -1))
1069 f.selection_box.wall_top = read_aabbox3df32(L, -1, BS);
1072 lua_getfield(L, -1, "wall_bottom");
1073 if(lua_istable(L, -1))
1074 f.selection_box.wall_bottom = read_aabbox3df32(L, -1, BS);
1077 lua_getfield(L, -1, "wall_side");
1078 if(lua_istable(L, -1))
1079 f.selection_box.wall_side = read_aabbox3df32(L, -1, BS);
1084 lua_getfield(L, nodedef_table, "material");
1085 if(lua_istable(L, -1)){
1086 f.material.diggability = (Diggability)getenumfield(L, -1, "diggability",
1087 es_Diggability, DIGGABLE_NORMAL);
1089 getfloatfield(L, -1, "constant_time", f.material.constant_time);
1090 getfloatfield(L, -1, "weight", f.material.weight);
1091 getfloatfield(L, -1, "crackiness", f.material.crackiness);
1092 getfloatfield(L, -1, "crumbliness", f.material.crumbliness);
1093 getfloatfield(L, -1, "cuttability", f.material.cuttability);
1094 getfloatfield(L, -1, "flammability", f.material.flammability);
1098 getstringfield(L, nodedef_table, "cookresult_item", f.cookresult_item);
1099 getfloatfield(L, nodedef_table, "furnace_cooktime", f.furnace_cooktime);
1100 getfloatfield(L, nodedef_table, "furnace_burntime", f.furnace_burntime);
1106 nodedef->set(name, f);
1108 return 0; /* number of results */
1111 // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
1112 static int l_register_craft(lua_State *L)
1114 //infostream<<"register_craft"<<std::endl;
1115 luaL_checktype(L, 1, LUA_TTABLE);
1118 // Get server from registry
1119 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
1120 Server *server = (Server*)lua_touserdata(L, -1);
1121 // And get the writable craft definition manager from the server
1122 IWritableCraftDefManager *craftdef =
1123 server->getWritableCraftDefManager();
1127 std::vector<std::string> input;
1129 lua_getfield(L, table0, "output");
1130 luaL_checktype(L, -1, LUA_TSTRING);
1131 if(lua_isstring(L, -1))
1132 output = lua_tostring(L, -1);
1135 lua_getfield(L, table0, "recipe");
1136 luaL_checktype(L, -1, LUA_TTABLE);
1137 if(lua_istable(L, -1)){
1138 int table1 = lua_gettop(L);
1141 while(lua_next(L, table1) != 0){
1143 // key at index -2 and value at index -1
1144 luaL_checktype(L, -1, LUA_TTABLE);
1145 if(lua_istable(L, -1)){
1146 int table2 = lua_gettop(L);
1148 while(lua_next(L, table2) != 0){
1149 // key at index -2 and value at index -1
1150 luaL_checktype(L, -1, LUA_TSTRING);
1151 input.push_back(lua_tostring(L, -1));
1152 // removes value, keeps key for next iteration
1160 if(colcount != width){
1162 error += "Invalid crafting recipe (output=\""
1164 throw LuaError(L, error);
1167 // removes value, keeps key for next iteration
1174 CraftDefinition def(output, width, input);
1175 craftdef->registerCraft(def);
1177 return 0; /* number of results */
1180 // setting_get(name)
1181 static int l_setting_get(lua_State *L)
1183 const char *name = luaL_checkstring(L, 1);
1185 std::string value = g_settings->get(name);
1186 lua_pushstring(L, value.c_str());
1187 } catch(SettingNotFoundException &e){
1193 // setting_getbool(name)
1194 static int l_setting_getbool(lua_State *L)
1196 const char *name = luaL_checkstring(L, 1);
1198 bool value = g_settings->getBool(name);
1199 lua_pushboolean(L, value);
1200 } catch(SettingNotFoundException &e){
1206 // chat_send_all(text)
1207 static int l_chat_send_all(lua_State *L)
1209 const char *text = luaL_checkstring(L, 1);
1210 // Get server from registry
1211 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
1212 Server *server = (Server*)lua_touserdata(L, -1);
1214 server->notifyPlayers(narrow_to_wide(text));
1218 // chat_send_player(name, text)
1219 static int l_chat_send_player(lua_State *L)
1221 const char *name = luaL_checkstring(L, 1);
1222 const char *text = luaL_checkstring(L, 2);
1223 // Get server from registry
1224 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
1225 Server *server = (Server*)lua_touserdata(L, -1);
1227 server->notifyPlayer(name, narrow_to_wide(text));
1231 // get_player_privs(name, text)
1232 static int l_get_player_privs(lua_State *L)
1234 const char *name = luaL_checkstring(L, 1);
1235 // Get server from registry
1236 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
1237 Server *server = (Server*)lua_touserdata(L, -1);
1240 int table = lua_gettop(L);
1241 u64 privs_i = server->getPlayerAuthPrivs(name);
1242 // Special case for the "name" setting (local player / server owner)
1243 if(name == g_settings->get("name"))
1245 std::set<std::string> privs_s = privsToSet(privs_i);
1246 for(std::set<std::string>::const_iterator
1247 i = privs_s.begin(); i != privs_s.end(); i++){
1248 lua_pushboolean(L, true);
1249 lua_setfield(L, table, i->c_str());
1251 lua_pushvalue(L, table);
1255 static const struct luaL_Reg minetest_f [] = {
1256 {"register_nodedef_defaults", l_register_nodedef_defaults},
1257 {"register_entity", l_register_entity},
1258 {"register_tool", l_register_tool},
1259 {"register_craftitem", l_register_craftitem},
1260 {"register_node", l_register_node},
1261 {"register_craft", l_register_craft},
1262 {"register_abm", l_register_abm},
1263 {"setting_get", l_setting_get},
1264 {"setting_getbool", l_setting_getbool},
1265 {"chat_send_all", l_chat_send_all},
1266 {"chat_send_player", l_chat_send_player},
1267 {"get_player_privs", l_get_player_privs},
1275 static const struct luaL_Reg minetest_entity_m [] = {
1280 Getters for stuff in main tables
1283 static void objectref_get(lua_State *L, u16 id)
1285 // Get minetest.object_refs[i]
1286 lua_getglobal(L, "minetest");
1287 lua_getfield(L, -1, "object_refs");
1288 luaL_checktype(L, -1, LUA_TTABLE);
1289 lua_pushnumber(L, id);
1290 lua_gettable(L, -2);
1291 lua_remove(L, -2); // object_refs
1292 lua_remove(L, -2); // minetest
1295 static void luaentity_get(lua_State *L, u16 id)
1297 // Get minetest.luaentities[i]
1298 lua_getglobal(L, "minetest");
1299 lua_getfield(L, -1, "luaentities");
1300 luaL_checktype(L, -1, LUA_TTABLE);
1301 lua_pushnumber(L, id);
1302 lua_gettable(L, -2);
1303 lua_remove(L, -2); // luaentities
1304 lua_remove(L, -2); // minetest
1311 #define method(class, name) {#name, class::l_##name}
1321 ServerEnvironment *m_env;
1323 static const char className[];
1324 static const luaL_reg methods[];
1326 static NodeMetaRef *checkobject(lua_State *L, int narg)
1328 luaL_checktype(L, narg, LUA_TUSERDATA);
1329 void *ud = luaL_checkudata(L, narg, className);
1330 if(!ud) luaL_typerror(L, narg, className);
1331 return *(NodeMetaRef**)ud; // unbox pointer
1334 static NodeMetadata* getmeta(NodeMetaRef *ref)
1336 NodeMetadata *meta = ref->m_env->getMap().getNodeMetadata(ref->m_p);
1340 /*static IGenericNodeMetadata* getgenericmeta(NodeMetaRef *ref)
1342 NodeMetadata *meta = getmeta(ref);
1345 if(meta->typeId() != NODEMETA_GENERIC)
1347 return (IGenericNodeMetadata*)meta;
1350 static void reportMetadataChange(NodeMetaRef *ref)
1352 // Inform other things that the metadata has changed
1353 v3s16 blockpos = getNodeBlockPos(ref->m_p);
1355 event.type = MEET_BLOCK_NODE_METADATA_CHANGED;
1357 ref->m_env->getMap().dispatchEvent(&event);
1358 // Set the block to be saved
1359 MapBlock *block = ref->m_env->getMap().getBlockNoCreateNoEx(blockpos);
1361 block->raiseModified(MOD_STATE_WRITE_NEEDED,
1362 "NodeMetaRef::reportMetadataChange");
1365 // Exported functions
1367 // garbage collector
1368 static int gc_object(lua_State *L) {
1369 NodeMetaRef *o = *(NodeMetaRef **)(lua_touserdata(L, 1));
1375 static int l_get_type(lua_State *L)
1377 NodeMetaRef *ref = checkobject(L, 1);
1378 NodeMetadata *meta = getmeta(ref);
1384 lua_pushstring(L, meta->typeName());
1388 // allows_text_input(self)
1389 static int l_allows_text_input(lua_State *L)
1391 NodeMetaRef *ref = checkobject(L, 1);
1392 NodeMetadata *meta = getmeta(ref);
1393 if(meta == NULL) return 0;
1395 lua_pushboolean(L, meta->allowsTextInput());
1399 // set_text(self, text)
1400 static int l_set_text(lua_State *L)
1402 NodeMetaRef *ref = checkobject(L, 1);
1403 NodeMetadata *meta = getmeta(ref);
1404 if(meta == NULL) return 0;
1406 std::string text = lua_tostring(L, 2);
1407 meta->setText(text);
1408 reportMetadataChange(ref);
1413 static int l_get_text(lua_State *L)
1415 NodeMetaRef *ref = checkobject(L, 1);
1416 NodeMetadata *meta = getmeta(ref);
1417 if(meta == NULL) return 0;
1419 std::string text = meta->getText();
1420 lua_pushstring(L, text.c_str());
1425 static int l_get_owner(lua_State *L)
1427 NodeMetaRef *ref = checkobject(L, 1);
1428 NodeMetadata *meta = getmeta(ref);
1429 if(meta == NULL) return 0;
1431 std::string owner = meta->getOwner();
1432 lua_pushstring(L, owner.c_str());
1436 /* IGenericNodeMetadata interface */
1438 // set_infotext(self, text)
1439 static int l_set_infotext(lua_State *L)
1441 infostream<<__FUNCTION_NAME<<std::endl;
1442 NodeMetaRef *ref = checkobject(L, 1);
1443 NodeMetadata *meta = getmeta(ref);
1444 if(meta == NULL) return 0;
1446 std::string text = lua_tostring(L, 2);
1447 meta->setInfoText(text);
1448 reportMetadataChange(ref);
1452 // inventory_set_list(self, name, {item1, item2, ...})
1453 static int l_inventory_set_list(lua_State *L)
1455 NodeMetaRef *ref = checkobject(L, 1);
1456 NodeMetadata *meta = getmeta(ref);
1457 if(meta == NULL) return 0;
1459 Inventory *inv = meta->getInventory();
1460 const char *name = lua_tostring(L, 2);
1461 inventory_set_list_from_lua(inv, name, L, 3,
1462 ref->m_env->getGameDef());
1463 reportMetadataChange(ref);
1467 // inventory_get_list(self, name)
1468 static int l_inventory_get_list(lua_State *L)
1470 NodeMetaRef *ref = checkobject(L, 1);
1471 NodeMetadata *meta = getmeta(ref);
1472 if(meta == NULL) return 0;
1474 Inventory *inv = meta->getInventory();
1475 const char *name = lua_tostring(L, 2);
1476 inventory_get_list_to_lua(inv, name, L);
1480 // set_inventory_draw_spec(self, text)
1481 static int l_set_inventory_draw_spec(lua_State *L)
1483 NodeMetaRef *ref = checkobject(L, 1);
1484 NodeMetadata *meta = getmeta(ref);
1485 if(meta == NULL) return 0;
1487 std::string text = lua_tostring(L, 2);
1488 meta->setInventoryDrawSpec(text);
1489 reportMetadataChange(ref);
1493 // set_allow_text_input(self, text)
1494 static int l_set_allow_text_input(lua_State *L)
1496 NodeMetaRef *ref = checkobject(L, 1);
1497 NodeMetadata *meta = getmeta(ref);
1498 if(meta == NULL) return 0;
1500 bool b = lua_toboolean(L, 2);
1501 meta->setAllowTextInput(b);
1502 reportMetadataChange(ref);
1506 // set_allow_removal(self, text)
1507 static int l_set_allow_removal(lua_State *L)
1509 NodeMetaRef *ref = checkobject(L, 1);
1510 NodeMetadata *meta = getmeta(ref);
1511 if(meta == NULL) return 0;
1513 bool b = lua_toboolean(L, 2);
1514 meta->setRemovalDisabled(!b);
1515 reportMetadataChange(ref);
1519 // set_enforce_owner(self, text)
1520 static int l_set_enforce_owner(lua_State *L)
1522 NodeMetaRef *ref = checkobject(L, 1);
1523 NodeMetadata *meta = getmeta(ref);
1524 if(meta == NULL) return 0;
1526 bool b = lua_toboolean(L, 2);
1527 meta->setEnforceOwner(b);
1528 reportMetadataChange(ref);
1532 // is_inventory_modified(self)
1533 static int l_is_inventory_modified(lua_State *L)
1535 NodeMetaRef *ref = checkobject(L, 1);
1536 NodeMetadata *meta = getmeta(ref);
1537 if(meta == NULL) return 0;
1539 lua_pushboolean(L, meta->isInventoryModified());
1543 // reset_inventory_modified(self)
1544 static int l_reset_inventory_modified(lua_State *L)
1546 NodeMetaRef *ref = checkobject(L, 1);
1547 NodeMetadata *meta = getmeta(ref);
1548 if(meta == NULL) return 0;
1550 meta->resetInventoryModified();
1551 reportMetadataChange(ref);
1555 // is_text_modified(self)
1556 static int l_is_text_modified(lua_State *L)
1558 NodeMetaRef *ref = checkobject(L, 1);
1559 NodeMetadata *meta = getmeta(ref);
1560 if(meta == NULL) return 0;
1562 lua_pushboolean(L, meta->isTextModified());
1566 // reset_text_modified(self)
1567 static int l_reset_text_modified(lua_State *L)
1569 NodeMetaRef *ref = checkobject(L, 1);
1570 NodeMetadata *meta = getmeta(ref);
1571 if(meta == NULL) return 0;
1573 meta->resetTextModified();
1574 reportMetadataChange(ref);
1578 // set_string(self, name, var)
1579 static int l_set_string(lua_State *L)
1581 NodeMetaRef *ref = checkobject(L, 1);
1582 NodeMetadata *meta = getmeta(ref);
1583 if(meta == NULL) return 0;
1585 std::string name = lua_tostring(L, 2);
1587 const char *s = lua_tolstring(L, 3, &len);
1588 std::string str(s, len);
1589 meta->setString(name, str);
1590 reportMetadataChange(ref);
1594 // get_string(self, name)
1595 static int l_get_string(lua_State *L)
1597 NodeMetaRef *ref = checkobject(L, 1);
1598 NodeMetadata *meta = getmeta(ref);
1599 if(meta == NULL) return 0;
1601 std::string name = lua_tostring(L, 2);
1602 std::string str = meta->getString(name);
1603 lua_pushlstring(L, str.c_str(), str.size());
1608 NodeMetaRef(v3s16 p, ServerEnvironment *env):
1618 // Creates an NodeMetaRef and leaves it on top of stack
1619 // Not callable from Lua; all references are created on the C side.
1620 static void create(lua_State *L, v3s16 p, ServerEnvironment *env)
1622 NodeMetaRef *o = new NodeMetaRef(p, env);
1623 //infostream<<"NodeMetaRef::create: o="<<o<<std::endl;
1624 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
1625 luaL_getmetatable(L, className);
1626 lua_setmetatable(L, -2);
1629 static void Register(lua_State *L)
1632 int methodtable = lua_gettop(L);
1633 luaL_newmetatable(L, className);
1634 int metatable = lua_gettop(L);
1636 lua_pushliteral(L, "__metatable");
1637 lua_pushvalue(L, methodtable);
1638 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
1640 lua_pushliteral(L, "__index");
1641 lua_pushvalue(L, methodtable);
1642 lua_settable(L, metatable);
1644 lua_pushliteral(L, "__gc");
1645 lua_pushcfunction(L, gc_object);
1646 lua_settable(L, metatable);
1648 lua_pop(L, 1); // drop metatable
1650 luaL_openlib(L, 0, methods, 0); // fill methodtable
1651 lua_pop(L, 1); // drop methodtable
1653 // Cannot be created from Lua
1654 //lua_register(L, className, create_object);
1657 const char NodeMetaRef::className[] = "NodeMetaRef";
1658 const luaL_reg NodeMetaRef::methods[] = {
1659 method(NodeMetaRef, get_type),
1660 method(NodeMetaRef, allows_text_input),
1661 method(NodeMetaRef, set_text),
1662 method(NodeMetaRef, get_text),
1663 method(NodeMetaRef, get_owner),
1664 method(NodeMetaRef, set_infotext),
1665 method(NodeMetaRef, inventory_set_list),
1666 method(NodeMetaRef, inventory_get_list),
1667 method(NodeMetaRef, set_inventory_draw_spec),
1668 method(NodeMetaRef, set_allow_text_input),
1669 method(NodeMetaRef, set_allow_removal),
1670 method(NodeMetaRef, set_enforce_owner),
1671 method(NodeMetaRef, is_inventory_modified),
1672 method(NodeMetaRef, reset_inventory_modified),
1673 method(NodeMetaRef, is_text_modified),
1674 method(NodeMetaRef, reset_text_modified),
1675 method(NodeMetaRef, set_string),
1676 method(NodeMetaRef, get_string),
1687 ServerActiveObject *m_object;
1689 static const char className[];
1690 static const luaL_reg methods[];
1692 static ObjectRef *checkobject(lua_State *L, int narg)
1694 luaL_checktype(L, narg, LUA_TUSERDATA);
1695 void *ud = luaL_checkudata(L, narg, className);
1696 if(!ud) luaL_typerror(L, narg, className);
1697 return *(ObjectRef**)ud; // unbox pointer
1700 static ServerActiveObject* getobject(ObjectRef *ref)
1702 ServerActiveObject *co = ref->m_object;
1706 static LuaEntitySAO* getluaobject(ObjectRef *ref)
1708 ServerActiveObject *obj = getobject(ref);
1711 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
1713 return (LuaEntitySAO*)obj;
1716 static ServerRemotePlayer* getplayer(ObjectRef *ref)
1718 ServerActiveObject *obj = getobject(ref);
1721 if(obj->getType() != ACTIVEOBJECT_TYPE_PLAYER)
1723 return static_cast<ServerRemotePlayer*>(obj);
1726 // Exported functions
1728 // garbage collector
1729 static int gc_object(lua_State *L) {
1730 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
1731 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
1737 static int l_remove(lua_State *L)
1739 ObjectRef *ref = checkobject(L, 1);
1740 ServerActiveObject *co = getobject(ref);
1741 if(co == NULL) return 0;
1742 infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
1743 co->m_removed = true;
1748 // returns: {x=num, y=num, z=num}
1749 static int l_getpos(lua_State *L)
1751 ObjectRef *ref = checkobject(L, 1);
1752 ServerActiveObject *co = getobject(ref);
1753 if(co == NULL) return 0;
1754 v3f pos = co->getBasePosition() / BS;
1756 lua_pushnumber(L, pos.X);
1757 lua_setfield(L, -2, "x");
1758 lua_pushnumber(L, pos.Y);
1759 lua_setfield(L, -2, "y");
1760 lua_pushnumber(L, pos.Z);
1761 lua_setfield(L, -2, "z");
1765 // setpos(self, pos)
1766 static int l_setpos(lua_State *L)
1768 ObjectRef *ref = checkobject(L, 1);
1769 //LuaEntitySAO *co = getluaobject(ref);
1770 ServerActiveObject *co = getobject(ref);
1771 if(co == NULL) return 0;
1773 v3f pos = readFloatPos(L, 2);
1779 // moveto(self, pos, continuous=false)
1780 static int l_moveto(lua_State *L)
1782 ObjectRef *ref = checkobject(L, 1);
1783 //LuaEntitySAO *co = getluaobject(ref);
1784 ServerActiveObject *co = getobject(ref);
1785 if(co == NULL) return 0;
1787 v3f pos = readFloatPos(L, 2);
1789 bool continuous = lua_toboolean(L, 3);
1791 co->moveTo(pos, continuous);
1795 // punch(self, puncher); puncher = an another ObjectRef
1796 static int l_punch(lua_State *L)
1798 ObjectRef *ref = checkobject(L, 1);
1799 ObjectRef *ref2 = checkobject(L, 2);
1800 ServerActiveObject *co = getobject(ref);
1801 ServerActiveObject *co2 = getobject(ref2);
1802 if(co == NULL) return 0;
1803 if(co2 == NULL) return 0;
1809 // right_click(self, clicker); clicker = an another ObjectRef
1810 static int l_right_click(lua_State *L)
1812 ObjectRef *ref = checkobject(L, 1);
1813 ObjectRef *ref2 = checkobject(L, 2);
1814 ServerActiveObject *co = getobject(ref);
1815 ServerActiveObject *co2 = getobject(ref2);
1816 if(co == NULL) return 0;
1817 if(co2 == NULL) return 0;
1819 co->rightClick(co2);
1823 // get_wield_digging_properties(self)
1824 static int l_get_wield_digging_properties(lua_State *L)
1826 ObjectRef *ref = checkobject(L, 1);
1827 ServerActiveObject *co = getobject(ref);
1828 if(co == NULL) return 0;
1830 ToolDiggingProperties prop;
1831 co->getWieldDiggingProperties(&prop);
1832 push_tool_digging_properties(L, prop);
1836 // damage_wielded_item(self, amount)
1837 static int l_damage_wielded_item(lua_State *L)
1839 ObjectRef *ref = checkobject(L, 1);
1840 ServerActiveObject *co = getobject(ref);
1841 if(co == NULL) return 0;
1843 int amount = lua_tonumber(L, 2);
1844 co->damageWieldedItem(amount);
1848 // add_to_inventory(self, itemstring)
1849 // returns: true if item was added, (false, "reason") otherwise
1850 static int l_add_to_inventory(lua_State *L)
1852 ObjectRef *ref = checkobject(L, 1);
1853 luaL_checkstring(L, 2);
1854 ServerActiveObject *co = getobject(ref);
1855 if(co == NULL) return 0;
1857 const char *itemstring = lua_tostring(L, 2);
1858 infostream<<"ObjectRef::l_add_to_inventory(): id="<<co->getId()
1859 <<" itemstring=\""<<itemstring<<"\""<<std::endl;
1861 std::istringstream is(itemstring, std::ios::binary);
1862 ServerEnvironment *env = co->getEnv();
1864 IGameDef *gamedef = env->getGameDef();
1866 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
1867 if(item->getCount() == 0)
1869 bool added = co->addToInventory(item);
1871 lua_pushboolean(L, added);
1873 lua_pushstring(L, "failed to add item");
1875 } catch(SerializationError &e){
1877 lua_pushboolean(L, false);
1878 lua_pushstring(L, (std::string("Invalid item: ")
1879 + e.what()).c_str());
1884 // add_to_inventory_later(self, itemstring)
1886 static int l_add_to_inventory_later(lua_State *L)
1888 ObjectRef *ref = checkobject(L, 1);
1889 luaL_checkstring(L, 2);
1890 ServerActiveObject *co = getobject(ref);
1891 if(co == NULL) return 0;
1893 const char *itemstring = lua_tostring(L, 2);
1894 infostream<<"ObjectRef::l_add_to_inventory_later(): id="<<co->getId()
1895 <<" itemstring=\""<<itemstring<<"\""<<std::endl;
1897 std::istringstream is(itemstring, std::ios::binary);
1898 ServerEnvironment *env = co->getEnv();
1900 IGameDef *gamedef = env->getGameDef();
1901 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
1902 infostream<<"item="<<env<<std::endl;
1903 co->addToInventoryLater(item);
1909 // hp = number of hitpoints (2 * number of hearts)
1911 static int l_set_hp(lua_State *L)
1913 ObjectRef *ref = checkobject(L, 1);
1914 luaL_checknumber(L, 2);
1915 ServerActiveObject *co = getobject(ref);
1916 if(co == NULL) return 0;
1917 int hp = lua_tonumber(L, 2);
1918 infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
1919 <<" hp="<<hp<<std::endl;
1927 // returns: number of hitpoints (2 * number of hearts)
1928 // 0 if not applicable to this type of object
1929 static int l_get_hp(lua_State *L)
1931 ObjectRef *ref = checkobject(L, 1);
1932 ServerActiveObject *co = getobject(ref);
1933 if(co == NULL) return 0;
1934 int hp = co->getHP();
1935 infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
1936 <<" hp="<<hp<<std::endl;
1938 lua_pushnumber(L, hp);
1942 /* LuaEntitySAO-only */
1944 // setvelocity(self, {x=num, y=num, z=num})
1945 static int l_setvelocity(lua_State *L)
1947 ObjectRef *ref = checkobject(L, 1);
1948 LuaEntitySAO *co = getluaobject(ref);
1949 if(co == NULL) return 0;
1951 v3f pos = readFloatPos(L, 2);
1953 co->setVelocity(pos);
1957 // setacceleration(self, {x=num, y=num, z=num})
1958 static int l_setacceleration(lua_State *L)
1960 ObjectRef *ref = checkobject(L, 1);
1961 LuaEntitySAO *co = getluaobject(ref);
1962 if(co == NULL) return 0;
1964 v3f pos = readFloatPos(L, 2);
1966 co->setAcceleration(pos);
1970 // getacceleration(self)
1971 static int l_getacceleration(lua_State *L)
1973 ObjectRef *ref = checkobject(L, 1);
1974 LuaEntitySAO *co = getluaobject(ref);
1975 if(co == NULL) return 0;
1977 v3f v = co->getAcceleration();
1982 // settexturemod(self, mod)
1983 static int l_settexturemod(lua_State *L)
1985 ObjectRef *ref = checkobject(L, 1);
1986 LuaEntitySAO *co = getluaobject(ref);
1987 if(co == NULL) return 0;
1989 std::string mod = lua_tostring(L, 2);
1990 co->setTextureMod(mod);
1994 // setsprite(self, p={x=0,y=0}, num_frames=1, framelength=0.2,
1995 // select_horiz_by_yawpitch=false)
1996 static int l_setsprite(lua_State *L)
1998 ObjectRef *ref = checkobject(L, 1);
1999 LuaEntitySAO *co = getluaobject(ref);
2000 if(co == NULL) return 0;
2003 if(!lua_isnil(L, 2))
2004 p = read_v2s16(L, 2);
2006 if(!lua_isnil(L, 3))
2007 num_frames = lua_tonumber(L, 3);
2008 float framelength = 0.2;
2009 if(!lua_isnil(L, 4))
2010 framelength = lua_tonumber(L, 4);
2011 bool select_horiz_by_yawpitch = false;
2012 if(!lua_isnil(L, 5))
2013 select_horiz_by_yawpitch = lua_toboolean(L, 5);
2014 co->setSprite(p, num_frames, framelength, select_horiz_by_yawpitch);
2020 // get_player_name(self)
2021 static int l_get_player_name(lua_State *L)
2023 ObjectRef *ref = checkobject(L, 1);
2024 ServerRemotePlayer *player = getplayer(ref);
2030 lua_pushstring(L, player->getName());
2034 // inventory_set_list(self, name, {item1, item2, ...})
2035 static int l_inventory_set_list(lua_State *L)
2037 ObjectRef *ref = checkobject(L, 1);
2038 ServerRemotePlayer *player = getplayer(ref);
2039 if(player == NULL) return 0;
2040 const char *name = lua_tostring(L, 2);
2042 inventory_set_list_from_lua(&player->inventory, name, L, 3,
2043 player->getEnv()->getGameDef(), PLAYER_INVENTORY_SIZE);
2044 player->m_inventory_not_sent = true;
2048 // inventory_get_list(self, name)
2049 static int l_inventory_get_list(lua_State *L)
2051 ObjectRef *ref = checkobject(L, 1);
2052 ServerRemotePlayer *player = getplayer(ref);
2053 if(player == NULL) return 0;
2054 const char *name = lua_tostring(L, 2);
2056 inventory_get_list_to_lua(&player->inventory, name, L);
2060 // get_wielded_itemstring(self)
2061 static int l_get_wielded_itemstring(lua_State *L)
2063 ObjectRef *ref = checkobject(L, 1);
2064 ServerRemotePlayer *player = getplayer(ref);
2065 if(player == NULL) return 0;
2067 InventoryItem *item = player->getWieldedItem();
2072 lua_pushstring(L, item->getItemString().c_str());
2076 // get_wielded_item(self)
2077 static int l_get_wielded_item(lua_State *L)
2079 ObjectRef *ref = checkobject(L, 1);
2080 ServerRemotePlayer *player = getplayer(ref);
2081 if(player == NULL) return 0;
2083 InventoryItem *item0 = player->getWieldedItem();
2088 if(std::string("MaterialItem") == item0->getName()){
2089 MaterialItem *item = (MaterialItem*)item0;
2091 lua_pushstring(L, "NodeItem");
2092 lua_setfield(L, -2, "type");
2093 lua_pushstring(L, item->getNodeName().c_str());
2094 lua_setfield(L, -2, "name");
2096 else if(std::string("CraftItem") == item0->getName()){
2097 CraftItem *item = (CraftItem*)item0;
2099 lua_pushstring(L, "CraftItem");
2100 lua_setfield(L, -2, "type");
2101 lua_pushstring(L, item->getSubName().c_str());
2102 lua_setfield(L, -2, "name");
2104 else if(std::string("ToolItem") == item0->getName()){
2105 ToolItem *item = (ToolItem*)item0;
2107 lua_pushstring(L, "ToolItem");
2108 lua_setfield(L, -2, "type");
2109 lua_pushstring(L, item->getToolName().c_str());
2110 lua_setfield(L, -2, "name");
2111 lua_pushstring(L, itos(item->getWear()).c_str());
2112 lua_setfield(L, -2, "wear");
2115 errorstream<<"l_get_wielded_item: Unknown item name: \""
2116 <<item0->getName()<<"\""<<std::endl;
2123 ObjectRef(ServerActiveObject *object):
2126 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
2132 infostream<<"ObjectRef destructing for id="
2133 <<m_object->getId()<<std::endl;
2135 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
2138 // Creates an ObjectRef and leaves it on top of stack
2139 // Not callable from Lua; all references are created on the C side.
2140 static void create(lua_State *L, ServerActiveObject *object)
2142 ObjectRef *o = new ObjectRef(object);
2143 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
2144 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
2145 luaL_getmetatable(L, className);
2146 lua_setmetatable(L, -2);
2149 static void set_null(lua_State *L)
2151 ObjectRef *o = checkobject(L, -1);
2155 static void Register(lua_State *L)
2158 int methodtable = lua_gettop(L);
2159 luaL_newmetatable(L, className);
2160 int metatable = lua_gettop(L);
2162 lua_pushliteral(L, "__metatable");
2163 lua_pushvalue(L, methodtable);
2164 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
2166 lua_pushliteral(L, "__index");
2167 lua_pushvalue(L, methodtable);
2168 lua_settable(L, metatable);
2170 lua_pushliteral(L, "__gc");
2171 lua_pushcfunction(L, gc_object);
2172 lua_settable(L, metatable);
2174 lua_pop(L, 1); // drop metatable
2176 luaL_openlib(L, 0, methods, 0); // fill methodtable
2177 lua_pop(L, 1); // drop methodtable
2179 // Cannot be created from Lua
2180 //lua_register(L, className, create_object);
2183 const char ObjectRef::className[] = "ObjectRef";
2184 const luaL_reg ObjectRef::methods[] = {
2185 // ServerActiveObject
2186 method(ObjectRef, remove),
2187 method(ObjectRef, getpos),
2188 method(ObjectRef, setpos),
2189 method(ObjectRef, moveto),
2190 method(ObjectRef, punch),
2191 method(ObjectRef, right_click),
2192 method(ObjectRef, get_wield_digging_properties),
2193 method(ObjectRef, damage_wielded_item),
2194 method(ObjectRef, add_to_inventory),
2195 method(ObjectRef, add_to_inventory_later),
2196 method(ObjectRef, set_hp),
2197 method(ObjectRef, get_hp),
2198 // LuaEntitySAO-only
2199 method(ObjectRef, setvelocity),
2200 method(ObjectRef, setacceleration),
2201 method(ObjectRef, getacceleration),
2202 method(ObjectRef, settexturemod),
2203 method(ObjectRef, setsprite),
2205 method(ObjectRef, get_player_name),
2206 method(ObjectRef, inventory_set_list),
2207 method(ObjectRef, inventory_get_list),
2208 method(ObjectRef, get_wielded_itemstring),
2209 method(ObjectRef, get_wielded_item),
2213 // Creates a new anonymous reference if id=0
2214 static void objectref_get_or_create(lua_State *L,
2215 ServerActiveObject *cobj)
2217 if(cobj->getId() == 0){
2218 ObjectRef::create(L, cobj);
2220 objectref_get(L, cobj->getId());
2231 ServerEnvironment *m_env;
2233 static const char className[];
2234 static const luaL_reg methods[];
2236 static EnvRef *checkobject(lua_State *L, int narg)
2238 luaL_checktype(L, narg, LUA_TUSERDATA);
2239 void *ud = luaL_checkudata(L, narg, className);
2240 if(!ud) luaL_typerror(L, narg, className);
2241 return *(EnvRef**)ud; // unbox pointer
2244 // Exported functions
2246 // EnvRef:add_node(pos, node)
2247 // pos = {x=num, y=num, z=num}
2248 static int l_add_node(lua_State *L)
2250 //infostream<<"EnvRef::l_add_node()"<<std::endl;
2251 EnvRef *o = checkobject(L, 1);
2252 ServerEnvironment *env = o->m_env;
2253 if(env == NULL) return 0;
2255 v3s16 pos = readpos(L, 2);
2257 MapNode n = readnode(L, 3, env->getGameDef()->ndef());
2259 bool succeeded = env->getMap().addNodeWithEvent(pos, n);
2260 lua_pushboolean(L, succeeded);
2264 // EnvRef:remove_node(pos)
2265 // pos = {x=num, y=num, z=num}
2266 static int l_remove_node(lua_State *L)
2268 //infostream<<"EnvRef::l_remove_node()"<<std::endl;
2269 EnvRef *o = checkobject(L, 1);
2270 ServerEnvironment *env = o->m_env;
2271 if(env == NULL) return 0;
2273 v3s16 pos = readpos(L, 2);
2275 bool succeeded = env->getMap().removeNodeWithEvent(pos);
2276 lua_pushboolean(L, succeeded);
2280 // EnvRef:get_node(pos)
2281 // pos = {x=num, y=num, z=num}
2282 static int l_get_node(lua_State *L)
2284 //infostream<<"EnvRef::l_get_node()"<<std::endl;
2285 EnvRef *o = checkobject(L, 1);
2286 ServerEnvironment *env = o->m_env;
2287 if(env == NULL) return 0;
2289 v3s16 pos = readpos(L, 2);
2291 MapNode n = env->getMap().getNodeNoEx(pos);
2293 pushnode(L, n, env->getGameDef()->ndef());
2297 // EnvRef:add_luaentity(pos, entityname)
2298 // pos = {x=num, y=num, z=num}
2299 static int l_add_luaentity(lua_State *L)
2301 //infostream<<"EnvRef::l_add_luaentity()"<<std::endl;
2302 EnvRef *o = checkobject(L, 1);
2303 ServerEnvironment *env = o->m_env;
2304 if(env == NULL) return 0;
2306 v3f pos = readFloatPos(L, 2);
2308 const char *name = lua_tostring(L, 3);
2310 ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
2311 env->addActiveObject(obj);
2315 // EnvRef:add_item(pos, inventorystring)
2316 // pos = {x=num, y=num, z=num}
2317 static int l_add_item(lua_State *L)
2319 infostream<<"EnvRef::l_add_item()"<<std::endl;
2320 EnvRef *o = checkobject(L, 1);
2321 ServerEnvironment *env = o->m_env;
2322 if(env == NULL) return 0;
2324 v3f pos = readFloatPos(L, 2);
2326 const char *inventorystring = lua_tostring(L, 3);
2328 ServerActiveObject *obj = new ItemSAO(env, pos, inventorystring);
2329 env->addActiveObject(obj);
2333 // EnvRef:add_rat(pos)
2334 // pos = {x=num, y=num, z=num}
2335 static int l_add_rat(lua_State *L)
2337 infostream<<"EnvRef::l_add_rat()"<<std::endl;
2338 EnvRef *o = checkobject(L, 1);
2339 ServerEnvironment *env = o->m_env;
2340 if(env == NULL) return 0;
2342 v3f pos = readFloatPos(L, 2);
2344 ServerActiveObject *obj = new RatSAO(env, pos);
2345 env->addActiveObject(obj);
2349 // EnvRef:add_firefly(pos)
2350 // pos = {x=num, y=num, z=num}
2351 static int l_add_firefly(lua_State *L)
2353 infostream<<"EnvRef::l_add_firefly()"<<std::endl;
2354 EnvRef *o = checkobject(L, 1);
2355 ServerEnvironment *env = o->m_env;
2356 if(env == NULL) return 0;
2358 v3f pos = readFloatPos(L, 2);
2360 ServerActiveObject *obj = new FireflySAO(env, pos);
2361 env->addActiveObject(obj);
2365 // EnvRef:get_meta(pos)
2366 static int l_get_meta(lua_State *L)
2368 //infostream<<"EnvRef::l_get_meta()"<<std::endl;
2369 EnvRef *o = checkobject(L, 1);
2370 ServerEnvironment *env = o->m_env;
2371 if(env == NULL) return 0;
2373 v3s16 p = readpos(L, 2);
2374 NodeMetaRef::create(L, p, env);
2378 // EnvRef:get_player_by_name(name)
2379 static int l_get_player_by_name(lua_State *L)
2381 EnvRef *o = checkobject(L, 1);
2382 ServerEnvironment *env = o->m_env;
2383 if(env == NULL) return 0;
2385 const char *name = lua_tostring(L, 2);
2386 ServerRemotePlayer *player =
2387 static_cast<ServerRemotePlayer*>(env->getPlayer(name));
2392 // Put player on stack
2393 objectref_get_or_create(L, player);
2397 static int gc_object(lua_State *L) {
2398 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
2404 EnvRef(ServerEnvironment *env):
2407 infostream<<"EnvRef created"<<std::endl;
2412 infostream<<"EnvRef destructing"<<std::endl;
2415 // Creates an EnvRef and leaves it on top of stack
2416 // Not callable from Lua; all references are created on the C side.
2417 static void create(lua_State *L, ServerEnvironment *env)
2419 EnvRef *o = new EnvRef(env);
2420 //infostream<<"EnvRef::create: o="<<o<<std::endl;
2421 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
2422 luaL_getmetatable(L, className);
2423 lua_setmetatable(L, -2);
2426 static void set_null(lua_State *L)
2428 EnvRef *o = checkobject(L, -1);
2432 static void Register(lua_State *L)
2435 int methodtable = lua_gettop(L);
2436 luaL_newmetatable(L, className);
2437 int metatable = lua_gettop(L);
2439 lua_pushliteral(L, "__metatable");
2440 lua_pushvalue(L, methodtable);
2441 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
2443 lua_pushliteral(L, "__index");
2444 lua_pushvalue(L, methodtable);
2445 lua_settable(L, metatable);
2447 lua_pushliteral(L, "__gc");
2448 lua_pushcfunction(L, gc_object);
2449 lua_settable(L, metatable);
2451 lua_pop(L, 1); // drop metatable
2453 luaL_openlib(L, 0, methods, 0); // fill methodtable
2454 lua_pop(L, 1); // drop methodtable
2456 // Cannot be created from Lua
2457 //lua_register(L, className, create_object);
2460 const char EnvRef::className[] = "EnvRef";
2461 const luaL_reg EnvRef::methods[] = {
2462 method(EnvRef, add_node),
2463 method(EnvRef, remove_node),
2464 method(EnvRef, get_node),
2465 method(EnvRef, add_luaentity),
2466 method(EnvRef, add_item),
2467 method(EnvRef, add_rat),
2468 method(EnvRef, add_firefly),
2469 method(EnvRef, get_meta),
2470 method(EnvRef, get_player_by_name),
2475 Main export function
2478 void scriptapi_export(lua_State *L, Server *server)
2481 assert(lua_checkstack(L, 20));
2482 infostream<<"scriptapi_export"<<std::endl;
2483 StackUnroller stack_unroller(L);
2485 // Store server as light userdata in registry
2486 lua_pushlightuserdata(L, server);
2487 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
2489 // Store nil as minetest_nodedef_defaults in registry
2491 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_nodedef_default");
2493 // Register global functions in table minetest
2495 luaL_register(L, NULL, minetest_f);
2496 lua_setglobal(L, "minetest");
2498 // Get the main minetest table
2499 lua_getglobal(L, "minetest");
2501 // Add tables to minetest
2504 lua_setfield(L, -2, "registered_nodes");
2506 lua_setfield(L, -2, "registered_entities");
2508 lua_setfield(L, -2, "registered_craftitems");
2510 lua_setfield(L, -2, "registered_abms");
2513 lua_setfield(L, -2, "object_refs");
2515 lua_setfield(L, -2, "luaentities");
2517 // Create entity prototype
2518 luaL_newmetatable(L, "minetest.entity");
2519 // metatable.__index = metatable
2520 lua_pushvalue(L, -1); // Duplicate metatable
2521 lua_setfield(L, -2, "__index");
2522 // Put functions in metatable
2523 luaL_register(L, NULL, minetest_entity_m);
2524 // Put other stuff in metatable
2526 // Register reference wrappers
2527 NodeMetaRef::Register(L);
2528 EnvRef::Register(L);
2529 ObjectRef::Register(L);
2532 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
2533 const std::string &modname)
2535 ModNameStorer modnamestorer(L, modname);
2537 bool success = false;
2540 success = script_load(L, scriptpath.c_str());
2543 errorstream<<"Error loading mod \""<<modname
2544 <<"\": "<<e.what()<<std::endl;
2550 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
2553 assert(lua_checkstack(L, 20));
2554 infostream<<"scriptapi_add_environment"<<std::endl;
2555 StackUnroller stack_unroller(L);
2557 // Create EnvRef on stack
2558 EnvRef::create(L, env);
2559 int envref = lua_gettop(L);
2561 // minetest.env = envref
2562 lua_getglobal(L, "minetest");
2563 luaL_checktype(L, -1, LUA_TTABLE);
2564 lua_pushvalue(L, envref);
2565 lua_setfield(L, -2, "env");
2567 // Store environment as light userdata in registry
2568 lua_pushlightuserdata(L, env);
2569 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_env");
2571 /* Add ActiveBlockModifiers to environment */
2573 // Get minetest.registered_abms
2574 lua_getglobal(L, "minetest");
2575 lua_getfield(L, -1, "registered_abms");
2576 luaL_checktype(L, -1, LUA_TTABLE);
2577 int registered_abms = lua_gettop(L);
2579 if(lua_istable(L, registered_abms)){
2580 int table = lua_gettop(L);
2582 while(lua_next(L, table) != 0){
2583 // key at index -2 and value at index -1
2584 int id = lua_tonumber(L, -2);
2585 int current_abm = lua_gettop(L);
2587 std::set<std::string> trigger_contents;
2588 lua_getfield(L, current_abm, "nodenames");
2589 if(lua_istable(L, -1)){
2590 int table = lua_gettop(L);
2592 while(lua_next(L, table) != 0){
2593 // key at index -2 and value at index -1
2594 luaL_checktype(L, -1, LUA_TSTRING);
2595 trigger_contents.insert(lua_tostring(L, -1));
2596 // removes value, keeps key for next iteration
2602 float trigger_interval = 10.0;
2603 getfloatfield(L, current_abm, "interval", trigger_interval);
2605 int trigger_chance = 50;
2606 getintfield(L, current_abm, "chance", trigger_chance);
2608 LuaABM *abm = new LuaABM(L, id, trigger_contents,
2609 trigger_interval, trigger_chance);
2611 env->addActiveBlockModifier(abm);
2613 // removes value, keeps key for next iteration
2621 // Dump stack top with the dump2 function
2622 static void dump2(lua_State *L, const char *name)
2624 // Dump object (debug)
2625 lua_getglobal(L, "dump2");
2626 luaL_checktype(L, -1, LUA_TFUNCTION);
2627 lua_pushvalue(L, -2); // Get previous stack top as first parameter
2628 lua_pushstring(L, name);
2629 if(lua_pcall(L, 2, 0, 0))
2630 script_error(L, "error: %s", lua_tostring(L, -1));
2638 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
2641 assert(lua_checkstack(L, 20));
2642 //infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
2643 StackUnroller stack_unroller(L);
2645 // Create object on stack
2646 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
2647 int object = lua_gettop(L);
2649 // Get minetest.object_refs table
2650 lua_getglobal(L, "minetest");
2651 lua_getfield(L, -1, "object_refs");
2652 luaL_checktype(L, -1, LUA_TTABLE);
2653 int objectstable = lua_gettop(L);
2655 // object_refs[id] = object
2656 lua_pushnumber(L, cobj->getId()); // Push id
2657 lua_pushvalue(L, object); // Copy object to top of stack
2658 lua_settable(L, objectstable);
2661 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
2664 assert(lua_checkstack(L, 20));
2665 //infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
2666 StackUnroller stack_unroller(L);
2668 // Get minetest.object_refs table
2669 lua_getglobal(L, "minetest");
2670 lua_getfield(L, -1, "object_refs");
2671 luaL_checktype(L, -1, LUA_TTABLE);
2672 int objectstable = lua_gettop(L);
2674 // Get object_refs[id]
2675 lua_pushnumber(L, cobj->getId()); // Push id
2676 lua_gettable(L, objectstable);
2677 // Set object reference to NULL
2678 ObjectRef::set_null(L);
2679 lua_pop(L, 1); // pop object
2681 // Set object_refs[id] = nil
2682 lua_pushnumber(L, cobj->getId()); // Push id
2684 lua_settable(L, objectstable);
2687 bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
2688 const std::string &message)
2691 assert(lua_checkstack(L, 20));
2692 StackUnroller stack_unroller(L);
2694 // Get minetest.registered_on_chat_messages
2695 lua_getglobal(L, "minetest");
2696 lua_getfield(L, -1, "registered_on_chat_messages");
2697 luaL_checktype(L, -1, LUA_TTABLE);
2698 int table = lua_gettop(L);
2701 while(lua_next(L, table) != 0){
2702 // key at index -2 and value at index -1
2703 luaL_checktype(L, -1, LUA_TFUNCTION);
2705 lua_pushstring(L, name.c_str());
2706 lua_pushstring(L, message.c_str());
2707 if(lua_pcall(L, 2, 1, 0))
2708 script_error(L, "error: %s", lua_tostring(L, -1));
2709 bool ate = lua_toboolean(L, -1);
2713 // value removed, keep key for next iteration
2722 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
2725 assert(lua_checkstack(L, 20));
2726 StackUnroller stack_unroller(L);
2728 // Get minetest.registered_on_newplayers
2729 lua_getglobal(L, "minetest");
2730 lua_getfield(L, -1, "registered_on_newplayers");
2731 luaL_checktype(L, -1, LUA_TTABLE);
2732 int table = lua_gettop(L);
2735 while(lua_next(L, table) != 0){
2736 // key at index -2 and value at index -1
2737 luaL_checktype(L, -1, LUA_TFUNCTION);
2739 objectref_get_or_create(L, player);
2740 if(lua_pcall(L, 1, 0, 0))
2741 script_error(L, "error: %s", lua_tostring(L, -1));
2742 // value removed, keep key for next iteration
2745 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
2748 assert(lua_checkstack(L, 20));
2749 StackUnroller stack_unroller(L);
2751 bool positioning_handled_by_some = false;
2753 // Get minetest.registered_on_respawnplayers
2754 lua_getglobal(L, "minetest");
2755 lua_getfield(L, -1, "registered_on_respawnplayers");
2756 luaL_checktype(L, -1, LUA_TTABLE);
2757 int table = lua_gettop(L);
2760 while(lua_next(L, table) != 0){
2761 // key at index -2 and value at index -1
2762 luaL_checktype(L, -1, LUA_TFUNCTION);
2764 objectref_get_or_create(L, player);
2765 if(lua_pcall(L, 1, 1, 0))
2766 script_error(L, "error: %s", lua_tostring(L, -1));
2767 bool positioning_handled = lua_toboolean(L, -1);
2769 if(positioning_handled)
2770 positioning_handled_by_some = true;
2771 // value removed, keep key for next iteration
2773 return positioning_handled_by_some;
2776 void scriptapi_get_creative_inventory(lua_State *L, ServerRemotePlayer *player)
2778 lua_getglobal(L, "minetest");
2779 lua_getfield(L, -1, "creative_inventory");
2780 luaL_checktype(L, -1, LUA_TTABLE);
2781 inventory_set_list_from_lua(&player->inventory, "main", L, -1,
2782 player->getEnv()->getGameDef(), PLAYER_INVENTORY_SIZE);
2789 static void pushPointedThing(lua_State *L, const PointedThing& pointed)
2792 if(pointed.type == POINTEDTHING_NODE)
2794 lua_pushstring(L, "node");
2795 lua_setfield(L, -2, "type");
2796 pushpos(L, pointed.node_undersurface);
2797 lua_setfield(L, -2, "under");
2798 pushpos(L, pointed.node_abovesurface);
2799 lua_setfield(L, -2, "above");
2801 else if(pointed.type == POINTEDTHING_OBJECT)
2803 lua_pushstring(L, "object");
2804 lua_setfield(L, -2, "type");
2805 objectref_get(L, pointed.object_id);
2806 lua_setfield(L, -2, "ref");
2810 lua_pushstring(L, "nothing");
2811 lua_setfield(L, -2, "type");
2815 void scriptapi_add_craftitem(lua_State *L, const char *name)
2817 StackUnroller stack_unroller(L);
2818 assert(lua_gettop(L) > 0);
2820 // Set minetest.registered_craftitems[name] = table on top of stack
2821 lua_getglobal(L, "minetest");
2822 lua_getfield(L, -1, "registered_craftitems");
2823 luaL_checktype(L, -1, LUA_TTABLE);
2824 lua_pushvalue(L, -3); // push another reference to the table to be registered
2825 lua_setfield(L, -2, name); // set minetest.registered_craftitems[name]
2828 static bool get_craftitem_callback(lua_State *L, const char *name,
2829 const char *callbackname)
2831 // Get minetest.registered_craftitems[name][callbackname]
2832 // If that is nil or on error, return false and stack is unchanged
2833 // If that is a function, returns true and pushes the
2834 // function onto the stack
2836 lua_getglobal(L, "minetest");
2837 lua_getfield(L, -1, "registered_craftitems");
2839 luaL_checktype(L, -1, LUA_TTABLE);
2840 lua_getfield(L, -1, name);
2842 // Should be a table
2843 if(lua_type(L, -1) != LUA_TTABLE)
2845 errorstream<<"CraftItem name \""<<name<<"\" not defined"<<std::endl;
2849 lua_getfield(L, -1, callbackname);
2851 // Should be a function or nil
2852 if(lua_type(L, -1) == LUA_TFUNCTION)
2856 else if(lua_isnil(L, -1))
2863 errorstream<<"CraftItem name \""<<name<<"\" callback \""
2864 <<callbackname<<" is not a function"<<std::endl;
2870 bool scriptapi_craftitem_on_drop(lua_State *L, const char *name,
2871 ServerActiveObject *dropper, v3f pos,
2872 bool &callback_exists)
2875 assert(lua_checkstack(L, 20));
2876 //infostream<<"scriptapi_craftitem_on_drop"<<std::endl;
2877 StackUnroller stack_unroller(L);
2879 bool result = false;
2880 callback_exists = get_craftitem_callback(L, name, "on_drop");
2884 lua_pushstring(L, name);
2885 objectref_get_or_create(L, dropper);
2886 pushFloatPos(L, pos);
2887 if(lua_pcall(L, 3, 1, 0))
2888 script_error(L, "error: %s", lua_tostring(L, -1));
2889 result = lua_toboolean(L, -1);
2894 bool scriptapi_craftitem_on_place_on_ground(lua_State *L, const char *name,
2895 ServerActiveObject *placer, v3f pos,
2896 bool &callback_exists)
2899 assert(lua_checkstack(L, 20));
2900 //infostream<<"scriptapi_craftitem_on_place_on_ground"<<std::endl;
2901 StackUnroller stack_unroller(L);
2903 bool result = false;
2904 callback_exists = get_craftitem_callback(L, name, "on_place_on_ground");
2908 lua_pushstring(L, name);
2909 objectref_get_or_create(L, placer);
2910 pushFloatPos(L, pos);
2911 if(lua_pcall(L, 3, 1, 0))
2912 script_error(L, "error: %s", lua_tostring(L, -1));
2913 result = lua_toboolean(L, -1);
2918 bool scriptapi_craftitem_on_use(lua_State *L, const char *name,
2919 ServerActiveObject *user, const PointedThing& pointed,
2920 bool &callback_exists)
2923 assert(lua_checkstack(L, 20));
2924 //infostream<<"scriptapi_craftitem_on_use"<<std::endl;
2925 StackUnroller stack_unroller(L);
2927 bool result = false;
2928 callback_exists = get_craftitem_callback(L, name, "on_use");
2932 lua_pushstring(L, name);
2933 objectref_get_or_create(L, user);
2934 pushPointedThing(L, pointed);
2935 if(lua_pcall(L, 3, 1, 0))
2936 script_error(L, "error: %s", lua_tostring(L, -1));
2937 result = lua_toboolean(L, -1);
2946 void scriptapi_environment_step(lua_State *L, float dtime)
2949 assert(lua_checkstack(L, 20));
2950 //infostream<<"scriptapi_environment_step"<<std::endl;
2951 StackUnroller stack_unroller(L);
2953 // Get minetest.registered_globalsteps
2954 lua_getglobal(L, "minetest");
2955 lua_getfield(L, -1, "registered_globalsteps");
2956 luaL_checktype(L, -1, LUA_TTABLE);
2957 int table = lua_gettop(L);
2960 while(lua_next(L, table) != 0){
2961 // key at index -2 and value at index -1
2962 luaL_checktype(L, -1, LUA_TFUNCTION);
2964 lua_pushnumber(L, dtime);
2965 if(lua_pcall(L, 1, 0, 0))
2966 script_error(L, "error: %s", lua_tostring(L, -1));
2967 // value removed, keep key for next iteration
2971 void scriptapi_environment_on_placenode(lua_State *L, v3s16 p, MapNode newnode,
2972 ServerActiveObject *placer)
2975 assert(lua_checkstack(L, 20));
2976 //infostream<<"scriptapi_environment_on_placenode"<<std::endl;
2977 StackUnroller stack_unroller(L);
2979 // Get server from registry
2980 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
2981 Server *server = (Server*)lua_touserdata(L, -1);
2982 // And get the writable node definition manager from the server
2983 IWritableNodeDefManager *ndef =
2984 server->getWritableNodeDefManager();
2986 // Get minetest.registered_on_placenodes
2987 lua_getglobal(L, "minetest");
2988 lua_getfield(L, -1, "registered_on_placenodes");
2989 luaL_checktype(L, -1, LUA_TTABLE);
2990 int table = lua_gettop(L);
2993 while(lua_next(L, table) != 0){
2994 // key at index -2 and value at index -1
2995 luaL_checktype(L, -1, LUA_TFUNCTION);
2998 pushnode(L, newnode, ndef);
2999 objectref_get_or_create(L, placer);
3000 if(lua_pcall(L, 3, 0, 0))
3001 script_error(L, "error: %s", lua_tostring(L, -1));
3002 // value removed, keep key for next iteration
3006 void scriptapi_environment_on_dignode(lua_State *L, v3s16 p, MapNode oldnode,
3007 ServerActiveObject *digger)
3010 assert(lua_checkstack(L, 20));
3011 //infostream<<"scriptapi_environment_on_dignode"<<std::endl;
3012 StackUnroller stack_unroller(L);
3014 // Get server from registry
3015 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
3016 Server *server = (Server*)lua_touserdata(L, -1);
3017 // And get the writable node definition manager from the server
3018 IWritableNodeDefManager *ndef =
3019 server->getWritableNodeDefManager();
3021 // Get minetest.registered_on_dignodes
3022 lua_getglobal(L, "minetest");
3023 lua_getfield(L, -1, "registered_on_dignodes");
3024 luaL_checktype(L, -1, LUA_TTABLE);
3025 int table = lua_gettop(L);
3028 while(lua_next(L, table) != 0){
3029 // key at index -2 and value at index -1
3030 luaL_checktype(L, -1, LUA_TFUNCTION);
3033 pushnode(L, oldnode, ndef);
3034 objectref_get_or_create(L, digger);
3035 if(lua_pcall(L, 3, 0, 0))
3036 script_error(L, "error: %s", lua_tostring(L, -1));
3037 // value removed, keep key for next iteration
3041 void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode node,
3042 ServerActiveObject *puncher)
3045 assert(lua_checkstack(L, 20));
3046 //infostream<<"scriptapi_environment_on_punchnode"<<std::endl;
3047 StackUnroller stack_unroller(L);
3049 // Get server from registry
3050 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
3051 Server *server = (Server*)lua_touserdata(L, -1);
3052 // And get the writable node definition manager from the server
3053 IWritableNodeDefManager *ndef =
3054 server->getWritableNodeDefManager();
3056 // Get minetest.registered_on_punchnodes
3057 lua_getglobal(L, "minetest");
3058 lua_getfield(L, -1, "registered_on_punchnodes");
3059 luaL_checktype(L, -1, LUA_TTABLE);
3060 int table = lua_gettop(L);
3063 while(lua_next(L, table) != 0){
3064 // key at index -2 and value at index -1
3065 luaL_checktype(L, -1, LUA_TFUNCTION);
3068 pushnode(L, node, ndef);
3069 objectref_get_or_create(L, puncher);
3070 if(lua_pcall(L, 3, 0, 0))
3071 script_error(L, "error: %s", lua_tostring(L, -1));
3072 // value removed, keep key for next iteration
3076 void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp)
3079 assert(lua_checkstack(L, 20));
3080 //infostream<<"scriptapi_environment_on_generated"<<std::endl;
3081 StackUnroller stack_unroller(L);
3083 // Get minetest.registered_on_generateds
3084 lua_getglobal(L, "minetest");
3085 lua_getfield(L, -1, "registered_on_generateds");
3086 luaL_checktype(L, -1, LUA_TTABLE);
3087 int table = lua_gettop(L);
3090 while(lua_next(L, table) != 0){
3091 // key at index -2 and value at index -1
3092 luaL_checktype(L, -1, LUA_TFUNCTION);
3096 if(lua_pcall(L, 2, 0, 0))
3097 script_error(L, "error: %s", lua_tostring(L, -1));
3098 // value removed, keep key for next iteration
3106 bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
3107 const std::string &staticdata)
3110 assert(lua_checkstack(L, 20));
3111 infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
3112 <<name<<"\""<<std::endl;
3113 StackUnroller stack_unroller(L);
3115 // Get minetest.registered_entities[name]
3116 lua_getglobal(L, "minetest");
3117 lua_getfield(L, -1, "registered_entities");
3118 luaL_checktype(L, -1, LUA_TTABLE);
3119 lua_pushstring(L, name);
3120 lua_gettable(L, -2);
3121 // Should be a table, which we will use as a prototype
3122 //luaL_checktype(L, -1, LUA_TTABLE);
3123 if(lua_type(L, -1) != LUA_TTABLE){
3124 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
3127 int prototype_table = lua_gettop(L);
3128 //dump2(L, "prototype_table");
3130 // Create entity object
3132 int object = lua_gettop(L);
3134 // Set object metatable
3135 lua_pushvalue(L, prototype_table);
3136 lua_setmetatable(L, -2);
3138 // Add object reference
3139 // This should be userdata with metatable ObjectRef
3140 objectref_get(L, id);
3141 luaL_checktype(L, -1, LUA_TUSERDATA);
3142 if(!luaL_checkudata(L, -1, "ObjectRef"))
3143 luaL_typerror(L, -1, "ObjectRef");
3144 lua_setfield(L, -2, "object");
3146 // minetest.luaentities[id] = object
3147 lua_getglobal(L, "minetest");
3148 lua_getfield(L, -1, "luaentities");
3149 luaL_checktype(L, -1, LUA_TTABLE);
3150 lua_pushnumber(L, id); // Push id
3151 lua_pushvalue(L, object); // Copy object to top of stack
3152 lua_settable(L, -3);
3154 // Get on_activate function
3155 lua_pushvalue(L, object);
3156 lua_getfield(L, -1, "on_activate");
3157 if(!lua_isnil(L, -1)){
3158 luaL_checktype(L, -1, LUA_TFUNCTION);
3159 lua_pushvalue(L, object); // self
3160 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
3161 // Call with 2 arguments, 0 results
3162 if(lua_pcall(L, 2, 0, 0))
3163 script_error(L, "error running function %s:on_activate: %s\n",
3164 name, lua_tostring(L, -1));
3170 void scriptapi_luaentity_rm(lua_State *L, u16 id)
3173 assert(lua_checkstack(L, 20));
3174 infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
3176 // Get minetest.luaentities table
3177 lua_getglobal(L, "minetest");
3178 lua_getfield(L, -1, "luaentities");
3179 luaL_checktype(L, -1, LUA_TTABLE);
3180 int objectstable = lua_gettop(L);
3182 // Set luaentities[id] = nil
3183 lua_pushnumber(L, id); // Push id
3185 lua_settable(L, objectstable);
3187 lua_pop(L, 2); // pop luaentities, minetest
3190 std::string scriptapi_luaentity_get_staticdata(lua_State *L, u16 id)
3193 assert(lua_checkstack(L, 20));
3194 infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
3195 StackUnroller stack_unroller(L);
3197 // Get minetest.luaentities[id]
3198 luaentity_get(L, id);
3199 int object = lua_gettop(L);
3201 // Get get_staticdata function
3202 lua_pushvalue(L, object);
3203 lua_getfield(L, -1, "get_staticdata");
3204 if(lua_isnil(L, -1))
3207 luaL_checktype(L, -1, LUA_TFUNCTION);
3208 lua_pushvalue(L, object); // self
3209 // Call with 1 arguments, 1 results
3210 if(lua_pcall(L, 1, 1, 0))
3211 script_error(L, "error running function get_staticdata: %s\n",
3212 lua_tostring(L, -1));
3215 const char *s = lua_tolstring(L, -1, &len);
3216 return std::string(s, len);
3219 void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
3220 LuaEntityProperties *prop)
3223 assert(lua_checkstack(L, 20));
3224 infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
3225 StackUnroller stack_unroller(L);
3227 // Get minetest.luaentities[id]
3228 luaentity_get(L, id);
3229 //int object = lua_gettop(L);
3233 getboolfield(L, -1, "physical", prop->physical);
3235 getfloatfield(L, -1, "weight", prop->weight);
3237 lua_getfield(L, -1, "collisionbox");
3238 if(lua_istable(L, -1))
3239 prop->collisionbox = read_aabbox3df32(L, -1, 1.0);
3242 getstringfield(L, -1, "visual", prop->visual);
3244 lua_getfield(L, -1, "visual_size");
3245 if(lua_istable(L, -1))
3246 prop->visual_size = read_v2f(L, -1);
3249 lua_getfield(L, -1, "textures");
3250 if(lua_istable(L, -1)){
3251 prop->textures.clear();
3252 int table = lua_gettop(L);
3254 while(lua_next(L, table) != 0){
3255 // key at index -2 and value at index -1
3256 if(lua_isstring(L, -1))
3257 prop->textures.push_back(lua_tostring(L, -1));
3259 prop->textures.push_back("");
3260 // removes value, keeps key for next iteration
3266 lua_getfield(L, -1, "spritediv");
3267 if(lua_istable(L, -1))
3268 prop->spritediv = read_v2s16(L, -1);
3271 lua_getfield(L, -1, "initial_sprite_basepos");
3272 if(lua_istable(L, -1))
3273 prop->initial_sprite_basepos = read_v2s16(L, -1);
3277 void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
3280 assert(lua_checkstack(L, 20));
3281 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
3282 StackUnroller stack_unroller(L);
3284 // Get minetest.luaentities[id]
3285 luaentity_get(L, id);
3286 int object = lua_gettop(L);
3287 // State: object is at top of stack
3288 // Get step function
3289 lua_getfield(L, -1, "on_step");
3290 if(lua_isnil(L, -1))
3292 luaL_checktype(L, -1, LUA_TFUNCTION);
3293 lua_pushvalue(L, object); // self
3294 lua_pushnumber(L, dtime); // dtime
3295 // Call with 2 arguments, 0 results
3296 if(lua_pcall(L, 2, 0, 0))
3297 script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
3300 // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch)
3301 void scriptapi_luaentity_punch(lua_State *L, u16 id,
3302 ServerActiveObject *puncher, float time_from_last_punch)
3305 assert(lua_checkstack(L, 20));
3306 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
3307 StackUnroller stack_unroller(L);
3309 // Get minetest.luaentities[id]
3310 luaentity_get(L, id);
3311 int object = lua_gettop(L);
3312 // State: object is at top of stack
3314 lua_getfield(L, -1, "on_punch");
3315 if(lua_isnil(L, -1))
3317 luaL_checktype(L, -1, LUA_TFUNCTION);
3318 lua_pushvalue(L, object); // self
3319 objectref_get_or_create(L, puncher); // Clicker reference
3320 lua_pushnumber(L, time_from_last_punch);
3321 // Call with 2 arguments, 0 results
3322 if(lua_pcall(L, 3, 0, 0))
3323 script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
3326 // Calls entity:on_rightclick(ObjectRef clicker)
3327 void scriptapi_luaentity_rightclick(lua_State *L, u16 id,
3328 ServerActiveObject *clicker)
3331 assert(lua_checkstack(L, 20));
3332 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
3333 StackUnroller stack_unroller(L);
3335 // Get minetest.luaentities[id]
3336 luaentity_get(L, id);
3337 int object = lua_gettop(L);
3338 // State: object is at top of stack
3340 lua_getfield(L, -1, "on_rightclick");
3341 if(lua_isnil(L, -1))
3343 luaL_checktype(L, -1, LUA_TFUNCTION);
3344 lua_pushvalue(L, object); // self
3345 objectref_get_or_create(L, clicker); // Clicker reference
3346 // Call with 2 arguments, 0 results
3347 if(lua_pcall(L, 2, 0, 0))
3348 script_error(L, "error running function 'on_rightclick': %s\n", lua_tostring(L, -1));