Skip to content

Commit d196c64

Browse files
authored
Merge pull request #4752 from Murat65536/bridging-improvements
Bridging Improvements
2 parents 2a3c224 + a56f3cf commit d196c64

File tree

4 files changed

+119
-6
lines changed

4 files changed

+119
-6
lines changed

src/api/java/baritone/api/utils/Rotation.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ public static float normalizeYaw(float yaw) {
159159
return newYaw;
160160
}
161161

162+
/**
163+
* Gets the distance between a starting yaw and an offset yaw.
164+
* Distance can be negative if the offset yaw is behind of the starting yaw.
165+
*
166+
* @param yaw The initial yaw
167+
* @param offsetYaw The offset yaw
168+
* @return The distance between the yaws
169+
*/
170+
public static float yawDistanceFromOffset(float yaw, float offsetYaw) {
171+
if ((yaw > 0 ^ offsetYaw > 0) && ((yaw > 90 || yaw < -90) ^ (offsetYaw > 90 || offsetYaw < -90))) {
172+
if (yaw < 0) {
173+
return 360 + (yaw - offsetYaw);
174+
} else {
175+
return 360 - (yaw - offsetYaw);
176+
}
177+
} else {
178+
return yaw - offsetYaw;
179+
}
180+
}
181+
162182
@Override
163183
public String toString() {
164184
return "Yaw: " + yaw + ", Pitch: " + pitch;

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import baritone.utils.ToolSet;
3232
import net.minecraft.core.BlockPos;
3333
import net.minecraft.core.Direction;
34+
import net.minecraft.util.Mth;
3435
import net.minecraft.world.item.enchantment.EnchantmentHelper;
3536
import net.minecraft.world.level.block.*;
3637
import net.minecraft.world.level.block.piston.MovingPistonBlock;
@@ -50,10 +51,9 @@
5051
import net.minecraft.world.phys.HitResult;
5152
import net.minecraft.world.phys.Vec3;
5253

53-
import java.util.ArrayList;
54-
import java.util.List;
55-
import java.util.Optional;
54+
import java.util.*;
5655

56+
import static baritone.api.utils.RotationUtils.DEG_TO_RAD_F;
5757
import static baritone.pathing.movement.Movement.HORIZONTALS_BUT_ALSO_DOWN_____SO_EVERY_DIRECTION_EXCEPT_UP;
5858
import static baritone.pathing.precompute.Ternary.*;
5959

@@ -659,6 +659,43 @@ static void moveTowards(IPlayerContext ctx, MovementState state, BlockPos pos) {
659659
)).setInput(Input.MOVE_FORWARD, true);
660660
}
661661

662+
static void moveTowardsWithoutRotation(IPlayerContext ctx, MovementState state, float idealYaw) {
663+
MovementOption.getOptions(
664+
Mth.sin(ctx.playerRotations().getYaw() * DEG_TO_RAD_F),
665+
Mth.cos(ctx.playerRotations().getYaw() * DEG_TO_RAD_F),
666+
Baritone.settings().allowSprint.value
667+
).min(Comparator.comparing(option -> option.distanceToSq(
668+
Mth.sin(idealYaw * DEG_TO_RAD_F),
669+
Mth.cos(idealYaw * DEG_TO_RAD_F)
670+
))).ifPresent(selection -> selection.setInputs(state));
671+
}
672+
673+
static void moveTowardsWithoutRotation(IPlayerContext ctx, MovementState state, BlockPos dest) {
674+
float idealYaw = RotationUtils.calcRotationFromVec3d(
675+
ctx.playerHead(),
676+
VecUtils.getBlockPosCenter(dest),
677+
ctx.playerRotations()
678+
).getYaw();
679+
moveTowardsWithoutRotation(ctx, state, idealYaw);
680+
}
681+
682+
static void moveTowardsWithSlightRotation(IPlayerContext ctx, MovementState state, BlockPos dest) {
683+
float idealYaw = RotationUtils.calcRotationFromVec3d(
684+
ctx.playerHead(),
685+
VecUtils.getBlockPosCenter(dest),
686+
ctx.playerRotations()
687+
).getYaw();
688+
float distance = Rotation.yawDistanceFromOffset(ctx.playerRotations().getYaw(), idealYaw) % 45f;
689+
float newYaw = distance > 0f ?
690+
distance > 22.5f ? distance - 45f : distance :
691+
distance < -22.5f ? distance + 45f : distance;
692+
state.setTarget(new MovementTarget(new Rotation(
693+
ctx.playerRotations().getYaw() - newYaw,
694+
ctx.playerRotations().getPitch()
695+
), true));
696+
moveTowardsWithoutRotation(ctx, state, idealYaw);
697+
}
698+
662699
/**
663700
* Returns whether or not the specified block is
664701
* water, regardless of whether or not it is flowing.
@@ -778,7 +815,7 @@ static PlaceResult attemptToPlaceABlock(MovementState state, IBaritone baritone,
778815
if (ctx.getSelectedBlock().isPresent()) {
779816
BlockPos selectedBlock = ctx.getSelectedBlock().get();
780817
Direction side = ((BlockHitResult) ctx.objectMouseOver()).getDirection();
781-
// only way for selectedBlock.equals(placeAt) to be true is if it's replacable
818+
// only way for selectedBlock.equals(placeAt) to be true is if it's replaceable
782819
if (selectedBlock.equals(placeAt) || (MovementHelper.canPlaceAgainst(ctx, selectedBlock) && selectedBlock.relative(side).equals(placeAt))) {
783820
if (wouldSneak) {
784821
state.setInput(Input.SNEAK, true);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import net.minecraft.util.Mth;
22+
23+
import java.util.stream.Stream;
24+
25+
public record MovementOption(Input input1, Input input2, float motionX, float motionZ) {
26+
private static final float SPRINT_MULTIPLIER = 1.3f;
27+
28+
public MovementOption(Input input1, float motionX, float motionZ) {
29+
this(input1, null, motionX, motionZ);
30+
}
31+
32+
public void setInputs(MovementState movementState) {
33+
if (input1 != null) {
34+
movementState.setInput(input1, true);
35+
}
36+
if (input2 != null) {
37+
movementState.setInput(input2, true);
38+
}
39+
}
40+
41+
public float distanceToSq(float otherX, float otherZ) {
42+
return Mth.abs(motionX() - otherX) + Mth.abs(motionZ() - otherZ);
43+
}
44+
45+
public static Stream<MovementOption> getOptions(float motionX, float motionZ, boolean canSprint) {
46+
return Stream.of(
47+
new MovementOption(Input.MOVE_FORWARD, canSprint ? motionX * SPRINT_MULTIPLIER : motionX, canSprint ? motionZ * SPRINT_MULTIPLIER : motionZ),
48+
new MovementOption(Input.MOVE_BACK, -motionX, -motionZ),
49+
new MovementOption(Input.MOVE_LEFT, -motionZ, motionX),
50+
new MovementOption(Input.MOVE_RIGHT, motionZ, -motionX),
51+
new MovementOption(Input.MOVE_FORWARD, Input.MOVE_LEFT, (canSprint ? motionX * SPRINT_MULTIPLIER : motionX) - motionZ, (canSprint ? motionZ * SPRINT_MULTIPLIER : motionZ) + motionX),
52+
new MovementOption(Input.MOVE_FORWARD, Input.MOVE_RIGHT, (canSprint ? motionX * SPRINT_MULTIPLIER : motionX) + motionZ, (canSprint ? motionZ * SPRINT_MULTIPLIER : motionZ) - motionX),
53+
new MovementOption(Input.MOVE_BACK, Input.MOVE_LEFT, -motionX - motionZ, -motionZ + motionX),
54+
new MovementOption(Input.MOVE_BACK, Input.MOVE_RIGHT, -motionX + motionZ, -motionZ - motionX)
55+
);
56+
}
57+
}

src/main/java/baritone/pathing/movement/movements/MovementTraverse.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,8 @@ public MovementState updateState(MovementState state) {
356356
}
357357
return state;
358358
}
359-
MovementHelper.moveTowards(ctx, state, positionsToBreak[0]);
359+
MovementHelper.moveTowardsWithSlightRotation(ctx, state, dest);
360360
return state;
361-
// TODO MovementManager.moveTowardsBlock(to); // move towards not look at because if we are bridging for a couple blocks in a row, it is faster if we dont spin around and walk forwards then spin around and place backwards for every block
362361
}
363362
}
364363

0 commit comments

Comments
 (0)