Create `ITickableAimProcessor`

This commit is contained in:
Brady 2023-06-22 20:35:22 -05:00
parent 0682e63707
commit bfae100cb9
No known key found for this signature in database
GPG Key ID: 73A788379A197567
4 changed files with 88 additions and 34 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}

View File

@ -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();