diff --git a/.classpath b/.classpath index def0e4f..c9772b7 100644 --- a/.classpath +++ b/.classpath @@ -24,5 +24,6 @@ + diff --git a/src/main/java/morlok8k/MinecraftLandGenerator/World.java b/src/main/java/morlok8k/MinecraftLandGenerator/World.java index 96659d3..b0a5345 100644 --- a/src/main/java/morlok8k/MinecraftLandGenerator/World.java +++ b/src/main/java/morlok8k/MinecraftLandGenerator/World.java @@ -6,6 +6,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -188,9 +189,10 @@ public class World { } private static List generateLinearSpawnpoints(int start, int length, int maxStep) { - int stepCount = (int) Math.ceil((double) length / maxStep); + double stepCount = Math.ceil((double) length / maxStep); + if (stepCount == 0) return Arrays.asList(start); double realStep = length / stepCount; - return IntStream.rangeClosed(0, stepCount).mapToObj(i -> start + (int) (realStep * i)) + return IntStream.rangeClosed(0, (int) stepCount).mapToObj(i -> start + (int) (realStep * i)) .collect(Collectors.toList()); } } diff --git a/src/test/java/SpawnpointTest.java b/src/test/java/SpawnpointTest.java new file mode 100644 index 0000000..c07a0b2 --- /dev/null +++ b/src/test/java/SpawnpointTest.java @@ -0,0 +1,50 @@ +import static org.junit.Assert.assertTrue; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.joml.Vector2i; +import org.junit.Test; + +import morlok8k.MinecraftLandGenerator.World; + +public class SpawnpointTest { + + static { + System.setProperty("joml.format", "false"); + } + + @Test + public void simpleTest() { + test(0, 0, 25, 25, 25); + test(-100, 10, 500, 400, 25); + test(-256, 16, 512, 256, 25); + test(-255, 15, 512, 256, 25); + test(-256, 16, 511, 255, 25); + test(-255, 15, 511, 255, 25); + test(25, 24, 50, 49, 25); + test(25, 24, 49, 50, 25); + } + + private static void test(int startX, int startZ, int width, int height, int increment) { + List spawn = World.generateSpawnpoints(startX, startZ, width, height, increment); + int margin = increment / 2; + Set coverage = new HashSet<>(); + for (Vector2i v : spawn) { + for (int z = v.y - margin; z <= v.y + margin; z++) + for (int x = v.x - margin; x <= v.x + margin; x++) + coverage.add(new Vector2i(x, z)); + } + for (int z = startZ; z < startZ + height; z++) { + for (int x = startX; x < startX + width; x++) { + assertTrue( + "Chunk (" + x + ", " + z + ") in (" + startX + ", " + startZ + ", " + width + + ", " + height + + ") should be covered the spawn chunks, but they were " + spawn, + coverage.remove(new Vector2i(x, z))); + } + } + assertTrue(coverage.isEmpty()); + } +}