Skip to content

Commit

Permalink
Refactored roof search complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Oct 11, 2020
1 parent 2a75722 commit 50e3bf2
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,7 @@ private boolean findRoof(Location loc) {
}
// If the roof was not found start going around in circles until something is found
// Expand in ever increasing squares around location until a wall block is found
for (int radius = 0; radius < 3 && !roofFound; radius++) {
for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius && !roofFound; x++) {
for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius && !roofFound; z++) {
if (!((x > loc.getBlockX() - radius && x < loc.getBlockX() + radius) && (z > loc.getBlockZ() - radius && z < loc.getBlockZ() + radius))) {
Block b = world.getBlockAt(x, roofY, z);
if (!Walls.wallBlocks(b.getType())) {
// Look up
for (int y = roofY; y < world.getMaxHeight() && !roofFound; y++) {
if (roofBlocks(world.getBlockAt(x,y,z).getType())) {
roofFound = true;
loc = new Location(world,x,y,z);
}
}
}
}
}
}
}
spiralSearch(loc, roofY);
if (!roofFound) return false;
// Record the height
this.height = loc.getBlockY();
Expand Down Expand Up @@ -110,6 +93,34 @@ private boolean findRoof(Location loc) {
return true;
}

private void spiralSearch(Location loc, int roofY) {
for (int radius = 0; radius < 3 && !roofFound; radius++) {
for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius && !roofFound; x++) {
for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius && !roofFound; z++) {
if (!((x > loc.getBlockX() - radius && x < loc.getBlockX() + radius) && (z > loc.getBlockZ() - radius && z < loc.getBlockZ() + radius))) {
checkVertically(loc, x, roofY, z);
}
}
}
}

}

private void checkVertically(Location loc, int x, int roofY, int z) {
World world = loc.getWorld();
Block b = world.getBlockAt(x, roofY, z);
if (!Walls.wallBlocks(b.getType())) {
// Look up
for (int y = roofY; y < world.getMaxHeight() && !roofFound; y++) {
if (roofBlocks(world.getBlockAt(x,y,z).getType())) {
roofFound = true;
loc = new Location(world,x,y,z);
}
}
}

}

/**
* This takes any location and tries to go as far as possible in NWSE directions finding contiguous roof blocks
* up to 100 in any direction
Expand Down

0 comments on commit 50e3bf2

Please sign in to comment.