diff --git a/src/main/java/baritone/bot/pathing/actions/Action.java b/src/main/java/baritone/bot/pathing/action/Action.java similarity index 59% rename from src/main/java/baritone/bot/pathing/actions/Action.java rename to src/main/java/baritone/bot/pathing/action/Action.java index 2c810ddf..fe78a2dd 100644 --- a/src/main/java/baritone/bot/pathing/actions/Action.java +++ b/src/main/java/baritone/bot/pathing/action/Action.java @@ -1,29 +1,31 @@ -package baritone.bot.pathing.actions; +package baritone.bot.pathing.action; +import baritone.bot.Baritone; import baritone.bot.behavior.Behavior; import baritone.bot.utils.Utils; import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; +import baritone.bot.pathing.action.ActionState.ActionStatus; public abstract class Action extends Behavior { protected ActionState currentState; - Action(BlockPos dest) { + public Action(BlockPos dest) { BlockPos playerEyePos = new BlockPos(player.posX, player.posY + player.getEyeHeight(), player.posZ); Tuple desiredRotation = Utils.calcRotationFromCoords( Utils.calcCenterFromCoords(dest, world), playerEyePos); // There's honestly not a good reason for this (Builder Pattern), I just believed strongly in it - currentState = new ActionState().setDesiredMovement(dest) - .setDesiredLook(desiredRotation) - .setFinished(false); + currentState = new ActionState() + .setGoal(new ActionState.ActionGoal(dest, desiredRotation)) + .setStatus(ActionStatus.WAITING); } /** * Lowest denominator of the dynamic costs. - * TODO: Investigate performant ways to assign costs to actions + * TODO: Investigate performant ways to assign costs to action * * @return Cost */ @@ -32,9 +34,20 @@ public abstract class Action extends Behavior { @Override public void onTick() { ActionState latestState = calcState(); - currentState = latestState; player.setPositionAndRotation(player.posX, player.posY, player.posZ, - latestState.desiredRotation.getFirst(), latestState.desiredRotation.getSecond()); + latestState.getGoal().rotation.getFirst(), latestState.getGoal().rotation.getSecond()); + latestState.inputState.forEach((input, forced) -> { + Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced); + }); + currentState = latestState; + + if(isFinished()) + return; + } + + public boolean isFinished() { + return(currentState.getStatus() != ActionStatus.RUNNING + && currentState.getStatus() != ActionStatus.WAITING); } /** diff --git a/src/main/java/baritone/bot/pathing/actions/ActionCosts.java b/src/main/java/baritone/bot/pathing/action/ActionCosts.java similarity index 97% rename from src/main/java/baritone/bot/pathing/actions/ActionCosts.java rename to src/main/java/baritone/bot/pathing/action/ActionCosts.java index f2f6dc60..16e9d24d 100644 --- a/src/main/java/baritone/bot/pathing/actions/ActionCosts.java +++ b/src/main/java/baritone/bot/pathing/action/ActionCosts.java @@ -1,4 +1,4 @@ -package baritone.bot.pathing.actions; +package baritone.bot.pathing.action; public interface ActionCosts { /** diff --git a/src/main/java/baritone/bot/pathing/action/ActionState.java b/src/main/java/baritone/bot/pathing/action/ActionState.java new file mode 100644 index 00000000..03abd4ff --- /dev/null +++ b/src/main/java/baritone/bot/pathing/action/ActionState.java @@ -0,0 +1,63 @@ +package baritone.bot.pathing.action; + +import baritone.bot.InputOverrideHandler.Input; +import baritone.bot.utils.DefaultHashMap; +import net.minecraft.util.Tuple; +import net.minecraft.util.math.BlockPos; + +import java.util.Map; + +public class ActionState { + + protected ActionStatus status; + public ActionGoal goal; + protected final Map inputState = new DefaultHashMap<>(false); + + public ActionState setStatus(ActionStatus status) { + this.status = status; + return this; + } + + public ActionStatus getStatus() { + return status; + } + + public static class ActionGoal { + /** + * Necessary movement to achieve + *

+ * TODO: Decide desiredMovement type + */ + protected BlockPos position; + /** + * Yaw and pitch angles that must be matched + *

+ * getFirst() -> YAW + * getSecond() -> PITCH + */ + protected Tuple rotation; + + public ActionGoal(BlockPos position, Tuple rotation) { + this.position = position; + this.rotation = rotation; + } + } + + public ActionGoal getGoal() { + return goal; + } + + public ActionState setGoal(ActionGoal goal) { + this.goal = goal; + return this; + } + + public ActionState setInput(Input input, boolean forced) { + this.inputState.put(input, forced); + return this; + } + + public enum ActionStatus { + WAITING, RUNNING, SUCCESS, UNREACHABLE, FAILED; + } +} diff --git a/src/main/java/baritone/bot/pathing/actions/ActionAscend.java b/src/main/java/baritone/bot/pathing/action/actions/ActionAscend.java similarity index 62% rename from src/main/java/baritone/bot/pathing/actions/ActionAscend.java rename to src/main/java/baritone/bot/pathing/action/actions/ActionAscend.java index 4de837f0..b44e1478 100644 --- a/src/main/java/baritone/bot/pathing/actions/ActionAscend.java +++ b/src/main/java/baritone/bot/pathing/action/actions/ActionAscend.java @@ -1,10 +1,9 @@ -package baritone.bot.pathing.actions; +package baritone.bot.pathing.action.actions; -import net.minecraft.client.entity.EntityPlayerSP; +import baritone.bot.pathing.action.Action; +import baritone.bot.pathing.action.ActionState; import net.minecraft.util.math.BlockPos; -import java.util.Optional; - public class ActionAscend extends Action { ActionAscend(BlockPos dest) { diff --git a/src/main/java/baritone/bot/pathing/actions/ActionState.java b/src/main/java/baritone/bot/pathing/actions/ActionState.java deleted file mode 100644 index 1f067818..00000000 --- a/src/main/java/baritone/bot/pathing/actions/ActionState.java +++ /dev/null @@ -1,54 +0,0 @@ -package baritone.bot.pathing.actions; - -import net.minecraft.util.Tuple; -import net.minecraft.util.math.BlockPos; - -public class ActionState { - - /** - * Is Action finished? - */ - protected boolean finished; - - public boolean isFinished() { - return finished; - } - - public ActionState setFinished(boolean finished) { - this.finished = finished; - return this; - } - - /** - * Necessary movement to achieve - * - * TODO: Decide desiredMovement type - */ - protected BlockPos desiredMovement; - - public BlockPos getDesiredMovement() { - return desiredMovement; - } - - public ActionState setDesiredMovement(BlockPos desiredMovement) { - this.desiredMovement = desiredMovement; - return this; - } - - /** - * Yaw and pitch angles that must be matched - * - * getFirst() -> YAW - * getSecond() -> PITCH - */ - protected Tuple desiredRotation; - - public Tuple getDesiredRotation() { - return desiredRotation; - } - - public ActionState setDesiredLook(Tuple desiredRotation) { - this.desiredRotation = desiredRotation; - return this; - } -} diff --git a/src/main/java/baritone/bot/pathing/goals/Goal.java b/src/main/java/baritone/bot/pathing/goals/Goal.java index 2d9063dd..da76606b 100644 --- a/src/main/java/baritone/bot/pathing/goals/Goal.java +++ b/src/main/java/baritone/bot/pathing/goals/Goal.java @@ -1,6 +1,6 @@ package baritone.bot.pathing.goals; -import baritone.bot.pathing.actions.ActionCosts; +import baritone.bot.pathing.action.ActionCosts; import net.minecraft.util.math.BlockPos; /** diff --git a/src/main/java/baritone/bot/pathing/movements/Movement.java b/src/main/java/baritone/bot/pathing/movements/Movement.java index 4c0ae47c..c739102b 100644 --- a/src/main/java/baritone/bot/pathing/movements/Movement.java +++ b/src/main/java/baritone/bot/pathing/movements/Movement.java @@ -1,6 +1,6 @@ package baritone.bot.pathing.movements; -import baritone.bot.pathing.actions.Action; +import baritone.bot.pathing.action.Action; import net.minecraft.util.math.BlockPos; import java.util.List; @@ -8,7 +8,7 @@ import java.util.List; public abstract class Movement { /** - * Flat list of ordered actions + * Flat list of ordered action */ protected List actions; diff --git a/src/main/java/baritone/bot/utils/DefaultHashMap.java b/src/main/java/baritone/bot/utils/DefaultHashMap.java index 4e0d9073..f5cdb3ef 100644 --- a/src/main/java/baritone/bot/utils/DefaultHashMap.java +++ b/src/main/java/baritone/bot/utils/DefaultHashMap.java @@ -2,11 +2,14 @@ package baritone.bot.utils; import java.util.HashMap; -public class DefaultHashMap extends HashMap { +public class DefaultHashMap extends HashMap { + protected V defaultValue; + public DefaultHashMap(V defaultValue) { this.defaultValue = defaultValue; } + @Override public V get(Object k) { return containsKey(k) ? super.get(k) : defaultValue; diff --git a/src/main/resources/baritone/pathfinding/Path.java b/src/main/resources/baritone/pathfinding/Path.java index 0f92bb92..46291b03 100644 --- a/src/main/resources/baritone/pathfinding/Path.java +++ b/src/main/resources/baritone/pathfinding/Path.java @@ -57,7 +57,7 @@ public class Path { } path.add(0, start.pos); /*Out.log("Final path: " + path); - Out.log("Final actions: " + actions); + Out.log("Final action: " + action); for (int i = 0; i < path.size() - 1; i++) {//print it all out int oldX = path.get(i).getX(); int oldY = path.get(i).getY(); @@ -68,7 +68,7 @@ public class Path { int xDiff = newX - oldX; int yDiff = newY - oldY; int zDiff = newZ - oldZ; - Out.log(actions.get(i) + ": " + xDiff + "," + yDiff + "," + zDiff);//print it all out + Out.log(action.get(i) + ": " + xDiff + "," + yDiff + "," + zDiff);//print it all out }*/ } @@ -96,7 +96,7 @@ public class Path { } } /** - * Where are we in the path? This is an index in the actions list + * Where are we in the path? This is an index in the action list */ int pathPosition = 0; @@ -201,7 +201,7 @@ public class Path { if ((actions.get(pathPosition) instanceof ActionBridge) && (actions.get(pathPosition + 1) instanceof ActionBridge)) { ActionBridge curr = (ActionBridge) actions.get(pathPosition); ActionBridge next = (ActionBridge) actions.get(pathPosition + 1); - if (curr.dx() != next.dx() || curr.dz() != next.dz()) {//two actions are not parallel, so this is a right angle + if (curr.dx() != next.dx() || curr.dz() != next.dz()) {//two action are not parallel, so this is a right angle if (curr.amIGood() && next.amIGood()) {//nothing in the way BlockPos cornerToCut1 = new BlockPos(next.to.getX() - next.from.getX() + curr.from.getX(), next.to.getY(), next.to.getZ() - next.from.getZ() + curr.from.getZ()); BlockPos cornerToCut2 = cornerToCut1.up(); diff --git a/src/main/resources/baritone/pathfinding/PathFinder.java b/src/main/resources/baritone/pathfinding/PathFinder.java index 7c17cfe3..0d2ba85f 100644 --- a/src/main/resources/baritone/pathfinding/PathFinder.java +++ b/src/main/resources/baritone/pathfinding/PathFinder.java @@ -97,7 +97,7 @@ public class PathFinder { return new Path(startNode, currentNode, goal, numNodes); } //long constructStart = System.nanoTime(); - Action[] possibleActions = getConnectedPositions(currentNodePos);//actions that we could take that start at myPos, in random order + Action[] possibleActions = getConnectedPositions(currentNodePos);//action that we could take that start at myPos, in random order shuffle(possibleActions); //long constructEnd = System.nanoTime(); //System.out.println(constructEnd - constructStart);