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"
33 #include "serverobject.h"
36 #include "luaentity_common.h"
37 #include "content_sao.h" // For LuaEntitySAO
41 - Global environment step function
42 - Random node triggers
43 - Object visual client-side stuff
45 - Spritesheets and animation
46 - Named node types and dynamic id allocation
48 blockdef.has_metadata = true/false
49 - Stores an inventory and stuff in a Settings object
50 meta.inventory_add_list("main")
51 blockdef.on_inventory_modified
52 meta.set("owner", playername)
56 static void stackDump(lua_State *L, std::ostream &o)
59 int top = lua_gettop(L);
60 for (i = 1; i <= top; i++) { /* repeat for each level */
61 int t = lua_type(L, i);
64 case LUA_TSTRING: /* strings */
65 o<<"\""<<lua_tostring(L, i)<<"\"";
68 case LUA_TBOOLEAN: /* booleans */
69 o<<(lua_toboolean(L, i) ? "true" : "false");
72 case LUA_TNUMBER: /* numbers */ {
74 snprintf(buf, 10, "%g", lua_tonumber(L, i));
78 default: /* other values */
79 o<<lua_typename(L, t);
88 static void realitycheck(lua_State *L)
90 int top = lua_gettop(L);
92 dstream<<"Stack is over 30:"<<std::endl;
93 stackDump(L, dstream);
94 script_error(L, "Stack is over 30 (reality check)");
104 StackUnroller(lua_State *L):
108 m_original_top = lua_gettop(m_lua); // store stack height
112 lua_settop(m_lua, m_original_top); // restore stack height
116 v3f readFloatPos(lua_State *L, int index)
119 lua_pushvalue(L, index); // Push pos
120 luaL_checktype(L, -1, LUA_TTABLE);
121 lua_getfield(L, -1, "x");
122 pos.X = lua_tonumber(L, -1);
124 lua_getfield(L, -1, "y");
125 pos.Y = lua_tonumber(L, -1);
127 lua_getfield(L, -1, "z");
128 pos.Z = lua_tonumber(L, -1);
130 lua_pop(L, 1); // Pop pos
131 pos *= BS; // Scale to internal format
139 // Register new object prototype
140 // register_entity(name, prototype)
141 static int l_register_entity(lua_State *L)
143 const char *name = luaL_checkstring(L, 1);
144 luaL_checktype(L, 2, LUA_TTABLE);
145 infostream<<"register_entity: "<<name<<std::endl;
147 // Get minetest.registered_entities
148 lua_getglobal(L, "minetest");
149 lua_getfield(L, -1, "registered_entities");
150 luaL_checktype(L, -1, LUA_TTABLE);
151 int registered_entities = lua_gettop(L);
152 lua_pushvalue(L, 2); // Object = param 2 -> stack top
153 // registered_entities[name] = object
154 lua_setfield(L, registered_entities, name);
156 // Get registered object to top of stack
159 // Set __index to point to itself
160 lua_pushvalue(L, -1);
161 lua_setfield(L, -2, "__index");
163 // Set metatable.__index = metatable
164 luaL_getmetatable(L, "minetest.entity");
165 lua_pushvalue(L, -1); // duplicate metatable
166 lua_setfield(L, -2, "__index");
167 // Set object metatable
168 lua_setmetatable(L, -2);
170 return 0; /* number of results */
173 static const struct luaL_Reg minetest_f [] = {
174 {"register_entity", l_register_entity},
182 static const struct luaL_Reg minetest_entity_m [] = {
187 Getters for stuff in main tables
190 static void objectref_get(lua_State *L, u16 id)
192 // Get minetest.object_refs[i]
193 lua_getglobal(L, "minetest");
194 lua_getfield(L, -1, "object_refs");
195 luaL_checktype(L, -1, LUA_TTABLE);
196 lua_pushnumber(L, id);
198 lua_remove(L, -2); // object_refs
199 lua_remove(L, -2); // minetest
202 static void luaentity_get(lua_State *L, u16 id)
204 // Get minetest.luaentities[i]
205 lua_getglobal(L, "minetest");
206 lua_getfield(L, -1, "luaentities");
207 luaL_checktype(L, -1, LUA_TTABLE);
208 lua_pushnumber(L, id);
210 lua_remove(L, -2); // luaentities
211 lua_remove(L, -2); // minetest
217 #define method(class, name) {#name, class::l_##name}
222 ServerEnvironment *m_env;
224 static const char className[];
225 static const luaL_reg methods[];
227 static EnvRef *checkobject(lua_State *L, int narg)
229 luaL_checktype(L, narg, LUA_TUSERDATA);
230 void *ud = luaL_checkudata(L, narg, className);
231 if(!ud) luaL_typerror(L, narg, className);
232 return *(EnvRef**)ud; // unbox pointer
235 // Exported functions
237 // EnvRef:add_node(pos, content)
238 // pos = {x=num, y=num, z=num}
240 static int l_add_node(lua_State *L)
242 infostream<<"EnvRef::l_add_node()"<<std::endl;
243 EnvRef *o = checkobject(L, 1);
244 ServerEnvironment *env = o->m_env;
245 if(env == NULL) return 0;
248 lua_pushvalue(L, 2); // Push pos
249 luaL_checktype(L, -1, LUA_TTABLE);
250 lua_getfield(L, -1, "x");
251 pos.X = lua_tonumber(L, -1);
253 lua_getfield(L, -1, "y");
254 pos.Y = lua_tonumber(L, -1);
256 lua_getfield(L, -1, "z");
257 pos.Z = lua_tonumber(L, -1);
259 lua_pop(L, 1); // Pop pos
262 lua_pushvalue(L, 3); // Push content
263 content = lua_tonumber(L, -1);
264 lua_pop(L, 1); // Pop content
266 env->getMap().addNodeWithEvent(pos, MapNode(content));
270 static int gc_object(lua_State *L) {
271 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
277 EnvRef(ServerEnvironment *env):
280 infostream<<"EnvRef created"<<std::endl;
285 infostream<<"EnvRef destructing"<<std::endl;
288 // Creates an EnvRef and leaves it on top of stack
289 // Not callable from Lua; all references are created on the C side.
290 static void create(lua_State *L, ServerEnvironment *env)
292 EnvRef *o = new EnvRef(env);
293 //infostream<<"EnvRef::create: o="<<o<<std::endl;
294 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
295 luaL_getmetatable(L, className);
296 lua_setmetatable(L, -2);
299 static void set_null(lua_State *L)
301 EnvRef *o = checkobject(L, -1);
305 static void Register(lua_State *L)
308 int methodtable = lua_gettop(L);
309 luaL_newmetatable(L, className);
310 int metatable = lua_gettop(L);
312 lua_pushliteral(L, "__metatable");
313 lua_pushvalue(L, methodtable);
314 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
316 lua_pushliteral(L, "__index");
317 lua_pushvalue(L, methodtable);
318 lua_settable(L, metatable);
320 lua_pushliteral(L, "__gc");
321 lua_pushcfunction(L, gc_object);
322 lua_settable(L, metatable);
324 lua_pop(L, 1); // drop metatable
326 luaL_openlib(L, 0, methods, 0); // fill methodtable
327 lua_pop(L, 1); // drop methodtable
329 // Cannot be created from Lua
330 //lua_register(L, className, create_object);
333 const char EnvRef::className[] = "EnvRef";
334 const luaL_reg EnvRef::methods[] = {
335 method(EnvRef, add_node),
342 ServerActiveObject *m_object;
344 static const char className[];
345 static const luaL_reg methods[];
347 static ObjectRef *checkobject(lua_State *L, int narg)
349 luaL_checktype(L, narg, LUA_TUSERDATA);
350 void *ud = luaL_checkudata(L, narg, className);
351 if(!ud) luaL_typerror(L, narg, className);
352 return *(ObjectRef**)ud; // unbox pointer
355 static ServerActiveObject* getobject(ObjectRef *ref)
357 ServerActiveObject *co = ref->m_object;
361 static LuaEntitySAO* getluaobject(ObjectRef *ref)
363 ServerActiveObject *obj = getobject(ref);
366 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
368 return (LuaEntitySAO*)obj;
371 // Exported functions
374 static int gc_object(lua_State *L) {
375 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
376 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
382 static int l_remove(lua_State *L)
384 ObjectRef *ref = checkobject(L, 1);
385 ServerActiveObject *co = getobject(ref);
386 if(co == NULL) return 0;
387 infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
388 co->m_removed = true;
393 // returns: {x=num, y=num, z=num}
394 static int l_getpos(lua_State *L)
396 ObjectRef *ref = checkobject(L, 1);
397 ServerActiveObject *co = getobject(ref);
398 if(co == NULL) return 0;
399 v3f pos = co->getBasePosition() / BS;
401 lua_pushnumber(L, pos.X);
402 lua_setfield(L, -2, "x");
403 lua_pushnumber(L, pos.Y);
404 lua_setfield(L, -2, "y");
405 lua_pushnumber(L, pos.Z);
406 lua_setfield(L, -2, "z");
411 static int l_setpos(lua_State *L)
413 ObjectRef *ref = checkobject(L, 1);
414 //LuaEntitySAO *co = getluaobject(ref);
415 ServerActiveObject *co = getobject(ref);
416 if(co == NULL) return 0;
418 v3f pos = readFloatPos(L, 2);
424 // moveto(self, pos, continuous=false)
425 static int l_moveto(lua_State *L)
427 ObjectRef *ref = checkobject(L, 1);
428 //LuaEntitySAO *co = getluaobject(ref);
429 ServerActiveObject *co = getobject(ref);
430 if(co == NULL) return 0;
432 v3f pos = readFloatPos(L, 2);
434 bool continuous = lua_toboolean(L, 3);
436 co->moveTo(pos, continuous);
440 // add_to_inventory(self, itemstring)
441 // returns: true if item was added, false otherwise
442 static int l_add_to_inventory(lua_State *L)
444 ObjectRef *ref = checkobject(L, 1);
445 luaL_checkstring(L, 2);
446 ServerActiveObject *co = getobject(ref);
447 if(co == NULL) return 0;
449 const char *itemstring = lua_tostring(L, 2);
450 infostream<<"ObjectRef::l_add_to_inventory(): id="<<co->getId()
451 <<" itemstring=\""<<itemstring<<"\""<<std::endl;
453 std::istringstream is(itemstring, std::ios::binary);
454 InventoryItem *item = InventoryItem::deSerialize(is);
455 bool fits = co->addToInventory(item);
457 lua_pushboolean(L, fits);
462 ObjectRef(ServerActiveObject *object):
465 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
471 infostream<<"ObjectRef destructing for id="
472 <<m_object->getId()<<std::endl;
474 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
477 // Creates an ObjectRef and leaves it on top of stack
478 // Not callable from Lua; all references are created on the C side.
479 static void create(lua_State *L, ServerActiveObject *object)
481 ObjectRef *o = new ObjectRef(object);
482 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
483 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
484 luaL_getmetatable(L, className);
485 lua_setmetatable(L, -2);
488 static void set_null(lua_State *L)
490 ObjectRef *o = checkobject(L, -1);
494 static void Register(lua_State *L)
497 int methodtable = lua_gettop(L);
498 luaL_newmetatable(L, className);
499 int metatable = lua_gettop(L);
501 lua_pushliteral(L, "__metatable");
502 lua_pushvalue(L, methodtable);
503 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
505 lua_pushliteral(L, "__index");
506 lua_pushvalue(L, methodtable);
507 lua_settable(L, metatable);
509 lua_pushliteral(L, "__gc");
510 lua_pushcfunction(L, gc_object);
511 lua_settable(L, metatable);
513 lua_pop(L, 1); // drop metatable
515 luaL_openlib(L, 0, methods, 0); // fill methodtable
516 lua_pop(L, 1); // drop methodtable
518 // Cannot be created from Lua
519 //lua_register(L, className, create_object);
522 const char ObjectRef::className[] = "ObjectRef";
523 const luaL_reg ObjectRef::methods[] = {
524 method(ObjectRef, remove),
525 method(ObjectRef, getpos),
526 method(ObjectRef, setpos),
527 method(ObjectRef, moveto),
528 method(ObjectRef, add_to_inventory),
532 // Creates a new anonymous reference if id=0
533 static void objectref_get_or_create(lua_State *L, ServerActiveObject *cobj)
535 if(cobj->getId() == 0){
536 ObjectRef::create(L, cobj);
538 objectref_get(L, cobj->getId());
546 void scriptapi_export(lua_State *L, Server *server)
549 assert(lua_checkstack(L, 20));
550 infostream<<"scriptapi_export"<<std::endl;
551 StackUnroller stack_unroller(L);
553 // Register global functions in table minetest
555 luaL_register(L, NULL, minetest_f);
556 lua_setglobal(L, "minetest");
558 // Get the main minetest table
559 lua_getglobal(L, "minetest");
561 // Add registered_entities table in minetest
563 lua_setfield(L, -2, "registered_entities");
565 // Add object_refs table in minetest
567 lua_setfield(L, -2, "object_refs");
569 // Add luaentities table in minetest
571 lua_setfield(L, -2, "luaentities");
573 // Create entity prototype
574 luaL_newmetatable(L, "minetest.entity");
575 // metatable.__index = metatable
576 lua_pushvalue(L, -1); // Duplicate metatable
577 lua_setfield(L, -2, "__index");
578 // Put functions in metatable
579 luaL_register(L, NULL, minetest_entity_m);
580 // Put other stuff in metatable
582 // Environment C reference
585 // Object C reference
586 ObjectRef::Register(L);
589 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
592 assert(lua_checkstack(L, 20));
593 infostream<<"scriptapi_add_environment"<<std::endl;
594 StackUnroller stack_unroller(L);
596 // Create EnvRef on stack
597 EnvRef::create(L, env);
598 int envref = lua_gettop(L);
600 // minetest.env = envref
601 lua_getglobal(L, "minetest");
602 luaL_checktype(L, -1, LUA_TTABLE);
603 lua_pushvalue(L, envref);
604 lua_setfield(L, -2, "env");
608 // Dump stack top with the dump2 function
609 static void dump2(lua_State *L, const char *name)
611 // Dump object (debug)
612 lua_getglobal(L, "dump2");
613 luaL_checktype(L, -1, LUA_TFUNCTION);
614 lua_pushvalue(L, -2); // Get previous stack top as first parameter
615 lua_pushstring(L, name);
616 if(lua_pcall(L, 2, 0, 0))
617 script_error(L, "error: %s\n", lua_tostring(L, -1));
625 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
628 assert(lua_checkstack(L, 20));
629 infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
630 StackUnroller stack_unroller(L);
632 // Create object on stack
633 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
634 int object = lua_gettop(L);
636 // Get minetest.object_refs table
637 lua_getglobal(L, "minetest");
638 lua_getfield(L, -1, "object_refs");
639 luaL_checktype(L, -1, LUA_TTABLE);
640 int objectstable = lua_gettop(L);
642 // object_refs[id] = object
643 lua_pushnumber(L, cobj->getId()); // Push id
644 lua_pushvalue(L, object); // Copy object to top of stack
645 lua_settable(L, objectstable);
648 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
651 assert(lua_checkstack(L, 20));
652 infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
653 StackUnroller stack_unroller(L);
655 // Get minetest.object_refs table
656 lua_getglobal(L, "minetest");
657 lua_getfield(L, -1, "object_refs");
658 luaL_checktype(L, -1, LUA_TTABLE);
659 int objectstable = lua_gettop(L);
661 // Get object_refs[id]
662 lua_pushnumber(L, cobj->getId()); // Push id
663 lua_gettable(L, objectstable);
664 // Set object reference to NULL
665 ObjectRef::set_null(L);
666 lua_pop(L, 1); // pop object
668 // Set object_refs[id] = nil
669 lua_pushnumber(L, cobj->getId()); // Push id
671 lua_settable(L, objectstable);
678 void scriptapi_environment_step(lua_State *L, float dtime)
681 assert(lua_checkstack(L, 20));
682 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
683 StackUnroller stack_unroller(L);
685 lua_getglobal(L, "on_step");
686 if(lua_type(L, -1) != LUA_TFUNCTION)
687 return; // If no on_step function exist, do nothing
688 lua_pushnumber(L, dtime);
689 if(lua_pcall(L, 1, 0, 0))
690 script_error(L, "error: %s\n", lua_tostring(L, -1));
697 void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
698 const char *init_state)
701 assert(lua_checkstack(L, 20));
702 infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
703 <<name<<"\""<<std::endl;
704 StackUnroller stack_unroller(L);
706 // Create object as a dummy string (TODO: Create properly)
708 // Get minetest.registered_entities[name]
709 lua_getglobal(L, "minetest");
710 lua_getfield(L, -1, "registered_entities");
711 luaL_checktype(L, -1, LUA_TTABLE);
712 lua_pushstring(L, name);
714 // Should be a table, which we will use as a prototype
715 luaL_checktype(L, -1, LUA_TTABLE);
716 int prototype_table = lua_gettop(L);
717 //dump2(L, "prototype_table");
719 // Create entity object
721 int object = lua_gettop(L);
723 // Set object metatable
724 lua_pushvalue(L, prototype_table);
725 lua_setmetatable(L, -2);
727 // Add object reference
728 // This should be userdata with metatable ObjectRef
729 objectref_get(L, id);
730 luaL_checktype(L, -1, LUA_TUSERDATA);
731 if(!luaL_checkudata(L, -1, "ObjectRef"))
732 luaL_typerror(L, -1, "ObjectRef");
733 lua_setfield(L, -2, "object");
735 // minetest.luaentities[id] = object
736 lua_getglobal(L, "minetest");
737 lua_getfield(L, -1, "luaentities");
738 luaL_checktype(L, -1, LUA_TTABLE);
739 lua_pushnumber(L, id); // Push id
740 lua_pushvalue(L, object); // Copy object to top of stack
743 // This callback doesn't really make sense
744 /*// Get on_activate function
745 lua_pushvalue(L, object);
746 lua_getfield(L, -1, "on_activate");
747 luaL_checktype(L, -1, LUA_TFUNCTION);
748 lua_pushvalue(L, object); // self
749 // Call with 1 arguments, 0 results
750 if(lua_pcall(L, 1, 0, 0))
751 script_error(L, "error running function %s:on_activate: %s\n",
752 name, lua_tostring(L, -1));*/
755 void scriptapi_luaentity_rm(lua_State *L, u16 id)
758 assert(lua_checkstack(L, 20));
759 infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
761 // Get minetest.luaentities table
762 lua_getglobal(L, "minetest");
763 lua_getfield(L, -1, "luaentities");
764 luaL_checktype(L, -1, LUA_TTABLE);
765 int objectstable = lua_gettop(L);
767 // Set luaentities[id] = nil
768 lua_pushnumber(L, id); // Push id
770 lua_settable(L, objectstable);
772 lua_pop(L, 2); // pop luaentities, minetest
775 std::string scriptapi_luaentity_get_state(lua_State *L, u16 id)
778 assert(lua_checkstack(L, 20));
779 infostream<<"scriptapi_luaentity_get_state: id="<<id<<std::endl;
784 void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
785 LuaEntityProperties *prop)
788 assert(lua_checkstack(L, 20));
789 infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
790 StackUnroller stack_unroller(L);
792 // Get minetest.luaentities[id]
793 luaentity_get(L, id);
794 //int object = lua_gettop(L);
796 lua_getfield(L, -1, "physical");
797 if(lua_isboolean(L, -1))
798 prop->physical = lua_toboolean(L, -1);
801 lua_getfield(L, -1, "weight");
802 prop->weight = lua_tonumber(L, -1);
805 lua_getfield(L, -1, "collisionbox");
806 if(lua_istable(L, -1)){
807 lua_rawgeti(L, -1, 1);
808 prop->collisionbox.MinEdge.X = lua_tonumber(L, -1);
810 lua_rawgeti(L, -1, 2);
811 prop->collisionbox.MinEdge.Y = lua_tonumber(L, -1);
813 lua_rawgeti(L, -1, 3);
814 prop->collisionbox.MinEdge.Z = lua_tonumber(L, -1);
816 lua_rawgeti(L, -1, 4);
817 prop->collisionbox.MaxEdge.X = lua_tonumber(L, -1);
819 lua_rawgeti(L, -1, 5);
820 prop->collisionbox.MaxEdge.Y = lua_tonumber(L, -1);
822 lua_rawgeti(L, -1, 6);
823 prop->collisionbox.MaxEdge.Z = lua_tonumber(L, -1);
828 lua_getfield(L, -1, "visual");
829 if(lua_isstring(L, -1))
830 prop->visual = lua_tostring(L, -1);
833 lua_getfield(L, -1, "textures");
834 if(lua_istable(L, -1)){
835 prop->textures.clear();
836 int table = lua_gettop(L);
838 while(lua_next(L, table) != 0){
839 // key at index -2 and value at index -1
840 if(lua_isstring(L, -1))
841 prop->textures.push_back(lua_tostring(L, -1));
843 prop->textures.push_back("");
844 // removes value, keeps key for next iteration
852 void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
855 assert(lua_checkstack(L, 20));
856 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
857 StackUnroller stack_unroller(L);
859 // Get minetest.luaentities[id]
860 luaentity_get(L, id);
861 int object = lua_gettop(L);
862 // State: object is at top of stack
864 lua_getfield(L, -1, "on_step");
865 luaL_checktype(L, -1, LUA_TFUNCTION);
866 lua_pushvalue(L, object); // self
867 lua_pushnumber(L, dtime); // dtime
868 // Call with 2 arguments, 0 results
869 if(lua_pcall(L, 2, 0, 0))
870 script_error(L, "error running function 'step': %s\n", lua_tostring(L, -1));
873 // Calls entity:on_punch(ObjectRef puncher)
874 void scriptapi_luaentity_punch(lua_State *L, u16 id,
875 ServerActiveObject *puncher)
878 assert(lua_checkstack(L, 20));
879 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
880 StackUnroller stack_unroller(L);
882 // Get minetest.luaentities[id]
883 luaentity_get(L, id);
884 int object = lua_gettop(L);
885 // State: object is at top of stack
887 lua_getfield(L, -1, "on_punch");
888 luaL_checktype(L, -1, LUA_TFUNCTION);
889 lua_pushvalue(L, object); // self
890 objectref_get_or_create(L, puncher); // Clicker reference
891 // Call with 2 arguments, 0 results
892 if(lua_pcall(L, 2, 0, 0))
893 script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
896 // Calls entity:on_rightclick(ObjectRef clicker)
897 void scriptapi_luaentity_rightclick(lua_State *L, u16 id,
898 ServerActiveObject *clicker)
901 assert(lua_checkstack(L, 20));
902 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
903 StackUnroller stack_unroller(L);
905 // Get minetest.luaentities[id]
906 luaentity_get(L, id);
907 int object = lua_gettop(L);
908 // State: object is at top of stack
910 lua_getfield(L, -1, "on_rightclick");
911 luaL_checktype(L, -1, LUA_TFUNCTION);
912 lua_pushvalue(L, object); // self
913 objectref_get_or_create(L, clicker); // Clicker reference
914 // Call with 2 arguments, 0 results
915 if(lua_pcall(L, 2, 0, 0))
916 script_error(L, "error running function 'on_rightclick': %s\n", lua_tostring(L, -1));