Skip to content

Commit

Permalink
Ensure ComplexCollisionBoxes can only contain SimpleCollisionBoxes
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Nov 17, 2024
1 parent 39a812c commit 743a2d9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private static CollisionBox[] makeShapes() {
float f = 0.5F - (float) 0.3125;
float f1 = 0.5F + (float) 0.3125;
SimpleCollisionBox baseShape = new SimpleCollisionBox(f, f, f, f1, f1, f1, false);
CollisionBox[] avoxelshape = new CollisionBox[directions.length];
SimpleCollisionBox[] avoxelshape = new SimpleCollisionBox[directions.length];

for (int i = 0; i < directions.length; ++i) {
BlockFace direction = directions[i];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package ac.grim.grimac.utils.collisions.blocks;

import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.collisions.datatypes.CollisionBox;
import ac.grim.grimac.utils.collisions.datatypes.CollisionFactory;
import ac.grim.grimac.utils.collisions.datatypes.ComplexCollisionBox;
import ac.grim.grimac.utils.collisions.datatypes.HexCollisionBox;
import ac.grim.grimac.utils.collisions.datatypes.*;
import ac.grim.grimac.utils.nmsutil.Materials;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
Expand All @@ -18,16 +15,16 @@
import java.util.stream.IntStream;

public class DynamicStair implements CollisionFactory {
protected static final CollisionBox TOP_AABB = new HexCollisionBox(0, 8, 0, 16, 16, 16);
protected static final CollisionBox BOTTOM_AABB = new HexCollisionBox(0, 0, 0, 16, 8, 16);
protected static final CollisionBox OCTET_NNN = new HexCollisionBox(0.0D, 0.0D, 0.0D, 8.0D, 8.0D, 8.0D);
protected static final CollisionBox OCTET_NNP = new HexCollisionBox(0.0D, 0.0D, 8.0D, 8.0D, 8.0D, 16.0D);
protected static final CollisionBox OCTET_NPN = new HexCollisionBox(0.0D, 8.0D, 0.0D, 8.0D, 16.0D, 8.0D);
protected static final CollisionBox OCTET_NPP = new HexCollisionBox(0.0D, 8.0D, 8.0D, 8.0D, 16.0D, 16.0D);
protected static final CollisionBox OCTET_PNN = new HexCollisionBox(8.0D, 0.0D, 0.0D, 16.0D, 8.0D, 8.0D);
protected static final CollisionBox OCTET_PNP = new HexCollisionBox(8.0D, 0.0D, 8.0D, 16.0D, 8.0D, 16.0D);
protected static final CollisionBox OCTET_PPN = new HexCollisionBox(8.0D, 8.0D, 0.0D, 16.0D, 16.0D, 8.0D);
protected static final CollisionBox OCTET_PPP = new HexCollisionBox(8.0D, 8.0D, 8.0D, 16.0D, 16.0D, 16.0D);
protected static final SimpleCollisionBox TOP_AABB = new HexCollisionBox(0, 8, 0, 16, 16, 16);
protected static final SimpleCollisionBox BOTTOM_AABB = new HexCollisionBox(0, 0, 0, 16, 8, 16);
protected static final SimpleCollisionBox OCTET_NNN = new HexCollisionBox(0.0D, 0.0D, 0.0D, 8.0D, 8.0D, 8.0D);
protected static final SimpleCollisionBox OCTET_NNP = new HexCollisionBox(0.0D, 0.0D, 8.0D, 8.0D, 8.0D, 16.0D);
protected static final SimpleCollisionBox OCTET_NPN = new HexCollisionBox(0.0D, 8.0D, 0.0D, 8.0D, 16.0D, 8.0D);
protected static final SimpleCollisionBox OCTET_NPP = new HexCollisionBox(0.0D, 8.0D, 8.0D, 8.0D, 16.0D, 16.0D);
protected static final SimpleCollisionBox OCTET_PNN = new HexCollisionBox(8.0D, 0.0D, 0.0D, 16.0D, 8.0D, 8.0D);
protected static final SimpleCollisionBox OCTET_PNP = new HexCollisionBox(8.0D, 0.0D, 8.0D, 16.0D, 8.0D, 16.0D);
protected static final SimpleCollisionBox OCTET_PPN = new HexCollisionBox(8.0D, 8.0D, 0.0D, 16.0D, 16.0D, 8.0D);
protected static final SimpleCollisionBox OCTET_PPP = new HexCollisionBox(8.0D, 8.0D, 8.0D, 16.0D, 16.0D, 16.0D);
protected static final CollisionBox[] TOP_SHAPES = makeShapes(TOP_AABB, OCTET_NNN, OCTET_PNN, OCTET_NNP, OCTET_PNP);
protected static final CollisionBox[] BOTTOM_SHAPES = makeShapes(BOTTOM_AABB, OCTET_NPN, OCTET_PPN, OCTET_NPP, OCTET_PPP);
private static final int[] SHAPE_BY_STATE = new int[]{12, 5, 3, 10, 14, 13, 7, 11, 13, 7, 11, 14, 8, 4, 1, 2, 4, 1, 2, 8};
Expand Down Expand Up @@ -85,11 +82,11 @@ private static BlockFace rotateYCCW(BlockFace face) {
};
}

private static CollisionBox[] makeShapes(CollisionBox p_199779_0_, CollisionBox p_199779_1_, CollisionBox p_199779_2_, CollisionBox p_199779_3_, CollisionBox p_199779_4_) {
private static CollisionBox[] makeShapes(SimpleCollisionBox p_199779_0_, SimpleCollisionBox p_199779_1_, SimpleCollisionBox p_199779_2_, SimpleCollisionBox p_199779_3_, SimpleCollisionBox p_199779_4_) {
return IntStream.range(0, 16).mapToObj((p_199780_5_) -> makeStairShape(p_199780_5_, p_199779_0_, p_199779_1_, p_199779_2_, p_199779_3_, p_199779_4_)).toArray(CollisionBox[]::new);
}

private static CollisionBox makeStairShape(int p_199781_0_, CollisionBox p_199781_1_, CollisionBox p_199781_2_, CollisionBox p_199781_3_, CollisionBox p_199781_4_, CollisionBox p_199781_5_) {
private static CollisionBox makeStairShape(int p_199781_0_, SimpleCollisionBox p_199781_1_, SimpleCollisionBox p_199781_2_, SimpleCollisionBox p_199781_3_, SimpleCollisionBox p_199781_4_, SimpleCollisionBox p_199781_5_) {
ComplexCollisionBox voxelshape = new ComplexCollisionBox(p_199781_1_);
if ((p_199781_0_ & 1) != 0) {
voxelshape.add(p_199781_2_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ public CollisionBox fetch(GrimPlayer player, ClientVersion version, WrappedBlock
if (player.getClientVersion().isNewerThanOrEquals(ClientVersion.V_1_13)) {
// Proper and faster way would be to compute all this beforehand
if (up) {
ComplexCollisionBox box = new ComplexCollisionBox(COLLISION_BOXES[getAABBIndex(north, east, south, west)].copy());
box.add(new HexCollisionBox(4, 0, 4, 12, 24, 12));
return box;
return COLLISION_BOXES[getAABBIndex(north, east, south, west)].copy().union(new HexCollisionBox(4, 0, 4, 12, 24, 12));
}

return COLLISION_BOXES[getAABBIndex(north, east, south, west)].copy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ public static CollisionBox[] makeShapes(float p_196408_1_, float p_196408_2_, fl
ComplexCollisionBox voxelshape5 = new ComplexCollisionBox(voxelshape1, voxelshape4);
ComplexCollisionBox voxelshape6 = new ComplexCollisionBox(voxelshape2, voxelshape3);

CollisionBox[] avoxelshape = new CollisionBox[]{NoCollisionBox.INSTANCE, voxelshape2, voxelshape3, voxelshape6, voxelshape1, new ComplexCollisionBox(voxelshape2, voxelshape1), new ComplexCollisionBox(voxelshape3, voxelshape1), new ComplexCollisionBox(voxelshape6, voxelshape1), voxelshape4, new ComplexCollisionBox(voxelshape2, voxelshape4), new ComplexCollisionBox(voxelshape3, voxelshape4), new ComplexCollisionBox(voxelshape6, voxelshape4), voxelshape5, new ComplexCollisionBox(voxelshape2, voxelshape5), new ComplexCollisionBox(voxelshape3, voxelshape5), new ComplexCollisionBox(voxelshape6, voxelshape5)};
CollisionBox[] avoxelshape = new CollisionBox[]{
NoCollisionBox.INSTANCE, voxelshape2, voxelshape3, voxelshape6, voxelshape1,
new ComplexCollisionBox(voxelshape2, voxelshape1), new ComplexCollisionBox(voxelshape3, voxelshape1),
new ComplexCollisionBox(voxelshape2, voxelshape3, voxelshape1), voxelshape4,
new ComplexCollisionBox(voxelshape2, voxelshape4), new ComplexCollisionBox(voxelshape3, voxelshape4),
new ComplexCollisionBox(voxelshape2, voxelshape3, voxelshape4), voxelshape5,
new ComplexCollisionBox(voxelshape2, voxelshape1, voxelshape4), new ComplexCollisionBox(voxelshape3, voxelshape1, voxelshape4),
new ComplexCollisionBox(voxelshape1, voxelshape2, voxelshape3, voxelshape4)};

if (includeCenter) {
for (int i = 0; i < 16; ++i) {
avoxelshape[i] = new ComplexCollisionBox(up, avoxelshape[i]);
avoxelshape[i] = avoxelshape[i].union(up);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

public class ComplexCollisionBox implements CollisionBox {

private final List<CollisionBox> boxes = new ArrayList<>();
private final List<SimpleCollisionBox> boxes = new ArrayList<>();

public ComplexCollisionBox(CollisionBox... boxes) {
public ComplexCollisionBox(SimpleCollisionBox... boxes) {
Collections.addAll(this.boxes, boxes);
}

public boolean add(CollisionBox collisionBox) {
public boolean add(SimpleCollisionBox collisionBox) {
return boxes.add(collisionBox);
}

Expand Down Expand Up @@ -42,7 +42,7 @@ public boolean isIntersected(SimpleCollisionBox other) {
@Override
public CollisionBox copy() {
ComplexCollisionBox cc = new ComplexCollisionBox();
for (CollisionBox b : boxes)
for (SimpleCollisionBox b : boxes)
cc.boxes.add(b.copy());
return cc;
}
Expand Down

0 comments on commit 743a2d9

Please sign in to comment.