Skip to content

Commit f1e5c44

Browse files
committed
Replace messy Vec2 with a cleaner record class. Also, remove a messy switch case.
1 parent 7b5ba80 commit f1e5c44

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

src/main/java/baritone/pathing/movement/MovementHelper.java

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -668,48 +668,39 @@ static void moveTowardsWithoutRotation(IPlayerContext ctx, MovementState state,
668668
Rotation blockRotation = RotationUtils.calcRotationFromVec3d(ctx.playerHead(),
669669
VecUtils.getBlockPosCenter(dest),
670670
ctx.playerRotations());
671-
int selection = getSelection(blockRotation, ax, az);
672-
switch (selection) {
673-
case 0 -> state.setInput(Input.MOVE_FORWARD, true);
674-
case 1 -> state.setInput(Input.MOVE_BACK, true);
675-
case 2 -> state.setInput(Input.MOVE_LEFT, true);
676-
case 3 -> state.setInput(Input.MOVE_RIGHT, true);
677-
case 4 -> state.setInput(Input.MOVE_FORWARD, true).setInput(Input.MOVE_LEFT, true);
678-
case 5 -> state.setInput(Input.MOVE_FORWARD, true).setInput(Input.MOVE_RIGHT, true);
679-
case 6 -> state.setInput(Input.MOVE_BACK, true).setInput(Input.MOVE_LEFT, true);
680-
case 7 -> state.setInput(Input.MOVE_BACK, true).setInput(Input.MOVE_RIGHT, true);
681-
default -> {}
682-
}
671+
MovementOption selection = getSelection(blockRotation, ax, az);
672+
selection.setInputs(state);
683673
}
684674

685-
private static int getSelection(Rotation blockRotation, float ax, float az) {
675+
private static MovementOption getSelection(Rotation blockRotation, float ax, float az) {
686676
float targetAx = Mth.sin(blockRotation.getYaw() * DEG_TO_RAD_F);
687677
float targetAz = Mth.cos(blockRotation.getYaw() * DEG_TO_RAD_F);
688-
Vec2[] options = getOptions(ax, az);
689-
int selection = -1;
678+
MovementOption[] options = getOptions(ax, az);
679+
MovementOption selection = null;
690680
float closestX = 100000;
691681
float closestZ = 100000;
692-
for (int i = 0; i < options.length; i++) {
693-
if (Mth.abs(targetAx - options[i].x) + Mth.abs(targetAz - options[i].y) < closestX + closestZ) {
694-
closestX = Math.abs(targetAx - options[i].x);
695-
closestZ = Math.abs(targetAz - options[i].y);
696-
selection = i;
682+
for (MovementOption option : options) {
683+
if (Mth.abs(targetAx - option.motionX()) + Mth.abs(targetAz - option.motionZ()) < closestX + closestZ) {
684+
closestX = Math.abs(targetAx - option.motionX());
685+
closestZ = Math.abs(targetAz - option.motionZ());
686+
selection = option;
697687
}
698688
}
699689
return selection;
700690
}
701691

702-
private static Vec2[] getOptions(float ax, float az) {
692+
private static MovementOption[] getOptions(float ax, float az) {
703693
boolean canSprint = Baritone.settings().allowSprint.value;
704-
return new Vec2[]{
705-
new Vec2(canSprint ? ax * 1.3f : ax, canSprint ? az * 1.3f : az), // W
706-
new Vec2(-ax, -az), // S
707-
new Vec2(-az, ax), // A
708-
new Vec2(az, -ax), // D
709-
new Vec2((canSprint ? ax * 1.3f : ax) - az, (canSprint ? az * 1.3f : az) + ax), // W+A
710-
new Vec2((canSprint ? ax * 1.3f : ax) + az, (canSprint ? az * 1.3f : az) - ax), // W+D
711-
new Vec2(-ax - az, -az + ax), // S+A
712-
new Vec2(-ax + az, -az - ax) // S+D
694+
695+
return new MovementOption[]{
696+
new MovementOption(Input.MOVE_FORWARD, canSprint ? ax * 1.3f : ax, canSprint ? az * 1.3f : az),
697+
new MovementOption(Input.MOVE_BACK, -ax, -az),
698+
new MovementOption(Input.MOVE_LEFT, -az, ax),
699+
new MovementOption(Input.MOVE_RIGHT, az, -ax),
700+
new MovementOption(Input.MOVE_FORWARD, Input.MOVE_LEFT, (canSprint ? ax * 1.3f : ax) - az, (canSprint ? az * 1.3f : az) + ax),
701+
new MovementOption(Input.MOVE_FORWARD, Input.MOVE_RIGHT, (canSprint ? ax * 1.3f : ax) + az, (canSprint ? az * 1.3f : az) - ax),
702+
new MovementOption(Input.MOVE_BACK, Input.MOVE_LEFT, -ax - az, -az + ax),
703+
new MovementOption(Input.MOVE_BACK, Input.MOVE_RIGHT, -ax + az, -az - ax),
713704
};
714705
}
715706

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of Baritone.
3+
*
4+
* Baritone is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Baritone is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package baritone.pathing.movement;
19+
20+
import baritone.api.utils.input.Input;
21+
22+
public record MovementOption(Input input1, Input input2, float motionX, float motionZ) {
23+
24+
public MovementOption(Input input1, float motionX, float motionZ) {
25+
this(input1, null, motionX, motionZ);
26+
}
27+
28+
public void setInputs(MovementState movementState) {
29+
if (input1 != null) {
30+
movementState.setInput(input1, true);
31+
}
32+
if (input2 != null) {
33+
movementState.setInput(input2, true);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)