Replace messy Vec2 with a cleaner record class. Also, remove a messy switch case.

This commit is contained in:
Murat65536
2025-08-17 19:52:14 -04:00
parent 7b5ba80f67
commit f1e5c44b85
2 changed files with 57 additions and 30 deletions

View File

@@ -668,48 +668,39 @@ public interface MovementHelper extends ActionCosts, Helper {
Rotation blockRotation = RotationUtils.calcRotationFromVec3d(ctx.playerHead(), Rotation blockRotation = RotationUtils.calcRotationFromVec3d(ctx.playerHead(),
VecUtils.getBlockPosCenter(dest), VecUtils.getBlockPosCenter(dest),
ctx.playerRotations()); ctx.playerRotations());
int selection = getSelection(blockRotation, ax, az); MovementOption selection = getSelection(blockRotation, ax, az);
switch (selection) { selection.setInputs(state);
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 -> {}
}
} }
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 targetAx = Mth.sin(blockRotation.getYaw() * DEG_TO_RAD_F);
float targetAz = Mth.cos(blockRotation.getYaw() * DEG_TO_RAD_F); float targetAz = Mth.cos(blockRotation.getYaw() * DEG_TO_RAD_F);
Vec2[] options = getOptions(ax, az); MovementOption[] options = getOptions(ax, az);
int selection = -1; MovementOption selection = null;
float closestX = 100000; float closestX = 100000;
float closestZ = 100000; float closestZ = 100000;
for (int i = 0; i < options.length; i++) { for (MovementOption option : options) {
if (Mth.abs(targetAx - options[i].x) + Mth.abs(targetAz - options[i].y) < closestX + closestZ) { if (Mth.abs(targetAx - option.motionX()) + Mth.abs(targetAz - option.motionZ()) < closestX + closestZ) {
closestX = Math.abs(targetAx - options[i].x); closestX = Math.abs(targetAx - option.motionX());
closestZ = Math.abs(targetAz - options[i].y); closestZ = Math.abs(targetAz - option.motionZ());
selection = i; selection = option;
} }
} }
return selection; return selection;
} }
private static Vec2[] getOptions(float ax, float az) { private static MovementOption[] getOptions(float ax, float az) {
boolean canSprint = Baritone.settings().allowSprint.value; boolean canSprint = Baritone.settings().allowSprint.value;
return new Vec2[]{
new Vec2(canSprint ? ax * 1.3f : ax, canSprint ? az * 1.3f : az), // W return new MovementOption[]{
new Vec2(-ax, -az), // S new MovementOption(Input.MOVE_FORWARD, canSprint ? ax * 1.3f : ax, canSprint ? az * 1.3f : az),
new Vec2(-az, ax), // A new MovementOption(Input.MOVE_BACK, -ax, -az),
new Vec2(az, -ax), // D new MovementOption(Input.MOVE_LEFT, -az, ax),
new Vec2((canSprint ? ax * 1.3f : ax) - az, (canSprint ? az * 1.3f : az) + ax), // W+A new MovementOption(Input.MOVE_RIGHT, az, -ax),
new Vec2((canSprint ? ax * 1.3f : ax) + az, (canSprint ? az * 1.3f : az) - ax), // W+D new MovementOption(Input.MOVE_FORWARD, Input.MOVE_LEFT, (canSprint ? ax * 1.3f : ax) - az, (canSprint ? az * 1.3f : az) + ax),
new Vec2(-ax - az, -az + ax), // S+A new MovementOption(Input.MOVE_FORWARD, Input.MOVE_RIGHT, (canSprint ? ax * 1.3f : ax) + az, (canSprint ? az * 1.3f : az) - ax),
new Vec2(-ax + az, -az - ax) // S+D new MovementOption(Input.MOVE_BACK, Input.MOVE_LEFT, -ax - az, -az + ax),
new MovementOption(Input.MOVE_BACK, Input.MOVE_RIGHT, -ax + az, -az - ax),
}; };
} }

View 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);
}
}
}