Skip to content

Commit

Permalink
Speed up new island spot search.
Browse files Browse the repository at this point in the history
Relates to BentoBoxWorld/CaveBlock#44
Added a test case to benchmark search algorithms.
  • Loading branch information
tastybento committed Mar 7, 2020
1 parent 30d9ed3 commit 34ce9d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ public Location getNextLocation(World world) {
// Find a free spot
Map<Result, Integer> result = new EnumMap<>(Result.class);
// Check center
last = Util.getClosestIsland(last);
Result r = isIsland(last);

while (!r.equals(Result.FREE) && result.getOrDefault(Result.BLOCKS_IN_AREA, 0) < MAX_UNOWNED_ISLANDS) {
nextGridLocation(last);
last = Util.getClosestIsland(last);
result.put(r, result.getOrDefault(r, 0) + 1);
r = isIsland(last);
}
Expand All @@ -73,10 +70,12 @@ public Location getNextLocation(World world) {
* Checks if there is an island or blocks at this location
*
* @param location - the location
* @return true if island found, null if blocks found, false if nothing found
* @return Result enum if island found, null if blocks found, false if nothing found
*/
protected Result isIsland(Location location) {

// Quick check
if (plugin.getIslands().getIslandAt(location).isPresent()) return Result.ISLAND_FOUND;

World world = location.getWorld();

// Check 4 corners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class DefaultNewIslandLocationStrategyTest {
@Mock
private Block adjBlock;

private int count;

/**
* @throws java.lang.Exception
*/
Expand Down Expand Up @@ -147,6 +149,21 @@ public void testGetNextLocationSuccessSomeIslands() {
assertEquals(location,dnils.getNextLocation(world));
verify(im).setLast(location);
}

/**
* Test method for {@link world.bentobox.bentobox.managers.island.DefaultNewIslandLocationStrategy#getNextLocation(org.bukkit.World)}.
*/
@Test
public void testGetNextLocationSuccessSomeIslands10() {
Optional<Island> opIsland = Optional.of(new Island());
Optional<Island> emptyIsland = Optional.empty();
count = 0;
//long time = System.currentTimeMillis();
when(im.getIslandAt(any())).thenAnswer(i -> count++ > 10 ? emptyIsland :opIsland);
assertEquals(location,dnils.getNextLocation(world));
//System.out.println(System.currentTimeMillis() - time);
verify(im).setLast(location);
}

/**
* Test method for {@link world.bentobox.bentobox.managers.island.DefaultNewIslandLocationStrategy#isIsland(org.bukkit.Location)}.
Expand Down

0 comments on commit 34ce9d3

Please sign in to comment.