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"
37 static void stackDump(lua_State *L, std::ostream &o)
40 int top = lua_gettop(L);
41 for (i = 1; i <= top; i++) { /* repeat for each level */
42 int t = lua_type(L, i);
45 case LUA_TSTRING: /* strings */
46 o<<"\""<<lua_tostring(L, i)<<"\"";
49 case LUA_TBOOLEAN: /* booleans */
50 o<<(lua_toboolean(L, i) ? "true" : "false");
53 case LUA_TNUMBER: /* numbers */ {
55 snprintf(buf, 10, "%g", lua_tonumber(L, i));
59 default: /* other values */
60 o<<lua_typename(L, t);
69 static void realitycheck(lua_State *L)
71 int top = lua_gettop(L);
73 dstream<<"Stack is over 30:"<<std::endl;
74 stackDump(L, dstream);
75 script_error(L, "Stack is over 30 (reality check)");
85 StackUnroller(lua_State *L):
89 m_original_top = lua_gettop(m_lua); // store stack height
93 lua_settop(m_lua, m_original_top); // restore stack height
97 // Register new object prototype
98 // register_entity(name, prototype)
99 static int l_register_entity(lua_State *L)
101 const char *name = luaL_checkstring(L, 1);
102 luaL_checktype(L, 2, LUA_TTABLE);
103 infostream<<"register_entity: "<<name<<std::endl;
105 // Get minetest.registered_entities
106 lua_getglobal(L, "minetest");
107 lua_getfield(L, -1, "registered_entities");
108 luaL_checktype(L, -1, LUA_TTABLE);
109 int registered_entities = lua_gettop(L);
110 lua_pushvalue(L, 2); // Object = param 2 -> stack top
111 // registered_entities[name] = object
112 lua_setfield(L, registered_entities, name);
114 // Get registered object to top of stack
117 // Set __index to point to itself
118 lua_pushvalue(L, -1);
119 lua_setfield(L, -2, "__index");
121 // Set metatable.__index = metatable
122 luaL_getmetatable(L, "minetest.entity");
123 lua_pushvalue(L, -1); // duplicate metatable
124 lua_setfield(L, -2, "__index");
125 // Set object metatable
126 lua_setmetatable(L, -2);
128 return 0; /* number of results */
132 static int l_new_entity(lua_State *L)
135 setmetatable(o, self)
140 luaL_checktype(L, -1, LUA_TTABLE);
141 luaL_getmetatable(L, "minetest.entity");
142 lua_pushvalue(L, -1); // duplicate metatable
143 lua_setfield(L, -2, "__index");
144 lua_setmetatable(L, -2);
150 static const struct luaL_Reg minetest_f [] = {
151 {"register_entity", l_register_entity},
152 //{"new_entity", l_new_entity},
156 static int l_entity_set_deleted(lua_State *L)
161 static const struct luaL_Reg minetest_entity_m [] = {
162 {"set_deleted", l_entity_set_deleted},
169 #define method(class, name) {#name, class::l_##name}
174 ServerEnvironment *m_env;
176 static const char className[];
177 static const luaL_reg methods[];
179 static EnvRef *checkobject(lua_State *L, int narg)
181 luaL_checktype(L, narg, LUA_TUSERDATA);
182 void *ud = luaL_checkudata(L, narg, className);
183 if(!ud) luaL_typerror(L, narg, className);
184 return *(EnvRef**)ud; // unbox pointer
187 // Exported functions
189 // EnvRef:add_node(pos, content)
190 // pos = {x=num, y=num, z=num}
192 static int l_add_node(lua_State *L)
194 infostream<<"EnvRef::l_add_node()"<<std::endl;
195 EnvRef *o = checkobject(L, 1);
196 ServerEnvironment *env = o->m_env;
197 if(env == NULL) return 0;
200 lua_pushvalue(L, 2); // Push pos
201 luaL_checktype(L, -1, LUA_TTABLE);
202 lua_getfield(L, -1, "x");
203 pos.X = lua_tonumber(L, -1);
205 lua_getfield(L, -1, "y");
206 pos.Y = lua_tonumber(L, -1);
208 lua_getfield(L, -1, "z");
209 pos.Z = lua_tonumber(L, -1);
211 lua_pop(L, 1); // Pop pos
214 lua_pushvalue(L, 3); // Push content
215 content = lua_tonumber(L, -1);
216 lua_pop(L, 1); // Pop content
218 env->getMap().addNodeWithEvent(pos, MapNode(content));
222 static int gc_object(lua_State *L) {
223 EnvRef *o = *(EnvRef **)(lua_touserdata(L, 1));
229 EnvRef(ServerEnvironment *env):
232 infostream<<"EnvRef created"<<std::endl;
237 infostream<<"EnvRef destructing"<<std::endl;
240 // Creates an EnvRef and leaves it on top of stack
241 // Not callable from Lua; all references are created on the C side.
242 static void create(lua_State *L, ServerEnvironment *env)
244 EnvRef *o = new EnvRef(env);
245 //infostream<<"EnvRef::create: o="<<o<<std::endl;
246 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
247 luaL_getmetatable(L, className);
248 lua_setmetatable(L, -2);
251 static void set_null(lua_State *L)
253 EnvRef *o = checkobject(L, -1);
257 static void Register(lua_State *L)
260 int methodtable = lua_gettop(L);
261 luaL_newmetatable(L, className);
262 int metatable = lua_gettop(L);
264 lua_pushliteral(L, "__metatable");
265 lua_pushvalue(L, methodtable);
266 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
268 lua_pushliteral(L, "__index");
269 lua_pushvalue(L, methodtable);
270 lua_settable(L, metatable);
272 lua_pushliteral(L, "__gc");
273 lua_pushcfunction(L, gc_object);
274 lua_settable(L, metatable);
276 lua_pop(L, 1); // drop metatable
278 luaL_openlib(L, 0, methods, 0); // fill methodtable
279 lua_pop(L, 1); // drop methodtable
281 // Cannot be created from Lua
282 //lua_register(L, className, create_object);
285 const char EnvRef::className[] = "EnvRef";
286 const luaL_reg EnvRef::methods[] = {
287 method(EnvRef, add_node),
294 ServerActiveObject *m_object;
296 static const char className[];
297 static const luaL_reg methods[];
299 static ObjectRef *checkobject(lua_State *L, int narg)
301 luaL_checktype(L, narg, LUA_TUSERDATA);
302 void *ud = luaL_checkudata(L, narg, className);
303 if(!ud) luaL_typerror(L, narg, className);
304 return *(ObjectRef**)ud; // unbox pointer
307 // Exported functions
309 static int l_remove(lua_State *L)
311 ObjectRef *o = checkobject(L, 1);
312 ServerActiveObject *co = o->m_object;
313 if(co == NULL) return 0;
314 infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
315 co->m_removed = true;
319 static int l_getpos(lua_State *L)
321 ObjectRef *o = checkobject(L, 1);
322 ServerActiveObject *co = o->m_object;
323 if(co == NULL) return 0;
324 infostream<<"ObjectRef::l_getpos(): id="<<co->getId()<<std::endl;
325 v3f pos = co->getBasePosition() / BS;
327 lua_pushnumber(L, pos.X);
328 lua_setfield(L, -2, "x");
329 lua_pushnumber(L, pos.Y);
330 lua_setfield(L, -2, "y");
331 lua_pushnumber(L, pos.Z);
332 lua_setfield(L, -2, "z");
336 static int gc_object(lua_State *L) {
337 //ObjectRef *o = checkobject(L, 1);
338 ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1));
339 //infostream<<"ObjectRef::gc_object: o="<<o<<std::endl;
345 ObjectRef(ServerActiveObject *object):
348 //infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
354 infostream<<"ObjectRef destructing for id="
355 <<m_object->getId()<<std::endl;
357 infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
360 // Creates an ObjectRef and leaves it on top of stack
361 // Not callable from Lua; all references are created on the C side.
362 static void create(lua_State *L, ServerActiveObject *object)
364 ObjectRef *o = new ObjectRef(object);
365 //infostream<<"ObjectRef::create: o="<<o<<std::endl;
366 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
367 luaL_getmetatable(L, className);
368 lua_setmetatable(L, -2);
371 static void set_null(lua_State *L)
373 ObjectRef *o = checkobject(L, -1);
377 static void Register(lua_State *L)
380 int methodtable = lua_gettop(L);
381 luaL_newmetatable(L, className);
382 int metatable = lua_gettop(L);
384 lua_pushliteral(L, "__metatable");
385 lua_pushvalue(L, methodtable);
386 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
388 lua_pushliteral(L, "__index");
389 lua_pushvalue(L, methodtable);
390 lua_settable(L, metatable);
392 lua_pushliteral(L, "__gc");
393 lua_pushcfunction(L, gc_object);
394 lua_settable(L, metatable);
396 lua_pop(L, 1); // drop metatable
398 luaL_openlib(L, 0, methods, 0); // fill methodtable
399 lua_pop(L, 1); // drop methodtable
401 // Cannot be created from Lua
402 //lua_register(L, className, create_object);
405 const char ObjectRef::className[] = "ObjectRef";
406 const luaL_reg ObjectRef::methods[] = {
407 method(ObjectRef, remove),
408 method(ObjectRef, getpos),
416 void scriptapi_export(lua_State *L, Server *server)
419 assert(lua_checkstack(L, 20));
420 infostream<<"scriptapi_export"<<std::endl;
422 // Register global functions in table minetest
424 luaL_register(L, NULL, minetest_f);
425 lua_setglobal(L, "minetest");
427 // Get the main minetest table
428 lua_getglobal(L, "minetest");
430 // Add registered_entities table in minetest
432 lua_setfield(L, -2, "registered_entities");
434 // Add object_refs table in minetest
436 lua_setfield(L, -2, "object_refs");
438 // Add luaentities table in minetest
440 lua_setfield(L, -2, "luaentities");
442 // Load and run some base Lua stuff
443 /*script_load(L, (porting::path_data + DIR_DELIM + "scripts"
444 + DIR_DELIM + "base.lua").c_str());*/
446 // Create entity reference metatable
447 //luaL_newmetatable(L, "minetest.entity_reference");
450 // Create entity prototype
451 luaL_newmetatable(L, "minetest.entity");
452 // metatable.__index = metatable
453 lua_pushvalue(L, -1); // Duplicate metatable
454 lua_setfield(L, -2, "__index");
455 // Put functions in metatable
456 luaL_register(L, NULL, minetest_entity_m);
457 // Put other stuff in metatable
459 // Environment C reference
462 // Object C reference
463 ObjectRef::Register(L);
466 void scriptapi_add_environment(lua_State *L, ServerEnvironment *env)
469 assert(lua_checkstack(L, 20));
470 infostream<<"scriptapi_add_environment"<<std::endl;
472 // Create EnvRef on stack
473 EnvRef::create(L, env);
474 int envref = lua_gettop(L);
476 // minetest.env = envref
477 lua_getglobal(L, "minetest");
478 luaL_checktype(L, -1, LUA_TTABLE);
479 lua_pushvalue(L, envref);
480 lua_setfield(L, -2, "env");
482 // pop minetest and envref
486 // Dump stack top with the dump2 function
487 static void dump2(lua_State *L, const char *name)
489 // Dump object (debug)
490 lua_getglobal(L, "dump2");
491 luaL_checktype(L, -1, LUA_TFUNCTION);
492 lua_pushvalue(L, -2); // Get previous stack top as first parameter
493 lua_pushstring(L, name);
494 if(lua_pcall(L, 2, 0, 0))
495 script_error(L, "error: %s\n", lua_tostring(L, -1));
502 void scriptapi_add_object_reference(lua_State *L, ServerActiveObject *cobj)
505 assert(lua_checkstack(L, 20));
506 infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
508 // Create object on stack
509 ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
510 int object = lua_gettop(L);
512 // Get minetest.object_refs table
513 lua_getglobal(L, "minetest");
514 lua_getfield(L, -1, "object_refs");
515 luaL_checktype(L, -1, LUA_TTABLE);
516 int objectstable = lua_gettop(L);
518 // object_refs[id] = object
519 lua_pushnumber(L, cobj->getId()); // Push id
520 lua_pushvalue(L, object); // Copy object to top of stack
521 lua_settable(L, objectstable);
523 // pop object_refs, minetest and the object
527 void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj)
530 assert(lua_checkstack(L, 20));
531 infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
533 // Get minetest.object_refs table
534 lua_getglobal(L, "minetest");
535 lua_getfield(L, -1, "object_refs");
536 luaL_checktype(L, -1, LUA_TTABLE);
537 int objectstable = lua_gettop(L);
539 // Get object_refs[id]
540 lua_pushnumber(L, cobj->getId()); // Push id
541 lua_gettable(L, objectstable);
542 // Set object reference to NULL
543 ObjectRef::set_null(L);
544 lua_pop(L, 1); // pop object
546 // Set object_refs[id] = nil
547 lua_pushnumber(L, cobj->getId()); // Push id
549 lua_settable(L, objectstable);
551 // pop object_refs, minetest
555 static void objectref_get(lua_State *L, u16 id)
557 // Get minetest.object_refs[i]
558 lua_getglobal(L, "minetest");
559 lua_getfield(L, -1, "object_refs");
560 luaL_checktype(L, -1, LUA_TTABLE);
561 lua_pushnumber(L, id);
563 lua_remove(L, -2); // object_refs
564 lua_remove(L, -2); // minetest
571 void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
572 const char *init_state)
575 assert(lua_checkstack(L, 20));
576 infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
577 <<name<<"\""<<std::endl;
578 StackUnroller stack_unroller(L);
580 // Create object as a dummy string (TODO: Create properly)
582 // Get minetest.registered_entities[name]
583 lua_getglobal(L, "minetest");
584 lua_getfield(L, -1, "registered_entities");
585 luaL_checktype(L, -1, LUA_TTABLE);
586 lua_pushstring(L, name);
588 // Should be a table, which we will use as a prototype
589 luaL_checktype(L, -1, LUA_TTABLE);
590 int prototype_table = lua_gettop(L);
591 //dump2(L, "prototype_table");
593 // Create entity object
595 int object = lua_gettop(L);
597 // Set object metatable
598 lua_pushvalue(L, prototype_table);
599 lua_setmetatable(L, -2);
601 // Add object reference
602 // This should be userdata with metatable ObjectRef
603 objectref_get(L, id);
604 luaL_checktype(L, -1, LUA_TUSERDATA);
605 if(!luaL_checkudata(L, -1, "ObjectRef"))
606 luaL_typerror(L, -1, "ObjectRef");
607 lua_setfield(L, -2, "object");
609 // Get minetest.luaentities table
610 lua_getglobal(L, "minetest");
611 lua_getfield(L, -1, "luaentities");
612 luaL_checktype(L, -1, LUA_TTABLE);
613 int luaentities = lua_gettop(L);
615 // luaentities[id] = object
616 lua_pushnumber(L, id); // Push id
617 lua_pushvalue(L, object); // Copy object to top of stack
618 lua_settable(L, luaentities);
621 void scriptapi_luaentity_rm(lua_State *L, u16 id)
624 assert(lua_checkstack(L, 20));
625 infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
627 // Get minetest.luaentities table
628 lua_getglobal(L, "minetest");
629 lua_getfield(L, -1, "luaentities");
630 luaL_checktype(L, -1, LUA_TTABLE);
631 int objectstable = lua_gettop(L);
633 // Set luaentities[id] = nil
634 lua_pushnumber(L, id); // Push id
636 lua_settable(L, objectstable);
638 lua_pop(L, 2); // pop luaentities, minetest
641 static void luaentity_get(lua_State *L, u16 id)
643 // Get minetest.luaentities[i]
644 lua_getglobal(L, "minetest");
645 lua_getfield(L, -1, "luaentities");
646 luaL_checktype(L, -1, LUA_TTABLE);
647 lua_pushnumber(L, id);
649 lua_remove(L, -2); // luaentities
650 lua_remove(L, -2); // minetest
653 std::string scriptapi_luaentity_get_state(lua_State *L, u16 id)
656 assert(lua_checkstack(L, 20));
657 infostream<<"scriptapi_luaentity_get_state: id="<<id<<std::endl;
662 void scriptapi_luaentity_step(lua_State *L, u16 id, float dtime)
665 assert(lua_checkstack(L, 20));
666 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
667 StackUnroller stack_unroller(L);
669 // Get minetest.luaentities[id]
670 luaentity_get(L, id);
671 int object = lua_gettop(L);
672 // State: object is at top of stack
674 lua_getfield(L, -1, "on_step");
675 luaL_checktype(L, -1, LUA_TFUNCTION);
676 lua_pushvalue(L, object); // self
677 lua_pushnumber(L, dtime); // dtime
678 // Call with 2 arguments, 0 results
679 if(lua_pcall(L, 2, 0, 0))
680 script_error(L, "error running function 'step': %s\n", lua_tostring(L, -1));
683 void scriptapi_luaentity_rightclick_player(lua_State *L, u16 id,
684 const char *playername)
687 assert(lua_checkstack(L, 20));
688 //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
689 StackUnroller stack_unroller(L);
691 // Get minetest.luaentities[id]
692 luaentity_get(L, id);
693 int object = lua_gettop(L);
694 // State: object is at top of stack
696 lua_getfield(L, -1, "on_rightclick");
697 luaL_checktype(L, -1, LUA_TFUNCTION);
698 lua_pushvalue(L, object); // self
699 // Call with 1 arguments, 0 results
700 if(lua_pcall(L, 1, 0, 0))
701 script_error(L, "error running function 'step': %s\n", lua_tostring(L, -1));