Skip to content

Instantly share code, notes, and snippets.

@N3X15
Created June 28, 2011 14:16
Show Gist options
  • Save N3X15/1051220 to your computer and use it in GitHub Desktop.
Save N3X15/1051220 to your computer and use it in GitHub Desktop.
Fluid pressure equalization
/**
* Equalize water pressure (think of how water acts in a U-tube)
* @param world
* @param cx Current X Coordinate
* @param cy
* @param cz
* @param ox Original X Coordinate
* @param oy
* @param oz
* @param data Metadata
* @param iter Current iteration (try to avoid stack overflows...)
* @param visited Blocks visited
* @return
*/
private boolean tryToEqualize(World world, int cx, int cy, int cz, int ox,
int oy, int oz, int data, int iter, List<Location> visited) {
// If water, and there's a block nearby with air above it...
int water = mod_NWater.nwater.blockID;
int stillWater = mod_NWater.nwater_still.blockID;
if (blockID != water)
return false;
int thisblock =world.getBlockId(cx, cy, cz);
if ( thisblock != stillWater && thisblock != water) {
return false;
}
if(world.getBlockId(cx, cy + 1, cz) == 0) {
// And the air block is at least one block below us...
if (cy + 1 < oy) {
placeWater(world, cx, cy + 1, cz, 1, 1);
if (data - 1 < 0) {
world.setBlockAndMetadata(ox, oy, oz, 0, 0);
} else {
world.setBlockMetadata(ox, oy, oz, data - 1);
}
return true;
}
}
iter++;
if (iter == 100)
return true;
visited.add(new Location(cx, cy, cz));
// Recursive flood fill.
if (!visited.contains(new Location(cx - 1, cy, cz))
&& tryToEqualize(world, cx - 1, cy, cz, ox, oy, oz, data, iter,
visited))
return true; // West
if (!visited.contains(new Location(cx + 1, cy, cz))
&& tryToEqualize(world, cx + 1, cy, cz, ox, oy, oz, data, iter,
visited))
return true; // East
if (!visited.contains(new Location(cx, cy, cz - 1))
&& tryToEqualize(world, cx, cy, cz - 1, ox, oy, oz, data, iter,
visited))
return true; // North
if (!visited.contains(new Location(cx, cy, cz + 1))
&& tryToEqualize(world, cx, cy, cz + 1, ox, oy, oz, data, iter,
visited))
return true; // South
if (!visited.contains(new Location(cx, cy - 1, cz))
&& tryToEqualize(world, cx, cy - 1, cz, ox, oy, oz, data, iter,
visited))
return true; // Down
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment