path stuff
This commit is contained in:
parent
a60b398b0c
commit
8c8ce9cd5e
38
src/main/java/baritone/bot/behavior/PathExecution.java
Normal file
38
src/main/java/baritone/bot/behavior/PathExecution.java
Normal file
@ -0,0 +1,38 @@
|
||||
package baritone.bot.behavior;
|
||||
|
||||
import baritone.bot.pathing.IPath;
|
||||
import baritone.bot.pathing.movements.Movement;
|
||||
import baritone.bot.utils.Helper;
|
||||
import net.minecraft.util.Tuple;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 8/1/2018 5:38 PM
|
||||
*/
|
||||
public class PathExecution extends Behavior implements Helper {
|
||||
private static final double MAX_DIST_FROM_PATH = 2;
|
||||
private final IPath path;
|
||||
|
||||
public PathExecution(IPath path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public void onTick() {
|
||||
BlockPos playerFeet = playerFeet();
|
||||
// TODO copy logic from Path in resources
|
||||
if (path.isInPath(playerFeet)) {
|
||||
// TODO the old Path used to keep track of the index in the path
|
||||
// and only increment it when the movement said it was done, not when it detected that the player feet had
|
||||
// moved into the next position
|
||||
Movement movement = path.subsequentMovement(playerFeet);
|
||||
//movement.tick()
|
||||
} else {
|
||||
Tuple<Double, BlockPos> closest = path.closestPathPos(player.posX, player.posY, player.posZ);
|
||||
if (closest.getFirst() > MAX_DIST_FROM_PATH) {
|
||||
// TODO how to indicate failure? Exception?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package baritone.bot.behavior;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 8/1/2018 5:38 PM
|
||||
*/
|
||||
public class PathFinding {
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
package baritone.bot.pathing;
|
||||
|
||||
import baritone.bot.pathing.movements.Movement;
|
||||
import baritone.bot.utils.Utils;
|
||||
import net.minecraft.util.Tuple;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -7,9 +10,12 @@ import java.util.List;
|
||||
|
||||
public interface IPath {
|
||||
/**
|
||||
*
|
||||
* Ordered list of movements to carry out.
|
||||
* movements.get(i).getSrc() should equal positions.get(i)
|
||||
* movements.get(i).getDest() should equal positions.get(i+1)
|
||||
* movements.size() should equal positions.size()-1
|
||||
*/
|
||||
List<BlockPos> movements();
|
||||
List<Movement> movements();
|
||||
|
||||
/**
|
||||
* All positions along the way.
|
||||
@ -17,6 +23,44 @@ public interface IPath {
|
||||
*/
|
||||
List<BlockPos> positions();
|
||||
|
||||
/**
|
||||
* What's the next step
|
||||
*
|
||||
* @param currentPosition the current position
|
||||
* @return
|
||||
*/
|
||||
default Movement subsequentMovement(BlockPos currentPosition) {
|
||||
List<BlockPos> pos = positions();
|
||||
List<Movement> moves = movements();
|
||||
for (int i = 0; i < pos.size(); i++) {
|
||||
if (currentPosition.equals(pos.get(i))) {
|
||||
return moves.get(i);
|
||||
}
|
||||
}
|
||||
throw new UnsupportedOperationException(currentPosition + " not in path");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param currentPosition
|
||||
* @return
|
||||
*/
|
||||
default boolean isInPath(BlockPos currentPosition) {
|
||||
return positions().stream().anyMatch(pos -> currentPosition.equals(pos));
|
||||
}
|
||||
|
||||
default Tuple<Double, BlockPos> closestPathPos(double x, double y, double z) {
|
||||
double best = -1;
|
||||
BlockPos bestPos = null;
|
||||
for (BlockPos pos : positions()) {
|
||||
double dist = Utils.distanceToCenter(pos, x, y, z);
|
||||
if (dist < best || best == -1) {
|
||||
best = dist;
|
||||
bestPos = pos;
|
||||
}
|
||||
}
|
||||
return new Tuple<>(best, bestPos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Where does this path start
|
||||
*/
|
||||
@ -29,12 +73,14 @@ public interface IPath {
|
||||
|
||||
/**
|
||||
* For rendering purposes, what blocks should be highlighted in red
|
||||
*
|
||||
* @return an unordered collection of positions
|
||||
*/
|
||||
Collection<BlockPos> getBlocksToMine();
|
||||
|
||||
/**
|
||||
* For rendering purposes, what blocks should be highlighted in green
|
||||
*
|
||||
* @return an unordered collection of positions
|
||||
*/
|
||||
Collection<BlockPos> getBlocksToPlace();
|
||||
|
@ -2,10 +2,10 @@ package baritone.bot.pathing.action;
|
||||
|
||||
import baritone.bot.Baritone;
|
||||
import baritone.bot.behavior.Behavior;
|
||||
import baritone.bot.pathing.action.ActionState.ActionStatus;
|
||||
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 {
|
||||
|
||||
@ -29,7 +29,9 @@ public abstract class Action extends Behavior {
|
||||
*
|
||||
* @return Cost
|
||||
*/
|
||||
public double cost() { return 0; }
|
||||
public double cost() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick() {
|
||||
@ -41,13 +43,13 @@ public abstract class Action extends Behavior {
|
||||
});
|
||||
currentState = latestState;
|
||||
|
||||
if(isFinished())
|
||||
if (isFinished())
|
||||
return;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return(currentState.getStatus() != ActionStatus.RUNNING
|
||||
&& currentState.getStatus() != ActionStatus.WAITING);
|
||||
return (currentState.getStatus() != ActionStatus.RUNNING
|
||||
&& currentState.getStatus() != ActionStatus.WAITING);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,6 +3,7 @@ package baritone.bot.utils;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.entity.EntityPlayerSP;
|
||||
import net.minecraft.client.multiplayer.WorldClient;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
@ -14,4 +15,8 @@ public interface Helper {
|
||||
EntityPlayerSP player = mc.player;
|
||||
WorldClient world = mc.world;
|
||||
|
||||
default BlockPos playerFeet() {
|
||||
return new BlockPos(player.posX, player.posY, player.posZ);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -30,4 +30,11 @@ public final class Utils {
|
||||
orig.getY() + yDiff,
|
||||
orig.getZ() + zDiff);
|
||||
}
|
||||
|
||||
public static double distanceToCenter(BlockPos pos, double x, double y, double z) {
|
||||
double xdiff = x - (pos.getX() + 0.5D);
|
||||
double ydiff = y - (pos.getY() + 0.5D);
|
||||
double zdiff = z - (pos.getZ() + 0.5D);
|
||||
return Math.sqrt(xdiff * xdiff + ydiff * ydiff + zdiff * zdiff);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user