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 network and client-side stuff
44 - Named node types and dynamic id allocation
46 blockdef.has_metadata = true/false
47 - Stores an inventory and stuff in a Settings object
48 meta.inventory_add_list("main")
49 blockdef.on_inventory_modified
50 meta.set("owner", playername)
54 static void stackDump(lua_State *L, std::ostream &o)
57 int top = lua_gettop(L);
58 for (i = 1; i <= top; i++) { /* repeat for each level */
59 int t = lua_type(L, i);
62 case LUA_TSTRING: /* strings */
63 o<<"\""<<lua_tostring(L, i)<<"\"";
66 case LUA_TBOOLEAN: /* booleans */
67 o<<(lua_toboolean(L, i) ? "true" : "false");
70 case LUA_TNUMBER: /* numbers */ {
72 snprintf(buf, 10, "%g", lua_tonumber(L, i));
76 default: /* other values */
77 o<<lua_typename(L, t);
86 static void realitycheck(lua_State *L)
88 int top = lua_gettop(L);
90 dstream<<"Stack is over 30:"<<std::endl;
91 stackDump(L, dstream);
92 script_error(L, "Stack is over 30 (reality check)");
102 StackUnroller(lua_State *L):
106 m_original_top = lua_gettop(m_lua); // store stack height
110 lua_settop(m_lua, m_original_top); // restore stack height
114 v3f readFloatPos(lua_State *L, int index)
117 lua_pushvalue(L, index); // Push pos
118 luaL_checktype(L, -1, LUA_TTABLE);
119 lua_getfield(L, -1, "x");
120 pos.X = lua_tonumber(L, -1);
122 lua_getfield(L, -1, "y");
123 pos.Y = lua_tonumber(L, -1);
125 lua_getfield(L, -1, "z");
126 pos.Z = lua_tonumber(L, -1);
128 lua_pop(L, 1); // Pop pos
129 pos *= BS; // Scale to internal format
137 // Register new object prototype
138 // register_entity(name, prototype)
139 static int l_register_entity(lua_State *L)
141 const char *name = luaL_checkstring(L, 1);
142 luaL_checktype(L, 2, LUA_TTABLE);
143 infostream<<"register_entity: "<<name<<std::endl;
145 // Get minetest.registered_entities
146 lua_getglobal(L, "minetest");
147 lua_getfield(L, -1, "registered_entities");
148 luaL_checktype(L, -1, LUA_TTABLE);
149 int registered_entities = lua_gettop(L);
150 lua_pushvalue(L, 2); // Object = param 2 -> stack top
151 // registered_entities[name] = object
152 lua_setfield(L, registered_entities, name);
154 // Get registered object to top of stack
157 // Set __index to point to itself
158 lua_pushvalue(L, -1);
159 lua_setfield(L, -2, "__index");
161 // Set metatable.__index = metatable
162 luaL_getmetatable(L, "minetest.entity");
163 lua_pushvalue(L, -1); // duplicate metatable
164 lua_setfield(L, -2, "__index");
165 // Set object metatable
166 lua_setmetatable(L, -2);
168 return 0; /* number of results */
171 static const struct luaL_Reg minetest_f [] = {
172 {"register_entity", l_register_entity},
180 static const struct luaL_Reg minetest_entity_m [] = {
185 Getters for stuff in main tables
188 static void objectref_get(lua_State *L, u16 id)
190 // Get minetest.object_refs[i]
191 lua_getglobal(L, "minetest");
192 lua_getfield(L, -1, "object_refs");
193 luaL_checktype(L, -1, LUA_TTABLE);
194 lua_pushnumber(L, id);
196 lua_remove(L, -2); // object_refs
197 lua_remove(L, -2); // minetest
200 static void luaentity_get(lua_State *L, u16 id)
202 // Get minetest.luaentities[i]
203 lua_getglobal(L, "minetest");
204 lua_getfield(L, -1, "luaentities");
205 luaL_checktype(L, -1, LUA_TTABLE);
206 lua_pushnumber(L, id);
208 lua_remove(L, -2); // luaentities
209 lua_remove(L, -2); // minetest
215 #define method(class, name) {#name, class::l_##name}
220 ServerEnvironment *m_env;
222 static const char className[];
223 static const luaL_reg methods[];
225 static EnvRef *checkobject(lua_State *L, int narg)
227 luaL_checktype(L, narg, LUA_TUSERDATA);
228 void *ud = luaL_checkudata(L, narg, className);
229 if(!ud) luaL_typerror(L, narg, className);
230 return *(EnvRef**)ud; // unbox pointer
233 // Exported functions
235 // EnvRef:add_node(pos, content)
236 // pos = {x=num, y=num, z=num}
238 static int l_add_node(lua_State *L)
240 infostream<<"EnvRef::l_add_node()"<<std::endl;
241 EnvRef *o = checkobject(L, 1);
242 ServerEnvironment *env = o->m_env;
243 if(env == NULL) return 0;
246 lua_pushvalue(L, 2); // Push pos
247 luaL_checktype(L, -1, LUA_TTABLE);
248 lua_getfield(L, -1, "x");
249 pos.X = lua_tonumber(L, -1);
251 lua_getfield(L, -1, "y");
252 pos.Y = lua_tonumber(L, -1);
254 lua_getfield(L, -1, "z");
255 pos.Z = lua_tonumber(L, -1);
257 lua_pop(L, 1); // Pop pos
260 lua_pushvalue(L, 3); // Push content
261 content = lua_tonumber(L, -1);
262 lua_pop(L, 1); // Pop content
264 env->getMap().addNodeWithEvent(pos, MapNode(content));
268 static int gc_object(lua_State *L) {
269 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
275 EnvRef(ServerEnvironment *env):
278 infostream<<"EnvRef created"<<std::endl;
283 infostream<<"EnvRef destructing"<<std::endl;
286 // Creates an EnvRef and leaves it on top of stack
287 // Not callable from Lua; all references are created on the C side.
288 static void create(lua_State *L, ServerEnvironment *env)
290 EnvRef *o = new EnvRef(env);
291 //infostream<<"EnvRef::create: o="<<o<<std::endl;
292 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
293 luaL_getmetatable(L, className);
294 lua_setmetatable(L, -2);
297 static void set_null(lua_State *L)
299 EnvRef *o = checkobject(L, -1);
303 static void Register(lua_State *L)
306 int methodtable = lua_gettop(L);
307 luaL_newmetatable(L, className);
308 int metatable = lua_gettop(L);
310 lua_pushliteral(L, "__metatable");
311 lua_pushvalue(L, methodtable);
312 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
314 lua_pushliteral(L, "__index");
315 lua_pushvalue(L, methodtable);
316 lua_settable(L, metatable);
318 lua_pushliteral(L, "__gc");
319 lua_pushcfunction(L, gc_object);
320 lua_settable(L, metatable);
322 lua_pop(L, 1); // drop metatable
324 luaL_openlib(L, 0, methods, 0); // fill methodtable
325 lua_pop(L, 1); // drop methodtable
327 // Cannot be created from Lua
328 //lua_register(L, className, create_object);
331 const char EnvRef::className[] = "EnvRef";
332 const luaL_reg EnvRef::methods[] = {
333 method(EnvRef, add_node),
340 ServerActiveObject *m_object;
342 static const char className[];
343 static const luaL_reg methods[];
345 static ObjectRef *checkobject(lua_State *L, int narg)
347 luaL_checktype(L, narg, LUA_TUSERDATA);
348 void *ud = luaL_checkudata(L, narg, className);
349 if(!ud) luaL_typerror(L, narg, className);
350 return *(ObjectRef**)ud; // unbox pointer
353 static ServerActiveObject* getobject(ObjectRef *ref)
355 ServerActiveObject *co = ref->m_object;
359 static LuaEntitySAO* getluaobject(ObjectRef *ref)
361 ServerActiveObject *obj = getobject(ref);
364 if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY)
366 return (LuaEntitySAO*)obj;
369 // Exported functions
371 static int l_remove(lua_State *L)
373 ObjectRef *ref = checkobject(L, 1);
374 ServerActiveObject *co = getobject(ref);
375 if(co == NULL) return 0;
376 infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
377 co->m_removed = true;
381 static int l_getpos(lua_State *L)
383 ObjectRef *ref = checkobject(L, 1);
384 ServerActiveObject *co = getobject(ref);
385 if(co == NULL) return 0;
386 v3f pos = co->getBasePosition() / BS;
388 lua_pushnumber(L, pos.X);
389 lua_setfield(L, -2, "x");
390 lua_pushnumber(L, pos.Y);
391 lua_setfield(L, -2, "y");
392 lua_pushnumber(L, pos.Z);
393 lua_setfield(L, -2, "z");
397 static int l_setpos(lua_State *L)
399 ObjectRef *ref = checkobject(L, 1);
400 //LuaEntitySAO *co = getluaobject(ref);
401 ServerActiveObject *co = getobject(ref);
402 if(co == NULL) return 0;
404 v3f pos = readFloatPos(L, 2);
410 static int l_moveto(lua_State *L)
412 ObjectRef *ref = checkobject(L, 1);
413 //LuaEntitySAO *co = getluaobject(ref);
414 ServerActiveObject *co = getobject(ref);
415 if(co == NULL) return 0;
417 v3f pos = readFloatPos(L, 2);
423 static int gc_object(lua_State *L) {
424 //ObjectRef *o = checkobject(L, 1);
425 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
426 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
432 ObjectRef(ServerActiveObject *object):
435 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
441 infostream<<"ObjectRef destructing for id="
442 <<m_object->getId()<<std::endl;
444 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
447 // Creates an ObjectRef and leaves it on top of stack
448 // Not callable from Lua; all references are created on the C side.
449 static void create(lua_State *L, ServerActiveObject *object)
451 ObjectRef *o = new ObjectRef(object);
452 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
453 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
454 luaL_getmetatable(L, className);
455 lua_setmetatable(L, -2);
458 static void set_null(lua_State *L)
460 ObjectRef *o = checkobject(L, -1);
464 static void Register(lua_State *L)
467 int methodtable = lua_gettop(L);
468 luaL_newmetatable(L, className);
469 int metatable = lua_gettop(L);
471 lua_pushliteral(L, "__metatable");
472 lua_pushvalue(L, methodtable);
473 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
475 lua_pushliteral(L, "__index");
476 lua_pushvalue(L, methodtable);
477 lua_settable(L, metatable);
479 lua_pushliteral(L, "__gc");
480 lua_pushcfunction(L, gc_object);
481 lua_settable(L, metatable);
483 lua_pop(L, 1); // drop metatable
485 luaL_openlib(L, 0, methods, 0); // fill methodtable
486 lua_pop(L, 1); // drop methodtable
488 // Cannot be created from Lua
489 //lua_register(L, className, create_object);
492 const char ObjectRef::className[] = "ObjectRef";
493 const luaL_reg ObjectRef::methods[] = {
494 method(ObjectRef, remove),
495 method(ObjectRef, getpos),
496 method(ObjectRef, setpos),
497 method(ObjectRef, moveto),
505 void scriptapi_export(lua_State *L, Server *server)
508 assert(lua_checkstack(L, 20));
509 infostream<<"scriptapi_export"<<std::endl;
510 StackUnroller stack_unroller(L);
512 // Register global functions in table minetest
514 luaL_register(L, NULL, minetest_f);
515 lua_setglobal(L, "minetest");
517 // Get the main minetest table
518 lua_getglobal(L, "minetest");
520 // Add registered_entities table in minetest
522 lua_setfield(L, -2, "registered_entities");
524 // Add object_refs table in minetest
526 lua_setfield(L, -2, "object_refs");
528 // Add luaentities table in minetest
530 lua_setfield(L, -2, "luaentities");
532 // Create entity prototype
533 luaL_newmetatable(L, "minetest.entity");
534 // metatable.__index = metatable
535 lua_pushvalue(L, -1); // Duplicate metatable
536 lua_setfield(L, -2, "__index");
537 // Put functions in metatable
538 luaL_register(L, NULL, minetest_entity_m);
539 // Put other stuff in metatable
541 // Environment C reference
544 // Object C reference
545 ObjectRef::Register(L);
548 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
551 assert(lua_checkstack(L, 20));
552 infostream<<"scriptapi_add_environment"<<std::endl;
553 StackUnroller stack_unroller(L);
555 // Create EnvRef on stack
556 EnvRef::create(L, env);
557 int envref = lua_gettop(L);
559 // minetest.env = envref
560 lua_getglobal(L, "minetest");
561 luaL_checktype(L, -1, LUA_TTABLE);
562 lua_pushvalue(L, envref);
563 lua_setfield(L, -2, "env");
566 // Dump stack top with the dump2 function
567 static void dump2(lua_State *L, const char *name)
569 // Dump object (debug)
570 lua_getglobal(L, "dump2");
571 luaL_checktype(L, -1, LUA_TFUNCTION);
572 lua_pushvalue(L, -2); // Get previous stack top as first parameter
573 lua_pushstring(L, name);
574 if(lua_pcall(L, 2, 0, 0))
575 script_error(L, "error: %s\n", lua_tostring(L, -1));
582 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
585 assert(lua_checkstack(L, 20));
586 infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
587 StackUnroller stack_unroller(L);
589 // Create object on stack
590 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
591 int object = lua_gettop(L);
593 // Get minetest.object_refs table
594 lua_getglobal(L, "minetest");
595 lua_getfield(L, -1, "object_refs");
596 luaL_checktype(L, -1, LUA_TTABLE);
597 int objectstable = lua_gettop(L);
599 // object_refs[id] = object
600 lua_pushnumber(L, cobj->getId()); // Push id
601 lua_pushvalue(L, object); // Copy object to top of stack
602 lua_settable(L, objectstable);
605 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
608 assert(lua_checkstack(L, 20));
609 infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
610 StackUnroller stack_unroller(L);
612 // Get minetest.object_refs table
613 lua_getglobal(L, "minetest");
614 lua_getfield(L, -1, "object_refs");
615 luaL_checktype(L, -1, LUA_TTABLE);
616 int objectstable = lua_gettop(L);
618 // Get object_refs[id]
619 lua_pushnumber(L, cobj->getId()); // Push id
620 lua_gettable(L, objectstable);
621 // Set object reference to NULL
622 ObjectRef::set_null(L);
623 lua_pop(L, 1); // pop object
625 // Set object_refs[id] = nil
626 lua_pushnumber(L, cobj->getId()); // Push id
628 lua_settable(L, objectstable);
635 void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
636 const char *init_state)
639 assert(lua_checkstack(L, 20));
640 infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
641 <<name<<"\""<<std::endl;
642 StackUnroller stack_unroller(L);
644 // Create object as a dummy string (TODO: Create properly)
646 // Get minetest.registered_entities[name]
647 lua_getglobal(L, "minetest");
648 lua_getfield(L, -1, "registered_entities");
649 luaL_checktype(L, -1, LUA_TTABLE);
650 lua_pushstring(L, name);
652 // Should be a table, which we will use as a prototype
653 luaL_checktype(L, -1, LUA_TTABLE);
654 int prototype_table = lua_gettop(L);
655 //dump2(L, "prototype_table");
657 // Create entity object
659 int object = lua_gettop(L);
661 // Set object metatable
662 lua_pushvalue(L, prototype_table);
663 lua_setmetatable(L, -2);
665 // Add object reference
666 // This should be userdata with metatable ObjectRef
667 objectref_get(L, id);
668 luaL_checktype(L, -1, LUA_TUSERDATA);
669 if(!luaL_checkudata(L, -1, "ObjectRef"))
670 luaL_typerror(L, -1, "ObjectRef");
671 lua_setfield(L, -2, "object");
673 // minetest.luaentities[id] = object
674 lua_getglobal(L, "minetest");
675 lua_getfield(L, -1, "luaentities");
676 luaL_checktype(L, -1, LUA_TTABLE);
677 lua_pushnumber(L, id); // Push id
678 lua_pushvalue(L, object); // Copy object to top of stack
681 // This callback doesn't really make sense
682 /*// Get on_activate function
683 lua_pushvalue(L, object);
684 lua_getfield(L, -1, "on_activate");
685 luaL_checktype(L, -1, LUA_TFUNCTION);
686 lua_pushvalue(L, object); // self
687 // Call with 1 arguments, 0 results
688 if(lua_pcall(L, 1, 0, 0))
689 script_error(L, "error running function %s:on_activate: %s\n",
690 name, lua_tostring(L, -1));*/
693 void scriptapi_luaentity_rm(lua_State *L, u16 id)
696 assert(lua_checkstack(L, 20));
697 infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
699 // Get minetest.luaentities table
700 lua_getglobal(L, "minetest");
701 lua_getfield(L, -1, "luaentities");
702 luaL_checktype(L, -1, LUA_TTABLE);
703 int objectstable = lua_gettop(L);
705 // Set luaentities[id] = nil
706 lua_pushnumber(L, id); // Push id
708 lua_settable(L, objectstable);
710 lua_pop(L, 2); // pop luaentities, minetest
713 std::string scriptapi_luaentity_get_state(lua_State *L, u16 id)
716 assert(lua_checkstack(L, 20));
717 infostream<<"scriptapi_luaentity_get_state: id="<<id<<std::endl;
722 void scriptapi_luaentity_get_properties(lua_State *L, u16 id,
723 LuaEntityProperties *prop)
726 assert(lua_checkstack(L, 20));
727 infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
728 StackUnroller stack_unroller(L);
730 // Get minetest.luaentities[id]
731 luaentity_get(L, id);
732 //int object = lua_gettop(L);
734 lua_getfield(L, -1, "physical");
735 if(lua_isboolean(L, -1))
736 prop->physical = lua_toboolean(L, -1);
739 lua_getfield(L, -1, "weight");
740 prop->weight = lua_tonumber(L, -1);
743 lua_getfield(L, -1, "collisionbox");
744 if(lua_istable(L, -1)){
745 lua_rawgeti(L, -1, 1);
746 prop->collisionbox.MinEdge.X = lua_tonumber(L, -1);
748 lua_rawgeti(L, -1, 2);
749 prop->collisionbox.MinEdge.Y = lua_tonumber(L, -1);
751 lua_rawgeti(L, -1, 3);
752 prop->collisionbox.MinEdge.Z = lua_tonumber(L, -1);
754 lua_rawgeti(L, -1, 4);
755 prop->collisionbox.MaxEdge.X = lua_tonumber(L, -1);
757 lua_rawgeti(L, -1, 5);
758 prop->collisionbox.MaxEdge.Y = lua_tonumber(L, -1);
760 lua_rawgeti(L, -1, 6);
761 prop->collisionbox.MaxEdge.Z = lua_tonumber(L, -1);
766 lua_getfield(L, -1, "visual");
767 if(lua_isstring(L, -1))
768 prop->visual = lua_tostring(L, -1);
771 lua_getfield(L, -1, "textures");
772 if(lua_istable(L, -1)){
773 prop->textures.clear();
774 int table = lua_gettop(L);
776 while(lua_next(L, table) != 0){
777 // key at index -2 and value at index -1
778 if(lua_isstring(L, -1))
779 prop->textures.push_back(lua_tostring(L, -1));
781 prop->textures.push_back("");
782 // removes value, keeps key for next iteration
789 void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
792 assert(lua_checkstack(L, 20));
793 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
794 StackUnroller stack_unroller(L);
796 // Get minetest.luaentities[id]
797 luaentity_get(L, id);
798 int object = lua_gettop(L);
799 // State: object is at top of stack
801 lua_getfield(L, -1, "on_step");
802 luaL_checktype(L, -1, LUA_TFUNCTION);
803 lua_pushvalue(L, object); // self
804 lua_pushnumber(L, dtime); // dtime
805 // Call with 2 arguments, 0 results
806 if(lua_pcall(L, 2, 0, 0))
807 script_error(L, "error running function 'step': %s\n", lua_tostring(L, -1));
810 void scriptapi_luaentity_rightclick_player(lua_State *L, u16 id,
811 const char *playername)
814 assert(lua_checkstack(L, 20));
815 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
816 StackUnroller stack_unroller(L);
818 // Get minetest.luaentities[id]
819 luaentity_get(L, id);
820 int object = lua_gettop(L);
821 // State: object is at top of stack
823 lua_getfield(L, -1, "on_rightclick");
824 luaL_checktype(L, -1, LUA_TFUNCTION);
825 lua_pushvalue(L, object); // self
826 // Call with 1 arguments, 0 results
827 if(lua_pcall(L, 1, 0, 0))
828 script_error(L, "error running function 'step': %s\n", lua_tostring(L, -1));