From: kwolekr Date: Sun, 24 Mar 2013 05:43:38 +0000 (-0400) Subject: Add Ore infrastructure and l_register_ore() X-Git-Tag: 0.4.6~22^2~22 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=57cbb8bfd8daaa1b8b1aa876723ff6355d21f7fc;p=oweals%2Fminetest.git Add Ore infrastructure and l_register_ore() --- diff --git a/src/emerge.h b/src/emerge.h index 70b67e731..3d717bce3 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -63,8 +63,9 @@ public: std::map blocks_enqueued; std::map peer_queue_count; - //biome manager + //Mapgen-related structures BiomeDefManager *biomedef; + std::vector ores; EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef); ~EmergeManager(); diff --git a/src/mapgen.cpp b/src/mapgen.cpp index dc6dab6bb..53b5d6867 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -49,6 +49,140 @@ FlagDesc flagdesc_mapgen[] = { /////////////////////////////////////////////////////////////////////////////// +Ore *createOre(OreType type) { + switch (type) { + case ORE_SCATTER: + return new OreScatter; + case ORE_SHEET: + return new OreSheet; + //case ORE_CLAYLIKE: //TODO: implement this! + // return new OreClaylike; + default: + return NULL; + } +} + + +void Ore::resolveNodeNames(INodeDefManager *ndef) { + if (ore == CONTENT_IGNORE) { + ore = ndef->getId(ore_name); + if (ore == CONTENT_IGNORE) { + errorstream << "Ore::resolveNodeNames: ore node '" + << ore_name << "' not defined"; + ore = CONTENT_AIR; + wherein = CONTENT_AIR; + } + } + + if (wherein == CONTENT_IGNORE) { + wherein = ndef->getId(wherein_name); + if (wherein == CONTENT_IGNORE) { + errorstream << "Ore::resolveNodeNames: wherein node '" + << wherein_name << "' not defined"; + ore = CONTENT_AIR; + wherein = CONTENT_AIR; + } + } +} + + +void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) { + if (nmin.Y > height_max || nmax.Y < height_min) + return; + + resolveNodeNames(mg->ndef); + + MapNode n_ore(ore); + ManualMapVoxelManipulator *vm = mg->vm; + PseudoRandom pr(blockseed); + + int ymin = MYMAX(nmin.Y, height_min); + int ymax = MYMIN(nmax.Y, height_max); + if (clust_size >= ymax - ymin + 1) + return; + + int volume = (nmax.X - nmin.X + 1) * + (nmax.Y - nmin.Y + 1) * + (nmax.Z - nmin.Z + 1); + int csize = clust_size; + int orechance = (csize * csize * csize) / clust_num_ores; + int nclusters = volume / clust_scarcity; + + for (int i = 0; i != nclusters; i++) { + int x0 = pr.range(nmin.X, nmax.X - csize + 1); + int y0 = pr.range(ymin, ymax - csize + 1); + int z0 = pr.range(nmin.Z, nmax.Z - csize + 1); + + if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh)) + continue; + + for (int z1 = 0; z1 != csize; z1++) + for (int y1 = 0; y1 != csize; y1++) + for (int x1 = 0; x1 != csize; x1++) { + if (pr.range(1, orechance) != 1) + continue; + + u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1); + if (vm->m_data[i].getContent() == wherein) + vm->m_data[i] = n_ore; + } + } +} + + +void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) { + if (nmin.Y > height_max || nmax.Y < height_min) + return; + + resolveNodeNames(mg->ndef); + + MapNode n_ore(ore); + ManualMapVoxelManipulator *vm = mg->vm; + PseudoRandom pr(blockseed + 4234); + + int ymin = MYMAX(nmin.Y, height_min); + int ymax = MYMIN(nmax.Y, height_max); + + int x0 = nmin.X; + int z0 = nmin.Z; + + int x1 = nmax.X; + int z1 = nmax.Z; + + int max_height = clust_size; + + int y_start = pr.range(ymin, ymax - max_height); + + if (!noise) { + int sx = nmax.X - nmin.X + 1; + int sz = nmax.Z - nmin.Z + 1; + noise = new Noise(np, 0, sx, sz); + } + noise->seed = mg->seed + y_start; + noise->perlinMap2D(x0, z0); + + int index = 0; + for (int z = z0; z != z1; z++) + for (int x = x0; x != x1; x++) { + + if (noise->result[index++] < nthresh) + continue; + + int height = max_height * (1. / pr.range(1, 3)); + int y0 = y_start + pr.range(1, 3) - 1; + int y1 = y0 + height; + for (int y = y0; y != y1; y++) { + u32 i = vm->m_area.index(x, y, z); + if (!vm->m_area.contains(i)) + continue; + + if (vm->m_data[i].getContent() == wherein) + vm->m_data[i] = n_ore; + } + } +} + + void Mapgen::updateLiquid(UniqueQueue *trans_liquid, v3s16 nmin, v3s16 nmax) { bool isliquid, wasliquid; v3s16 em = vm->m_area.getExtent(); diff --git a/src/mapgen.h b/src/mapgen.h index 2e917a3aa..a900985da 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -97,5 +97,48 @@ struct MapgenFactory { virtual MapgenParams *createMapgenParams() = 0; }; +enum OreType { + ORE_SCATTER, + ORE_SHEET, + ORE_CLAYLIKE +}; + +class Ore { +public: + std::string ore_name; + std::string wherein_name; + + content_t ore; + content_t wherein; // the node to be replaced + s16 clust_scarcity; // + s16 clust_num_ores; // how many ore nodes are in a chunk + s16 clust_size; // how large (in nodes) a chunk of ore is + s16 height_min; + s16 height_max; + float nthresh; // threshhold for noise at which an ore is placed + NoiseParams *np; // noise for distribution of clusters (NULL for uniform scattering) + Noise *noise; + + Ore() { + ore = CONTENT_IGNORE; + wherein = CONTENT_IGNORE; + np = NULL; + noise = NULL; + } + + void resolveNodeNames(INodeDefManager *ndef); + virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) = 0; +}; + +class OreScatter : public Ore { + void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax); +}; + +class OreSheet : public Ore { + void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax); +}; + +Ore *createOre(OreType type); + #endif diff --git a/src/mapgen_indev.cpp b/src/mapgen_indev.cpp index 6956fc63f..5d455827a 100644 --- a/src/mapgen_indev.cpp +++ b/src/mapgen_indev.cpp @@ -80,7 +80,9 @@ void NoiseIndev::transformNoiseMapFarScale(float xx, float yy, float zz) { } } -MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params) : MapgenV6(mapgenid, params) { +MapgenIndev::MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge) + : MapgenV6(mapgenid, params, emerge) +{ noiseindev_terrain_base = new NoiseIndev(params->npindev_terrain_base, seed, csize.X, csize.Z); noiseindev_terrain_higher = new NoiseIndev(params->npindev_terrain_higher, seed, csize.X, csize.Z); noiseindev_steepness = new NoiseIndev(params->npindev_steepness, seed, csize.X, csize.Z); diff --git a/src/mapgen_indev.h b/src/mapgen_indev.h index 7ce65dfe3..fdac1ba20 100644 --- a/src/mapgen_indev.h +++ b/src/mapgen_indev.h @@ -126,7 +126,7 @@ class MapgenIndev : public MapgenV6 { NoiseIndev *noiseindev_float_islands2; NoiseIndev *noiseindev_float_islands3; - MapgenIndev(int mapgenid, MapgenIndevParams *params); + MapgenIndev(int mapgenid, MapgenIndevParams *params, EmergeManager *emerge); ~MapgenIndev(); void calculateNoise(); @@ -141,7 +141,7 @@ class MapgenIndev : public MapgenV6 { struct MapgenFactoryIndev : public MapgenFactoryV6 { Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) { - return new MapgenIndev(mgid, (MapgenIndevParams *)params); + return new MapgenIndev(mgid, (MapgenIndevParams *)params, emerge); }; MapgenParams *createMapgenParams() { diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index 1efa3ad74..0b419617d 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -64,9 +64,10 @@ NoiseParams nparams_v6_def_apple_trees = /////////////////////////////////////////////////////////////////////////////// -MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params) { +MapgenV6::MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge) { this->generating = false; this->id = mapgenid; + this->emerge = emerge; this->seed = (int)params->seed; this->water_level = params->water_level; @@ -463,6 +464,12 @@ void MapgenV6::makeChunk(BlockMakeData *data) { if (flags & MG_TREES) placeTreesAndJungleGrass(); + // Generate the registered ores + for (unsigned int i = 0; i != emerge->ores.size(); i++) { + Ore *ore = emerge->ores[i]; + ore->generate(this, blockseed + i, node_min, node_max); + } + // Calculate lighting calcLighting(node_min, node_max); @@ -494,14 +501,13 @@ void MapgenV6::calculateNoise() { noise_height_select->perlinMap2D( x + 0.5 * noise_height_select->np->spread.X, z + 0.5 * noise_height_select->np->spread.Z); - } - - if (!(flags & MG_FLAT)) { + noise_mud->perlinMap2D( x + 0.5 * noise_mud->np->spread.X, z + 0.5 * noise_mud->np->spread.Z); noise_mud->transformNoiseMap(); } + noise_beach->perlinMap2D( x + 0.2 * noise_beach->np->spread.X, z + 0.7 * noise_beach->np->spread.Z); diff --git a/src/mapgen_v6.h b/src/mapgen_v6.h index 89a3324cd..d37e406cb 100644 --- a/src/mapgen_v6.h +++ b/src/mapgen_v6.h @@ -91,6 +91,8 @@ struct MapgenV6Params : public MapgenParams { class MapgenV6 : public Mapgen { public: + EmergeManager *emerge; + int ystride; v3s16 csize; u32 flags; @@ -128,7 +130,7 @@ public: content_t c_desert_sand; content_t c_desert_stone; - MapgenV6(int mapgenid, MapgenV6Params *params); + MapgenV6(int mapgenid, MapgenV6Params *params, EmergeManager *emerge); ~MapgenV6(); void makeChunk(BlockMakeData *data); @@ -172,7 +174,7 @@ public: struct MapgenFactoryV6 : public MapgenFactory { Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) { - return new MapgenV6(mgid, (MapgenV6Params *)params); + return new MapgenV6(mgid, (MapgenV6Params *)params, emerge); }; MapgenParams *createMapgenParams() { diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 29494ff69..ddffbb0b7 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -30,6 +30,7 @@ extern "C" { #include "settings.h" // For accessing g_settings #include "main.h" // For g_settings #include "biome.h" +#include "emerge.h" #include "script.h" #include "rollback.h" @@ -242,6 +243,14 @@ struct EnumString es_BiomeTerrainType[] = {0, NULL}, }; +struct EnumString es_OreType[] = +{ + {ORE_SCATTER, "scatter"}, + {ORE_SHEET, "sheet"}, + {ORE_CLAYLIKE, "claylike"}, + {0, NULL}, +}; + /*****************************************************************************/ /* Parameters */ /*****************************************************************************/ @@ -612,8 +621,6 @@ static int l_register_biome_groups(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); int index = 1; - if (!lua_istable(L, index)) - throw LuaError(L, "register_biome_groups: parameter is not a table"); BiomeDefManager *bmgr = get_server(L)->getBiomeDef(); if (!bmgr) { @@ -686,6 +693,40 @@ static int l_register_biome(lua_State *L) } +static int l_register_ore(lua_State *L) +{ + int index = 1; + luaL_checktype(L, index, LUA_TTABLE); + + IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager(); + EmergeManager *emerge = get_server(L)->getEmergeManager(); + + enum OreType oretype = (OreType)getenumfield(L, index, + "ore_type", es_OreType, ORE_SCATTER); + Ore *ore = createOre(oretype); + + ore->ore_name = getstringfield_default(L, index, "ore", ""); + ore->wherein_name = getstringfield_default(L, index, "wherein", ""); + ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 0); + ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 0); + 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.); + + lua_getfield(L, index, "noise_params"); + ore->np = read_noiseparams(L, -1); + lua_pop(L, 1); + + ore->noise = NULL; + + emerge->ores.push_back(ore); + + verbosestream << "register_ore: ore '" << ore->ore_name + << "' registered" << std::endl; + return 0; +} + // setting_set(name, value) static int l_setting_set(lua_State *L) @@ -1060,6 +1101,7 @@ static const struct luaL_Reg minetest_f [] = { {"register_craft", l_register_craft}, {"register_biome", l_register_biome}, {"register_biome_groups", l_register_biome_groups}, + {"register_ore", l_register_ore}, {"setting_set", l_setting_set}, {"setting_get", l_setting_get}, {"setting_getbool", l_setting_getbool}, diff --git a/src/scriptapi_noise.cpp b/src/scriptapi_noise.cpp index 1dd6ef8e0..2c1a83c4c 100644 --- a/src/scriptapi_noise.cpp +++ b/src/scriptapi_noise.cpp @@ -269,6 +269,7 @@ NoiseParams *read_noiseparams(lua_State *L, int index) np->scale = getfloatfield_default(L, index, "scale", 0.0); lua_getfield(L, index, "spread"); np->spread = read_v3f(L, -1); + lua_pop(L, 1); np->seed = getintfield_default(L, index, "seed", 0); np->octaves = getintfield_default(L, index, "octaves", 0); np->persist = getfloatfield_default(L, index, "persist", 0.0); @@ -276,6 +277,7 @@ NoiseParams *read_noiseparams(lua_State *L, int index) return np; } + /* LuaPseudoRandom */ diff --git a/src/server.cpp b/src/server.cpp index 644f89349..db05b95cc 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -653,7 +653,6 @@ Server::Server( m_craftdef(createCraftDefManager()), m_event(new EventManager()), m_thread(this), - //m_emergethread(this), m_time_of_day_send_timer(0), m_uptime(0), m_shutdown_requested(false), @@ -698,7 +697,10 @@ Server::Server( // Create biome definition manager m_biomedef = new BiomeDefManager(this); - + + // Create emerge manager + m_emerge = new EmergeManager(this, m_biomedef); + // Create rollback manager std::string rollback_path = m_path_world+DIR_DELIM+"rollback.txt"; m_rollback = createRollbackManager(rollback_path, this); @@ -814,9 +816,6 @@ Server::Server( // Add default biomes after nodedef had its aliases added m_biomedef->addDefaultBiomes(); - // Create emerge manager - m_emerge = new EmergeManager(this, m_biomedef); - // Initialize Environment ServerMap *servermap = new ServerMap(path_world, this, m_emerge); m_env = new ServerEnvironment(servermap, m_lua, this, this);