void ScriptApiItem::pushPointedThing(const PointedThing& pointed) { lua_State* L = getStack(); lua_newtable(L); if(pointed.type == POINTEDTHING_NODE) { lua_pushstring(L, "node"); lua_setfield(L, -2, "type"); push_v3s16(L, pointed.node_undersurface); lua_setfield(L, -2, "under"); push_v3s16(L, pointed.node_abovesurface); lua_setfield(L, -2, "above"); } else if(pointed.type == POINTEDTHING_OBJECT) { lua_pushstring(L, "object"); lua_setfield(L, -2, "type"); objectrefGet(L, pointed.object_id); lua_setfield(L, -2, "ref"); } else { lua_pushstring(L, "nothing"); lua_setfield(L, -2, "type"); } }
// Creates a new anonymous reference if cobj=NULL or id=0 void ScriptApiBase::objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj) { if (cobj == NULL || cobj->getId() == 0) { ObjectRef::create(L, cobj); } else { objectrefGet(L, cobj->getId()); } }
bool ScriptApiEntity::luaentity_Add(u16 id, const char *name, bool force_usage) { SCRIPTAPI_PRECHECKHEADER /* verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\"" <<name<<"\""<<std::endl; */ // Get core.registered_entities[name] lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_entities"); luaL_checktype(L, -1, LUA_TTABLE); lua_pushstring(L, name); lua_gettable(L, -2); // Should be a table, which we will use as a prototype //luaL_checktype(L, -1, LUA_TTABLE); if (lua_type(L, -1) != LUA_TTABLE){ infostream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl; return false; } int prototype_table = lua_gettop(L); //dump2(L, "prototype_table"); // Create entity object lua_newtable(L); int object = lua_gettop(L); // Set object metatable lua_pushvalue(L, prototype_table); lua_setmetatable(L, -2); // Add object reference // This should be userdata with metatable ObjectRef objectrefGet(L, id); luaL_checktype(L, -1, LUA_TUSERDATA); if (!luaL_checkudata(L, -1, "ObjectRef")) luaL_typerror(L, -1, "ObjectRef"); lua_setfield(L, -2, "object"); // core.luaentities[id] = object lua_getglobal(L, "core"); lua_getfield(L, -1, "luaentities"); luaL_checktype(L, -1, LUA_TTABLE); lua_pushnumber(L, id); // Push id lua_pushvalue(L, object); // Copy object to top of stack lua_settable(L, -3); return true; }