Make Action more robust

This commit is contained in:
Howard Stark 2018-08-02 10:59:51 -04:00
parent c86ee0f1bc
commit 36d3ab9e1d
No known key found for this signature in database
GPG Key ID: 9FA4E350B33067F3
4 changed files with 40 additions and 22 deletions

View File

@ -1,25 +1,33 @@
package baritone.bot.pathing.action;
import baritone.bot.Baritone;
import baritone.bot.behavior.Behavior;
import baritone.bot.pathing.action.ActionState.ActionStatus;
import baritone.bot.event.AbstractGameEventListener;
import baritone.bot.utils.Helper;
import baritone.bot.utils.Utils;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos;
import baritone.bot.pathing.action.ActionState.ActionStatus;
import net.minecraft.util.math.Vec3d;
public abstract class Action extends Behavior {
public abstract class Action implements AbstractGameEventListener, Helper {
protected ActionState currentState;
public Action(BlockPos dest) {
BlockPos playerEyePos = new BlockPos(player.posX, player.posY + player.getEyeHeight(), player.posZ);
Tuple<Float, Float> desiredRotation = Utils.calcRotationFromCoords(
Utils.calcCenterFromCoords(dest, world),
playerEyePos);
this(Utils.calcCenterFromCoords(dest, mc.world));
}
// There's honestly not a good reason for this (Builder Pattern), I just believed strongly in it
public Action(Vec3d dest) {
this(dest, dest);
}
public Action(BlockPos dest, Vec3d rotationTarget) {
this(Utils.calcCenterFromCoords(dest, mc.world), rotationTarget);
}
public Action(Vec3d dest, Vec3d rotationTarget) {
currentState = new ActionState()
.setGoal(new ActionState.ActionGoal(dest, desiredRotation))
.setGoal(new ActionState.ActionGoal(dest, rotationTarget))
.setStatus(ActionStatus.WAITING);
}
@ -36,8 +44,10 @@ public abstract class Action extends Behavior {
@Override
public void onTick() {
ActionState latestState = calcState();
player.setPositionAndRotation(player.posX, player.posY, player.posZ,
latestState.getGoal().rotation.getFirst(), latestState.getGoal().rotation.getSecond());
Tuple<Float, Float> rotation = Utils.calcRotationFromVec3d(mc.player.getPositionEyes(1.0F),
latestState.getGoal().rotation);
player.setPositionAndRotation(mc.player.posX, mc.player.posY, mc.player.posZ,
rotation.getFirst(), rotation.getSecond());
latestState.inputState.forEach((input, forced) -> {
Baritone.INSTANCE.getInputOverrideHandler().setInputForceState(input, forced);
});

View File

@ -1,8 +1,7 @@
package baritone.bot.pathing.action;
import baritone.bot.InputOverrideHandler.Input;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import java.util.HashMap;
import java.util.Map;
@ -28,16 +27,16 @@ public class ActionState {
* <p>
* TODO: Decide desiredMovement type
*/
public BlockPos position;
public Vec3d position;
/**
* Yaw and pitch angles that must be matched
* <p>
* getFirst() -> YAW
* getSecond() -> PITCH
*/
public Tuple<Float, Float> rotation;
public Vec3d rotation;
public ActionGoal(BlockPos position, Tuple<Float, Float> rotation) {
public ActionGoal(Vec3d position, Vec3d rotation) {
this.position = position;
this.rotation = rotation;
}

View File

@ -14,7 +14,7 @@ public class ActionAscend extends Action {
@Override
public ActionState calcState() {
ActionState latestState = currentState.setInput(InputOverrideHandler.Input.JUMP,true).setInput(InputOverrideHandler.Input.MOVE_FORWARD, true);
if(player.getPosition().equals(latestState.getGoal().position))
if(mc.player.getPosition().equals(latestState.getGoal().position))
latestState.setStatus(ActionState.ActionStatus.SUCCESS);
return latestState;
}

View File

@ -4,6 +4,7 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
/**
@ -13,24 +14,32 @@ import net.minecraft.world.World;
public final class Utils {
public static Tuple<Float, Float> calcRotationFromCoords(BlockPos orig, BlockPos dest) {
double yaw = Math.atan2(orig.getX() - dest.getX(), -orig.getZ() + dest.getZ());
double dist = Math.sqrt((orig.getX() - dest.getX()) * (orig.getX() - dest.getX()) + (-orig.getZ() + dest.getZ()) * (-orig.getZ() + dest.getZ()));
double pitch = Math.atan2(orig.getY() - dest.getY(), dist);
return calcRotationFromVec3d(vec3dFromBlockPos(orig), vec3dFromBlockPos(dest));
}
public static Tuple<Float, Float> calcRotationFromVec3d(Vec3d orig, Vec3d dest) {
double yaw = Math.atan2(orig.x - dest.x, -orig.z + dest.z);
double dist = Math.sqrt((orig.x - dest.x) * (orig.x - dest.x) + (-orig.x + dest.x) * (-orig.z + dest.z));
double pitch = Math.atan2(orig.y - dest.y, dist);
return new Tuple<>((float) (yaw * 180 / Math.PI),
(float) (pitch * 180 / Math.PI));
}
public static BlockPos calcCenterFromCoords(BlockPos orig, World world) {
public static Vec3d calcCenterFromCoords(BlockPos orig, World world) {
IBlockState b = world.getBlockState(orig);
AxisAlignedBB bbox = b.getBoundingBox(world, orig);
double xDiff = (bbox.minX + bbox.maxX) / 2;
double yDiff = (bbox.minY + bbox.maxY) / 2;
double zDiff = (bbox.minZ + bbox.maxZ) / 2;
return new BlockPos(orig.getX() + xDiff,
return new Vec3d(orig.getX() + xDiff,
orig.getY() + yDiff,
orig.getZ() + zDiff);
}
public static Vec3d vec3dFromBlockPos(BlockPos orig) {
return new Vec3d(orig.getX() + 0.0D, orig.getY() + 0.0D, orig.getZ() + 0.0D);
}
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);