Add features to Action scaffolding

This commit is contained in:
Howard Stark 2018-08-02 04:15:51 -04:00
parent cb238ec130
commit f0488b24dd
No known key found for this signature in database
GPG Key ID: 9FA4E350B33067F3
10 changed files with 100 additions and 76 deletions

View File

@ -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.behavior.Behavior;
import baritone.bot.utils.Utils; import baritone.bot.utils.Utils;
import net.minecraft.util.Tuple; import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import baritone.bot.pathing.action.ActionState.ActionStatus;
public abstract class Action extends Behavior { public abstract class Action extends Behavior {
protected ActionState currentState; protected ActionState currentState;
Action(BlockPos dest) { public Action(BlockPos dest) {
BlockPos playerEyePos = new BlockPos(player.posX, player.posY + player.getEyeHeight(), player.posZ); BlockPos playerEyePos = new BlockPos(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Tuple<Float, Float> desiredRotation = Utils.calcRotationFromCoords( Tuple<Float, Float> desiredRotation = Utils.calcRotationFromCoords(
Utils.calcCenterFromCoords(dest, world), Utils.calcCenterFromCoords(dest, world),
playerEyePos); playerEyePos);
// There's honestly not a good reason for this (Builder Pattern), I just believed strongly in it // There's honestly not a good reason for this (Builder Pattern), I just believed strongly in it
currentState = new ActionState().setDesiredMovement(dest) currentState = new ActionState()
.setDesiredLook(desiredRotation) .setGoal(new ActionState.ActionGoal(dest, desiredRotation))
.setFinished(false); .setStatus(ActionStatus.WAITING);
} }
/** /**
* Lowest denominator of the dynamic costs. * 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 * @return Cost
*/ */
@ -32,9 +34,20 @@ public abstract class Action extends Behavior {
@Override @Override
public void onTick() { public void onTick() {
ActionState latestState = calcState(); ActionState latestState = calcState();
currentState = latestState;
player.setPositionAndRotation(player.posX, player.posY, player.posZ, 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);
} }
/** /**

View File

@ -1,4 +1,4 @@
package baritone.bot.pathing.actions; package baritone.bot.pathing.action;
public interface ActionCosts { public interface ActionCosts {
/** /**

View File

@ -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<Input, Boolean> 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
* <p>
* TODO: Decide desiredMovement type
*/
protected BlockPos position;
/**
* Yaw and pitch angles that must be matched
* <p>
* getFirst() -> YAW
* getSecond() -> PITCH
*/
protected Tuple<Float, Float> rotation;
public ActionGoal(BlockPos position, Tuple<Float, Float> 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;
}
}

View File

@ -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 net.minecraft.util.math.BlockPos;
import java.util.Optional;
public class ActionAscend extends Action { public class ActionAscend extends Action {
ActionAscend(BlockPos dest) { ActionAscend(BlockPos dest) {

View File

@ -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<Float, Float> desiredRotation;
public Tuple<Float, Float> getDesiredRotation() {
return desiredRotation;
}
public ActionState setDesiredLook(Tuple<Float, Float> desiredRotation) {
this.desiredRotation = desiredRotation;
return this;
}
}

View File

@ -1,6 +1,6 @@
package baritone.bot.pathing.goals; package baritone.bot.pathing.goals;
import baritone.bot.pathing.actions.ActionCosts; import baritone.bot.pathing.action.ActionCosts;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
/** /**

View File

@ -1,6 +1,6 @@
package baritone.bot.pathing.movements; package baritone.bot.pathing.movements;
import baritone.bot.pathing.actions.Action; import baritone.bot.pathing.action.Action;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.List; import java.util.List;
@ -8,7 +8,7 @@ import java.util.List;
public abstract class Movement { public abstract class Movement {
/** /**
* Flat list of ordered actions * Flat list of ordered action
*/ */
protected List<Action> actions; protected List<Action> actions;

View File

@ -2,11 +2,14 @@ package baritone.bot.utils;
import java.util.HashMap; import java.util.HashMap;
public class DefaultHashMap<K,V> extends HashMap<K,V> { public class DefaultHashMap<K, V> extends HashMap<K, V> {
protected V defaultValue; protected V defaultValue;
public DefaultHashMap(V defaultValue) { public DefaultHashMap(V defaultValue) {
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
@Override @Override
public V get(Object k) { public V get(Object k) {
return containsKey(k) ? super.get(k) : defaultValue; return containsKey(k) ? super.get(k) : defaultValue;

View File

@ -57,7 +57,7 @@ public class Path {
} }
path.add(0, start.pos); path.add(0, start.pos);
/*Out.log("Final path: " + path); /*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 for (int i = 0; i < path.size() - 1; i++) {//print it all out
int oldX = path.get(i).getX(); int oldX = path.get(i).getX();
int oldY = path.get(i).getY(); int oldY = path.get(i).getY();
@ -68,7 +68,7 @@ public class Path {
int xDiff = newX - oldX; int xDiff = newX - oldX;
int yDiff = newY - oldY; int yDiff = newY - oldY;
int zDiff = newZ - oldZ; 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; int pathPosition = 0;
@ -201,7 +201,7 @@ public class Path {
if ((actions.get(pathPosition) instanceof ActionBridge) && (actions.get(pathPosition + 1) instanceof ActionBridge)) { if ((actions.get(pathPosition) instanceof ActionBridge) && (actions.get(pathPosition + 1) instanceof ActionBridge)) {
ActionBridge curr = (ActionBridge) actions.get(pathPosition); ActionBridge curr = (ActionBridge) actions.get(pathPosition);
ActionBridge next = (ActionBridge) actions.get(pathPosition + 1); 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 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 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(); BlockPos cornerToCut2 = cornerToCut1.up();

View File

@ -97,7 +97,7 @@ public class PathFinder {
return new Path(startNode, currentNode, goal, numNodes); return new Path(startNode, currentNode, goal, numNodes);
} }
//long constructStart = System.nanoTime(); //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); shuffle(possibleActions);
//long constructEnd = System.nanoTime(); //long constructEnd = System.nanoTime();
//System.out.println(constructEnd - constructStart); //System.out.println(constructEnd - constructStart);