Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add param2 field for ores, some minor fixes and misc. code cleanup
  • Loading branch information
kwolekr committed Apr 8, 2013
1 parent 4468ea8 commit 7c0e707
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 48 deletions.
64 changes: 22 additions & 42 deletions src/mapgen.cpp
Expand Up @@ -94,7 +94,7 @@ void Ore::resolveNodeNames(INodeDefManager *ndef) {
}


void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
void Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
int in_range = 0;

in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
Expand All @@ -105,9 +105,6 @@ void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {

resolveNodeNames(mg->ndef);

MapNode n_ore(ore);
ManualMapVoxelManipulator *vm = mg->vm;
PseudoRandom pr(blockseed);
int ymin, ymax;

if (in_range & ORE_RANGE_MIRROR) {
Expand All @@ -120,6 +117,17 @@ void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
if (clust_size >= ymax - ymin + 1)
return;

nmin.Y = ymin;
nmax.Y = ymax;
generate(mg->vm, mg->seed, blockseed, nmin, nmax);
}


void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
u32 blockseed, v3s16 nmin, v3s16 nmax) {
PseudoRandom pr(blockseed);
MapNode n_ore(ore, 0, ore_param2);

int volume = (nmax.X - nmin.X + 1) *
(nmax.Y - nmin.Y + 1) *
(nmax.Z - nmin.Z + 1);
Expand All @@ -129,10 +137,10 @@ void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {

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 y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);

if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
continue;

for (int z1 = 0; z1 != csize; z1++)
Expand All @@ -149,53 +157,25 @@ void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
}


void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
int in_range = 0;

in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
if (flags & OREFLAG_ABSHEIGHT)
in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
if (!in_range)
return;

resolveNodeNames(mg->ndef);

MapNode n_ore(ore);
ManualMapVoxelManipulator *vm = mg->vm;
void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
u32 blockseed, v3s16 nmin, v3s16 nmax) {
PseudoRandom pr(blockseed + 4234);
int ymin, ymax;

if (in_range & ORE_RANGE_MIRROR) {
ymin = MYMAX(nmin.Y, -height_max);
ymax = MYMIN(nmax.Y, -height_min);
} else {
ymin = MYMAX(nmin.Y, height_min);
ymax = MYMIN(nmax.Y, height_max);
}

if (clust_size >= ymax - ymin + 1)
return;

int x0 = nmin.X;
int z0 = nmin.Z;

int x1 = nmax.X;
int z1 = nmax.Z;
MapNode n_ore(ore, 0, ore_param2);

int max_height = clust_size;
int y_start = pr.range(ymin, ymax - max_height);
int y_start = pr.range(nmin.Y, nmax.Y - 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);
noise->seed = seed + y_start;
noise->perlinMap2D(nmin.X, nmin.Z);

int index = 0;
for (int z = z0; z != z1; z++)
for (int x = x0; x != x1; x++) {
for (int z = nmin.Z; z <= nmax.Z; z++)
for (int x = nmin.X; x <= nmax.X; x++) {
float noiseval = noise->result[index++];
if (noiseval < nthresh)
continue;
Expand Down
12 changes: 8 additions & 4 deletions src/mapgen.h
Expand Up @@ -120,14 +120,14 @@ class Ore {
public:
std::string ore_name;
std::string wherein_name;

content_t ore;
content_t wherein; // the node to be replaced
u32 clust_scarcity; // ore cluster has a 1-in-clust_scarcity chance of appearing at a node
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;
u8 ore_param2; // to set node-specific attributes
u32 flags; // attributes for this ore
float nthresh; // threshhold for noise at which an ore is placed
NoiseParams *np; // noise for distribution of clusters (NULL for uniform scattering)
Expand All @@ -141,15 +141,19 @@ class Ore {
}

void resolveNodeNames(INodeDefManager *ndef);
virtual void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
void placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
virtual void generate(ManualMapVoxelManipulator *vm, int seed,
u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
};

class OreScatter : public Ore {
void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
virtual void generate(ManualMapVoxelManipulator *vm, int seed,
u32 blockseed, v3s16 nmin, v3s16 nmax);
};

class OreSheet : public Ore {
void generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
virtual void generate(ManualMapVoxelManipulator *vm, int seed,
u32 blockseed, v3s16 nmin, v3s16 nmax);
};

Ore *createOre(OreType type);
Expand Down
2 changes: 1 addition & 1 deletion src/mapgen_v6.cpp
Expand Up @@ -467,7 +467,7 @@ void MapgenV6::makeChunk(BlockMakeData *data) {
// 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);
ore->placeOre(this, blockseed + i, node_min, node_max);
}

// Calculate lighting
Expand Down
2 changes: 1 addition & 1 deletion src/mapgen_v7.cpp
Expand Up @@ -180,7 +180,7 @@ void MapgenV7::makeChunk(BlockMakeData *data) {

for (size_t i = 0; i != emerge->ores.size(); i++) {
Ore *ore = emerge->ores[i];
ore->generate(this, blockseed + i, node_min, node_max);
ore->placeOre(this, blockseed + i, node_min, node_max);
}

//printf("makeChunk: %dms\n", t.stop());
Expand Down
1 change: 1 addition & 0 deletions src/scriptapi.cpp
Expand Up @@ -670,6 +670,7 @@ static int l_register_ore(lua_State *L)
}

ore->ore_name = getstringfield_default(L, index, "ore", "");
ore->ore_param2 = (u8)getintfield_default(L, index, "ore_param2", 0);
ore->wherein_name = getstringfield_default(L, index, "wherein", "");
ore->clust_scarcity = getintfield_default(L, index, "clust_scarcity", 1);
ore->clust_num_ores = getintfield_default(L, index, "clust_num_ores", 1);
Expand Down

0 comments on commit 7c0e707

Please sign in to comment.