// place_schematic(p, schematic, rotation) int ModApiBasic::l_place_schematic(lua_State *L) { DecoSchematic dschem; Map *map = &(getEnv(L)->getMap()); INodeDefManager *ndef = getServer(L)->getNodeDefManager(); v3s16 p = read_v3s16(L, 1); if (!read_schematic(L, 2, &dschem, getServer(L))) return 0; Rotation rot = ROTATE_0; if (lua_isstring(L, 3)) string_to_enum(es_Rotation, (int &)rot, std::string(lua_tostring(L, 3))); dschem.rotation = rot; if (!dschem.filename.empty()) { if (!dschem.loadSchematicFile()) { errorstream << "place_schematic: failed to load schematic file '" << dschem.filename << "'" << std::endl; return 0; } dschem.resolveNodeNames(ndef); } dschem.placeStructure(map, p); return 1; }
// place_schematic(p, schematic, rotation, replacement) int ModApiMapgen::l_place_schematic(lua_State *L) { Schematic schem; Map *map = &(getEnv(L)->getMap()); INodeDefManager *ndef = getServer(L)->getNodeDefManager(); //// Read position v3s16 p = read_v3s16(L, 1); //// Read rotation int rot = ROTATE_0; if (lua_isstring(L, 3)) string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3))); //// Read force placement bool force_placement = true; if (lua_isboolean(L, 5)) force_placement = lua_toboolean(L, 5); //// Read node replacements std::map<std::string, std::string> replace_names; if (lua_istable(L, 4)) read_schematic_replacements(L, replace_names, 4); //// Read schematic if (!get_schematic(L, 2, &schem, ndef, replace_names)) { errorstream << "place_schematic: failed to get schematic" << std::endl; return 0; } schem.placeStructure(map, p, 0, (Rotation)rot, force_placement, ndef); return 1; }
int getenumfield(lua_State *L, int table, const char *fieldname, const EnumString *spec, int default_) { int result = default_; string_to_enum(spec, result, getstringfield_default(L, table, fieldname, "")); return result; }
// place_schematic(p, schematic, rotation, replacement) int ModApiMapgen::l_place_schematic(lua_State *L) { DecoSchematic dschem; Map *map = &(getEnv(L)->getMap()); INodeDefManager *ndef = getServer(L)->getNodeDefManager(); v3s16 p = read_v3s16(L, 1); if (!read_schematic(L, 2, &dschem, getServer(L))) return 0; int rot = ROTATE_0; if (lua_isstring(L, 3)) string_to_enum(es_Rotation, rot, std::string(lua_tostring(L, 3))); dschem.rotation = (Rotation)rot; if (lua_istable(L, 4)) { lua_pushnil(L); while (lua_next(L, 4) != 0) { // key at index -2 and value at index -1 lua_rawgeti(L, -1, 1); std::string replace_from = lua_tostring(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); std::string replace_to = lua_tostring(L, -1); lua_pop(L, 1); dschem.replacements[replace_from] = replace_to; // removes value, keeps key for next iteration lua_pop(L, 1); } } bool force_placement = true; if (lua_isboolean(L, 5)) force_placement = lua_toboolean(L, 5); if (!dschem.filename.empty()) { if (!dschem.loadSchematicFile()) { errorstream << "place_schematic: failed to load schematic file '" << dschem.filename << "'" << std::endl; return 0; } dschem.resolveNodeNames(ndef); } dschem.placeStructure(map, p, force_placement); return 1; }
// serialize_schematic(schematic, format, options={...}) int ModApiMapgen::l_serialize_schematic(lua_State *L) { NO_MAP_LOCK_REQUIRED; SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr; //// Read options bool use_comments = getboolfield_default(L, 3, "lua_use_comments", false); u32 indent_spaces = getintfield_default(L, 3, "lua_num_indent_spaces", 0); //// Get schematic bool was_loaded = false; Schematic *schem = (Schematic *)get_objdef(L, 1, schemmgr); if (!schem) { schem = load_schematic(L, 1, NULL, NULL); was_loaded = true; } if (!schem) { errorstream << "serialize_schematic: failed to get schematic" << std::endl; return 0; } //// Read format of definition to save as int schem_format = SCHEM_FMT_MTS; const char *enumstr = lua_tostring(L, 2); if (enumstr) string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr)); //// Serialize to binary string std::ostringstream os(std::ios_base::binary); switch (schem_format) { case SCHEM_FMT_MTS: schem->serializeToMts(&os, schem->m_nodenames); break; case SCHEM_FMT_LUA: schem->serializeToLua(&os, schem->m_nodenames, use_comments, indent_spaces); break; default: return 0; } if (was_loaded) delete schem; std::string ser = os.str(); lua_pushlstring(L, ser.c_str(), ser.length()); return 1; }
static audio_output_flags_t parse_flag_names(char *name) { uint32_t flag = 0; char *flag_name = strtok(name, "|"); while (flag_name != NULL) { if (strlen(flag_name) != 0) { flag |= string_to_enum(s_flag_name_to_enum_table, ARRAY_SIZE(s_flag_name_to_enum_table), flag_name); } flag_name = strtok(NULL, "|"); } ALOGV("parse_flag_names: flag - %d", flag); return (audio_output_flags_t)flag; }
int ModApiMapgen::l_place_schematic_on_vmanip(lua_State *L) { NO_MAP_LOCK_REQUIRED; SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr; //// Read VoxelManip object MMVManip *vm = LuaVoxelManip::checkobject(L, 1)->vm; //// Read position v3s16 p = check_v3s16(L, 2); //// Read rotation int rot = ROTATE_0; const char *enumstr = lua_tostring(L, 4); if (enumstr) string_to_enum(es_Rotation, rot, std::string(enumstr)); //// Read force placement bool force_placement = true; if (lua_isboolean(L, 6)) force_placement = lua_toboolean(L, 6); //// Read node replacements StringMap replace_names; if (lua_istable(L, 5)) read_schematic_replacements(L, 5, &replace_names); //// Read schematic Schematic *schem = get_or_load_schematic(L, 3, schemmgr, &replace_names); if (!schem) { errorstream << "place_schematic: failed to get schematic" << std::endl; return 0; } bool schematic_did_fit = schem->placeOnVManip( vm, p, 0, (Rotation)rot, force_placement); lua_pushboolean(L, schematic_did_fit); return 1; }
// serialize_schematic(schematic, format, options={...}) int ModApiMapgen::l_serialize_schematic(lua_State *L) { SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr; //// Read options NodeResolveMethod resolve_method = (NodeResolveMethod)getenumfield(L, 3, "node_resolve_method", es_NodeResolveMethod, NODE_RESOLVE_NONE); bool register_on_load = getboolfield_default(L, 3, "register_on_load", false); bool use_comments = getboolfield_default(L, 3, "use_lua_comments", false); //// Read schematic Schematic *schem = get_or_load_schematic(L, 1, schemmgr, NULL, register_on_load, resolve_method); if (!schem) { errorstream << "serialize_schematic: failed to get schematic" << std::endl; return 0; } //// Read format of definition to save as int schem_format = SCHEM_FMT_MTS; const char *enumstr = lua_tostring(L, 2); if (enumstr) string_to_enum(es_SchematicFormatType, schem_format, std::string(enumstr)); //// Serialize to binary string std::ostringstream os(std::ios_base::binary); switch (schem_format) { case SCHEM_FMT_MTS: schem->serializeToMts(&os); break; case SCHEM_FMT_LUA: schem->serializeToLua(&os, use_comments); break; default: return 0; } std::string ser = os.str(); lua_pushlstring(L, ser.c_str(), ser.length()); return 1; }
// place_schematic(p, schematic, rotation, replacement) int ModApiMapgen::l_place_schematic(lua_State *L) { MAP_LOCK_REQUIRED; GET_ENV_PTR; ServerMap *map = &(env->getServerMap()); SchematicManager *schemmgr = getServer(L)->getEmergeManager()->schemmgr; //// Read position v3s16 p = check_v3s16(L, 1); //// Read rotation int rot = ROTATE_0; const char *enumstr = lua_tostring(L, 3); if (enumstr) string_to_enum(es_Rotation, rot, std::string(enumstr)); //// Read force placement bool force_placement = true; if (lua_isboolean(L, 5)) force_placement = lua_toboolean(L, 5); //// Read node replacements StringMap replace_names; if (lua_istable(L, 4)) read_schematic_replacements(L, 4, &replace_names); //// Read schematic Schematic *schem = get_or_load_schematic(L, 2, schemmgr, &replace_names); if (!schem) { errorstream << "place_schematic: failed to get schematic" << std::endl; return 0; } schem->placeOnMap(map, p, 0, (Rotation)rot, force_placement); lua_pushboolean(L, true); return 1; }
static void parse_format_names(char *name, struct streams_output_cfg *so_info) { struct stream_format *sf_info = NULL; char *str = strtok(name, "|"); if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) return; list_init(&so_info->format_list); while (str != NULL) { audio_format_t format = (audio_format_t)string_to_enum(s_format_name_to_enum_table, ARRAY_SIZE(s_format_name_to_enum_table), str); ALOGV("%s: format - %d", __func__, format); if (format != 0) { sf_info = (struct stream_format *)calloc(1, sizeof(struct stream_format)); if (sf_info == NULL) break; /* return whatever was parsed */ sf_info->format = format; list_add_tail(&so_info->format_list, &sf_info->list); } str = strtok(NULL, "|"); } }
// get_mapgen_object(objectname) // returns the requested object used during map generation int ModApiMapgen::l_get_mapgen_object(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *mgobjstr = lua_tostring(L, 1); int mgobjint; if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : "")) return 0; enum MapgenObject mgobj = (MapgenObject)mgobjint; EmergeManager *emerge = getServer(L)->getEmergeManager(); Mapgen *mg = emerge->getCurrentMapgen(); if (!mg) throw LuaError("Must only be called in a mapgen thread!"); size_t maplen = mg->csize.X * mg->csize.Z; switch (mgobj) { case MGOBJ_VMANIP: { MMVManip *vm = mg->vm; // VoxelManip object LuaVoxelManip *o = new LuaVoxelManip(vm, true); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, "VoxelManip"); lua_setmetatable(L, -2); // emerged min pos push_v3s16(L, vm->m_area.MinEdge); // emerged max pos push_v3s16(L, vm->m_area.MaxEdge); return 3; } case MGOBJ_HEIGHTMAP: { if (!mg->heightmap) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushinteger(L, mg->heightmap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_BIOMEMAP: { if (!mg->biomegen) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushinteger(L, mg->biomegen->biomemap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_HEATMAP: { if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushnumber(L, bg->heatmap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_HUMIDMAP: { if (!mg->biomegen || mg->biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; BiomeGenOriginal *bg = (BiomeGenOriginal *)mg->biomegen; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushnumber(L, bg->humidmap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_GENNOTIFY: { std::map<std::string, std::vector<v3s16> >event_map; std::map<std::string, std::vector<v3s16> >::iterator it; mg->gennotify.getEvents(event_map); lua_newtable(L); for (it = event_map.begin(); it != event_map.end(); ++it) { lua_newtable(L); for (size_t j = 0; j != it->second.size(); j++) { push_v3s16(L, it->second[j]); lua_rawseti(L, -2, j + 1); } lua_setfield(L, -2, it->first.c_str()); } return 1; } } return 0; }
// get_mapgen_object(objectname) // returns the requested object used during map generation int ModApiMapgen::l_get_mapgen_object(lua_State *L) { const char *mgobjstr = lua_tostring(L, 1); int mgobjint; if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : "")) return 0; enum MapgenObject mgobj = (MapgenObject)mgobjint; EmergeManager *emerge = getServer(L)->getEmergeManager(); Mapgen *mg = emerge->getCurrentMapgen(); if (!mg) return 0; size_t maplen = mg->csize.X * mg->csize.Z; switch (mgobj) { case MGOBJ_VMANIP: { MMVManip *vm = mg->vm; // VoxelManip object LuaVoxelManip *o = new LuaVoxelManip(vm, true); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, "VoxelManip"); lua_setmetatable(L, -2); // emerged min pos push_v3s16(L, vm->m_area.MinEdge); // emerged max pos push_v3s16(L, vm->m_area.MaxEdge); return 3; } case MGOBJ_HEIGHTMAP: { if (!mg->heightmap) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushinteger(L, mg->heightmap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_BIOMEMAP: { if (!mg->biomemap) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushinteger(L, mg->biomemap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_HEATMAP: { // Mapgen V7 specific objects case MGOBJ_HUMIDMAP: if (strcmp(emerge->params.mg_name.c_str(), "v7")) return 0; MapgenV7 *mgv7 = (MapgenV7 *)mg; float *arr = (mgobj == MGOBJ_HEATMAP) ? mgv7->noise_heat->result : mgv7->noise_humidity->result; if (!arr) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushnumber(L, arr[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_GENNOTIFY: { std::map<std::string, std::vector<v3s16> >event_map; std::map<std::string, std::vector<v3s16> >::iterator it; mg->gennotify.getEvents(event_map); lua_newtable(L); for (it = event_map.begin(); it != event_map.end(); ++it) { lua_newtable(L); for (size_t j = 0; j != it->second.size(); j++) { push_v3s16(L, it->second[j]); lua_rawseti(L, -2, j + 1); } lua_setfield(L, -2, it->first.c_str()); } return 1; } } return 0; }
// minetest.get_mapgen_object(objectname) // returns the requested object used during map generation int ModApiMapgen::l_get_mapgen_object(lua_State *L) { const char *mgobjstr = lua_tostring(L, 1); int mgobjint; if (!string_to_enum(es_MapgenObject, mgobjint, mgobjstr ? mgobjstr : "")) return 0; enum MapgenObject mgobj = (MapgenObject)mgobjint; EmergeManager *emerge = getServer(L)->getEmergeManager(); Mapgen *mg = emerge->getCurrentMapgen(); if (!mg) return 0; size_t maplen = mg->csize.X * mg->csize.Z; switch (mgobj) { case MGOBJ_VMANIP: { ManualMapVoxelManipulator *vm = mg->vm; // VoxelManip object LuaVoxelManip *o = new LuaVoxelManip(vm, true); *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, "VoxelManip"); lua_setmetatable(L, -2); // emerged min pos push_v3s16(L, vm->m_area.MinEdge); // emerged max pos push_v3s16(L, vm->m_area.MaxEdge); return 3; } case MGOBJ_HEIGHTMAP: { if (!mg->heightmap) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushinteger(L, mg->heightmap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_BIOMEMAP: { if (!mg->biomemap) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushinteger(L, mg->biomemap[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_HEATMAP: { // Mapgen V7 specific objects case MGOBJ_HUMIDMAP: if (strcmp(emerge->params.mg_name.c_str(), "v7")) return 0; MapgenV7 *mgv7 = (MapgenV7 *)mg; float *arr = (mgobj == MGOBJ_HEATMAP) ? mgv7->noise_heat->result : mgv7->noise_humidity->result; if (!arr) return 0; lua_newtable(L); for (size_t i = 0; i != maplen; i++) { lua_pushnumber(L, arr[i]); lua_rawseti(L, -2, i + 1); } return 1; } case MGOBJ_GENNOTIFY: { lua_newtable(L); for (int i = 0; flagdesc_gennotify[i].name; i++) { if (!(emerge->gennotify & flagdesc_gennotify[i].flag)) continue; std::vector<v3s16> *posvec = mg->gen_notifications[i]; if (!posvec) return 0; lua_newtable(L); for (unsigned int j = 0; j != posvec->size(); j++) { push_v3s16(L, (*posvec)[j]); lua_rawseti(L, -2, j + 1); } lua_setfield(L, -2, flagdesc_gennotify[i].name); posvec->clear(); } return 1; } } return 0; }
// hud_change(self, id, stat, data) int ObjectRef::l_hud_change(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); Player *player = getplayer(ref); if (player == NULL) return 0; u32 id = lua_isnumber(L, 2) ? lua_tonumber(L, 2) : -1; HudElement *e = player->getHud(id); if (!e) return 0; HudElementStat stat = HUD_STAT_NUMBER; if (lua_isstring(L, 3)) { int statint; std::string statstr = lua_tostring(L, 3); stat = string_to_enum(es_HudElementStat, statint, statstr) ? (HudElementStat)statint : HUD_STAT_NUMBER; } void *value = NULL; switch (stat) { case HUD_STAT_POS: e->pos = read_v2f(L, 4); value = &e->pos; break; case HUD_STAT_NAME: e->name = luaL_checkstring(L, 4); value = &e->name; break; case HUD_STAT_SCALE: e->scale = read_v2f(L, 4); value = &e->scale; break; case HUD_STAT_TEXT: e->text = luaL_checkstring(L, 4); value = &e->text; break; case HUD_STAT_NUMBER: e->number = luaL_checknumber(L, 4); value = &e->number; break; case HUD_STAT_ITEM: e->item = luaL_checknumber(L, 4); value = &e->item; break; case HUD_STAT_DIR: e->dir = luaL_checknumber(L, 4); value = &e->dir; break; case HUD_STAT_ALIGN: e->align = read_v2f(L, 4); value = &e->align; break; case HUD_STAT_OFFSET: e->offset = read_v2f(L, 4); value = &e->offset; break; case HUD_STAT_WORLD_POS: e->world_pos = read_v3f(L, 4); value = &e->world_pos; break; case HUD_STAT_SIZE: e->size = read_v2s32(L, 4); value = &e->size; break; } getServer(L)->hudChange(player, id, stat, value); lua_pushboolean(L, true); return 1; }