Add Ore infrastructure and l_register_ore()
authorkwolekr <kwolekr@minetest.net>
Sun, 24 Mar 2013 05:43:38 +0000 (01:43 -0400)
committerkwolekr <kwolekr@minetest.net>
Sun, 24 Mar 2013 17:23:33 +0000 (13:23 -0400)
src/emerge.h
src/mapgen.cpp
src/mapgen.h
src/mapgen_indev.cpp
src/mapgen_indev.h
src/mapgen_v6.cpp
src/mapgen_v6.h
src/scriptapi.cpp
src/scriptapi_noise.cpp
src/server.cpp

index 70b67e731f05360a3fc44198eaa5fd43a3d0bc9d..3d717bce3fd9f19a93746c3b8d7e67dcfabad97d 100644 (file)
@@ -63,8 +63,9 @@ public:
        std::map<v3s16, BlockEmergeData *> blocks_enqueued;
        std::map<u16, u16> peer_queue_count;
 
-       //biome manager
+       //Mapgen-related structures
        BiomeDefManager *biomedef;
+       std::vector<Ore *> ores;
 
        EmergeManager(IGameDef *gamedef, BiomeDefManager *bdef);
        ~EmergeManager();
index dc6dab6bb902bb4ea87cb4c500e891d51609ffbd..53b5d68679ecc07f18afecf599bb68aa6ce8dda4 100644 (file)
@@ -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<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
        bool isliquid, wasliquid;
        v3s16 em  = vm->m_area.getExtent();
index 2e917a3aae93e167f2420aca1f9cd0634921ce91..a900985da5bfbb44586e646d747d0e1a1bc2ffce 100644 (file)
@@ -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
 
index 6956fc63f14880cac96d7f29556f2a9ba4e852bc..5d455827ae4db28fe314731c096f935f6181589f 100644 (file)
@@ -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);
index 7ce65dfe3fb164d15a4cf419c1c33d44bfda9216..fdac1ba209f4808217527909c43e26236477946e 100644 (file)
@@ -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() {
index 1efa3ad745555f5a60b3a55035449a57deaf9d62..0b419617d22bb2d8a54986c1600386fa3fa4ec65 100644 (file)
@@ -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);
index 89a3324cd6d0cb74692e7d0d58bea96c08e28b6f..d37e406cbcb1d65933e31ae51d8aa4c07b8edd92 100644 (file)
@@ -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() {
index 29494ff696134de7cf3173813701028c2562f90c..ddffbb0b7af256574f3fc02ef0d849f8b63ef20c 100644 (file)
@@ -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},
index 1dd6ef8e06c105ab7c34d8fe3408b47a0d9b6637..2c1a83c4c5f10adbcd5c28166112e73f8a76fd55 100644 (file)
@@ -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
 */
index 644f8934918985aa972fcc9373ad1b54fdd7c574..db05b95ccc10eef2fc2012e70b9eb021296694bf 100644 (file)
@@ -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);