General cleanups to the movement system.

This commit is contained in:
Brady
2018-08-07 16:14:36 -05:00
parent 732ab5273c
commit fc082f42e7
8 changed files with 112 additions and 110 deletions

View File

@@ -17,6 +17,7 @@ import static baritone.bot.InputOverrideHandler.Input;
public abstract class Movement implements Helper, MovementHelper {
private MovementState currentState = new MovementState().setStatus(MovementStatus.PREPPING);
protected final BlockPos src;
protected final BlockPos dest;
@@ -24,12 +25,12 @@ public abstract class Movement implements Helper, MovementHelper {
/**
* The positions that need to be broken before this movement can ensue
*/
public final BlockPos[] positionsToBreak;
protected final BlockPos[] positionsToBreak;
/**
* The positions where we need to place a block before this movement can ensue
*/
public final BlockPos[] positionsToPlace;
protected final BlockPos[] positionsToPlace;
private Double cost;
@@ -75,11 +76,11 @@ public abstract class Movement implements Helper, MovementHelper {
latestState.getTarget().getRotation().ifPresent(LookBehavior.INSTANCE::updateTarget);
// TODO: calculate movement inputs from latestState.getGoal().position
// latestState.getTarget().position.ifPresent(null); NULL CONSUMER REALLY SHOULDN'T BE THE FINAL THING YOU SHOULD REALLY REPLACE THIS WITH ALMOST ACTUALLY ANYTHING ELSE JUST PLEASE DON'T LEAVE IT AS IT IS THANK YOU KANYE
latestState.inputState.forEach((input, forced) -> {
latestState.getInputStates().forEach((input, forced) -> {
Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced);
System.out.println(input + " AND " + forced);
});
latestState.inputState.replaceAll((input, forced) -> false);
latestState.getInputStates().replaceAll((input, forced) -> false);
currentState = latestState;
if (isFinished())
@@ -88,7 +89,6 @@ public abstract class Movement implements Helper, MovementHelper {
return currentState.getStatus();
}
private boolean prepared(MovementState state) {
if (state.getStatus() == MovementStatus.WAITING)
return true;
@@ -123,14 +123,14 @@ public abstract class Movement implements Helper, MovementHelper {
* Run cleanup on state finish and declare success.
*/
public void onFinish(MovementState state) {
state.inputState.replaceAll((input, forced) -> false);
state.inputState.forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
state.getInputStates().replaceAll((input, forced) -> false);
state.getInputStates().forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
state.setStatus(MovementStatus.SUCCESS);
}
public void cancel() {
currentState.inputState.replaceAll((input, forced) -> false);
currentState.inputState.forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
currentState.getInputStates().replaceAll((input, forced) -> false);
currentState.getInputStates().forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
currentState.setStatus(MovementStatus.CANCELED);
}

View File

@@ -13,7 +13,7 @@ public class MovementState {
private MovementStatus status;
private MovementTarget goal = new MovementTarget();
private MovementTarget target = new MovementTarget();
protected final Map<Input, Boolean> inputState = new HashMap<>();
private final Map<Input, Boolean> inputState = new HashMap<>();
public MovementState setStatus(MovementStatus status) {
this.status = status;
@@ -24,13 +24,50 @@ public class MovementState {
return status;
}
public MovementTarget getGoal() {
return this.goal;
}
public MovementState setGoal(MovementTarget goal) {
this.goal = goal;
return this;
}
public MovementTarget getTarget() {
return this.target;
}
public MovementState setTarget(MovementTarget target) {
this.target = target;
return this;
}
public MovementState setInput(Input input, boolean forced) {
this.inputState.put(input, forced);
return this;
}
public boolean getInput(Input input) {
return this.inputState.getOrDefault(input, false);
}
public Map<Input, Boolean> getInputStates() {
return this.inputState;
}
public enum MovementStatus {
PREPPING, WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED, CANCELED
}
public static class MovementTarget {
/**
* Necessary movement to achieve
* <p>
* TODO: Decide desiredMovement type
*/
public Vec3d position;
/**
* Yaw and pitch angles that must be matched
* <p>
@@ -64,35 +101,4 @@ public class MovementState {
return Optional.ofNullable(this.rotation);
}
}
public MovementTarget getGoal() {
return goal;
}
public MovementState setGoal(MovementTarget goal) {
this.goal = goal;
return this;
}
public MovementTarget getTarget() {
return target;
}
public MovementState setTarget(MovementTarget target) {
this.target = target;
return this;
}
public MovementState setInput(Input input, boolean forced) {
inputState.put(input, forced);
return this;
}
public boolean getInput(Input input) {
return inputState.getOrDefault(input, false);
}
public enum MovementStatus {
PREPPING, WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED, CANCELED;
}
}

View File

@@ -11,29 +11,26 @@ import net.minecraft.block.BlockFalling;
import net.minecraft.util.math.BlockPos;
public class MovementAscend extends Movement {
BlockPos[] against = new BlockPos[3];
private BlockPos[] against = new BlockPos[3];
public MovementAscend(BlockPos src, BlockPos dest) {
super(src, dest, new BlockPos[] { dest, src.up(2), dest.up() }, new BlockPos[] { dest.down() });
BlockPos placementLocation = positionsToPlace[0];//end.down()
BlockPos placementLocation = positionsToPlace[0]; // dest.down()
int i = 0;
if (!placementLocation.north().equals(src)) {
against[i] = placementLocation.north();
i++;
}
if (!placementLocation.south().equals(src)) {
against[i] = placementLocation.south();
i++;
}
if (!placementLocation.east().equals(src)) {
against[i] = placementLocation.east();
i++;
}
if (!placementLocation.west().equals(src)) {
if (!placementLocation.north().equals(src))
against[i++] = placementLocation.north();
if (!placementLocation.south().equals(src))
against[i++] = placementLocation.south();
if (!placementLocation.east().equals(src))
against[i++] = placementLocation.east();
if (!placementLocation.west().equals(src))
against[i] = placementLocation.west();
i++;
}
// TODO: add ability to place against .down() as well as the cardinal directions
// useful for when you are starting a staircase without anything to place against
// Counterpoint to the above TODO ^ you should move then pillar instead of ascend

View File

@@ -13,6 +13,7 @@ import net.minecraft.block.BlockVine;
import net.minecraft.util.math.BlockPos;
public class MovementDescend extends Movement {
public MovementDescend(BlockPos start, BlockPos end) {
super(start, end, new BlockPos[] { end.up(2), end.up(), end }, new BlockPos[] { end.down() });
}

View File

@@ -9,6 +9,7 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
public class MovementDiagonal extends Movement {
public MovementDiagonal(BlockPos start, EnumFacing dir1, EnumFacing dir2) {
this(start, start.offset(dir1), start.offset(dir2), dir2);
// super(start, start.offset(dir1).offset(dir2), new BlockPos[]{start.offset(dir1), start.offset(dir1).up(), start.offset(dir2), start.offset(dir2).up(), start.offset(dir1).offset(dir2), start.offset(dir1).offset(dir2).up()}, new BlockPos[]{start.offset(dir1).offset(dir2).down()});

View File

@@ -11,12 +11,13 @@ import net.minecraft.block.BlockVine;
import net.minecraft.util.math.BlockPos;
public class MovementDownward extends Movement {
private int numTicks = 0;
public MovementDownward(BlockPos start) {
super(start, start.down(), new BlockPos[]{start.down()}, new BlockPos[0]);
}
int numTicks = 0;
@Override
public MovementState updateState(MovementState state) {
super.updateState(state);

View File

@@ -20,19 +20,6 @@ import net.minecraft.util.math.Vec3d;
public class MovementFall extends Movement {
private static BlockPos[] buildPositionsToBreak(BlockPos src, BlockPos dest) {
BlockPos[] toBreak;
int diffX = src.getX() - dest.getX();
int diffZ = src.getZ() - dest.getZ();
int diffY = src.getY() - dest.getY();
toBreak = new BlockPos[diffY + 2];
for (int i = 0; i < toBreak.length; i++) {
toBreak[i] = new BlockPos(src.getX() - diffX, src.getY() + 1 - i, src.getZ() - diffZ);
}
return toBreak;
}
public MovementFall(BlockPos src, BlockPos dest) {
super(src, dest, MovementFall.buildPositionsToBreak(src, dest), new BlockPos[] { dest.down() });
}
@@ -92,4 +79,15 @@ public class MovementFall extends Movement {
}
}
private static BlockPos[] buildPositionsToBreak(BlockPos src, BlockPos dest) {
BlockPos[] toBreak;
int diffX = src.getX() - dest.getX();
int diffZ = src.getZ() - dest.getZ();
int diffY = src.getY() - dest.getY();
toBreak = new BlockPos[diffY + 2];
for (int i = 0; i < toBreak.length; i++) {
toBreak[i] = new BlockPos(src.getX() - diffX, src.getY() + 1 - i, src.getZ() - diffZ);
}
return toBreak;
}
}

View File

@@ -22,27 +22,28 @@ import java.util.Objects;
public class MovementTraverse extends Movement {
BlockPos[] against = new BlockPos[3];
private BlockPos[] against = new BlockPos[3];
/**
* Did we have to place a bridge block or was it always there
*/
private boolean wasTheBridgeBlockAlwaysThere = true;
public MovementTraverse(BlockPos from, BlockPos to) {
super(from, to, new BlockPos[]{to.up(), to}, new BlockPos[]{to.down()});
int i = 0;
if (!to.north().equals(from)) {
against[i] = to.north().down();
i++;
}
if (!to.south().equals(from)) {
against[i] = to.south().down();
i++;
}
if (!to.east().equals(from)) {
against[i] = to.east().down();
i++;
}
if (!to.west().equals(from)) {
if (!to.north().equals(from))
against[i++] = to.north().down();
if (!to.south().equals(from))
against[i++] = to.south().down();
if (!to.east().equals(from))
against[i++] = to.east().down();
if (!to.west().equals(from))
against[i] = to.west().down();
i++;
}
//note: do NOT add ability to place against .down().down()
}
@@ -80,8 +81,6 @@ public class MovementTraverse extends Movement {
}
}
boolean wasTheBridgeBlockAlwaysThere = true;//did we have to place a bridge block or was it always there
@Override
public MovementState updateState(MovementState state) {
super.updateState(state);
@@ -174,6 +173,5 @@ public class MovementTraverse extends Movement {
// TODO MovementManager.moveTowardsBlock(to); // move towards not look at because if we are bridging for a couple blocks in a row, it is faster if we dont spin around and walk forwards then spin around and place backwards for every block
}
}
}
}