Speed up new island spot search.

Relates to https://github.com/BentoBoxWorld/CaveBlock/issues/44
Added a test case to benchmark search algorithms.
This commit is contained in:
tastybento 2020-03-06 18:04:38 -08:00
parent 30d9ed3887
commit 34ce9d3fc2
2 changed files with 21 additions and 5 deletions

View File

@ -46,12 +46,9 @@ public class DefaultNewIslandLocationStrategy implements NewIslandLocationStrate
// 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);
}
@ -73,10 +70,12 @@ public class DefaultNewIslandLocationStrategy implements NewIslandLocationStrate
* 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

View File

@ -61,6 +61,8 @@ public class DefaultNewIslandLocationStrategyTest {
@Mock
private Block adjBlock;
private int count;
/**
* @throws java.lang.Exception
*/
@ -147,6 +149,21 @@ public class DefaultNewIslandLocationStrategyTest {
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)}.