From 7616537bc071bc93f8d36c84b94603528be1efb0 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Wed, 12 Nov 2014 23:01:13 -0500 Subject: Add Generator Element Management framework Add BiomeManager, OreManager, DecorationManager, and SchematicManager --- src/script/common/c_content.cpp | 148 ++++++++++++++++++++++------------------ src/script/common/c_content.h | 12 ++-- src/script/lua_api/l_mapgen.cpp | 129 +++++++++++++++++----------------- src/script/lua_api/l_mapgen.h | 3 +- 4 files changed, 157 insertions(+), 135 deletions(-) (limited to 'src/script') diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 88bbedec5..0e1e608c4 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1009,86 +1009,98 @@ bool read_noiseparams_nc(lua_State *L, int index, NoiseParams *np) } /******************************************************************************/ -bool read_schematic(lua_State *L, int index, DecoSchematic *dschem, Server *server) { + +bool get_schematic(lua_State *L, int index, Schematic *schem, + INodeDefManager *ndef, std::map &replace_names) +{ if (index < 0) index = lua_gettop(L) + 1 + index; - INodeDefManager *ndef = server->getNodeDefManager(); - if (lua_istable(L, index)) { - lua_getfield(L, index, "size"); - v3s16 size = read_v3s16(L, -1); - lua_pop(L, 1); - - int numnodes = size.X * size.Y * size.Z; - MapNode *schemdata = new MapNode[numnodes]; - int i = 0; - - // Get schematic data - lua_getfield(L, index, "data"); - luaL_checktype(L, -1, LUA_TTABLE); - - lua_pushnil(L); - while (lua_next(L, -2)) { - if (i < numnodes) { - // same as readnode, except param1 default is MTSCHEM_PROB_CONST - lua_getfield(L, -1, "name"); - const char *name = luaL_checkstring(L, -1); - lua_pop(L, 1); - - u8 param1; - lua_getfield(L, -1, "param1"); - param1 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : MTSCHEM_PROB_ALWAYS; - lua_pop(L, 1); + return read_schematic(L, index, schem, ndef, replace_names); + } else if (lua_isstring(L, index)) { + NodeResolver *resolver = ndef->getResolver(); + const char *filename = lua_tostring(L, index); + return schem->loadSchematicFromFile(filename, resolver, replace_names); + } else { + return false; + } +} + +bool read_schematic(lua_State *L, int index, Schematic *schem, + INodeDefManager *ndef, std::map &replace_names) +{ + //// Get schematic size + lua_getfield(L, index, "size"); + v3s16 size = read_v3s16(L, -1); + lua_pop(L, 1); + + //// Get schematic data + lua_getfield(L, index, "data"); + luaL_checktype(L, -1, LUA_TTABLE); - u8 param2; - lua_getfield(L, -1, "param2"); - param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0; - lua_pop(L, 1); - - schemdata[i] = MapNode(ndef, name, param1, param2); - } - - i++; + int numnodes = size.X * size.Y * size.Z; + MapNode *schemdata = new MapNode[numnodes]; + int i = 0; + + lua_pushnil(L); + while (lua_next(L, -2)) { + if (i < numnodes) { + // same as readnode, except param1 default is MTSCHEM_PROB_CONST + lua_getfield(L, -1, "name"); + std::string name = luaL_checkstring(L, -1); + lua_pop(L, 1); + + u8 param1; + lua_getfield(L, -1, "param1"); + param1 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : MTSCHEM_PROB_ALWAYS; lua_pop(L, 1); + + u8 param2; + lua_getfield(L, -1, "param2"); + param2 = !lua_isnil(L, -1) ? lua_tonumber(L, -1) : 0; + lua_pop(L, 1); + + std::map::iterator it; + it = replace_names.find(name); + if (it != replace_names.end()) + name = it->second; + + schemdata[i] = MapNode(ndef, name, param1, param2); } - if (i != numnodes) { - errorstream << "read_schematic: incorrect number of " - "nodes provided in raw schematic data (got " << i << - ", expected " << numnodes << ")." << std::endl; - return false; - } + i++; + lua_pop(L, 1); + } - u8 *sliceprobs = new u8[size.Y]; - for (i = 0; i != size.Y; i++) - sliceprobs[i] = MTSCHEM_PROB_ALWAYS; - - // Get Y-slice probability values (if present) - lua_getfield(L, index, "yslice_prob"); - if (lua_istable(L, -1)) { - lua_pushnil(L); - while (lua_next(L, -2)) { - if (getintfield(L, -1, "ypos", i) && i >= 0 && i < size.Y) { - sliceprobs[i] = getintfield_default(L, -1, - "prob", MTSCHEM_PROB_ALWAYS); - } - lua_pop(L, 1); - } - } + if (i != numnodes) { + errorstream << "read_schematic: incorrect number of " + "nodes provided in raw schematic data (got " << i << + ", expected " << numnodes << ")." << std::endl; + return false; + } - dschem->size = size; - dschem->schematic = schemdata; - dschem->slice_probs = sliceprobs; + //// Get Y-slice probability values (if present) + u8 *slice_probs = new u8[size.Y]; + for (i = 0; i != size.Y; i++) + slice_probs[i] = MTSCHEM_PROB_ALWAYS; - } else if (lua_isstring(L, index)) { - dschem->filename = std::string(lua_tostring(L, index)); - } else { - errorstream << "read_schematic: missing schematic " - "filename or raw schematic data" << std::endl; - return false; + lua_getfield(L, index, "yslice_prob"); + if (lua_istable(L, -1)) { + lua_pushnil(L); + while (lua_next(L, -2)) { + if (getintfield(L, -1, "ypos", i) && i >= 0 && i < size.Y) { + slice_probs[i] = getintfield_default(L, -1, + "prob", MTSCHEM_PROB_ALWAYS); + } + lua_pop(L, 1); + } } - + + schem->flags = 0; + schem->size = size; + schem->schemdata = schemdata; + schem->slice_probs = slice_probs; return true; } diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index f48c673bd..5b4dff2bd 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -59,7 +59,7 @@ struct DigParams; struct HitParams; struct EnumString; struct NoiseParams; -class DecoSchematic; +class Schematic; ContentFeatures read_content_features (lua_State *L, int index); @@ -151,10 +151,14 @@ NoiseParams* read_noiseparams (lua_State *L, int index); bool read_noiseparams_nc (lua_State *L, int index, NoiseParams *np); - +bool get_schematic (lua_State *L, int index, + Schematic *schem, + INodeDefManager *ndef, + std::map &replace_names); bool read_schematic (lua_State *L, int index, - DecoSchematic *dschem, - Server *server); + Schematic *dschem, + INodeDefManager *ndef, + std::map &replace_names); void luaentity_get (lua_State *L,u16 id); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 2ed9fadf9..71b1f4740 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -25,13 +25,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/serialize.h" #include "server.h" #include "environment.h" -#include "mg_biome.h" #include "emerge.h" #include "mg_biome.h" #include "mg_ore.h" #include "mg_decoration.h" #include "mg_schematic.h" #include "mapgen_v7.h" +#include "settings.h" #include "main.h" #include "log.h" @@ -85,7 +85,7 @@ struct EnumString ModApiMapgen::es_Rotation[] = static void read_schematic_replacements(lua_State *L, - std::map replace_names, int index) + std::map &replace_names, int index) { lua_pushnil(L); while (lua_next(L, index)) { @@ -310,13 +310,13 @@ int ModApiMapgen::l_register_biome(lua_State *L) luaL_checktype(L, index, LUA_TTABLE); NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver(); - BiomeDefManager *bmgr = getServer(L)->getEmergeManager()->biomedef; + BiomeManager *bmgr = getServer(L)->getEmergeManager()->biomemgr; enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index, "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL); - Biome *b = bmgr->createBiome(terrain); + Biome *b = bmgr->create(terrain); - b->name = getstringfield_default(L, index, "name", ""); + b->name = getstringfield_default(L, index, "name", ""); b->depth_top = getintfield_default(L, index, "depth_top", 1); b->depth_filler = getintfield_default(L, index, "depth_filler", 3); b->height_min = getintfield_default(L, index, "height_min", 0); @@ -325,7 +325,8 @@ int ModApiMapgen::l_register_biome(lua_State *L) b->humidity_point = getfloatfield_default(L, index, "humidity_point", 0.); b->flags = 0; //reserved - if (!bmgr->addBiome(b)) { + u32 id = bmgr->add(b); + if (id == (u32)-1) { delete b; return 0; } @@ -344,7 +345,8 @@ int ModApiMapgen::l_register_biome(lua_State *L) verbosestream << "register_biome: " << b->name << std::endl; - return 0; + lua_pushinteger(L, id); + return 1; } // register_decoration({lots of stuff}) @@ -353,20 +355,22 @@ int ModApiMapgen::l_register_decoration(lua_State *L) int index = 1; luaL_checktype(L, index, LUA_TTABLE); - EmergeManager *emerge = getServer(L)->getEmergeManager(); - BiomeDefManager *bdef = emerge->biomedef; - NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver(); + INodeDefManager *ndef = getServer(L)->getNodeDefManager(); + NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver(); + DecorationManager *decomgr = getServer(L)->getEmergeManager()->decomgr; + BiomeManager *biomemgr = getServer(L)->getEmergeManager()->biomemgr; enum DecorationType decotype = (DecorationType)getenumfield(L, index, "deco_type", es_DecorationType, -1); - Decoration *deco = createDecoration(decotype); + Decoration *deco = decomgr->create(decotype); if (!deco) { errorstream << "register_decoration: decoration placement type " << decotype << " not implemented"; return 0; } + deco->name = getstringfield_default(L, index, "name", ""); deco->fill_ratio = getfloatfield_default(L, index, "fill_ratio", 0.02); deco->sidelen = getintfield_default(L, index, "sidelen", 8); if (deco->sidelen <= 0) { @@ -391,9 +395,11 @@ int ModApiMapgen::l_register_decoration(lua_State *L) std::vector biome_list; getstringlistfield(L, index, "biomes", biome_list); for (size_t i = 0; i != biome_list.size(); i++) { - u8 biomeid = bdef->getBiomeIdByName(biome_list[i]); - if (biomeid) - deco->biomes.insert(biomeid); + Biome *b = (Biome *)biomemgr->getByName(biome_list[i]); + if (!b) + continue; + + deco->biomes.insert(b->id); } //// Handle decoration type-specific parameters @@ -403,7 +409,7 @@ int ModApiMapgen::l_register_decoration(lua_State *L) success = regDecoSimple(L, resolver, (DecoSimple *)deco); break; case DECO_SCHEMATIC: - success = regDecoSchematic(L, resolver, (DecoSchematic *)deco); + success = regDecoSchematic(L, ndef, (DecoSchematic *)deco); break; case DECO_LSYSTEM: break; @@ -414,12 +420,14 @@ int ModApiMapgen::l_register_decoration(lua_State *L) return 0; } - emerge->decorations.push_back(deco); - - verbosestream << "register_decoration: decoration '" << deco->getName() - << "' registered" << std::endl; + u32 id = decomgr->add(deco); + if (id == (u32)-1) { + delete deco; + return 0; + } - return 0; + lua_pushinteger(L, id); + return 1; } bool ModApiMapgen::regDecoSimple(lua_State *L, @@ -461,8 +469,8 @@ bool ModApiMapgen::regDecoSimple(lua_State *L, return true; } -bool ModApiMapgen::regDecoSchematic(lua_State *L, - NodeResolver *resolver, DecoSchematic *deco) +bool ModApiMapgen::regDecoSchematic(lua_State *L, INodeDefManager *ndef, + DecoSchematic *deco) { int index = 1; @@ -478,19 +486,16 @@ bool ModApiMapgen::regDecoSchematic(lua_State *L, read_schematic_replacements(L, replace_names, lua_gettop(L)); lua_pop(L, 1); + Schematic *schem = new Schematic; lua_getfield(L, index, "schematic"); - if (!read_schematic(L, -1, deco, getServer(L))) { + if (!get_schematic(L, -1, schem, ndef, replace_names)) { lua_pop(L, 1); + delete schem; return false; } lua_pop(L, 1); - if (!deco->filename.empty() && - !deco->loadSchematicFile(resolver, replace_names)) { - errorstream << "register_decoration: failed to load schematic" - " file '" << deco->filename << "'" << std::endl; - return false; - } + deco->schematic = schem; return true; } @@ -501,25 +506,25 @@ int ModApiMapgen::l_register_ore(lua_State *L) int index = 1; luaL_checktype(L, index, LUA_TTABLE); - EmergeManager *emerge = getServer(L)->getEmergeManager(); NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver(); + OreManager *oremgr = getServer(L)->getEmergeManager()->oremgr; enum OreType oretype = (OreType)getenumfield(L, index, "ore_type", es_OreType, ORE_SCATTER); - Ore *ore = createOre(oretype); + Ore *ore = oremgr->create(oretype); if (!ore) { - errorstream << "register_ore: ore_type " - << oretype << " not implemented"; + errorstream << "register_ore: ore_type " << oretype << " not implemented"; return 0; } + ore->name = getstringfield_default(L, index, "name", ""); ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0); ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1); ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1); ore->clust_size = getintfield_default(L, index, "clust_size", 0); ore->height_min = getintfield_default(L, index, "height_min", 0); ore->height_max = getintfield_default(L, index, "height_max", 0); - ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0.); + ore->nthresh = getfloatfield_default(L, index, "noise_threshhold", 0); ore->noise = NULL; ore->flags = 0; @@ -536,6 +541,12 @@ int ModApiMapgen::l_register_ore(lua_State *L) ore->np = read_noiseparams(L, -1); lua_pop(L, 1); + u32 id = oremgr->add(ore); + if (id == (u32)-1) { + delete ore; + return 0; + } + std::vector wherein_names; getstringlistfield(L, index, "wherein", wherein_names); for (size_t i = 0; i != wherein_names.size(); i++) @@ -544,17 +555,14 @@ int ModApiMapgen::l_register_ore(lua_State *L) resolver->addNode(getstringfield_default(L, index, "ore", ""), "", CONTENT_AIR, &ore->c_ore); - emerge->ores.push_back(ore); - - //verbosestream << "register_ore: ore '" << ore->ore_name - // << "' registered" << std::endl; - return 0; + lua_pushinteger(L, id); + return 1; } // create_schematic(p1, p2, probability_list, filename) int ModApiMapgen::l_create_schematic(lua_State *L) { - DecoSchematic dschem; + Schematic schem; Map *map = &(getEnv(L)->getMap()); INodeDefManager *ndef = getServer(L)->getNodeDefManager(); @@ -594,20 +602,19 @@ int ModApiMapgen::l_create_schematic(lua_State *L) } } - const char *s = lua_tostring(L, 4); - dschem.filename = std::string(s ? s : ""); + const char *filename = luaL_checkstring(L, 4); - if (!dschem.getSchematicFromMap(map, p1, p2)) { + if (!schem.getSchematicFromMap(map, p1, p2)) { errorstream << "create_schematic: failed to get schematic " "from map" << std::endl; return 0; } - dschem.applyProbabilities(p1, &prob_list, &slice_prob_list); + schem.applyProbabilities(p1, &prob_list, &slice_prob_list); - dschem.saveSchematicFile(ndef); + schem.saveSchematicToFile(filename, ndef); actionstream << "create_schematic: saved schematic file '" - << dschem.filename << "'." << std::endl; + << filename << "'." << std::endl; return 1; } @@ -615,38 +622,36 @@ int ModApiMapgen::l_create_schematic(lua_State *L) // place_schematic(p, schematic, rotation, replacement) int ModApiMapgen::l_place_schematic(lua_State *L) { - DecoSchematic dschem; + Schematic schem; Map *map = &(getEnv(L)->getMap()); - NodeResolver *resolver = getServer(L)->getNodeDefManager()->getResolver(); + INodeDefManager *ndef = getServer(L)->getNodeDefManager(); + //// Read position v3s16 p = read_v3s16(L, 1); - if (!read_schematic(L, 2, &dschem, getServer(L))) - return 0; + //// Read rotation 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; + //// Read force placement + bool force_placement = true; + if (lua_isboolean(L, 5)) + force_placement = lua_toboolean(L, 5); + //// Read node replacements std::map replace_names; if (lua_istable(L, 4)) read_schematic_replacements(L, replace_names, 4); - bool force_placement = true; - if (lua_isboolean(L, 5)) - force_placement = lua_toboolean(L, 5); - - if (!dschem.filename.empty()) { - if (!dschem.loadSchematicFile(resolver, replace_names)) { - errorstream << "place_schematic: failed to load schematic file '" - << dschem.filename << "'" << std::endl; - return 0; - } + //// Read schematic + if (!get_schematic(L, 2, &schem, ndef, replace_names)) { + errorstream << "place_schematic: failed to get schematic" << std::endl; + return 0; } - dschem.placeStructure(map, p, force_placement); + schem.placeStructure(map, p, 0, (Rotation)rot, force_placement, ndef); return 1; } diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index d14471a08..dac0f00a7 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_base.h" +class INodeDefManager; class NodeResolver; class DecoSimple; class DecoSchematic; @@ -60,7 +61,7 @@ private: static bool regDecoSimple(lua_State *L, NodeResolver *resolver, DecoSimple *deco); static bool regDecoSchematic(lua_State *L, - NodeResolver *resolver, DecoSchematic *deco); + INodeDefManager *ndef, DecoSchematic *deco); static struct EnumString es_BiomeTerrainType[]; static struct EnumString es_DecorationType[]; -- cgit v1.2.3