From bfae100cb91b28ab41a4378997cbbc32d85f5217 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 22 Jun 2023 20:35:22 -0500 Subject: [PATCH] Create `ITickableAimProcessor` --- .../baritone/api/behavior/ILookBehavior.java | 9 ++-- .../api/behavior/look/IAimProcessor.java | 15 +++--- .../behavior/look/ITickableAimProcessor.java | 47 +++++++++++++++++ .../java/baritone/behavior/LookBehavior.java | 51 +++++++++++-------- 4 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 src/api/java/baritone/api/behavior/look/ITickableAimProcessor.java diff --git a/src/api/java/baritone/api/behavior/ILookBehavior.java b/src/api/java/baritone/api/behavior/ILookBehavior.java index eb7c992b8..d78e7f8b3 100644 --- a/src/api/java/baritone/api/behavior/ILookBehavior.java +++ b/src/api/java/baritone/api/behavior/ILookBehavior.java @@ -40,14 +40,11 @@ public interface ILookBehavior extends IBehavior { void updateTarget(Rotation rotation, boolean blockInteract); /** - * The aim processor instance for this {@link ILookBehavior}, which is responsible for applying additional, deterministic - * transformations to the target rotation set by {@link #updateTarget}. Whenever {@link IAimProcessor#nextRotation(Rotation)} - * is called on the instance returned by this method, the returned value always reflects what would happen in the - * upcoming tick. In other words, it is a pure function, and no internal state changes. If simulation of the - * rotation states beyond the next tick is required, then a {@link IAimProcessor#fork(int) fork} should be created. + * The aim processor instance for this {@link ILookBehavior}, which is responsible for applying additional, + * deterministic transformations to the target rotation set by {@link #updateTarget}. * * @return The aim processor - * @see IAimProcessor#fork(int) + * @see IAimProcessor#fork */ IAimProcessor getAimProcessor(); } diff --git a/src/api/java/baritone/api/behavior/look/IAimProcessor.java b/src/api/java/baritone/api/behavior/look/IAimProcessor.java index 72a4c681d..c7c60f413 100644 --- a/src/api/java/baritone/api/behavior/look/IAimProcessor.java +++ b/src/api/java/baritone/api/behavior/look/IAimProcessor.java @@ -25,20 +25,21 @@ import baritone.api.utils.Rotation; public interface IAimProcessor { /** - * Returns the actual rotation that will be used when the desired rotation is requested. This is not guaranteed to - * return the same value for a given input. + * Returns the actual rotation that will be used when the desired rotation is requested. The returned rotation + * always reflects what would happen in the upcoming tick. In other words, it is a pure function, and no internal + * state changes. If simulation of the rotation states beyond the next tick is required, then a + * {@link IAimProcessor#fork fork} should be created. * * @param desired The desired rotation to set * @return The actual rotation */ - Rotation nextRotation(Rotation desired); + Rotation peekRotation(Rotation desired); /** - * Returns a copy of this {@link IAimProcessor} which has its own internal state and updates on each call to - * {@link #nextRotation(Rotation)}. + * Returns a copy of this {@link IAimProcessor} which has its own internal state and is manually tickable. * - * @param ticksAdvanced The number of ticks to advance ahead of time * @return The forked processor + * @see ITickableAimProcessor */ - IAimProcessor fork(int ticksAdvanced); + ITickableAimProcessor fork(); } diff --git a/src/api/java/baritone/api/behavior/look/ITickableAimProcessor.java b/src/api/java/baritone/api/behavior/look/ITickableAimProcessor.java new file mode 100644 index 000000000..e0a07ae57 --- /dev/null +++ b/src/api/java/baritone/api/behavior/look/ITickableAimProcessor.java @@ -0,0 +1,47 @@ +/* + * 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 . + */ + +package baritone.api.behavior.look; + +import baritone.api.utils.Rotation; + +/** + * @author Brady + */ +public interface ITickableAimProcessor extends IAimProcessor { + + /** + * Advances the internal state of this aim processor by a single tick. + */ + void tick(); + + /** + * Calls {@link #tick()} the specified number of times. + * + * @param ticks The number of calls + */ + void advance(int ticks); + + /** + * Returns the actual rotation as provided by {@link #peekRotation(Rotation)}, and then automatically advances the + * internal state by one {@link #tick() tick}. + * + * @param rotation The desired rotation to set + * @return The actual rotation + */ + Rotation nextRotation(Rotation rotation); +} diff --git a/src/main/java/baritone/behavior/LookBehavior.java b/src/main/java/baritone/behavior/LookBehavior.java index 57dfd206c..4ea0274d7 100644 --- a/src/main/java/baritone/behavior/LookBehavior.java +++ b/src/main/java/baritone/behavior/LookBehavior.java @@ -21,6 +21,7 @@ import baritone.Baritone; import baritone.api.Settings; import baritone.api.behavior.ILookBehavior; import baritone.api.behavior.look.IAimProcessor; +import baritone.api.behavior.look.ITickableAimProcessor; import baritone.api.event.events.*; import baritone.api.utils.IPlayerContext; import baritone.api.utils.Rotation; @@ -68,7 +69,6 @@ public final class LookBehavior extends Behavior implements ILookBehavior { @Override public void onTick(TickEvent event) { if (event.getType() == TickEvent.Type.IN) { - // Unlike forked AimProcessors, the root one needs to be manually updated each game tick this.processor.tick(); } } @@ -88,7 +88,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { this.prevRotation = new Rotation(ctx.player().rotationYaw, ctx.player().rotationPitch); } - final Rotation actual = this.processor.nextRotation(this.target.rotation); + final Rotation actual = this.processor.peekRotation(this.target.rotation); ctx.player().rotationYaw = actual.getYaw(); ctx.player().rotationPitch = actual.getPitch(); break; @@ -129,7 +129,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { public void pig() { if (this.target != null) { - final Rotation actual = this.processor.nextRotation(this.target.rotation); + final Rotation actual = this.processor.peekRotation(this.target.rotation); ctx.player().rotationYaw = actual.getYaw(); } } @@ -145,7 +145,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { @Override public void onPlayerRotationMove(RotationMoveEvent event) { if (this.target != null) { - final Rotation actual = this.processor.nextRotation(this.target.rotation); + final Rotation actual = this.processor.peekRotation(this.target.rotation); event.setYaw(actual.getYaw()); event.setPitch(actual.getPitch()); } @@ -164,7 +164,7 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } } - private static abstract class AbstractAimProcessor implements IAimProcessor { + private static abstract class AbstractAimProcessor implements ITickableAimProcessor { protected final IPlayerContext ctx; private final ForkableRandom rand; @@ -183,13 +183,8 @@ public final class LookBehavior extends Behavior implements ILookBehavior { this.randomPitchOffset = source.randomPitchOffset; } - public void tick() { - this.randomYawOffset = (this.rand.nextDouble() - 0.5) * Baritone.settings().randomLooking.value; - this.randomPitchOffset = (this.rand.nextDouble() - 0.5) * Baritone.settings().randomLooking.value; - } - @Override - public Rotation nextRotation(final Rotation rotation) { + public final Rotation peekRotation(final Rotation rotation) { final Rotation prev = this.getPrevRotation(); float desiredYaw = rotation.getYaw(); @@ -211,16 +206,34 @@ public final class LookBehavior extends Behavior implements ILookBehavior { } @Override - public final IAimProcessor fork(final int ticksAdvanced) { - final AbstractAimProcessor fork = new AbstractAimProcessor(this) { + public final void tick() { + this.randomYawOffset = (this.rand.nextDouble() - 0.5) * Baritone.settings().randomLooking.value; + this.randomPitchOffset = (this.rand.nextDouble() - 0.5) * Baritone.settings().randomLooking.value; + } + + @Override + public final void advance(int ticks) { + for (int i = 0; i < ticks; i++) { + this.tick(); + } + } + + @Override + public Rotation nextRotation(final Rotation rotation) { + final Rotation actual = this.peekRotation(rotation); + this.tick(); + return actual; + } + + @Override + public final ITickableAimProcessor fork() { + return new AbstractAimProcessor(this) { private Rotation prev = AbstractAimProcessor.this.getPrevRotation(); @Override - public Rotation nextRotation(Rotation rotation) { - final Rotation actual = super.nextRotation(rotation); - this.tick(); - return (this.prev = actual); + public Rotation nextRotation(final Rotation rotation) { + return (this.prev = super.nextRotation(rotation)); } @Override @@ -228,10 +241,6 @@ public final class LookBehavior extends Behavior implements ILookBehavior { return this.prev; } }; - for (int i = 0; i < ticksAdvanced; i++) { - fork.tick(); - } - return fork; } protected abstract Rotation getPrevRotation();