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 { public abstract class Movement implements Helper, MovementHelper {
private MovementState currentState = new MovementState().setStatus(MovementStatus.PREPPING); private MovementState currentState = new MovementState().setStatus(MovementStatus.PREPPING);
protected final BlockPos src; protected final BlockPos src;
protected final BlockPos dest; 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 * 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 * 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; private Double cost;
@@ -75,11 +76,11 @@ public abstract class Movement implements Helper, MovementHelper {
latestState.getTarget().getRotation().ifPresent(LookBehavior.INSTANCE::updateTarget); latestState.getTarget().getRotation().ifPresent(LookBehavior.INSTANCE::updateTarget);
// TODO: calculate movement inputs from latestState.getGoal().position // 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.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); Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced);
System.out.println(input + " AND " + forced); System.out.println(input + " AND " + forced);
}); });
latestState.inputState.replaceAll((input, forced) -> false); latestState.getInputStates().replaceAll((input, forced) -> false);
currentState = latestState; currentState = latestState;
if (isFinished()) if (isFinished())
@@ -88,7 +89,6 @@ public abstract class Movement implements Helper, MovementHelper {
return currentState.getStatus(); return currentState.getStatus();
} }
private boolean prepared(MovementState state) { private boolean prepared(MovementState state) {
if (state.getStatus() == MovementStatus.WAITING) if (state.getStatus() == MovementStatus.WAITING)
return true; return true;
@@ -123,14 +123,14 @@ public abstract class Movement implements Helper, MovementHelper {
* Run cleanup on state finish and declare success. * Run cleanup on state finish and declare success.
*/ */
public void onFinish(MovementState state) { public void onFinish(MovementState state) {
state.inputState.replaceAll((input, forced) -> false); state.getInputStates().replaceAll((input, forced) -> false);
state.inputState.forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced)); state.getInputStates().forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
state.setStatus(MovementStatus.SUCCESS); state.setStatus(MovementStatus.SUCCESS);
} }
public void cancel() { public void cancel() {
currentState.inputState.replaceAll((input, forced) -> false); currentState.getInputStates().replaceAll((input, forced) -> false);
currentState.inputState.forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced)); currentState.getInputStates().forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
currentState.setStatus(MovementStatus.CANCELED); currentState.setStatus(MovementStatus.CANCELED);
} }

View File

@@ -13,7 +13,7 @@ public class MovementState {
private MovementStatus status; private MovementStatus status;
private MovementTarget goal = new MovementTarget(); private MovementTarget goal = new MovementTarget();
private MovementTarget target = 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) { public MovementState setStatus(MovementStatus status) {
this.status = status; this.status = status;
@@ -24,13 +24,50 @@ public class MovementState {
return status; 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 { public static class MovementTarget {
/** /**
* Necessary movement to achieve * Necessary movement to achieve
* <p> * <p>
* TODO: Decide desiredMovement type * TODO: Decide desiredMovement type
*/ */
public Vec3d position; public Vec3d position;
/** /**
* Yaw and pitch angles that must be matched * Yaw and pitch angles that must be matched
* <p> * <p>
@@ -64,35 +101,4 @@ public class MovementState {
return Optional.ofNullable(this.rotation); 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,31 +11,28 @@ import net.minecraft.block.BlockFalling;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
public class MovementAscend extends Movement { public class MovementAscend extends Movement {
BlockPos[] against = new BlockPos[3];
private BlockPos[] against = new BlockPos[3];
public MovementAscend(BlockPos src, BlockPos dest) { public MovementAscend(BlockPos src, BlockPos dest) {
super(src, dest, new BlockPos[]{dest, src.up(2), dest.up()}, new BlockPos[]{dest.down()}); 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; int i = 0;
if (!placementLocation.north().equals(src)) { if (!placementLocation.north().equals(src))
against[i] = placementLocation.north(); against[i++] = placementLocation.north();
i++;
} if (!placementLocation.south().equals(src))
if (!placementLocation.south().equals(src)) { against[i++] = placementLocation.south();
against[i] = placementLocation.south();
i++; if (!placementLocation.east().equals(src))
} against[i++] = placementLocation.east();
if (!placementLocation.east().equals(src)) {
against[i] = placementLocation.east(); if (!placementLocation.west().equals(src))
i++;
}
if (!placementLocation.west().equals(src)) {
against[i] = placementLocation.west(); against[i] = placementLocation.west();
i++;
} // TODO: add ability to place against .down() as well as the cardinal directions
//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
//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 // Counterpoint to the above TODO ^ you should move then pillar instead of ascend
} }

View File

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

View File

@@ -9,9 +9,10 @@ import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
public class MovementDiagonal extends Movement { public class MovementDiagonal extends Movement {
public MovementDiagonal(BlockPos start, EnumFacing dir1, EnumFacing dir2) { public MovementDiagonal(BlockPos start, EnumFacing dir1, EnumFacing dir2) {
this(start, start.offset(dir1), start.offset(dir2), 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()}); // 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()});
} }
public MovementDiagonal(BlockPos start, BlockPos dir1, BlockPos dir2, EnumFacing drr2) { public MovementDiagonal(BlockPos start, BlockPos dir1, BlockPos dir2, EnumFacing drr2) {
@@ -19,7 +20,7 @@ public class MovementDiagonal extends Movement {
} }
public MovementDiagonal(BlockPos start, BlockPos end, BlockPos dir1, BlockPos dir2) { public MovementDiagonal(BlockPos start, BlockPos end, BlockPos dir1, BlockPos dir2) {
super(start, end, new BlockPos[]{dir1, dir1.up(), dir2, dir2.up(), end, end.up()}, new BlockPos[]{end.down()}); super(start, end, new BlockPos[] { dir1, dir1.up(), dir2, dir2.up(), end, end.up() }, new BlockPos[] { end.down() });
} }
@Override @Override

View File

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

View File

@@ -20,21 +20,8 @@ import net.minecraft.util.math.Vec3d;
public class MovementFall extends Movement { 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) { public MovementFall(BlockPos src, BlockPos dest) {
super(src, dest, MovementFall.buildPositionsToBreak(src, dest), new BlockPos[]{dest.down()}); super(src, dest, MovementFall.buildPositionsToBreak(src, dest), new BlockPos[] { dest.down() });
} }
@Override @Override
@@ -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 { 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) { public MovementTraverse(BlockPos from, BlockPos to) {
super(from, to, new BlockPos[]{to.up(), to}, new BlockPos[]{to.down()}); super(from, to, new BlockPos[]{to.up(), to}, new BlockPos[]{to.down()});
int i = 0; int i = 0;
if (!to.north().equals(from)) { if (!to.north().equals(from))
against[i] = to.north().down(); against[i++] = to.north().down();
i++;
} if (!to.south().equals(from))
if (!to.south().equals(from)) { against[i++] = to.south().down();
against[i] = to.south().down();
i++; if (!to.east().equals(from))
} against[i++] = to.east().down();
if (!to.east().equals(from)) {
against[i] = to.east().down(); if (!to.west().equals(from))
i++;
}
if (!to.west().equals(from)) {
against[i] = to.west().down(); against[i] = to.west().down();
i++;
}
//note: do NOT add ability to place against .down().down() //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 @Override
public MovementState updateState(MovementState state) { public MovementState updateState(MovementState state) {
super.updateState(state); super.updateState(state);
@@ -115,7 +114,7 @@ public class MovementTraverse extends Movement {
return state; return state;
} }
if (wasTheBridgeBlockAlwaysThere) { if (wasTheBridgeBlockAlwaysThere) {
//player().setSprinting(true); // player().setSprinting(true);
} }
moveTowards(positionsToBreak[0]); moveTowards(positionsToBreak[0]);
return state; return state;
@@ -123,7 +122,7 @@ public class MovementTraverse extends Movement {
wasTheBridgeBlockAlwaysThere = false; wasTheBridgeBlockAlwaysThere = false;
for (BlockPos against1 : against) { for (BlockPos against1 : against) {
if (BlockStateInterface.get(against1).isBlockNormalCube()) { if (BlockStateInterface.get(against1).isBlockNormalCube()) {
if (!MovementHelper.switchtothrowaway()) {//get ready to place a throwaway block if (!MovementHelper.switchtothrowaway()) { // get ready to place a throwaway block
displayChatMessageRaw("bb pls get me some blocks. dirt or cobble"); displayChatMessageRaw("bb pls get me some blocks. dirt or cobble");
return state; return state;
} }
@@ -138,7 +137,7 @@ public class MovementTraverse extends Movement {
if (LookBehaviorUtils.getSelectedBlock().get().offset(side).equals(positionsToPlace[0])) { if (LookBehaviorUtils.getSelectedBlock().get().offset(side).equals(positionsToPlace[0])) {
state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true);
} else { } else {
//Out.gui("Wrong. " + side + " " + LookBehaviorUtils.getSelectedBlock().get().offset(side) + " " + positionsToPlace[0], Out.Mode.Debug); // Out.gui("Wrong. " + side + " " + LookBehaviorUtils.getSelectedBlock().get().offset(side) + " " + positionsToPlace[0], Out.Mode.Debug);
} }
} }
System.out.println("Trying to look at " + against1 + ", actually looking at" + LookBehaviorUtils.getSelectedBlock()); System.out.println("Trying to look at " + against1 + ", actually looking at" + LookBehaviorUtils.getSelectedBlock());
@@ -147,33 +146,32 @@ public class MovementTraverse extends Movement {
} }
state.setInput(InputOverrideHandler.Input.SNEAK, true); state.setInput(InputOverrideHandler.Input.SNEAK, true);
if (whereAmI.equals(dest)) { if (whereAmI.equals(dest)) {
//if we are in the block that we are trying to get to, we are sneaking over air and we need to place a block beneath us against the one we just walked off of // if we are in the block that we are trying to get to, we are sneaking over air and we need to place a block beneath us against the one we just walked off of
//Out.log(from + " " + to + " " + faceX + "," + faceY + "," + faceZ + " " + whereAmI); // Out.log(from + " " + to + " " + faceX + "," + faceY + "," + faceZ + " " + whereAmI);
if (!MovementHelper.switchtothrowaway()) {//get ready to place a throwaway block if (!MovementHelper.switchtothrowaway()) {// get ready to place a throwaway block
displayChatMessageRaw("bb pls get me some blocks. dirt or cobble"); displayChatMessageRaw("bb pls get me some blocks. dirt or cobble");
return state; return state;
} }
double faceX = (dest.getX() + src.getX() + 1.0D) * 0.5D; double faceX = (dest.getX() + src.getX() + 1.0D) * 0.5D;
double faceY = (dest.getY() + src.getY() - 1.0D) * 0.5D; double faceY = (dest.getY() + src.getY() - 1.0D) * 0.5D;
double faceZ = (dest.getZ() + src.getZ() + 1.0D) * 0.5D; double faceZ = (dest.getZ() + src.getZ() + 1.0D) * 0.5D;
//faceX,faceY,faceZ is the middle of the face between from and to // faceX, faceY, faceZ is the middle of the face between from and to
BlockPos goalLook = src.down();//this is the block we were just standing on, and the one we want to place against BlockPos goalLook = src.down(); // this is the block we were just standing on, and the one we want to place against
state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ), playerRotations()))); state.setTarget(new MovementState.MovementTarget(Utils.calcRotationFromVec3d(playerHead(), new Vec3d(faceX, faceY, faceZ), playerRotations())));
state.setInput(InputOverrideHandler.Input.MOVE_BACK, true); state.setInput(InputOverrideHandler.Input.MOVE_BACK, true);
state.setInput(InputOverrideHandler.Input.SNEAK, true); state.setInput(InputOverrideHandler.Input.SNEAK, true);
if (Objects.equals(LookBehaviorUtils.getSelectedBlock().orElse(null), goalLook)) { if (Objects.equals(LookBehaviorUtils.getSelectedBlock().orElse(null), goalLook)) {
state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true);//wait to right click until we are able to place state.setInput(InputOverrideHandler.Input.CLICK_RIGHT, true); // wait to right click until we are able to place
return state; return state;
} }
//Out.log("Trying to look at " + goalLook + ", actually looking at" + Baritone.whatAreYouLookingAt()); // Out.log("Trying to look at " + goalLook + ", actually looking at" + Baritone.whatAreYouLookingAt());
return state; return state;
} else { } else {
moveTowards(positionsToBreak[0]); moveTowards(positionsToBreak[0]);
return state; return state;
//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 // 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
} }
} }
} }
} }