Skip to content

Commit

Permalink
Remake a few enums
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 17, 2024
1 parent d8de6df commit 7b17fca
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.soulfiremc.server.data.BlockState;
import com.soulfiremc.server.pathfinding.SFVec3i;
import com.soulfiremc.server.pathfinding.graph.actions.movement.BodyPart;
import com.soulfiremc.server.pathfinding.graph.actions.movement.MovementDirection;
import com.soulfiremc.server.pathfinding.graph.actions.movement.DiagonalDirection;
import com.soulfiremc.server.pathfinding.graph.actions.movement.MovementSide;
import com.soulfiremc.server.protocol.bot.block.GlobalBlockPalette;
import com.soulfiremc.server.protocol.bot.state.entity.Player;
Expand All @@ -31,9 +31,9 @@ public class DiagonalCollisionCalculator {
private static final Vector3d START_POSITION = Vector3d.from(0.5, 0, 0.5);
private static final Vector3d[] STEPS = new Vector3d[]{Vector3d.from(0.25, 0, 0.25), Vector3d.from(0.5, 0, 0.5), Vector3d.from(0.75, 0, 0.75)};
private static final IDMap<BlockState, boolean[][][]> COLLISIONS = new IDMap<>(GlobalBlockPalette.INSTANCE.getBlockStates(), blockState -> {
var diagonalsArray = new boolean[MovementDirection.DIAGONALS.length][][];
var diagonalsArray = new boolean[DiagonalDirection.VALUES.length][][];

for (var diagonal : MovementDirection.DIAGONALS) {
for (var diagonal : DiagonalDirection.VALUES) {
var baseOffset = diagonal.offset(SFVec3i.ZERO);

var bodyPartArray = new boolean[BodyPart.VALUES.length][];
Expand All @@ -60,7 +60,7 @@ public class DiagonalCollisionCalculator {
bodyPartArray[bodyPart.ordinal()] = sideArray;
}

diagonalsArray[diagonal.diagonalArrayIndex()] = bodyPartArray;
diagonalsArray[diagonal.ordinal()] = bodyPartArray;
}

return diagonalsArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private int registerRequiredFreeBlocks(SubscriptionConsumer blockSubscribers) {
if (diagonal) {
blockBreakSideHint = null; // We don't mine blocks in diagonals
} else {
blockBreakSideHint = direction.toBlockFace();
blockBreakSideHint = direction.toSkyDirection().blockFace();
}

var blockIndex = blockIndexCounter++;
Expand Down Expand Up @@ -207,7 +207,7 @@ private void registerDiagonalCollisionBlocks(SubscriptionConsumer blockSubscribe

for (var side : MovementSide.VALUES) {
// If these blocks are solid, the bot moves slower because the bot is running around a corner
var corner = modifier.offsetIfJump(direction.side(side).offset(FEET_POSITION_RELATIVE_BLOCK));
var corner = modifier.offsetIfJump(direction.toDiagonalDirection().side(side).offset(FEET_POSITION_RELATIVE_BLOCK));
for (var bodyOffset : BodyPart.VALUES) {
// Apply jump shift to target edge and offset for body part
blockSubscribers.subscribe(bodyOffset.offset(corner), new MovementDiagonalCollisionSubscription(
Expand Down Expand Up @@ -236,7 +236,7 @@ private void registerPossibleBlocksToPlaceAgainst(SubscriptionConsumer blockSubs
blockSubscribers.subscribe(floorBlock.sub(0, 1, 0), new MovementAgainstPlaceSolidSubscription(BlockFace.TOP));

for (var skyDirection : SkyDirection.VALUES) {
blockSubscribers.subscribe(skyDirection.offset(floorBlock), new MovementAgainstPlaceSolidSubscription(skyDirection.opposite().toBlockFace()));
blockSubscribers.subscribe(skyDirection.offset(floorBlock), new MovementAgainstPlaceSolidSubscription(skyDirection.opposite().blockFace()));
}
}
case JUMP_UP_BLOCK, FALL_1 -> { // 4 - no scaffolding
Expand All @@ -248,7 +248,7 @@ private void registerPossibleBlocksToPlaceAgainst(SubscriptionConsumer blockSubs
continue;
}

blockSubscribers.subscribe(skyDirection.offset(floorBlock), new MovementAgainstPlaceSolidSubscription(skyDirection.opposite().toBlockFace()));
blockSubscribers.subscribe(skyDirection.offset(floorBlock), new MovementAgainstPlaceSolidSubscription(skyDirection.opposite().blockFace()));
}
}
default -> throw new IllegalStateException("Unexpected value: " + modifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@

@RequiredArgsConstructor
public enum BodyPart {
HEAD,
FEET;
HEAD {
@Override
public SFVec3i offset(SFVec3i position) {
return position.add(0, 1, 0);
}
},
FEET {
@Override
public SFVec3i offset(SFVec3i position) {
return position;
}
};

public static final BodyPart[] VALUES = values();

public SFVec3i offset(SFVec3i position) {
return switch (this) {
case HEAD -> position.add(0, 1, 0);
case FEET -> position;
};
}
public abstract SFVec3i offset(SFVec3i position);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SoulFire
* Copyright (C) 2024 AlexProgrammerDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.soulfiremc.server.pathfinding.graph.actions.movement;

import com.soulfiremc.server.pathfinding.SFVec3i;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum DiagonalDirection {
NORTH_EAST(new SFVec3i(1, 0, -1), SkyDirection.NORTH, SkyDirection.EAST),
NORTH_WEST(new SFVec3i(-1, 0, -1), SkyDirection.NORTH, SkyDirection.WEST),
SOUTH_EAST(new SFVec3i(1, 0, 1), SkyDirection.SOUTH, SkyDirection.EAST),
SOUTH_WEST(new SFVec3i(-1, 0, 1), SkyDirection.SOUTH, SkyDirection.WEST);

public static final DiagonalDirection[] VALUES = values();
private final SFVec3i offsetVector;
private final SkyDirection leftSide;
private final SkyDirection rightSide;

public SFVec3i offset(SFVec3i vector) {
return vector.add(offsetVector);
}

public SkyDirection side(MovementSide side) {
if (side == MovementSide.LEFT) {
return leftSide;
} else {
return rightSide;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,28 @@
package com.soulfiremc.server.pathfinding.graph.actions.movement;

import com.soulfiremc.server.pathfinding.SFVec3i;
import com.soulfiremc.server.pathfinding.graph.BlockFace;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@RequiredArgsConstructor
public enum MovementDirection {
NORTH,
SOUTH,
EAST,
WEST,
NORTH_EAST,
NORTH_WEST,
SOUTH_EAST,
SOUTH_WEST;
NORTH(new SFVec3i(0, 0, -1)),
SOUTH(new SFVec3i(0, 0, 1)),
EAST(new SFVec3i(1, 0, 0)),
WEST(new SFVec3i(-1, 0, 0)),
NORTH_EAST(new SFVec3i(1, 0, -1)),
NORTH_WEST(new SFVec3i(-1, 0, -1)),
SOUTH_EAST(new SFVec3i(1, 0, 1)),
SOUTH_WEST(new SFVec3i(-1, 0, 1));

public static final MovementDirection[] VALUES = values();
public static final MovementDirection[] DIAGONALS = {NORTH_EAST, NORTH_WEST, SOUTH_EAST, SOUTH_WEST};

private final SFVec3i offsetVector;
private int diagonalArrayIndex;

static {
for (var direction : VALUES) {
direction.diagonalArrayIndex = List.of(DIAGONALS).indexOf(direction);
}
}

public SkyDirection side(MovementSide side) {
return switch (this) {
case NORTH_EAST -> switch (side) {
case LEFT -> SkyDirection.NORTH;
case RIGHT -> SkyDirection.EAST;
};
case NORTH_WEST -> switch (side) {
case LEFT -> SkyDirection.NORTH;
case RIGHT -> SkyDirection.WEST;
};
case SOUTH_EAST -> switch (side) {
case LEFT -> SkyDirection.SOUTH;
case RIGHT -> SkyDirection.EAST;
};
case SOUTH_WEST -> switch (side) {
case LEFT -> SkyDirection.SOUTH;
case RIGHT -> SkyDirection.WEST;
};
default -> throw new IllegalStateException("Unexpected value: " + this);
};
public SFVec3i offset(SFVec3i vector) {
return vector.add(offsetVector);
}

public SkyDirection toSkyDirection() {
Expand All @@ -79,30 +52,17 @@ public SkyDirection toSkyDirection() {
};
}

public BlockFace toBlockFace() {
public DiagonalDirection toDiagonalDirection() {
return switch (this) {
case NORTH -> BlockFace.NORTH;
case SOUTH -> BlockFace.SOUTH;
case EAST -> BlockFace.EAST;
case WEST -> BlockFace.WEST;
case NORTH_EAST -> DiagonalDirection.NORTH_EAST;
case NORTH_WEST -> DiagonalDirection.NORTH_WEST;
case SOUTH_EAST -> DiagonalDirection.SOUTH_EAST;
case SOUTH_WEST -> DiagonalDirection.SOUTH_WEST;
default -> throw new IllegalStateException("Unexpected value: " + this);
};
}

public SFVec3i offset(SFVec3i vector) {
return switch (this) {
case NORTH -> vector.add(0, 0, -1);
case SOUTH -> vector.add(0, 0, 1);
case EAST -> vector.add(1, 0, 0);
case WEST -> vector.add(-1, 0, 0);
case NORTH_EAST -> vector.add(1, 0, -1);
case NORTH_WEST -> vector.add(-1, 0, -1);
case SOUTH_EAST -> vector.add(1, 0, 1);
case SOUTH_WEST -> vector.add(-1, 0, 1);
};
}

public boolean isDiagonal() {
return diagonalArrayIndex != -1;
return this == NORTH_EAST || this == NORTH_WEST || this == SOUTH_EAST || this == SOUTH_WEST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,22 @@
package com.soulfiremc.server.pathfinding.graph.actions.movement;

import com.soulfiremc.server.pathfinding.SFVec3i;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum ParkourDirection {
NORTH,
SOUTH,
EAST,
WEST;
NORTH(new SFVec3i(0, 0, -1)),
SOUTH(new SFVec3i(0, 0, 1)),
EAST(new SFVec3i(1, 0, 0)),
WEST(new SFVec3i(-1, 0, 0));

public static final ParkourDirection[] VALUES = values();
private final SFVec3i offsetVector;

@SuppressWarnings("DuplicatedCode")
public SFVec3i offset(SFVec3i vector) {
return switch (this) {
case NORTH -> vector.add(0, 0, -1);
case SOUTH -> vector.add(0, 0, 1);
case EAST -> vector.add(1, 0, 0);
case WEST -> vector.add(-1, 0, 0);
};
}

public SkyDirection toSkyDirection() {
return switch (this) {
case NORTH -> SkyDirection.NORTH;
case SOUTH -> SkyDirection.SOUTH;
case EAST -> SkyDirection.EAST;
case WEST -> SkyDirection.WEST;
};
return vector.add(offsetVector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@
@Getter
@RequiredArgsConstructor
public enum SkyDirection {
NORTH(Direction.NORTH),
SOUTH(Direction.SOUTH),
EAST(Direction.EAST),
WEST(Direction.WEST);
NORTH(new SFVec3i(0, 0, -1), Direction.NORTH, BlockFace.NORTH),
SOUTH(new SFVec3i(0, 0, 1), Direction.SOUTH, BlockFace.SOUTH),
EAST(new SFVec3i(1, 0, 0), Direction.EAST, BlockFace.EAST),
WEST(new SFVec3i(-1, 0, 0), Direction.WEST, BlockFace.WEST);

public static final SkyDirection[] VALUES = values();
private final SFVec3i offsetVector;
private final Direction direction;
private final BlockFace blockFace;

@SuppressWarnings("DuplicatedCode")
public SFVec3i offset(SFVec3i vector) {
return switch (this) {
case NORTH -> vector.add(0, 0, -1);
case SOUTH -> vector.add(0, 0, 1);
case EAST -> vector.add(1, 0, 0);
case WEST -> vector.add(-1, 0, 0);
};
return vector.add(offsetVector);
}

public SkyDirection opposite() {
Expand All @@ -52,31 +49,4 @@ public SkyDirection opposite() {
case WEST -> EAST;
};
}

public SkyDirection leftSide() {
return switch (this) {
case NORTH -> WEST;
case SOUTH -> EAST;
case EAST -> NORTH;
case WEST -> SOUTH;
};
}

public SkyDirection rightSide() {
return switch (this) {
case NORTH -> EAST;
case SOUTH -> WEST;
case EAST -> SOUTH;
case WEST -> NORTH;
};
}

public BlockFace toBlockFace() {
return switch (this) {
case NORTH -> BlockFace.NORTH;
case SOUTH -> BlockFace.SOUTH;
case EAST -> BlockFace.EAST;
case WEST -> BlockFace.WEST;
};
}
}

0 comments on commit 7b17fca

Please sign in to comment.