// minetest.get_objects_inside_radius(pos, radius) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) { // Get the table insert function lua_getglobal(L, "table"); lua_getfield(L, -1, "insert"); int table_insert = lua_gettop(L); GET_ENV_PTR; // Do it v3f pos = checkFloatPos(L, 1); float radius = luaL_checknumber(L, 2) * BS; std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); lua_newtable(L); int table = lua_gettop(L); for(std::set<u16>::const_iterator i = ids.begin(); i != ids.end(); i++){ ServerActiveObject *obj = env->getActiveObject(*i); // Insert object reference into table lua_pushvalue(L, table_insert); lua_pushvalue(L, table); getScriptApiBase(L)->objectrefGetOrCreate(obj); if(lua_pcall(L, 2, 0, 0)) script_error(L, "error: %s", lua_tostring(L, -1)); } return 1; }
// get_attach(self) int ObjectRef::l_get_attach(lua_State *L) { GET_ENV_PTR; ObjectRef *ref = checkobject(L, 1); ServerActiveObject *co = getobject(ref); if (co == NULL) return 0; // Do it int parent_id = 0; std::string bone; v3f position = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0); co->getAttachment(&parent_id, &bone, &position, &rotation); if (!parent_id) return 0; ServerActiveObject *parent = env->getActiveObject(parent_id); getScriptApiBase(L)->objectrefGetOrCreate(L, parent); lua_pushlstring(L, bone.c_str(), bone.size()); push_v3f(L, position); push_v3f(L, rotation); return 4; }
// set_last_run_mod(modname) int ModApiServer::l_set_last_run_mod(lua_State *L) { NO_MAP_LOCK_REQUIRED; #ifdef SCRIPTAPI_DEBUG const char *mod = lua_tostring(L, 1); getScriptApiBase(L)->setOriginDirect(mod); //printf(">>>> last mod set from Lua: %s\n", mod); #endif return 0; }
// set_last_run_mod(modname) int ModApiClient::l_set_last_run_mod(lua_State *L) { if (!lua_isstring(L, 1)) return 0; const char *mod = lua_tostring(L, 1); getScriptApiBase(L)->setOriginDirect(mod); lua_pushboolean(L, true); return 1; }
// get_last_run_mod() int ModApiClient::l_get_last_run_mod(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); const char *current_mod = lua_tostring(L, -1); if (current_mod == NULL || current_mod[0] == '\0') { lua_pop(L, 1); lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str()); } return 1; }
// add_entity(pos, entityname) -> ObjectRef or nil // pos = {x=num, y=num, z=num} int ModApiEnvMod::l_add_entity(lua_State *L) { GET_ENV_PTR; // pos v3f pos = checkFloatPos(L, 1); // content const char *name = luaL_checkstring(L, 2); // Do it ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, ""); int objectid = env->addActiveObject(obj); // If failed to add, return nothing (reads as nil) if(objectid == 0) return 0; // Return ObjectRef getScriptApiBase(L)->objectrefGetOrCreate(obj); return 1; }
// get_objects_inside_radius(pos, radius) int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L) { GET_ENV_PTR; // Do it v3f pos = checkFloatPos(L, 1); float radius = luaL_checknumber(L, 2) * BS; std::set<u16> ids = env->getObjectsInsideRadius(pos, radius); ScriptApiBase *script = getScriptApiBase(L); lua_createtable(L, ids.size(), 0); std::set<u16>::const_iterator iter = ids.begin(); for(u32 i = 0; iter != ids.end(); iter++) { ServerActiveObject *obj = env->getActiveObject(*iter); // Insert object reference into table script->objectrefGetOrCreate(obj); lua_rawseti(L, -2, ++i); } return 1; }
// emerge_area(p1, p2, [callback, context]) // emerge mapblocks in area p1..p2, calls callback with context upon completion int ModApiEnvMod::l_emerge_area(lua_State *L) { GET_ENV_PTR; EmergeCompletionCallback callback = NULL; ScriptCallbackState *state = NULL; EmergeManager *emerge = getServer(L)->getEmergeManager(); v3s16 bpmin = getNodeBlockPos(read_v3s16(L, 1)); v3s16 bpmax = getNodeBlockPos(read_v3s16(L, 2)); sortBoxVerticies(bpmin, bpmax); size_t num_blocks = VoxelArea(bpmin, bpmax).getVolume(); assert(num_blocks != 0); if (lua_isfunction(L, 3)) { callback = LuaEmergeAreaCallback; lua_pushvalue(L, 3); int callback_ref = luaL_ref(L, LUA_REGISTRYINDEX); lua_pushvalue(L, 4); int args_ref = luaL_ref(L, LUA_REGISTRYINDEX); state = new ScriptCallbackState; state->script = getServer(L)->getScriptIface(); state->callback_ref = callback_ref; state->args_ref = args_ref; state->refcount = num_blocks; state->origin = getScriptApiBase(L)->getOrigin(); } for (s16 z = bpmin.Z; z <= bpmax.Z; z++) for (s16 y = bpmin.Y; y <= bpmax.Y; y++) for (s16 x = bpmin.X; x <= bpmax.X; x++) { emerge->enqueueBlockEmergeEx(v3s16(x, y, z), PEER_ID_INEXISTENT, BLOCK_EMERGE_ALLOW_GEN | BLOCK_EMERGE_FORCE_QUEUE, callback, state); } return 0; }
// get_player_by_name(name) int ModApiEnvMod::l_get_player_by_name(lua_State *L) { GET_ENV_PTR; // Do it const char *name = luaL_checkstring(L, 1); Player *player = env->getPlayer(name); if(player == NULL){ lua_pushnil(L); return 1; } PlayerSAO *sao = player->getPlayerSAO(); if(sao == NULL){ lua_pushnil(L); return 1; } // Put player on stack getScriptApiBase(L)->objectrefGetOrCreate(sao); return 1; }
GUIEngine *ModApiBase::getGuiEngine(lua_State *L) { return getScriptApiBase(L)->getGuiEngine(); }
Environment *ModApiBase::getEnv(lua_State *L) { return getScriptApiBase(L)->getEnv(); }
IGameDef *ModApiBase::getGameDef(lua_State *L) { return getScriptApiBase(L)->getGameDef(); }
Client *ModApiBase::getClient(lua_State *L) { return getScriptApiBase(L)->getClient(); }
Server *ModApiBase::getServer(lua_State *L) { return getScriptApiBase(L)->getServer(); }