From 526eedf98e45fc3b0ad768ecb1d0cbc1968def2f Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 12 Nov 2011 11:59:56 +0200 Subject: Scripting WIP --- src/scriptapi.cpp | 109 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 26 deletions(-) (limited to 'src/scriptapi.cpp') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index a50516edf..b0a99041e 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -34,6 +34,7 @@ extern "C" { #include "script.h" //#include "luna.h" #include "luaentity_common.h" +#include "content_sao.h" // For LuaEntitySAO /* TODO: @@ -110,6 +111,29 @@ public: } }; +v3f readFloatPos(lua_State *L, int index) +{ + v3f pos; + lua_pushvalue(L, index); // Push pos + luaL_checktype(L, -1, LUA_TTABLE); + lua_getfield(L, -1, "x"); + pos.X = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "y"); + pos.Y = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "z"); + pos.Z = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_pop(L, 1); // Pop pos + pos *= BS; // Scale to internal format + return pos; +} + +/* + Global functions +*/ + // Register new object prototype // register_entity(name, prototype) static int l_register_entity(lua_State *L) @@ -149,16 +173,18 @@ static const struct luaL_Reg minetest_f [] = { {NULL, NULL} }; -static int l_entity_set_deleted(lua_State *L) -{ - return 0; -} +/* + LuaEntity functions +*/ static const struct luaL_Reg minetest_entity_m [] = { - {"set_deleted", l_entity_set_deleted}, {NULL, NULL} }; +/* + Getters for stuff in main tables +*/ + static void objectref_get(lua_State *L, u16 id) { // Get minetest.object_refs[i] @@ -324,12 +350,28 @@ private: return *(ObjectRef**)ud; // unbox pointer } + static ServerActiveObject* getobject(ObjectRef *ref) + { + ServerActiveObject *co = ref->m_object; + return co; + } + + static LuaEntitySAO* getluaobject(ObjectRef *ref) + { + ServerActiveObject *obj = getobject(ref); + if(obj == NULL) + return NULL; + if(obj->getType() != ACTIVEOBJECT_TYPE_LUAENTITY) + return NULL; + return (LuaEntitySAO*)obj; + } + // Exported functions static int l_remove(lua_State *L) { - ObjectRef *o = checkobject(L, 1); - ServerActiveObject *co = o->m_object; + ObjectRef *ref = checkobject(L, 1); + ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; infostream<<"ObjectRef::l_remove(): id="<getId()<m_removed = true; @@ -338,8 +380,8 @@ private: static int l_getpos(lua_State *L) { - ObjectRef *o = checkobject(L, 1); - ServerActiveObject *co = o->m_object; + ObjectRef *ref = checkobject(L, 1); + ServerActiveObject *co = getobject(ref); if(co == NULL) return 0; infostream<<"ObjectRef::l_getpos(): id="<getId()<getBasePosition() / BS; @@ -353,6 +395,32 @@ private: return 1; } + static int l_setpos(lua_State *L) + { + ObjectRef *ref = checkobject(L, 1); + //LuaEntitySAO *co = getluaobject(ref); + ServerActiveObject *co = getobject(ref); + if(co == NULL) return 0; + // pos + v3f pos = readFloatPos(L, 2); + // Do it + co->setPos(pos); + return 0; + } + + static int l_moveto(lua_State *L) + { + ObjectRef *ref = checkobject(L, 1); + //LuaEntitySAO *co = getluaobject(ref); + ServerActiveObject *co = getobject(ref); + if(co == NULL) return 0; + // pos + v3f pos = readFloatPos(L, 2); + // Do it + co->moveTo(pos); + return 0; + } + static int gc_object(lua_State *L) { //ObjectRef *o = checkobject(L, 1); ObjectRef *o = *(ObjectRef **)(lua_touserdata(L, 1)); @@ -426,6 +494,8 @@ const char ObjectRef::className[] = "ObjectRef"; const luaL_reg ObjectRef::methods[] = { method(ObjectRef, remove), method(ObjectRef, getpos), + method(ObjectRef, setpos), + method(ObjectRef, moveto), {0,0} }; @@ -438,6 +508,7 @@ void scriptapi_export(lua_State *L, Server *server) realitycheck(L); assert(lua_checkstack(L, 20)); infostream<<"scriptapi_export"<getId()); // Push id lua_pushnil(L); lua_settable(L, objectstable); - - // pop object_refs, minetest - lua_pop(L, 2); } /* -- cgit v1.2.3