Skip to content

Commit

Permalink
Use void air like mc does internally
Browse files Browse the repository at this point in the history
mc uses void air for unloaded chunks and outside build height
had to make a few adjustments for make sure pathfinding keeps working
  • Loading branch information
AlexProgrammerDE committed Oct 3, 2024
1 parent 0329488 commit c969dc6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ public static Set<SFVec3i> searchWithinRadiusLayered(BotConnection botConnection
for (var x = -radius; x <= radius; x++) {
for (var z = -radius; z <= radius; z++) {
var blockPos = rootPosition.add(x, y, z);
var blockState = level.getBlockState(blockPos);
if (blockState.blockType() == BlockType.VOID_AIR) {
continue;
}

if (checker.test(blockState)) {
if (checker.test(level.getBlockState(blockPos))) {
list.add(blockPos);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void tick(BotConnection connection) {
if (remainingTicks == -1) {
var optionalBlockType = level.getBlockState(blockPosition).blockType();
if (optionalBlockType == BlockType.VOID_AIR) {
log.warn("Block at {} is not in view range!", blockPosition);
log.warn("Block at {} is not loaded!", blockPosition);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static boolean placeBestToolInHand(SessionDataManager dataManager, SFVec3

var optionalBlockType = dataManager.currentLevel().getBlockState(blockPosition).blockType();
if (optionalBlockType == BlockType.VOID_AIR) {
throw new IllegalStateException("Block at %s is not in view range".formatted(blockPosition));
throw new IllegalStateException("Block at %s is not loaded".formatted(blockPosition));
}

var cost =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

@Slf4j
public record MinecraftGraph(TagsState tagsState,
BlockAccessor blockAccessor, ProjectedInventory inventory,
BlockAccessor blockAccessor,
ProjectedInventory inventory,
PathConstraint pathConstraint) {
private static final Object2ObjectFunction<
? super SFVec3i, ? extends List<WrappedActionSubscription>>
Expand Down Expand Up @@ -144,7 +145,7 @@ private void processSubscription(
absolutePositionBlock = node.blockPosition().add(key);
blockState = blockAccessor.getBlockState(absolutePositionBlock);

if (blockState.blockType() == BlockType.VOID_AIR) {
if (pathConstraint.isOutOfLevel(blockState, absolutePositionBlock)) {
throw new OutOfLevelException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.soulfiremc.server.pathfinding.graph;

import com.soulfiremc.server.data.BlockState;
import com.soulfiremc.server.data.BlockType;
import com.soulfiremc.server.pathfinding.SFVec3i;
import com.soulfiremc.server.protocol.bot.container.SFItemStack;
Expand Down Expand Up @@ -44,6 +45,10 @@ public boolean isTool(SFItemStack item) {
return ItemTypeHelper.isTool(item);
}

public boolean isOutOfLevel(BlockState blockState, SFVec3i pos) {
return blockState.blockType() == BlockType.VOID_AIR && !levelHeightAccessor.isOutsideBuildHeight(pos.y);
}

public boolean canBreakBlockPos(SFVec3i pos) {
return !levelHeightAccessor.isOutsideBuildHeight(pos.y);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ChunkHolder implements BlockAccessor {
private static final BlockState AIR_BLOCK_STATE = BlockState.forDefaultBlockType(BlockType.AIR);
private static final BlockState VOID_AIR_BLOCK_STATE = BlockState.forDefaultBlockType(BlockType.VOID_AIR);
private final Long2ObjectOpenHashMap<ChunkData> chunks = new Long2ObjectOpenHashMap<>();
private final Lock readLock;
Expand Down Expand Up @@ -109,7 +108,7 @@ public ChunkData getOrCreateChunk(int x, int z) {
@Override
public BlockState getBlockState(int x, int y, int z) {
if (levelHeightAccessor.isOutsideBuildHeight(y)) {
return AIR_BLOCK_STATE;
return VOID_AIR_BLOCK_STATE;
}

var chunkData = getChunk(SectionUtils.blockToSection(x), SectionUtils.blockToSection(z));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static boolean isUsableBlockItem(BlockType blockType) {
}

public static boolean isEmptyBlock(BlockType type) {
// Void air stands for unloaded blocks, so we do not know what is there
// Void air stands for not loaded blocks, so we do not know what is there
return type.air() && type != BlockType.VOID_AIR;
}
}

0 comments on commit c969dc6

Please sign in to comment.