Skip to content

Commit

Permalink
Refactor decoration-related code
Browse files Browse the repository at this point in the history
Split up ModApiMapgen::l_register_decoration()
Define and make use of CONTAINS() and ARRLEN() macros
  • Loading branch information
kwolekr committed Oct 29, 2014
1 parent 7c6da2f commit 1cb6ea6
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 136 deletions.
10 changes: 5 additions & 5 deletions src/clouds.cpp
Expand Up @@ -189,7 +189,7 @@ void Clouds::render()
} }


#define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius)) #define GETINDEX(x, z, radius) (((z)+(radius))*(radius)*2 + (x)+(radius))
#define CONTAINS(x, z, radius) \ #define INAREA(x, z, radius) \
((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius)) ((x) >= -(radius) && (x) < (radius) && (z) >= -(radius) && (z) < (radius))


for(s16 zi0=-cloud_radius_i; zi0<cloud_radius_i; zi0++) for(s16 zi0=-cloud_radius_i; zi0<cloud_radius_i; zi0++)
Expand Down Expand Up @@ -247,7 +247,7 @@ void Clouds::render()
v[3].Pos.set( rx, ry,-rz); v[3].Pos.set( rx, ry,-rz);
break; break;
case 1: // back case 1: // back
if(CONTAINS(xi, zi-1, cloud_radius_i)){ if(INAREA(xi, zi-1, cloud_radius_i)){
u32 j = GETINDEX(xi, zi-1, cloud_radius_i); u32 j = GETINDEX(xi, zi-1, cloud_radius_i);
if(grid[j]) if(grid[j])
continue; continue;
Expand All @@ -262,7 +262,7 @@ void Clouds::render()
v[3].Pos.set(-rx,-ry,-rz); v[3].Pos.set(-rx,-ry,-rz);
break; break;
case 2: //right case 2: //right
if(CONTAINS(xi+1, zi, cloud_radius_i)){ if(INAREA(xi+1, zi, cloud_radius_i)){
u32 j = GETINDEX(xi+1, zi, cloud_radius_i); u32 j = GETINDEX(xi+1, zi, cloud_radius_i);
if(grid[j]) if(grid[j])
continue; continue;
Expand All @@ -277,7 +277,7 @@ void Clouds::render()
v[3].Pos.set( rx,-ry,-rz); v[3].Pos.set( rx,-ry,-rz);
break; break;
case 3: // front case 3: // front
if(CONTAINS(xi, zi+1, cloud_radius_i)){ if(INAREA(xi, zi+1, cloud_radius_i)){
u32 j = GETINDEX(xi, zi+1, cloud_radius_i); u32 j = GETINDEX(xi, zi+1, cloud_radius_i);
if(grid[j]) if(grid[j])
continue; continue;
Expand All @@ -292,7 +292,7 @@ void Clouds::render()
v[3].Pos.set( rx,-ry, rz); v[3].Pos.set( rx,-ry, rz);
break; break;
case 4: // left case 4: // left
if(CONTAINS(xi-1, zi, cloud_radius_i)){ if(INAREA(xi-1, zi, cloud_radius_i)){
u32 j = GETINDEX(xi-1, zi, cloud_radius_i); u32 j = GETINDEX(xi-1, zi, cloud_radius_i);
if(grid[j]) if(grid[j])
continue; continue;
Expand Down
105 changes: 51 additions & 54 deletions src/mapgen.cpp
Expand Up @@ -149,9 +149,10 @@ void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
continue; continue;


u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1); u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
for (size_t ii = 0; ii < c_wherein.size(); ii++) if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
if (vm->m_data[i].getContent() == c_wherein[ii]) continue;
vm->m_data[i] = n_ore;
vm->m_data[i] = n_ore;
} }
} }
} }
Expand Down Expand Up @@ -187,13 +188,10 @@ void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
u32 i = vm->m_area.index(x, y, z); u32 i = vm->m_area.index(x, y, z);
if (!vm->m_area.contains(i)) if (!vm->m_area.contains(i))
continue; continue;
if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
continue;


for (size_t ii = 0; ii < c_wherein.size(); ii++) { vm->m_data[i] = n_ore;
if (vm->m_data[i].getContent() == c_wherein[ii]) {
vm->m_data[i] = n_ore;
break;
}
}
} }
} }
} }
Expand Down Expand Up @@ -360,53 +358,56 @@ void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)


/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////



bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p) {
void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) { // Don't bother if there aren't any decorations to place
ManualMapVoxelManipulator *vm = mg->vm; if (c_decos.size() == 0)
return false;


u32 vi = vm->m_area.index(p); u32 vi = vm->m_area.index(p);
content_t c = vm->m_data[vi].getContent();
size_t idx;
for (idx = 0; idx != c_place_on.size(); idx++) {
if (c == c_place_on[idx])
break;
}
if ((idx != 0) && (idx == c_place_on.size()))
return;


if (nspawnby != -1) { // Check if the decoration can be placed on this node
int nneighs = 0; if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
v3s16 dirs[8] = { // a Moore neighborhood return false;
v3s16( 0, 0, 1),
v3s16( 0, 0, -1),
v3s16( 1, 0, 0),
v3s16(-1, 0, 0),
v3s16( 1, 0, 1),
v3s16(-1, 0, 1),
v3s16(-1, 0, -1),
v3s16( 1, 0, -1)
};

for (int i = 0; i != 8; i++) {
u32 index = vm->m_area.index(p + dirs[i]);
if (!vm->m_area.contains(index))
continue;


content_t c = vm->m_data[index].getContent(); // Don't continue if there are no spawnby constraints
for (size_t j = 0; j != c_spawnby.size(); j++) { if (nspawnby == -1)
if (c == c_spawnby[j]) { return true;
nneighs++;
break; int nneighs = 0;
} v3s16 dirs[8] = {
} v3s16( 0, 0, 1),
} v3s16( 0, 0, -1),
v3s16( 1, 0, 0),
v3s16(-1, 0, 0),
v3s16( 1, 0, 1),
v3s16(-1, 0, 1),
v3s16(-1, 0, -1),
v3s16( 1, 0, -1)
};

// Check a Moore neighborhood if there are enough spawnby nodes
for (size_t i = 0; i != ARRLEN(dirs); i++) {
u32 index = vm->m_area.index(p + dirs[i]);
if (!vm->m_area.contains(index))
continue;


if (nneighs < nspawnby) if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
return; nneighs++;
} }


if (c_decos.size() == 0) if (nneighs < nspawnby)
return false;

return true;
}


void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
ManualMapVoxelManipulator *vm = mg->vm;

if (!canPlaceDecoration(vm, p))
return; return;

content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)]; content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];


s16 height = (deco_height_max > 0) ? s16 height = (deco_height_max > 0) ?
Expand All @@ -415,6 +416,7 @@ void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {
height = MYMIN(height, max_y - p.Y); height = MYMIN(height, max_y - p.Y);


v3s16 em = vm->m_area.getExtent(); v3s16 em = vm->m_area.getExtent();
u32 vi = vm->m_area.index(p);
for (int i = 0; i < height; i++) { for (int i = 0; i < height; i++) {
vm->m_area.add_y(em, vi, 1); vm->m_area.add_y(em, vi, 1);


Expand Down Expand Up @@ -477,12 +479,7 @@ void DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) {


u32 vi = vm->m_area.index(p); u32 vi = vm->m_area.index(p);
content_t c = vm->m_data[vi].getContent(); content_t c = vm->m_data[vi].getContent();
size_t idx; if (!CONTAINS(c_place_on, c))
for (idx = 0; idx != c_place_on.size(); idx++) {
if (c == c_place_on[idx])
break;
}
if ((idx != 0) && (idx == c_place_on.size()))
return; return;


Rotation rot = (rotation == ROTATE_RAND) ? Rotation rot = (rotation == ROTATE_RAND) ?
Expand Down
3 changes: 2 additions & 1 deletion src/mapgen.h
Expand Up @@ -207,7 +207,7 @@ Ore *createOre(OreType type);




enum DecorationType { enum DecorationType {
DECO_SIMPLE = 1, DECO_SIMPLE,
DECO_SCHEMATIC, DECO_SCHEMATIC,
DECO_LSYSTEM DECO_LSYSTEM
}; };
Expand Down Expand Up @@ -262,6 +262,7 @@ class DecoSimple : public Decoration {


~DecoSimple() {} ~DecoSimple() {}


bool canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p);
virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p); virtual void generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p);
virtual int getHeight(); virtual int getHeight();
virtual std::string getName(); virtual std::string getName();
Expand Down
2 changes: 1 addition & 1 deletion src/nodedef.h
Expand Up @@ -350,7 +350,7 @@ class NodeResolver {
before the pending request had been satisfied, cancelNodeList() must be before the pending request had been satisfied, cancelNodeList() must be
called. called.
@param nodename Name of node (or node group) to be resolved. @param nodename Name of node (or node group) to be resolved.
@param content_vec Pointer to content_t vector onto which the results @param content_vec Pointer to content_t vector onto which the results
are added. are added.
Expand Down

0 comments on commit 1cb6ea6

Please sign in to comment.