Skip to content

Commit a61d749

Browse files
committed
Add spiral algorithm test cases
1 parent 7a2604c commit a61d749

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/main/java/com/robotgryphon/compactmachines/util/MathUtil.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.robotgryphon.compactmachines.util;
22

3+
import net.minecraft.util.math.BlockPos;
34
import net.minecraft.util.math.vector.Vector3i;
45

56
public class MathUtil {
@@ -40,4 +41,11 @@ public static Vector3i getRegionPositionByIndex(int i) {
4041

4142
return new Vector3i(x, 0, y);
4243
}
44+
45+
public static BlockPos getCenterWithY(Vector3i regionIndex, int y) {
46+
return new BlockPos(
47+
(regionIndex.getX() * 1024) + 8,
48+
y,
49+
(regionIndex.getZ() * 1024) + 8);
50+
}
4351
}

src/main/java/com/robotgryphon/compactmachines/util/PlayerUtil.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ public static void teleportPlayerIntoMachine(ServerPlayerEntity serverPlayer, Bl
8282
Vector3i location = MathUtil.getRegionPositionByIndex(nextId);
8383

8484
int centerY = ServerConfig.MACHINE_FLOOR_Y.get() + (size.getInternalSize() / 2);
85-
BlockPos newCenter = new BlockPos(
86-
(location.getX() * 1024) + 8,
87-
centerY,
88-
(location.getZ() * 1024) + 8);
85+
BlockPos newCenter = MathUtil.getCenterWithY(location, centerY);
8986

9087
// Generate a new machine inside and update the tile
9188
CompactStructureGenerator.generateCompactStructure(compactWorld, size, newCenter);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.robotgryphon.compactmachines.tests;
2+
3+
import com.robotgryphon.compactmachines.util.MathUtil;
4+
import net.minecraft.util.math.BlockPos;
5+
import net.minecraft.util.math.ChunkPos;
6+
import net.minecraft.util.math.vector.Vector3i;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.util.HashMap;
11+
12+
public class MathTests {
13+
14+
@Test
15+
void positionGeneratorWorksCorrectly() {
16+
// Our generation works in a counter-clockwise spiral, starting at 0,0
17+
/*
18+
* 6 5 4
19+
* 7 0 3
20+
* 8 1 2
21+
*/
22+
23+
HashMap<Integer, ChunkPos> tests = new HashMap<>();
24+
tests.put(0, new ChunkPos(0, 0));
25+
tests.put(1, new ChunkPos(0, -64));
26+
tests.put(2, new ChunkPos(64, -64));
27+
tests.put(3, new ChunkPos(64, 0));
28+
tests.put(4, new ChunkPos(64, 64));
29+
tests.put(5, new ChunkPos(0, 64));
30+
tests.put(6, new ChunkPos(-64, 64));
31+
tests.put(7, new ChunkPos(-64, 0));
32+
tests.put(8, new ChunkPos(-64, -64));
33+
34+
tests.forEach((id, expectedChunk) -> {
35+
Vector3i byIndex = MathUtil.getRegionPositionByIndex(id);
36+
BlockPos finalPos = MathUtil.getCenterWithY(byIndex, 0);
37+
38+
ChunkPos calculatedChunk = new ChunkPos(finalPos);
39+
40+
String error = String.format("Generation did not match for %s.", id);
41+
Assertions.assertEquals(expectedChunk, calculatedChunk, error);
42+
});
43+
}
44+
}

0 commit comments

Comments
 (0)