Replace messy Vec2 with a cleaner record class. Also, remove a messy switch case.
This commit is contained in:
@@ -668,48 +668,39 @@ public interface MovementHelper extends ActionCosts, Helper {
|
||||
Rotation blockRotation = RotationUtils.calcRotationFromVec3d(ctx.playerHead(),
|
||||
VecUtils.getBlockPosCenter(dest),
|
||||
ctx.playerRotations());
|
||||
int selection = getSelection(blockRotation, ax, az);
|
||||
switch (selection) {
|
||||
case 0 -> state.setInput(Input.MOVE_FORWARD, true);
|
||||
case 1 -> state.setInput(Input.MOVE_BACK, true);
|
||||
case 2 -> state.setInput(Input.MOVE_LEFT, true);
|
||||
case 3 -> state.setInput(Input.MOVE_RIGHT, true);
|
||||
case 4 -> state.setInput(Input.MOVE_FORWARD, true).setInput(Input.MOVE_LEFT, true);
|
||||
case 5 -> state.setInput(Input.MOVE_FORWARD, true).setInput(Input.MOVE_RIGHT, true);
|
||||
case 6 -> state.setInput(Input.MOVE_BACK, true).setInput(Input.MOVE_LEFT, true);
|
||||
case 7 -> state.setInput(Input.MOVE_BACK, true).setInput(Input.MOVE_RIGHT, true);
|
||||
default -> {}
|
||||
}
|
||||
MovementOption selection = getSelection(blockRotation, ax, az);
|
||||
selection.setInputs(state);
|
||||
}
|
||||
|
||||
private static int getSelection(Rotation blockRotation, float ax, float az) {
|
||||
private static MovementOption getSelection(Rotation blockRotation, float ax, float az) {
|
||||
float targetAx = Mth.sin(blockRotation.getYaw() * DEG_TO_RAD_F);
|
||||
float targetAz = Mth.cos(blockRotation.getYaw() * DEG_TO_RAD_F);
|
||||
Vec2[] options = getOptions(ax, az);
|
||||
int selection = -1;
|
||||
MovementOption[] options = getOptions(ax, az);
|
||||
MovementOption selection = null;
|
||||
float closestX = 100000;
|
||||
float closestZ = 100000;
|
||||
for (int i = 0; i < options.length; i++) {
|
||||
if (Mth.abs(targetAx - options[i].x) + Mth.abs(targetAz - options[i].y) < closestX + closestZ) {
|
||||
closestX = Math.abs(targetAx - options[i].x);
|
||||
closestZ = Math.abs(targetAz - options[i].y);
|
||||
selection = i;
|
||||
for (MovementOption option : options) {
|
||||
if (Mth.abs(targetAx - option.motionX()) + Mth.abs(targetAz - option.motionZ()) < closestX + closestZ) {
|
||||
closestX = Math.abs(targetAx - option.motionX());
|
||||
closestZ = Math.abs(targetAz - option.motionZ());
|
||||
selection = option;
|
||||
}
|
||||
}
|
||||
return selection;
|
||||
}
|
||||
|
||||
private static Vec2[] getOptions(float ax, float az) {
|
||||
private static MovementOption[] getOptions(float ax, float az) {
|
||||
boolean canSprint = Baritone.settings().allowSprint.value;
|
||||
return new Vec2[]{
|
||||
new Vec2(canSprint ? ax * 1.3f : ax, canSprint ? az * 1.3f : az), // W
|
||||
new Vec2(-ax, -az), // S
|
||||
new Vec2(-az, ax), // A
|
||||
new Vec2(az, -ax), // D
|
||||
new Vec2((canSprint ? ax * 1.3f : ax) - az, (canSprint ? az * 1.3f : az) + ax), // W+A
|
||||
new Vec2((canSprint ? ax * 1.3f : ax) + az, (canSprint ? az * 1.3f : az) - ax), // W+D
|
||||
new Vec2(-ax - az, -az + ax), // S+A
|
||||
new Vec2(-ax + az, -az - ax) // S+D
|
||||
|
||||
return new MovementOption[]{
|
||||
new MovementOption(Input.MOVE_FORWARD, canSprint ? ax * 1.3f : ax, canSprint ? az * 1.3f : az),
|
||||
new MovementOption(Input.MOVE_BACK, -ax, -az),
|
||||
new MovementOption(Input.MOVE_LEFT, -az, ax),
|
||||
new MovementOption(Input.MOVE_RIGHT, az, -ax),
|
||||
new MovementOption(Input.MOVE_FORWARD, Input.MOVE_LEFT, (canSprint ? ax * 1.3f : ax) - az, (canSprint ? az * 1.3f : az) + ax),
|
||||
new MovementOption(Input.MOVE_FORWARD, Input.MOVE_RIGHT, (canSprint ? ax * 1.3f : ax) + az, (canSprint ? az * 1.3f : az) - ax),
|
||||
new MovementOption(Input.MOVE_BACK, Input.MOVE_LEFT, -ax - az, -az + ax),
|
||||
new MovementOption(Input.MOVE_BACK, Input.MOVE_RIGHT, -ax + az, -az - ax),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
36
src/main/java/baritone/pathing/movement/MovementOption.java
Normal file
36
src/main/java/baritone/pathing/movement/MovementOption.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package baritone.pathing.movement;
|
||||
|
||||
import baritone.api.utils.input.Input;
|
||||
|
||||
public record MovementOption(Input input1, Input input2, float motionX, float motionZ) {
|
||||
|
||||
public MovementOption(Input input1, float motionX, float motionZ) {
|
||||
this(input1, null, motionX, motionZ);
|
||||
}
|
||||
|
||||
public void setInputs(MovementState movementState) {
|
||||
if (input1 != null) {
|
||||
movementState.setInput(input1, true);
|
||||
}
|
||||
if (input2 != null) {
|
||||
movementState.setInput(input2, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user