baritone/src/main/java/baritone/bot/pathing/movement/Movement.java

192 lines
6.5 KiB
Java
Raw Normal View History

2018-08-02 21:28:35 +00:00
package baritone.bot.pathing.movement;
2018-08-02 04:07:55 +00:00
2018-08-02 08:15:51 +00:00
import baritone.bot.Baritone;
2018-08-06 00:37:42 +00:00
import baritone.bot.behavior.impl.LookBehavior;
import baritone.bot.behavior.impl.LookBehaviorUtils;
2018-08-02 21:28:35 +00:00
import baritone.bot.pathing.movement.MovementState.MovementStatus;
2018-08-04 22:17:53 +00:00
import baritone.bot.utils.BlockStateInterface;
2018-08-02 14:59:51 +00:00
import baritone.bot.utils.Helper;
2018-08-02 22:35:36 +00:00
import baritone.bot.utils.ToolSet;
import baritone.bot.utils.*;
import net.minecraft.util.math.BlockPos;
2018-08-02 14:59:51 +00:00
import net.minecraft.util.math.Vec3d;
2018-08-02 22:35:36 +00:00
import java.util.Optional;
2018-08-06 00:48:02 +00:00
import static baritone.bot.InputOverrideHandler.Input;
2018-08-06 00:37:42 +00:00
2018-08-05 03:01:38 +00:00
public abstract class Movement implements Helper, MovementHelper {
2018-08-04 22:17:53 +00:00
private MovementState currentState = new MovementState().setStatus(MovementStatus.PREPPING);
2018-08-02 18:12:56 +00:00
protected final BlockPos src;
2018-08-02 18:12:56 +00:00
protected final BlockPos dest;
2018-08-03 03:30:35 +00:00
/**
* The positions that need to be broken before this movement can ensue
*/
public final BlockPos[] positionsToBreak;
2018-08-03 03:30:35 +00:00
/**
* The positions where we need to place a block before this movement can ensue
*/
public final BlockPos[] positionsToPlace;
2018-08-06 00:27:45 +00:00
private Double cost;
2018-08-03 03:30:35 +00:00
protected Movement(BlockPos src, BlockPos dest, BlockPos[] toBreak, BlockPos[] toPlace) {
2018-08-02 22:35:36 +00:00
this.src = src;
this.dest = dest;
2018-08-03 03:30:35 +00:00
this.positionsToBreak = toBreak;
this.positionsToPlace = toPlace;
2018-08-02 14:59:51 +00:00
}
2018-08-03 03:30:35 +00:00
protected Movement(BlockPos src, BlockPos dest, BlockPos[] toBreak, BlockPos[] toPlace, Vec3d rotationTarget) {
this(src, dest, toBreak, toPlace);
}
2018-08-06 00:27:45 +00:00
public double getCost(ToolSet ts) {
if (cost == null) {
if (ts == null) {
ts = new ToolSet();
}
cost = calculateCost(ts);
}
return cost;
}
public double getTotalHardnessOfBlocksToBreak(ToolSet ts) {
/*
double sum = 0;
HashSet<BlockPos> toBreak = new HashSet();
for (BlockPos positionsToBreak1 : positionsToBreak) {
toBreak.add(positionsToBreak1);
if (this instanceof ActionFall) {//if we are digging straight down, assume we have already broken the sand above us
continue;
}
BlockPos tmp = positionsToBreak1.up();
while (canFall(tmp)) {
toBreak.add(tmp);
tmp = tmp.up();
}
}
for (BlockPos pos : toBreak) {
sum += getHardness(ts, Baritone.get(pos), pos);
if (sum >= COST_INF) {
return COST_INF;
}
}
if (!Baritone.allowBreakOrPlace || !Baritone.hasThrowaway) {
for (int i = 0; i < blocksToPlace.length; i++) {
if (!canWalkOn(positionsToPlace[i])) {
return COST_INF;
}
}
}*/
//^ the above implementation properly deals with falling blocks, TODO integrate
double sum = 0;
for (BlockPos pos : positionsToBreak) {
sum += MovementHelper.getMiningDurationTicks(ts, BlockStateInterface.get(pos), pos);
if (sum >= COST_INF) {
return COST_INF;
}
}
return sum;
}
protected abstract double calculateCost(ToolSet ts); // TODO pass in information like whether it's allowed to place throwaway blocks
public double recalculateCost() {
cost = null;
return getCost(null);
}
2018-08-05 03:01:38 +00:00
/**
* Handles the execution of the latest Movement
* State, and offers a Status to the calling class.
*
* @return Status
*/
2018-08-04 18:49:52 +00:00
public MovementStatus update() {
2018-08-04 22:17:53 +00:00
MovementState latestState = updateState(currentState);
2018-08-06 00:37:42 +00:00
latestState.getTarget().rotation.ifPresent(LookBehavior.INSTANCE::updateTarget);
2018-08-02 22:35:36 +00:00
//TODO calculate movement inputs from latestState.getGoal().position
2018-08-06 01:52:55 +00:00
//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) -> {
Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced);
System.out.println(input + " AND " + forced);
});
2018-08-06 01:17:54 +00:00
latestState.inputState.replaceAll((input, forced) -> false);
2018-08-02 08:15:51 +00:00
currentState = latestState;
2018-08-02 14:01:34 +00:00
if (isFinished())
2018-08-06 01:17:54 +00:00
onFinish(latestState);
2018-08-04 22:17:53 +00:00
return currentState.getStatus();
2018-08-02 08:15:51 +00:00
}
2018-08-06 00:48:02 +00:00
2018-08-06 00:37:42 +00:00
private boolean prepared(MovementState state) {
2018-08-06 00:27:45 +00:00
if (state.getStatus() == MovementStatus.WAITING) {
2018-08-04 22:17:53 +00:00
return true;
}
for(BlockPos blockPos : positionsToBreak) {
if(!MovementHelper.canWalkThrough(blockPos, BlockStateInterface.get(blockPos))) {
Optional<Rotation> reachable = LookBehaviorUtils.reachable(blockPos);
2018-08-06 00:37:42 +00:00
reachable.ifPresent(rotation -> {
2018-08-06 01:17:54 +00:00
state.setTarget(new MovementState.MovementTarget(Optional.empty(), reachable))
.setInput(Input.CLICK_LEFT, true);
2018-08-06 00:37:42 +00:00
});
if (reachable.isPresent())
return false;
2018-08-04 22:17:53 +00:00
}
2018-08-04 18:49:52 +00:00
}
2018-08-04 22:17:53 +00:00
return true;
2018-08-04 18:49:52 +00:00
}
2018-08-02 08:15:51 +00:00
public boolean isFinished() {
2018-08-02 21:28:35 +00:00
return (currentState.getStatus() != MovementStatus.RUNNING
2018-08-06 00:37:42 +00:00
&& currentState.getStatus() != MovementStatus.PREPPING
2018-08-02 21:28:35 +00:00
&& currentState.getStatus() != MovementStatus.WAITING);
}
2018-08-02 18:05:47 +00:00
public BlockPos getSrc() {
2018-08-02 18:12:56 +00:00
return src;
2018-08-02 18:05:47 +00:00
}
public BlockPos getDest() {
2018-08-02 18:12:56 +00:00
return dest;
2018-08-02 18:05:47 +00:00
}
2018-08-03 03:40:27 +00:00
/**
2018-08-06 01:17:54 +00:00
* Run cleanup on state finish and declare success.
2018-08-03 03:40:27 +00:00
*/
2018-08-06 01:17:54 +00:00
public void onFinish(MovementState state) {
2018-08-06 01:52:55 +00:00
state.inputState.replaceAll((input, forced) -> false);
state.inputState.forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
2018-08-06 01:17:54 +00:00
state.setStatus(MovementStatus.SUCCESS);
}
2018-08-03 03:40:27 +00:00
2018-08-06 02:08:23 +00:00
public void cancel() {
currentState.inputState.replaceAll((input, forced) -> false);
currentState.inputState.forEach((input, forced) -> Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced));
currentState.setStatus(MovementStatus.CANCELED);
}
/**
2018-08-02 21:28:35 +00:00
* Calculate latest movement state.
* Gets called once a tick.
*
* @return
*/
2018-08-04 18:49:52 +00:00
public MovementState updateState(MovementState state) {
2018-08-06 00:48:02 +00:00
if (!prepared(state))
2018-08-04 18:49:52 +00:00
return state.setStatus(MovementStatus.PREPPING);
2018-08-06 00:48:02 +00:00
else if (state.getStatus() == MovementStatus.PREPPING) {
state.setStatus(MovementStatus.WAITING);
2018-08-06 00:37:42 +00:00
}
2018-08-04 18:49:52 +00:00
return state;
}
2018-08-02 04:07:55 +00:00
}