Skip to content

Commit

Permalink
Implement supporting block
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 21, 2024
1 parent b534326 commit 1ecaf99
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.soulfiremc.server.protocol.bot.state.entity.Entity;
import com.soulfiremc.server.protocol.bot.state.registry.DimensionType;
import com.soulfiremc.server.util.MathHelper;
import com.soulfiremc.server.util.VectorHelper;
import com.soulfiremc.server.util.mcstructs.AABB;
import com.soulfiremc.server.util.structs.TickRateManager;
import lombok.Getter;
Expand Down Expand Up @@ -112,7 +113,7 @@ public BlockState getBlockState(int x, int y, int z) {
return chunks.getBlockState(x, y, z);
}

public List<AABB> getCollisionBoxes(AABB aabb) {
public List<Vector3i> getTouchedPositions(AABB aabb) {
var startX = MathHelper.floorDouble(aabb.minX - AABB.EPSILON) - 1;
var endX = MathHelper.floorDouble(aabb.maxX + AABB.EPSILON) + 1;
var startY = MathHelper.floorDouble(aabb.minY - AABB.EPSILON) - 1;
Expand All @@ -121,24 +122,24 @@ public List<AABB> getCollisionBoxes(AABB aabb) {
var endZ = MathHelper.floorDouble(aabb.maxZ + AABB.EPSILON) + 1;

var predictedSize = (endX - startX + 1) * (endY - startY + 1) * (endZ - startZ + 1);
var surroundingBBs = new ArrayList<AABB>(predictedSize);
var surroundingBlocks = new ArrayList<Vector3i>(predictedSize);

for (var x = startX; x <= endX; x++) {
for (var y = startY; y <= endY; y++) {
for (var z = startZ; z <= endZ; z++) {
var cursor = Vector3i.from(x, y, z);
var blockState = getBlockState(cursor);

for (var collisionBox : blockState.getCollisionBoxes(cursor)) {
if (collisionBox.intersects(aabb)) {
surroundingBBs.add(collisionBox);
}
}
surroundingBlocks.add(Vector3i.from(x, y, z));
}
}
}

return surroundingBBs;
return surroundingBlocks;
}

public List<AABB> getCollisionBoxes(AABB aabb) {
return getTouchedPositions(aabb).stream()
.flatMap(cursor -> getBlockState(cursor).getCollisionBoxes(cursor).stream())
.filter(collisionBox -> collisionBox.intersects(aabb))
.toList();
}

public boolean containsAnyLiquid(AABB bb) {
Expand Down Expand Up @@ -168,6 +169,17 @@ public boolean noCollision(AABB bb) {
}

public Optional<Vector3i> findSupportingBlock(Entity entity, AABB bb) {
return Optional.empty(); // TODO
Vector3i block = null;
var distance = Double.MAX_VALUE;

for (var position : getTouchedPositions(bb)) {
var distanceToCenter = VectorHelper.distToCenterSqr(position, entity.pos());
if (distanceToCenter < distance || distanceToCenter == distance && (block == null || block.compareTo(position) < 0)) {
block = position;
distance = distanceToCenter;
}
}

return Optional.ofNullable(block);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.soulfiremc.server.data.BlockShapeGroup;
import com.soulfiremc.server.data.BlockState;
import org.cloudburstmc.math.vector.Vector3d;
import org.cloudburstmc.math.vector.Vector3i;

public class VectorHelper {
private VectorHelper() {}
Expand Down Expand Up @@ -63,4 +64,11 @@ public static Vector3d normalizeSafe(Vector3d vec) {
var length = vec.length();
return length < 1.0E-5F ? Vector3d.ZERO : Vector3d.from(vec.getX() / length, vec.getY() / length, vec.getZ() / length);
}

public static double distToCenterSqr(Vector3i current, Vector3d other) {
var x = current.getX() + 0.5 - other.getX();
var y = current.getY() + 0.5 - other.getY();
var z = current.getZ() + 0.5 - other.getZ();
return x * x + y * y + z * z;
}
}

0 comments on commit 1ecaf99

Please sign in to comment.