Permalink
Browse files

Add Ore infrastructure and l_register_ore()

  • Loading branch information...
kwolekr committed Mar 24, 2013
1 parent f70378f commit 57cbb8bfd8daaa1b8b1aa876723ff6355d21f7fc
Showing with 248 additions and 17 deletions.
  1. +2 −1 src/emerge.h
  2. +134 −0 src/mapgen.cpp
  3. +43 −0 src/mapgen.h
  4. +3 −1 src/mapgen_indev.cpp
  5. +2 −2 src/mapgen_indev.h
  6. +10 −4 src/mapgen_v6.cpp
  7. +4 −2 src/mapgen_v6.h
  8. +44 −2 src/scriptapi.cpp
  9. +2 −0 src/scriptapi_noise.cpp
  10. +4 −5 src/server.cpp
@@ -63,8 +63,9 @@ class EmergeManager {
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();
@@ -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();
@@ -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

@@ -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);
@@ -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() {
@@ -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);
@@ -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 @@ class MapgenV6 : public Mapgen {
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 @@ class MapgenV6 : public Mapgen {

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() {
@@ -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},
@@ -269,13 +269,15 @@ 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);

return np;
}


/*
LuaPseudoRandom
*/
Oops, something went wrong.

0 comments on commit 57cbb8b

Please sign in to comment.