Skip to content

Commit

Permalink
Split up block accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 19, 2024
1 parent 7b3060a commit dd3d8ea
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
10 changes: 6 additions & 4 deletions server/src/jmh/java/com/soulfiremc/jmh/PathfindingBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.soulfiremc.server.protocol.bot.state.TagsState;
import com.soulfiremc.server.util.SFHelpers;
import com.soulfiremc.server.util.structs.GsonInstance;
import com.soulfiremc.test.utils.TestBlockAccessor;
import com.soulfiremc.test.utils.TestBlockAccessorBuilder;
import com.soulfiremc.test.utils.TestPathConstraint;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.key.Key;
Expand Down Expand Up @@ -69,7 +69,7 @@ public void setup() {
log.info("Parsing world data...");

var maxY = 0;
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
for (var x = 0; x < data.length; x++) {
var xArray = data[x];
for (var y = 0; y < xArray.length; y++) {
Expand All @@ -83,10 +83,12 @@ public void setup() {

log.info("Calculating world data...");

var builtAccessor = accessor.build();

// Find the first safe block at 0 0
var safeY = 0;
for (var y = maxY; y >= 0; y--) {
if (accessor.getBlockState(0, y, 0).blockType() != BlockType.AIR) {
if (builtAccessor.getBlockState(0, y, 0).blockType() != BlockType.AIR) {
safeY = y + 1;
break;
}
Expand All @@ -97,7 +99,7 @@ public void setup() {
log.info("Initial state: {}", initialState.blockPosition().formatXYZ());

routeFinder = new RouteFinder(new MinecraftGraph(new TagsState(),
accessor,
builtAccessor,
inventory,
TestPathConstraint.INSTANCE), new PosGoal(100, 80, 100));

Expand Down
67 changes: 34 additions & 33 deletions server/src/test/java/com/soulfiremc/test/PathfindingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.soulfiremc.server.pathfinding.graph.ProjectedInventory;
import com.soulfiremc.server.protocol.bot.container.SFItemStack;
import com.soulfiremc.server.util.DefaultTagsState;
import com.soulfiremc.test.utils.TestBlockAccessor;
import com.soulfiremc.test.utils.TestBlockAccessorBuilder;
import com.soulfiremc.test.utils.TestPathConstraint;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -42,14 +42,15 @@
public class PathfindingTest {
@Test
public void testPathfindingStraight() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, 0, 0, BlockType.STONE);
accessor.setBlockAt(2, 0, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor, inventory,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(2, 1, 0));

var initialState = NodeState.forInfo(new SFVec3i(0, 1, 0), inventory);
Expand All @@ -61,7 +62,7 @@ public void testPathfindingStraight() {

@Test
public void testPathfindingImpossible() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, 0, 0, BlockType.STONE);
accessor.setBlockAt(2, 0, 0, BlockType.STONE);
Expand All @@ -70,7 +71,7 @@ public void testPathfindingImpossible() {
var routeFinder =
new RouteFinder(
new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE),
// This is impossible to reach
Expand All @@ -84,14 +85,14 @@ public void testPathfindingImpossible() {

@Test
public void testPathfindingDiagonal() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, 0, 1, BlockType.STONE);
accessor.setBlockAt(2, 0, 2, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(2, 1, 2));

Expand All @@ -104,7 +105,7 @@ public void testPathfindingDiagonal() {

@Test
public void testPathfindingDiagonalImpossible() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, 0, 1, BlockType.STONE);
accessor.setBlockAt(2, 0, 2, BlockType.STONE);
Expand All @@ -117,7 +118,7 @@ public void testPathfindingDiagonalImpossible() {

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(2, 1, 2));

Expand All @@ -130,13 +131,13 @@ public void testPathfindingDiagonalImpossible() {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
public void testPathfindingJump(int height) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, height, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(1, height + 1, 0));

Expand All @@ -154,13 +155,13 @@ public void testPathfindingJump(int height) {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
public void testPathfindingJumpDiagonal(int height) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, height, 1, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(1, height + 1, 1));

Expand All @@ -178,13 +179,13 @@ public void testPathfindingJumpDiagonal(int height) {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
public void testPathfindingFall(int height) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, -height, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(1, -height + 1, 0));

Expand All @@ -202,13 +203,13 @@ public void testPathfindingFall(int height) {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
public void testPathfindingFallDiagonal(int height) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, -height, 1, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(1, -height + 1, 1));

Expand All @@ -226,13 +227,13 @@ public void testPathfindingFallDiagonal(int height) {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4, 5})
public void testPathfindingGapJump(int gapLength) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(gapLength + 1, 0, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(gapLength + 1, 1, 0));

Expand All @@ -250,12 +251,12 @@ public void testPathfindingGapJump(int gapLength) {

@Test
public void testPathfindingUp() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.STONE)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(0, 2, 0));

Expand All @@ -268,12 +269,12 @@ public void testPathfindingUp() {
@ParameterizedTest
@ValueSource(ints = {15, 20, 25})
public void testPathfindingUpStacking(int amount) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeWithAmount(ItemType.STONE, amount)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(0, 21, 0));

Expand All @@ -290,13 +291,13 @@ public void testPathfindingUpStacking(int amount) {

@Test
public void testPathfindingDown() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(0, -1, 0, BlockType.STONE);

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(0, 0, 0));

Expand All @@ -308,7 +309,7 @@ public void testPathfindingDown() {

@Test
public void testPathfindingThroughWallToMoveUp() {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, 0, 0, BlockType.STONE);
accessor.setBlockAt(1, 1, 0, BlockType.STONE);
Expand All @@ -317,7 +318,7 @@ public void testPathfindingThroughWallToMoveUp() {

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(2, 1, 0));

Expand All @@ -330,7 +331,7 @@ public void testPathfindingThroughWallToMoveUp() {
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testPathfindingMoveUpSideUnsafe(boolean unsafe) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(0, 3, 0, BlockType.STONE);
if (unsafe) {
Expand All @@ -339,7 +340,7 @@ public void testPathfindingMoveUpSideUnsafe(boolean unsafe) {

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(0, 2, 0));

Expand All @@ -357,7 +358,7 @@ public void testPathfindingMoveUpSideUnsafe(boolean unsafe) {
@ParameterizedTest
@ValueSource(booleans = {true, false})
public void testPathfindingDigSideUnsafe(boolean unsafe) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(0, -1, 0, BlockType.STONE);
if (unsafe) {
Expand All @@ -366,7 +367,7 @@ public void testPathfindingDigSideUnsafe(boolean unsafe) {

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(0, 0, 0));

Expand All @@ -384,7 +385,7 @@ public void testPathfindingDigSideUnsafe(boolean unsafe) {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 4})
public void testPathfindingDigBelowUnsafe(int level) {
var accessor = new TestBlockAccessor();
var accessor = new TestBlockAccessorBuilder();
accessor.setBlockAt(0, 0, 0, BlockType.STONE);
accessor.setBlockAt(0, -1, 0, BlockType.LAVA);
accessor.setBlockAt(0, -2, 0, BlockType.LAVA);
Expand All @@ -395,7 +396,7 @@ public void testPathfindingDigBelowUnsafe(int level) {

var inventory = ProjectedInventory.forUnitTest(List.of(SFItemStack.forTypeSingle(ItemType.DIAMOND_PICKAXE)), TestPathConstraint.INSTANCE);
var routeFinder = new RouteFinder(new MinecraftGraph(DefaultTagsState.TAGS_STATE,
accessor,
accessor.build(),
inventory,
TestPathConstraint.INSTANCE), new PosGoal(0, 0, 0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,30 @@
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;

public class TestBlockAccessor implements BlockAccessor {
public class TestBlockAccessorBuilder {
private final Long2ObjectMap<BlockState> blocks = new Long2ObjectOpenHashMap<>();
private final BlockState defaultBlock;

public TestBlockAccessor() {
public TestBlockAccessorBuilder() {
this(BlockState.forDefaultBlockType(BlockType.AIR));
}

public TestBlockAccessor(BlockState defaultBlock) {
public TestBlockAccessorBuilder(BlockState defaultBlock) {
this.defaultBlock = defaultBlock;
}

public void setBlockAt(int x, int y, int z, BlockType block) {
blocks.put(VectorHelper.asLong(x, y, z), BlockState.forDefaultBlockType(block));
}

@Override
public BlockState getBlockState(int x, int y, int z) {
return blocks.getOrDefault(VectorHelper.asLong(x, y, z), defaultBlock);
public BlockAccessor build() {
return new TestBlockAccessor(blocks, defaultBlock);
}

private record TestBlockAccessor(Long2ObjectMap<BlockState> blocks, BlockState defaultBlock) implements BlockAccessor {
@Override
public BlockState getBlockState(int x, int y, int z) {
return blocks.getOrDefault(VectorHelper.asLong(x, y, z), defaultBlock);
}
}
}

0 comments on commit dd3d8ea

Please sign in to comment.